blob: 3e7ce368f55da999c759b18794f4f80caf720920 [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 Daniel45ec62b2017-01-04 14:27:00 -050034#include "renderstate/RenderState.h"
John Reck322b8ab2019-03-14 13:15:28 -070035#include "utils/TraceUtils.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050036
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037namespace android {
38namespace uirenderer {
39namespace renderthread {
40
Bo Liu7b8c1eb2019-01-08 20:17:55 -080041static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
42 // All Vulkan structs that could be part of the features chain will start with the
43 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
44 // so we can get access to the pNext for the next struct.
45 struct CommonVulkanHeader {
46 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070047 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080048 };
49
50 void* pNext = features.pNext;
51 while (pNext) {
52 void* current = pNext;
53 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
54 free(current);
55 }
56}
57
Alec Mouriaa3e4982020-12-14 14:47:57 -080058GrVkGetProc VulkanManager::sSkiaGetProp = [](const char* proc_name, VkInstance instance,
59 VkDevice device) {
60 if (device != VK_NULL_HANDLE) {
61 if (strcmp("vkQueueSubmit", proc_name) == 0) {
62 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueSubmit;
63 } else if (strcmp("vkQueueWaitIdle", proc_name) == 0) {
64 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueWaitIdle;
65 }
66 return vkGetDeviceProcAddr(device, proc_name);
67 }
68 return vkGetInstanceProcAddr(instance, proc_name);
69};
70
Greg Daniel2ff202712018-06-14 11:50:10 -040071#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
72#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
73#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074
Derek Sollenberger802fefa2020-08-13 16:53:30 -040075sp<VulkanManager> VulkanManager::getInstance() {
76 // cache a weakptr to the context to enable a second thread to share the same vulkan state
77 static wp<VulkanManager> sWeakInstance = nullptr;
78 static std::mutex sLock;
79
80 std::lock_guard _lock{sLock};
81 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
82 if (!vulkanManager.get()) {
83 vulkanManager = new VulkanManager();
84 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050085 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050086
Derek Sollenberger802fefa2020-08-13 16:53:30 -040087 return vulkanManager;
88}
89
90VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -040091 if (mDevice != VK_NULL_HANDLE) {
92 mDeviceWaitIdle(mDevice);
93 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070094 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050095
Greg Daniel2ff202712018-06-14 11:50:10 -040096 if (mInstance != VK_NULL_HANDLE) {
97 mDestroyInstance(mInstance, nullptr);
98 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050099
Greg Daniel2ff202712018-06-14 11:50:10 -0400100 mGraphicsQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400101 mDevice = VK_NULL_HANDLE;
102 mPhysicalDevice = VK_NULL_HANDLE;
103 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800104 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800105 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800106 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800107 mDeviceExtensions.clear();
108 free_features_extensions_structs(mPhysicalDeviceFeatures2);
109 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400110}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500111
Stan Iliev90276c82019-02-03 18:01:02 -0500112void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400113 VkResult err;
114
115 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700116 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
117 nullptr, // pNext
118 "android framework", // pApplicationName
119 0, // applicationVersion
120 "android framework", // pEngineName
121 0, // engineVerison
122 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400123 };
124
Greg Daniel2ff202712018-06-14 11:50:10 -0400125 {
126 GET_PROC(EnumerateInstanceExtensionProperties);
127
128 uint32_t extensionCount = 0;
129 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500130 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800131 mInstanceExtensionsOwner.resize(extensionCount);
132 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
133 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500134 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400135 bool hasKHRSurfaceExtension = false;
136 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800137 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
138 mInstanceExtensions.push_back(extension.extensionName);
139 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400140 hasKHRSurfaceExtension = true;
141 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800142 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400143 hasKHRAndroidSurfaceExtension = true;
144 }
145 }
Stan Iliev90276c82019-02-03 18:01:02 -0500146 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400147 }
148
149 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700150 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
151 nullptr, // pNext
152 0, // flags
153 &app_info, // pApplicationInfo
154 0, // enabledLayerNameCount
155 nullptr, // ppEnabledLayerNames
156 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
157 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400158 };
159
160 GET_PROC(CreateInstance);
161 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500162 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400163
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700164 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400165 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700166 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400167 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400168 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500169 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700170 GET_INST_PROC(GetPhysicalDeviceProperties);
171 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400172
173 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500174 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
175 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400176 // Just returning the first physical device instead of getting the whole array. Since there
177 // should only be one device on android.
178 gpuCount = 1;
179 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
180 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500181 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400182
Greg Daniel96259622018-10-01 14:42:56 -0400183 VkPhysicalDeviceProperties physDeviceProperties;
184 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500185 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400186 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400187
Greg Daniel2ff202712018-06-14 11:50:10 -0400188 // query to get the initial queue props size
189 uint32_t queueCount;
190 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500191 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400192
193 // now get the actual queue props
194 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
195 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
196
197 // iterate to find the graphics queue
198 mGraphicsQueueIndex = queueCount;
199 for (uint32_t i = 0; i < queueCount; i++) {
200 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
201 mGraphicsQueueIndex = i;
202 break;
203 }
204 }
Stan Iliev90276c82019-02-03 18:01:02 -0500205 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400206
Greg Daniel2ff202712018-06-14 11:50:10 -0400207 {
208 uint32_t extensionCount = 0;
209 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700210 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500211 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800212 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400213 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700214 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500215 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400216 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800217 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
218 mDeviceExtensions.push_back(extension.extensionName);
219 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400220 hasKHRSwapchainExtension = true;
221 }
222 }
Stan Iliev90276c82019-02-03 18:01:02 -0500223 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400224 }
225
Alec Mouriaa3e4982020-12-14 14:47:57 -0800226 grExtensions.init(sSkiaGetProp, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700227 mInstanceExtensions.data(), mDeviceExtensions.size(),
228 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400229
Stan Iliev90276c82019-02-03 18:01:02 -0500230 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400231
Greg Daniela227dbb2018-08-20 09:19:48 -0400232 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
233 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
234 features.pNext = nullptr;
235
236 // Setup all extension feature structs we may want to use.
237 void** tailPNext = &features.pNext;
238
239 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
240 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700241 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400242 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
243 LOG_ALWAYS_FATAL_IF(!blend);
244 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
245 blend->pNext = nullptr;
246 *tailPNext = blend;
247 tailPNext = &blend->pNext;
248 }
249
Greg Daniel05036172018-11-28 17:08:04 -0500250 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700251 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500252 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
253 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
254 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
255 ycbcrFeature->pNext = nullptr;
256 *tailPNext = ycbcrFeature;
257 tailPNext = &ycbcrFeature->pNext;
258
Greg Daniela227dbb2018-08-20 09:19:48 -0400259 // query to get the physical device features
260 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400261 // this looks like it would slow things down,
262 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400263 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400264
John Reck0fa0cbc2019-04-05 16:57:46 -0700265 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400266
Stan Iliev7e733362019-02-28 13:16:36 -0500267 void* queueNextPtr = nullptr;
268
269 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
270
John Reck0fa0cbc2019-04-05 16:57:46 -0700271 if (Properties::contextPriority != 0 &&
272 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500273 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
274 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700275 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500276 queuePriorityCreateInfo.pNext = nullptr;
277 switch (Properties::contextPriority) {
278 case EGL_CONTEXT_PRIORITY_LOW_IMG:
279 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
280 break;
281 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
282 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
283 break;
284 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
285 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
286 break;
287 default:
288 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700289 }
290 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500291 }
292
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700293 const VkDeviceQueueCreateInfo queueInfo = {
294 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
295 queueNextPtr, // pNext
296 0, // VkDeviceQueueCreateFlags
297 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouriaa3e4982020-12-14 14:47:57 -0800298 1, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700299 queuePriorities, // pQueuePriorities
300 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400301
302 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700303 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
304 &features, // pNext
305 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700306 1, // queueCreateInfoCount
307 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700308 0, // layerCount
309 nullptr, // ppEnabledLayerNames
310 (uint32_t)mDeviceExtensions.size(), // extensionCount
311 mDeviceExtensions.data(), // ppEnabledExtensionNames
312 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400313 };
314
Stan Iliev90276c82019-02-03 18:01:02 -0500315 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400316
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500317 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500318 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500319 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700320 GET_DEV_PROC(CreateCommandPool);
321 GET_DEV_PROC(CreateFence);
322 GET_DEV_PROC(CreateSemaphore);
323 GET_DEV_PROC(DestroyCommandPool);
324 GET_DEV_PROC(DestroyDevice);
325 GET_DEV_PROC(DestroyFence);
326 GET_DEV_PROC(DestroySemaphore);
327 GET_DEV_PROC(DeviceWaitIdle);
328 GET_DEV_PROC(EndCommandBuffer);
329 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500330 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700331 GET_DEV_PROC(GetSemaphoreFdKHR);
332 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500333 GET_DEV_PROC(QueueSubmit);
334 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700335 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500336 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700337 GET_DEV_PROC(WaitForFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400338}
339
340void VulkanManager::initialize() {
341 if (mDevice != VK_NULL_HANDLE) {
342 return;
343 }
344
Greg Daniela227dbb2018-08-20 09:19:48 -0400345 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500346 uint32_t instanceVersion;
347 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
348 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400349
Stan Iliev981afe72019-02-13 14:24:33 -0500350 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400351
352 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
353
Greg Danielcd558522016-11-17 13:31:40 -0500354 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
355 mSwapBehavior = SwapBehavior::BufferAge;
356 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500357}
358
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400359sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
360 ContextType contextType) {
Stan Iliev981afe72019-02-13 14:24:33 -0500361
362 GrVkBackendContext backendContext;
363 backendContext.fInstance = mInstance;
364 backendContext.fPhysicalDevice = mPhysicalDevice;
365 backendContext.fDevice = mDevice;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800366 backendContext.fQueue = mGraphicsQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500367 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
368 backendContext.fMaxAPIVersion = mAPIVersion;
369 backendContext.fVkExtensions = &mExtensions;
370 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800371 backendContext.fGetProc = sSkiaGetProp;
Stan Iliev981afe72019-02-13 14:24:33 -0500372
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400373 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500374}
375
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800376VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
377 return VkFunctorInitParams{
378 .instance = mInstance,
379 .physical_device = mPhysicalDevice,
380 .device = mDevice,
381 .queue = mGraphicsQueue,
382 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500383 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800384 .enabled_instance_extension_names = mInstanceExtensions.data(),
385 .enabled_instance_extension_names_length =
386 static_cast<uint32_t>(mInstanceExtensions.size()),
387 .enabled_device_extension_names = mDeviceExtensions.data(),
388 .enabled_device_extension_names_length =
389 static_cast<uint32_t>(mDeviceExtensions.size()),
390 .device_features_2 = &mPhysicalDeviceFeatures2,
391 };
392}
393
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500394Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500395 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
396
397 if (bufferInfo == nullptr) {
398 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
399 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500400 }
401
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500402 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500403
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500404 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400405 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
406 bool isSignalPending = false;
407 if (finfo != NULL) {
408 isSignalPending = finfo->status != 1;
409 sync_file_info_free(finfo);
410 }
411 if (isSignalPending) {
412 int fence_clone = dup(bufferInfo->dequeue_fence);
413 if (fence_clone == -1) {
414 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
415 errno);
416 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
417 } else {
418 VkSemaphoreCreateInfo semaphoreInfo;
419 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
420 semaphoreInfo.pNext = nullptr;
421 semaphoreInfo.flags = 0;
422 VkSemaphore semaphore;
423 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
424 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
425 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500426
Stan Iliev197843d2019-03-21 11:34:15 -0400427 VkImportSemaphoreFdInfoKHR importInfo;
428 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
429 importInfo.pNext = nullptr;
430 importInfo.semaphore = semaphore;
431 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
432 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
433 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500434
Stan Iliev197843d2019-03-21 11:34:15 -0400435 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
436 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500437
Stan Iliev197843d2019-03-21 11:34:15 -0400438 GrBackendSemaphore backendSemaphore;
439 backendSemaphore.initVulkan(semaphore);
440 bufferInfo->skSurface->wait(1, &backendSemaphore);
441 // The following flush blocks the GPU immediately instead of waiting for other
442 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700443 // TODO: remove the flush after finding why backendSemaphore is not working.
Greg Danielc7ad4082020-05-14 15:38:26 -0400444 bufferInfo->skSurface->flushAndSubmit();
Stan Iliev197843d2019-03-21 11:34:15 -0400445 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500446 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500447 }
448
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500449 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
450 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500451}
452
Greg Danield92a9b12019-04-23 10:11:04 -0400453struct DestroySemaphoreInfo {
454 PFN_vkDestroySemaphore mDestroyFunction;
455 VkDevice mDevice;
456 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400457 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
458 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
459 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
460 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
461 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
462 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
463 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400464
465 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400466 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400467 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
468};
469
470static void destroy_semaphore(void* context) {
471 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400472 --info->mRefs;
473 if (!info->mRefs) {
474 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
475 delete info;
476 }
Greg Danield92a9b12019-04-23 10:11:04 -0400477}
478
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500479void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
480 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
481 ATRACE_NAME("Finishing GPU work");
482 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500483 }
484
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400485 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
486 if (!bufferInfo) {
487 // If VulkanSurface::dequeueNativeBuffer failed earlier, then swapBuffers is a no-op.
488 return;
489 }
490
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500491 VkExportSemaphoreCreateInfo exportInfo;
492 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
493 exportInfo.pNext = nullptr;
494 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500495
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500496 VkSemaphoreCreateInfo semaphoreInfo;
497 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
498 semaphoreInfo.pNext = &exportInfo;
499 semaphoreInfo.flags = 0;
500 VkSemaphore semaphore;
501 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
502 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500503
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500504 GrBackendSemaphore backendSemaphore;
505 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500506
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500507 int fenceFd = -1;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400508 DestroySemaphoreInfo* destroyInfo =
509 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400510 GrFlushInfo flushInfo;
511 flushInfo.fNumSemaphores = 1;
512 flushInfo.fSignalSemaphores = &backendSemaphore;
513 flushInfo.fFinishedProc = destroy_semaphore;
514 flushInfo.fFinishedContext = destroyInfo;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400515 GrSemaphoresSubmitted submitted = bufferInfo->skSurface->flush(
Greg Danielc7ad4082020-05-14 15:38:26 -0400516 SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400517 GrDirectContext* context = GrAsDirectContext(bufferInfo->skSurface->recordingContext());
518 ALOGE_IF(!context, "Surface is not backed by gpu");
519 context->submit();
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500520 if (submitted == GrSemaphoresSubmitted::kYes) {
521 VkSemaphoreGetFdInfoKHR getFdInfo;
522 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
523 getFdInfo.pNext = nullptr;
524 getFdInfo.semaphore = semaphore;
525 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500526
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500527 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
528 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
529 } else {
530 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800531
532 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500533 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500534 }
Greg Danielfd429392019-05-09 15:44:56 -0400535 destroy_semaphore(destroyInfo);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500536
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500537 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500538}
539
540void VulkanManager::destroySurface(VulkanSurface* surface) {
541 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700542 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800543 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700544 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500545 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400546 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500547
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500548 delete surface;
549}
550
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400551VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
552 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800553 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400554 SkColorType surfaceColorType,
555 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700556 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500557 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500558 if (!window) {
559 return nullptr;
560 }
561
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500562 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700563 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500564}
565
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400566status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400567 if (!hasVkContext()) {
568 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
569 return INVALID_OPERATION;
570 }
571
Stan Iliev7a081272018-10-26 17:54:18 -0400572 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400573 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400574 if (fenceFd == -1) {
575 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
576 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000577 }
Stan Iliev7a081272018-10-26 17:54:18 -0400578
579 VkSemaphoreCreateInfo semaphoreInfo;
580 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
581 semaphoreInfo.pNext = nullptr;
582 semaphoreInfo.flags = 0;
583 VkSemaphore semaphore;
584 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
585 if (VK_SUCCESS != err) {
586 ALOGE("Failed to create import semaphore, err: %d", err);
587 return UNKNOWN_ERROR;
588 }
589 VkImportSemaphoreFdInfoKHR importInfo;
590 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
591 importInfo.pNext = nullptr;
592 importInfo.semaphore = semaphore;
593 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
594 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
595 importInfo.fd = fenceFd;
596
597 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
598 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400599 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400600 ALOGE("Failed to import semaphore, err: %d", err);
601 return UNKNOWN_ERROR;
602 }
603
Greg Danield92a9b12019-04-23 10:11:04 -0400604 GrBackendSemaphore beSemaphore;
605 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400606
Greg Danield92a9b12019-04-23 10:11:04 -0400607 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
608 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400609 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400610
Stan Iliev564ca3e2018-09-04 22:00:00 +0000611 return OK;
612}
613
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400614status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400615 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400616 if (!hasVkContext()) {
617 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
618 return INVALID_OPERATION;
619 }
620
Greg Daniel26e0dca2018-09-18 10:33:19 -0400621 VkExportSemaphoreCreateInfo exportInfo;
622 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
623 exportInfo.pNext = nullptr;
624 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
625
626 VkSemaphoreCreateInfo semaphoreInfo;
627 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
628 semaphoreInfo.pNext = &exportInfo;
629 semaphoreInfo.flags = 0;
630 VkSemaphore semaphore;
631 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
632 if (VK_SUCCESS != err) {
633 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
634 return INVALID_OPERATION;
635 }
636
Greg Danield92a9b12019-04-23 10:11:04 -0400637 GrBackendSemaphore backendSemaphore;
638 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400639
Stan Ilievaaa9e832019-09-17 14:07:23 -0400640 DestroySemaphoreInfo* destroyInfo =
641 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400642 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
643 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
644 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400645 GrFlushInfo flushInfo;
646 flushInfo.fNumSemaphores = 1;
647 flushInfo.fSignalSemaphores = &backendSemaphore;
648 flushInfo.fFinishedProc = destroy_semaphore;
649 flushInfo.fFinishedContext = destroyInfo;
650 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
651 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400652
Greg Danield92a9b12019-04-23 10:11:04 -0400653 if (submitted == GrSemaphoresSubmitted::kNo) {
654 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400655 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400656 return INVALID_OPERATION;
657 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400658
659 VkSemaphoreGetFdInfoKHR getFdInfo;
660 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
661 getFdInfo.pNext = nullptr;
662 getFdInfo.semaphore = semaphore;
663 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
664
665 int fenceFd = 0;
666
667 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400668 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400669 if (VK_SUCCESS != err) {
670 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
671 return INVALID_OPERATION;
672 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400673 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400674
Stan Iliev564ca3e2018-09-04 22:00:00 +0000675 return OK;
676}
677
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500678} /* namespace renderthread */
679} /* namespace uirenderer */
680} /* namespace android */