Skip to content

Commit a7cb895

Browse files
jwinarskeandywolff
andauthored
Do not present the root surface when an external view embedder owns it (flutter#193000)
`GPUSurfaceVulkan` and `GPUSurfaceGLImpeller` both take `render_to_surface` and return a no-op `SurfaceFrame` when it is false. `GPUSurfaceVulkanImpeller` has no such flag, so with a compositor the engine acquires an embedder image and presents it after every composited frame. The compositor has already presented that frame's layers. The root present puts an unrendered image on top of them. An embedder holding a single image sees nothing; one cycling a ring of scanout buffers sees its frame replaced by whatever the ring handed back — one good frame, then black, at a flip rate that still reads 60fps. That is what it looked like on hardware before I found this. Nothing can reach it today: with a compositor and Impeller on Vulkan the embedder cannot build a render target at all, so the frame fails earlier (flutter#187525). It becomes reachable with that fix, so the gate is groundwork for it. Also moves `DisposeThreadLocalCachedResources` ahead of the gate. It is per-frame upkeep, not part of presenting, and leaving it after would hand the compositor path a raster thread that holds its command and descriptor pools for the life of the engine. ## Tests Two in `gpu_surface_vulkan_impeller_unittests.cc`: - `DoesNotPresentWhenNotRenderingToSurface` — no image acquired, none presented, and the thread-local pools still released. Fails without the gate; fails again if the dispose is moved back after it ("thread-local pools survived a frame the compositor presented"). - `PresentsWhenRenderingToSurface` — the default still acquires, which is what a compositor-less embedder relies on. Verified against the failing behavior, not just green: reverting each half of the change fails the matching assertion. Second of six, from flutter#187525. Independent of the others; conflicts textually with the third (both touch `gpu_surface_vulkan_impeller.cc`), so whichever lands second wants a rebase. --------- Co-authored-by: Andy Wolff <[email protected]>
1 parent af028a8 commit a7cb895

4 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller.cc‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ class WrappedTextureSourceVK : public impeller::TextureSourceVK {
5555

5656
GPUSurfaceVulkanImpeller::GPUSurfaceVulkanImpeller(
5757
GPUSurfaceVulkanDelegate* delegate,
58-
std::shared_ptr<impeller::Context> context)
59-
: delegate_(delegate) {
58+
std::shared_ptr<impeller::Context> context,
59+
bool render_to_surface)
60+
: delegate_(delegate), render_to_surface_(render_to_surface) {
6061
if (!context || !context->IsValid()) {
6162
return;
6263
}
@@ -88,6 +89,27 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkanImpeller::AcquireFrame(
8889
return nullptr;
8990
}
9091

92+
// Per-frame upkeep, not part of presenting: release this thread's cached
93+
// command and descriptor pools whether or not a frame is acquired below.
94+
// Skipping it when the compositor presents would leave the raster thread
95+
// holding those pools for the life of the engine.
96+
if (impeller_context_) {
97+
impeller_context_->DisposeThreadLocalCachedResources();
98+
}
99+
100+
// Nothing to acquire or present when an external view embedder owns
101+
// presentation: the compositor presents the layers. Without this the engine
102+
// presents an unrendered root image after every composited frame, which on a
103+
// scanout ring overwrites the frame just shown -- one good frame, then black.
104+
if (!render_to_surface_) {
105+
return std::make_unique<SurfaceFrame>(
106+
nullptr, SurfaceFrame::FramebufferInfo{.supports_readback = true},
107+
[](const SurfaceFrame& surface_frame, DlCanvas* canvas) {
108+
return true;
109+
},
110+
[](const SurfaceFrame& surface_frame) { return true; }, size);
111+
}
112+
91113
if (size.IsEmpty()) {
92114
FML_LOG(ERROR) << "Vulkan surface was asked for an empty frame.";
93115
return nullptr;
@@ -161,8 +183,6 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkanImpeller::AcquireFrame(
161183
impeller::ContextVK& context_vk =
162184
impeller::ContextVK::Cast(*impeller_context_);
163185

164-
context_vk.DisposeThreadLocalCachedResources();
165-
166186
impeller::vk::Image vk_image =
167187
impeller::vk::Image(reinterpret_cast<VkImage>(flutter_image.image));
168188

‎engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ FML_TEST_CLASS(GPUSurfaceVulkanImpeller,
2323

2424
class GPUSurfaceVulkanImpeller final : public Surface {
2525
public:
26+
/// @param render_to_surface False when an external view embedder owns
27+
/// presentation, in which case this surface must
28+
/// neither acquire nor present an image: the
29+
/// compositor presents the layers instead, and a
30+
/// root-surface present would overwrite them.
2631
explicit GPUSurfaceVulkanImpeller(GPUSurfaceVulkanDelegate* delegate,
27-
std::shared_ptr<impeller::Context> context);
32+
std::shared_ptr<impeller::Context> context,
33+
bool render_to_surface = true);
2834

2935
// |Surface|
3036
~GPUSurfaceVulkanImpeller() override;
@@ -37,6 +43,7 @@ class GPUSurfaceVulkanImpeller final : public Surface {
3743
RecreatesTransientsWhenFrameSizeChanges);
3844

3945
GPUSurfaceVulkanDelegate* delegate_;
46+
bool render_to_surface_ = true;
4047
std::shared_ptr<impeller::Context> impeller_context_;
4148
std::shared_ptr<impeller::AiksContext> aiks_context_;
4249
std::shared_ptr<impeller::SwapchainTransientsVK> transients_;

‎engine/src/flutter/shell/gpu/gpu_surface_vulkan_impeller_unittests.cc‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TestGPUSurfaceVulkanDelegate : public GPUSurfaceVulkanDelegate {
3939
const vulkan::VulkanProcTable& vk() override { return *vk_; }
4040

4141
FlutterVulkanImage AcquireImage(const DlISize& size) override {
42+
++acquire_count_;
4243
if (!test_surface_ || surface_size_ != size) {
4344
test_surface_ = TestVulkanSurface::Create(*test_context_, size);
4445
surface_size_ = size;
@@ -53,9 +54,17 @@ class TestGPUSurfaceVulkanDelegate : public GPUSurfaceVulkanDelegate {
5354
};
5455
}
5556

56-
bool PresentImage(VkImage image, VkFormat format) override { return true; }
57+
bool PresentImage(VkImage image, VkFormat format) override {
58+
++present_count_;
59+
return true;
60+
}
61+
62+
int acquire_count() const { return acquire_count_; }
63+
int present_count() const { return present_count_; }
5764

5865
private:
66+
int acquire_count_ = 0;
67+
int present_count_ = 0;
5968
fml::RefPtr<vulkan::VulkanProcTable> vk_;
6069
fml::RefPtr<TestVulkanContext> test_context_;
6170
std::unique_ptr<TestVulkanSurface> test_surface_;
@@ -83,6 +92,56 @@ TEST(GPUSurfaceVulkanImpeller, DisposesThreadLocalResources) {
8392
EXPECT_EQ(impeller::CommandPoolRecyclerVK::GetGlobalPoolCount(*context), 0);
8493
}
8594

95+
/// An external view embedder presents the frame's layers itself, so the root
96+
/// surface must neither acquire an image nor present one. It used to do both:
97+
/// the engine presented an unrendered root image after every composited frame,
98+
/// which on an embedder that recycles scanout buffers overwrote the frame just
99+
/// shown -- one good frame, then black.
100+
TEST(GPUSurfaceVulkanImpeller, DoesNotPresentWhenNotRenderingToSurface) {
101+
impeller::ContextVK::Settings context_settings;
102+
context_settings.proc_address_callback = vkGetInstanceProcAddr;
103+
context_settings.shader_libraries_data = ShaderLibraryMappings();
104+
auto context = impeller::ContextVK::Create(std::move(context_settings));
105+
106+
TestGPUSurfaceVulkanDelegate delegate;
107+
std::unique_ptr<Surface> surface = std::make_unique<GPUSurfaceVulkanImpeller>(
108+
&delegate, context, /*render_to_surface=*/false);
109+
110+
// Held before the frame, so the dispose below is observable.
111+
auto pool = context->GetCommandPoolRecycler()->Get();
112+
EXPECT_EQ(impeller::CommandPoolRecyclerVK::GetGlobalPoolCount(*context), 1);
113+
114+
auto frame = surface->AcquireFrame(DlISize(100, 100));
115+
ASSERT_NE(frame, nullptr);
116+
EXPECT_TRUE(frame->Submit());
117+
118+
EXPECT_EQ(delegate.acquire_count(), 0)
119+
<< "acquired an embedder image while the compositor owns presentation";
120+
EXPECT_EQ(delegate.present_count(), 0)
121+
<< "presented the root surface over the compositor's layers";
122+
// Not presenting is not a reason to stop the per-frame upkeep: the raster
123+
// thread would otherwise hold these pools for the life of the engine.
124+
EXPECT_EQ(impeller::CommandPoolRecyclerVK::GetGlobalPoolCount(*context), 0)
125+
<< "thread-local pools survived a frame the compositor presented";
126+
}
127+
128+
/// The default still renders to the surface, which is what a compositor-less
129+
/// embedder relies on.
130+
TEST(GPUSurfaceVulkanImpeller, PresentsWhenRenderingToSurface) {
131+
impeller::ContextVK::Settings context_settings;
132+
context_settings.proc_address_callback = vkGetInstanceProcAddr;
133+
context_settings.shader_libraries_data = ShaderLibraryMappings();
134+
auto context = impeller::ContextVK::Create(std::move(context_settings));
135+
136+
TestGPUSurfaceVulkanDelegate delegate;
137+
std::unique_ptr<Surface> surface =
138+
std::make_unique<GPUSurfaceVulkanImpeller>(&delegate, context);
139+
140+
auto frame = surface->AcquireFrame(DlISize(100, 100));
141+
ASSERT_NE(frame, nullptr);
142+
EXPECT_GT(delegate.acquire_count(), 0);
143+
}
144+
86145
TEST(GPUSurfaceVulkanImpeller, RecreatesTransientsWhenFrameSizeChanges) {
87146
impeller::ContextVK::Settings context_settings;
88147
context_settings.proc_address_callback = vkGetInstanceProcAddr;

‎engine/src/flutter/shell/platform/embedder/embedder_surface_vulkan_impeller.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ bool EmbedderSurfaceVulkanImpeller::IsValid() const {
118118

119119
// |EmbedderSurface|
120120
std::unique_ptr<Surface> EmbedderSurfaceVulkanImpeller::CreateGPUSurface() {
121-
return std::make_unique<GPUSurfaceVulkanImpeller>(this, context_);
121+
// The compositor presents when one is supplied; see GPUSurfaceVulkanImpeller.
122+
const bool render_to_surface = !external_view_embedder_;
123+
return std::make_unique<GPUSurfaceVulkanImpeller>(this, context_,
124+
render_to_surface);
122125
}
123126

124127
// |EmbedderSurface|

0 commit comments

Comments
 (0)