blob: f7014911111665b2521f8fbc7d4b0efc727f8f73 [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
rnleece9762b2021-05-21 15:40:53 -070032#include <gui/TraceUtils.h>
Greg Danielcd558522016-11-17 13:31:40 -050033#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050035#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050036#include "renderstate/RenderState.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);
Greg Danield6670772021-06-09 12:01:12 -0400428 if (err != VK_SUCCESS) {
429 ALOGE("Failed to create import semaphore, err: %d", err);
430 close(fence_clone);
431 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
432 } else {
433 VkImportSemaphoreFdInfoKHR importInfo;
434 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
435 importInfo.pNext = nullptr;
436 importInfo.semaphore = semaphore;
437 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
438 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
439 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500440
Greg Danield6670772021-06-09 12:01:12 -0400441 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
442 if (err != VK_SUCCESS) {
443 ALOGE("Failed to import semaphore, err: %d", err);
444 mDestroySemaphore(mDevice, semaphore, nullptr);
445 close(fence_clone);
446 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
447 } else {
448 GrBackendSemaphore backendSemaphore;
449 backendSemaphore.initVulkan(semaphore);
450 // Skia will take ownership of the VkSemaphore and delete it once the wait
451 // has finished. The VkSemaphore also owns the imported fd, so it will
452 // close the fd when it is deleted.
453 bufferInfo->skSurface->wait(1, &backendSemaphore);
454 // The following flush blocks the GPU immediately instead of waiting for
455 // other drawing ops. It seems dequeue_fence is not respected otherwise.
456 // TODO: remove the flush after finding why backendSemaphore is not working.
457 bufferInfo->skSurface->flushAndSubmit();
458 }
459 }
Stan Iliev197843d2019-03-21 11:34:15 -0400460 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500461 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500462 }
463
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500464 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
465 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500466}
467
Greg Danield92a9b12019-04-23 10:11:04 -0400468struct DestroySemaphoreInfo {
469 PFN_vkDestroySemaphore mDestroyFunction;
470 VkDevice mDevice;
471 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400472 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
473 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
474 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
475 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
476 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
477 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
478 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400479
480 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400481 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400482 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
483};
484
485static void destroy_semaphore(void* context) {
486 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400487 --info->mRefs;
488 if (!info->mRefs) {
489 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
490 delete info;
491 }
Greg Danield92a9b12019-04-23 10:11:04 -0400492}
493
Greg Danielbe2803a2021-02-19 18:32:16 -0500494void VulkanManager::finishFrame(SkSurface* surface) {
495 ATRACE_NAME("Vulkan finish frame");
496 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
497 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400498
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500499 VkExportSemaphoreCreateInfo exportInfo;
500 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
501 exportInfo.pNext = nullptr;
502 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500503
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500504 VkSemaphoreCreateInfo semaphoreInfo;
505 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
506 semaphoreInfo.pNext = &exportInfo;
507 semaphoreInfo.flags = 0;
508 VkSemaphore semaphore;
509 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500510 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500511
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500512 GrBackendSemaphore backendSemaphore;
513 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500514
Greg Danielc7ad4082020-05-14 15:38:26 -0400515 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500516 if (err == VK_SUCCESS) {
517 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
518 flushInfo.fNumSemaphores = 1;
519 flushInfo.fSignalSemaphores = &backendSemaphore;
520 flushInfo.fFinishedProc = destroy_semaphore;
521 flushInfo.fFinishedContext = mDestroySemaphoreContext;
522 } else {
523 semaphore = VK_NULL_HANDLE;
524 }
525 GrSemaphoresSubmitted submitted =
526 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
527 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400528 ALOGE_IF(!context, "Surface is not backed by gpu");
529 context->submit();
Greg Danielbe2803a2021-02-19 18:32:16 -0500530 if (semaphore != VK_NULL_HANDLE) {
531 if (submitted == GrSemaphoresSubmitted::kYes) {
532 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000533 if (mFrameBoundaryANDROID) {
534 // retrieve VkImage used as render target
535 VkImage image = VK_NULL_HANDLE;
536 GrBackendRenderTarget backendRenderTarget =
537 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
538 if (backendRenderTarget.isValid()) {
539 GrVkImageInfo info;
540 if (backendRenderTarget.getVkImageInfo(&info)) {
541 image = info.fImage;
542 } else {
543 ALOGE("Frame boundary: backend is not vulkan");
544 }
545 } else {
546 ALOGE("Frame boundary: invalid backend render target");
547 }
548 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
549 // it won't wait on it.
550 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
551 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500552 } else {
553 destroy_semaphore(mDestroySemaphoreContext);
554 mDestroySemaphoreContext = nullptr;
555 }
556 }
557 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
558}
559
560void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
561 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
562 ATRACE_NAME("Finishing GPU work");
563 mDeviceWaitIdle(mDevice);
564 }
565
566 int fenceFd = -1;
567 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500568 VkSemaphoreGetFdInfoKHR getFdInfo;
569 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
570 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500571 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500572 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500573
Greg Danielbe2803a2021-02-19 18:32:16 -0500574 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500575 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
576 } else {
577 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800578
579 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500580 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500581 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500582 destroy_semaphore(mDestroySemaphoreContext);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500583
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500584 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500585 mSwapSemaphore = VK_NULL_HANDLE;
586 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500587}
588
589void VulkanManager::destroySurface(VulkanSurface* surface) {
590 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700591 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800592 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700593 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500594 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400595 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500596
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500597 delete surface;
598}
599
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400600VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
601 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800602 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400603 SkColorType surfaceColorType,
604 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700605 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500606 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500607 if (!window) {
608 return nullptr;
609 }
610
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500611 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700612 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500613}
614
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400615status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400616 if (!hasVkContext()) {
617 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
618 return INVALID_OPERATION;
619 }
620
Stan Iliev7a081272018-10-26 17:54:18 -0400621 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400622 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400623 if (fenceFd == -1) {
624 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
625 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000626 }
Stan Iliev7a081272018-10-26 17:54:18 -0400627
628 VkSemaphoreCreateInfo semaphoreInfo;
629 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
630 semaphoreInfo.pNext = nullptr;
631 semaphoreInfo.flags = 0;
632 VkSemaphore semaphore;
633 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
634 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400635 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400636 ALOGE("Failed to create import semaphore, err: %d", err);
637 return UNKNOWN_ERROR;
638 }
639 VkImportSemaphoreFdInfoKHR importInfo;
640 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
641 importInfo.pNext = nullptr;
642 importInfo.semaphore = semaphore;
643 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
644 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
645 importInfo.fd = fenceFd;
646
647 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
648 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400649 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400650 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400651 ALOGE("Failed to import semaphore, err: %d", err);
652 return UNKNOWN_ERROR;
653 }
654
Greg Danield92a9b12019-04-23 10:11:04 -0400655 GrBackendSemaphore beSemaphore;
656 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400657
Greg Danield6670772021-06-09 12:01:12 -0400658 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
659 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400660 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400661 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400662
Stan Iliev564ca3e2018-09-04 22:00:00 +0000663 return OK;
664}
665
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400666status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400667 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400668 if (!hasVkContext()) {
669 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
670 return INVALID_OPERATION;
671 }
672
Greg Daniel26e0dca2018-09-18 10:33:19 -0400673 VkExportSemaphoreCreateInfo exportInfo;
674 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
675 exportInfo.pNext = nullptr;
676 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
677
678 VkSemaphoreCreateInfo semaphoreInfo;
679 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
680 semaphoreInfo.pNext = &exportInfo;
681 semaphoreInfo.flags = 0;
682 VkSemaphore semaphore;
683 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
684 if (VK_SUCCESS != err) {
685 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
686 return INVALID_OPERATION;
687 }
688
Greg Danield92a9b12019-04-23 10:11:04 -0400689 GrBackendSemaphore backendSemaphore;
690 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400691
Stan Ilievaaa9e832019-09-17 14:07:23 -0400692 DestroySemaphoreInfo* destroyInfo =
693 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400694 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
695 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
696 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400697 GrFlushInfo flushInfo;
698 flushInfo.fNumSemaphores = 1;
699 flushInfo.fSignalSemaphores = &backendSemaphore;
700 flushInfo.fFinishedProc = destroy_semaphore;
701 flushInfo.fFinishedContext = destroyInfo;
702 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
703 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400704
Greg Danield92a9b12019-04-23 10:11:04 -0400705 if (submitted == GrSemaphoresSubmitted::kNo) {
706 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400707 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400708 return INVALID_OPERATION;
709 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400710
711 VkSemaphoreGetFdInfoKHR getFdInfo;
712 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
713 getFdInfo.pNext = nullptr;
714 getFdInfo.semaphore = semaphore;
715 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
716
717 int fenceFd = 0;
718
719 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400720 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400721 if (VK_SUCCESS != err) {
722 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
723 return INVALID_OPERATION;
724 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400725 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400726
Stan Iliev564ca3e2018-09-04 22:00:00 +0000727 return OK;
728}
729
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500730} /* namespace renderthread */
731} /* namespace uirenderer */
732} /* namespace android */