Skip to content

Commit 298bce9

Browse files
committed
Give the Vulkan test context the extensions Impeller requires
Impeller's capability check reads the extension lists the embedder declares rather than querying the driver: it wants VK_KHR_surface plus one WSI extension on the instance, and VK_KHR_swapchain on the device. The test context enables none of them and reports none, so Impeller refuses the context with "Could not find the surface extension" and no Impeller Vulkan embedder test can run at all. Request the surface extensions the ICD offers, create the logical device with VK_KHR_swapchain, and report both lists through FlutterVulkanRendererConfig. Falls back to a device without the extension when the ICD refuses it, and reports nothing when the surface extensions are missing, so a context that cannot serve Impeller still serves the Skia tests exactly as before. None of this is used to present -- the tests render into images -- but Impeller will not build a context without it. Also set FlutterVulkanImage.struct_size in the backing store producer. The API says it must be sizeof(FlutterVulkanImage); the Skia path never checks, so leaving it zero went unnoticed.
1 parent 12384f9 commit 298bce9

4 files changed

Lines changed: 183 additions & 2 deletions

File tree

‎engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_vulkan.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bool EmbedderTestBackingStoreProducerVulkan::Create(
8484
backing_store_out->type = kFlutterBackingStoreTypeVulkan;
8585

8686
FlutterVulkanImage* image = new FlutterVulkanImage();
87+
image->struct_size = sizeof(FlutterVulkanImage);
8788
image->image = reinterpret_cast<uint64_t>(image_info.fImage);
8889
image->format = VK_FORMAT_R8G8B8A8_UNORM;
8990
backing_store_out->vulkan.image = image;

‎engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_vulkan.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ EmbedderTestContextVulkan::EmbedderTestContextVulkan(std::string assets_path)
2929
.device = vulkan_context_->device_->GetHandle(),
3030
.queue_family_index = vulkan_context_->device_->GetGraphicsQueueIndex(),
3131
.queue = vulkan_context_->device_->GetQueueHandle(),
32+
.enabled_instance_extension_count =
33+
vulkan_context_->GetEnabledInstanceExtensions().size(),
34+
.enabled_instance_extensions = const_cast<const char**>(
35+
vulkan_context_->GetEnabledInstanceExtensions().data()),
36+
.enabled_device_extension_count =
37+
vulkan_context_->GetEnabledDeviceExtensions().size(),
38+
.enabled_device_extensions = const_cast<const char**>(
39+
vulkan_context_->GetEnabledDeviceExtensions().data()),
3240
.get_instance_proc_address_callback =
3341
EmbedderTestContextVulkan::InstanceProcAddr,
3442
.get_next_image_callback =

‎engine/src/flutter/testing/test_vulkan_context.cc‎

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
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"
@@ -23,6 +25,50 @@
2325

2426
namespace 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+
2672
TestVulkanContext::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+
110262
TestVulkanContext::~TestVulkanContext() {
111263
if (context_) {
112264
context_->releaseResourcesAndAbandonContext();

‎engine/src/flutter/testing/test_vulkan_context.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#ifndef FLUTTER_TESTING_TEST_VULKAN_CONTEXT_H_
66
#define FLUTTER_TESTING_TEST_VULKAN_CONTEXT_H_
77

8+
#include <string>
9+
#include <vector>
10+
811
#include "flutter/display_list/geometry/dl_geometry_types.h"
912
#include "flutter/fml/macros.h"
1013
#include "flutter/fml/memory/ref_ptr.h"
@@ -26,11 +29,28 @@ class TestVulkanContext : public fml::RefCountedThreadSafe<TestVulkanContext> {
2629

2730
sk_sp<GrDirectContext> GetGrDirectContext() const;
2831

32+
/// The instance and device extensions this context enabled, in the form
33+
/// `FlutterVulkanRendererConfig` takes them. Impeller's embedder path checks
34+
/// its requirements against these lists rather than querying the driver.
35+
const std::vector<const char*>& GetEnabledInstanceExtensions() const {
36+
return enabled_instance_extension_names_;
37+
}
38+
const std::vector<const char*>& GetEnabledDeviceExtensions() const {
39+
return enabled_device_extension_names_;
40+
}
41+
2942
private:
3043
fml::RefPtr<vulkan::VulkanProcTable> vk_;
3144
std::unique_ptr<vulkan::VulkanApplication> application_;
3245
std::unique_ptr<vulkan::VulkanDevice> device_;
3346

47+
std::vector<std::string> enabled_instance_extensions_;
48+
std::vector<std::string> enabled_device_extensions_;
49+
std::vector<const char*> enabled_instance_extension_names_;
50+
std::vector<const char*> enabled_device_extension_names_;
51+
52+
std::unique_ptr<vulkan::VulkanDevice> CreateLogicalDevice();
53+
3454
sk_sp<GrDirectContext> context_;
3555

3656
friend class EmbedderTestContextVulkan;

0 commit comments

Comments
 (0)