blob: 01126860b3ba5b116fc44936c8a4d287ad1079dc [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() {
343 if (mDevice != VK_NULL_HANDLE) {
344 return;
345 }
346
Greg Daniela227dbb2018-08-20 09:19:48 -0400347 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500348 uint32_t instanceVersion;
349 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
350 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400351
Stan Iliev981afe72019-02-13 14:24:33 -0500352 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400353
354 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
355
Greg Danielcd558522016-11-17 13:31:40 -0500356 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
357 mSwapBehavior = SwapBehavior::BufferAge;
358 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500359}
360
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400361sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
362 ContextType contextType) {
Stan Iliev981afe72019-02-13 14:24:33 -0500363
364 GrVkBackendContext backendContext;
365 backendContext.fInstance = mInstance;
366 backendContext.fPhysicalDevice = mPhysicalDevice;
367 backendContext.fDevice = mDevice;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800368 backendContext.fQueue = mGraphicsQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500369 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
370 backendContext.fMaxAPIVersion = mAPIVersion;
371 backendContext.fVkExtensions = &mExtensions;
372 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800373 backendContext.fGetProc = sSkiaGetProp;
Stan Iliev981afe72019-02-13 14:24:33 -0500374
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400375 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500376}
377
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800378VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
379 return VkFunctorInitParams{
380 .instance = mInstance,
381 .physical_device = mPhysicalDevice,
382 .device = mDevice,
383 .queue = mGraphicsQueue,
384 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500385 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800386 .enabled_instance_extension_names = mInstanceExtensions.data(),
387 .enabled_instance_extension_names_length =
388 static_cast<uint32_t>(mInstanceExtensions.size()),
389 .enabled_device_extension_names = mDeviceExtensions.data(),
390 .enabled_device_extension_names_length =
391 static_cast<uint32_t>(mDeviceExtensions.size()),
392 .device_features_2 = &mPhysicalDeviceFeatures2,
393 };
394}
395
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500396Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500397 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
398
399 if (bufferInfo == nullptr) {
400 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
401 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500402 }
403
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500404 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500405
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500406 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400407 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
408 bool isSignalPending = false;
409 if (finfo != NULL) {
410 isSignalPending = finfo->status != 1;
411 sync_file_info_free(finfo);
412 }
413 if (isSignalPending) {
414 int fence_clone = dup(bufferInfo->dequeue_fence);
415 if (fence_clone == -1) {
416 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
417 errno);
418 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
419 } else {
420 VkSemaphoreCreateInfo semaphoreInfo;
421 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
422 semaphoreInfo.pNext = nullptr;
423 semaphoreInfo.flags = 0;
424 VkSemaphore semaphore;
425 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
426 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
427 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500428
Stan Iliev197843d2019-03-21 11:34:15 -0400429 VkImportSemaphoreFdInfoKHR importInfo;
430 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
431 importInfo.pNext = nullptr;
432 importInfo.semaphore = semaphore;
433 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
434 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
435 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500436
Stan Iliev197843d2019-03-21 11:34:15 -0400437 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
438 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500439
Stan Iliev197843d2019-03-21 11:34:15 -0400440 GrBackendSemaphore backendSemaphore;
441 backendSemaphore.initVulkan(semaphore);
442 bufferInfo->skSurface->wait(1, &backendSemaphore);
443 // The following flush blocks the GPU immediately instead of waiting for other
444 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700445 // TODO: remove the flush after finding why backendSemaphore is not working.
Greg Danielc7ad4082020-05-14 15:38:26 -0400446 bufferInfo->skSurface->flushAndSubmit();
Stan Iliev197843d2019-03-21 11:34:15 -0400447 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500448 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500449 }
450
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500451 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
452 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500453}
454
Greg Danield92a9b12019-04-23 10:11:04 -0400455struct DestroySemaphoreInfo {
456 PFN_vkDestroySemaphore mDestroyFunction;
457 VkDevice mDevice;
458 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400459 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
460 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
461 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
462 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
463 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
464 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
465 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400466
467 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400468 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400469 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
470};
471
472static void destroy_semaphore(void* context) {
473 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400474 --info->mRefs;
475 if (!info->mRefs) {
476 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
477 delete info;
478 }
Greg Danield92a9b12019-04-23 10:11:04 -0400479}
480
Greg Danielbe2803a2021-02-19 18:32:16 -0500481void VulkanManager::finishFrame(SkSurface* surface) {
482 ATRACE_NAME("Vulkan finish frame");
483 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
484 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400485
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500486 VkExportSemaphoreCreateInfo exportInfo;
487 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
488 exportInfo.pNext = nullptr;
489 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500490
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500491 VkSemaphoreCreateInfo semaphoreInfo;
492 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
493 semaphoreInfo.pNext = &exportInfo;
494 semaphoreInfo.flags = 0;
495 VkSemaphore semaphore;
496 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500497 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500498
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500499 GrBackendSemaphore backendSemaphore;
500 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500501
Greg Danielc7ad4082020-05-14 15:38:26 -0400502 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500503 if (err == VK_SUCCESS) {
504 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
505 flushInfo.fNumSemaphores = 1;
506 flushInfo.fSignalSemaphores = &backendSemaphore;
507 flushInfo.fFinishedProc = destroy_semaphore;
508 flushInfo.fFinishedContext = mDestroySemaphoreContext;
509 } else {
510 semaphore = VK_NULL_HANDLE;
511 }
512 GrSemaphoresSubmitted submitted =
513 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
514 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400515 ALOGE_IF(!context, "Surface is not backed by gpu");
516 context->submit();
Greg Danielbe2803a2021-02-19 18:32:16 -0500517 if (semaphore != VK_NULL_HANDLE) {
518 if (submitted == GrSemaphoresSubmitted::kYes) {
519 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000520 if (mFrameBoundaryANDROID) {
521 // retrieve VkImage used as render target
522 VkImage image = VK_NULL_HANDLE;
523 GrBackendRenderTarget backendRenderTarget =
524 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
525 if (backendRenderTarget.isValid()) {
526 GrVkImageInfo info;
527 if (backendRenderTarget.getVkImageInfo(&info)) {
528 image = info.fImage;
529 } else {
530 ALOGE("Frame boundary: backend is not vulkan");
531 }
532 } else {
533 ALOGE("Frame boundary: invalid backend render target");
534 }
535 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
536 // it won't wait on it.
537 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
538 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500539 } else {
540 destroy_semaphore(mDestroySemaphoreContext);
541 mDestroySemaphoreContext = nullptr;
542 }
543 }
544 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
545}
546
547void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
548 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
549 ATRACE_NAME("Finishing GPU work");
550 mDeviceWaitIdle(mDevice);
551 }
552
553 int fenceFd = -1;
554 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500555 VkSemaphoreGetFdInfoKHR getFdInfo;
556 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
557 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500558 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500559 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500560
Greg Danielbe2803a2021-02-19 18:32:16 -0500561 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500562 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
563 } else {
564 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800565
566 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500567 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500568 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500569 destroy_semaphore(mDestroySemaphoreContext);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500570
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500571 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500572 mSwapSemaphore = VK_NULL_HANDLE;
573 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500574}
575
576void VulkanManager::destroySurface(VulkanSurface* surface) {
577 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700578 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800579 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700580 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500581 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400582 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500583
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500584 delete surface;
585}
586
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400587VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
588 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800589 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400590 SkColorType surfaceColorType,
591 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700592 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500593 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500594 if (!window) {
595 return nullptr;
596 }
597
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500598 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700599 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500600}
601
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400602status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400603 if (!hasVkContext()) {
604 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
605 return INVALID_OPERATION;
606 }
607
Stan Iliev7a081272018-10-26 17:54:18 -0400608 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400609 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400610 if (fenceFd == -1) {
611 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
612 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000613 }
Stan Iliev7a081272018-10-26 17:54:18 -0400614
615 VkSemaphoreCreateInfo semaphoreInfo;
616 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
617 semaphoreInfo.pNext = nullptr;
618 semaphoreInfo.flags = 0;
619 VkSemaphore semaphore;
620 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
621 if (VK_SUCCESS != err) {
622 ALOGE("Failed to create import semaphore, err: %d", err);
623 return UNKNOWN_ERROR;
624 }
625 VkImportSemaphoreFdInfoKHR importInfo;
626 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
627 importInfo.pNext = nullptr;
628 importInfo.semaphore = semaphore;
629 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
630 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
631 importInfo.fd = fenceFd;
632
633 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
634 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400635 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400636 ALOGE("Failed to import semaphore, err: %d", err);
637 return UNKNOWN_ERROR;
638 }
639
Greg Danield92a9b12019-04-23 10:11:04 -0400640 GrBackendSemaphore beSemaphore;
641 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400642
Greg Danield92a9b12019-04-23 10:11:04 -0400643 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
644 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400645 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400646
Stan Iliev564ca3e2018-09-04 22:00:00 +0000647 return OK;
648}
649
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400650status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400651 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400652 if (!hasVkContext()) {
653 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
654 return INVALID_OPERATION;
655 }
656
Greg Daniel26e0dca2018-09-18 10:33:19 -0400657 VkExportSemaphoreCreateInfo exportInfo;
658 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
659 exportInfo.pNext = nullptr;
660 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
661
662 VkSemaphoreCreateInfo semaphoreInfo;
663 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
664 semaphoreInfo.pNext = &exportInfo;
665 semaphoreInfo.flags = 0;
666 VkSemaphore semaphore;
667 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
668 if (VK_SUCCESS != err) {
669 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
670 return INVALID_OPERATION;
671 }
672
Greg Danield92a9b12019-04-23 10:11:04 -0400673 GrBackendSemaphore backendSemaphore;
674 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400675
Stan Ilievaaa9e832019-09-17 14:07:23 -0400676 DestroySemaphoreInfo* destroyInfo =
677 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400678 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
679 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
680 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400681 GrFlushInfo flushInfo;
682 flushInfo.fNumSemaphores = 1;
683 flushInfo.fSignalSemaphores = &backendSemaphore;
684 flushInfo.fFinishedProc = destroy_semaphore;
685 flushInfo.fFinishedContext = destroyInfo;
686 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
687 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400688
Greg Danield92a9b12019-04-23 10:11:04 -0400689 if (submitted == GrSemaphoresSubmitted::kNo) {
690 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400691 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400692 return INVALID_OPERATION;
693 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400694
695 VkSemaphoreGetFdInfoKHR getFdInfo;
696 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
697 getFdInfo.pNext = nullptr;
698 getFdInfo.semaphore = semaphore;
699 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
700
701 int fenceFd = 0;
702
703 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400704 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400705 if (VK_SUCCESS != err) {
706 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
707 return INVALID_OPERATION;
708 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400709 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400710
Stan Iliev564ca3e2018-09-04 22:00:00 +0000711 return OK;
712}
713
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500714} /* namespace renderthread */
715} /* namespace uirenderer */
716} /* namespace android */