blob: e2b541aa5ecb2544b26eaae724ca12d581d4637b [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
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037namespace android {
38namespace uirenderer {
39namespace renderthread {
40
Mattias Simonssone545f962023-07-17 08:03:14 +000041static std::array<std::string_view, 20> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040042 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
43 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
44 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
45 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
46 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
47 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
48 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
49 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
50 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
51 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
52 VK_KHR_SURFACE_EXTENSION_NAME,
53 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
54 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040055 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040056 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
57 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
58 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
59 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
60 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Mattias Simonssone545f962023-07-17 08:03:14 +000061 VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040062};
63
64static bool shouldEnableExtension(const std::string_view& extension) {
65 for (const auto& it : sEnableExtensions) {
66 if (it == extension) {
67 return true;
68 }
69 }
70 return false;
71}
72
Bo Liu7b8c1eb2019-01-08 20:17:55 -080073static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
74 // All Vulkan structs that could be part of the features chain will start with the
75 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
76 // so we can get access to the pNext for the next struct.
77 struct CommonVulkanHeader {
78 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070079 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080080 };
81
82 void* pNext = features.pNext;
83 while (pNext) {
84 void* current = pNext;
85 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
86 free(current);
87 }
88}
89
Greg Daniel2ff202712018-06-14 11:50:10 -040090#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
91#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
92#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050093
John Reck9fc3d272023-05-01 16:33:22 -040094// cache a weakptr to the context to enable a second thread to share the same vulkan state
95static wp<VulkanManager> sWeakInstance = nullptr;
96static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -040097
John Reck9fc3d272023-05-01 16:33:22 -040098sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -040099 std::lock_guard _lock{sLock};
100 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
101 if (!vulkanManager.get()) {
102 vulkanManager = new VulkanManager();
103 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500104 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500105
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400106 return vulkanManager;
107}
108
John Reck9fc3d272023-05-01 16:33:22 -0400109sp<VulkanManager> VulkanManager::peekInstance() {
110 std::lock_guard _lock{sLock};
111 return sWeakInstance.promote();
112}
113
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400114VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400115 if (mDevice != VK_NULL_HANDLE) {
116 mDeviceWaitIdle(mDevice);
117 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700118 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500119
Greg Daniel2ff202712018-06-14 11:50:10 -0400120 if (mInstance != VK_NULL_HANDLE) {
121 mDestroyInstance(mInstance, nullptr);
122 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500123
Greg Daniel2ff202712018-06-14 11:50:10 -0400124 mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000125 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400126 mDevice = VK_NULL_HANDLE;
127 mPhysicalDevice = VK_NULL_HANDLE;
128 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800129 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800130 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800131 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800132 mDeviceExtensions.clear();
133 free_features_extensions_structs(mPhysicalDeviceFeatures2);
134 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400135}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136
Stan Iliev90276c82019-02-03 18:01:02 -0500137void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400138 VkResult err;
139
140 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700141 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
142 nullptr, // pNext
143 "android framework", // pApplicationName
144 0, // applicationVersion
145 "android framework", // pEngineName
146 0, // engineVerison
147 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400148 };
149
Greg Daniel2ff202712018-06-14 11:50:10 -0400150 {
151 GET_PROC(EnumerateInstanceExtensionProperties);
152
153 uint32_t extensionCount = 0;
154 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500155 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800156 mInstanceExtensionsOwner.resize(extensionCount);
157 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
158 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500159 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400160 bool hasKHRSurfaceExtension = false;
161 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800162 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400163 if (!shouldEnableExtension(extension.extensionName)) {
164 ALOGV("Not enabling instance extension %s", extension.extensionName);
165 continue;
166 }
167 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800168 mInstanceExtensions.push_back(extension.extensionName);
169 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400170 hasKHRSurfaceExtension = true;
171 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800172 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400173 hasKHRAndroidSurfaceExtension = true;
174 }
175 }
Stan Iliev90276c82019-02-03 18:01:02 -0500176 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400177 }
178
179 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700180 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
181 nullptr, // pNext
182 0, // flags
183 &app_info, // pApplicationInfo
184 0, // enabledLayerNameCount
185 nullptr, // ppEnabledLayerNames
186 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
187 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400188 };
189
190 GET_PROC(CreateInstance);
191 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500192 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400193
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700194 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400195 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700196 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400197 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400198 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500199 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700200 GET_INST_PROC(GetPhysicalDeviceProperties);
201 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400202
203 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500204 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
205 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400206 // Just returning the first physical device instead of getting the whole array. Since there
207 // should only be one device on android.
208 gpuCount = 1;
209 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
210 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500211 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400212
Greg Daniel96259622018-10-01 14:42:56 -0400213 VkPhysicalDeviceProperties physDeviceProperties;
214 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500215 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400216 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400217
Greg Daniel2ff202712018-06-14 11:50:10 -0400218 // query to get the initial queue props size
Alec Mouri219997a2023-05-23 17:25:19 +0000219 uint32_t queueCount = 0;
Greg Daniel2ff202712018-06-14 11:50:10 -0400220 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500221 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400222
223 // now get the actual queue props
224 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
225 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
226
Alec Mouri219997a2023-05-23 17:25:19 +0000227 constexpr auto kRequestedQueueCount = 2;
228
Greg Daniel2ff202712018-06-14 11:50:10 -0400229 // iterate to find the graphics queue
230 mGraphicsQueueIndex = queueCount;
231 for (uint32_t i = 0; i < queueCount; i++) {
232 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
233 mGraphicsQueueIndex = i;
Alec Mouri219997a2023-05-23 17:25:19 +0000234 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400235 break;
236 }
237 }
Stan Iliev90276c82019-02-03 18:01:02 -0500238 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400239
Greg Daniel2ff202712018-06-14 11:50:10 -0400240 {
241 uint32_t extensionCount = 0;
242 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700243 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500244 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800245 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400246 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700247 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500248 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400249 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800250 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400251 if (!shouldEnableExtension(extension.extensionName)) {
252 ALOGV("Not enabling device extension %s", extension.extensionName);
253 continue;
254 }
255 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800256 mDeviceExtensions.push_back(extension.extensionName);
257 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400258 hasKHRSwapchainExtension = true;
259 }
260 }
Stan Iliev90276c82019-02-03 18:01:02 -0500261 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400262 }
263
Alec Mouri219997a2023-05-23 17:25:19 +0000264 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
265 if (device != VK_NULL_HANDLE) {
266 return vkGetDeviceProcAddr(device, proc_name);
267 }
268 return vkGetInstanceProcAddr(instance, proc_name);
269 };
270
271 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700272 mInstanceExtensions.data(), mDeviceExtensions.size(),
273 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400274
Stan Iliev90276c82019-02-03 18:01:02 -0500275 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400276
Greg Daniela227dbb2018-08-20 09:19:48 -0400277 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
278 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
279 features.pNext = nullptr;
280
281 // Setup all extension feature structs we may want to use.
282 void** tailPNext = &features.pNext;
283
284 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
285 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700286 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400287 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
288 LOG_ALWAYS_FATAL_IF(!blend);
289 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
290 blend->pNext = nullptr;
291 *tailPNext = blend;
292 tailPNext = &blend->pNext;
293 }
294
Greg Daniel05036172018-11-28 17:08:04 -0500295 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700296 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500297 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
298 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
299 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
300 ycbcrFeature->pNext = nullptr;
301 *tailPNext = ycbcrFeature;
302 tailPNext = &ycbcrFeature->pNext;
303
Greg Daniela227dbb2018-08-20 09:19:48 -0400304 // query to get the physical device features
305 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400306 // this looks like it would slow things down,
307 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400308 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400309
Alec Mouri219997a2023-05-23 17:25:19 +0000310 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400311
Stan Iliev7e733362019-02-28 13:16:36 -0500312 void* queueNextPtr = nullptr;
313
314 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
315
John Reck0fa0cbc2019-04-05 16:57:46 -0700316 if (Properties::contextPriority != 0 &&
317 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500318 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
319 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700320 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500321 queuePriorityCreateInfo.pNext = nullptr;
322 switch (Properties::contextPriority) {
323 case EGL_CONTEXT_PRIORITY_LOW_IMG:
324 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
325 break;
326 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
327 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
328 break;
329 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
330 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
331 break;
332 default:
333 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700334 }
335 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500336 }
337
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700338 const VkDeviceQueueCreateInfo queueInfo = {
339 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
340 queueNextPtr, // pNext
341 0, // VkDeviceQueueCreateFlags
342 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000343 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700344 queuePriorities, // pQueuePriorities
345 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400346
347 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700348 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
349 &features, // pNext
350 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700351 1, // queueCreateInfoCount
352 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700353 0, // layerCount
354 nullptr, // ppEnabledLayerNames
355 (uint32_t)mDeviceExtensions.size(), // extensionCount
356 mDeviceExtensions.data(), // ppEnabledExtensionNames
357 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400358 };
359
Stan Iliev90276c82019-02-03 18:01:02 -0500360 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400361
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500362 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500363 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500364 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700365 GET_DEV_PROC(CreateCommandPool);
366 GET_DEV_PROC(CreateFence);
367 GET_DEV_PROC(CreateSemaphore);
368 GET_DEV_PROC(DestroyCommandPool);
369 GET_DEV_PROC(DestroyDevice);
370 GET_DEV_PROC(DestroyFence);
371 GET_DEV_PROC(DestroySemaphore);
372 GET_DEV_PROC(DeviceWaitIdle);
373 GET_DEV_PROC(EndCommandBuffer);
374 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500375 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700376 GET_DEV_PROC(GetSemaphoreFdKHR);
377 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500378 GET_DEV_PROC(QueueSubmit);
379 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700380 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500381 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700382 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000383 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400384}
385
386void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000387 std::call_once(mInitFlag, [&] {
388 GET_PROC(EnumerateInstanceVersion);
389 uint32_t instanceVersion;
390 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
391 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400392
Alec Mouri671a9f62023-08-25 17:18:02 +0000393 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400394
Alec Mouri671a9f62023-08-25 17:18:02 +0000395 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
396 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400397
Alec Mouri671a9f62023-08-25 17:18:02 +0000398 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
399 mSwapBehavior = SwapBehavior::BufferAge;
400 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400401
Alec Mouri671a9f62023-08-25 17:18:02 +0000402 mInitialized = true;
403 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500404}
405
John Reck9fc3d272023-05-01 16:33:22 -0400406static void onGrContextReleased(void* context) {
407 VulkanManager* manager = (VulkanManager*)context;
408 manager->decStrong((void*)onGrContextReleased);
409}
Stan Iliev981afe72019-02-13 14:24:33 -0500410
John Reck9fc3d272023-05-01 16:33:22 -0400411sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
412 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000413 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
414 if (device != VK_NULL_HANDLE) {
415 return vkGetDeviceProcAddr(device, proc_name);
416 }
417 return vkGetInstanceProcAddr(instance, proc_name);
418 };
419
Stan Iliev981afe72019-02-13 14:24:33 -0500420 GrVkBackendContext backendContext;
421 backendContext.fInstance = mInstance;
422 backendContext.fPhysicalDevice = mPhysicalDevice;
423 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000424 backendContext.fQueue =
425 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500426 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
427 backendContext.fMaxAPIVersion = mAPIVersion;
428 backendContext.fVkExtensions = &mExtensions;
429 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000430 backendContext.fGetProc = std::move(getProc);
Stan Iliev981afe72019-02-13 14:24:33 -0500431
John Reck9fc3d272023-05-01 16:33:22 -0400432 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
433 this->incStrong((void*)onGrContextReleased);
434 options.fContextDeleteContext = this;
435 options.fContextDeleteProc = onGrContextReleased;
436
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400437 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500438}
439
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800440VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
441 return VkFunctorInitParams{
442 .instance = mInstance,
443 .physical_device = mPhysicalDevice,
444 .device = mDevice,
445 .queue = mGraphicsQueue,
446 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500447 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800448 .enabled_instance_extension_names = mInstanceExtensions.data(),
449 .enabled_instance_extension_names_length =
450 static_cast<uint32_t>(mInstanceExtensions.size()),
451 .enabled_device_extension_names = mDeviceExtensions.data(),
452 .enabled_device_extension_names_length =
453 static_cast<uint32_t>(mDeviceExtensions.size()),
454 .device_features_2 = &mPhysicalDeviceFeatures2,
455 };
456}
457
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500458Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500459 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
460
461 if (bufferInfo == nullptr) {
462 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
463 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500464 }
465
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500466 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500467
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500468 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400469 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
470 bool isSignalPending = false;
471 if (finfo != NULL) {
472 isSignalPending = finfo->status != 1;
473 sync_file_info_free(finfo);
474 }
475 if (isSignalPending) {
476 int fence_clone = dup(bufferInfo->dequeue_fence);
477 if (fence_clone == -1) {
478 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
479 errno);
480 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
481 } else {
482 VkSemaphoreCreateInfo semaphoreInfo;
483 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
484 semaphoreInfo.pNext = nullptr;
485 semaphoreInfo.flags = 0;
486 VkSemaphore semaphore;
487 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400488 if (err != VK_SUCCESS) {
489 ALOGE("Failed to create import semaphore, err: %d", err);
490 close(fence_clone);
491 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
492 } else {
493 VkImportSemaphoreFdInfoKHR importInfo;
494 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
495 importInfo.pNext = nullptr;
496 importInfo.semaphore = semaphore;
497 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
498 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
499 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500500
Greg Danield6670772021-06-09 12:01:12 -0400501 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
502 if (err != VK_SUCCESS) {
503 ALOGE("Failed to import semaphore, err: %d", err);
504 mDestroySemaphore(mDevice, semaphore, nullptr);
505 close(fence_clone);
506 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
507 } else {
508 GrBackendSemaphore backendSemaphore;
509 backendSemaphore.initVulkan(semaphore);
510 // Skia will take ownership of the VkSemaphore and delete it once the wait
511 // has finished. The VkSemaphore also owns the imported fd, so it will
512 // close the fd when it is deleted.
513 bufferInfo->skSurface->wait(1, &backendSemaphore);
514 // The following flush blocks the GPU immediately instead of waiting for
515 // other drawing ops. It seems dequeue_fence is not respected otherwise.
516 // TODO: remove the flush after finding why backendSemaphore is not working.
Kevin Lubickaa592d02023-05-30 15:07:06 +0000517 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface);
Greg Danield6670772021-06-09 12:01:12 -0400518 }
519 }
Stan Iliev197843d2019-03-21 11:34:15 -0400520 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500521 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500522 }
523
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500524 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
525 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500526}
527
Greg Danield92a9b12019-04-23 10:11:04 -0400528struct DestroySemaphoreInfo {
529 PFN_vkDestroySemaphore mDestroyFunction;
530 VkDevice mDevice;
531 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400532 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
533 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
534 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
535 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
536 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
537 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
538 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400539
540 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400541 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400542 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
543};
544
545static void destroy_semaphore(void* context) {
546 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400547 --info->mRefs;
548 if (!info->mRefs) {
549 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
550 delete info;
551 }
Greg Danield92a9b12019-04-23 10:11:04 -0400552}
553
Alec Mouri3afb3972022-05-27 22:03:11 +0000554nsecs_t VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500555 ATRACE_NAME("Vulkan finish frame");
556 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
557 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400558
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500559 VkExportSemaphoreCreateInfo exportInfo;
560 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
561 exportInfo.pNext = nullptr;
562 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500563
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500564 VkSemaphoreCreateInfo semaphoreInfo;
565 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
566 semaphoreInfo.pNext = &exportInfo;
567 semaphoreInfo.flags = 0;
568 VkSemaphore semaphore;
569 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500570 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500571
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500572 GrBackendSemaphore backendSemaphore;
573 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500574
Greg Danielc7ad4082020-05-14 15:38:26 -0400575 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500576 if (err == VK_SUCCESS) {
577 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
578 flushInfo.fNumSemaphores = 1;
579 flushInfo.fSignalSemaphores = &backendSemaphore;
580 flushInfo.fFinishedProc = destroy_semaphore;
581 flushInfo.fFinishedContext = mDestroySemaphoreContext;
582 } else {
583 semaphore = VK_NULL_HANDLE;
584 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500585 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400586 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000587 GrSemaphoresSubmitted submitted = context->flush(
588 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400589 context->submit();
Alec Mouri3afb3972022-05-27 22:03:11 +0000590 const nsecs_t submissionTime = systemTime();
Greg Danielbe2803a2021-02-19 18:32:16 -0500591 if (semaphore != VK_NULL_HANDLE) {
592 if (submitted == GrSemaphoresSubmitted::kYes) {
593 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000594 if (mFrameBoundaryANDROID) {
595 // retrieve VkImage used as render target
596 VkImage image = VK_NULL_HANDLE;
597 GrBackendRenderTarget backendRenderTarget =
Kevin Lubick098ed4c2023-05-08 12:03:59 +0000598 SkSurfaces::GetBackendRenderTarget(
599 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000600 if (backendRenderTarget.isValid()) {
601 GrVkImageInfo info;
602 if (backendRenderTarget.getVkImageInfo(&info)) {
603 image = info.fImage;
604 } else {
605 ALOGE("Frame boundary: backend is not vulkan");
606 }
607 } else {
608 ALOGE("Frame boundary: invalid backend render target");
609 }
610 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
611 // it won't wait on it.
612 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
613 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500614 } else {
615 destroy_semaphore(mDestroySemaphoreContext);
616 mDestroySemaphoreContext = nullptr;
617 }
618 }
619 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000620
621 return submissionTime;
Greg Danielbe2803a2021-02-19 18:32:16 -0500622}
623
624void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
625 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
626 ATRACE_NAME("Finishing GPU work");
627 mDeviceWaitIdle(mDevice);
628 }
629
630 int fenceFd = -1;
631 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500632 VkSemaphoreGetFdInfoKHR getFdInfo;
633 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
634 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500635 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500636 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500637
Greg Danielbe2803a2021-02-19 18:32:16 -0500638 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500639 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
640 } else {
641 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
642 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500643 }
Greg Daniele8dc3972021-07-15 13:38:44 -0400644 if (mDestroySemaphoreContext) {
645 destroy_semaphore(mDestroySemaphoreContext);
646 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500647
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500648 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500649 mSwapSemaphore = VK_NULL_HANDLE;
650 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500651}
652
653void VulkanManager::destroySurface(VulkanSurface* surface) {
654 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700655 if (VK_NULL_HANDLE != mGraphicsQueue) {
656 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500657 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500658
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500659 delete surface;
660}
661
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400662VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
663 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800664 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400665 SkColorType surfaceColorType,
666 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700667 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500668 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500669 if (!window) {
670 return nullptr;
671 }
672
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500673 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700674 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500675}
676
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400677status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400678 if (!hasVkContext()) {
679 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
680 return INVALID_OPERATION;
681 }
682
Stan Iliev7a081272018-10-26 17:54:18 -0400683 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400684 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400685 if (fenceFd == -1) {
686 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
687 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000688 }
Stan Iliev7a081272018-10-26 17:54:18 -0400689
690 VkSemaphoreCreateInfo semaphoreInfo;
691 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
692 semaphoreInfo.pNext = nullptr;
693 semaphoreInfo.flags = 0;
694 VkSemaphore semaphore;
695 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
696 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400697 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400698 ALOGE("Failed to create import semaphore, err: %d", err);
699 return UNKNOWN_ERROR;
700 }
701 VkImportSemaphoreFdInfoKHR importInfo;
702 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
703 importInfo.pNext = nullptr;
704 importInfo.semaphore = semaphore;
705 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
706 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
707 importInfo.fd = fenceFd;
708
709 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
710 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400711 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400712 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400713 ALOGE("Failed to import semaphore, err: %d", err);
714 return UNKNOWN_ERROR;
715 }
716
Greg Danield92a9b12019-04-23 10:11:04 -0400717 GrBackendSemaphore beSemaphore;
718 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400719
Greg Danield6670772021-06-09 12:01:12 -0400720 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
721 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400722 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400723 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400724
Stan Iliev564ca3e2018-09-04 22:00:00 +0000725 return OK;
726}
727
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400728status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400729 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400730 if (!hasVkContext()) {
731 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
732 return INVALID_OPERATION;
733 }
734
Greg Daniel26e0dca2018-09-18 10:33:19 -0400735 VkExportSemaphoreCreateInfo exportInfo;
736 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
737 exportInfo.pNext = nullptr;
738 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
739
740 VkSemaphoreCreateInfo semaphoreInfo;
741 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
742 semaphoreInfo.pNext = &exportInfo;
743 semaphoreInfo.flags = 0;
744 VkSemaphore semaphore;
745 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
746 if (VK_SUCCESS != err) {
747 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
748 return INVALID_OPERATION;
749 }
750
Greg Danield92a9b12019-04-23 10:11:04 -0400751 GrBackendSemaphore backendSemaphore;
752 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400753
Stan Ilievaaa9e832019-09-17 14:07:23 -0400754 DestroySemaphoreInfo* destroyInfo =
755 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400756 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
757 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
758 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400759 GrFlushInfo flushInfo;
760 flushInfo.fNumSemaphores = 1;
761 flushInfo.fSignalSemaphores = &backendSemaphore;
762 flushInfo.fFinishedProc = destroy_semaphore;
763 flushInfo.fFinishedContext = destroyInfo;
764 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
765 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400766
Greg Danield92a9b12019-04-23 10:11:04 -0400767 if (submitted == GrSemaphoresSubmitted::kNo) {
768 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400769 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400770 return INVALID_OPERATION;
771 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400772
773 VkSemaphoreGetFdInfoKHR getFdInfo;
774 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
775 getFdInfo.pNext = nullptr;
776 getFdInfo.semaphore = semaphore;
777 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
778
779 int fenceFd = 0;
780
781 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400782 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400783 if (VK_SUCCESS != err) {
784 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
785 return INVALID_OPERATION;
786 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400787 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400788
Stan Iliev564ca3e2018-09-04 22:00:00 +0000789 return OK;
790}
791
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500792} /* namespace renderthread */
793} /* namespace uirenderer */
794} /* namespace android */