blob: ee3b2e48fe4bc719250dfbd4bfce26d0efe58bda [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>
Brian Osman734d8102023-08-29 18:07:56 +000028#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000029#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040030#include <vk/GrVkExtensions.h>
31#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050032
Greg Danielcd558522016-11-17 13:31:40 -050033#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050035#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050036#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038namespace android {
39namespace uirenderer {
40namespace renderthread {
41
Mattias Simonssone545f962023-07-17 08:03:14 +000042static std::array<std::string_view, 20> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040043 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
44 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
45 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
46 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
47 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
48 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
49 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
50 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
51 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
52 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
53 VK_KHR_SURFACE_EXTENSION_NAME,
54 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
55 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040056 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040057 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
58 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
59 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
60 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
61 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Mattias Simonssone545f962023-07-17 08:03:14 +000062 VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040063};
64
65static bool shouldEnableExtension(const std::string_view& extension) {
66 for (const auto& it : sEnableExtensions) {
67 if (it == extension) {
68 return true;
69 }
70 }
71 return false;
72}
73
Bo Liu7b8c1eb2019-01-08 20:17:55 -080074static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
75 // All Vulkan structs that could be part of the features chain will start with the
76 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
77 // so we can get access to the pNext for the next struct.
78 struct CommonVulkanHeader {
79 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070080 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080081 };
82
83 void* pNext = features.pNext;
84 while (pNext) {
85 void* current = pNext;
86 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
87 free(current);
88 }
89}
90
Greg Daniel2ff202712018-06-14 11:50:10 -040091#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
92#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
93#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050094
John Reck9fc3d272023-05-01 16:33:22 -040095// cache a weakptr to the context to enable a second thread to share the same vulkan state
96static wp<VulkanManager> sWeakInstance = nullptr;
97static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -040098
John Reck9fc3d272023-05-01 16:33:22 -040099sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400100 std::lock_guard _lock{sLock};
101 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
102 if (!vulkanManager.get()) {
103 vulkanManager = new VulkanManager();
104 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500105 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500106
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400107 return vulkanManager;
108}
109
John Reck9fc3d272023-05-01 16:33:22 -0400110sp<VulkanManager> VulkanManager::peekInstance() {
111 std::lock_guard _lock{sLock};
112 return sWeakInstance.promote();
113}
114
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400115VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400116 if (mDevice != VK_NULL_HANDLE) {
117 mDeviceWaitIdle(mDevice);
118 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700119 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500120
Greg Daniel2ff202712018-06-14 11:50:10 -0400121 if (mInstance != VK_NULL_HANDLE) {
122 mDestroyInstance(mInstance, nullptr);
123 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500124
Greg Daniel2ff202712018-06-14 11:50:10 -0400125 mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000126 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 mDevice = VK_NULL_HANDLE;
128 mPhysicalDevice = VK_NULL_HANDLE;
129 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800130 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800131 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800132 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800133 mDeviceExtensions.clear();
134 free_features_extensions_structs(mPhysicalDeviceFeatures2);
135 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400136}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137
Stan Iliev90276c82019-02-03 18:01:02 -0500138void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400139 VkResult err;
140
141 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700142 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
143 nullptr, // pNext
144 "android framework", // pApplicationName
145 0, // applicationVersion
146 "android framework", // pEngineName
147 0, // engineVerison
148 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400149 };
150
Greg Daniel2ff202712018-06-14 11:50:10 -0400151 {
152 GET_PROC(EnumerateInstanceExtensionProperties);
153
154 uint32_t extensionCount = 0;
155 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500156 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800157 mInstanceExtensionsOwner.resize(extensionCount);
158 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
159 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500160 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400161 bool hasKHRSurfaceExtension = false;
162 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800163 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400164 if (!shouldEnableExtension(extension.extensionName)) {
165 ALOGV("Not enabling instance extension %s", extension.extensionName);
166 continue;
167 }
168 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800169 mInstanceExtensions.push_back(extension.extensionName);
170 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400171 hasKHRSurfaceExtension = true;
172 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800173 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400174 hasKHRAndroidSurfaceExtension = true;
175 }
176 }
Stan Iliev90276c82019-02-03 18:01:02 -0500177 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400178 }
179
180 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700181 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
182 nullptr, // pNext
183 0, // flags
184 &app_info, // pApplicationInfo
185 0, // enabledLayerNameCount
186 nullptr, // ppEnabledLayerNames
187 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
188 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400189 };
190
191 GET_PROC(CreateInstance);
192 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500193 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400194
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700195 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400196 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700197 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400198 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400199 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500200 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700201 GET_INST_PROC(GetPhysicalDeviceProperties);
202 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400203
204 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500205 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
206 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400207 // Just returning the first physical device instead of getting the whole array. Since there
208 // should only be one device on android.
209 gpuCount = 1;
210 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
211 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500212 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400213
Greg Daniel96259622018-10-01 14:42:56 -0400214 VkPhysicalDeviceProperties physDeviceProperties;
215 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500216 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400217 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400218
Greg Daniel2ff202712018-06-14 11:50:10 -0400219 // query to get the initial queue props size
Alec Mouri219997a2023-05-23 17:25:19 +0000220 uint32_t queueCount = 0;
Greg Daniel2ff202712018-06-14 11:50:10 -0400221 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500222 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400223
224 // now get the actual queue props
225 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
226 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
227
Alec Mouri219997a2023-05-23 17:25:19 +0000228 constexpr auto kRequestedQueueCount = 2;
229
Greg Daniel2ff202712018-06-14 11:50:10 -0400230 // iterate to find the graphics queue
231 mGraphicsQueueIndex = queueCount;
232 for (uint32_t i = 0; i < queueCount; i++) {
233 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
234 mGraphicsQueueIndex = i;
Alec Mouri219997a2023-05-23 17:25:19 +0000235 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400236 break;
237 }
238 }
Stan Iliev90276c82019-02-03 18:01:02 -0500239 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400240
Greg Daniel2ff202712018-06-14 11:50:10 -0400241 {
242 uint32_t extensionCount = 0;
243 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700244 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500245 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800246 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400247 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700248 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500249 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400250 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800251 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400252 if (!shouldEnableExtension(extension.extensionName)) {
253 ALOGV("Not enabling device extension %s", extension.extensionName);
254 continue;
255 }
256 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800257 mDeviceExtensions.push_back(extension.extensionName);
258 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400259 hasKHRSwapchainExtension = true;
260 }
261 }
Stan Iliev90276c82019-02-03 18:01:02 -0500262 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400263 }
264
Alec Mouri219997a2023-05-23 17:25:19 +0000265 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
266 if (device != VK_NULL_HANDLE) {
267 return vkGetDeviceProcAddr(device, proc_name);
268 }
269 return vkGetInstanceProcAddr(instance, proc_name);
270 };
271
272 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700273 mInstanceExtensions.data(), mDeviceExtensions.size(),
274 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400275
Stan Iliev90276c82019-02-03 18:01:02 -0500276 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400277
Greg Daniela227dbb2018-08-20 09:19:48 -0400278 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
279 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
280 features.pNext = nullptr;
281
282 // Setup all extension feature structs we may want to use.
283 void** tailPNext = &features.pNext;
284
285 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
286 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700287 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400288 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
289 LOG_ALWAYS_FATAL_IF(!blend);
290 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
291 blend->pNext = nullptr;
292 *tailPNext = blend;
293 tailPNext = &blend->pNext;
294 }
295
Greg Daniel05036172018-11-28 17:08:04 -0500296 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700297 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500298 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
299 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
300 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
301 ycbcrFeature->pNext = nullptr;
302 *tailPNext = ycbcrFeature;
303 tailPNext = &ycbcrFeature->pNext;
304
Greg Daniela227dbb2018-08-20 09:19:48 -0400305 // query to get the physical device features
306 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400307 // this looks like it would slow things down,
308 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400309 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400310
Alec Mouri219997a2023-05-23 17:25:19 +0000311 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400312
Stan Iliev7e733362019-02-28 13:16:36 -0500313 void* queueNextPtr = nullptr;
314
315 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
316
John Reck0fa0cbc2019-04-05 16:57:46 -0700317 if (Properties::contextPriority != 0 &&
318 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500319 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
320 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700321 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500322 queuePriorityCreateInfo.pNext = nullptr;
323 switch (Properties::contextPriority) {
324 case EGL_CONTEXT_PRIORITY_LOW_IMG:
325 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
326 break;
327 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
328 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
329 break;
330 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
331 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
332 break;
333 default:
334 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700335 }
336 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500337 }
338
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700339 const VkDeviceQueueCreateInfo queueInfo = {
340 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
341 queueNextPtr, // pNext
342 0, // VkDeviceQueueCreateFlags
343 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000344 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700345 queuePriorities, // pQueuePriorities
346 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400347
348 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700349 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
350 &features, // pNext
351 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700352 1, // queueCreateInfoCount
353 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700354 0, // layerCount
355 nullptr, // ppEnabledLayerNames
356 (uint32_t)mDeviceExtensions.size(), // extensionCount
357 mDeviceExtensions.data(), // ppEnabledExtensionNames
358 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400359 };
360
Stan Iliev90276c82019-02-03 18:01:02 -0500361 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400362
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500363 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500364 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500365 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700366 GET_DEV_PROC(CreateCommandPool);
367 GET_DEV_PROC(CreateFence);
368 GET_DEV_PROC(CreateSemaphore);
369 GET_DEV_PROC(DestroyCommandPool);
370 GET_DEV_PROC(DestroyDevice);
371 GET_DEV_PROC(DestroyFence);
372 GET_DEV_PROC(DestroySemaphore);
373 GET_DEV_PROC(DeviceWaitIdle);
374 GET_DEV_PROC(EndCommandBuffer);
375 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500376 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700377 GET_DEV_PROC(GetSemaphoreFdKHR);
378 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500379 GET_DEV_PROC(QueueSubmit);
380 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700381 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500382 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700383 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000384 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400385}
386
387void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000388 std::call_once(mInitFlag, [&] {
389 GET_PROC(EnumerateInstanceVersion);
390 uint32_t instanceVersion;
391 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
392 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400393
Alec Mouri671a9f62023-08-25 17:18:02 +0000394 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400395
Alec Mouri671a9f62023-08-25 17:18:02 +0000396 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
397 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400398
Alec Mouri671a9f62023-08-25 17:18:02 +0000399 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
400 mSwapBehavior = SwapBehavior::BufferAge;
401 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400402
Alec Mouri671a9f62023-08-25 17:18:02 +0000403 mInitialized = true;
404 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500405}
406
John Reck9fc3d272023-05-01 16:33:22 -0400407static void onGrContextReleased(void* context) {
408 VulkanManager* manager = (VulkanManager*)context;
409 manager->decStrong((void*)onGrContextReleased);
410}
Stan Iliev981afe72019-02-13 14:24:33 -0500411
John Reck9fc3d272023-05-01 16:33:22 -0400412sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
413 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000414 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
415 if (device != VK_NULL_HANDLE) {
416 return vkGetDeviceProcAddr(device, proc_name);
417 }
418 return vkGetInstanceProcAddr(instance, proc_name);
419 };
420
Stan Iliev981afe72019-02-13 14:24:33 -0500421 GrVkBackendContext backendContext;
422 backendContext.fInstance = mInstance;
423 backendContext.fPhysicalDevice = mPhysicalDevice;
424 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000425 backendContext.fQueue =
426 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500427 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
428 backendContext.fMaxAPIVersion = mAPIVersion;
429 backendContext.fVkExtensions = &mExtensions;
430 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000431 backendContext.fGetProc = std::move(getProc);
Stan Iliev981afe72019-02-13 14:24:33 -0500432
John Reck9fc3d272023-05-01 16:33:22 -0400433 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
434 this->incStrong((void*)onGrContextReleased);
435 options.fContextDeleteContext = this;
436 options.fContextDeleteProc = onGrContextReleased;
437
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400438 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500439}
440
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800441VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
442 return VkFunctorInitParams{
443 .instance = mInstance,
444 .physical_device = mPhysicalDevice,
445 .device = mDevice,
446 .queue = mGraphicsQueue,
447 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500448 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800449 .enabled_instance_extension_names = mInstanceExtensions.data(),
450 .enabled_instance_extension_names_length =
451 static_cast<uint32_t>(mInstanceExtensions.size()),
452 .enabled_device_extension_names = mDeviceExtensions.data(),
453 .enabled_device_extension_names_length =
454 static_cast<uint32_t>(mDeviceExtensions.size()),
455 .device_features_2 = &mPhysicalDeviceFeatures2,
456 };
457}
458
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500459Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500460 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
461
462 if (bufferInfo == nullptr) {
463 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
464 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500465 }
466
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500467 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500468
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500469 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400470 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
471 bool isSignalPending = false;
472 if (finfo != NULL) {
473 isSignalPending = finfo->status != 1;
474 sync_file_info_free(finfo);
475 }
476 if (isSignalPending) {
477 int fence_clone = dup(bufferInfo->dequeue_fence);
478 if (fence_clone == -1) {
479 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
480 errno);
481 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
482 } else {
483 VkSemaphoreCreateInfo semaphoreInfo;
484 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
485 semaphoreInfo.pNext = nullptr;
486 semaphoreInfo.flags = 0;
487 VkSemaphore semaphore;
488 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400489 if (err != VK_SUCCESS) {
490 ALOGE("Failed to create import semaphore, err: %d", err);
491 close(fence_clone);
492 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
493 } else {
494 VkImportSemaphoreFdInfoKHR importInfo;
495 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
496 importInfo.pNext = nullptr;
497 importInfo.semaphore = semaphore;
498 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
499 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
500 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500501
Greg Danield6670772021-06-09 12:01:12 -0400502 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
503 if (err != VK_SUCCESS) {
504 ALOGE("Failed to import semaphore, err: %d", err);
505 mDestroySemaphore(mDevice, semaphore, nullptr);
506 close(fence_clone);
507 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
508 } else {
509 GrBackendSemaphore backendSemaphore;
510 backendSemaphore.initVulkan(semaphore);
511 // Skia will take ownership of the VkSemaphore and delete it once the wait
512 // has finished. The VkSemaphore also owns the imported fd, so it will
513 // close the fd when it is deleted.
514 bufferInfo->skSurface->wait(1, &backendSemaphore);
515 // The following flush blocks the GPU immediately instead of waiting for
516 // other drawing ops. It seems dequeue_fence is not respected otherwise.
517 // TODO: remove the flush after finding why backendSemaphore is not working.
Kevin Lubickaa592d02023-05-30 15:07:06 +0000518 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface);
Greg Danield6670772021-06-09 12:01:12 -0400519 }
520 }
Stan Iliev197843d2019-03-21 11:34:15 -0400521 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500522 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500523 }
524
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500525 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
526 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500527}
528
Greg Danield92a9b12019-04-23 10:11:04 -0400529struct DestroySemaphoreInfo {
530 PFN_vkDestroySemaphore mDestroyFunction;
531 VkDevice mDevice;
532 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400533 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
534 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
535 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
536 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
537 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
538 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
539 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400540
541 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400542 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400543 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
544};
545
546static void destroy_semaphore(void* context) {
547 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400548 --info->mRefs;
549 if (!info->mRefs) {
550 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
551 delete info;
552 }
Greg Danield92a9b12019-04-23 10:11:04 -0400553}
554
Alec Mouri3afb3972022-05-27 22:03:11 +0000555nsecs_t VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500556 ATRACE_NAME("Vulkan finish frame");
557 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
558 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400559
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500560 VkExportSemaphoreCreateInfo exportInfo;
561 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
562 exportInfo.pNext = nullptr;
563 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500564
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500565 VkSemaphoreCreateInfo semaphoreInfo;
566 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
567 semaphoreInfo.pNext = &exportInfo;
568 semaphoreInfo.flags = 0;
569 VkSemaphore semaphore;
570 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500571 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500572
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500573 GrBackendSemaphore backendSemaphore;
574 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500575
Greg Danielc7ad4082020-05-14 15:38:26 -0400576 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500577 if (err == VK_SUCCESS) {
578 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
579 flushInfo.fNumSemaphores = 1;
580 flushInfo.fSignalSemaphores = &backendSemaphore;
581 flushInfo.fFinishedProc = destroy_semaphore;
582 flushInfo.fFinishedContext = mDestroySemaphoreContext;
583 } else {
584 semaphore = VK_NULL_HANDLE;
585 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500586 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400587 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000588 GrSemaphoresSubmitted submitted = context->flush(
589 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400590 context->submit();
Alec Mouri3afb3972022-05-27 22:03:11 +0000591 const nsecs_t submissionTime = systemTime();
Greg Danielbe2803a2021-02-19 18:32:16 -0500592 if (semaphore != VK_NULL_HANDLE) {
593 if (submitted == GrSemaphoresSubmitted::kYes) {
594 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000595 if (mFrameBoundaryANDROID) {
596 // retrieve VkImage used as render target
597 VkImage image = VK_NULL_HANDLE;
598 GrBackendRenderTarget backendRenderTarget =
Kevin Lubick098ed4c2023-05-08 12:03:59 +0000599 SkSurfaces::GetBackendRenderTarget(
600 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000601 if (backendRenderTarget.isValid()) {
602 GrVkImageInfo info;
Brian Osman734d8102023-08-29 18:07:56 +0000603 if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) {
Hugues Evrardb9510a02021-03-01 14:35:22 +0000604 image = info.fImage;
605 } else {
606 ALOGE("Frame boundary: backend is not vulkan");
607 }
608 } else {
609 ALOGE("Frame boundary: invalid backend render target");
610 }
611 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
612 // it won't wait on it.
613 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
614 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500615 } else {
616 destroy_semaphore(mDestroySemaphoreContext);
617 mDestroySemaphoreContext = nullptr;
618 }
619 }
620 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000621
622 return submissionTime;
Greg Danielbe2803a2021-02-19 18:32:16 -0500623}
624
625void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
626 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
627 ATRACE_NAME("Finishing GPU work");
628 mDeviceWaitIdle(mDevice);
629 }
630
631 int fenceFd = -1;
632 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500633 VkSemaphoreGetFdInfoKHR getFdInfo;
634 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
635 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500636 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500637 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500638
Greg Danielbe2803a2021-02-19 18:32:16 -0500639 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500640 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
641 } else {
642 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
643 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500644 }
Greg Daniele8dc3972021-07-15 13:38:44 -0400645 if (mDestroySemaphoreContext) {
646 destroy_semaphore(mDestroySemaphoreContext);
647 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500648
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500649 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500650 mSwapSemaphore = VK_NULL_HANDLE;
651 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500652}
653
654void VulkanManager::destroySurface(VulkanSurface* surface) {
655 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700656 if (VK_NULL_HANDLE != mGraphicsQueue) {
657 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500658 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500659
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500660 delete surface;
661}
662
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400663VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
664 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800665 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400666 SkColorType surfaceColorType,
667 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700668 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500669 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500670 if (!window) {
671 return nullptr;
672 }
673
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500674 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700675 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500676}
677
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400678status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400679 if (!hasVkContext()) {
680 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
681 return INVALID_OPERATION;
682 }
683
Stan Iliev7a081272018-10-26 17:54:18 -0400684 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400685 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400686 if (fenceFd == -1) {
687 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
688 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000689 }
Stan Iliev7a081272018-10-26 17:54:18 -0400690
691 VkSemaphoreCreateInfo semaphoreInfo;
692 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
693 semaphoreInfo.pNext = nullptr;
694 semaphoreInfo.flags = 0;
695 VkSemaphore semaphore;
696 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
697 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400698 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400699 ALOGE("Failed to create import semaphore, err: %d", err);
700 return UNKNOWN_ERROR;
701 }
702 VkImportSemaphoreFdInfoKHR importInfo;
703 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
704 importInfo.pNext = nullptr;
705 importInfo.semaphore = semaphore;
706 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
707 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
708 importInfo.fd = fenceFd;
709
710 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
711 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400712 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400713 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400714 ALOGE("Failed to import semaphore, err: %d", err);
715 return UNKNOWN_ERROR;
716 }
717
Greg Danield92a9b12019-04-23 10:11:04 -0400718 GrBackendSemaphore beSemaphore;
719 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400720
Greg Danield6670772021-06-09 12:01:12 -0400721 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
722 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400723 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400724 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400725
Stan Iliev564ca3e2018-09-04 22:00:00 +0000726 return OK;
727}
728
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400729status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400730 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400731 if (!hasVkContext()) {
732 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
733 return INVALID_OPERATION;
734 }
735
Greg Daniel26e0dca2018-09-18 10:33:19 -0400736 VkExportSemaphoreCreateInfo exportInfo;
737 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
738 exportInfo.pNext = nullptr;
739 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
740
741 VkSemaphoreCreateInfo semaphoreInfo;
742 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
743 semaphoreInfo.pNext = &exportInfo;
744 semaphoreInfo.flags = 0;
745 VkSemaphore semaphore;
746 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
747 if (VK_SUCCESS != err) {
748 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
749 return INVALID_OPERATION;
750 }
751
Greg Danield92a9b12019-04-23 10:11:04 -0400752 GrBackendSemaphore backendSemaphore;
753 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400754
Stan Ilievaaa9e832019-09-17 14:07:23 -0400755 DestroySemaphoreInfo* destroyInfo =
756 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400757 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
758 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
759 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400760 GrFlushInfo flushInfo;
761 flushInfo.fNumSemaphores = 1;
762 flushInfo.fSignalSemaphores = &backendSemaphore;
763 flushInfo.fFinishedProc = destroy_semaphore;
764 flushInfo.fFinishedContext = destroyInfo;
765 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
766 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400767
Greg Danield92a9b12019-04-23 10:11:04 -0400768 if (submitted == GrSemaphoresSubmitted::kNo) {
769 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400770 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400771 return INVALID_OPERATION;
772 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400773
774 VkSemaphoreGetFdInfoKHR getFdInfo;
775 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
776 getFdInfo.pNext = nullptr;
777 getFdInfo.semaphore = semaphore;
778 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
779
780 int fenceFd = 0;
781
782 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400783 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400784 if (VK_SUCCESS != err) {
785 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
786 return INVALID_OPERATION;
787 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400788 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400789
Stan Iliev564ca3e2018-09-04 22:00:00 +0000790 return OK;
791}
792
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500793} /* namespace renderthread */
794} /* namespace uirenderer */
795} /* namespace android */