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

[embedder] Ensure destruction called on present #38078

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
8 changes: 8 additions & 0 deletions shell/platform/embedder/tests/embedder_test_context_metal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions shell/platform/embedder/tests/embedder_test_context_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
using NextDrawableCallback =
std::function<FlutterMetalTexture(const FlutterFrameInfo* frame_info)>;

using PresentCallback = std::function<bool(int64_t texture_id)>;

explicit EmbedderTestContextMetal(std::string assets_path = "");

~EmbedderTestContextMetal() override;
Expand All @@ -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,
Expand All @@ -65,6 +70,7 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
std::unique_ptr<TestMetalContext> metal_context_;
std::unique_ptr<TestMetalSurface> metal_surface_;
size_t present_count_ = 0;
PresentCallback present_callback_ = nullptr;
NextDrawableCallback next_drawable_callback_ = nullptr;

void SetupSurface(SkISize surface_size) override;
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/embedder/tests/embedder_unittests_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ GrBackendTexture backend_texture(texture_size.width(), texture_size.height(), Gr
builder.SetDartEntrypoint("texture_destruction_callback_called_without_custom_compositor");

struct CollectContext {
int present_count = 0;
int collect_count = 0;
fml::AutoResetWaitableEvent latch;
};
Expand All @@ -249,11 +250,16 @@ GrBackendTexture backend_texture(texture_size.width(), texture_size.height(), Gr
texture.user_data = collect_context.get();
texture.destruction_callback = [](void* user_data) {
CollectContext* callback_collect_context = reinterpret_cast<CollectContext*>(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();

Expand Down