blob: 0d0af1110ca4be523a6a25b9a40e25af49a20d6f [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 Daniela227dbb2018-08-20 09:19:48 -0400320 // query to get the physical device features
321 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400322 // this looks like it would slow things down,
323 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400324 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400325
Alec Mouri219997a2023-05-23 17:25:19 +0000326 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400327
Stan Iliev7e733362019-02-28 13:16:36 -0500328 void* queueNextPtr = nullptr;
329
330 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
331
John Reck0fa0cbc2019-04-05 16:57:46 -0700332 if (Properties::contextPriority != 0 &&
333 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500334 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
335 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700336 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500337 queuePriorityCreateInfo.pNext = nullptr;
338 switch (Properties::contextPriority) {
339 case EGL_CONTEXT_PRIORITY_LOW_IMG:
340 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
341 break;
342 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
343 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
344 break;
345 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
346 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
347 break;
348 default:
349 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700350 }
351 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500352 }
353
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700354 const VkDeviceQueueCreateInfo queueInfo = {
355 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
356 queueNextPtr, // pNext
357 0, // VkDeviceQueueCreateFlags
358 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000359 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700360 queuePriorities, // pQueuePriorities
361 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400362
363 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700364 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
365 &features, // pNext
366 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700367 1, // queueCreateInfoCount
368 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700369 0, // layerCount
370 nullptr, // ppEnabledLayerNames
371 (uint32_t)mDeviceExtensions.size(), // extensionCount
372 mDeviceExtensions.data(), // ppEnabledExtensionNames
373 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400374 };
375
Stan Iliev90276c82019-02-03 18:01:02 -0500376 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400377
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500378 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500379 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500380 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700381 GET_DEV_PROC(CreateCommandPool);
382 GET_DEV_PROC(CreateFence);
383 GET_DEV_PROC(CreateSemaphore);
384 GET_DEV_PROC(DestroyCommandPool);
385 GET_DEV_PROC(DestroyDevice);
386 GET_DEV_PROC(DestroyFence);
387 GET_DEV_PROC(DestroySemaphore);
388 GET_DEV_PROC(DeviceWaitIdle);
389 GET_DEV_PROC(EndCommandBuffer);
390 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500391 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700392 GET_DEV_PROC(GetSemaphoreFdKHR);
393 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500394 GET_DEV_PROC(QueueSubmit);
395 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700396 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500397 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700398 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000399 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400400}
401
402void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000403 std::call_once(mInitFlag, [&] {
404 GET_PROC(EnumerateInstanceVersion);
405 uint32_t instanceVersion;
406 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
407 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400408
Alec Mouri671a9f62023-08-25 17:18:02 +0000409 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400410
Alec Mouri671a9f62023-08-25 17:18:02 +0000411 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
412 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400413
Alec Mouri671a9f62023-08-25 17:18:02 +0000414 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
415 mSwapBehavior = SwapBehavior::BufferAge;
416 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400417
Alec Mouri671a9f62023-08-25 17:18:02 +0000418 mInitialized = true;
419 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500420}
421
Nolan Scobieddc9c662024-01-30 17:38:42 -0500422namespace {
423void onVkDeviceFault(const std::string& contextLabel, const std::string& description,
424 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
425 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
426 const std::vector<std::byte>& vendorBinaryData) {
427 // The final crash string should contain as much differentiating info as possible, up to 1024
428 // bytes. As this final message is constructed, the same information is also dumped to the logs
429 // but in a more verbose format. Building the crash string is unsightly, so the clearer logging
430 // statement is always placed first to give context.
431 ALOGE("VK_ERROR_DEVICE_LOST (%s context): %s", contextLabel.c_str(), description.c_str());
432 std::stringstream crashMsg;
433 crashMsg << "VK_ERROR_DEVICE_LOST (" << contextLabel;
434
435 if (!addressInfos.empty()) {
436 ALOGE("%zu VkDeviceFaultAddressInfoEXT:", addressInfos.size());
437 crashMsg << ", " << addressInfos.size() << " address info (";
438 for (VkDeviceFaultAddressInfoEXT addressInfo : addressInfos) {
439 ALOGE(" addressType: %d", (int)addressInfo.addressType);
440 ALOGE(" reportedAddress: %" PRIu64, addressInfo.reportedAddress);
441 ALOGE(" addressPrecision: %" PRIu64, addressInfo.addressPrecision);
442 crashMsg << addressInfo.addressType << ":"
443 << addressInfo.reportedAddress << ":"
444 << addressInfo.addressPrecision << ", ";
445 }
446 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
447 crashMsg << ")";
448 }
449
450 if (!vendorInfos.empty()) {
451 ALOGE("%zu VkDeviceFaultVendorInfoEXT:", vendorInfos.size());
452 crashMsg << ", " << vendorInfos.size() << " vendor info (";
453 for (VkDeviceFaultVendorInfoEXT vendorInfo : vendorInfos) {
454 ALOGE(" description: %s", vendorInfo.description);
455 ALOGE(" vendorFaultCode: %" PRIu64, vendorInfo.vendorFaultCode);
456 ALOGE(" vendorFaultData: %" PRIu64, vendorInfo.vendorFaultData);
457 // Omit descriptions for individual vendor info structs in the crash string, as the
458 // fault code and fault data fields should be enough for clustering, and the verbosity
459 // isn't worth it. Additionally, vendors may just set the general description field of
460 // the overall fault to the description of the first element in this list, and that
461 // overall description will be placed at the end of the crash string.
462 crashMsg << vendorInfo.vendorFaultCode << ":"
463 << vendorInfo.vendorFaultData << ", ";
464 }
465 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
466 crashMsg << ")";
467 }
468
469 if (!vendorBinaryData.empty()) {
470 // TODO: b/322830575 - Log in base64, or dump directly to a file that gets put in bugreports
471 ALOGE("%zu bytes of vendor-specific binary data (please notify Android's Core Graphics"
472 " Stack team if you observe this message).",
473 vendorBinaryData.size());
474 crashMsg << ", " << vendorBinaryData.size() << " bytes binary";
475 }
476
477 crashMsg << "): " << description;
478 LOG_ALWAYS_FATAL("%s", crashMsg.str().c_str());
479}
480
481void deviceLostProcRenderThread(void* callbackContext, const std::string& description,
482 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
483 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
484 const std::vector<std::byte>& vendorBinaryData) {
485 onVkDeviceFault("RenderThread", description, addressInfos, vendorInfos, vendorBinaryData);
486}
487void deviceLostProcUploadThread(void* callbackContext, const std::string& description,
488 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
489 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
490 const std::vector<std::byte>& vendorBinaryData) {
491 onVkDeviceFault("UploadThread", description, addressInfos, vendorInfos, vendorBinaryData);
492}
493} // anonymous namespace
494
John Reck9fc3d272023-05-01 16:33:22 -0400495static void onGrContextReleased(void* context) {
496 VulkanManager* manager = (VulkanManager*)context;
497 manager->decStrong((void*)onGrContextReleased);
498}
Stan Iliev981afe72019-02-13 14:24:33 -0500499
John Reck9fc3d272023-05-01 16:33:22 -0400500sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
501 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000502 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
503 if (device != VK_NULL_HANDLE) {
504 return vkGetDeviceProcAddr(device, proc_name);
505 }
506 return vkGetInstanceProcAddr(instance, proc_name);
507 };
508
Stan Iliev981afe72019-02-13 14:24:33 -0500509 GrVkBackendContext backendContext;
510 backendContext.fInstance = mInstance;
511 backendContext.fPhysicalDevice = mPhysicalDevice;
512 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000513 backendContext.fQueue =
514 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500515 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
516 backendContext.fMaxAPIVersion = mAPIVersion;
517 backendContext.fVkExtensions = &mExtensions;
518 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000519 backendContext.fGetProc = std::move(getProc);
Nolan Scobieddc9c662024-01-30 17:38:42 -0500520 backendContext.fDeviceLostContext = nullptr;
521 backendContext.fDeviceLostProc = (contextType == ContextType::kRenderThread)
522 ? deviceLostProcRenderThread
523 : deviceLostProcUploadThread;
Stan Iliev981afe72019-02-13 14:24:33 -0500524
John Reck9fc3d272023-05-01 16:33:22 -0400525 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
526 this->incStrong((void*)onGrContextReleased);
527 options.fContextDeleteContext = this;
528 options.fContextDeleteProc = onGrContextReleased;
529
Kevin Lubickf6b21952023-10-13 13:12:17 +0000530 return GrDirectContexts::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500531}
532
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800533VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
534 return VkFunctorInitParams{
535 .instance = mInstance,
536 .physical_device = mPhysicalDevice,
537 .device = mDevice,
538 .queue = mGraphicsQueue,
539 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500540 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800541 .enabled_instance_extension_names = mInstanceExtensions.data(),
542 .enabled_instance_extension_names_length =
543 static_cast<uint32_t>(mInstanceExtensions.size()),
544 .enabled_device_extension_names = mDeviceExtensions.data(),
545 .enabled_device_extension_names_length =
546 static_cast<uint32_t>(mDeviceExtensions.size()),
547 .device_features_2 = &mPhysicalDeviceFeatures2,
548 };
549}
550
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500551Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500552 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
553
554 if (bufferInfo == nullptr) {
555 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
556 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500557 }
558
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500559 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500560
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500561 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400562 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
563 bool isSignalPending = false;
564 if (finfo != NULL) {
565 isSignalPending = finfo->status != 1;
566 sync_file_info_free(finfo);
567 }
568 if (isSignalPending) {
569 int fence_clone = dup(bufferInfo->dequeue_fence);
570 if (fence_clone == -1) {
571 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
572 errno);
573 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
574 } else {
575 VkSemaphoreCreateInfo semaphoreInfo;
576 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
577 semaphoreInfo.pNext = nullptr;
578 semaphoreInfo.flags = 0;
579 VkSemaphore semaphore;
580 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400581 if (err != VK_SUCCESS) {
582 ALOGE("Failed to create import semaphore, err: %d", err);
583 close(fence_clone);
584 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
585 } else {
586 VkImportSemaphoreFdInfoKHR importInfo;
587 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
588 importInfo.pNext = nullptr;
589 importInfo.semaphore = semaphore;
590 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
591 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
592 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500593
Greg Danield6670772021-06-09 12:01:12 -0400594 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
595 if (err != VK_SUCCESS) {
596 ALOGE("Failed to import semaphore, err: %d", err);
597 mDestroySemaphore(mDevice, semaphore, nullptr);
598 close(fence_clone);
599 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
600 } else {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000601 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400602 // Skia will take ownership of the VkSemaphore and delete it once the wait
603 // has finished. The VkSemaphore also owns the imported fd, so it will
604 // close the fd when it is deleted.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000605 bufferInfo->skSurface->wait(1, &beSemaphore);
Greg Danield6670772021-06-09 12:01:12 -0400606 // The following flush blocks the GPU immediately instead of waiting for
607 // other drawing ops. It seems dequeue_fence is not respected otherwise.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000608 // TODO: remove the flush after finding why beSemaphore is not working.
Kevin Lubickcae0b212023-09-12 18:33:10 +0000609 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface.get());
Greg Danield6670772021-06-09 12:01:12 -0400610 }
611 }
Stan Iliev197843d2019-03-21 11:34:15 -0400612 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500613 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500614 }
615
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500616 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
617 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500618}
619
John Reck4db23fd2023-11-10 15:31:49 -0500620class SharedSemaphoreInfo : public LightRefBase<SharedSemaphoreInfo> {
Greg Danield92a9b12019-04-23 10:11:04 -0400621 PFN_vkDestroySemaphore mDestroyFunction;
622 VkDevice mDevice;
623 VkSemaphore mSemaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500624 GrBackendSemaphore mGrBackendSemaphore;
Greg Danield92a9b12019-04-23 10:11:04 -0400625
John Reck4db23fd2023-11-10 15:31:49 -0500626 SharedSemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
627 VkSemaphore semaphore)
Priyanka Advanidfca12a2024-03-06 22:25:04 +0000628 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000629 mGrBackendSemaphore = GrBackendSemaphores::MakeVk(mSemaphore);
John Reck4db23fd2023-11-10 15:31:49 -0500630 }
John Reck5d3fac12023-11-08 23:08:10 -0500631
John Reck4db23fd2023-11-10 15:31:49 -0500632 ~SharedSemaphoreInfo() { mDestroyFunction(mDevice, mSemaphore, nullptr); }
633
634 friend class LightRefBase<SharedSemaphoreInfo>;
635 friend class sp<SharedSemaphoreInfo>;
636
637public:
638 VkSemaphore semaphore() const { return mSemaphore; }
639
640 GrBackendSemaphore* grBackendSemaphore() { return &mGrBackendSemaphore; }
Greg Danield92a9b12019-04-23 10:11:04 -0400641};
642
643static void destroy_semaphore(void* context) {
John Reck4db23fd2023-11-10 15:31:49 -0500644 SharedSemaphoreInfo* info = reinterpret_cast<SharedSemaphoreInfo*>(context);
645 info->decStrong(0);
Greg Danield92a9b12019-04-23 10:11:04 -0400646}
647
John Reck5d3fac12023-11-08 23:08:10 -0500648VulkanManager::VkDrawResult VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500649 ATRACE_NAME("Vulkan finish frame");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400650
John Reck4db23fd2023-11-10 15:31:49 -0500651 sp<SharedSemaphoreInfo> sharedSemaphore;
Greg Danielc7ad4082020-05-14 15:38:26 -0400652 GrFlushInfo flushInfo;
John Reck4db23fd2023-11-10 15:31:49 -0500653
654 {
655 VkExportSemaphoreCreateInfo exportInfo;
656 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
657 exportInfo.pNext = nullptr;
658 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
659
660 VkSemaphoreCreateInfo semaphoreInfo;
661 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
662 semaphoreInfo.pNext = &exportInfo;
663 semaphoreInfo.flags = 0;
664 VkSemaphore semaphore;
665 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
666 ALOGE_IF(VK_SUCCESS != err,
667 "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
668
669 if (err == VK_SUCCESS) {
670 sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
671 flushInfo.fNumSemaphores = 1;
672 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
673 flushInfo.fFinishedProc = destroy_semaphore;
674 sharedSemaphore->incStrong(0);
675 flushInfo.fFinishedContext = sharedSemaphore.get();
676 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500677 }
John Reck4db23fd2023-11-10 15:31:49 -0500678
Greg Danielbe2803a2021-02-19 18:32:16 -0500679 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400680 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000681 GrSemaphoresSubmitted submitted = context->flush(
682 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400683 context->submit();
John Reck5d3fac12023-11-08 23:08:10 -0500684 VkDrawResult drawResult{
685 .submissionTime = systemTime(),
686 };
John Reck4db23fd2023-11-10 15:31:49 -0500687 if (sharedSemaphore) {
688 if (submitted == GrSemaphoresSubmitted::kYes && mFrameBoundaryANDROID) {
689 // retrieve VkImage used as render target
690 VkImage image = VK_NULL_HANDLE;
691 GrBackendRenderTarget backendRenderTarget = SkSurfaces::GetBackendRenderTarget(
692 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
693 if (backendRenderTarget.isValid()) {
694 GrVkImageInfo info;
695 if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) {
696 image = info.fImage;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000697 } else {
John Reck4db23fd2023-11-10 15:31:49 -0500698 ALOGE("Frame boundary: backend is not vulkan");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000699 }
John Reck4db23fd2023-11-10 15:31:49 -0500700 } else {
701 ALOGE("Frame boundary: invalid backend render target");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000702 }
John Reck4db23fd2023-11-10 15:31:49 -0500703 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
704 // it won't wait on it.
705 mFrameBoundaryANDROID(mDevice, sharedSemaphore->semaphore(), image);
Greg Danielbe2803a2021-02-19 18:32:16 -0500706 }
John Reck5d3fac12023-11-08 23:08:10 -0500707 VkSemaphoreGetFdInfoKHR getFdInfo;
708 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
709 getFdInfo.pNext = nullptr;
John Reck4db23fd2023-11-10 15:31:49 -0500710 getFdInfo.semaphore = sharedSemaphore->semaphore();
John Reck5d3fac12023-11-08 23:08:10 -0500711 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
712
713 int fenceFd = -1;
John Reck4db23fd2023-11-10 15:31:49 -0500714 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
John Reck5d3fac12023-11-08 23:08:10 -0500715 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
716 drawResult.presentFence.reset(fenceFd);
717 } else {
718 ALOGE("VulkanManager::finishFrame(): Semaphore submission failed");
719 mQueueWaitIdle(mGraphicsQueue);
Greg Danielbe2803a2021-02-19 18:32:16 -0500720 }
John Reck5d3fac12023-11-08 23:08:10 -0500721
Greg Danielbe2803a2021-02-19 18:32:16 -0500722 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000723
John Reck5d3fac12023-11-08 23:08:10 -0500724 return drawResult;
Greg Danielbe2803a2021-02-19 18:32:16 -0500725}
726
John Reck5d3fac12023-11-08 23:08:10 -0500727void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect,
728 android::base::unique_fd&& presentFence) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500729 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
730 ATRACE_NAME("Finishing GPU work");
731 mDeviceWaitIdle(mDevice);
732 }
733
John Reck5d3fac12023-11-08 23:08:10 -0500734 surface->presentCurrentBuffer(dirtyRect, presentFence.release());
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500735}
736
737void VulkanManager::destroySurface(VulkanSurface* surface) {
738 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700739 if (VK_NULL_HANDLE != mGraphicsQueue) {
740 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500741 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500742
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500743 delete surface;
744}
745
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400746VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
747 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800748 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400749 SkColorType surfaceColorType,
750 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700751 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500752 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500753 if (!window) {
754 return nullptr;
755 }
756
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500757 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700758 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500759}
760
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400761status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400762 if (!hasVkContext()) {
763 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
764 return INVALID_OPERATION;
765 }
766
Stan Iliev7a081272018-10-26 17:54:18 -0400767 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400768 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400769 if (fenceFd == -1) {
770 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
771 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000772 }
Stan Iliev7a081272018-10-26 17:54:18 -0400773
774 VkSemaphoreCreateInfo semaphoreInfo;
775 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
776 semaphoreInfo.pNext = nullptr;
777 semaphoreInfo.flags = 0;
778 VkSemaphore semaphore;
779 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
780 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400781 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400782 ALOGE("Failed to create import semaphore, err: %d", err);
783 return UNKNOWN_ERROR;
784 }
785 VkImportSemaphoreFdInfoKHR importInfo;
786 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
787 importInfo.pNext = nullptr;
788 importInfo.semaphore = semaphore;
789 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
790 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
791 importInfo.fd = fenceFd;
792
793 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
794 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400795 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400796 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400797 ALOGE("Failed to import semaphore, err: %d", err);
798 return UNKNOWN_ERROR;
799 }
800
Kevin Lubick913e9a42024-03-07 13:14:46 +0000801 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400802
Greg Danield6670772021-06-09 12:01:12 -0400803 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
804 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400805 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400806 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400807
Stan Iliev564ca3e2018-09-04 22:00:00 +0000808 return OK;
809}
810
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400811status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400812 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400813 if (!hasVkContext()) {
814 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
815 return INVALID_OPERATION;
816 }
817
Greg Daniel26e0dca2018-09-18 10:33:19 -0400818 VkExportSemaphoreCreateInfo exportInfo;
819 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
820 exportInfo.pNext = nullptr;
821 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
822
823 VkSemaphoreCreateInfo semaphoreInfo;
824 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
825 semaphoreInfo.pNext = &exportInfo;
826 semaphoreInfo.flags = 0;
827 VkSemaphore semaphore;
828 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
829 if (VK_SUCCESS != err) {
830 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
831 return INVALID_OPERATION;
832 }
833
John Reck4db23fd2023-11-10 15:31:49 -0500834 auto sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400835
Greg Danielfd429392019-05-09 15:44:56 -0400836 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
Greg Danielc7ad4082020-05-14 15:38:26 -0400837 GrFlushInfo flushInfo;
838 flushInfo.fNumSemaphores = 1;
John Reck4db23fd2023-11-10 15:31:49 -0500839 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
Greg Danielc7ad4082020-05-14 15:38:26 -0400840 flushInfo.fFinishedProc = destroy_semaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500841 sharedSemaphore->incStrong(0);
842 flushInfo.fFinishedContext = sharedSemaphore.get();
Greg Danielc7ad4082020-05-14 15:38:26 -0400843 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
844 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400845
Greg Danield92a9b12019-04-23 10:11:04 -0400846 if (submitted == GrSemaphoresSubmitted::kNo) {
847 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danield92a9b12019-04-23 10:11:04 -0400848 return INVALID_OPERATION;
849 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400850
851 VkSemaphoreGetFdInfoKHR getFdInfo;
852 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
853 getFdInfo.pNext = nullptr;
854 getFdInfo.semaphore = semaphore;
855 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
856
857 int fenceFd = 0;
858
859 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
860 if (VK_SUCCESS != err) {
861 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
862 return INVALID_OPERATION;
863 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400864 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400865
Stan Iliev564ca3e2018-09-04 22:00:00 +0000866 return OK;
867}
868
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500869} /* namespace renderthread */
870} /* namespace uirenderer */
871} /* namespace android */