Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit eb031d3

Browse files
authored
Add windows plugin texture support (#19405)
1 parent 3f0cf18 commit eb031d3

31 files changed

+1138
-21
lines changed

ci/licenses_golden/licenses_flutter

100755100644
+8
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/
894894
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_codec_serializer.h
895895
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h
896896
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h
897+
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/texture_registrar.h
897898
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_call_unittests.cc
898899
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc
899900
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_result_functions_unittests.cc
@@ -902,6 +903,8 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar
902903
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc
903904
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_message_codec_unittests.cc
904905
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_method_codec_unittests.cc
906+
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/texture_registrar_impl.h
907+
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/texture_registrar_unittests.cc
905908
FILE: ../../../flutter/shell/platform/common/cpp/engine_switches.cc
906909
FILE: ../../../flutter/shell/platform/common/cpp/engine_switches.h
907910
FILE: ../../../flutter/shell/platform/common/cpp/engine_switches_unittests.cc
@@ -1441,13 +1444,18 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plu
14411444
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
14421445
FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc
14431446
FILE: ../../../flutter/shell/platform/windows/cursor_handler.h
1447+
FILE: ../../../flutter/shell/platform/windows/external_texture_gl.cc
1448+
FILE: ../../../flutter/shell/platform/windows/external_texture_gl.h
14441449
FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle.cc
14451450
FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle.h
14461451
FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle_unittests.cc
14471452
FILE: ../../../flutter/shell/platform/windows/flutter_windows.cc
14481453
FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine.cc
14491454
FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine.h
14501455
FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine_unittests.cc
1456+
FILE: ../../../flutter/shell/platform/windows/flutter_windows_texture_registrar.cc
1457+
FILE: ../../../flutter/shell/platform/windows/flutter_windows_texture_registrar.h
1458+
FILE: ../../../flutter/shell/platform/windows/flutter_windows_texture_registrar_unittests.cc
14511459
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.cc
14521460
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.h
14531461
FILE: ../../../flutter/shell/platform/windows/flutter_windows_win32.cc

shell/platform/common/cpp/client_wrapper/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ executable("client_wrapper_unittests") {
5151
"standard_method_codec_unittests.cc",
5252
"testing/test_codec_extensions.cc",
5353
"testing/test_codec_extensions.h",
54+
"texture_registrar_unittests.cc",
5455
]
5556

5657
deps = [

shell/platform/common/cpp/client_wrapper/core_implementations.cc

+42
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
#include <cassert>
1717
#include <iostream>
18+
#include <variant>
1819

1920
#include "binary_messenger_impl.h"
2021
#include "include/flutter/engine_method_result.h"
22+
#include "texture_registrar_impl.h"
2123

2224
namespace flutter {
2325

@@ -146,4 +148,44 @@ void ReplyManager::SendResponseData(const std::vector<uint8_t>* data) {
146148

147149
} // namespace internal
148150

151+
// ========== texture_registrar_impl.h ==========
152+
153+
TextureRegistrarImpl::TextureRegistrarImpl(
154+
FlutterDesktopTextureRegistrarRef texture_registrar_ref)
155+
: texture_registrar_ref_(texture_registrar_ref) {}
156+
157+
TextureRegistrarImpl::~TextureRegistrarImpl() = default;
158+
159+
int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) {
160+
if (auto pixel_buffer_texture = std::get_if<PixelBufferTexture>(texture)) {
161+
FlutterDesktopTextureInfo info = {};
162+
info.type = kFlutterDesktopPixelBufferTexture;
163+
info.pixel_buffer_config.user_data = pixel_buffer_texture;
164+
info.pixel_buffer_config.callback =
165+
[](size_t width, size_t height,
166+
void* user_data) -> const FlutterDesktopPixelBuffer* {
167+
auto texture = static_cast<PixelBufferTexture*>(user_data);
168+
auto buffer = texture->CopyPixelBuffer(width, height);
169+
return buffer;
170+
};
171+
172+
int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
173+
texture_registrar_ref_, &info);
174+
return texture_id;
175+
}
176+
177+
std::cerr << "Attempting to register unknown texture variant." << std::endl;
178+
return -1;
179+
} // namespace flutter
180+
181+
bool TextureRegistrarImpl::MarkTextureFrameAvailable(int64_t texture_id) {
182+
return FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable(
183+
texture_registrar_ref_, texture_id);
184+
}
185+
186+
bool TextureRegistrarImpl::UnregisterTexture(int64_t texture_id) {
187+
return FlutterDesktopTextureRegistrarUnregisterExternalTexture(
188+
texture_registrar_ref_, texture_id);
189+
}
190+
149191
} // namespace flutter

shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ core_cpp_client_wrapper_includes =
2424
"include/flutter/standard_codec_serializer.h",
2525
"include/flutter/standard_message_codec.h",
2626
"include/flutter/standard_method_codec.h",
27+
"include/flutter/texture_registrar.h",
2728
],
2829
"abspath")
2930

