blob: 5edf3301b2e811e0cac258233a992361317fc629 [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
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050019#include <android/sync.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050020#include <gui/Surface.h>
21
Greg Danielcd558522016-11-17 13:31:40 -050022#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050023#include "RenderThread.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050024#include "renderstate/RenderState.h"
Ben Wagnereec27d52017-01-11 15:32:07 -050025#include "utils/FatVector.h"
John Reck322b8ab2019-03-14 13:15:28 -070026#include "utils/TraceUtils.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050027
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050028#include <GrBackendSemaphore.h>
Greg Danielac2d2322017-07-12 11:30:15 -040029#include <GrBackendSurface.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030#include <GrContext.h>
31#include <GrTypes.h>
Greg Daniela227dbb2018-08-20 09:19:48 -040032#include <vk/GrVkExtensions.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050033#include <vk/GrVkTypes.h>
34
35namespace android {
36namespace uirenderer {
37namespace renderthread {
38
Bo Liu7b8c1eb2019-01-08 20:17:55 -080039static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
40 // All Vulkan structs that could be part of the features chain will start with the
41 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
42 // so we can get access to the pNext for the next struct.
43 struct CommonVulkanHeader {
44 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070045 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080046 };
47
48 void* pNext = features.pNext;
49 while (pNext) {
50 void* current = pNext;
51 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
52 free(current);
53 }
54}
55
Greg Daniel2ff202712018-06-14 11:50:10 -040056#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
57#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
58#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050059
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050060void VulkanManager::destroy() {
Greg Daniel26e0dca2018-09-18 10:33:19 -040061 // We don't need to explicitly free the command buffer since it automatically gets freed when we
62 // delete the VkCommandPool below.
63 mDummyCB = VK_NULL_HANDLE;
64
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065 if (VK_NULL_HANDLE != mCommandPool) {
Greg Daniel2ff202712018-06-14 11:50:10 -040066 mDestroyCommandPool(mDevice, mCommandPool, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050067 mCommandPool = VK_NULL_HANDLE;
68 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069
Greg Daniel2ff202712018-06-14 11:50:10 -040070 if (mDevice != VK_NULL_HANDLE) {
71 mDeviceWaitIdle(mDevice);
72 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070073 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074
Greg Daniel2ff202712018-06-14 11:50:10 -040075 if (mInstance != VK_NULL_HANDLE) {
76 mDestroyInstance(mInstance, nullptr);
77 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078
Greg Daniel2ff202712018-06-14 11:50:10 -040079 mGraphicsQueue = VK_NULL_HANDLE;
80 mPresentQueue = VK_NULL_HANDLE;
81 mDevice = VK_NULL_HANDLE;
82 mPhysicalDevice = VK_NULL_HANDLE;
83 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080084 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080085 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080086 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080087 mDeviceExtensions.clear();
88 free_features_extensions_structs(mPhysicalDeviceFeatures2);
89 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040090}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050091
Stan Iliev90276c82019-02-03 18:01:02 -050092void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -040093 VkResult err;
94
95 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -070096 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
97 nullptr, // pNext
98 "android framework", // pApplicationName
99 0, // applicationVersion
100 "android framework", // pEngineName
101 0, // engineVerison
102 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400103 };
104
Greg Daniel2ff202712018-06-14 11:50:10 -0400105 {
106 GET_PROC(EnumerateInstanceExtensionProperties);
107
108 uint32_t extensionCount = 0;
109 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500110 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800111 mInstanceExtensionsOwner.resize(extensionCount);
112 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
113 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500114 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400115 bool hasKHRSurfaceExtension = false;
116 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800117 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
118 mInstanceExtensions.push_back(extension.extensionName);
119 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400120 hasKHRSurfaceExtension = true;
121 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800122 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400123 hasKHRAndroidSurfaceExtension = true;
124 }
125 }
Stan Iliev90276c82019-02-03 18:01:02 -0500126 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 }
128
129 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700130 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
131 nullptr, // pNext
132 0, // flags
133 &app_info, // pApplicationInfo
134 0, // enabledLayerNameCount
135 nullptr, // ppEnabledLayerNames
136 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
137 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400138 };
139
140 GET_PROC(CreateInstance);
141 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500142 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400143
144 GET_INST_PROC(DestroyInstance);
145 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniel96259622018-10-01 14:42:56 -0400146 GET_INST_PROC(GetPhysicalDeviceProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400147 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniela227dbb2018-08-20 09:19:48 -0400148 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500149 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400150 GET_INST_PROC(CreateDevice);
151 GET_INST_PROC(EnumerateDeviceExtensionProperties);
152 GET_INST_PROC(CreateAndroidSurfaceKHR);
153 GET_INST_PROC(DestroySurfaceKHR);
154 GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR);
155 GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
156 GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
157 GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
158
159 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500160 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
161 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400162 // Just returning the first physical device instead of getting the whole array. Since there
163 // should only be one device on android.
164 gpuCount = 1;
165 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
166 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500167 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400168
Greg Daniel96259622018-10-01 14:42:56 -0400169 VkPhysicalDeviceProperties physDeviceProperties;
170 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500171 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400172 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400173
Greg Daniel2173f182019-04-01 09:29:44 -0400174 mIsQualcomm = physDeviceProperties.vendorID == 20803;
175
Greg Daniel2ff202712018-06-14 11:50:10 -0400176 // query to get the initial queue props size
177 uint32_t queueCount;
178 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500179 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400180
181 // now get the actual queue props
182 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
183 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
184
185 // iterate to find the graphics queue
186 mGraphicsQueueIndex = queueCount;
187 for (uint32_t i = 0; i < queueCount; i++) {
188 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
189 mGraphicsQueueIndex = i;
190 break;
191 }
192 }
Stan Iliev90276c82019-02-03 18:01:02 -0500193 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400194
195 // All physical devices and queue families on Android must be capable of
196 // presentation with any native window. So just use the first one.
197 mPresentQueueIndex = 0;
198
Greg Daniel2ff202712018-06-14 11:50:10 -0400199 {
200 uint32_t extensionCount = 0;
201 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700202 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500203 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800204 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400205 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700206 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500207 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800209 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
210 mDeviceExtensions.push_back(extension.extensionName);
211 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400212 hasKHRSwapchainExtension = true;
213 }
214 }
Stan Iliev90276c82019-02-03 18:01:02 -0500215 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400216 }
217
John Reck0fa0cbc2019-04-05 16:57:46 -0700218 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
Greg Daniela227dbb2018-08-20 09:19:48 -0400219 if (device != VK_NULL_HANDLE) {
220 return vkGetDeviceProcAddr(device, proc_name);
221 }
222 return vkGetInstanceProcAddr(instance, proc_name);
223 };
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800224
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800225 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700226 mInstanceExtensions.data(), mDeviceExtensions.size(),
227 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400228
Stan Iliev90276c82019-02-03 18:01:02 -0500229 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400230
Greg Daniela227dbb2018-08-20 09:19:48 -0400231 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
232 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
233 features.pNext = nullptr;
234
235 // Setup all extension feature structs we may want to use.
236 void** tailPNext = &features.pNext;
237
238 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
239 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700240 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400241 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
242 LOG_ALWAYS_FATAL_IF(!blend);
243 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
244 blend->pNext = nullptr;
245 *tailPNext = blend;
246 tailPNext = &blend->pNext;
247 }
248
Greg Daniel05036172018-11-28 17:08:04 -0500249 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700250 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500251 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
252 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
253 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
254 ycbcrFeature->pNext = nullptr;
255 *tailPNext = ycbcrFeature;
256 tailPNext = &ycbcrFeature->pNext;
257
Greg Daniela227dbb2018-08-20 09:19:48 -0400258 // query to get the physical device features
259 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400260 // this looks like it would slow things down,
261 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400262 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400263
John Reck0fa0cbc2019-04-05 16:57:46 -0700264 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400265
Stan Iliev7e733362019-02-28 13:16:36 -0500266 void* queueNextPtr = nullptr;
267
268 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
269
John Reck0fa0cbc2019-04-05 16:57:46 -0700270 if (Properties::contextPriority != 0 &&
271 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500272 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
273 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700274 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500275 queuePriorityCreateInfo.pNext = nullptr;
276 switch (Properties::contextPriority) {
277 case EGL_CONTEXT_PRIORITY_LOW_IMG:
278 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
279 break;
280 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
281 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
282 break;
283 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
284 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
285 break;
286 default:
287 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700288 }
289 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500290 }
291
Greg Daniel2ff202712018-06-14 11:50:10 -0400292 const VkDeviceQueueCreateInfo queueInfo[2] = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700293 {
294 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
295 queueNextPtr, // pNext
296 0, // VkDeviceQueueCreateFlags
297 mGraphicsQueueIndex, // queueFamilyIndex
298 1, // queueCount
299 queuePriorities, // pQueuePriorities
300 },
301 {
302 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
303 queueNextPtr, // pNext
304 0, // VkDeviceQueueCreateFlags
305 mPresentQueueIndex, // queueFamilyIndex
306 1, // queueCount
307 queuePriorities, // pQueuePriorities
308 }};
Greg Daniel2ff202712018-06-14 11:50:10 -0400309 uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1;
310
311 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700312 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
313 &features, // pNext
314 0, // VkDeviceCreateFlags
315 queueInfoCount, // queueCreateInfoCount
316 queueInfo, // pQueueCreateInfos
317 0, // layerCount
318 nullptr, // ppEnabledLayerNames
319 (uint32_t)mDeviceExtensions.size(), // extensionCount
320 mDeviceExtensions.data(), // ppEnabledExtensionNames
321 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400322 };
323
Stan Iliev90276c82019-02-03 18:01:02 -0500324 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400325
326 GET_DEV_PROC(GetDeviceQueue);
327 GET_DEV_PROC(DeviceWaitIdle);
328 GET_DEV_PROC(DestroyDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500329 GET_DEV_PROC(CreateCommandPool);
330 GET_DEV_PROC(DestroyCommandPool);
331 GET_DEV_PROC(AllocateCommandBuffers);
332 GET_DEV_PROC(FreeCommandBuffers);
333 GET_DEV_PROC(ResetCommandBuffer);
334 GET_DEV_PROC(BeginCommandBuffer);
335 GET_DEV_PROC(EndCommandBuffer);
336 GET_DEV_PROC(CmdPipelineBarrier);
337 GET_DEV_PROC(GetDeviceQueue);
338 GET_DEV_PROC(QueueSubmit);
339 GET_DEV_PROC(QueueWaitIdle);
340 GET_DEV_PROC(DeviceWaitIdle);
341 GET_DEV_PROC(CreateSemaphore);
342 GET_DEV_PROC(DestroySemaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400343 GET_DEV_PROC(ImportSemaphoreFdKHR);
344 GET_DEV_PROC(GetSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500345 GET_DEV_PROC(CreateFence);
346 GET_DEV_PROC(DestroyFence);
347 GET_DEV_PROC(WaitForFences);
348 GET_DEV_PROC(ResetFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400349}
350
351void VulkanManager::initialize() {
352 if (mDevice != VK_NULL_HANDLE) {
353 return;
354 }
355
Greg Daniela227dbb2018-08-20 09:19:48 -0400356 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500357 uint32_t instanceVersion;
358 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
359 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400360
Stan Iliev981afe72019-02-13 14:24:33 -0500361 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400362
363 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
364
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500365 // create the command pool for the command buffers
366 if (VK_NULL_HANDLE == mCommandPool) {
367 VkCommandPoolCreateInfo commandPoolInfo;
368 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
369 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
370 // this needs to be on the render queue
Greg Daniel2ff202712018-06-14 11:50:10 -0400371 commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500372 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
John Reck0fa0cbc2019-04-05 16:57:46 -0700373 SkDEBUGCODE(VkResult res =)
374 mCreateCommandPool(mDevice, &commandPoolInfo, nullptr, &mCommandPool);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500375 SkASSERT(VK_SUCCESS == res);
376 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400377 LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE);
378
379 if (!setupDummyCommandBuffer()) {
380 this->destroy();
Stan Iliev90276c82019-02-03 18:01:02 -0500381 // Pass through will crash on next line.
Greg Daniel26e0dca2018-09-18 10:33:19 -0400382 }
383 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
384
Greg Daniel2ff202712018-06-14 11:50:10 -0400385 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500386
Greg Danielcd558522016-11-17 13:31:40 -0500387 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
388 mSwapBehavior = SwapBehavior::BufferAge;
389 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500390}
391
Stan Iliev898123b2019-02-14 14:57:44 -0500392sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700393 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
Stan Iliev981afe72019-02-13 14:24:33 -0500394 if (device != VK_NULL_HANDLE) {
395 return vkGetDeviceProcAddr(device, proc_name);
396 }
397 return vkGetInstanceProcAddr(instance, proc_name);
398 };
399
400 GrVkBackendContext backendContext;
401 backendContext.fInstance = mInstance;
402 backendContext.fPhysicalDevice = mPhysicalDevice;
403 backendContext.fDevice = mDevice;
404 backendContext.fQueue = mGraphicsQueue;
405 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
406 backendContext.fMaxAPIVersion = mAPIVersion;
407 backendContext.fVkExtensions = &mExtensions;
408 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
409 backendContext.fGetProc = std::move(getProc);
410
411 return GrContext::MakeVulkan(backendContext, options);
412}
413
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800414VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
415 return VkFunctorInitParams{
416 .instance = mInstance,
417 .physical_device = mPhysicalDevice,
418 .device = mDevice,
419 .queue = mGraphicsQueue,
420 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500421 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800422 .enabled_instance_extension_names = mInstanceExtensions.data(),
423 .enabled_instance_extension_names_length =
424 static_cast<uint32_t>(mInstanceExtensions.size()),
425 .enabled_device_extension_names = mDeviceExtensions.data(),
426 .enabled_device_extension_names_length =
427 static_cast<uint32_t>(mDeviceExtensions.size()),
428 .device_features_2 = &mPhysicalDeviceFeatures2,
429 };
430}
431
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500432Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500433 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
434
435 if (bufferInfo == nullptr) {
436 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
437 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500438 }
439
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500440 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500441
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500442 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400443 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
444 bool isSignalPending = false;
445 if (finfo != NULL) {
446 isSignalPending = finfo->status != 1;
447 sync_file_info_free(finfo);
448 }
449 if (isSignalPending) {
450 int fence_clone = dup(bufferInfo->dequeue_fence);
451 if (fence_clone == -1) {
452 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
453 errno);
454 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
455 } else {
456 VkSemaphoreCreateInfo semaphoreInfo;
457 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
458 semaphoreInfo.pNext = nullptr;
459 semaphoreInfo.flags = 0;
460 VkSemaphore semaphore;
461 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
462 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
463 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500464
Stan Iliev197843d2019-03-21 11:34:15 -0400465 VkImportSemaphoreFdInfoKHR importInfo;
466 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
467 importInfo.pNext = nullptr;
468 importInfo.semaphore = semaphore;
469 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
470 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
471 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500472
Stan Iliev197843d2019-03-21 11:34:15 -0400473 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
474 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500475
Stan Iliev197843d2019-03-21 11:34:15 -0400476 GrBackendSemaphore backendSemaphore;
477 backendSemaphore.initVulkan(semaphore);
478 bufferInfo->skSurface->wait(1, &backendSemaphore);
479 // The following flush blocks the GPU immediately instead of waiting for other
480 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700481 // TODO: remove the flush after finding why backendSemaphore is not working.
Stan Iliev197843d2019-03-21 11:34:15 -0400482 bufferInfo->skSurface->flush();
483 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500484 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500485 }
486
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500487 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
488 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500489}
490
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500491void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
492 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
493 ATRACE_NAME("Finishing GPU work");
494 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500495 }
496
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400497 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
498 if (!bufferInfo) {
499 // If VulkanSurface::dequeueNativeBuffer failed earlier, then swapBuffers is a no-op.
500 return;
501 }
502
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500503 VkExportSemaphoreCreateInfo exportInfo;
504 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
505 exportInfo.pNext = nullptr;
506 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500507
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500508 VkSemaphoreCreateInfo semaphoreInfo;
509 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
510 semaphoreInfo.pNext = &exportInfo;
511 semaphoreInfo.flags = 0;
512 VkSemaphore semaphore;
513 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
514 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500515
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500516 GrBackendSemaphore backendSemaphore;
517 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500518
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500519 int fenceFd = -1;
520 GrSemaphoresSubmitted submitted =
521 bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
522 SkSurface::kNone_FlushFlags, 1, &backendSemaphore);
523 if (submitted == GrSemaphoresSubmitted::kYes) {
524 VkSemaphoreGetFdInfoKHR getFdInfo;
525 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
526 getFdInfo.pNext = nullptr;
527 getFdInfo.semaphore = semaphore;
528 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500529
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500530 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
531 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
532 } else {
533 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
534 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500535 }
536
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500537 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500538
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500539 // Exporting a semaphore with copy transference via vkGetSemaphoreFdKHR, has the same effect of
540 // destroying the semaphore and creating a new one with the same handle, and the payloads
541 // ownership is move to the Fd we created. Thus the semaphore is in a state that we can delete
542 // it and we don't need to wait on the command buffer we submitted to finish.
543 mDestroySemaphore(mDevice, semaphore, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500544}
545
546void VulkanManager::destroySurface(VulkanSurface* surface) {
547 // Make sure all submit commands have finished before starting to destroy objects.
548 if (VK_NULL_HANDLE != mPresentQueue) {
549 mQueueWaitIdle(mPresentQueue);
550 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400551 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500552
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500553 delete surface;
554}
555
Stan Iliev987a80c02018-12-04 10:07:21 -0500556VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800557 sk_sp<SkColorSpace> surfaceColorSpace,
John Reck0fa0cbc2019-04-05 16:57:46 -0700558 SkColorType surfaceColorType, GrContext* grContext,
559 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500560 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500561 if (!window) {
562 return nullptr;
563 }
564
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500565 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700566 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500567}
568
Greg Daniel26e0dca2018-09-18 10:33:19 -0400569bool VulkanManager::setupDummyCommandBuffer() {
570 if (mDummyCB != VK_NULL_HANDLE) {
571 return true;
572 }
573
574 VkCommandBufferAllocateInfo commandBuffersInfo;
575 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
576 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
577 commandBuffersInfo.pNext = nullptr;
578 commandBuffersInfo.commandPool = mCommandPool;
579 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
580 commandBuffersInfo.commandBufferCount = 1;
581
582 VkResult err = mAllocateCommandBuffers(mDevice, &commandBuffersInfo, &mDummyCB);
583 if (err != VK_SUCCESS) {
584 // It is probably unnecessary to set this back to VK_NULL_HANDLE, but we set it anyways to
585 // make sure the driver didn't set a value and then return a failure.
586 mDummyCB = VK_NULL_HANDLE;
587 return false;
588 }
589
590 VkCommandBufferBeginInfo beginInfo;
591 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
592 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
593 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
594
595 mBeginCommandBuffer(mDummyCB, &beginInfo);
596 mEndCommandBuffer(mDummyCB);
597 return true;
598}
599
Stan Iliev564ca3e2018-09-04 22:00:00 +0000600status_t VulkanManager::fenceWait(sp<Fence>& fence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400601 if (!hasVkContext()) {
602 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
603 return INVALID_OPERATION;
604 }
605
Stan Iliev7a081272018-10-26 17:54:18 -0400606 // Block GPU on the fence.
607 int fenceFd = fence->dup();
608 if (fenceFd == -1) {
609 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
610 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000611 }
Stan Iliev7a081272018-10-26 17:54:18 -0400612
613 VkSemaphoreCreateInfo semaphoreInfo;
614 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
615 semaphoreInfo.pNext = nullptr;
616 semaphoreInfo.flags = 0;
617 VkSemaphore semaphore;
618 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
619 if (VK_SUCCESS != err) {
620 ALOGE("Failed to create import semaphore, err: %d", err);
621 return UNKNOWN_ERROR;
622 }
623 VkImportSemaphoreFdInfoKHR importInfo;
624 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
625 importInfo.pNext = nullptr;
626 importInfo.semaphore = semaphore;
627 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
628 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
629 importInfo.fd = fenceFd;
630
631 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
632 if (VK_SUCCESS != err) {
633 ALOGE("Failed to import semaphore, err: %d", err);
634 return UNKNOWN_ERROR;
635 }
636
637 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
638
639 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
640
641 VkSubmitInfo submitInfo;
642 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
643 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
644 submitInfo.waitSemaphoreCount = 1;
645 // Wait to make sure aquire semaphore set above has signaled.
646 submitInfo.pWaitSemaphores = &semaphore;
647 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
648 submitInfo.commandBufferCount = 1;
649 submitInfo.pCommandBuffers = &mDummyCB;
650 submitInfo.signalSemaphoreCount = 0;
651
652 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
653
654 // On Android when we import a semaphore, it is imported using temporary permanence. That
655 // means as soon as we queue the semaphore for a wait it reverts to its previous permanent
656 // state before importing. This means it will now be in an idle state with no pending
657 // signal or wait operations, so it is safe to immediately delete it.
658 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000659 return OK;
660}
661
662status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400663 if (!hasVkContext()) {
664 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
665 return INVALID_OPERATION;
666 }
667
Greg Daniel26e0dca2018-09-18 10:33:19 -0400668 VkExportSemaphoreCreateInfo exportInfo;
669 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
670 exportInfo.pNext = nullptr;
671 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
672
673 VkSemaphoreCreateInfo semaphoreInfo;
674 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
675 semaphoreInfo.pNext = &exportInfo;
676 semaphoreInfo.flags = 0;
677 VkSemaphore semaphore;
678 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
679 if (VK_SUCCESS != err) {
680 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
681 return INVALID_OPERATION;
682 }
683
684 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
685
686 VkSubmitInfo submitInfo;
687 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
688 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
689 submitInfo.waitSemaphoreCount = 0;
690 submitInfo.pWaitSemaphores = nullptr;
691 submitInfo.pWaitDstStageMask = nullptr;
692 submitInfo.commandBufferCount = 1;
693 submitInfo.pCommandBuffers = &mDummyCB;
694 submitInfo.signalSemaphoreCount = 1;
695 submitInfo.pSignalSemaphores = &semaphore;
696
697 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
698
699 VkSemaphoreGetFdInfoKHR getFdInfo;
700 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
701 getFdInfo.pNext = nullptr;
702 getFdInfo.semaphore = semaphore;
703 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
704
705 int fenceFd = 0;
706
707 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
708 if (VK_SUCCESS != err) {
709 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
710 return INVALID_OPERATION;
711 }
712 nativeFence = new Fence(fenceFd);
713
714 // Exporting a semaphore with copy transference via vkGetSemahporeFdKHR, has the same effect of
715 // destroying the semaphore and creating a new one with the same handle, and the payloads
716 // ownership is move to the Fd we created. Thus the semahpore is in a state that we can delete
717 // it and we don't need to wait on the command buffer we submitted to finish.
718 mDestroySemaphore(mDevice, semaphore, nullptr);
719
Stan Iliev564ca3e2018-09-04 22:00:00 +0000720 return OK;
721}
722
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500723} /* namespace renderthread */
724} /* namespace uirenderer */
725} /* namespace android */