5 #include "flutter/fml/synchronization/waitable_event.h"
6 #include "flutter/testing/testing.h"
11 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
16 TEST(ContextVKTest, CommonHardwareConcurrencyConfigurations) {
29 TEST(ContextVKTest, DeletesCommandPools) {
30 std::weak_ptr<ContextVK> weak_context;
31 std::weak_ptr<CommandPoolVK> weak_pool;
33 std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
34 auto const pool = context->GetCommandPoolRecycler()->Get();
36 weak_context = context;
37 ASSERT_TRUE(weak_pool.lock());
38 ASSERT_TRUE(weak_context.lock());
40 ASSERT_FALSE(weak_pool.lock());
41 ASSERT_FALSE(weak_context.lock());
44 TEST(ContextVKTest, DeletesCommandPoolsOnAllThreads) {
45 std::weak_ptr<ContextVK> weak_context;
46 std::weak_ptr<CommandPoolVK> weak_pool_main;
48 std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
49 weak_pool_main = context->GetCommandPoolRecycler()->Get();
50 weak_context = context;
51 ASSERT_TRUE(weak_pool_main.lock());
52 ASSERT_TRUE(weak_context.lock());
55 fml::AutoResetWaitableEvent latch1, latch2;
56 std::weak_ptr<CommandPoolVK> weak_pool_thread;
57 std::thread thread([&]() {
58 weak_pool_thread = context->GetCommandPoolRecycler()->Get();
66 ASSERT_FALSE(weak_pool_main.lock());
67 ASSERT_FALSE(weak_context.lock());
72 ASSERT_FALSE(weak_pool_thread.lock());
75 TEST(ContextVKTest, DeletePipelineAfterContext) {
76 std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline;
77 std::shared_ptr<std::vector<std::string>> functions;
79 std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
83 context->GetPipelineLibrary()->GetPipeline(pipeline_desc);
84 pipeline = pipeline_future.
Get();
85 ASSERT_TRUE(pipeline);
86 functions = GetMockVulkanFunctions(context->GetDevice());
87 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
88 "vkCreateGraphicsPipelines") != functions->end());
90 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
91 "vkDestroyDevice") != functions->end());
94 TEST(ContextVKTest, DeleteShaderFunctionAfterContext) {
95 std::shared_ptr<const ShaderFunction> shader_function;
96 std::shared_ptr<std::vector<std::string>> functions;
98 std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
101 std::vector<uint8_t> data = {0x03, 0x02, 0x23, 0x07};
102 context->GetShaderLibrary()->RegisterFunction(
104 std::make_shared<fml::DataMapping>(data), [](
bool) {});
105 shader_function = context->GetShaderLibrary()->GetFunction(
107 ASSERT_TRUE(shader_function);
108 functions = GetMockVulkanFunctions(context->GetDevice());
109 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
110 "vkCreateShaderModule") != functions->end());
112 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
113 "vkDestroyDevice") != functions->end());
116 TEST(ContextVKTest, DeletePipelineLibraryAfterContext) {
117 std::shared_ptr<PipelineLibrary> pipeline_library;
118 std::shared_ptr<std::vector<std::string>> functions;
120 std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
123 pipeline_library = context->GetPipelineLibrary();
124 functions = GetMockVulkanFunctions(context->GetDevice());
125 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
126 "vkCreatePipelineCache") != functions->end());
128 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
129 "vkDestroyDevice") != functions->end());
132 TEST(ContextVKTest, CanCreateContextInAbsenceOfValidationLayers) {
135 auto context = MockVulkanContextBuilder()
136 .SetSettingsCallback([](
auto& settings) {
137 settings.enable_validation =
true;
140 ASSERT_NE(context,
nullptr);
142 reinterpret_cast<const CapabilitiesVK*
>(context->GetCapabilities().get());
146 TEST(ContextVKTest, CanCreateContextWithValidationLayers) {
148 MockVulkanContextBuilder()
149 .SetSettingsCallback(
150 [](
auto& settings) { settings.enable_validation =
true; })
151 .SetInstanceExtensions(
152 {
"VK_KHR_surface",
"VK_MVK_macos_surface",
"VK_EXT_debug_utils"})
153 .SetInstanceLayers({
"VK_LAYER_KHRONOS_validation"})
155 ASSERT_NE(context,
nullptr);
157 reinterpret_cast<const CapabilitiesVK*
>(context->GetCapabilities().get());
164 TEST(CapabilitiesVKTest, ContextInitializesWithNoStencilFormat) {
165 const std::shared_ptr<ContextVK> context =
166 MockVulkanContextBuilder()
167 .SetPhysicalDeviceFormatPropertiesCallback(
168 [](VkPhysicalDevice physicalDevice, VkFormat format,
169 VkFormatProperties* pFormatProperties) {
170 if (format == VK_FORMAT_B8G8R8A8_UNORM) {
171 pFormatProperties->optimalTilingFeatures =
172 static_cast<VkFormatFeatureFlags
>(
173 vk::FormatFeatureFlagBits::eColorAttachment);
174 }
else if (format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
175 pFormatProperties->optimalTilingFeatures =
176 static_cast<VkFormatFeatureFlags
>(
177 vk::FormatFeatureFlagBits::eDepthStencilAttachment);
182 ASSERT_NE(context,
nullptr);
184 reinterpret_cast<const CapabilitiesVK*
>(context->GetCapabilities().get());
196 ContextFailsInitializationForNoCombinedDepthStencilFormat) {
198 const std::shared_ptr<ContextVK> context =
199 MockVulkanContextBuilder()
200 .SetPhysicalDeviceFormatPropertiesCallback(
201 [](VkPhysicalDevice physicalDevice, VkFormat format,
202 VkFormatProperties* pFormatProperties) {
203 if (format == VK_FORMAT_B8G8R8A8_UNORM) {
204 pFormatProperties->optimalTilingFeatures =
205 static_cast<VkFormatFeatureFlags
>(
206 vk::FormatFeatureFlagBits::eColorAttachment);
211 ASSERT_EQ(context,
nullptr);
214 TEST(ContextVKTest, WarmUpFunctionCreatesRenderPass) {
215 const std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
218 context->InitializeCommonlyUsedShadersIfNeeded();
220 auto functions = GetMockVulkanFunctions(context->GetDevice());
221 ASSERT_TRUE(std::find(functions->begin(), functions->end(),
222 "vkCreateRenderPass") != functions->end());