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_;
76 TEST(CommandPoolRecyclerVKTest, ReclaimMakesCommandPoolAvailable) {
77 auto const context = MockVulkanContextBuilder().Build();
81 auto const recycler = context->GetCommandPoolRecycler();
82 auto const pool = recycler->Get();
91 auto waiter = fml::AutoResetWaitableEvent();
92 auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
100 std::thread thread([&]() {
101 auto const pool = context->GetCommandPoolRecycler()->Get();
102 EXPECT_NE(pool.get(),
nullptr);
108 auto const called = GetMockVulkanFunctions(context->GetDevice());
109 EXPECT_EQ(std::count(called->begin(), called->end(),
"vkCreateCommandPool"),
115 TEST(CommandPoolRecyclerVKTest, CommandBuffersAreRecycled) {
116 auto const context = MockVulkanContextBuilder().Build();
120 auto const recycler = context->GetCommandPoolRecycler();
121 auto pool = recycler->Get();
123 auto buffer = pool->CreateCommandBuffer();
124 pool->CollectCommandBuffer(std::move(buffer));
131 for (
auto i = 0u; i < 2u; i++) {
132 auto waiter = fml::AutoResetWaitableEvent();
133 auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
144 auto const recycler = context->GetCommandPoolRecycler();
145 auto pool = recycler->Get();
147 auto buffer = pool->CreateCommandBuffer();
148 pool->CollectCommandBuffer(std::move(buffer));
155 auto const called = GetMockVulkanFunctions(context->GetDevice());
156 EXPECT_EQ(std::count(called->begin(), called->end(),
"vkCreateCommandPool"),
159 std::count(called->begin(), called->end(),
"vkAllocateCommandBuffers"),