Skip to content

Commit

Permalink
Fix vulkan surface leaks. (flutter#24372)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dreveman and David Reveman authored Feb 12, 2021
1 parent 7376d18 commit 157797e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
29 changes: 21 additions & 8 deletions shell/platform/fuchsia/flutter/vulkan_surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ VulkanSurface::VulkanSurface(
scenic::Session* session,
const SkISize& size,
uint32_t buffer_id)
: vulkan_provider_(vulkan_provider),
session_(session),
buffer_id_(buffer_id),
wait_(this) {
: vulkan_provider_(vulkan_provider), session_(session), wait_(this) {
FML_DCHECK(session_);

if (!AllocateDeviceMemory(sysmem_allocator, std::move(context), size)) {
if (!AllocateDeviceMemory(sysmem_allocator, std::move(context), size,
buffer_id)) {
FML_DLOG(INFO) << "Could not allocate device memory.";
return;
}
Expand All @@ -135,6 +133,12 @@ VulkanSurface::VulkanSurface(
}

VulkanSurface::~VulkanSurface() {
if (image_id_) {
session_->Enqueue(scenic::NewReleaseResourceCmd(image_id_));
}
if (buffer_id_) {
session_->DeregisterBufferCollection(buffer_id_);
}
wait_.Cancel();
wait_.set_object(ZX_HANDLE_INVALID);
}
Expand Down Expand Up @@ -221,7 +225,8 @@ bool VulkanSurface::CreateFences() {
bool VulkanSurface::AllocateDeviceMemory(
fuchsia::sysmem::AllocatorSyncPtr& sysmem_allocator,
sk_sp<GrDirectContext> context,
const SkISize& size) {
const SkISize& size,
uint32_t buffer_id) {
if (size.isEmpty()) {
return false;
}
Expand All @@ -237,16 +242,24 @@ bool VulkanSurface::AllocateDeviceMemory(
status = vulkan_token->Sync();
LOG_AND_RETURN(status != ZX_OK, "Failed to sync token");

session_->RegisterBufferCollection(buffer_id_, std::move(scenic_token));
session_->RegisterBufferCollection(buffer_id, std::move(scenic_token));
buffer_id_ = buffer_id;

VkBufferCollectionCreateInfoFUCHSIA import_info;
import_info.collectionToken = vulkan_token.Unbind().TakeChannel().release();
VkBufferCollectionFUCHSIA collection;
if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().CreateBufferCollectionFUCHSIA(
vulkan_provider_.vk_device(), &import_info, nullptr, &collection_)) !=
vulkan_provider_.vk_device(), &import_info, nullptr, &collection)) !=
VK_SUCCESS) {
return false;
}

collection_ = {collection, [&vulkan_provider = vulkan_provider_](
VkBufferCollectionFUCHSIA collection) {
vulkan_provider.vk().DestroyBufferCollectionFUCHSIA(
vulkan_provider.vk_device(), collection, nullptr);
}};

VulkanImage vulkan_image;
LOG_AND_RETURN(!CreateVulkanImage(vulkan_provider_, size, &vulkan_image),
"Failed to create VkImage");
Expand Down
7 changes: 4 additions & 3 deletions shell/platform/fuchsia/flutter/vulkan_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ class VulkanSurface final : public SurfaceProducerSurface {

bool AllocateDeviceMemory(fuchsia::sysmem::AllocatorSyncPtr& sysmem_allocator,
sk_sp<GrDirectContext> context,
const SkISize& size);
const SkISize& size,
uint32_t buffer_id);

bool CreateVulkanImage(vulkan::VulkanProvider& vulkan_provider,
const SkISize& size,
Expand Down Expand Up @@ -174,9 +175,9 @@ class VulkanSurface final : public SurfaceProducerSurface {
VkMemoryAllocateInfo vk_memory_info_;
vulkan::VulkanHandle<VkFence> command_buffer_fence_;
sk_sp<SkSurface> sk_surface_;
const uint32_t buffer_id_;
uint32_t buffer_id_ = 0;
uint32_t image_id_ = 0;
VkBufferCollectionFUCHSIA collection_;
vulkan::VulkanHandle<VkBufferCollectionFUCHSIA> collection_;
zx::event acquire_event_;
vulkan::VulkanHandle<VkSemaphore> acquire_semaphore_;
std::unique_ptr<vulkan::VulkanCommandBuffer> command_buffer_;
Expand Down
1 change: 1 addition & 0 deletions vulkan/vulkan_proc_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ bool VulkanProcTable::SetupDeviceProcAddresses(
#endif // OS_ANDROID
#if OS_FUCHSIA
ACQUIRE_PROC(CreateBufferCollectionFUCHSIA, handle);
ACQUIRE_PROC(DestroyBufferCollectionFUCHSIA, handle);
ACQUIRE_PROC(GetMemoryZirconHandleFUCHSIA, handle);
ACQUIRE_PROC(ImportSemaphoreZirconHandleFUCHSIA, handle);
ACQUIRE_PROC(SetBufferCollectionConstraintsFUCHSIA, handle);
Expand Down
1 change: 1 addition & 0 deletions vulkan/vulkan_proc_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class VulkanProcTable : public fml::RefCountedThreadSafe<VulkanProcTable> {
#endif // OS_ANDROID
#if OS_FUCHSIA
DEFINE_PROC(CreateBufferCollectionFUCHSIA);
DEFINE_PROC(DestroyBufferCollectionFUCHSIA);
DEFINE_PROC(GetMemoryZirconHandleFUCHSIA);
DEFINE_PROC(ImportSemaphoreZirconHandleFUCHSIA);
DEFINE_PROC(SetBufferCollectionConstraintsFUCHSIA);
Expand Down

0 comments on commit 157797e

Please sign in to comment.