5 #include "flutter/testing/testing.h"
6 #include "fml/synchronization/waitable_event.h"
9 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
14 TEST(CommandPoolRecyclerVKTest, GetsACommandPoolPerThread) {
15 auto const context = MockVulkanContextBuilder().Build();
23 std::shared_ptr<CommandPoolVK> pool1;
24 std::shared_ptr<CommandPoolVK> pool2;
28 [&]() { pool1 = context->GetCommandPoolRecycler()->Get(); });
31 [&]() { pool2 = context->GetCommandPoolRecycler()->Get(); });
37 EXPECT_NE(pool1, pool2);
43 TEST(CommandPoolRecyclerVKTest, GetsTheSameCommandPoolOnSameThread) {
44 auto const context = MockVulkanContextBuilder().Build();
46 auto const pool1 = context->GetCommandPoolRecycler()->Get();
47 auto const pool2 = context->GetCommandPoolRecycler()->Get();
50 EXPECT_EQ(pool1.get(), pool2.get());
60 class DeathRattle final {
62 explicit DeathRattle(std::function<
void()> callback)
63 : callback_(
std::move(callback)) {}
65 DeathRattle(DeathRattle&&) =
default;
66 DeathRattle& operator=(DeathRattle&&) =
default;
68 ~DeathRattle() { callback_(); }
71 std::function<void()> callback_;
75 void WaitForReclaim(
const std::shared_ptr<ContextVK>& context) {
85 for (
int i = 0; i < 2; i++) {
86 auto waiter = fml::AutoResetWaitableEvent();
87 auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
89 UniqueResourceVKT<DeathRattle> resource(context->GetResourceManager(),
99 std::shared_ptr<std::vector<std::string>> ReclaimAndGetMockVulkanFunctions(
100 const std::shared_ptr<ContextVK>& context) {
101 WaitForReclaim(context);
102 return GetMockVulkanFunctions(context->GetDevice());
107 TEST(CommandPoolRecyclerVKTest, ReclaimMakesCommandPoolAvailable) {
108 auto const context = MockVulkanContextBuilder().Build();
112 auto const recycler = context->GetCommandPoolRecycler();
113 auto const pool = recycler->Get();
119 WaitForReclaim(context);
122 std::thread thread([&]() {
123 auto const pool = context->GetCommandPoolRecycler()->Get();
124 EXPECT_NE(pool.get(),
nullptr);
130 auto const called = ReclaimAndGetMockVulkanFunctions(context);
131 EXPECT_EQ(std::count(called->begin(), called->end(),
"vkCreateCommandPool"),
137 TEST(CommandPoolRecyclerVKTest, CommandBuffersAreRecycled) {
138 auto const context = MockVulkanContextBuilder().Build();
142 auto const recycler = context->GetCommandPoolRecycler();
143 auto pool = recycler->Get();
145 auto buffer = pool->CreateCommandBuffer();
146 pool->CollectCommandBuffer(std::move(buffer));
152 WaitForReclaim(context);
157 auto const recycler = context->GetCommandPoolRecycler();
158 auto pool = recycler->Get();
160 auto buffer = pool->CreateCommandBuffer();
161 pool->CollectCommandBuffer(std::move(buffer));
168 auto const called = ReclaimAndGetMockVulkanFunctions(context);
169 EXPECT_EQ(std::count(called->begin(), called->end(),
"vkCreateCommandPool"),
172 std::count(called->begin(), called->end(),
"vkAllocateCommandBuffers"),
178 TEST(CommandPoolRecyclerVKTest, ExtraCommandBufferAllocationsTriggerTrim) {
179 auto const context = MockVulkanContextBuilder().Build();
183 auto const recycler = context->GetCommandPoolRecycler();
184 auto pool = recycler->Get();
187 for (
auto i = 0; i < 64; i++) {
188 auto buffer = pool->CreateCommandBuffer();
189 pool->CollectCommandBuffer(std::move(buffer));
197 auto called = ReclaimAndGetMockVulkanFunctions(context);
198 EXPECT_EQ(std::count(called->begin(), called->end(),
"vkResetCommandPool"),
204 auto const recycler = context->GetCommandPoolRecycler();
205 auto pool = recycler->Get();
214 called = ReclaimAndGetMockVulkanFunctions(context);
215 EXPECT_EQ(std::count(called->begin(), called->end(),
216 "vkResetCommandPoolReleaseResources"),
222 TEST(CommandPoolRecyclerVKTest, RecyclerGlobalPoolMapSize) {
223 auto context = MockVulkanContextBuilder().Build();
224 auto const recycler = context->GetCommandPoolRecycler();
230 auto pool = recycler->Get();
static int GetGlobalPoolCount(const ContextVK &context)
TEST(AllocationSizeTest, CanCreateTypedAllocations)