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

Commit 1e6ac95

Browse files
committed
Add destruction test
1 parent 7af0c70 commit 1e6ac95

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

shell/platform/embedder/fixtures/main.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,25 @@ void can_composite_platform_views_with_platform_layer_on_bottom() {
498498
PlatformDispatcher.instance.scheduleFrame();
499499
}
500500

501+
@pragma('vm:external-name', 'SignalBeginFrame')
502+
external void signalBeginFrame();
503+
504+
@pragma('vm:entry-point')
505+
void texture_destruction_callback_called_without_custom_compositor() async {
506+
PlatformDispatcher.instance.onBeginFrame = (Duration duration) {
507+
Color red = Color.fromARGB(127, 255, 0, 0);
508+
Size size = Size(50.0, 150.0);
509+
SceneBuilder builder = SceneBuilder();
510+
builder.pushOffset(0.0, 0.0);
511+
builder.addPicture(
512+
Offset(10.0, 10.0), CreateColoredBox(red, size)); // red - flutter
513+
builder.pop();
514+
PlatformDispatcher.instance.views.first.render(builder.build());
515+
};
516+
PlatformDispatcher.instance.scheduleFrame();
517+
}
518+
519+
501520
@pragma('vm:entry-point')
502521
void can_render_scene_without_custom_compositor() {
503522
PlatformDispatcher.instance.onBeginFrame = (Duration duration) {

shell/platform/embedder/tests/embedder_test_context_metal.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,22 @@ bool EmbedderTestContextMetal::PopulateExternalTexture(
7171
}
7272
}
7373

74+
TestMetalContext::TextureInfo EmbedderTestContextMetal::GetTextureInfo() {
75+
return metal_surface_->GetTextureInfo();
76+
}
77+
78+
void EmbedderTestContextMetal::SetNextDrawableCallback(
79+
NextDrawableCallback next_drawable_callback) {
80+
next_drawable_callback_ = std::move(next_drawable_callback);
81+
}
82+
7483
FlutterMetalTexture EmbedderTestContextMetal::GetNextDrawable(
7584
const FlutterFrameInfo* frame_info) {
76-
auto texture_info = metal_surface_->GetTextureInfo();
85+
if (next_drawable_callback_ != nullptr) {
86+
return next_drawable_callback_(frame_info);
87+
}
7788

89+
auto texture_info = metal_surface_->GetTextureInfo();
7890
FlutterMetalTexture texture;
7991
texture.struct_size = sizeof(FlutterMetalTexture);
8092
texture.texture_id = texture_info.texture_id;

shell/platform/embedder/tests/embedder_test_context_metal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
2020
size_t h,
2121
FlutterMetalExternalTexture* output)>;
2222

23+
using NextDrawableCallback =
24+
std::function<FlutterMetalTexture(const FlutterFrameInfo* frame_info)>;
25+
2326
explicit EmbedderTestContextMetal(std::string assets_path = "");
2427

2528
~EmbedderTestContextMetal() override;
@@ -45,6 +48,12 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
4548

4649
TestMetalContext* GetTestMetalContext();
4750

51+
// Returns the TextureInfo for the test Metal surface.
52+
TestMetalContext::TextureInfo GetTextureInfo();
53+
54+
// Override the default handling for GetNextDrawable.
55+
void SetNextDrawableCallback(NextDrawableCallback next_drawable_callback);
56+
4857
FlutterMetalTexture GetNextDrawable(const FlutterFrameInfo* frame_info);
4958

5059
private:
@@ -56,6 +65,7 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
5665
std::unique_ptr<TestMetalContext> metal_context_;
5766
std::unique_ptr<TestMetalSurface> metal_surface_;
5867
size_t present_count_ = 0;
68+
NextDrawableCallback next_drawable_callback_ = nullptr;
5969

6070
void SetupSurface(SkISize surface_size) override;
6171

shell/platform/embedder/tests/embedder_unittests_metal.mm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,49 @@ GrBackendTexture backend_texture(texture_size.width(), texture_size.height(), Gr
227227
ASSERT_TRUE(ImageMatchesFixture("scene_without_custom_compositor.png", rendered_scene));
228228
}
229229

230+
TEST_F(EmbedderTest, TextureDestructionCallbackCalledWithoutCustomCompositorMetal) {
231+
EmbedderTestContextMetal& context = reinterpret_cast<EmbedderTestContextMetal&>(
232+
GetEmbedderContext(EmbedderTestContextType::kMetalContext));
233+
EmbedderConfigBuilder builder(context);
234+
builder.SetMetalRendererConfig(SkISize::Make(800, 600));
235+
builder.SetDartEntrypoint("texture_destruction_callback_called_without_custom_compositor");
236+
237+
struct CollectContext {
238+
int collect_count = 0;
239+
fml::AutoResetWaitableEvent latch;
240+
};
241+
242+
auto collect_context = std::make_unique<CollectContext>();
243+
context.SetNextDrawableCallback([&context, &collect_context](const FlutterFrameInfo* frame_info) {
244+
auto texture_info = context.GetTextureInfo();
245+
FlutterMetalTexture texture;
246+
texture.struct_size = sizeof(FlutterMetalTexture);
247+
texture.texture_id = texture_info.texture_id;
248+
texture.texture = reinterpret_cast<FlutterMetalTextureHandle>(texture_info.texture);
249+
texture.user_data = collect_context.get();
250+
texture.destruction_callback = [](void* user_data) {
251+
CollectContext* callback_collect_context = reinterpret_cast<CollectContext*>(user_data);
252+
callback_collect_context->collect_count++;
253+
callback_collect_context->latch.Signal();
254+
};
255+
return texture;
256+
});
257+
258+
auto engine = builder.LaunchEngine();
259+
260+
// Send a window metrics events so frames may be scheduled.
261+
FlutterWindowMetricsEvent event = {};
262+
event.struct_size = sizeof(event);
263+
event.width = 800;
264+
event.height = 600;
265+
event.pixel_ratio = 1.0;
266+
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), kSuccess);
267+
ASSERT_TRUE(engine.is_valid());
268+
269+
collect_context->latch.Wait();
270+
EXPECT_EQ(collect_context->collect_count, 1);
271+
}
272+
230273
TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownSceneMetal) {
231274
auto& context = GetEmbedderContext(EmbedderTestContextType::kMetalContext);
232275

0 commit comments

Comments
 (0)