| 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
5 #include "flutter/shell/platform/windows/external_texture_d3d.h"
6
7 #include "flutter/fml/logging.h"
8 #include "flutter/shell/platform/embedder/embedder_struct_macros.h"
9
10 namespace flutter {
11
12 ExternalTextureD3d::ExternalTextureD3d(
13 FlutterDesktopGpuSurfaceType type,
14 const FlutterDesktopGpuSurfaceTextureCallback texture_callback,
15 void* user_data,
16 const AngleSurfaceManager* surface_manager,
17 const GlProcs& gl_procs)
18 : type_(type),
19 texture_callback_(texture_callback),
20 user_data_(user_data),
21 surface_manager_(surface_manager),
22 gl_(gl_procs) {}
23
24 ExternalTextureD3d::~ExternalTextureD3d() {
25 ReleaseImage();
26
27 if (gl_texture_ != 0) {
28 gl_.glDeleteTextures(1, &gl_texture_);
29 }
30 }
31
32 bool ExternalTextureD3d::PopulateTexture(size_t width,
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(surface_manager_->egl_display(), egl_surface_,
57 EGL_BACK_BUFFER);
58 eglDestroySurface(surface_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_.glGenTextures(1, &gl_texture_);
73
74 gl_.glBindTexture(GL_TEXTURE_2D, gl_texture_);
75 gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
76 gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
77 gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78 gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79 } else {
80 gl_.glBindTexture(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_ = surface_manager_->CreateSurfaceFromHandle(
99 (type_ == kFlutterDesktopGpuSurfaceTypeD3d11Texture2D)
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(surface_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::AngleSurfaceManager::egl_display
EGLDisplay egl_display() const
Definition: angle_surface_manager.h:90
flutter::GlProcs::glTexParameteri
glTexParameteriProc glTexParameteri
Definition: external_texture.h:34
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:47
type
enum flutter::testing::@66::KeyboardChange::Type type
FlutterDesktopGpuSurfaceType
FlutterDesktopGpuSurfaceType
Definition: flutter_texture_registrar.h:32
flutter::AngleSurfaceManager
Definition: angle_surface_manager.h:28
flutter::GlProcs::glBindTexture
glBindTextureProc glBindTexture
Definition: external_texture.h:33
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterDesktopGpuSurfaceDescriptor
Definition: flutter_texture_registrar.h:70
kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
@ kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
Definition: flutter_texture_registrar.h:40
flutter::ExternalTextureD3d::~ExternalTextureD3d
virtual ~ExternalTextureD3d()
Definition: external_texture_d3d.cc:24
flutter::GlProcs
Definition: external_texture.h:30
flutter::GlProcs::glGenTextures
glGenTexturesProc glGenTextures
Definition: external_texture.h:31
flutter::ExternalTextureD3d::PopulateTexture
bool PopulateTexture(size_t width, size_t height, FlutterOpenGLTexture *opengl_texture) override
Definition: external_texture_d3d.cc:32
flutter::AngleSurfaceManager::CreateSurfaceFromHandle
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
Definition: angle_surface_manager.cc:336
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::GlProcs::glDeleteTextures
glDeleteTexturesProc glDeleteTextures
Definition: external_texture.h:32
flutter::ExternalTextureD3d::ExternalTextureD3d
ExternalTextureD3d(FlutterDesktopGpuSurfaceType type, const FlutterDesktopGpuSurfaceTextureCallback texture_callback, void *user_data, const AngleSurfaceManager *surface_manager, const GlProcs &gl_procs)
Definition: external_texture_d3d.cc:12
texture_callback
FlutterDesktopPixelBufferTextureCallback texture_callback
Definition: texture_registrar_unittests.cc:26