22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5+ #include < algorithm>
56#include < cassert>
67#include < memory>
78#include < optional>
9+ #include < string_view>
810
911#include " flutter/flutter_vma/flutter_skia_vma.h"
1012#include " flutter/fml/logging.h"
2325
2426namespace flutter ::testing {
2527
28+ namespace {
29+
30+ // / The surface extensions Impeller requires of an embedder's instance:
31+ // / VK_KHR_surface plus at least one window-system extension. Neither is used to
32+ // / present here -- the tests render into images -- but Impeller refuses a
33+ // / context without them. Only extensions the ICD offers are returned, so an ICD
34+ // / without them still yields a context for the Skia tests.
35+ std::vector<std::string> SurfaceInstanceExtensions (
36+ const vulkan::VulkanProcTable& vk) {
37+ uint32_t count = 0 ;
38+ if (vk.EnumerateInstanceExtensionProperties (nullptr , &count, nullptr ) !=
39+ VK_SUCCESS ||
40+ count == 0 ) {
41+ return {};
42+ }
43+ std::vector<VkExtensionProperties> properties (count);
44+ if (vk.EnumerateInstanceExtensionProperties (
45+ nullptr , &count, properties.data ()) != VK_SUCCESS ) {
46+ return {};
47+ }
48+ const auto supported = [&](std::string_view name) {
49+ return std::any_of (properties.begin (), properties.begin () + count,
50+ [&](const VkExtensionProperties& p) {
51+ return name == p.extensionName ;
52+ });
53+ };
54+
55+ if (!supported (VK_KHR_SURFACE_EXTENSION_NAME )) {
56+ return {};
57+ }
58+ // Any one of them satisfies Impeller; which exist depends on the ICD and the
59+ // host, so ask for the first that is offered rather than assuming Linux.
60+ for (const char * wsi : {" VK_KHR_xcb_surface" , " VK_KHR_xlib_surface" ,
61+ " VK_KHR_wayland_surface" , " VK_KHR_win32_surface" ,
62+ " VK_EXT_metal_surface" , " VK_MVK_macos_surface" }) {
63+ if (supported (wsi)) {
64+ return {VK_KHR_SURFACE_EXTENSION_NAME , wsi};
65+ }
66+ }
67+ return {};
68+ }
69+
70+ } // namespace
71+
2672TestVulkanContext::TestVulkanContext () {
2773 // ---------------------------------------------------------------------------
2874 // Initialize basic Vulkan state using the Swiftshader ICD.
@@ -46,8 +92,9 @@ TestVulkanContext::TestVulkanContext() {
4692 return ;
4793 }
4894
95+ enabled_instance_extensions_ = SurfaceInstanceExtensions (*vk_);
4996 application_ = std::make_unique<vulkan::VulkanApplication>(
50- *vk_, " Flutter Unittests" , std::vector<std::string>{} ,
97+ *vk_, " Flutter Unittests" , enabled_instance_extensions_ ,
5198 VK_MAKE_VERSION (1 , 0 , 0 ), VK_MAKE_VERSION (1 , 1 , 0 ), true );
5299 if (!application_->IsValid ()) {
53100 FML_LOG (ERROR ) << " Failed to initialize basic Vulkan state." ;
@@ -58,12 +105,19 @@ TestVulkanContext::TestVulkanContext() {
58105 return ;
59106 }
60107
61- device_ = application_-> AcquireFirstCompatibleLogicalDevice ();
108+ device_ = CreateLogicalDevice ();
62109 if (!device_ || !device_->IsValid ()) {
63110 FML_LOG (ERROR ) << " Failed to create compatible logical device." ;
64111 return ;
65112 }
66113
114+ for (const auto & name : enabled_instance_extensions_) {
115+ enabled_instance_extension_names_.push_back (name.c_str ());
116+ }
117+ for (const auto & name : enabled_device_extensions_) {
118+ enabled_device_extension_names_.push_back (name.c_str ());
119+ }
120+
67121 // ---------------------------------------------------------------------------
68122 // Create a Skia context.
69123 // For creating SkSurfaces from VkImages and snapshotting them, etc.
@@ -107,6 +161,104 @@ TestVulkanContext::TestVulkanContext() {
107161 context_ = GrDirectContexts::MakeVulkan (backend_context, options);
108162}
109163
164+ std::unique_ptr<vulkan::VulkanDevice> TestVulkanContext::CreateLogicalDevice () {
165+ const VkInstance instance = application_->GetInstance ();
166+ uint32_t device_count = 0 ;
167+ if (vk_->EnumeratePhysicalDevices (instance, &device_count, nullptr ) !=
168+ VK_SUCCESS ||
169+ device_count == 0 ) {
170+ return nullptr ;
171+ }
172+ std::vector<VkPhysicalDevice> physical_devices (device_count);
173+ if (vk_->EnumeratePhysicalDevices (instance, &device_count,
174+ physical_devices.data ()) != VK_SUCCESS ) {
175+ return nullptr ;
176+ }
177+
178+ // Impeller's embedder path also requires VK_KHR_swapchain on the device.
179+ // The proc table cannot enumerate device extensions, so ask for it and fall
180+ // back to a device without it if the ICD refuses.
181+ const std::vector<std::vector<std::string>> attempts =
182+ enabled_instance_extensions_.empty ()
183+ ? std::vector<std::vector<std::string>>{{}}
184+ : std::vector<std::vector<std::string>>{
185+ {VK_KHR_SWAPCHAIN_EXTENSION_NAME }, {}};
186+
187+ for (VkPhysicalDevice physical_device : physical_devices) {
188+ uint32_t family_count = 0 ;
189+ vk_->GetPhysicalDeviceQueueFamilyProperties (physical_device, &family_count,
190+ nullptr );
191+ std::vector<VkQueueFamilyProperties> families (family_count);
192+ vk_->GetPhysicalDeviceQueueFamilyProperties (physical_device, &family_count,
193+ families.data ());
194+ uint32_t queue_family = family_count;
195+ for (uint32_t i = 0 ; i < family_count; i++) {
196+ if (families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT ) {
197+ queue_family = i;
198+ break ;
199+ }
200+ }
201+ if (queue_family == family_count) {
202+ continue ;
203+ }
204+
205+ const float priority = 1 .0f ;
206+ const VkDeviceQueueCreateInfo queue_info = {
207+ .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO ,
208+ .queueFamilyIndex = queue_family,
209+ .queueCount = 1 ,
210+ .pQueuePriorities = &priority,
211+ };
212+
213+ for (const auto & extensions : attempts) {
214+ std::vector<const char *> names;
215+ names.reserve (extensions.size ());
216+ for (const auto & name : extensions) {
217+ names.push_back (name.c_str ());
218+ }
219+ const VkDeviceCreateInfo create_info = {
220+ .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO ,
221+ .queueCreateInfoCount = 1 ,
222+ .pQueueCreateInfos = &queue_info,
223+ .enabledExtensionCount = static_cast <uint32_t >(names.size ()),
224+ .ppEnabledExtensionNames = names.data (),
225+ };
226+ VkDevice device = VK_NULL_HANDLE ;
227+ if (vk_->CreateDevice (physical_device, &create_info, nullptr , &device) !=
228+ VK_SUCCESS ) {
229+ continue ;
230+ }
231+ if (!vk_->SetupDeviceProcAddresses (
232+ vulkan::VulkanHandle<VkDevice>(device))) {
233+ vk_->DestroyDevice (device, nullptr );
234+ continue ;
235+ }
236+ VkQueue queue = VK_NULL_HANDLE ;
237+ vk_->GetDeviceQueue (device, queue_family, 0 , &queue);
238+
239+ auto result = std::make_unique<vulkan::VulkanDevice>(
240+ *vk_, vulkan::VulkanHandle<VkPhysicalDevice>(physical_device),
241+ vulkan::VulkanHandle<VkDevice>(device,
242+ [vk = vk_](VkDevice d) {
243+ vk->DeviceWaitIdle (d);
244+ vk->DestroyDevice (d, nullptr );
245+ }),
246+ queue_family, vulkan::VulkanHandle<VkQueue>(queue));
247+ if (!result->IsValid ()) {
248+ continue ;
249+ }
250+ enabled_device_extensions_ = extensions;
251+ if (extensions.empty () && !enabled_instance_extensions_.empty ()) {
252+ // Without the device extension Impeller cannot use this context, so
253+ // the instance list would only mislead it.
254+ enabled_instance_extensions_.clear ();
255+ }
256+ return result;
257+ }
258+ }
259+ return nullptr ;
260+ }
261+
110262TestVulkanContext::~TestVulkanContext () {
111263 if (context_) {
112264 context_->releaseResourcesAndAbandonContext ();
0 commit comments