|
| 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 | +#define FML_USED_ON_EMBEDDER |
| 6 | + |
| 7 | +#include <cstring> |
| 8 | +#include <string> |
| 9 | +#include <utility> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include "embedder.h" |
| 13 | +#include "embedder_engine.h" |
| 14 | +#include "flutter/fml/synchronization/count_down_latch.h" |
| 15 | +#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h" |
| 16 | +#include "flutter/shell/platform/embedder/tests/embedder_test.h" |
| 17 | +#include "flutter/shell/platform/embedder/tests/embedder_test_context_vulkan.h" |
| 18 | +#include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h" |
| 19 | +#include "flutter/testing/testing.h" |
| 20 | + |
| 21 | +// CREATE_NATIVE_ENTRY is leaky by design |
| 22 | +// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape) |
| 23 | + |
| 24 | +namespace flutter { |
| 25 | +namespace testing { |
| 26 | + |
| 27 | +using EmbedderTest = testing::EmbedderTest; |
| 28 | + |
| 29 | +//////////////////////////////////////////////////////////////////////////////// |
| 30 | +// Notice: Other Vulkan unit tests exist in embedder_gl_unittests.cc. |
| 31 | +// See https://github.com/flutter/flutter/issues/134322 |
| 32 | +//////////////////////////////////////////////////////////////////////////////// |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +struct VulkanProcInfo { |
| 37 | + decltype(vkGetInstanceProcAddr)* get_instance_proc_addr = nullptr; |
| 38 | + decltype(vkGetDeviceProcAddr)* get_device_proc_addr = nullptr; |
| 39 | + decltype(vkQueueSubmit)* queue_submit_proc_addr = nullptr; |
| 40 | + bool did_call_queue_submit = false; |
| 41 | +}; |
| 42 | + |
| 43 | +static_assert(std::is_trivially_destructible_v<VulkanProcInfo>); |
| 44 | + |
| 45 | +VulkanProcInfo g_vulkan_proc_info; |
| 46 | + |
| 47 | +VkResult QueueSubmit(VkQueue queue, |
| 48 | + uint32_t submitCount, |
| 49 | + const VkSubmitInfo* pSubmits, |
| 50 | + VkFence fence) { |
| 51 | + FML_DCHECK(g_vulkan_proc_info.queue_submit_proc_addr != nullptr); |
| 52 | + g_vulkan_proc_info.did_call_queue_submit = true; |
| 53 | + return g_vulkan_proc_info.queue_submit_proc_addr(queue, submitCount, pSubmits, |
| 54 | + fence); |
| 55 | +} |
| 56 | + |
| 57 | +template <size_t N> |
| 58 | +int StrcmpFixed(const char* str1, const char (&str2)[N]) { |
| 59 | + return strncmp(str1, str2, N - 1); |
| 60 | +} |
| 61 | + |
| 62 | +PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) { |
| 63 | + FML_DCHECK(g_vulkan_proc_info.get_device_proc_addr != nullptr); |
| 64 | + if (StrcmpFixed(pName, "vkQueueSubmit") == 0) { |
| 65 | + g_vulkan_proc_info.queue_submit_proc_addr = |
| 66 | + reinterpret_cast<decltype(vkQueueSubmit)*>( |
| 67 | + g_vulkan_proc_info.get_device_proc_addr(device, pName)); |
| 68 | + return reinterpret_cast<PFN_vkVoidFunction>(QueueSubmit); |
| 69 | + } |
| 70 | + return g_vulkan_proc_info.get_device_proc_addr(device, pName); |
| 71 | +} |
| 72 | + |
| 73 | +PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) { |
| 74 | + FML_DCHECK(g_vulkan_proc_info.get_instance_proc_addr != nullptr); |
| 75 | + if (StrcmpFixed(pName, "vkGetDeviceProcAddr") == 0) { |
| 76 | + g_vulkan_proc_info.get_device_proc_addr = |
| 77 | + reinterpret_cast<decltype(vkGetDeviceProcAddr)*>( |
| 78 | + g_vulkan_proc_info.get_instance_proc_addr(instance, pName)); |
| 79 | + return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr); |
| 80 | + } |
| 81 | + return g_vulkan_proc_info.get_instance_proc_addr(instance, pName); |
| 82 | +} |
| 83 | + |
| 84 | +template <typename T, typename U> |
| 85 | +struct CheckSameSignature : std::false_type {}; |
| 86 | + |
| 87 | +template <typename Ret, typename... Args> |
| 88 | +struct CheckSameSignature<Ret(Args...), Ret(Args...)> : std::true_type {}; |
| 89 | + |
| 90 | +static_assert(CheckSameSignature<decltype(GetInstanceProcAddr), |
| 91 | + decltype(vkGetInstanceProcAddr)>::value); |
| 92 | +static_assert(CheckSameSignature<decltype(GetDeviceProcAddr), |
| 93 | + decltype(vkGetDeviceProcAddr)>::value); |
| 94 | +static_assert( |
| 95 | + CheckSameSignature<decltype(QueueSubmit), decltype(vkQueueSubmit)>::value); |
| 96 | +} // namespace |
| 97 | + |
| 98 | +TEST_F(EmbedderTest, CanSwapOutVulkanCalls) { |
| 99 | + auto& context = GetEmbedderContext(EmbedderTestContextType::kVulkanContext); |
| 100 | + fml::AutoResetWaitableEvent latch; |
| 101 | + context.AddIsolateCreateCallback([&latch]() { latch.Signal(); }); |
| 102 | + EmbedderConfigBuilder builder(context); |
| 103 | + builder.SetVulkanRendererConfig( |
| 104 | + SkISize::Make(1024, 1024), |
| 105 | + [](void* user_data, FlutterVulkanInstanceHandle instance, |
| 106 | + const char* name) -> void* { |
| 107 | + if (StrcmpFixed(name, "vkGetInstanceProcAddr") == 0) { |
| 108 | + g_vulkan_proc_info.get_instance_proc_addr = |
| 109 | + reinterpret_cast<decltype(vkGetInstanceProcAddr)*>( |
| 110 | + EmbedderTestContextVulkan::InstanceProcAddr(user_data, |
| 111 | + instance, name)); |
| 112 | + return reinterpret_cast<void*>(GetInstanceProcAddr); |
| 113 | + } |
| 114 | + return EmbedderTestContextVulkan::InstanceProcAddr(user_data, instance, |
| 115 | + name); |
| 116 | + }); |
| 117 | + auto engine = builder.LaunchEngine(); |
| 118 | + ASSERT_TRUE(engine.is_valid()); |
| 119 | + // Wait for the root isolate to launch. |
| 120 | + latch.Wait(); |
| 121 | + engine.reset(); |
| 122 | + EXPECT_TRUE(g_vulkan_proc_info.did_call_queue_submit); |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace testing |
| 126 | +} // namespace flutter |
| 127 | + |
| 128 | +// NOLINTEND(clang-analyzer-core.StackAddressEscape) |
0 commit comments