| Flutter Windows Embedder |

flutter_windows_texture_registrar.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/flutter_windows_texture_registrar.h"
6
7 #include <mutex>
8
9 #include "flutter/fml/logging.h"
10 #include "flutter/shell/platform/embedder/embedder_struct_macros.h"
11 #include "flutter/shell/platform/windows/external_texture_d3d.h"
12 #include "flutter/shell/platform/windows/external_texture_pixelbuffer.h"
13 #include "flutter/shell/platform/windows/flutter_windows_engine.h"
14
15 namespace {
16 static constexpr int64_t kInvalidTexture = -1;
17 }
18
19 namespace flutter {
20
21 FlutterWindowsTextureRegistrar::FlutterWindowsTextureRegistrar(
22 FlutterWindowsEngine* engine,
23 const GlProcs& gl_procs)
24 : engine_(engine), gl_procs_(gl_procs) {}
25
26 int64_t FlutterWindowsTextureRegistrar::RegisterTexture(
27 const FlutterDesktopTextureInfo* texture_info) {
28 if (!gl_procs_.valid) {
29 return kInvalidTexture;
30 }
31
32 if (texture_info->type == kFlutterDesktopPixelBufferTexture) {
33 if (!texture_info->pixel_buffer_config.callback) {
34 FML_LOG(ERROR) << "Invalid pixel buffer texture callback.";
35 return kInvalidTexture;
36 }
37
38 return EmplaceTexture(std::make_unique<flutter::ExternalTexturePixelBuffer>(
39 texture_info->pixel_buffer_config.callback,
40 texture_info->pixel_buffer_config.user_data, gl_procs_));
41 } else if (texture_info->type == kFlutterDesktopGpuSurfaceTexture) {
42 const FlutterDesktopGpuSurfaceTextureConfig* gpu_surface_config =
43 &texture_info->gpu_surface_config;
44 auto surface_type = SAFE_ACCESS(gpu_surface_config, type,
45 kFlutterDesktopGpuSurfaceTypeNone);
46 if (surface_type == kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle ||
47 surface_type == kFlutterDesktopGpuSurfaceTypeD3d11Texture2D) {
48 auto callback = SAFE_ACCESS(gpu_surface_config, callback, nullptr);
49 if (!callback) {
50 FML_LOG(ERROR) << "Invalid GPU surface descriptor callback.";
51 return kInvalidTexture;
52 }
53
54 auto user_data = SAFE_ACCESS(gpu_surface_config, user_data, nullptr);
55 return EmplaceTexture(std::make_unique<flutter::ExternalTextureD3d>(
56 surface_type, callback, user_data, engine_->surface_manager(),
57 gl_procs_));
58 }
59 }
60
61 FML_LOG(ERROR) << "Attempted to register texture of unsupport type.";
62 return kInvalidTexture;
63 }
64
65 int64_t FlutterWindowsTextureRegistrar::EmplaceTexture(
66 std::unique_ptr<ExternalTexture> texture) {
67 int64_t texture_id = texture->texture_id();
68 {
69 std::lock_guard<std::mutex> lock(map_mutex_);
70 textures_[texture_id] = std::move(texture);
71 }
72
73 engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
74 engine->RegisterExternalTexture(texture_id);
75 });
76
77 return texture_id;
78 }
79
80 void FlutterWindowsTextureRegistrar::UnregisterTexture(int64_t texture_id,
81 fml::closure callback) {
82 engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
83 engine->UnregisterExternalTexture(texture_id);
84 });
85
86 bool posted = engine_->PostRasterThreadTask([this, texture_id, callback]() {
87 {
88 std::lock_guard<std::mutex> lock(map_mutex_);
89 auto it = textures_.find(texture_id);
90 if (it != textures_.end()) {
91 textures_.erase(it);
92 }
93 }
94 if (callback) {
95 callback();
96 }
97 });
98
99 if (!posted && callback) {
100 callback();
101 }
102 }
103
104 bool FlutterWindowsTextureRegistrar::MarkTextureFrameAvailable(
105 int64_t texture_id) {
106 engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
107 engine->MarkExternalTextureFrameAvailable(texture_id);
108 });
109 return true;
110 }
111
112 bool FlutterWindowsTextureRegistrar::PopulateTexture(
113 int64_t texture_id,
114 size_t width,
115 size_t height,
116 FlutterOpenGLTexture* opengl_texture) {
117 flutter::ExternalTexture* texture;
118 {
119 std::lock_guard<std::mutex> lock(map_mutex_);
120 auto it = textures_.find(texture_id);
121 if (it == textures_.end()) {
122 return false;
123 }
124 texture = it->second.get();
125 }
126 return texture->PopulateTexture(width, height, opengl_texture);
127 }
128
129 void FlutterWindowsTextureRegistrar::ResolveGlFunctions(GlProcs& procs) {
130 procs.glGenTextures =
131 reinterpret_cast<glGenTexturesProc>(eglGetProcAddress("glGenTextures"));
132 procs.glDeleteTextures = reinterpret_cast<glDeleteTexturesProc>(
133 eglGetProcAddress("glDeleteTextures"));
134 procs.glBindTexture =
135 reinterpret_cast<glBindTextureProc>(eglGetProcAddress("glBindTexture"));
136 procs.glTexParameteri = reinterpret_cast<glTexParameteriProc>(
137 eglGetProcAddress("glTexParameteri"));
138 procs.glTexImage2D =
139 reinterpret_cast<glTexImage2DProc>(eglGetProcAddress("glTexImage2D"));
140
141 procs.valid = procs.glGenTextures && procs.glDeleteTextures &&
142 procs.glBindTexture && procs.glTexParameteri &&
143 procs.glTexImage2D;
144 }
145
146 }; // namespace flutter
external_texture_pixelbuffer.h
flutter_windows_texture_registrar.h
flutter::GlProcs::glTexParameteri
glTexParameteriProc glTexParameteri
Definition: external_texture.h:34
flutter::TaskRunner::RunNowOrPostTask
void RunNowOrPostTask(TaskClosure task)
Definition: task_runner.h:51
kFlutterDesktopGpuSurfaceTexture
@ kFlutterDesktopGpuSurfaceTexture
Definition: flutter_texture_registrar.h:28
flutter::FlutterWindowsEngine::task_runner
TaskRunner * task_runner()
Definition: flutter_windows_engine.h:134
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:47
flutter::FlutterWindowsTextureRegistrar::MarkTextureFrameAvailable
bool MarkTextureFrameAvailable(int64_t texture_id)
Definition: flutter_windows_texture_registrar.cc:104
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:78
flutter::GlProcs::valid
bool valid
Definition: external_texture.h:36
flutter::glDeleteTexturesProc
void(* glDeleteTexturesProc)(GLsizei n, const GLuint *textures)
Definition: external_texture.h:16
flutter::FlutterWindowsTextureRegistrar::PopulateTexture
bool PopulateTexture(int64_t texture_id, size_t width, size_t height, FlutterOpenGLTexture *texture)
Definition: flutter_windows_texture_registrar.cc:112
FlutterDesktopPixelBufferTextureConfig::user_data
void * user_data
Definition: flutter_texture_registrar.h:134
flutter::glTexImage2DProc
void(* glTexImage2DProc)(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *data)
Definition: external_texture.h:19
kFlutterDesktopGpuSurfaceTypeNone
@ kFlutterDesktopGpuSurfaceTypeNone
Definition: flutter_texture_registrar.h:34
type
enum flutter::testing::@66::KeyboardChange::Type type
flutter::GlProcs::glTexImage2D
glTexImage2DProc glTexImage2D
Definition: external_texture.h:35
FlutterDesktopPixelBufferTextureConfig::callback
FlutterDesktopPixelBufferTextureCallback callback
Definition: flutter_texture_registrar.h:132
flutter::ExternalTexture::PopulateTexture
virtual bool PopulateTexture(size_t width, size_t height, FlutterOpenGLTexture *opengl_texture)=0
flutter::glGenTexturesProc
void(* glGenTexturesProc)(GLsizei n, GLuint *textures)
Definition: external_texture.h:15
FlutterDesktopTextureInfo::type
FlutterDesktopTextureType type
Definition: flutter_texture_registrar.h:152
flutter::glTexParameteriProc
void(* glTexParameteriProc)(GLenum target, GLenum pname, GLint param)
Definition: external_texture.h:18
FlutterDesktopTextureInfo::pixel_buffer_config
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config
Definition: flutter_texture_registrar.h:154
FlutterDesktopTextureInfo
Definition: flutter_texture_registrar.h:151
flutter::GlProcs::glBindTexture
glBindTextureProc glBindTexture
Definition: external_texture.h:33
flutter
Definition: accessibility_bridge_windows.cc:11
kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
@ kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
Definition: flutter_texture_registrar.h:40
FlutterDesktopTextureInfo::gpu_surface_config
FlutterDesktopGpuSurfaceTextureConfig gpu_surface_config
Definition: flutter_texture_registrar.h:155
kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle
@ kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle
Definition: flutter_texture_registrar.h:38
flutter::glBindTextureProc
void(* glBindTextureProc)(GLenum target, GLuint texture)
Definition: external_texture.h:17
flutter::FlutterWindowsEngine::surface_manager
AngleSurfaceManager * surface_manager()
Definition: flutter_windows_engine.h:142
flutter_windows_engine.h
flutter::GlProcs
Definition: external_texture.h:30
flutter::GlProcs::glGenTextures
glGenTexturesProc glGenTextures
Definition: external_texture.h:31
FlutterDesktopGpuSurfaceTextureConfig
Definition: flutter_texture_registrar.h:138
flutter::FlutterWindowsEngine::PostRasterThreadTask
virtual bool PostRasterThreadTask(fml::closure callback)
Definition: flutter_windows_engine.cc:682
flutter::FlutterWindowsTextureRegistrar::ResolveGlFunctions
static void ResolveGlFunctions(GlProcs &gl_procs)
Definition: flutter_windows_texture_registrar.cc:129
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
flutter::FlutterWindowsTextureRegistrar::FlutterWindowsTextureRegistrar
FlutterWindowsTextureRegistrar(FlutterWindowsEngine *engine, const GlProcs &gl_procs)
Definition: flutter_windows_texture_registrar.cc:21
external_texture_d3d.h
flutter::ExternalTexture
Definition: external_texture.h:40
kFlutterDesktopPixelBufferTexture
@ kFlutterDesktopPixelBufferTexture
Definition: flutter_texture_registrar.h:26
flutter::FlutterWindowsTextureRegistrar::UnregisterTexture
void UnregisterTexture(int64_t texture_id, fml::closure callback=nullptr)
Definition: flutter_windows_texture_registrar.cc:80
flutter::GlProcs::glDeleteTextures
glDeleteTexturesProc glDeleteTextures
Definition: external_texture.h:32
flutter::FlutterWindowsTextureRegistrar::RegisterTexture
int64_t RegisterTexture(const FlutterDesktopTextureInfo *texture_info)
Definition: flutter_windows_texture_registrar.cc:26
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:46