From 0fab2ea52dd736da957c27a8e326a0f007f96c78 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 7 Dec 2022 12:40:22 -0800 Subject: [PATCH] [embedder] Ensure destruction called on present (flutter/engine#38078) This is a followup to flutter/engine#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/960af0a3503e65afccfd9455ccc45f739da2cad8/shell/gpu/gpu_surface_metal_skia.mm#LL233 which is invoked by the submit callback passed to Skia: https://github.com/flutter/engine/blob/960af0a3503e65afccfd9455ccc45f739da2cad8/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: https://github.com/flutter/flutter/issues/116381 --- engine/src/flutter/shell/platform/embedder/embedder.cc | 2 ++ .../embedder/tests/embedder_test_context_metal.cc | 8 ++++++++ .../platform/embedder/tests/embedder_test_context_metal.h | 6 ++++++ .../platform/embedder/tests/embedder_unittests_metal.mm | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index e0f3d47e1f..64b856971c 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -463,6 +463,8 @@ InferMetalPlatformViewCreationCallback( embedder_texture.struct_size = sizeof(FlutterMetalTexture); embedder_texture.texture = texture.texture; embedder_texture.texture_id = texture.texture_id; + embedder_texture.user_data = texture.destruction_context; + embedder_texture.destruction_callback = texture.destruction_callback; return ptr(user_data, &embedder_texture); }; auto metal_get_texture = diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc index 7554a9278a..aa83ca73dd 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc @@ -47,10 +47,18 @@ TestMetalContext* EmbedderTestContextMetal::GetTestMetalContext() { return metal_context_.get(); } +void EmbedderTestContextMetal::SetPresentCallback( + PresentCallback present_callback) { + present_callback_ = std::move(present_callback); +} + bool EmbedderTestContextMetal::Present(int64_t texture_id) { FireRootSurfacePresentCallbackIfPresent( [&]() { return metal_surface_->GetRasterSurfaceSnapshot(); }); present_count_++; + if (present_callback_ != nullptr) { + return present_callback_(texture_id); + } return metal_context_->Present(texture_id); } diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h index d4b5914e9a..ad1e08b831 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h @@ -23,6 +23,8 @@ class EmbedderTestContextMetal : public EmbedderTestContext { using NextDrawableCallback = std::function; + using PresentCallback = std::function; + explicit EmbedderTestContextMetal(std::string assets_path = ""); ~EmbedderTestContextMetal() override; @@ -39,6 +41,9 @@ class EmbedderTestContextMetal : public EmbedderTestContext { void SetExternalTextureCallback( TestExternalTextureCallback external_texture_frame_callback); + // Override the default handling for Present. + void SetPresentCallback(PresentCallback present_callback); + bool Present(int64_t texture_id); bool PopulateExternalTexture(int64_t texture_id, @@ -65,6 +70,7 @@ class EmbedderTestContextMetal : public EmbedderTestContext { std::unique_ptr metal_context_; std::unique_ptr metal_surface_; size_t present_count_ = 0; + PresentCallback present_callback_ = nullptr; NextDrawableCallback next_drawable_callback_ = nullptr; void SetupSurface(SkISize surface_size) override; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.mm b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.mm index 351b9ea428..7d07b54d7e 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.mm +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.mm @@ -235,6 +235,7 @@ TEST_F(EmbedderTest, TextureDestructionCallbackCalledWithoutCustomCompositorMeta builder.SetDartEntrypoint("texture_destruction_callback_called_without_custom_compositor"); struct CollectContext { + int present_count = 0; int collect_count = 0; fml::AutoResetWaitableEvent latch; }; @@ -249,11 +250,16 @@ TEST_F(EmbedderTest, TextureDestructionCallbackCalledWithoutCustomCompositorMeta texture.user_data = collect_context.get(); texture.destruction_callback = [](void* user_data) { CollectContext* callback_collect_context = reinterpret_cast(user_data); + ASSERT_TRUE(callback_collect_context->present_count > 0); callback_collect_context->collect_count++; callback_collect_context->latch.Signal(); }; return texture; }); + context.SetPresentCallback([&context, &collect_context](int64_t texture_id) { + collect_context->present_count++; + return context.GetTestMetalContext()->Present(texture_id); + }); auto engine = builder.LaunchEngine();