Flutter Engine
 
Loading...
Searching...
No Matches
test_vulkan_context.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 <cassert>
6#include <memory>
7#include <optional>
8
10#include "flutter/fml/logging.h"
14
18#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
19#include "third_party/skia/include/gpu/ganesh/vk/GrVkDirectContext.h"
20#include "third_party/skia/include/gpu/vk/VulkanBackendContext.h"
21#include "third_party/skia/include/gpu/vk/VulkanExtensions.h"
22#include "vulkan/vulkan_core.h"
23
24namespace flutter::testing {
25
27 // ---------------------------------------------------------------------------
28 // Initialize basic Vulkan state using the Swiftshader ICD.
29 // ---------------------------------------------------------------------------
30
31 const char* vulkan_icd = VULKAN_SO_PATH;
32
33 // TODO(96949): Clean this up and pass a native library directly to
34 // VulkanProcTable.
36 FML_LOG(ERROR) << "Couldn't find Vulkan ICD \"" << vulkan_icd
37 << "\", trying \"libvulkan.so\" instead.";
38 vulkan_icd = "libvulkan.so";
39 }
40
41 FML_LOG(INFO) << "Using Vulkan ICD: " << vulkan_icd;
42
43 vk_ = fml::MakeRefCounted<vulkan::VulkanProcTable>(vulkan_icd);
44 if (!vk_ || !vk_->HasAcquiredMandatoryProcAddresses()) {
45 FML_LOG(ERROR) << "Proc table has not acquired mandatory proc addresses.";
46 return;
47 }
48
49 application_ = std::make_unique<vulkan::VulkanApplication>(
50 *vk_, "Flutter Unittests", std::vector<std::string>{},
51 VK_MAKE_VERSION(1, 0, 0), VK_MAKE_VERSION(1, 1, 0), true);
52 if (!application_->IsValid()) {
53 FML_LOG(ERROR) << "Failed to initialize basic Vulkan state.";
54 return;
55 }
56 if (!vk_->AreInstanceProcsSetup()) {
57 FML_LOG(ERROR) << "Failed to acquire full proc table.";
58 return;
59 }
60
61 device_ = application_->AcquireFirstCompatibleLogicalDevice();
62 if (!device_ || !device_->IsValid()) {
63 FML_LOG(ERROR) << "Failed to create compatible logical device.";
64 return;
65 }
66
67 // ---------------------------------------------------------------------------
68 // Create a Skia context.
69 // For creating SkSurfaces from VkImages and snapshotting them, etc.
70 // ---------------------------------------------------------------------------
71
72 VkPhysicalDeviceFeatures features;
73 if (!device_->GetPhysicalDeviceFeatures(&features)) {
74 FML_LOG(ERROR) << "Failed to get physical device features.";
75
76 return;
77 }
78
79 auto get_proc = vulkan::CreateSkiaGetProc(vk_);
80 if (get_proc == nullptr) {
81 FML_LOG(ERROR) << "Failed to create Vulkan getProc for Skia.";
82 return;
83 }
84
85 sk_sp<skgpu::VulkanMemoryAllocator> allocator =
87 VK_MAKE_VERSION(1, 1, 0), application_->GetInstance(),
88 device_->GetPhysicalDeviceHandle(), device_->GetHandle(), vk_, true);
89
90 skgpu::VulkanExtensions extensions;
91
92 skgpu::VulkanBackendContext backend_context = {};
93 backend_context.fInstance = application_->GetInstance();
94 backend_context.fPhysicalDevice = device_->GetPhysicalDeviceHandle();
95 backend_context.fDevice = device_->GetHandle();
96 backend_context.fQueue = device_->GetQueueHandle();
97 backend_context.fGraphicsQueueIndex = device_->GetGraphicsQueueIndex();
98 backend_context.fMaxAPIVersion = VK_MAKE_VERSION(1, 1, 0);
99 backend_context.fDeviceFeatures = &features;
100 backend_context.fVkExtensions = &extensions;
101 backend_context.fGetProc = get_proc;
102 backend_context.fMemoryAllocator = allocator;
103
104 GrContextOptions options =
105 MakeDefaultContextOptions(ContextType::kRender, GrBackendApi::kVulkan);
106 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
107 context_ = GrDirectContexts::MakeVulkan(backend_context, options);
108}
109
111 if (context_) {
112 context_->releaseResourcesAndAbandonContext();
113 }
114}
115
116std::optional<TestVulkanImage> TestVulkanContext::CreateImage(
117 const DlISize& size) const {
118 TestVulkanImage result;
119
120 VkImageCreateInfo info = {
121 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
122 .pNext = nullptr,
123 .flags = 0,
124 .imageType = VK_IMAGE_TYPE_2D,
125 .format = VK_FORMAT_R8G8B8A8_UNORM,
126 .extent = VkExtent3D{static_cast<uint32_t>(size.width),
127 static_cast<uint32_t>(size.height), 1},
128 .mipLevels = 1,
129 .arrayLayers = 1,
130 .samples = VK_SAMPLE_COUNT_1_BIT,
131 .tiling = VK_IMAGE_TILING_OPTIMAL,
132 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
133 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
134 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
135 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
136 .queueFamilyIndexCount = 0,
137 .pQueueFamilyIndices = nullptr,
138 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
139 };
140
141 VkImage image;
143 vk_->CreateImage(device_->GetHandle(), &info, nullptr, &image)))) {
144 return std::nullopt;
145 }
146
147 result.image_ = vulkan::VulkanHandle<VkImage>(
148 image, [&vk = vk_, &device = device_](VkImage image) {
149 vk->DestroyImage(device->GetHandle(), image, nullptr);
150 });
151
152 VkMemoryRequirements mem_req;
153 vk_->GetImageMemoryRequirements(device_->GetHandle(), image, &mem_req);
154 VkMemoryAllocateInfo alloc_info{};
155 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
156 alloc_info.allocationSize = mem_req.size;
157 alloc_info.memoryTypeIndex = static_cast<uint32_t>(__builtin_ctz(
158 mem_req.memoryTypeBits & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT));
159
160 VkDeviceMemory memory;
161 if (VK_CALL_LOG_ERROR(vk_->AllocateMemory(device_->GetHandle(), &alloc_info,
162 nullptr, &memory)) != VK_SUCCESS) {
163 return std::nullopt;
164 }
165
167 memory, [&vk = vk_, &device = device_](VkDeviceMemory memory) {
168 vk->FreeMemory(device->GetHandle(), memory, nullptr);
169 }};
170
171 if (VK_CALL_LOG_ERROR(VK_CALL_LOG_ERROR(vk_->BindImageMemory(
172 device_->GetHandle(), result.image_, result.memory_, 0)))) {
173 return std::nullopt;
174 }
175
176 result.context_ =
178
179 return result;
180}
181
182sk_sp<GrDirectContext> TestVulkanContext::GetGrDirectContext() const {
183 return context_;
184}
185
186} // namespace flutter::testing
static sk_sp< VulkanMemoryAllocator > Make(uint32_t vulkan_api_version, VkInstance instance, VkPhysicalDevice physicalDevice, VkDevice device, const fml::RefPtr< vulkan::VulkanProcTable > &vk, bool mustUseCoherentHostVisibleMemory)
sk_sp< GrDirectContext > GetGrDirectContext() const
std::optional< TestVulkanImage > CreateImage(const DlISize &size) const
Captures the lifetime of a test VkImage along with its bound memory.
static fml::RefPtr< NativeLibrary > Create(const char *path)
bool HasAcquiredMandatoryProcAddresses() const
FlutterVulkanImage * image
VkDevice device
Definition main.cc:69
#define FML_LOG(severity)
Definition logging.h:101
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
GrContextOptions MakeDefaultContextOptions(ContextType type, std::optional< GrBackendApi > api)
Initializes GrContextOptions with values suitable for Flutter. The options can be further tweaked bef...
@ kRender
The context is used to render to a texture or renderbuffer.
skgpu::VulkanGetProc CreateSkiaGetProc(const fml::RefPtr< vulkan::VulkanProcTable > &vk)
#define VULKAN_SO_PATH
#define VK_CALL_LOG_ERROR(expression)