Skip to content

Commit 75f4b80

Browse files
[Impeller] implement external texture gl for embedder. (flutter#56277)
Fixes flutter#143809
1 parent c55042c commit 75f4b80

6 files changed

Lines changed: 151 additions & 1 deletion

File tree

‎impeller/renderer/backend/gles/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impeller_component("gles_unittests") {
2222
"test/mock_gles_unittests.cc",
2323
"test/pipeline_library_gles_unittests.cc",
2424
"test/proc_table_gles_unittests.cc",
25+
"test/reactor_unittests.cc",
2526
"test/specialization_constants_unittests.cc",
2627
]
2728
deps = [

‎impeller/renderer/backend/gles/reactor_gles.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <algorithm>
88

99
#include "flutter/fml/trace_event.h"
10+
#include "fml/closure.h"
1011
#include "fml/logging.h"
1112
#include "impeller/base/validation.h"
1213

@@ -85,6 +86,19 @@ bool ReactorGLES::AddOperation(Operation operation) {
8586
return true;
8687
}
8788

89+
bool ReactorGLES::RegisterCleanupCallback(const HandleGLES& handle,
90+
const fml::closure& callback) {
91+
if (handle.IsDead()) {
92+
return false;
93+
}
94+
WriterLock handles_lock(handles_mutex_);
95+
if (auto found = handles_.find(handle); found != handles_.end()) {
96+
found->second.callback = fml::ScopedCleanupClosure(callback);
97+
return true;
98+
}
99+
return false;
100+
}
101+
88102
static std::optional<GLuint> CreateGLHandle(const ProcTableGLES& gl,
89103
HandleType type) {
90104
GLuint handle = GL_NONE;

‎impeller/renderer/backend/gles/reactor_gles.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <memory>
1010
#include <vector>
1111

12+
#include "fml/closure.h"
1213
#include "impeller/base/thread.h"
1314
#include "impeller/renderer/backend/gles/handle_gles.h"
1415
#include "impeller/renderer/backend/gles/proc_table_gles.h"
@@ -216,6 +217,23 @@ class ReactorGLES {
216217
///
217218
[[nodiscard]] bool AddOperation(Operation operation);
218219

220+
//----------------------------------------------------------------------------
221+
/// @brief Register a cleanup callback that will be invokved with the
222+
/// provided user data when the handle is destroyed.
223+
///
224+
/// This operation is not guaranteed to run immediately. It will
225+
/// complete in a finite amount of time on any thread as long as
226+
/// there is a reactor worker and the reactor itself is not being
227+
/// torn down.
228+
///
229+
/// @param[in] handle The handle to attach the cleanup to.
230+
/// @param[in] callback The cleanup callback to execute.
231+
///
232+
/// @return If the operation was successfully queued for completion.
233+
///
234+
bool RegisterCleanupCallback(const HandleGLES& handle,
235+
const fml::closure& callback);
236+
219237
//----------------------------------------------------------------------------
220238
/// @brief Perform a reaction on the current thread if able.
221239
///
@@ -231,6 +249,7 @@ class ReactorGLES {
231249
std::optional<GLuint> name;
232250
std::optional<std::string> pending_debug_label;
233251
bool pending_collection = false;
252+
fml::ScopedCleanupClosure callback = {};
234253

235254
LiveHandle() = default;
236255

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 <memory>
6+
#include "flutter/testing/testing.h" // IWYU pragma: keep
7+
#include "gtest/gtest.h"
8+
#include "impeller/renderer/backend/gles/handle_gles.h"
9+
#include "impeller/renderer/backend/gles/proc_table_gles.h"
10+
#include "impeller/renderer/backend/gles/reactor_gles.h"
11+
#include "impeller/renderer/backend/gles/test/mock_gles.h"
12+
13+
namespace impeller {
14+
namespace testing {
15+
16+
class TestWorker : public ReactorGLES::Worker {
17+
public:
18+
bool CanReactorReactOnCurrentThreadNow(
19+
const ReactorGLES& reactor) const override {
20+
return true;
21+
}
22+
};
23+
24+
TEST(ReactorGLES, CanAttachCleanupCallbacksToHandles) {
25+
auto mock_gles = MockGLES::Init();
26+
ProcTableGLES::Resolver resolver = kMockResolverGLES;
27+
auto proc_table = std::make_unique<ProcTableGLES>(resolver);
28+
auto worker = std::make_shared<TestWorker>();
29+
auto reactor = std::make_shared<ReactorGLES>(std::move(proc_table));
30+
reactor->AddWorker(worker);
31+
32+
int value = 0;
33+
auto handle = reactor->CreateHandle(HandleType::kTexture, 1123);
34+
auto added =
35+
reactor->RegisterCleanupCallback(handle, [&value]() { value++; });
36+
37+
EXPECT_TRUE(added);
38+
EXPECT_TRUE(reactor->React());
39+
40+
reactor->CollectHandle(handle);
41+
EXPECT_TRUE(reactor->AddOperation([](const ReactorGLES& reactor) {}));
42+
EXPECT_TRUE(reactor->React());
43+
EXPECT_EQ(value, 1);
44+
}
45+
46+
} // namespace testing
47+
} // namespace impeller

‎shell/platform/embedder/embedder_external_texture_gl.cc‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
#include "flutter/shell/platform/embedder/embedder_external_texture_gl.h"
66

77
#include "flutter/fml/logging.h"
8-
#include "include/core/SkCanvas.h"
8+
#include "impeller/core/texture_descriptor.h"
9+
#include "impeller/display_list/aiks_context.h"
10+
#include "impeller/display_list/dl_image_impeller.h"
11+
#include "impeller/geometry/size.h"
12+
#include "impeller/renderer/backend/gles/context_gles.h"
13+
#include "impeller/renderer/backend/gles/handle_gles.h"
14+
#include "impeller/renderer/backend/gles/texture_gles.h"
15+
916
#include "include/core/SkPaint.h"
1017
#include "third_party/skia/include/core/SkAlphaType.h"
1118
#include "third_party/skia/include/core/SkColorSpace.h"
@@ -62,6 +69,17 @@ sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTexture(
6269
GrDirectContext* context,
6370
impeller::AiksContext* aiks_context,
6471
const SkISize& size) {
72+
if (!!aiks_context) {
73+
return ResolveTextureImpeller(texture_id, aiks_context, size);
74+
} else {
75+
return ResolveTextureSkia(texture_id, context, size);
76+
}
77+
}
78+
79+
sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureSkia(
80+
int64_t texture_id,
81+
GrDirectContext* context,
82+
const SkISize& size) {
6583
context->flushAndSubmit();
6684
context->resetContext(kAll_GrBackendState);
6785
std::unique_ptr<FlutterOpenGLTexture> texture =
@@ -110,6 +128,49 @@ sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTexture(
110128
return DlImage::Make(std::move(image));
111129
}
112130

131+
sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureImpeller(
132+
int64_t texture_id,
133+
impeller::AiksContext* aiks_context,
134+
const SkISize& size) {
135+
std::unique_ptr<FlutterOpenGLTexture> texture =
136+
external_texture_callback_(texture_id, size.width(), size.height());
137+
138+
if (!texture) {
139+
return nullptr;
140+
}
141+
142+
impeller::TextureDescriptor desc;
143+
desc.size = impeller::ISize(texture->width, texture->height);
144+
145+
impeller::ContextGLES& context =
146+
impeller::ContextGLES::Cast(*aiks_context->GetContext());
147+
impeller::HandleGLES handle = context.GetReactor()->CreateHandle(
148+
impeller::HandleType::kTexture, texture->target);
149+
std::shared_ptr<impeller::TextureGLES> image =
150+
std::make_shared<impeller::TextureGLES>(context.GetReactor(), desc,
151+
handle);
152+
153+
if (!image) {
154+
// In case Skia rejects the image, call the release proc so that
155+
// embedders can perform collection of intermediates.
156+
if (texture->destruction_callback) {
157+
texture->destruction_callback(texture->user_data);
158+
}
159+
FML_LOG(ERROR) << "Could not create external texture";
160+
return nullptr;
161+
}
162+
if (texture->destruction_callback &&
163+
!context.GetReactor()->RegisterCleanupCallback(
164+
handle,
165+
[callback = texture->destruction_callback,
166+
user_data = texture->user_data]() { callback(user_data); })) {
167+
FML_LOG(ERROR) << "Could not register destruction callback";
168+
return nullptr;
169+
}
170+
171+
return impeller::DlImageImpeller::Make(image);
172+
}
173+
113174
// |flutter::Texture|
114175
void EmbedderExternalTextureGL::OnGrContextCreated() {}
115176

‎shell/platform/embedder/embedder_external_texture_gl.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class EmbedderExternalTextureGL : public flutter::Texture {
3131
impeller::AiksContext* aiks_context,
3232
const SkISize& size);
3333

34+
sk_sp<DlImage> ResolveTextureSkia(int64_t texture_id,
35+
GrDirectContext* context,
36+
const SkISize& size);
37+
38+
sk_sp<DlImage> ResolveTextureImpeller(int64_t texture_id,
39+
impeller::AiksContext* aiks_context,
40+
const SkISize& size);
41+
3442
// |flutter::Texture|
3543
void Paint(PaintContext& context,
3644
const SkRect& bounds,

0 commit comments

Comments
 (0)