Flutter Impeller
command_encoder_vk_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <thread>
6 
7 #include "flutter/testing/testing.h" // IWYU pragma: keep
8 #include "fml/synchronization/waitable_event.h"
10 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
12 
13 namespace impeller {
14 namespace testing {
15 
16 TEST(CommandEncoderVKTest, DeleteEncoderAfterThreadDies) {
17  // Tests that when a CommandEncoderVK is deleted that it will clean up its
18  // command buffers before it cleans up its command pool.
19  std::shared_ptr<std::vector<std::string>> called_functions;
20  {
21  auto context = MockVulkanContextBuilder().Build();
22  called_functions = GetMockVulkanFunctions(context->GetDevice());
23  std::shared_ptr<CommandEncoderVK> encoder;
24  std::thread thread([&] {
25  CommandEncoderFactoryVK factory(context);
26  encoder = factory.Create();
27  });
28  thread.join();
29  context->Shutdown();
30  }
31  auto destroy_pool =
32  std::find(called_functions->begin(), called_functions->end(),
33  "vkDestroyCommandPool");
34  auto free_buffers =
35  std::find(called_functions->begin(), called_functions->end(),
36  "vkFreeCommandBuffers");
37  EXPECT_TRUE(destroy_pool != called_functions->end());
38  EXPECT_TRUE(free_buffers != called_functions->end());
39  EXPECT_TRUE(free_buffers < destroy_pool);
40 }
41 
42 TEST(CommandEncoderVKTest, CleanupAfterSubmit) {
43  // This tests deleting the TrackedObjects where the thread is killed before
44  // the fence waiter has disposed of them, making sure the command buffer and
45  // its pools are deleted in that order.
46  std::shared_ptr<std::vector<std::string>> called_functions;
47  {
48  fml::AutoResetWaitableEvent wait_for_submit;
49  fml::AutoResetWaitableEvent wait_for_thread_join;
50  auto context = MockVulkanContextBuilder().Build();
51  std::thread thread([&] {
52  auto buffer = context->CreateCommandBuffer();
53  context->GetCommandQueue()->Submit(
54  {buffer}, [&](CommandBuffer::Status status) {
55  ASSERT_EQ(status, CommandBuffer::Status::kCompleted);
56  wait_for_thread_join.Wait();
57  wait_for_submit.Signal();
58  });
59  });
60  thread.join();
61  wait_for_thread_join.Signal();
62  wait_for_submit.Wait();
63  called_functions = GetMockVulkanFunctions(context->GetDevice());
64  context->Shutdown();
65  }
66 
67  auto destroy_pool =
68  std::find(called_functions->begin(), called_functions->end(),
69  "vkDestroyCommandPool");
70  auto free_buffers =
71  std::find(called_functions->begin(), called_functions->end(),
72  "vkFreeCommandBuffers");
73  EXPECT_TRUE(destroy_pool != called_functions->end());
74  EXPECT_TRUE(free_buffers != called_functions->end());
75  EXPECT_TRUE(free_buffers < destroy_pool);
76 }
77 
78 } // namespace testing
79 } // namespace impeller
command_encoder_vk.h
impeller::CommandEncoderFactoryVK
Definition: command_encoder_vk.h:32
impeller::testing::TEST
TEST(CanvasRecorder, Save)
Definition: canvas_recorder_unittests.cc:65
command_buffer.h
impeller::CommandBuffer::Status::kCompleted
@ kCompleted
impeller::CommandBuffer::Status
Status
Definition: command_buffer.h:49
impeller
Definition: aiks_blur_unittests.cc:20
impeller::CommandEncoderFactoryVK::Create
std::shared_ptr< CommandEncoderVK > Create()
Definition: command_encoder_vk.cc:27