Flutter Windows Embedder
external_texture_d3d.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include "flutter/fml/logging.h"
8 #include "flutter/shell/platform/embedder/embedder_struct_macros.h"
9 
10 namespace flutter {
11 
15  void* user_data,
16  const egl::Manager* egl_manager,
17  std::shared_ptr<egl::ProcTable> gl)
18  : type_(type),
19  texture_callback_(texture_callback),
20  user_data_(user_data),
21  egl_manager_(egl_manager),
22  gl_(std::move(gl)) {}
23 
25  ReleaseImage();
26 
27  if (gl_texture_ != 0) {
28  gl_->DeleteTextures(1, &gl_texture_);
29  }
30 }
31 
33  size_t height,
34  FlutterOpenGLTexture* opengl_texture) {
35  const FlutterDesktopGpuSurfaceDescriptor* descriptor =
36  texture_callback_(width, height, user_data_);
37 
38  if (!CreateOrUpdateTexture(descriptor)) {
39  return false;
40  }
41 
42  // Populate the texture object used by the engine.
43  opengl_texture->target = GL_TEXTURE_2D;
44  opengl_texture->name = gl_texture_;
45  opengl_texture->format = GL_RGBA8_OES;
46  opengl_texture->destruction_callback = nullptr;
47  opengl_texture->user_data = nullptr;
48  opengl_texture->width = SAFE_ACCESS(descriptor, visible_width, 0);
49  opengl_texture->height = SAFE_ACCESS(descriptor, visible_height, 0);
50 
51  return true;
52 }
53 
54 void ExternalTextureD3d::ReleaseImage() {
55  if (egl_surface_ != EGL_NO_SURFACE) {
56  eglReleaseTexImage(egl_manager_->egl_display(), egl_surface_,
57  EGL_BACK_BUFFER);
58  eglDestroySurface(egl_manager_->egl_display(), egl_surface_);
59  egl_surface_ = EGL_NO_SURFACE;
60  }
61 }
62 
63 bool ExternalTextureD3d::CreateOrUpdateTexture(
64  const FlutterDesktopGpuSurfaceDescriptor* descriptor) {
65  if (descriptor == nullptr ||
66  SAFE_ACCESS(descriptor, handle, nullptr) == nullptr) {
67  ReleaseImage();
68  return false;
69  }
70 
71  if (gl_texture_ == 0) {
72  gl_->GenTextures(1, &gl_texture_);
73 
74  gl_->BindTexture(GL_TEXTURE_2D, gl_texture_);
75  gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
76  gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
77  gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78  gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79  } else {
80  gl_->BindTexture(GL_TEXTURE_2D, gl_texture_);
81  }
82 
83  auto handle = SAFE_ACCESS(descriptor, handle, nullptr);
84  if (handle != last_surface_handle_) {
85  ReleaseImage();
86 
87  EGLint attributes[] = {
88  EGL_WIDTH,
89  static_cast<EGLint>(SAFE_ACCESS(descriptor, width, 0)),
90  EGL_HEIGHT,
91  static_cast<EGLint>(SAFE_ACCESS(descriptor, height, 0)),
92  EGL_TEXTURE_TARGET,
93  EGL_TEXTURE_2D,
94  EGL_TEXTURE_FORMAT,
95  EGL_TEXTURE_RGBA, // always EGL_TEXTURE_RGBA
96  EGL_NONE};
97 
98  egl_surface_ = egl_manager_->CreateSurfaceFromHandle(
100  ? EGL_D3D_TEXTURE_ANGLE
101  : EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
102  handle, attributes);
103 
104  if (egl_surface_ == EGL_NO_SURFACE ||
105  eglBindTexImage(egl_manager_->egl_display(), egl_surface_,
106  EGL_BACK_BUFFER) == EGL_FALSE) {
107  FML_LOG(ERROR) << "Binding D3D surface failed.";
108  }
109  last_surface_handle_ = handle;
110  }
111 
112  auto release_callback = SAFE_ACCESS(descriptor, release_callback, nullptr);
113  if (release_callback) {
114  release_callback(SAFE_ACCESS(descriptor, release_context, nullptr));
115  }
116  return egl_surface_ != EGL_NO_SURFACE;
117 }
118 
119 } // namespace flutter
flutter::ExternalTextureD3d::ExternalTextureD3d
ExternalTextureD3d(FlutterDesktopGpuSurfaceType type, const FlutterDesktopGpuSurfaceTextureCallback texture_callback, void *user_data, const egl::Manager *egl_manager, std::shared_ptr< egl::ProcTable > gl)
Definition: external_texture_d3d.cc:12
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:53
FlutterDesktopGpuSurfaceType
FlutterDesktopGpuSurfaceType
Definition: flutter_texture_registrar.h:32
flutter::egl::Manager::CreateSurfaceFromHandle
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
Definition: manager.cc:300
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterDesktopGpuSurfaceDescriptor
Definition: flutter_texture_registrar.h:70
flutter::egl::Manager
Definition: manager.h:31
type
enum flutter::testing::@88::KeyboardChange::Type type
kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
@ kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
Definition: flutter_texture_registrar.h:40
flutter::ExternalTextureD3d::~ExternalTextureD3d
virtual ~ExternalTextureD3d()
Definition: external_texture_d3d.cc:24
flutter::ExternalTextureD3d::PopulateTexture
bool PopulateTexture(size_t width, size_t height, FlutterOpenGLTexture *opengl_texture) override
Definition: external_texture_d3d.cc:32
FlutterDesktopGpuSurfaceTextureCallback
const typedef FlutterDesktopGpuSurfaceDescriptor *(* FlutterDesktopGpuSurfaceTextureCallback)(size_t width, size_t height, void *user_data)
Definition: flutter_texture_registrar.h:125
external_texture_d3d.h
flutter::egl::Manager::egl_display
EGLDisplay egl_display() const
Definition: manager.h:63
texture_callback
FlutterDesktopPixelBufferTextureCallback texture_callback
Definition: texture_registrar_unittests.cc:26