@@ -34,6 +35,7 @@ core_cpp_client_wrapper_internal_headers =
3435
get_path_info([
3536
"binary_messenger_impl.h",
3637
"byte_buffer_streams.h",
38+
"texture_registrar_impl.h",
3739
],
3840
"abspath")
3941

shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string>
1414

1515
#include "binary_messenger.h"
16+
#include "texture_registrar.h"
1617

1718
namespace flutter {
1819

@@ -41,6 +42,10 @@ class PluginRegistrar {
4142
// This pointer will remain valid for the lifetime of this instance.
4243
BinaryMessenger* messenger() { return messenger_.get(); }
4344

45+
// Returns the texture registrar to use for the plugin to render a pixel
46+
// buffer.
47+
TextureRegistrar* texture_registrar() { return texture_registrar_.get(); }
48+
4449
// Takes ownership of |plugin|.
4550
//
4651
// Plugins are not required to call this method if they have other lifetime
@@ -62,6 +67,8 @@ class PluginRegistrar {
6267

6368
std::unique_ptr<BinaryMessenger> messenger_;
6469

70+
std::unique_ptr<TextureRegistrar> texture_registrar_;
71+
6572
// Plugins registered for ownership.
6673
std::set<std::unique_ptr<Plugin>> plugins_;
6774
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
6+
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
7+
8+
#include <flutter_texture_registrar.h>
9+
10+
#include <cstdint>
11+
#include <functional>
12+
#include <memory>
13+
#include <variant>
14+
15+
namespace flutter {
16+
17+
// A pixel buffer texture.
18+
class PixelBufferTexture {
19+
public:
20+
// A callback used for retrieving pixel buffers.
21+
typedef std::function<const FlutterDesktopPixelBuffer*(size_t width,
22+
size_t height)>
23+
CopyBufferCallback;
24+
25+
// Creates a pixel buffer texture that uses the provided |copy_buffer_cb| to
26+
// retrieve the buffer.
27+
// As the callback is usually invoked from the render thread, the callee must
28+
// take care of proper synchronization. It also needs to be ensured that the
29+
// returned buffer isn't released prior to unregistering this texture.
30+
PixelBufferTexture(CopyBufferCallback copy_buffer_callback)
31+
: copy_buffer_callback_(copy_buffer_callback) {}
32+
33+
// Returns the callback-provided FlutterDesktopPixelBuffer that contains the
34+
// actual pixel data. The intended surface size is specified by |width| and
35+
// |height|.
36+
const FlutterDesktopPixelBuffer* CopyPixelBuffer(size_t width,
37+
size_t height) const {
38+
return copy_buffer_callback_(width, height);
39+
}
40+
41+
private:
42+
const CopyBufferCallback copy_buffer_callback_;
43+
};
44+
45+
// The available texture variants.
46+
// Only PixelBufferTexture is currently implemented.
47+
// Other variants are expected to be added in the future.
48+
typedef std::variant<PixelBufferTexture> TextureVariant;
49+
50+
// An object keeping track of external textures.
51+
//
52+
// Thread safety:
53+
// It's safe to call the member methods from any thread.
54+
class TextureRegistrar {
55+
public:
56+
virtual ~TextureRegistrar() = default;
57+
58+
// Registers a |texture| object and returns the ID for that texture.
59+
virtual int64_t RegisterTexture(TextureVariant* texture) = 0;
60+
61+
// Notifies the flutter engine that the texture object corresponding
62+
// to |texure_id| needs to render a new frame.
63+
//
64+
// For PixelBufferTextures, this will effectively make the engine invoke
65+
// the callback that was provided upon creating the texture.
66+
virtual bool MarkTextureFrameAvailable(int64_t texture_id) = 0;
67+
68+
// Unregisters an existing Texture object.
69+
// Textures must not be unregistered while they're in use.
70+
virtual bool UnregisterTexture(int64_t texture_id) = 0;
71+
};
72+
73+
} // namespace flutter
74+
75+
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_

shell/platform/common/cpp/client_wrapper/plugin_registrar.cc

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "binary_messenger_impl.h"
1111
#include "include/flutter/engine_method_result.h"
1212
#include "include/flutter/method_channel.h"
13+
#include "texture_registrar_impl.h"
1314

1415
namespace flutter {
1516

@@ -19,6 +20,11 @@ PluginRegistrar::PluginRegistrar(FlutterDesktopPluginRegistrarRef registrar)
1920
: registrar_(registrar) {
2021
auto core_messenger = FlutterDesktopPluginRegistrarGetMessenger(registrar_);
2122
messenger_ = std::make_unique<BinaryMessengerImpl>(core_messenger);
23+
24+
auto texture_registrar =
25+
FlutterDesktopRegistrarGetTextureRegistrar(registrar_);
26+
texture_registrar_ =
27+
std::make_unique<TextureRegistrarImpl>(texture_registrar);
2228
}
2329

2430
PluginRegistrar::~PluginRegistrar() {

shell/platform/common/cpp/client_wrapper/plugin_registrar_unittests.cc

+11
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,15 @@ TEST(PluginRegistrarTest, ManagerRemovesOnDestruction) {
214214
nullptr);
215215
}
216216

217+
// Tests that the texture registrar getter returns a non-null TextureRegistrar
218+
TEST(PluginRegistrarTest, TextureRegistrarNotNull) {
219+
auto dummy_registrar_handle =
220+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
221+
PluginRegistrar registrar(dummy_registrar_handle);
222+
223+
TextureRegistrar* texture_registrar = registrar.texture_registrar();
224+
225+
ASSERT_NE(texture_registrar, nullptr);
226+
}
227+
217228
} // namespace flutter

shell/platform/common/cpp/client_wrapper/testing/stub_flutter_api.cc

+38
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,41 @@ void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger,
9292
s_stub_implementation->MessengerSetCallback(channel, callback, user_data);
9393
}
9494
}
95+
96+
FlutterDesktopTextureRegistrarRef FlutterDesktopRegistrarGetTextureRegistrar(
97+
FlutterDesktopPluginRegistrarRef registrar) {
98+
return reinterpret_cast<FlutterDesktopTextureRegistrarRef>(1);
99+
}
100+
101+
int64_t FlutterDesktopTextureRegistrarRegisterExternalTexture(
102+
FlutterDesktopTextureRegistrarRef texture_registrar,
103+
const FlutterDesktopTextureInfo* info) {
104+
uint64_t result = -1;
105+
if (s_stub_implementation) {
106+
result =
107+
s_stub_implementation->TextureRegistrarRegisterExternalTexture(info);
108+
}
109+
return result;
110+
}
111+
112+
bool FlutterDesktopTextureRegistrarUnregisterExternalTexture(
113+
FlutterDesktopTextureRegistrarRef texture_registrar,
114+
int64_t texture_id) {
115+
bool result = false;
116+
if (s_stub_implementation) {
117+
result = s_stub_implementation->TextureRegistrarUnregisterExternalTexture(
118+
texture_id);
119+
}
120+
return result;
121+
}
122+
123+
bool FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable(
124+
FlutterDesktopTextureRegistrarRef texture_registrar,
125+
int64_t texture_id) {
126+
bool result = false;
127+
if (s_stub_implementation) {
128+
result = s_stub_implementation->TextureRegistrarMarkTextureFrameAvailable(
129+
texture_id);
130+
}
131+
return result;
132+
}

shell/platform/common/cpp/client_wrapper/testing/stub_flutter_api.h

+16
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ class StubFlutterApi {
6464
virtual void MessengerSetCallback(const char* channel,
6565
FlutterDesktopMessageCallback callback,
6666
void* user_data) {}
67+
68+
// Called for FlutterDesktopRegisterExternalTexture.
69+
virtual int64_t TextureRegistrarRegisterExternalTexture(
70+
const FlutterDesktopTextureInfo* info) {
71+
return -1;
72+
}
73+
74+
// Called for FlutterDesktopUnregisterExternalTexture.
75+
virtual bool TextureRegistrarUnregisterExternalTexture(int64_t texture_id) {
76+
return false;
77+
}
78+
79+
// Called for FlutterDesktopMarkExternalTextureFrameAvailable.
80+
virtual bool TextureRegistrarMarkTextureFrameAvailable(int64_t texture_id) {
81+
return false;
82+
}
6783
};
6884

6985
// A test helper that owns a stub implementation, making it the test stub for
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TEXTURE_REGISTRAR_IMPL_H_
6+
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TEXTURE_REGISTRAR_IMPL_H_
7+
8+
#include "include/flutter/texture_registrar.h"
9+
10+
namespace flutter {
11+
12+
// Wrapper around a FlutterDesktopTextureRegistrarRef that implements the
13+
// TextureRegistrar API.
14+
class TextureRegistrarImpl : public TextureRegistrar {
15+
public:
16+
explicit TextureRegistrarImpl(
17+
FlutterDesktopTextureRegistrarRef texture_registrar_ref);
18+
virtual ~TextureRegistrarImpl();
19+
20+
// Prevent copying.
21+
TextureRegistrarImpl(TextureRegistrarImpl const&) = delete;
22+
TextureRegistrarImpl& operator=(TextureRegistrarImpl const&) = delete;
23+
24+
// |flutter::TextureRegistrar|
25+
int64_t RegisterTexture(TextureVariant* texture) override;
26+
27+
// |flutter::TextureRegistrar|
28+
bool MarkTextureFrameAvailable(int64_t texture_id) override;
29+
30+
// |flutter::TextureRegistrar|
31+
bool UnregisterTexture(int64_t texture_id) override;
32+
33+
private:
34+
// Handle for interacting with the C API.
35+
FlutterDesktopTextureRegistrarRef texture_registrar_ref_;
36+
};
37+
38+
} // namespace flutter
39+
40+
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TEXTURE_REGISTRAR_IMPL_H_

0 commit comments

Comments
 (0)