blob: 001c8e550723ce34946708bd4e04659bd370a2c4 [file] [log] [blame]
Derek Sollenberger0e3cba32016-11-09 11:58:36 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VulkanManager.h"
18
Alec Mouri6db59a62019-08-02 17:05:26 -070019#include <EGL/egl.h>
20#include <EGL/eglext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040021#include <GrBackendSemaphore.h>
22#include <GrBackendSurface.h>
Adlai Hollerf8c434e2020-07-27 11:42:45 -040023#include <GrDirectContext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040024#include <GrTypes.h>
25#include <android/sync.h>
Alec Mouri219997a2023-05-23 17:25:19 +000026#include <gui/TraceUtils.h>
27#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Kevin Lubick913e9a42024-03-07 13:14:46 +000028#include <include/gpu/ganesh/vk/GrVkBackendSemaphore.h>
Brian Osman734d8102023-08-29 18:07:56 +000029#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
Kevin Lubickf6b21952023-10-13 13:12:17 +000030#include <include/gpu/ganesh/vk/GrVkDirectContext.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000031#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040032#include <vk/GrVkExtensions.h>
33#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050034
Nolan Scobieddc9c662024-01-30 17:38:42 -050035#include <sstream>
36
Greg Danielcd558522016-11-17 13:31:40 -050037#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050039#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050040#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050041
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042namespace android {
43namespace uirenderer {
44namespace renderthread {
45
Nolan Scobieddc9c662024-01-30 17:38:42 -050046// Not all of these are strictly required, but are all enabled if present.
47static std::array<std::string_view, 21> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040048 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
49 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
50 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
51 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
52 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
53 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
54 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
55 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
56 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
57 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
58 VK_KHR_SURFACE_EXTENSION_NAME,
59 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
60 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040061 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040062 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
63 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
64 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
65 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
66 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Mattias Simonssone545f962023-07-17 08:03:14 +000067 VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME,
Nolan Scobieddc9c662024-01-30 17:38:42 -050068 VK_EXT_DEVICE_FAULT_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040069};
70
71static bool shouldEnableExtension(const std::string_view& extension) {
72 for (const auto& it : sEnableExtensions) {
73 if (it == extension) {
74 return true;
75 }
76 }
77 return false;
78}
79
Bo Liu7b8c1eb2019-01-08 20:17:55 -080080static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
81 // All Vulkan structs that could be part of the features chain will start with the
82 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
83 // so we can get access to the pNext for the next struct.
84 struct CommonVulkanHeader {
85 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070086 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080087 };
88
89 void* pNext = features.pNext;
90 while (pNext) {
91 void* current = pNext;
92 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
93 free(current);
94 }
95}
96
Greg Daniel2ff202712018-06-14 11:50:10 -040097#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
98#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
99#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100
John Reck9fc3d272023-05-01 16:33:22 -0400101// cache a weakptr to the context to enable a second thread to share the same vulkan state
102static wp<VulkanManager> sWeakInstance = nullptr;
103static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400104
John Reck9fc3d272023-05-01 16:33:22 -0400105sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400106 std::lock_guard _lock{sLock};
107 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
108 if (!vulkanManager.get()) {
109 vulkanManager = new VulkanManager();
110 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500111 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500112
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400113 return vulkanManager;
114}
115
John Reck9fc3d272023-05-01 16:33:22 -0400116sp<VulkanManager> VulkanManager::peekInstance() {
117 std::lock_guard _lock{sLock};
118 return sWeakInstance.promote();
119}
120
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400121VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 if (mDevice != VK_NULL_HANDLE) {
123 mDeviceWaitIdle(mDevice);
124 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700125 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500126
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 if (mInstance != VK_NULL_HANDLE) {
128 mDestroyInstance(mInstance, nullptr);
129 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500130
Greg Daniel2ff202712018-06-14 11:50:10 -0400131 mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000132 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400133 mDevice = VK_NULL_HANDLE;
134 mPhysicalDevice = VK_NULL_HANDLE;
135 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800136 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800137 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800138 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800139 mDeviceExtensions.clear();
140 free_features_extensions_structs(mPhysicalDeviceFeatures2);
141 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400142}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143
Stan Iliev90276c82019-02-03 18:01:02 -0500144void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400145 VkResult err;
146
147 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700148 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
149 nullptr, // pNext
150 "android framework", // pApplicationName
151 0, // applicationVersion
152 "android framework", // pEngineName
153 0, // engineVerison
154 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400155 };
156
Greg Daniel2ff202712018-06-14 11:50:10 -0400157 {
158 GET_PROC(EnumerateInstanceExtensionProperties);
159
160 uint32_t extensionCount = 0;
161 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500162 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800163 mInstanceExtensionsOwner.resize(extensionCount);
164 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
165 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500166 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400167 bool hasKHRSurfaceExtension = false;
168 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800169 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400170 if (!shouldEnableExtension(extension.extensionName)) {
171 ALOGV("Not enabling instance extension %s", extension.extensionName);
172 continue;
173 }
174 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800175 mInstanceExtensions.push_back(extension.extensionName);
176 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400177 hasKHRSurfaceExtension = true;
178 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800179 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400180 hasKHRAndroidSurfaceExtension = true;
181 }
182 }
Stan Iliev90276c82019-02-03 18:01:02 -0500183 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400184 }
185
186 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700187 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
188 nullptr, // pNext
189 0, // flags
190 &app_info, // pApplicationInfo
191 0, // enabledLayerNameCount
192 nullptr, // ppEnabledLayerNames
193 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
194 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400195 };
196
197 GET_PROC(CreateInstance);
198 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500199 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400200
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700201 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400202 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700203 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400204 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400205 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500206 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700207 GET_INST_PROC(GetPhysicalDeviceProperties);
208 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400209
210 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500211 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
212 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400213 // Just returning the first physical device instead of getting the whole array. Since there
214 // should only be one device on android.
215 gpuCount = 1;
216 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
217 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500218 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400219
Greg Daniel96259622018-10-01 14:42:56 -0400220 VkPhysicalDeviceProperties physDeviceProperties;
221 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500222 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400223 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400224
Greg Daniel2ff202712018-06-14 11:50:10 -0400225 // query to get the initial queue props size
Alec Mouri219997a2023-05-23 17:25:19 +0000226 uint32_t queueCount = 0;
Greg Daniel2ff202712018-06-14 11:50:10 -0400227 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500228 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400229
230 // now get the actual queue props
231 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
232 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
233
Alec Mouri219997a2023-05-23 17:25:19 +0000234 constexpr auto kRequestedQueueCount = 2;
235
Greg Daniel2ff202712018-06-14 11:50:10 -0400236 // iterate to find the graphics queue
237 mGraphicsQueueIndex = queueCount;
238 for (uint32_t i = 0; i < queueCount; i++) {
239 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
240 mGraphicsQueueIndex = i;
Alec Mouri219997a2023-05-23 17:25:19 +0000241 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400242 break;
243 }
244 }
Stan Iliev90276c82019-02-03 18:01:02 -0500245 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400246
Greg Daniel2ff202712018-06-14 11:50:10 -0400247 {
248 uint32_t extensionCount = 0;
249 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700250 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500251 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800252 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400253 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700254 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500255 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400256 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800257 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400258 if (!shouldEnableExtension(extension.extensionName)) {
259 ALOGV("Not enabling device extension %s", extension.extensionName);
260 continue;
261 }
262 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800263 mDeviceExtensions.push_back(extension.extensionName);
264 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400265 hasKHRSwapchainExtension = true;
266 }
267 }
Stan Iliev90276c82019-02-03 18:01:02 -0500268 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400269 }
270
Alec Mouri219997a2023-05-23 17:25:19 +0000271 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
272 if (device != VK_NULL_HANDLE) {
273 return vkGetDeviceProcAddr(device, proc_name);
274 }
275 return vkGetInstanceProcAddr(instance, proc_name);
276 };
277
278 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700279 mInstanceExtensions.data(), mDeviceExtensions.size(),
280 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400281
Stan Iliev90276c82019-02-03 18:01:02 -0500282 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400283
Greg Daniela227dbb2018-08-20 09:19:48 -0400284 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
285 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
286 features.pNext = nullptr;
287
288 // Setup all extension feature structs we may want to use.
289 void** tailPNext = &features.pNext;
290
291 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
292 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700293 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400294 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
295 LOG_ALWAYS_FATAL_IF(!blend);
296 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
297 blend->pNext = nullptr;
298 *tailPNext = blend;
299 tailPNext = &blend->pNext;
300 }
301
Greg Daniel05036172018-11-28 17:08:04 -0500302 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700303 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500304 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
305 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
306 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
307 ycbcrFeature->pNext = nullptr;
308 *tailPNext = ycbcrFeature;
309 tailPNext = &ycbcrFeature->pNext;
310
Nolan Scobieddc9c662024-01-30 17:38:42 -0500311 if (grExtensions.hasExtension(VK_EXT_DEVICE_FAULT_EXTENSION_NAME, 1)) {
312 VkPhysicalDeviceFaultFeaturesEXT* deviceFaultFeatures =
313 new VkPhysicalDeviceFaultFeaturesEXT;
314 deviceFaultFeatures->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FAULT_FEATURES_EXT;
315 deviceFaultFeatures->pNext = nullptr;
316 *tailPNext = deviceFaultFeatures;
317 tailPNext = &deviceFaultFeatures->pNext;
318 }
319
Greg Daniele77ab7a2024-09-04 15:29:52 +0000320 if (grExtensions.hasExtension(VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME, 1)) {
321 VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT* formatFeatures =
322 new VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT;
323 formatFeatures->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RGBA10X6_FORMATS_FEATURES_EXT;
324 formatFeatures->pNext = nullptr;
325 *tailPNext = formatFeatures;
326 tailPNext = &formatFeatures->pNext;
327 }
328
Greg Daniela227dbb2018-08-20 09:19:48 -0400329 // query to get the physical device features
330 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400331 // this looks like it would slow things down,
332 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400333 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400334
Alec Mouri219997a2023-05-23 17:25:19 +0000335 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400336
Stan Iliev7e733362019-02-28 13:16:36 -0500337 void* queueNextPtr = nullptr;
338
339 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
340
John Reck0fa0cbc2019-04-05 16:57:46 -0700341 if (Properties::contextPriority != 0 &&
342 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500343 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
344 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700345 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500346 queuePriorityCreateInfo.pNext = nullptr;
347 switch (Properties::contextPriority) {
348 case EGL_CONTEXT_PRIORITY_LOW_IMG:
349 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
350 break;
351 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
352 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
353 break;
354 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
355 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
356 break;
357 default:
358 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700359 }
360 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500361 }
362
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700363 const VkDeviceQueueCreateInfo queueInfo = {
364 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
365 queueNextPtr, // pNext
366 0, // VkDeviceQueueCreateFlags
367 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000368 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700369 queuePriorities, // pQueuePriorities
370 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400371
372 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700373 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
374 &features, // pNext
375 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700376 1, // queueCreateInfoCount
377 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700378 0, // layerCount
379 nullptr, // ppEnabledLayerNames
380 (uint32_t)mDeviceExtensions.size(), // extensionCount
381 mDeviceExtensions.data(), // ppEnabledExtensionNames
382 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400383 };
384
Stan Iliev90276c82019-02-03 18:01:02 -0500385 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400386
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500387 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500388 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500389 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700390 GET_DEV_PROC(CreateCommandPool);
391 GET_DEV_PROC(CreateFence);
392 GET_DEV_PROC(CreateSemaphore);
393 GET_DEV_PROC(DestroyCommandPool);
394 GET_DEV_PROC(DestroyDevice);
395 GET_DEV_PROC(DestroyFence);
396 GET_DEV_PROC(DestroySemaphore);
397 GET_DEV_PROC(DeviceWaitIdle);
398 GET_DEV_PROC(EndCommandBuffer);
399 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500400 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700401 GET_DEV_PROC(GetSemaphoreFdKHR);
402 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500403 GET_DEV_PROC(QueueSubmit);
404 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700405 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500406 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700407 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000408 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400409}
410
411void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000412 std::call_once(mInitFlag, [&] {
413 GET_PROC(EnumerateInstanceVersion);
414 uint32_t instanceVersion;
415 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
416 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400417
Alec Mouri671a9f62023-08-25 17:18:02 +0000418 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400419
Alec Mouri671a9f62023-08-25 17:18:02 +0000420 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
421 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400422
Alec Mouri671a9f62023-08-25 17:18:02 +0000423 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
424 mSwapBehavior = SwapBehavior::BufferAge;
425 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400426
Alec Mouri671a9f62023-08-25 17:18:02 +0000427 mInitialized = true;
428 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500429}
430
Nolan Scobieddc9c662024-01-30 17:38:42 -0500431namespace {
432void onVkDeviceFault(const std::string& contextLabel, const std::string& description,
433 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
434 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
435 const std::vector<std::byte>& vendorBinaryData) {
436 // The final crash string should contain as much differentiating info as possible, up to 1024
437 // bytes. As this final message is constructed, the same information is also dumped to the logs
438 // but in a more verbose format. Building the crash string is unsightly, so the clearer logging
439 // statement is always placed first to give context.
440 ALOGE("VK_ERROR_DEVICE_LOST (%s context): %s", contextLabel.c_str(), description.c_str());
441 std::stringstream crashMsg;
442 crashMsg << "VK_ERROR_DEVICE_LOST (" << contextLabel;
443
444 if (!addressInfos.empty()) {
445 ALOGE("%zu VkDeviceFaultAddressInfoEXT:", addressInfos.size());
446 crashMsg << ", " << addressInfos.size() << " address info (";
447 for (VkDeviceFaultAddressInfoEXT addressInfo : addressInfos) {
448 ALOGE(" addressType: %d", (int)addressInfo.addressType);
449 ALOGE(" reportedAddress: %" PRIu64, addressInfo.reportedAddress);
450 ALOGE(" addressPrecision: %" PRIu64, addressInfo.addressPrecision);
451 crashMsg << addressInfo.addressType << ":"
452 << addressInfo.reportedAddress << ":"
453 << addressInfo.addressPrecision << ", ";
454 }
455 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
456 crashMsg << ")";
457 }
458
459 if (!vendorInfos.empty()) {
460 ALOGE("%zu VkDeviceFaultVendorInfoEXT:", vendorInfos.size());
461 crashMsg << ", " << vendorInfos.size() << " vendor info (";
462 for (VkDeviceFaultVendorInfoEXT vendorInfo : vendorInfos) {
463 ALOGE(" description: %s", vendorInfo.description);
464 ALOGE(" vendorFaultCode: %" PRIu64, vendorInfo.vendorFaultCode);
465 ALOGE(" vendorFaultData: %" PRIu64, vendorInfo.vendorFaultData);
466 // Omit descriptions for individual vendor info structs in the crash string, as the
467 // fault code and fault data fields should be enough for clustering, and the verbosity
468 // isn't worth it. Additionally, vendors may just set the general description field of
469 // the overall fault to the description of the first element in this list, and that
470 // overall description will be placed at the end of the crash string.
471 crashMsg << vendorInfo.vendorFaultCode << ":"
472 << vendorInfo.vendorFaultData << ", ";
473 }
474 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
475 crashMsg << ")";
476 }
477
478 if (!vendorBinaryData.empty()) {
479 // TODO: b/322830575 - Log in base64, or dump directly to a file that gets put in bugreports
480 ALOGE("%zu bytes of vendor-specific binary data (please notify Android's Core Graphics"
481 " Stack team if you observe this message).",
482 vendorBinaryData.size());
483 crashMsg << ", " << vendorBinaryData.size() << " bytes binary";
484 }
485
486 crashMsg << "): " << description;
487 LOG_ALWAYS_FATAL("%s", crashMsg.str().c_str());
488}
489
490void deviceLostProcRenderThread(void* callbackContext, const std::string& description,
491 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
492 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
493 const std::vector<std::byte>& vendorBinaryData) {
494 onVkDeviceFault("RenderThread", description, addressInfos, vendorInfos, vendorBinaryData);
495}
496void deviceLostProcUploadThread(void* callbackContext, const std::string& description,
497 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
498 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
499 const std::vector<std::byte>& vendorBinaryData) {
500 onVkDeviceFault("UploadThread", description, addressInfos, vendorInfos, vendorBinaryData);
501}
502} // anonymous namespace
503
John Reck9fc3d272023-05-01 16:33:22 -0400504static void onGrContextReleased(void* context) {
505 VulkanManager* manager = (VulkanManager*)context;
506 manager->decStrong((void*)onGrContextReleased);
507}
Stan Iliev981afe72019-02-13 14:24:33 -0500508
John Reck9fc3d272023-05-01 16:33:22 -0400509sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
510 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000511 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
512 if (device != VK_NULL_HANDLE) {
513 return vkGetDeviceProcAddr(device, proc_name);
514 }
515 return vkGetInstanceProcAddr(instance, proc_name);
516 };
517
Stan Iliev981afe72019-02-13 14:24:33 -0500518 GrVkBackendContext backendContext;
519 backendContext.fInstance = mInstance;
520 backendContext.fPhysicalDevice = mPhysicalDevice;
521 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000522 backendContext.fQueue =
523 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500524 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
525 backendContext.fMaxAPIVersion = mAPIVersion;
526 backendContext.fVkExtensions = &mExtensions;
527 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000528 backendContext.fGetProc = std::move(getProc);
Nolan Scobieddc9c662024-01-30 17:38:42 -0500529 backendContext.fDeviceLostContext = nullptr;
530 backendContext.fDeviceLostProc = (contextType == ContextType::kRenderThread)
531 ? deviceLostProcRenderThread
532 : deviceLostProcUploadThread;
Stan Iliev981afe72019-02-13 14:24:33 -0500533
John Reck9fc3d272023-05-01 16:33:22 -0400534 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
535 this->incStrong((void*)onGrContextReleased);
536 options.fContextDeleteContext = this;
537 options.fContextDeleteProc = onGrContextReleased;
538
Kevin Lubickf6b21952023-10-13 13:12:17 +0000539 return GrDirectContexts::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500540}
541
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800542VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
543 return VkFunctorInitParams{
544 .instance = mInstance,
545 .physical_device = mPhysicalDevice,
546 .device = mDevice,
547 .queue = mGraphicsQueue,
548 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500549 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800550 .enabled_instance_extension_names = mInstanceExtensions.data(),
551 .enabled_instance_extension_names_length =
552 static_cast<uint32_t>(mInstanceExtensions.size()),
553 .enabled_device_extension_names = mDeviceExtensions.data(),
554 .enabled_device_extension_names_length =
555 static_cast<uint32_t>(mDeviceExtensions.size()),
556 .device_features_2 = &mPhysicalDeviceFeatures2,
557 };
558}
559
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500560Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500561 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
562
563 if (bufferInfo == nullptr) {
564 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
565 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500566 }
567
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500568 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500569
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500570 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400571 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
572 bool isSignalPending = false;
573 if (finfo != NULL) {
574 isSignalPending = finfo->status != 1;
575 sync_file_info_free(finfo);
576 }
577 if (isSignalPending) {
578 int fence_clone = dup(bufferInfo->dequeue_fence);
579 if (fence_clone == -1) {
580 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
581 errno);
582 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
583 } else {
584 VkSemaphoreCreateInfo semaphoreInfo;
585 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
586 semaphoreInfo.pNext = nullptr;
587 semaphoreInfo.flags = 0;
588 VkSemaphore semaphore;
589 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400590 if (err != VK_SUCCESS) {
591 ALOGE("Failed to create import semaphore, err: %d", err);
592 close(fence_clone);
593 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
594 } else {
595 VkImportSemaphoreFdInfoKHR importInfo;
596 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
597 importInfo.pNext = nullptr;
598 importInfo.semaphore = semaphore;
599 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
600 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
601 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500602
Greg Danield6670772021-06-09 12:01:12 -0400603 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
604 if (err != VK_SUCCESS) {
605 ALOGE("Failed to import semaphore, err: %d", err);
606 mDestroySemaphore(mDevice, semaphore, nullptr);
607 close(fence_clone);
608 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
609 } else {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000610 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400611 // Skia will take ownership of the VkSemaphore and delete it once the wait
612 // has finished. The VkSemaphore also owns the imported fd, so it will
613 // close the fd when it is deleted.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000614 bufferInfo->skSurface->wait(1, &beSemaphore);
Greg Danield6670772021-06-09 12:01:12 -0400615 // The following flush blocks the GPU immediately instead of waiting for
616 // other drawing ops. It seems dequeue_fence is not respected otherwise.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000617 // TODO: remove the flush after finding why beSemaphore is not working.
Kevin Lubickcae0b212023-09-12 18:33:10 +0000618 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface.get());
Greg Danield6670772021-06-09 12:01:12 -0400619 }
620 }
Stan Iliev197843d2019-03-21 11:34:15 -0400621 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500622 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500623 }
624
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500625 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
626 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500627}
628
John Reck4db23fd2023-11-10 15:31:49 -0500629class SharedSemaphoreInfo : public LightRefBase<SharedSemaphoreInfo> {
Greg Danield92a9b12019-04-23 10:11:04 -0400630 PFN_vkDestroySemaphore mDestroyFunction;
631 VkDevice mDevice;
632 VkSemaphore mSemaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500633 GrBackendSemaphore mGrBackendSemaphore;
Greg Danield92a9b12019-04-23 10:11:04 -0400634
John Reck4db23fd2023-11-10 15:31:49 -0500635 SharedSemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
636 VkSemaphore semaphore)
Priyanka Advanidfca12a2024-03-06 22:25:04 +0000637 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000638 mGrBackendSemaphore = GrBackendSemaphores::MakeVk(mSemaphore);
John Reck4db23fd2023-11-10 15:31:49 -0500639 }
John Reck5d3fac12023-11-08 23:08:10 -0500640
John Reck4db23fd2023-11-10 15:31:49 -0500641 ~SharedSemaphoreInfo() { mDestroyFunction(mDevice, mSemaphore, nullptr); }
642
643 friend class LightRefBase<SharedSemaphoreInfo>;
644 friend class sp<SharedSemaphoreInfo>;
645
646public:
647 VkSemaphore semaphore() const { return mSemaphore; }
648
649 GrBackendSemaphore* grBackendSemaphore() { return &mGrBackendSemaphore; }
Greg Danield92a9b12019-04-23 10:11:04 -0400650};
651
652static void destroy_semaphore(void* context) {
John Reck4db23fd2023-11-10 15:31:49 -0500653 SharedSemaphoreInfo* info = reinterpret_cast<SharedSemaphoreInfo*>(context);
654 info->decStrong(0);
Greg Danield92a9b12019-04-23 10:11:04 -0400655}
656
John Reck5d3fac12023-11-08 23:08:10 -0500657VulkanManager::VkDrawResult VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500658 ATRACE_NAME("Vulkan finish frame");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400659
John Reck4db23fd2023-11-10 15:31:49 -0500660 sp<SharedSemaphoreInfo> sharedSemaphore;
Greg Danielc7ad4082020-05-14 15:38:26 -0400661 GrFlushInfo flushInfo;
John Reck4db23fd2023-11-10 15:31:49 -0500662
663 {
664 VkExportSemaphoreCreateInfo exportInfo;
665 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
666 exportInfo.pNext = nullptr;
667 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
668
669 VkSemaphoreCreateInfo semaphoreInfo;
670 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
671 semaphoreInfo.pNext = &exportInfo;
672 semaphoreInfo.flags = 0;
673 VkSemaphore semaphore;
674 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
675 ALOGE_IF(VK_SUCCESS != err,
676 "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
677
678 if (err == VK_SUCCESS) {
679 sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
680 flushInfo.fNumSemaphores = 1;
681 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
682 flushInfo.fFinishedProc = destroy_semaphore;
683 sharedSemaphore->incStrong(0);
684 flushInfo.fFinishedContext = sharedSemaphore.get();
685 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500686 }
John Reck4db23fd2023-11-10 15:31:49 -0500687
Greg Danielbe2803a2021-02-19 18:32:16 -0500688 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400689 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000690 GrSemaphoresSubmitted submitted = context->flush(
691 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400692 context->submit();
John Reck5d3fac12023-11-08 23:08:10 -0500693 VkDrawResult drawResult{
694 .submissionTime = systemTime(),
695 };
John Reck4db23fd2023-11-10 15:31:49 -0500696 if (sharedSemaphore) {
697 if (submitted == GrSemaphoresSubmitted::kYes && mFrameBoundaryANDROID) {
698 // retrieve VkImage used as render target
699 VkImage image = VK_NULL_HANDLE;
700 GrBackendRenderTarget backendRenderTarget = SkSurfaces::GetBackendRenderTarget(
701 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
702 if (backendRenderTarget.isValid()) {
703 GrVkImageInfo info;
704 if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) {
705 image = info.fImage;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000706 } else {
John Reck4db23fd2023-11-10 15:31:49 -0500707 ALOGE("Frame boundary: backend is not vulkan");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000708 }
John Reck4db23fd2023-11-10 15:31:49 -0500709 } else {
710 ALOGE("Frame boundary: invalid backend render target");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000711 }
John Reck4db23fd2023-11-10 15:31:49 -0500712 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
713 // it won't wait on it.
714 mFrameBoundaryANDROID(mDevice, sharedSemaphore->semaphore(), image);
Greg Danielbe2803a2021-02-19 18:32:16 -0500715 }
John Reck5d3fac12023-11-08 23:08:10 -0500716 VkSemaphoreGetFdInfoKHR getFdInfo;
717 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
718 getFdInfo.pNext = nullptr;
John Reck4db23fd2023-11-10 15:31:49 -0500719 getFdInfo.semaphore = sharedSemaphore->semaphore();
John Reck5d3fac12023-11-08 23:08:10 -0500720 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
721
722 int fenceFd = -1;
John Reck4db23fd2023-11-10 15:31:49 -0500723 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
John Reck5d3fac12023-11-08 23:08:10 -0500724 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
725 drawResult.presentFence.reset(fenceFd);
726 } else {
727 ALOGE("VulkanManager::finishFrame(): Semaphore submission failed");
728 mQueueWaitIdle(mGraphicsQueue);
Greg Danielbe2803a2021-02-19 18:32:16 -0500729 }
John Reck5d3fac12023-11-08 23:08:10 -0500730
Greg Danielbe2803a2021-02-19 18:32:16 -0500731 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000732
John Reck5d3fac12023-11-08 23:08:10 -0500733 return drawResult;
Greg Danielbe2803a2021-02-19 18:32:16 -0500734}
735
John Reck5d3fac12023-11-08 23:08:10 -0500736void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect,
737 android::base::unique_fd&& presentFence) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500738 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
739 ATRACE_NAME("Finishing GPU work");
740 mDeviceWaitIdle(mDevice);
741 }
742
John Reck5d3fac12023-11-08 23:08:10 -0500743 surface->presentCurrentBuffer(dirtyRect, presentFence.release());
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500744}
745
746void VulkanManager::destroySurface(VulkanSurface* surface) {
747 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700748 if (VK_NULL_HANDLE != mGraphicsQueue) {
749 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500750 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500751
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500752 delete surface;
753}
754
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400755VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
756 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800757 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400758 SkColorType surfaceColorType,
759 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700760 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500761 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500762 if (!window) {
763 return nullptr;
764 }
765
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500766 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700767 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500768}
769
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400770status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400771 if (!hasVkContext()) {
772 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
773 return INVALID_OPERATION;
774 }
775
Stan Iliev7a081272018-10-26 17:54:18 -0400776 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400777 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400778 if (fenceFd == -1) {
779 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
780 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000781 }
Stan Iliev7a081272018-10-26 17:54:18 -0400782
783 VkSemaphoreCreateInfo semaphoreInfo;
784 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
785 semaphoreInfo.pNext = nullptr;
786 semaphoreInfo.flags = 0;
787 VkSemaphore semaphore;
788 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
789 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400790 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400791 ALOGE("Failed to create import semaphore, err: %d", err);
792 return UNKNOWN_ERROR;
793 }
794 VkImportSemaphoreFdInfoKHR importInfo;
795 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
796 importInfo.pNext = nullptr;
797 importInfo.semaphore = semaphore;
798 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
799 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
800 importInfo.fd = fenceFd;
801
802 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
803 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400804 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400805 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400806 ALOGE("Failed to import semaphore, err: %d", err);
807 return UNKNOWN_ERROR;
808 }
809
Kevin Lubick913e9a42024-03-07 13:14:46 +0000810 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400811
Greg Danield6670772021-06-09 12:01:12 -0400812 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
813 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400814 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400815 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400816
Stan Iliev564ca3e2018-09-04 22:00:00 +0000817 return OK;
818}
819
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400820status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400821 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400822 if (!hasVkContext()) {
823 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
824 return INVALID_OPERATION;
825 }
826
Greg Daniel26e0dca2018-09-18 10:33:19 -0400827 VkExportSemaphoreCreateInfo exportInfo;
828 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
829 exportInfo.pNext = nullptr;
830 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
831
832 VkSemaphoreCreateInfo semaphoreInfo;
833 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
834 semaphoreInfo.pNext = &exportInfo;
835 semaphoreInfo.flags = 0;
836 VkSemaphore semaphore;
837 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
838 if (VK_SUCCESS != err) {
839 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
840 return INVALID_OPERATION;
841 }
842
John Reck4db23fd2023-11-10 15:31:49 -0500843 auto sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400844
Greg Danielfd429392019-05-09 15:44:56 -0400845 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
Greg Danielc7ad4082020-05-14 15:38:26 -0400846 GrFlushInfo flushInfo;
847 flushInfo.fNumSemaphores = 1;
John Reck4db23fd2023-11-10 15:31:49 -0500848 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
Greg Danielc7ad4082020-05-14 15:38:26 -0400849 flushInfo.fFinishedProc = destroy_semaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500850 sharedSemaphore->incStrong(0);
851 flushInfo.fFinishedContext = sharedSemaphore.get();
Greg Danielc7ad4082020-05-14 15:38:26 -0400852 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
853 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400854
Greg Danield92a9b12019-04-23 10:11:04 -0400855 if (submitted == GrSemaphoresSubmitted::kNo) {
856 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danield92a9b12019-04-23 10:11:04 -0400857 return INVALID_OPERATION;
858 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400859
860 VkSemaphoreGetFdInfoKHR getFdInfo;
861 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
862 getFdInfo.pNext = nullptr;
863 getFdInfo.semaphore = semaphore;
864 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
865
866 int fenceFd = 0;
867
868 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
869 if (VK_SUCCESS != err) {
870 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
871 return INVALID_OPERATION;
872 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400873 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400874
Stan Iliev564ca3e2018-09-04 22:00:00 +0000875 return OK;
876}
877
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500878} /* namespace renderthread */
879} /* namespace uirenderer */
880} /* namespace android */