From f679a6aa54fe09154f050e37d0d9706196108f67 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 9 Sep 2024 11:18:02 -0700 Subject: [PATCH] [Impeller] fix obvious memory leak. (flutter/engine#55036) Fixes https://github.com/flutter/flutter/issues/154549 Each overlay surface gets its own content context and each cc gets its own transient buffer. So not reseting the overlay surfaces causes a memory leak. The overlay surfaces would just continue to allocate into the same buffer which would allocate endlessly --- engine/src/flutter/shell/gpu/gpu_surface_gl_impeller.cc | 8 +++----- .../src/flutter/shell/gpu/gpu_surface_metal_impeller.mm | 7 ++----- .../shell/gpu/gpu_surface_metal_impeller_unittests.mm | 7 ++++--- .../src/flutter/shell/gpu/gpu_surface_vulkan_impeller.cc | 9 +++------ 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl_impeller.cc b/engine/src/flutter/shell/gpu/gpu_surface_gl_impeller.cc index da51753d00..78a8bcaf0a 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl_impeller.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl_impeller.cc @@ -117,7 +117,6 @@ std::unique_ptr GPUSurfaceGLImpeller::AcquireFrame( auto cull_rect = render_target.GetRenderTargetSize(); impeller::Rect dl_cull_rect = impeller::Rect::MakeSize(cull_rect); - const bool reset_host_buffer = surface_frame.submit_info().frame_boundary; #if EXPERIMENTAL_CANVAS auto skia_cull_rect = SkIRect::MakeWH(cull_rect.width, cull_rect.height); @@ -133,9 +132,7 @@ std::unique_ptr GPUSurfaceGLImpeller::AcquireFrame( display_list->Dispatch(impeller_dispatcher, skia_cull_rect); impeller_dispatcher.FinishRecording(); aiks_context->GetContentContext().GetLazyGlyphAtlas()->ResetTextFrames(); - if (reset_host_buffer) { - aiks_context->GetContentContext().GetTransientsBuffer().Reset(); - } + aiks_context->GetContentContext().GetTransientsBuffer().Reset(); return true; #else impeller::DlDispatcher impeller_dispatcher(dl_cull_rect); @@ -143,7 +140,8 @@ std::unique_ptr GPUSurfaceGLImpeller::AcquireFrame( SkIRect::MakeWH(cull_rect.width, cull_rect.height)); auto picture = impeller_dispatcher.EndRecordingAsPicture(); - return aiks_context->Render(picture, render_target, reset_host_buffer); + return aiks_context->Render(picture, render_target, + /*reset_host_buffer=*/false); #endif // EXPERIMENTAL_CANVAS }; diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm index 6c6e4f9e9c..933ae1a50a 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm @@ -166,7 +166,6 @@ std::unique_ptr GPUSurfaceMetalImpeller::AcquireFrameFromCAMetalLa impeller::IRect cull_rect = surface->coverage(); SkIRect sk_cull_rect = SkIRect::MakeWH(cull_rect.GetWidth(), cull_rect.GetHeight()); - const bool reset_host_buffer = surface_frame.submit_info().frame_boundary; impeller::RenderTarget render_target = surface->GetTargetRenderPassDescriptor(); surface->SetFrameBoundary(surface_frame.submit_info().frame_boundary); @@ -183,9 +182,7 @@ std::unique_ptr GPUSurfaceMetalImpeller::AcquireFrameFromCAMetalLa display_list->Dispatch(impeller_dispatcher, sk_cull_rect); impeller_dispatcher.FinishRecording(); aiks_context->GetContentContext().GetLazyGlyphAtlas()->ResetTextFrames(); - if (reset_host_buffer) { - aiks_context->GetContentContext().GetTransientsBuffer().Reset(); - } + aiks_context->GetContentContext().GetTransientsBuffer().Reset(); if (!surface->PreparePresent()) { return false; @@ -196,7 +193,7 @@ std::unique_ptr GPUSurfaceMetalImpeller::AcquireFrameFromCAMetalLa impeller::DlDispatcher impeller_dispatcher(cull_rect); display_list->Dispatch(impeller_dispatcher, sk_cull_rect); auto picture = impeller_dispatcher.EndRecordingAsPicture(); - auto result = aiks_context->Render(picture, render_target, reset_host_buffer); + auto result = aiks_context->Render(picture, render_target, /*reset_host_buffer=*/true); if (!surface->PreparePresent()) { return false; diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller_unittests.mm b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller_unittests.mm index a5927ff035..16632279eb 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller_unittests.mm +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller_unittests.mm @@ -97,7 +97,8 @@ TEST(GPUSurfaceMetalImpeller, AcquireFrameFromCAMetalLayerDoesNotRetainThis) { ASSERT_TRUE(frame->Submit()); } -TEST(GPUSurfaceMetalImpeller, ResetHostBufferBasedOnFrameBoundary) { +// Because each overlay surface gets its own HostBuffer, we always need to reset. +TEST(GPUSurfaceMetalImpeller, DoesNotResetHostBufferBasedOnFrameBoundary) { auto delegate = std::make_shared(); delegate->SetDevice(); @@ -115,13 +116,13 @@ TEST(GPUSurfaceMetalImpeller, ResetHostBufferBasedOnFrameBoundary) { frame->set_submit_info({.frame_boundary = false}); ASSERT_TRUE(frame->Submit()); - EXPECT_EQ(host_buffer.GetStateForTest().current_frame, 0u); + EXPECT_EQ(host_buffer.GetStateForTest().current_frame, 1u); frame = surface->AcquireFrame(SkISize::Make(100, 100)); frame->set_submit_info({.frame_boundary = true}); ASSERT_TRUE(frame->Submit()); - EXPECT_EQ(host_buffer.GetStateForTest().current_frame, 1u); + EXPECT_EQ(host_buffer.GetStateForTest().current_frame, 2u); } #ifdef IMPELLER_DEBUG diff --git a/engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller.cc b/engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller.cc index 02a9a4dfd5..6cd1ea54b4 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller.cc @@ -80,7 +80,6 @@ std::unique_ptr GPUSurfaceVulkanImpeller::AcquireFrame( return false; } - const bool reset_host_buffer = surface_frame.submit_info().frame_boundary; #if EXPERIMENTAL_CANVAS impeller::TextFrameDispatcher collector(aiks_context->GetContentContext(), impeller::Matrix()); @@ -94,11 +93,8 @@ std::unique_ptr GPUSurfaceVulkanImpeller::AcquireFrame( display_list->Dispatch(impeller_dispatcher, SkIRect::MakeWH(cull_rect.width, cull_rect.height)); impeller_dispatcher.FinishRecording(); - aiks_context->GetContentContext().GetTransientsBuffer().Reset(); aiks_context->GetContentContext().GetLazyGlyphAtlas()->ResetTextFrames(); - if (reset_host_buffer) { - aiks_context->GetContentContext().GetTransientsBuffer().Reset(); - } + aiks_context->GetContentContext().GetTransientsBuffer().Reset(); return true; #else impeller::Rect dl_cull_rect = impeller::Rect::MakeSize(cull_rect); @@ -106,7 +102,8 @@ std::unique_ptr GPUSurfaceVulkanImpeller::AcquireFrame( display_list->Dispatch(impeller_dispatcher, SkIRect::MakeWH(cull_rect.width, cull_rect.height)); auto picture = impeller_dispatcher.EndRecordingAsPicture(); - return aiks_context->Render(picture, render_target, reset_host_buffer); + return aiks_context->Render(picture, render_target, + /*reset_host_buffer=*/false); #endif };