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

Commit 157797e

Browse files
drevemanDavid Reveman
andauthored
Fix vulkan surface leaks. (#24372)
This fixes 3 memory leaks: 1. Destroys local vulkan buffer collections. 2. Releases image2 resource in Scenic. 3. Deregister buffer collections from Scenic session. Co-authored-by: David Reveman <[email protected]>
1 parent 7376d18 commit 157797e

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

shell/platform/fuchsia/flutter/vulkan_surface.cc

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ VulkanSurface::VulkanSurface(
107107
scenic::Session* session,
108108
const SkISize& size,
109109
uint32_t buffer_id)
110-
: vulkan_provider_(vulkan_provider),
111-
session_(session),
112-
buffer_id_(buffer_id),
113-
wait_(this) {
110+
: vulkan_provider_(vulkan_provider), session_(session), wait_(this) {
114111
FML_DCHECK(session_);
115112

116-
if (!AllocateDeviceMemory(sysmem_allocator, std::move(context), size)) {
113+
if (!AllocateDeviceMemory(sysmem_allocator, std::move(context), size,
114+
buffer_id)) {
117115
FML_DLOG(INFO) << "Could not allocate device memory.";
118116
return;
119117
}
@@ -135,6 +133,12 @@ VulkanSurface::VulkanSurface(
135133
}
136134

137135
VulkanSurface::~VulkanSurface() {
136+
if (image_id_) {
137+
session_->Enqueue(scenic::NewReleaseResourceCmd(image_id_));
138+
}
139+
if (buffer_id_) {
140+
session_->DeregisterBufferCollection(buffer_id_);
141+
}
138142
wait_.Cancel();
139143
wait_.set_object(ZX_HANDLE_INVALID);
140144
}
@@ -221,7 +225,8 @@ bool VulkanSurface::CreateFences() {
221225
bool VulkanSurface::AllocateDeviceMemory(
222226
fuchsia::sysmem::AllocatorSyncPtr& sysmem_allocator,
223227
sk_sp<GrDirectContext> context,
224-
const SkISize& size) {
228+
const SkISize& size,
229+
uint32_t buffer_id) {
225230
if (size.isEmpty()) {
226231
return false;
227232
}
@@ -237,16 +242,24 @@ bool VulkanSurface::AllocateDeviceMemory(
237242
status = vulkan_token->Sync();
238243
LOG_AND_RETURN(status != ZX_OK, "Failed to sync token");
239244

240-
session_->RegisterBufferCollection(buffer_id_, std::move(scenic_token));
245+
session_->RegisterBufferCollection(buffer_id, std::move(scenic_token));
246+
buffer_id_ = buffer_id;
241247

242248
VkBufferCollectionCreateInfoFUCHSIA import_info;
243249
import_info.collectionToken = vulkan_token.Unbind().TakeChannel().release();
250+
VkBufferCollectionFUCHSIA collection;
244251
if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().CreateBufferCollectionFUCHSIA(
245-
vulkan_provider_.vk_device(), &import_info, nullptr, &collection_)) !=
252+
vulkan_provider_.vk_device(), &import_info, nullptr, &collection)) !=
246253
VK_SUCCESS) {
247254
return false;
248255
}
249256

257+
collection_ = {collection, [&vulkan_provider = vulkan_provider_](
258+
VkBufferCollectionFUCHSIA collection) {
259+
vulkan_provider.vk().DestroyBufferCollectionFUCHSIA(
260+
vulkan_provider.vk_device(), collection, nullptr);
261+
}};
262+
250263
VulkanImage vulkan_image;
251264
LOG_AND_RETURN(!CreateVulkanImage(vulkan_provider_, size, &vulkan_image),
252265
"Failed to create VkImage");

shell/platform/fuchsia/flutter/vulkan_surface.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ class VulkanSurface final : public SurfaceProducerSurface {
146146

147147
bool AllocateDeviceMemory(fuchsia::sysmem::AllocatorSyncPtr& sysmem_allocator,
148148
sk_sp<GrDirectContext> context,
149-
const SkISize& size);
149+
const SkISize& size,
150+
uint32_t buffer_id);
150151

151152
bool CreateVulkanImage(vulkan::VulkanProvider& vulkan_provider,
152153
const SkISize& size,
@@ -174,9 +175,9 @@ class VulkanSurface final : public SurfaceProducerSurface {
174175
VkMemoryAllocateInfo vk_memory_info_;
175176
vulkan::VulkanHandle<VkFence> command_buffer_fence_;
176177
sk_sp<SkSurface> sk_surface_;
177-
const uint32_t buffer_id_;
178+
uint32_t buffer_id_ = 0;
178179
uint32_t image_id_ = 0;
179-
VkBufferCollectionFUCHSIA collection_;
180+
vulkan::VulkanHandle<VkBufferCollectionFUCHSIA> collection_;
180181
zx::event acquire_event_;
181182
vulkan::VulkanHandle<VkSemaphore> acquire_semaphore_;
182183
std::unique_ptr<vulkan::VulkanCommandBuffer> command_buffer_;

vulkan/vulkan_proc_table.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ bool VulkanProcTable::SetupDeviceProcAddresses(
138138
#endif // OS_ANDROID
139139
#if OS_FUCHSIA
140140
ACQUIRE_PROC(CreateBufferCollectionFUCHSIA, handle);
141+
ACQUIRE_PROC(DestroyBufferCollectionFUCHSIA, handle);
141142
ACQUIRE_PROC(GetMemoryZirconHandleFUCHSIA, handle);
142143
ACQUIRE_PROC(ImportSemaphoreZirconHandleFUCHSIA, handle);
143144
ACQUIRE_PROC(SetBufferCollectionConstraintsFUCHSIA, handle);

vulkan/vulkan_proc_table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class VulkanProcTable : public fml::RefCountedThreadSafe<VulkanProcTable> {
116116
#endif // OS_ANDROID
117117
#if OS_FUCHSIA
118118
DEFINE_PROC(CreateBufferCollectionFUCHSIA);
119+
DEFINE_PROC(DestroyBufferCollectionFUCHSIA);
119120
DEFINE_PROC(GetMemoryZirconHandleFUCHSIA);
120121
DEFINE_PROC(ImportSemaphoreZirconHandleFUCHSIA);
121122
DEFINE_PROC(SetBufferCollectionConstraintsFUCHSIA);

0 commit comments

Comments
 (0)