Skip to content

Commit 0b89480

Browse files
authored
[Impeller] Fix VkImageView leak in WrappedTextureSourceVK (#181966)
## Description Fixes #181967 `WrappedTextureSourceVK` in the embedder delegate path creates a `VkImageView` every frame via `createImageView()` but never destroys it. The handle is stored as a raw non-owning `vk::ImageView` and the destructor is empty, leaking one `VkImageView` per frame (~60/sec on a typical app). This switches to `vk::UniqueImageView` for RAII ownership so the image view is destroyed when the `WrappedTextureSourceVK` is released. ## Why this is safe The resource lifetime is correct because Impeller's `TrackedObjectsVK` holds a `shared_ptr` to the `TextureSourceVK` until the GPU fence signals completion: 1. `RenderPassVK` calls `Track(attachment.texture)` on all render target textures 2. `CommandBufferVK::Track()` stores `shared_ptr<TextureSourceVK>` in `TrackedObjectsVK` 3. `TrackedObjectsVK` is cleared only in the fence completion callback, after the GPU finishes The `VkImageView` cannot be destroyed while the GPU is still using it. ## Scope This only affects the **embedder delegate code path** (when `delegate_ != nullptr` in `GPUSurfaceVulkanImpeller::AcquireFrame`). Android passes `nullptr` as the delegate and uses `AcquireNextSurface()` instead, so this change has **no effect on Android, iOS, or standard swapchain rendering**. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I added a test for the change if applicable. *(See note below)* **Note on testing:** This is a resource leak fix in a Vulkan code path that requires a running Vulkan device and embedder delegate. I was unable to find existing test infrastructure for the embedder delegate path in `GPUSurfaceVulkanImpeller`. Happy to add a test if reviewers can point me to the right test harness. Found while working on Impeller Vulkan support for Linux/Wayland with a custom embedder. [Contributor Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md
1 parent ea4c736 commit 0b89480

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,29 @@ namespace flutter {
2525
class WrappedTextureSourceVK : public impeller::TextureSourceVK {
2626
public:
2727
explicit WrappedTextureSourceVK(impeller::vk::Image image,
28-
impeller::vk::ImageView image_view,
28+
impeller::vk::UniqueImageView image_view,
2929
impeller::TextureDescriptor desc)
30-
: TextureSourceVK(desc), image_(image), image_view_(image_view) {}
30+
: TextureSourceVK(desc),
31+
image_(image),
32+
image_view_(std::move(image_view)) {}
3133

32-
~WrappedTextureSourceVK() {}
34+
~WrappedTextureSourceVK() override = default;
3335

3436
private:
3537
impeller::vk::Image GetImage() const override { return image_; }
3638

37-
impeller::vk::ImageView GetImageView() const override { return image_view_; }
39+
impeller::vk::ImageView GetImageView() const override {
40+
return image_view_.get();
41+
}
3842

3943
impeller::vk::ImageView GetRenderTargetView() const override {
40-
return image_view_;
44+
return image_view_.get();
4145
}
4246

4347
bool IsSwapchainImage() const override { return true; }
4448

4549
impeller::vk::Image image_;
46-
impeller::vk::ImageView image_view_;
50+
impeller::vk::UniqueImageView image_view_;
4751
};
4852

4953
GPUSurfaceVulkanImpeller::GPUSurfaceVulkanImpeller(
@@ -177,7 +181,7 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkanImpeller::AcquireFrame(
177181
view_info.image = vk_image;
178182

179183
auto [result, image_view] =
180-
context_vk.GetDevice().createImageView(view_info);
184+
context_vk.GetDevice().createImageViewUnique(view_info);
181185
if (result != impeller::vk::Result::eSuccess) {
182186
FML_LOG(ERROR) << "Failed to create image view for provided image: "
183187
<< impeller::vk::to_string(result);
@@ -190,8 +194,8 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkanImpeller::AcquireFrame(
190194
/*enable_msaa=*/true);
191195
}
192196

193-
auto wrapped_onscreen =
194-
std::make_shared<WrappedTextureSourceVK>(vk_image, image_view, desc);
197+
auto wrapped_onscreen = std::make_shared<WrappedTextureSourceVK>(
198+
vk_image, std::move(image_view), desc);
195199
auto surface = impeller::SurfaceVK::WrapSwapchainImage(
196200
transients_, wrapped_onscreen, [&]() -> bool { return true; });
197201
impeller::RenderTarget render_target = surface->GetRenderTarget();

0 commit comments

Comments
 (0)