Flutter Engine
The Flutter Engine
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
15namespace {
16static constexpr int64_t kInvalidTexture = -1;
17}
18
19namespace flutter {
20
23 std::shared_ptr<egl::ProcTable> gl)
24 : engine_(engine), gl_(std::move(gl)) {}
25
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,
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
64int64_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
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
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
112 int64_t texture_id,
113 size_t width,
114 size_t height,
115 FlutterOpenGLTexture* opengl_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
GLenum type
virtual bool PostRasterThreadTask(fml::closure callback) const
bool PopulateTexture(int64_t texture_id, size_t width, size_t height, FlutterOpenGLTexture *texture)
int64_t RegisterTexture(const FlutterDesktopTextureInfo *texture_info)
FlutterWindowsTextureRegistrar(FlutterWindowsEngine *engine, std::shared_ptr< egl::ProcTable > gl)
void UnregisterTexture(int64_t texture_id, fml::closure callback=nullptr)
void RunNowOrPostTask(TaskClosure task)
Definition: task_runner.h:51
#define SAFE_ACCESS(pointer, member, default_value)
FlutterEngine engine
Definition: main.cc:68
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
@ kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle
@ kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
@ kFlutterDesktopGpuSurfaceTypeNone
@ kFlutterDesktopGpuSurfaceTexture
@ kFlutterDesktopPixelBufferTexture
#define FML_LOG(severity)
Definition: logging.h:82
FlTexture * texture
std::function< void()> closure
Definition: closure.h:14
gl
Definition: malisc.py:41
Definition: ref_ptr.h:256
int32_t height
int32_t width
FlutterDesktopPixelBufferTextureCallback callback
FlutterDesktopGpuSurfaceTextureConfig gpu_surface_config
FlutterDesktopTextureType type
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config
void * user_data
int64_t texture_id
#define ERROR(message)
Definition: elf_loader.cc:260