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

Commit 133b948

Browse files
author
David Reveman
committed
Fix vulkan surface leaks.
This fixes 3 memory leaks: 1. Destroys local vulkan buffer collections. 2. Releases image2 resource in Scenic. 3. Deregister buffer collections from Scenic session.
1 parent b3a5cd1 commit 133b948

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

shell/platform/fuchsia/flutter/vulkan_surface.cc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ VulkanSurface::VulkanSurface(
109109
uint32_t buffer_id)
110110
: vulkan_provider_(vulkan_provider),
111111
session_(session),
112-
buffer_id_(buffer_id),
113112
wait_(this) {
114113
FML_DCHECK(session_);
115114

116-
if (!AllocateDeviceMemory(sysmem_allocator, std::move(context), size)) {
115+
if (!AllocateDeviceMemory(sysmem_allocator, std::move(context), size, buffer_id)) {
117116
FML_DLOG(INFO) << "Could not allocate device memory.";
118117
return;
119118
}
@@ -135,6 +134,12 @@ VulkanSurface::VulkanSurface(
135134
}
136135

137136
VulkanSurface::~VulkanSurface() {
137+
if (image_id_) {
138+
session_->Enqueue(scenic::NewReleaseResourceCmd(image_id_));
139+
}
140+
if (buffer_id_) {
141+
session_->DeregisterBufferCollection(buffer_id_);
142+
}
138143
wait_.Cancel();
139144
wait_.set_object(ZX_HANDLE_INVALID);
140145
}
@@ -221,7 +226,8 @@ bool VulkanSurface::CreateFences() {
221226
bool VulkanSurface::AllocateDeviceMemory(
222227
fuchsia::sysmem::AllocatorSyncPtr& sysmem_allocator,
223228
sk_sp<GrDirectContext> context,
224-
const SkISize& size) {
229+
const SkISize& size,
230+
uint32_t buffer_id) {
225231
if (size.isEmpty()) {
226232
return false;
227233
}
@@ -237,16 +243,24 @@ bool VulkanSurface::AllocateDeviceMemory(
237243
status = vulkan_token->Sync();
238244
LOG_AND_RETURN(status != ZX_OK, "Failed to sync token");
239245

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

242249
VkBufferCollectionCreateInfoFUCHSIA import_info;
243250
import_info.collectionToken = vulkan_token.Unbind().TakeChannel().release();
251+
VkBufferCollectionFUCHSIA collection;
244252
if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().CreateBufferCollectionFUCHSIA(
245-
vulkan_provider_.vk_device(), &import_info, nullptr, &collection_)) !=
253+
vulkan_provider_.vk_device(), &import_info, nullptr, &collection)) !=
246254
VK_SUCCESS) {
247255
return false;
248256
}
249257

258+
collection_ = {collection,
259+
[&vulkan_provider = vulkan_provider_](VkBufferCollectionFUCHSIA collection) {
260+
vulkan_provider.vk().DestroyBufferCollectionFUCHSIA(
261+
vulkan_provider.vk_device(), collection, nullptr);
262+
}};
263+
250264
VulkanImage vulkan_image;
251265
LOG_AND_RETURN(!CreateVulkanImage(vulkan_provider_, size, &vulkan_image),
252266
"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)