| 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 std::shared_ptr<egl::ProcTable> gl)
24 : engine_(engine), gl_(std::move(gl)) {}
25
26 int64_t FlutterWindowsTextureRegistrar::RegisterTexture(
27 const FlutterDesktopTextureInfo* texture_info) {
28 if (!gl_) {
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_));
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_->egl_manager(), gl_));
57 }
58 }
59
60 FML_LOG(ERROR) << "Attempted to register texture of unsupport type.";
61 return kInvalidTexture;
62 }
63
64 int64_t FlutterWindowsTextureRegistrar::EmplaceTexture(
65 std::unique_ptr<ExternalTexture> texture) {
66 int64_t texture_id = texture->texture_id();
67 {
68 std::lock_guard<std::mutex> lock(map_mutex_);
69 textures_[texture_id] = std::move(texture);
70 }
71
72 engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
73 engine->RegisterExternalTexture(texture_id);
74 });
75
76 return texture_id;
77 }
78
79 void FlutterWindowsTextureRegistrar::UnregisterTexture(int64_t texture_id,
80 fml::closure callback) {
81 engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
82 engine->UnregisterExternalTexture(texture_id);
83 });
84
85 bool posted = engine_->PostRasterThreadTask([this, texture_id, callback]() {
86 {
87 std::lock_guard<std::mutex> lock(map_mutex_);
88 auto it = textures_.find(texture_id);
89 if (it != textures_.end()) {
90 textures_.erase(it);
91 }
92 }
93 if (callback) {
94 callback();
95 }
96 });
97
98 if (!posted && callback) {
99 callback();
100 }
101 }
102
103 bool FlutterWindowsTextureRegistrar::MarkTextureFrameAvailable(
104 int64_t texture_id) {
105 engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
106 engine->MarkExternalTextureFrameAvailable(texture_id);
107 });
108 return true;
109 }
110
111 bool FlutterWindowsTextureRegistrar::PopulateTexture(
112 int64_t texture_id,
113 size_t width,
114 size_t height,
115 FlutterOpenGLTexture* opengl_texture) {
116 flutter::ExternalTexture* texture;
117 {
118 std::lock_guard<std::mutex> lock(map_mutex_);
119 auto it = textures_.find(texture_id);
120 if (it == textures_.end()) {
121 return false;
122 }
123 texture = it->second.get();
124 }
125 return texture->PopulateTexture(width, height, opengl_texture);
126 }
127
128 }; // namespace flutter
flutter::ExternalTexture
Definition: external_texture.h:16
flutter::ExternalTexture::PopulateTexture
virtual bool PopulateTexture(size_t width, size_t height, FlutterOpenGLTexture *opengl_texture)=0
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:90
flutter::FlutterWindowsEngine::task_runner
TaskRunner * task_runner()
Definition: flutter_windows_engine.h:161
flutter::FlutterWindowsEngine::PostRasterThreadTask
virtual bool PostRasterThreadTask(fml::closure callback) const
Definition: flutter_windows_engine.cc:913
flutter::FlutterWindowsEngine::egl_manager
egl::Manager * egl_manager() const
Definition: flutter_windows_engine.h:171
flutter::FlutterWindowsTextureRegistrar::PopulateTexture
bool PopulateTexture(int64_t texture_id, size_t width, size_t height, FlutterOpenGLTexture *texture)
Definition: flutter_windows_texture_registrar.cc:111
flutter::FlutterWindowsTextureRegistrar::RegisterTexture
int64_t RegisterTexture(const FlutterDesktopTextureInfo *texture_info)
Definition: flutter_windows_texture_registrar.cc:26
flutter::FlutterWindowsTextureRegistrar::FlutterWindowsTextureRegistrar
FlutterWindowsTextureRegistrar(FlutterWindowsEngine *engine, std::shared_ptr< egl::ProcTable > gl)
Definition: flutter_windows_texture_registrar.cc:21
flutter::FlutterWindowsTextureRegistrar::UnregisterTexture
void UnregisterTexture(int64_t texture_id, fml::closure callback=nullptr)
Definition: flutter_windows_texture_registrar.cc:79
flutter::FlutterWindowsTextureRegistrar::MarkTextureFrameAvailable
bool MarkTextureFrameAvailable(int64_t texture_id)
Definition: flutter_windows_texture_registrar.cc:103
flutter::TaskRunner::RunNowOrPostTask
void RunNowOrPostTask(TaskClosure task)
Definition: task_runner.h:55
texture_id
uint32_t texture_id
Definition: compositor_opengl.cc:20
external_texture_d3d.h
external_texture_pixelbuffer.h
kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle
@ kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle
Definition: flutter_texture_registrar.h:38
kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
@ kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
Definition: flutter_texture_registrar.h:40
kFlutterDesktopGpuSurfaceTypeNone
@ kFlutterDesktopGpuSurfaceTypeNone
Definition: flutter_texture_registrar.h:34
kFlutterDesktopGpuSurfaceTexture
@ kFlutterDesktopGpuSurfaceTexture
Definition: flutter_texture_registrar.h:28
kFlutterDesktopPixelBufferTexture
@ kFlutterDesktopPixelBufferTexture
Definition: flutter_texture_registrar.h:26
flutter_windows_engine.h
flutter_windows_texture_registrar.h
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:53
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:52
type
enum flutter::testing::@88::KeyboardChange::Type type
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterDesktopGpuSurfaceTextureConfig
Definition: flutter_texture_registrar.h:138
FlutterDesktopPixelBufferTextureConfig::user_data
void * user_data
Definition: flutter_texture_registrar.h:134
FlutterDesktopPixelBufferTextureConfig::callback
FlutterDesktopPixelBufferTextureCallback callback
Definition: flutter_texture_registrar.h:132
FlutterDesktopTextureInfo
Definition: flutter_texture_registrar.h:151
FlutterDesktopTextureInfo::gpu_surface_config
FlutterDesktopGpuSurfaceTextureConfig gpu_surface_config
Definition: flutter_texture_registrar.h:155
FlutterDesktopTextureInfo::type
FlutterDesktopTextureType type
Definition: flutter_texture_registrar.h:152
FlutterDesktopTextureInfo::pixel_buffer_config
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config
Definition: flutter_texture_registrar.h:154