blob: f3409455d40132dbed92008aa43c060976834d9a [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>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000028#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040029#include <vk/GrVkExtensions.h>
30#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050031
Greg Danielcd558522016-11-17 13:31:40 -050032#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050033#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050034#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050035#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050036
Leon Scroggins III7ccb8a42021-11-30 14:17:28 -050037#undef LOG_TAG
38#define LOG_TAG "VulkanManager"
39
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050040namespace android {
41namespace uirenderer {
42namespace renderthread {
43
Mattias Simonssone545f962023-07-17 08:03:14 +000044static std::array<std::string_view, 20> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040045 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
46 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
47 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
48 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
49 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
50 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
51 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
52 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
53 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
54 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
55 VK_KHR_SURFACE_EXTENSION_NAME,
56 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
57 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040058 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040059 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
60 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
61 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
62 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
63 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Mattias Simonssone545f962023-07-17 08:03:14 +000064 VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040065};
66
67static bool shouldEnableExtension(const std::string_view& extension) {
68 for (const auto& it : sEnableExtensions) {
69 if (it == extension) {
70 return true;
71 }
72 }
73 return false;
74}
75
Bo Liu7b8c1eb2019-01-08 20:17:55 -080076static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
77 // All Vulkan structs that could be part of the features chain will start with the
78 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
79 // so we can get access to the pNext for the next struct.
80 struct CommonVulkanHeader {
81 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070082 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080083 };
84
85 void* pNext = features.pNext;
86 while (pNext) {
87 void* current = pNext;
88 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
89 free(current);
90 }
91}
92
Greg Daniel2ff202712018-06-14 11:50:10 -040093#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
94#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
95#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096
John Reck9fc3d272023-05-01 16:33:22 -040097// cache a weakptr to the context to enable a second thread to share the same vulkan state
98static wp<VulkanManager> sWeakInstance = nullptr;
99static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400100
John Reck9fc3d272023-05-01 16:33:22 -0400101sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400102 std::lock_guard _lock{sLock};
103 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
104 if (!vulkanManager.get()) {
105 vulkanManager = new VulkanManager();
106 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500107 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500108
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400109 return vulkanManager;
110}
111
John Reck9fc3d272023-05-01 16:33:22 -0400112sp<VulkanManager> VulkanManager::peekInstance() {
113 std::lock_guard _lock{sLock};
114 return sWeakInstance.promote();
115}
116
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400117VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400118 if (mDevice != VK_NULL_HANDLE) {
119 mDeviceWaitIdle(mDevice);
120 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700121 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500122
Greg Daniel2ff202712018-06-14 11:50:10 -0400123 if (mInstance != VK_NULL_HANDLE) {
124 mDestroyInstance(mInstance, nullptr);
125 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500126
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000128 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400129 mDevice = VK_NULL_HANDLE;
130 mPhysicalDevice = VK_NULL_HANDLE;
131 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800132 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800133 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800134 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800135 mDeviceExtensions.clear();
136 free_features_extensions_structs(mPhysicalDeviceFeatures2);
137 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400138}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139
Stan Iliev90276c82019-02-03 18:01:02 -0500140void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400141 VkResult err;
142
143 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700144 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
145 nullptr, // pNext
146 "android framework", // pApplicationName
147 0, // applicationVersion
148 "android framework", // pEngineName
149 0, // engineVerison
150 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400151 };
152
Greg Daniel2ff202712018-06-14 11:50:10 -0400153 {
154 GET_PROC(EnumerateInstanceExtensionProperties);
155
156 uint32_t extensionCount = 0;
157 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500158 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800159 mInstanceExtensionsOwner.resize(extensionCount);
160 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
161 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500162 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400163 bool hasKHRSurfaceExtension = false;
164 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800165 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400166 if (!shouldEnableExtension(extension.extensionName)) {
167 ALOGV("Not enabling instance extension %s", extension.extensionName);
168 continue;
169 }
170 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800171 mInstanceExtensions.push_back(extension.extensionName);
172 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400173 hasKHRSurfaceExtension = true;
174 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800175 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400176 hasKHRAndroidSurfaceExtension = true;
177 }
178 }
Stan Iliev90276c82019-02-03 18:01:02 -0500179 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400180 }
181
182 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700183 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
184 nullptr, // pNext
185 0, // flags
186 &app_info, // pApplicationInfo
187 0, // enabledLayerNameCount
188 nullptr, // ppEnabledLayerNames
189 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
190 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400191 };
192
193 GET_PROC(CreateInstance);
194 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500195 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400196
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700197 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400198 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700199 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400200 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400201 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500202 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700203 GET_INST_PROC(GetPhysicalDeviceProperties);
204 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400205
206 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500207 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
208 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400209 // Just returning the first physical device instead of getting the whole array. Since there
210 // should only be one device on android.
211 gpuCount = 1;
212 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
213 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500214 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400215
Greg Daniel96259622018-10-01 14:42:56 -0400216 VkPhysicalDeviceProperties physDeviceProperties;
217 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500218 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400219 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400220
Greg Daniel2ff202712018-06-14 11:50:10 -0400221 // query to get the initial queue props size
Alec Mouri219997a2023-05-23 17:25:19 +0000222 uint32_t queueCount = 0;
Greg Daniel2ff202712018-06-14 11:50:10 -0400223 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500224 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400225
226 // now get the actual queue props
227 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
228 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
229
Alec Mouri219997a2023-05-23 17:25:19 +0000230 constexpr auto kRequestedQueueCount = 2;
231
Greg Daniel2ff202712018-06-14 11:50:10 -0400232 // iterate to find the graphics queue
233 mGraphicsQueueIndex = queueCount;
234 for (uint32_t i = 0; i < queueCount; i++) {
235 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
236 mGraphicsQueueIndex = i;
Alec Mouri219997a2023-05-23 17:25:19 +0000237 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400238 break;
239 }
240 }
Stan Iliev90276c82019-02-03 18:01:02 -0500241 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400242
Greg Daniel2ff202712018-06-14 11:50:10 -0400243 {
244 uint32_t extensionCount = 0;
245 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700246 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500247 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800248 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400249 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700250 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500251 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400252 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800253 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400254 if (!shouldEnableExtension(extension.extensionName)) {
255 ALOGV("Not enabling device extension %s", extension.extensionName);
256 continue;
257 }
258 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800259 mDeviceExtensions.push_back(extension.extensionName);
260 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400261 hasKHRSwapchainExtension = true;
262 }
263 }
Stan Iliev90276c82019-02-03 18:01:02 -0500264 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400265 }
266
Alec Mouri219997a2023-05-23 17:25:19 +0000267 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
268 if (device != VK_NULL_HANDLE) {
269 return vkGetDeviceProcAddr(device, proc_name);
270 }
271 return vkGetInstanceProcAddr(instance, proc_name);
272 };
273
274 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700275 mInstanceExtensions.data(), mDeviceExtensions.size(),
276 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400277
Stan Iliev90276c82019-02-03 18:01:02 -0500278 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400279
Greg Daniela227dbb2018-08-20 09:19:48 -0400280 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
281 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
282 features.pNext = nullptr;
283
284 // Setup all extension feature structs we may want to use.
285 void** tailPNext = &features.pNext;
286
287 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
288 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700289 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400290 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
291 LOG_ALWAYS_FATAL_IF(!blend);
292 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
293 blend->pNext = nullptr;
294 *tailPNext = blend;
295 tailPNext = &blend->pNext;
296 }
297
Greg Daniel05036172018-11-28 17:08:04 -0500298 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700299 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500300 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
301 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
302 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
303 ycbcrFeature->pNext = nullptr;
304 *tailPNext = ycbcrFeature;
305 tailPNext = &ycbcrFeature->pNext;
306
Greg Daniela227dbb2018-08-20 09:19:48 -0400307 // query to get the physical device features
308 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400309 // this looks like it would slow things down,
310 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400311 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400312
Alec Mouri219997a2023-05-23 17:25:19 +0000313 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400314
Stan Iliev7e733362019-02-28 13:16:36 -0500315 void* queueNextPtr = nullptr;
316
317 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
318
John Reck0fa0cbc2019-04-05 16:57:46 -0700319 if (Properties::contextPriority != 0 &&
320 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500321 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
322 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700323 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500324 queuePriorityCreateInfo.pNext = nullptr;
325 switch (Properties::contextPriority) {
326 case EGL_CONTEXT_PRIORITY_LOW_IMG:
327 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
328 break;
329 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
330 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
331 break;
332 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
333 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
334 break;
335 default:
336 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700337 }
338 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500339 }
340
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700341 const VkDeviceQueueCreateInfo queueInfo = {
342 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
343 queueNextPtr, // pNext
344 0, // VkDeviceQueueCreateFlags
345 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000346 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700347 queuePriorities, // pQueuePriorities
348 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400349
350 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700351 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
352 &features, // pNext
353 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700354 1, // queueCreateInfoCount
355 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700356 0, // layerCount
357 nullptr, // ppEnabledLayerNames
358 (uint32_t)mDeviceExtensions.size(), // extensionCount
359 mDeviceExtensions.data(), // ppEnabledExtensionNames
360 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400361 };
362
Stan Iliev90276c82019-02-03 18:01:02 -0500363 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400364
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500365 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500366 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500367 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700368 GET_DEV_PROC(CreateCommandPool);
369 GET_DEV_PROC(CreateFence);
370 GET_DEV_PROC(CreateSemaphore);
371 GET_DEV_PROC(DestroyCommandPool);
372 GET_DEV_PROC(DestroyDevice);
373 GET_DEV_PROC(DestroyFence);
374 GET_DEV_PROC(DestroySemaphore);
375 GET_DEV_PROC(DeviceWaitIdle);
376 GET_DEV_PROC(EndCommandBuffer);
377 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500378 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700379 GET_DEV_PROC(GetSemaphoreFdKHR);
380 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500381 GET_DEV_PROC(QueueSubmit);
382 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700383 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500384 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700385 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000386 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400387}
388
389void VulkanManager::initialize() {
Greg Daniel1d9de712021-05-14 09:28:34 -0400390 std::lock_guard _lock{mInitializeLock};
391
Greg Daniel2ff202712018-06-14 11:50:10 -0400392 if (mDevice != VK_NULL_HANDLE) {
393 return;
394 }
395
Greg Daniela227dbb2018-08-20 09:19:48 -0400396 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500397 uint32_t instanceVersion;
398 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
399 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400400
Stan Iliev981afe72019-02-13 14:24:33 -0500401 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400402
403 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
Alec Mouri219997a2023-05-23 17:25:19 +0000404 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniel2ff202712018-06-14 11:50:10 -0400405
Greg Danielcd558522016-11-17 13:31:40 -0500406 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
407 mSwapBehavior = SwapBehavior::BufferAge;
408 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500409}
410
John Reck9fc3d272023-05-01 16:33:22 -0400411static void onGrContextReleased(void* context) {
412 VulkanManager* manager = (VulkanManager*)context;
413 manager->decStrong((void*)onGrContextReleased);
414}
Stan Iliev981afe72019-02-13 14:24:33 -0500415
John Reck9fc3d272023-05-01 16:33:22 -0400416sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
417 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000418 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
419 if (device != VK_NULL_HANDLE) {
420 return vkGetDeviceProcAddr(device, proc_name);
421 }
422 return vkGetInstanceProcAddr(instance, proc_name);
423 };
424
Stan Iliev981afe72019-02-13 14:24:33 -0500425 GrVkBackendContext backendContext;
426 backendContext.fInstance = mInstance;
427 backendContext.fPhysicalDevice = mPhysicalDevice;
428 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000429 backendContext.fQueue =
430 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500431 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
432 backendContext.fMaxAPIVersion = mAPIVersion;
433 backendContext.fVkExtensions = &mExtensions;
434 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000435 backendContext.fGetProc = std::move(getProc);
Stan Iliev981afe72019-02-13 14:24:33 -0500436
John Reck9fc3d272023-05-01 16:33:22 -0400437 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
438 this->incStrong((void*)onGrContextReleased);
439 options.fContextDeleteContext = this;
440 options.fContextDeleteProc = onGrContextReleased;
441
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400442 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500443}
444
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800445VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
446 return VkFunctorInitParams{
447 .instance = mInstance,
448 .physical_device = mPhysicalDevice,
449 .device = mDevice,
450 .queue = mGraphicsQueue,
451 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500452 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800453 .enabled_instance_extension_names = mInstanceExtensions.data(),
454 .enabled_instance_extension_names_length =
455 static_cast<uint32_t>(mInstanceExtensions.size()),
456 .enabled_device_extension_names = mDeviceExtensions.data(),
457 .enabled_device_extension_names_length =
458 static_cast<uint32_t>(mDeviceExtensions.size()),
459 .device_features_2 = &mPhysicalDeviceFeatures2,
460 };
461}
462
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500463Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500464 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
465
466 if (bufferInfo == nullptr) {
467 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
468 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500469 }
470
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500471 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500472
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500473 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400474 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
475 bool isSignalPending = false;
476 if (finfo != NULL) {
477 isSignalPending = finfo->status != 1;
478 sync_file_info_free(finfo);
479 }
480 if (isSignalPending) {
481 int fence_clone = dup(bufferInfo->dequeue_fence);
482 if (fence_clone == -1) {
483 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
484 errno);
485 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
486 } else {
487 VkSemaphoreCreateInfo semaphoreInfo;
488 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
489 semaphoreInfo.pNext = nullptr;
490 semaphoreInfo.flags = 0;
491 VkSemaphore semaphore;
492 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400493 if (err != VK_SUCCESS) {
494 ALOGE("Failed to create import semaphore, err: %d", err);
495 close(fence_clone);
496 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
497 } else {
498 VkImportSemaphoreFdInfoKHR importInfo;
499 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
500 importInfo.pNext = nullptr;
501 importInfo.semaphore = semaphore;
502 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
503 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
504 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500505
Greg Danield6670772021-06-09 12:01:12 -0400506 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
507 if (err != VK_SUCCESS) {
508 ALOGE("Failed to import semaphore, err: %d", err);
509 mDestroySemaphore(mDevice, semaphore, nullptr);
510 close(fence_clone);
511 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
512 } else {
513 GrBackendSemaphore backendSemaphore;
514 backendSemaphore.initVulkan(semaphore);
515 // Skia will take ownership of the VkSemaphore and delete it once the wait
516 // has finished. The VkSemaphore also owns the imported fd, so it will
517 // close the fd when it is deleted.
518 bufferInfo->skSurface->wait(1, &backendSemaphore);
519 // The following flush blocks the GPU immediately instead of waiting for
520 // other drawing ops. It seems dequeue_fence is not respected otherwise.
521 // TODO: remove the flush after finding why backendSemaphore is not working.
Kevin Lubickaa592d02023-05-30 15:07:06 +0000522 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface);
Greg Danield6670772021-06-09 12:01:12 -0400523 }
524 }
Stan Iliev197843d2019-03-21 11:34:15 -0400525 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500526 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500527 }
528
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500529 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
530 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500531}
532
Greg Danield92a9b12019-04-23 10:11:04 -0400533struct DestroySemaphoreInfo {
534 PFN_vkDestroySemaphore mDestroyFunction;
535 VkDevice mDevice;
536 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400537 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
538 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
539 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
540 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
541 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
542 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
543 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400544
545 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400546 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400547 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
548};
549
550static void destroy_semaphore(void* context) {
551 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400552 --info->mRefs;
553 if (!info->mRefs) {
554 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
555 delete info;
556 }
Greg Danield92a9b12019-04-23 10:11:04 -0400557}
558
Alec Mouri3afb3972022-05-27 22:03:11 +0000559nsecs_t VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500560 ATRACE_NAME("Vulkan finish frame");
561 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
562 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400563
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500564 VkExportSemaphoreCreateInfo exportInfo;
565 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
566 exportInfo.pNext = nullptr;
567 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500568
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500569 VkSemaphoreCreateInfo semaphoreInfo;
570 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
571 semaphoreInfo.pNext = &exportInfo;
572 semaphoreInfo.flags = 0;
573 VkSemaphore semaphore;
574 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500575 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500576
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500577 GrBackendSemaphore backendSemaphore;
578 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500579
Greg Danielc7ad4082020-05-14 15:38:26 -0400580 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500581 if (err == VK_SUCCESS) {
582 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
583 flushInfo.fNumSemaphores = 1;
584 flushInfo.fSignalSemaphores = &backendSemaphore;
585 flushInfo.fFinishedProc = destroy_semaphore;
586 flushInfo.fFinishedContext = mDestroySemaphoreContext;
587 } else {
588 semaphore = VK_NULL_HANDLE;
589 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500590 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400591 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000592 GrSemaphoresSubmitted submitted = context->flush(
593 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400594 context->submit();
Alec Mouri3afb3972022-05-27 22:03:11 +0000595 const nsecs_t submissionTime = systemTime();
Greg Danielbe2803a2021-02-19 18:32:16 -0500596 if (semaphore != VK_NULL_HANDLE) {
597 if (submitted == GrSemaphoresSubmitted::kYes) {
598 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000599 if (mFrameBoundaryANDROID) {
600 // retrieve VkImage used as render target
601 VkImage image = VK_NULL_HANDLE;
602 GrBackendRenderTarget backendRenderTarget =
Kevin Lubick098ed4c2023-05-08 12:03:59 +0000603 SkSurfaces::GetBackendRenderTarget(
604 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000605 if (backendRenderTarget.isValid()) {
606 GrVkImageInfo info;
607 if (backendRenderTarget.getVkImageInfo(&info)) {
608 image = info.fImage;
609 } else {
610 ALOGE("Frame boundary: backend is not vulkan");
611 }
612 } else {
613 ALOGE("Frame boundary: invalid backend render target");
614 }
615 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
616 // it won't wait on it.
617 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
618 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500619 } else {
620 destroy_semaphore(mDestroySemaphoreContext);
621 mDestroySemaphoreContext = nullptr;
622 }
623 }
624 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000625
626 return submissionTime;
Greg Danielbe2803a2021-02-19 18:32:16 -0500627}
628
629void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
630 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
631 ATRACE_NAME("Finishing GPU work");
632 mDeviceWaitIdle(mDevice);
633 }
634
635 int fenceFd = -1;
636 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500637 VkSemaphoreGetFdInfoKHR getFdInfo;
638 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
639 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500640 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500641 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500642
Greg Danielbe2803a2021-02-19 18:32:16 -0500643 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500644 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
645 } else {
646 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
647 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500648 }
Greg Daniele8dc3972021-07-15 13:38:44 -0400649 if (mDestroySemaphoreContext) {
650 destroy_semaphore(mDestroySemaphoreContext);
651 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500652
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500653 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500654 mSwapSemaphore = VK_NULL_HANDLE;
655 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500656}
657
658void VulkanManager::destroySurface(VulkanSurface* surface) {
659 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700660 if (VK_NULL_HANDLE != mGraphicsQueue) {
661 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500662 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500663
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500664 delete surface;
665}
666
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400667VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
668 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800669 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400670 SkColorType surfaceColorType,
671 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700672 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500673 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500674 if (!window) {
675 return nullptr;
676 }
677
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500678 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700679 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500680}
681
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400682status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400683 if (!hasVkContext()) {
684 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
685 return INVALID_OPERATION;
686 }
687
Stan Iliev7a081272018-10-26 17:54:18 -0400688 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400689 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400690 if (fenceFd == -1) {
691 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
692 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000693 }
Stan Iliev7a081272018-10-26 17:54:18 -0400694
695 VkSemaphoreCreateInfo semaphoreInfo;
696 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
697 semaphoreInfo.pNext = nullptr;
698 semaphoreInfo.flags = 0;
699 VkSemaphore semaphore;
700 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
701 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400702 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400703 ALOGE("Failed to create import semaphore, err: %d", err);
704 return UNKNOWN_ERROR;
705 }
706 VkImportSemaphoreFdInfoKHR importInfo;
707 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
708 importInfo.pNext = nullptr;
709 importInfo.semaphore = semaphore;
710 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
711 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
712 importInfo.fd = fenceFd;
713
714 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
715 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400716 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400717 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400718 ALOGE("Failed to import semaphore, err: %d", err);
719 return UNKNOWN_ERROR;
720 }
721
Greg Danield92a9b12019-04-23 10:11:04 -0400722 GrBackendSemaphore beSemaphore;
723 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400724
Greg Danield6670772021-06-09 12:01:12 -0400725 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
726 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400727 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400728 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400729
Stan Iliev564ca3e2018-09-04 22:00:00 +0000730 return OK;
731}
732
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400733status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400734 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400735 if (!hasVkContext()) {
736 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
737 return INVALID_OPERATION;
738 }
739
Greg Daniel26e0dca2018-09-18 10:33:19 -0400740 VkExportSemaphoreCreateInfo exportInfo;
741 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
742 exportInfo.pNext = nullptr;
743 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
744
745 VkSemaphoreCreateInfo semaphoreInfo;
746 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
747 semaphoreInfo.pNext = &exportInfo;
748 semaphoreInfo.flags = 0;
749 VkSemaphore semaphore;
750 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
751 if (VK_SUCCESS != err) {
752 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
753 return INVALID_OPERATION;
754 }
755
Greg Danield92a9b12019-04-23 10:11:04 -0400756 GrBackendSemaphore backendSemaphore;
757 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400758
Stan Ilievaaa9e832019-09-17 14:07:23 -0400759 DestroySemaphoreInfo* destroyInfo =
760 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400761 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
762 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
763 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400764 GrFlushInfo flushInfo;
765 flushInfo.fNumSemaphores = 1;
766 flushInfo.fSignalSemaphores = &backendSemaphore;
767 flushInfo.fFinishedProc = destroy_semaphore;
768 flushInfo.fFinishedContext = destroyInfo;
769 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
770 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400771
Greg Danield92a9b12019-04-23 10:11:04 -0400772 if (submitted == GrSemaphoresSubmitted::kNo) {
773 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400774 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400775 return INVALID_OPERATION;
776 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400777
778 VkSemaphoreGetFdInfoKHR getFdInfo;
779 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
780 getFdInfo.pNext = nullptr;
781 getFdInfo.semaphore = semaphore;
782 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
783
784 int fenceFd = 0;
785
786 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400787 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400788 if (VK_SUCCESS != err) {
789 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
790 return INVALID_OPERATION;
791 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400792 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400793
Stan Iliev564ca3e2018-09-04 22:00:00 +0000794 return OK;
795}
796
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500797} /* namespace renderthread */
798} /* namespace uirenderer */
799} /* namespace android */