Skip to content

Commit a7dcb16

Browse files
committed
[embedder] Ensure destruction called on present
This is a followup to flutter#38038. In that patch, a destruction callback for textures created via the FlutterRendererConfig callbacks (as opposed to by the FlutterCompositor callbacks) was added and passed through to the texture info attached to the SurfaceFrame generated in GPUSurfaceMetalSkia::AcquireFrameFromMTLTexture (called via GPUSurfaceMetalSkia::GetMTLTexture, which invokes the get_next_drawable_callback), however, in order for the destruction callback to make it to the presented Skia texture, it needs to be passed through to the present callback here: https://github.com/flutter/engine/blob/5545ccf8719c9568d8f521f9f18a869ca9e10056/shell/gpu/gpu_surface_metal_skia.mm#LL233 which is invoked by the submit callback passed to Skia: https://github.com/flutter/engine/blob/5545ccf8719c9568d8f521f9f18a869ca9e10056/shell/gpu/gpu_surface_metal_skia.mm#LL239 The present callback is implemented in EmbedderSurface::PresentTexture, which invokes the present callback registered in FlutterMetalRendererConfig.present_drawable_callback. This patch ensures that the destruction callback is passed through to Skia's present callback so destruction occurs in the right place. Issue: flutter/flutter#116381
1 parent b4de17d commit a7dcb16

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

shell/platform/embedder/embedder.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ InferMetalPlatformViewCreationCallback(
463463
embedder_texture.struct_size = sizeof(FlutterMetalTexture);
464464
embedder_texture.texture = texture.texture;
465465
embedder_texture.texture_id = texture.texture_id;
466+
embedder_texture.user_data = texture.destruction_context;
467+
embedder_texture.destruction_callback = texture.destruction_callback;
466468
return ptr(user_data, &embedder_texture);
467469
};
468470
auto metal_get_texture =

shell/platform/embedder/tests/embedder_test_context_metal.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,18 @@ TestMetalContext* EmbedderTestContextMetal::GetTestMetalContext() {
4747
return metal_context_.get();
4848
}
4949

50+
void EmbedderTestContextMetal::SetPresentCallback(
51+
PresentCallback present_callback) {
52+
present_callback_ = std::move(present_callback);
53+
}
54+
5055
bool EmbedderTestContextMetal::Present(int64_t texture_id) {
5156
FireRootSurfacePresentCallbackIfPresent(
5257
[&]() { return metal_surface_->GetRasterSurfaceSnapshot(); });
5358
present_count_++;
59+
if (present_callback_ != nullptr) {
60+
return present_callback_(texture_id);
61+
}
5462
return metal_context_->Present(texture_id);
5563
}
5664

shell/platform/embedder/tests/embedder_test_context_metal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
2323
using NextDrawableCallback =
2424
std::function<FlutterMetalTexture(const FlutterFrameInfo* frame_info)>;
2525

26+
using PresentCallback = std::function<bool(int64_t texture_id)>;
27+
2628
explicit EmbedderTestContextMetal(std::string assets_path = "");
2729

2830
~EmbedderTestContextMetal() override;
@@ -39,6 +41,9 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
3941
void SetExternalTextureCallback(
4042
TestExternalTextureCallback external_texture_frame_callback);
4143

44+
// Override the default handling for Present.
45+
void SetPresentCallback(PresentCallback present_callback);
46+
4247
bool Present(int64_t texture_id);
4348

4449
bool PopulateExternalTexture(int64_t texture_id,
@@ -65,6 +70,7 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
6570
std::unique_ptr<TestMetalContext> metal_context_;
6671
std::unique_ptr<TestMetalSurface> metal_surface_;
6772
size_t present_count_ = 0;
73+
PresentCallback present_callback_ = nullptr;
6874
NextDrawableCallback next_drawable_callback_ = nullptr;
6975

7076
void SetupSurface(SkISize surface_size) override;

shell/platform/embedder/tests/embedder_unittests_metal.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ GrBackendTexture backend_texture(texture_size.width(), texture_size.height(), Gr
235235
builder.SetDartEntrypoint("texture_destruction_callback_called_without_custom_compositor");
236236

237237
struct CollectContext {
238+
int present_count = 0;
238239
int collect_count = 0;
239240
fml::AutoResetWaitableEvent latch;
240241
};
@@ -249,11 +250,16 @@ GrBackendTexture backend_texture(texture_size.width(), texture_size.height(), Gr
249250
texture.user_data = collect_context.get();
250251
texture.destruction_callback = [](void* user_data) {
251252
CollectContext* callback_collect_context = reinterpret_cast<CollectContext*>(user_data);
253+
ASSERT_TRUE(callback_collect_context->present_count > 0);
252254
callback_collect_context->collect_count++;
253255
callback_collect_context->latch.Signal();
254256
};
255257
return texture;
256258
});
259+
context.SetPresentCallback([&context, &collect_context](int64_t texture_id) {
260+
collect_context->present_count++;
261+
return context.GetTestMetalContext()->Present(texture_id);
262+
});
257263

258264
auto engine = builder.LaunchEngine();
259265

0 commit comments

Comments
 (0)