Skip to content

Commit b013e41

Browse files
[Impeller] keep device holder and allocator alive until last vk image is destroyed. (flutter#166725)
Fixes flutter#166410 keep the potentially shutdown context alive until the last texture is reclaimed, to work around issues where pending UI tasks aren't flushed during platform view shutdown.
1 parent d3be3de commit b013e41

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#include "impeller/renderer/backend/vulkan/allocator_vk.h"
66

77
#include <memory>
8+
#include <utility>
89

910
#include "flutter/fml/memory/ref_ptr.h"
1011
#include "flutter/fml/trace_event.h"
1112
#include "impeller/base/allocation_size.h"
1213
#include "impeller/core/formats.h"
1314
#include "impeller/renderer/backend/vulkan/capabilities_vk.h"
1415
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
16+
#include "impeller/renderer/backend/vulkan/device_holder_vk.h"
1517
#include "impeller/renderer/backend/vulkan/formats_vk.h"
1618
#include "impeller/renderer/backend/vulkan/texture_vk.h"
1719
#include "vulkan/vulkan_enums.hpp"
@@ -405,9 +407,10 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
405407
return;
406408
}
407409

408-
resource_.Swap(ImageResource(ImageVMA{allocator, allocation, image},
409-
std::move(image_view),
410-
std::move(rt_image_view)));
410+
resource_.Swap(ImageResource(
411+
ImageVMA{allocator, allocation, image}, std::move(image_view),
412+
std::move(rt_image_view), context.GetResourceAllocator(),
413+
context.GetDeviceHolder()));
411414
is_valid_ = true;
412415
}
413416

@@ -429,6 +432,8 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
429432

430433
private:
431434
struct ImageResource {
435+
std::shared_ptr<DeviceHolderVK> device_holder;
436+
std::shared_ptr<Allocator> allocator;
432437
UniqueImageVMA image;
433438
vk::UniqueImageView image_view;
434439
vk::UniqueImageView rt_image_view;
@@ -437,8 +442,12 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
437442

438443
ImageResource(ImageVMA p_image,
439444
vk::UniqueImageView p_image_view,
440-
vk::UniqueImageView p_rt_image_view)
441-
: image(p_image),
445+
vk::UniqueImageView p_rt_image_view,
446+
std::shared_ptr<Allocator> allocator,
447+
std::shared_ptr<DeviceHolderVK> device_holder)
448+
: device_holder(std::move(device_holder)),
449+
allocator(std::move(allocator)),
450+
image(p_image),
442451
image_view(std::move(p_image_view)),
443452
rt_image_view(std::move(p_rt_image_view)) {}
444453

engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk_unittests.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "flutter/testing/testing.h" // IWYU pragma: keep
66
#include "gtest/gtest.h"
77
#include "impeller/base/allocation_size.h"
8+
#include "impeller/core/allocator.h"
89
#include "impeller/core/device_buffer.h"
910
#include "impeller/core/device_buffer_descriptor.h"
1011
#include "impeller/core/formats.h"
@@ -73,6 +74,25 @@ TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
7374
EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
7475
}
7576

77+
TEST(AllocatorVKTest, ImageResourceKeepsVulkanDeviceAlive) {
78+
std::shared_ptr<Texture> texture;
79+
std::weak_ptr<Allocator> weak_allocator;
80+
{
81+
auto const context = MockVulkanContextBuilder().Build();
82+
weak_allocator = context->GetResourceAllocator();
83+
auto allocator = context->GetResourceAllocator();
84+
85+
texture = allocator->CreateTexture(TextureDescriptor{
86+
.storage_mode = StorageMode::kDevicePrivate,
87+
.format = PixelFormat::kR8G8B8A8UNormInt,
88+
.size = {1, 1},
89+
});
90+
context->Shutdown();
91+
}
92+
93+
ASSERT_TRUE(weak_allocator.lock());
94+
}
95+
7696
#ifdef IMPELLER_DEBUG
7797

7898
TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {

engine/src/flutter/impeller/renderer/backend/vulkan/texture_source_vk.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
1313
#include "impeller/renderer/backend/vulkan/vk.h"
1414
#include "impeller/renderer/backend/vulkan/yuv_conversion_vk.h"
15-
#include "vulkan/vulkan_handles.hpp"
1615

1716
namespace impeller {
1817

0 commit comments

Comments
 (0)