blob: 31a92ac5ab239cbbe0b88d9989548e4d52718751 [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 Mourid290c36e2023-05-23 17:25:19 +000026#include <gui/TraceUtils.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000027#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040028#include <vk/GrVkExtensions.h>
29#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050030
Greg Danielcd558522016-11-17 13:31:40 -050031#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050032#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050033#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050034#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050035
Leon Scroggins III7ccb8a42021-11-30 14:17:28 -050036#undef LOG_TAG
37#define LOG_TAG "VulkanManager"
38
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050039namespace android {
40namespace uirenderer {
41namespace renderthread {
42
John Reck90b244d2023-04-28 15:41:55 -040043static std::array<std::string_view, 19> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040044 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
45 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
46 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
47 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
48 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
49 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
50 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
51 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
52 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
53 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
54 VK_KHR_SURFACE_EXTENSION_NAME,
55 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
56 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040057 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040058 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
59 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
60 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
61 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
62 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
63};
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 Mourid290c36e2023-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 Mourid290c36e2023-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 Mourid290c36e2023-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 Mourid290c36e2023-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 Mourid290c36e2023-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 Mourid290c36e2023-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 Mourid290c36e2023-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() {
Greg Daniel1d9de712021-05-14 09:28:34 -0400388 std::lock_guard _lock{mInitializeLock};
389
Greg Daniel2ff202712018-06-14 11:50:10 -0400390 if (mDevice != VK_NULL_HANDLE) {
391 return;
392 }
393
Greg Daniela227dbb2018-08-20 09:19:48 -0400394 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500395 uint32_t instanceVersion;
396 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
397 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400398
Stan Iliev981afe72019-02-13 14:24:33 -0500399 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400400
401 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
Alec Mourid290c36e2023-05-23 17:25:19 +0000402 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniel2ff202712018-06-14 11:50:10 -0400403
Greg Danielcd558522016-11-17 13:31:40 -0500404 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
405 mSwapBehavior = SwapBehavior::BufferAge;
406 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500407}
408
John Reck9fc3d272023-05-01 16:33:22 -0400409static void onGrContextReleased(void* context) {
410 VulkanManager* manager = (VulkanManager*)context;
411 manager->decStrong((void*)onGrContextReleased);
412}
Stan Iliev981afe72019-02-13 14:24:33 -0500413
John Reck9fc3d272023-05-01 16:33:22 -0400414sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
415 ContextType contextType) {
Alec Mourid290c36e2023-05-23 17:25:19 +0000416 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
417 if (device != VK_NULL_HANDLE) {
418 return vkGetDeviceProcAddr(device, proc_name);
419 }
420 return vkGetInstanceProcAddr(instance, proc_name);
421 };
422
Stan Iliev981afe72019-02-13 14:24:33 -0500423 GrVkBackendContext backendContext;
424 backendContext.fInstance = mInstance;
425 backendContext.fPhysicalDevice = mPhysicalDevice;
426 backendContext.fDevice = mDevice;
Alec Mourid290c36e2023-05-23 17:25:19 +0000427 backendContext.fQueue =
428 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500429 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
430 backendContext.fMaxAPIVersion = mAPIVersion;
431 backendContext.fVkExtensions = &mExtensions;
432 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mourid290c36e2023-05-23 17:25:19 +0000433 backendContext.fGetProc = std::move(getProc);
Stan Iliev981afe72019-02-13 14:24:33 -0500434
John Reck9fc3d272023-05-01 16:33:22 -0400435 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
436 this->incStrong((void*)onGrContextReleased);
437 options.fContextDeleteContext = this;
438 options.fContextDeleteProc = onGrContextReleased;
439
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400440 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500441}
442
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800443VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
444 return VkFunctorInitParams{
445 .instance = mInstance,
446 .physical_device = mPhysicalDevice,
447 .device = mDevice,
448 .queue = mGraphicsQueue,
449 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500450 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800451 .enabled_instance_extension_names = mInstanceExtensions.data(),
452 .enabled_instance_extension_names_length =
453 static_cast<uint32_t>(mInstanceExtensions.size()),
454 .enabled_device_extension_names = mDeviceExtensions.data(),
455 .enabled_device_extension_names_length =
456 static_cast<uint32_t>(mDeviceExtensions.size()),
457 .device_features_2 = &mPhysicalDeviceFeatures2,
458 };
459}
460
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500461Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500462 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
463
464 if (bufferInfo == nullptr) {
465 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
466 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500467 }
468
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500469 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500470
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500471 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400472 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
473 bool isSignalPending = false;
474 if (finfo != NULL) {
475 isSignalPending = finfo->status != 1;
476 sync_file_info_free(finfo);
477 }
478 if (isSignalPending) {
479 int fence_clone = dup(bufferInfo->dequeue_fence);
480 if (fence_clone == -1) {
481 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
482 errno);
483 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
484 } else {
485 VkSemaphoreCreateInfo semaphoreInfo;
486 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
487 semaphoreInfo.pNext = nullptr;
488 semaphoreInfo.flags = 0;
489 VkSemaphore semaphore;
490 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400491 if (err != VK_SUCCESS) {
492 ALOGE("Failed to create import semaphore, err: %d", err);
493 close(fence_clone);
494 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
495 } else {
496 VkImportSemaphoreFdInfoKHR importInfo;
497 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
498 importInfo.pNext = nullptr;
499 importInfo.semaphore = semaphore;
500 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
501 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
502 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500503
Greg Danield6670772021-06-09 12:01:12 -0400504 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
505 if (err != VK_SUCCESS) {
506 ALOGE("Failed to import semaphore, err: %d", err);
507 mDestroySemaphore(mDevice, semaphore, nullptr);
508 close(fence_clone);
509 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
510 } else {
511 GrBackendSemaphore backendSemaphore;
512 backendSemaphore.initVulkan(semaphore);
513 // Skia will take ownership of the VkSemaphore and delete it once the wait
514 // has finished. The VkSemaphore also owns the imported fd, so it will
515 // close the fd when it is deleted.
516 bufferInfo->skSurface->wait(1, &backendSemaphore);
517 // The following flush blocks the GPU immediately instead of waiting for
518 // other drawing ops. It seems dequeue_fence is not respected otherwise.
519 // TODO: remove the flush after finding why backendSemaphore is not working.
520 bufferInfo->skSurface->flushAndSubmit();
521 }
522 }
Stan Iliev197843d2019-03-21 11:34:15 -0400523 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500524 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500525 }
526
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500527 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
528 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500529}
530
Greg Danield92a9b12019-04-23 10:11:04 -0400531struct DestroySemaphoreInfo {
532 PFN_vkDestroySemaphore mDestroyFunction;
533 VkDevice mDevice;
534 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400535 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
536 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
537 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
538 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
539 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
540 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
541 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400542
543 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400544 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400545 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
546};
547
548static void destroy_semaphore(void* context) {
549 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400550 --info->mRefs;
551 if (!info->mRefs) {
552 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
553 delete info;
554 }
Greg Danield92a9b12019-04-23 10:11:04 -0400555}
556
Alec Mouri3afb3972022-05-27 22:03:11 +0000557nsecs_t VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500558 ATRACE_NAME("Vulkan finish frame");
559 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
560 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400561
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500562 VkExportSemaphoreCreateInfo exportInfo;
563 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
564 exportInfo.pNext = nullptr;
565 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500566
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500567 VkSemaphoreCreateInfo semaphoreInfo;
568 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
569 semaphoreInfo.pNext = &exportInfo;
570 semaphoreInfo.flags = 0;
571 VkSemaphore semaphore;
572 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500573 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500574
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500575 GrBackendSemaphore backendSemaphore;
576 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500577
Greg Danielc7ad4082020-05-14 15:38:26 -0400578 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500579 if (err == VK_SUCCESS) {
580 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
581 flushInfo.fNumSemaphores = 1;
582 flushInfo.fSignalSemaphores = &backendSemaphore;
583 flushInfo.fFinishedProc = destroy_semaphore;
584 flushInfo.fFinishedContext = mDestroySemaphoreContext;
585 } else {
586 semaphore = VK_NULL_HANDLE;
587 }
588 GrSemaphoresSubmitted submitted =
589 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
590 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400591 ALOGE_IF(!context, "Surface is not backed by gpu");
592 context->submit();
Alec Mouri3afb3972022-05-27 22:03:11 +0000593 const nsecs_t submissionTime = systemTime();
Greg Danielbe2803a2021-02-19 18:32:16 -0500594 if (semaphore != VK_NULL_HANDLE) {
595 if (submitted == GrSemaphoresSubmitted::kYes) {
596 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000597 if (mFrameBoundaryANDROID) {
598 // retrieve VkImage used as render target
599 VkImage image = VK_NULL_HANDLE;
600 GrBackendRenderTarget backendRenderTarget =
601 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
602 if (backendRenderTarget.isValid()) {
603 GrVkImageInfo info;
604 if (backendRenderTarget.getVkImageInfo(&info)) {
605 image = info.fImage;
606 } else {
607 ALOGE("Frame boundary: backend is not vulkan");
608 }
609 } else {
610 ALOGE("Frame boundary: invalid backend render target");
611 }
612 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
613 // it won't wait on it.
614 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
615 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500616 } else {
617 destroy_semaphore(mDestroySemaphoreContext);
618 mDestroySemaphoreContext = nullptr;
619 }
620 }
621 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000622
623 return submissionTime;
Greg Danielbe2803a2021-02-19 18:32:16 -0500624}
625
626void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
627 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
628 ATRACE_NAME("Finishing GPU work");
629 mDeviceWaitIdle(mDevice);
630 }
631
632 int fenceFd = -1;
633 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500634 VkSemaphoreGetFdInfoKHR getFdInfo;
635 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
636 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500637 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500638 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500639
Greg Danielbe2803a2021-02-19 18:32:16 -0500640 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500641 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
642 } else {
643 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
644 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500645 }
Greg Daniele8dc3972021-07-15 13:38:44 -0400646 if (mDestroySemaphoreContext) {
647 destroy_semaphore(mDestroySemaphoreContext);
648 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500649
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500650 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500651 mSwapSemaphore = VK_NULL_HANDLE;
652 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500653}
654
655void VulkanManager::destroySurface(VulkanSurface* surface) {
656 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700657 if (VK_NULL_HANDLE != mGraphicsQueue) {
658 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500659 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500660
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500661 delete surface;
662}
663
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400664VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
665 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800666 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400667 SkColorType surfaceColorType,
668 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700669 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500670 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500671 if (!window) {
672 return nullptr;
673 }
674
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500675 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700676 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500677}
678
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400679status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400680 if (!hasVkContext()) {
681 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
682 return INVALID_OPERATION;
683 }
684
Stan Iliev7a081272018-10-26 17:54:18 -0400685 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400686 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400687 if (fenceFd == -1) {
688 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
689 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000690 }
Stan Iliev7a081272018-10-26 17:54:18 -0400691
692 VkSemaphoreCreateInfo semaphoreInfo;
693 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
694 semaphoreInfo.pNext = nullptr;
695 semaphoreInfo.flags = 0;
696 VkSemaphore semaphore;
697 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
698 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400699 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400700 ALOGE("Failed to create import semaphore, err: %d", err);
701 return UNKNOWN_ERROR;
702 }
703 VkImportSemaphoreFdInfoKHR importInfo;
704 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
705 importInfo.pNext = nullptr;
706 importInfo.semaphore = semaphore;
707 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
708 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
709 importInfo.fd = fenceFd;
710
711 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
712 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400713 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400714 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400715 ALOGE("Failed to import semaphore, err: %d", err);
716 return UNKNOWN_ERROR;
717 }
718
Greg Danield92a9b12019-04-23 10:11:04 -0400719 GrBackendSemaphore beSemaphore;
720 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400721
Greg Danield6670772021-06-09 12:01:12 -0400722 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
723 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400724 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400725 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400726
Stan Iliev564ca3e2018-09-04 22:00:00 +0000727 return OK;
728}
729
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400730status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400731 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400732 if (!hasVkContext()) {
733 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
734 return INVALID_OPERATION;
735 }
736
Greg Daniel26e0dca2018-09-18 10:33:19 -0400737 VkExportSemaphoreCreateInfo exportInfo;
738 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
739 exportInfo.pNext = nullptr;
740 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
741
742 VkSemaphoreCreateInfo semaphoreInfo;
743 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
744 semaphoreInfo.pNext = &exportInfo;
745 semaphoreInfo.flags = 0;
746 VkSemaphore semaphore;
747 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
748 if (VK_SUCCESS != err) {
749 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
750 return INVALID_OPERATION;
751 }
752
Greg Danield92a9b12019-04-23 10:11:04 -0400753 GrBackendSemaphore backendSemaphore;
754 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400755
Stan Ilievaaa9e832019-09-17 14:07:23 -0400756 DestroySemaphoreInfo* destroyInfo =
757 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400758 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
759 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
760 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400761 GrFlushInfo flushInfo;
762 flushInfo.fNumSemaphores = 1;
763 flushInfo.fSignalSemaphores = &backendSemaphore;
764 flushInfo.fFinishedProc = destroy_semaphore;
765 flushInfo.fFinishedContext = destroyInfo;
766 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
767 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400768
Greg Danield92a9b12019-04-23 10:11:04 -0400769 if (submitted == GrSemaphoresSubmitted::kNo) {
770 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400771 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400772 return INVALID_OPERATION;
773 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400774
775 VkSemaphoreGetFdInfoKHR getFdInfo;
776 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
777 getFdInfo.pNext = nullptr;
778 getFdInfo.semaphore = semaphore;
779 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
780
781 int fenceFd = 0;
782
783 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400784 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400785 if (VK_SUCCESS != err) {
786 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
787 return INVALID_OPERATION;
788 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400789 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400790
Stan Iliev564ca3e2018-09-04 22:00:00 +0000791 return OK;
792}
793
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500794} /* namespace renderthread */
795} /* namespace uirenderer */
796} /* namespace android */