blob: 07146e845714f494b71ff429a7771cf7dc18eb94 [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>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000026#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040027#include <vk/GrVkExtensions.h>
28#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050029
Alec Mouriaa3e4982020-12-14 14:47:57 -080030#include <cstring>
31
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"
John Reck322b8ab2019-03-14 13:15:28 -070036#include "utils/TraceUtils.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038namespace android {
39namespace uirenderer {
40namespace renderthread {
41
Bo Liu7b8c1eb2019-01-08 20:17:55 -080042static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
43 // All Vulkan structs that could be part of the features chain will start with the
44 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
45 // so we can get access to the pNext for the next struct.
46 struct CommonVulkanHeader {
47 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070048 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080049 };
50
51 void* pNext = features.pNext;
52 while (pNext) {
53 void* current = pNext;
54 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
55 free(current);
56 }
57}
58
Alec Mouriaa3e4982020-12-14 14:47:57 -080059GrVkGetProc VulkanManager::sSkiaGetProp = [](const char* proc_name, VkInstance instance,
60 VkDevice device) {
61 if (device != VK_NULL_HANDLE) {
62 if (strcmp("vkQueueSubmit", proc_name) == 0) {
63 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueSubmit;
64 } else if (strcmp("vkQueueWaitIdle", proc_name) == 0) {
65 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueWaitIdle;
66 }
67 return vkGetDeviceProcAddr(device, proc_name);
68 }
69 return vkGetInstanceProcAddr(instance, proc_name);
70};
71
Greg Daniel2ff202712018-06-14 11:50:10 -040072#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
73#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
74#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050075
Derek Sollenberger802fefa2020-08-13 16:53:30 -040076sp<VulkanManager> VulkanManager::getInstance() {
77 // cache a weakptr to the context to enable a second thread to share the same vulkan state
78 static wp<VulkanManager> sWeakInstance = nullptr;
79 static std::mutex sLock;
80
81 std::lock_guard _lock{sLock};
82 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
83 if (!vulkanManager.get()) {
84 vulkanManager = new VulkanManager();
85 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050086 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050087
Derek Sollenberger802fefa2020-08-13 16:53:30 -040088 return vulkanManager;
89}
90
91VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -040092 if (mDevice != VK_NULL_HANDLE) {
93 mDeviceWaitIdle(mDevice);
94 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070095 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096
Greg Daniel2ff202712018-06-14 11:50:10 -040097 if (mInstance != VK_NULL_HANDLE) {
98 mDestroyInstance(mInstance, nullptr);
99 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100
Greg Daniel2ff202712018-06-14 11:50:10 -0400101 mGraphicsQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400102 mDevice = VK_NULL_HANDLE;
103 mPhysicalDevice = VK_NULL_HANDLE;
104 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800105 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800106 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800107 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800108 mDeviceExtensions.clear();
109 free_features_extensions_structs(mPhysicalDeviceFeatures2);
110 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400111}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500112
Stan Iliev90276c82019-02-03 18:01:02 -0500113void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400114 VkResult err;
115
116 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700117 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
118 nullptr, // pNext
119 "android framework", // pApplicationName
120 0, // applicationVersion
121 "android framework", // pEngineName
122 0, // engineVerison
123 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400124 };
125
Greg Daniel2ff202712018-06-14 11:50:10 -0400126 {
127 GET_PROC(EnumerateInstanceExtensionProperties);
128
129 uint32_t extensionCount = 0;
130 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500131 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800132 mInstanceExtensionsOwner.resize(extensionCount);
133 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
134 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500135 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400136 bool hasKHRSurfaceExtension = false;
137 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800138 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
139 mInstanceExtensions.push_back(extension.extensionName);
140 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400141 hasKHRSurfaceExtension = true;
142 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800143 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400144 hasKHRAndroidSurfaceExtension = true;
145 }
146 }
Stan Iliev90276c82019-02-03 18:01:02 -0500147 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400148 }
149
150 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700151 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
152 nullptr, // pNext
153 0, // flags
154 &app_info, // pApplicationInfo
155 0, // enabledLayerNameCount
156 nullptr, // ppEnabledLayerNames
157 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
158 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400159 };
160
161 GET_PROC(CreateInstance);
162 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500163 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400164
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700165 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400166 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700167 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400168 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400169 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500170 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700171 GET_INST_PROC(GetPhysicalDeviceProperties);
172 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400173
174 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500175 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
176 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400177 // Just returning the first physical device instead of getting the whole array. Since there
178 // should only be one device on android.
179 gpuCount = 1;
180 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
181 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500182 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400183
Greg Daniel96259622018-10-01 14:42:56 -0400184 VkPhysicalDeviceProperties physDeviceProperties;
185 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500186 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400187 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400188
Greg Daniel2ff202712018-06-14 11:50:10 -0400189 // query to get the initial queue props size
190 uint32_t queueCount;
191 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500192 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400193
194 // now get the actual queue props
195 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
196 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
197
198 // iterate to find the graphics queue
199 mGraphicsQueueIndex = queueCount;
200 for (uint32_t i = 0; i < queueCount; i++) {
201 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
202 mGraphicsQueueIndex = i;
203 break;
204 }
205 }
Stan Iliev90276c82019-02-03 18:01:02 -0500206 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400207
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 {
209 uint32_t extensionCount = 0;
210 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700211 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500212 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800213 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400214 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700215 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500216 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400217 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800218 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
219 mDeviceExtensions.push_back(extension.extensionName);
220 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400221 hasKHRSwapchainExtension = true;
222 }
223 }
Stan Iliev90276c82019-02-03 18:01:02 -0500224 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400225 }
226
Alec Mouriaa3e4982020-12-14 14:47:57 -0800227 grExtensions.init(sSkiaGetProp, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700228 mInstanceExtensions.data(), mDeviceExtensions.size(),
229 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400230
Stan Iliev90276c82019-02-03 18:01:02 -0500231 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400232
Greg Daniela227dbb2018-08-20 09:19:48 -0400233 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
234 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
235 features.pNext = nullptr;
236
237 // Setup all extension feature structs we may want to use.
238 void** tailPNext = &features.pNext;
239
240 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
241 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700242 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400243 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
244 LOG_ALWAYS_FATAL_IF(!blend);
245 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
246 blend->pNext = nullptr;
247 *tailPNext = blend;
248 tailPNext = &blend->pNext;
249 }
250
Greg Daniel05036172018-11-28 17:08:04 -0500251 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700252 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500253 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
254 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
255 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
256 ycbcrFeature->pNext = nullptr;
257 *tailPNext = ycbcrFeature;
258 tailPNext = &ycbcrFeature->pNext;
259
Greg Daniela227dbb2018-08-20 09:19:48 -0400260 // query to get the physical device features
261 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400262 // this looks like it would slow things down,
263 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400264 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400265
John Reck0fa0cbc2019-04-05 16:57:46 -0700266 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400267
Stan Iliev7e733362019-02-28 13:16:36 -0500268 void* queueNextPtr = nullptr;
269
270 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
271
John Reck0fa0cbc2019-04-05 16:57:46 -0700272 if (Properties::contextPriority != 0 &&
273 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500274 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
275 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700276 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500277 queuePriorityCreateInfo.pNext = nullptr;
278 switch (Properties::contextPriority) {
279 case EGL_CONTEXT_PRIORITY_LOW_IMG:
280 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
281 break;
282 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
283 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
284 break;
285 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
286 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
287 break;
288 default:
289 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700290 }
291 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500292 }
293
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700294 const VkDeviceQueueCreateInfo queueInfo = {
295 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
296 queueNextPtr, // pNext
297 0, // VkDeviceQueueCreateFlags
298 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouriaa3e4982020-12-14 14:47:57 -0800299 1, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700300 queuePriorities, // pQueuePriorities
301 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400302
303 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700304 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
305 &features, // pNext
306 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700307 1, // queueCreateInfoCount
308 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700309 0, // layerCount
310 nullptr, // ppEnabledLayerNames
311 (uint32_t)mDeviceExtensions.size(), // extensionCount
312 mDeviceExtensions.data(), // ppEnabledExtensionNames
313 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400314 };
315
Stan Iliev90276c82019-02-03 18:01:02 -0500316 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400317
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500318 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500319 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500320 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700321 GET_DEV_PROC(CreateCommandPool);
322 GET_DEV_PROC(CreateFence);
323 GET_DEV_PROC(CreateSemaphore);
324 GET_DEV_PROC(DestroyCommandPool);
325 GET_DEV_PROC(DestroyDevice);
326 GET_DEV_PROC(DestroyFence);
327 GET_DEV_PROC(DestroySemaphore);
328 GET_DEV_PROC(DeviceWaitIdle);
329 GET_DEV_PROC(EndCommandBuffer);
330 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500331 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700332 GET_DEV_PROC(GetSemaphoreFdKHR);
333 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500334 GET_DEV_PROC(QueueSubmit);
335 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700336 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500337 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700338 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000339 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400340}
341
342void VulkanManager::initialize() {
Greg Daniel1d9de712021-05-14 09:28:34 -0400343 std::lock_guard _lock{mInitializeLock};
344
Greg Daniel2ff202712018-06-14 11:50:10 -0400345 if (mDevice != VK_NULL_HANDLE) {
346 return;
347 }
348
Greg Daniela227dbb2018-08-20 09:19:48 -0400349 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500350 uint32_t instanceVersion;
351 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
352 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400353
Stan Iliev981afe72019-02-13 14:24:33 -0500354 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400355
356 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
357
Greg Danielcd558522016-11-17 13:31:40 -0500358 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
359 mSwapBehavior = SwapBehavior::BufferAge;
360 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500361}
362
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400363sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
364 ContextType contextType) {
Stan Iliev981afe72019-02-13 14:24:33 -0500365
366 GrVkBackendContext backendContext;
367 backendContext.fInstance = mInstance;
368 backendContext.fPhysicalDevice = mPhysicalDevice;
369 backendContext.fDevice = mDevice;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800370 backendContext.fQueue = mGraphicsQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500371 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
372 backendContext.fMaxAPIVersion = mAPIVersion;
373 backendContext.fVkExtensions = &mExtensions;
374 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800375 backendContext.fGetProc = sSkiaGetProp;
Stan Iliev981afe72019-02-13 14:24:33 -0500376
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400377 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500378}
379
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800380VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
381 return VkFunctorInitParams{
382 .instance = mInstance,
383 .physical_device = mPhysicalDevice,
384 .device = mDevice,
385 .queue = mGraphicsQueue,
386 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500387 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800388 .enabled_instance_extension_names = mInstanceExtensions.data(),
389 .enabled_instance_extension_names_length =
390 static_cast<uint32_t>(mInstanceExtensions.size()),
391 .enabled_device_extension_names = mDeviceExtensions.data(),
392 .enabled_device_extension_names_length =
393 static_cast<uint32_t>(mDeviceExtensions.size()),
394 .device_features_2 = &mPhysicalDeviceFeatures2,
395 };
396}
397
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500398Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500399 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
400
401 if (bufferInfo == nullptr) {
402 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
403 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500404 }
405
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500406 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500407
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500408 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400409 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
410 bool isSignalPending = false;
411 if (finfo != NULL) {
412 isSignalPending = finfo->status != 1;
413 sync_file_info_free(finfo);
414 }
415 if (isSignalPending) {
416 int fence_clone = dup(bufferInfo->dequeue_fence);
417 if (fence_clone == -1) {
418 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
419 errno);
420 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
421 } else {
422 VkSemaphoreCreateInfo semaphoreInfo;
423 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
424 semaphoreInfo.pNext = nullptr;
425 semaphoreInfo.flags = 0;
426 VkSemaphore semaphore;
427 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
428 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
429 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500430
Stan Iliev197843d2019-03-21 11:34:15 -0400431 VkImportSemaphoreFdInfoKHR importInfo;
432 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
433 importInfo.pNext = nullptr;
434 importInfo.semaphore = semaphore;
435 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
436 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
437 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500438
Stan Iliev197843d2019-03-21 11:34:15 -0400439 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
440 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500441
Stan Iliev197843d2019-03-21 11:34:15 -0400442 GrBackendSemaphore backendSemaphore;
443 backendSemaphore.initVulkan(semaphore);
444 bufferInfo->skSurface->wait(1, &backendSemaphore);
445 // The following flush blocks the GPU immediately instead of waiting for other
446 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700447 // TODO: remove the flush after finding why backendSemaphore is not working.
Greg Danielc7ad4082020-05-14 15:38:26 -0400448 bufferInfo->skSurface->flushAndSubmit();
Stan Iliev197843d2019-03-21 11:34:15 -0400449 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500450 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500451 }
452
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500453 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
454 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500455}
456
Greg Danield92a9b12019-04-23 10:11:04 -0400457struct DestroySemaphoreInfo {
458 PFN_vkDestroySemaphore mDestroyFunction;
459 VkDevice mDevice;
460 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400461 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
462 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
463 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
464 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
465 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
466 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
467 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400468
469 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400470 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400471 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
472};
473
474static void destroy_semaphore(void* context) {
475 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400476 --info->mRefs;
477 if (!info->mRefs) {
478 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
479 delete info;
480 }
Greg Danield92a9b12019-04-23 10:11:04 -0400481}
482
Greg Danielbe2803a2021-02-19 18:32:16 -0500483void VulkanManager::finishFrame(SkSurface* surface) {
484 ATRACE_NAME("Vulkan finish frame");
485 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
486 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400487
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500488 VkExportSemaphoreCreateInfo exportInfo;
489 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
490 exportInfo.pNext = nullptr;
491 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500492
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500493 VkSemaphoreCreateInfo semaphoreInfo;
494 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
495 semaphoreInfo.pNext = &exportInfo;
496 semaphoreInfo.flags = 0;
497 VkSemaphore semaphore;
498 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500499 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500500
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500501 GrBackendSemaphore backendSemaphore;
502 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500503
Greg Danielc7ad4082020-05-14 15:38:26 -0400504 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500505 if (err == VK_SUCCESS) {
506 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
507 flushInfo.fNumSemaphores = 1;
508 flushInfo.fSignalSemaphores = &backendSemaphore;
509 flushInfo.fFinishedProc = destroy_semaphore;
510 flushInfo.fFinishedContext = mDestroySemaphoreContext;
511 } else {
512 semaphore = VK_NULL_HANDLE;
513 }
514 GrSemaphoresSubmitted submitted =
515 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
516 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400517 ALOGE_IF(!context, "Surface is not backed by gpu");
518 context->submit();
Greg Danielbe2803a2021-02-19 18:32:16 -0500519 if (semaphore != VK_NULL_HANDLE) {
520 if (submitted == GrSemaphoresSubmitted::kYes) {
521 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000522 if (mFrameBoundaryANDROID) {
523 // retrieve VkImage used as render target
524 VkImage image = VK_NULL_HANDLE;
525 GrBackendRenderTarget backendRenderTarget =
526 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
527 if (backendRenderTarget.isValid()) {
528 GrVkImageInfo info;
529 if (backendRenderTarget.getVkImageInfo(&info)) {
530 image = info.fImage;
531 } else {
532 ALOGE("Frame boundary: backend is not vulkan");
533 }
534 } else {
535 ALOGE("Frame boundary: invalid backend render target");
536 }
537 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
538 // it won't wait on it.
539 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
540 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500541 } else {
542 destroy_semaphore(mDestroySemaphoreContext);
543 mDestroySemaphoreContext = nullptr;
544 }
545 }
546 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
547}
548
549void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
550 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
551 ATRACE_NAME("Finishing GPU work");
552 mDeviceWaitIdle(mDevice);
553 }
554
555 int fenceFd = -1;
556 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500557 VkSemaphoreGetFdInfoKHR getFdInfo;
558 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
559 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500560 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500561 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500562
Greg Danielbe2803a2021-02-19 18:32:16 -0500563 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500564 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
565 } else {
566 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800567
568 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500569 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500570 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500571 destroy_semaphore(mDestroySemaphoreContext);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500572
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500573 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500574 mSwapSemaphore = VK_NULL_HANDLE;
575 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500576}
577
578void VulkanManager::destroySurface(VulkanSurface* surface) {
579 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700580 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800581 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700582 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500583 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400584 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500585
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500586 delete surface;
587}
588
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400589VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
590 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800591 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400592 SkColorType surfaceColorType,
593 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700594 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500595 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500596 if (!window) {
597 return nullptr;
598 }
599
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500600 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700601 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500602}
603
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400604status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400605 if (!hasVkContext()) {
606 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
607 return INVALID_OPERATION;
608 }
609
Stan Iliev7a081272018-10-26 17:54:18 -0400610 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400611 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400612 if (fenceFd == -1) {
613 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
614 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000615 }
Stan Iliev7a081272018-10-26 17:54:18 -0400616
617 VkSemaphoreCreateInfo semaphoreInfo;
618 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
619 semaphoreInfo.pNext = nullptr;
620 semaphoreInfo.flags = 0;
621 VkSemaphore semaphore;
622 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
623 if (VK_SUCCESS != err) {
624 ALOGE("Failed to create import semaphore, err: %d", err);
625 return UNKNOWN_ERROR;
626 }
627 VkImportSemaphoreFdInfoKHR importInfo;
628 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
629 importInfo.pNext = nullptr;
630 importInfo.semaphore = semaphore;
631 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
632 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
633 importInfo.fd = fenceFd;
634
635 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
636 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400637 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400638 ALOGE("Failed to import semaphore, err: %d", err);
639 return UNKNOWN_ERROR;
640 }
641
Greg Danield92a9b12019-04-23 10:11:04 -0400642 GrBackendSemaphore beSemaphore;
643 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400644
Greg Danield92a9b12019-04-23 10:11:04 -0400645 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
646 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400647 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400648
Stan Iliev564ca3e2018-09-04 22:00:00 +0000649 return OK;
650}
651
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400652status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400653 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400654 if (!hasVkContext()) {
655 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
656 return INVALID_OPERATION;
657 }
658
Greg Daniel26e0dca2018-09-18 10:33:19 -0400659 VkExportSemaphoreCreateInfo exportInfo;
660 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
661 exportInfo.pNext = nullptr;
662 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
663
664 VkSemaphoreCreateInfo semaphoreInfo;
665 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
666 semaphoreInfo.pNext = &exportInfo;
667 semaphoreInfo.flags = 0;
668 VkSemaphore semaphore;
669 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
670 if (VK_SUCCESS != err) {
671 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
672 return INVALID_OPERATION;
673 }
674
Greg Danield92a9b12019-04-23 10:11:04 -0400675 GrBackendSemaphore backendSemaphore;
676 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400677
Stan Ilievaaa9e832019-09-17 14:07:23 -0400678 DestroySemaphoreInfo* destroyInfo =
679 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400680 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
681 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
682 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400683 GrFlushInfo flushInfo;
684 flushInfo.fNumSemaphores = 1;
685 flushInfo.fSignalSemaphores = &backendSemaphore;
686 flushInfo.fFinishedProc = destroy_semaphore;
687 flushInfo.fFinishedContext = destroyInfo;
688 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
689 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400690
Greg Danield92a9b12019-04-23 10:11:04 -0400691 if (submitted == GrSemaphoresSubmitted::kNo) {
692 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400693 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400694 return INVALID_OPERATION;
695 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400696
697 VkSemaphoreGetFdInfoKHR getFdInfo;
698 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
699 getFdInfo.pNext = nullptr;
700 getFdInfo.semaphore = semaphore;
701 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
702
703 int fenceFd = 0;
704
705 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400706 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400707 if (VK_SUCCESS != err) {
708 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
709 return INVALID_OPERATION;
710 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400711 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400712
Stan Iliev564ca3e2018-09-04 22:00:00 +0000713 return OK;
714}
715
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500716} /* namespace renderthread */
717} /* namespace uirenderer */
718} /* namespace android */