blob: 76ec078ce3c944fc3b9140871765327c8fc3dcb5 [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
Greg Danielcd558522016-11-17 13:31:40 -050030#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050031#include "RenderThread.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050032#include "renderstate/RenderState.h"
John Reck322b8ab2019-03-14 13:15:28 -070033#include "utils/TraceUtils.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050035namespace 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 Sollenberger802fefa2020-08-13 16:53:30 -040060sp<VulkanManager> VulkanManager::getInstance() {
61 // cache a weakptr to the context to enable a second thread to share the same vulkan state
62 static wp<VulkanManager> sWeakInstance = nullptr;
63 static std::mutex sLock;
64
65 std::lock_guard _lock{sLock};
66 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
67 if (!vulkanManager.get()) {
68 vulkanManager = new VulkanManager();
69 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050070 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050071
Derek Sollenberger802fefa2020-08-13 16:53:30 -040072 return vulkanManager;
73}
74
75VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -040076 if (mDevice != VK_NULL_HANDLE) {
77 mDeviceWaitIdle(mDevice);
78 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070079 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050080
Greg Daniel2ff202712018-06-14 11:50:10 -040081 if (mInstance != VK_NULL_HANDLE) {
82 mDestroyInstance(mInstance, nullptr);
83 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050084
Greg Daniel2ff202712018-06-14 11:50:10 -040085 mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger802fefa2020-08-13 16:53:30 -040086 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -040087 mPresentQueue = VK_NULL_HANDLE;
88 mDevice = VK_NULL_HANDLE;
89 mPhysicalDevice = VK_NULL_HANDLE;
90 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080091 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080092 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080093 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080094 mDeviceExtensions.clear();
95 free_features_extensions_structs(mPhysicalDeviceFeatures2);
96 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040097}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050098
Stan Iliev90276c82019-02-03 18:01:02 -050099void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400100 VkResult err;
101
102 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700103 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
104 nullptr, // pNext
105 "android framework", // pApplicationName
106 0, // applicationVersion
107 "android framework", // pEngineName
108 0, // engineVerison
109 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400110 };
111
Greg Daniel2ff202712018-06-14 11:50:10 -0400112 {
113 GET_PROC(EnumerateInstanceExtensionProperties);
114
115 uint32_t extensionCount = 0;
116 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500117 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800118 mInstanceExtensionsOwner.resize(extensionCount);
119 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
120 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500121 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 bool hasKHRSurfaceExtension = false;
123 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800124 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
125 mInstanceExtensions.push_back(extension.extensionName);
126 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 hasKHRSurfaceExtension = true;
128 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800129 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400130 hasKHRAndroidSurfaceExtension = true;
131 }
132 }
Stan Iliev90276c82019-02-03 18:01:02 -0500133 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400134 }
135
136 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700137 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
138 nullptr, // pNext
139 0, // flags
140 &app_info, // pApplicationInfo
141 0, // enabledLayerNameCount
142 nullptr, // ppEnabledLayerNames
143 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
144 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400145 };
146
147 GET_PROC(CreateInstance);
148 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500149 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400150
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700151 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400152 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700153 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400154 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400155 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500156 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700157 GET_INST_PROC(GetPhysicalDeviceProperties);
158 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400159
160 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500161 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
162 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400163 // Just returning the first physical device instead of getting the whole array. Since there
164 // should only be one device on android.
165 gpuCount = 1;
166 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
167 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500168 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400169
Greg Daniel96259622018-10-01 14:42:56 -0400170 VkPhysicalDeviceProperties physDeviceProperties;
171 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500172 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400173 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400174
Greg Daniel2ff202712018-06-14 11:50:10 -0400175 // query to get the initial queue props size
176 uint32_t queueCount;
177 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500178 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400179
180 // now get the actual queue props
181 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
182 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
183
184 // iterate to find the graphics queue
185 mGraphicsQueueIndex = queueCount;
186 for (uint32_t i = 0; i < queueCount; i++) {
187 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
188 mGraphicsQueueIndex = i;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400189 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < 2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400190 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
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400298 2, // queueCount
John Reck0fa0cbc2019-04-05 16:57:46 -0700299 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
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500326 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500327 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500328 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700329 GET_DEV_PROC(CreateCommandPool);
330 GET_DEV_PROC(CreateFence);
331 GET_DEV_PROC(CreateSemaphore);
332 GET_DEV_PROC(DestroyCommandPool);
333 GET_DEV_PROC(DestroyDevice);
334 GET_DEV_PROC(DestroyFence);
335 GET_DEV_PROC(DestroySemaphore);
336 GET_DEV_PROC(DeviceWaitIdle);
337 GET_DEV_PROC(EndCommandBuffer);
338 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500339 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700340 GET_DEV_PROC(GetSemaphoreFdKHR);
341 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500342 GET_DEV_PROC(QueueSubmit);
343 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700344 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500345 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700346 GET_DEV_PROC(WaitForFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400347}
348
349void VulkanManager::initialize() {
350 if (mDevice != VK_NULL_HANDLE) {
351 return;
352 }
353
Greg Daniela227dbb2018-08-20 09:19:48 -0400354 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500355 uint32_t instanceVersion;
356 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
357 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400358
Stan Iliev981afe72019-02-13 14:24:33 -0500359 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400360
361 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400362 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniel2ff202712018-06-14 11:50:10 -0400363
Greg Daniel2ff202712018-06-14 11:50:10 -0400364 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500365
Greg Danielcd558522016-11-17 13:31:40 -0500366 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
367 mSwapBehavior = SwapBehavior::BufferAge;
368 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500369}
370
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400371sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
372 ContextType contextType) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700373 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
Stan Iliev981afe72019-02-13 14:24:33 -0500374 if (device != VK_NULL_HANDLE) {
375 return vkGetDeviceProcAddr(device, proc_name);
376 }
377 return vkGetInstanceProcAddr(instance, proc_name);
378 };
379
380 GrVkBackendContext backendContext;
381 backendContext.fInstance = mInstance;
382 backendContext.fPhysicalDevice = mPhysicalDevice;
383 backendContext.fDevice = mDevice;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400384 backendContext.fQueue = (contextType == ContextType::kRenderThread) ? mGraphicsQueue
385 : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500386 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
387 backendContext.fMaxAPIVersion = mAPIVersion;
388 backendContext.fVkExtensions = &mExtensions;
389 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
390 backendContext.fGetProc = std::move(getProc);
391
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400392 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500393}
394
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800395VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
396 return VkFunctorInitParams{
397 .instance = mInstance,
398 .physical_device = mPhysicalDevice,
399 .device = mDevice,
400 .queue = mGraphicsQueue,
401 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500402 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800403 .enabled_instance_extension_names = mInstanceExtensions.data(),
404 .enabled_instance_extension_names_length =
405 static_cast<uint32_t>(mInstanceExtensions.size()),
406 .enabled_device_extension_names = mDeviceExtensions.data(),
407 .enabled_device_extension_names_length =
408 static_cast<uint32_t>(mDeviceExtensions.size()),
409 .device_features_2 = &mPhysicalDeviceFeatures2,
410 };
411}
412
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500413Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500414 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
415
416 if (bufferInfo == nullptr) {
417 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
418 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500419 }
420
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500421 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500422
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500423 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400424 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
425 bool isSignalPending = false;
426 if (finfo != NULL) {
427 isSignalPending = finfo->status != 1;
428 sync_file_info_free(finfo);
429 }
430 if (isSignalPending) {
431 int fence_clone = dup(bufferInfo->dequeue_fence);
432 if (fence_clone == -1) {
433 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
434 errno);
435 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
436 } else {
437 VkSemaphoreCreateInfo semaphoreInfo;
438 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
439 semaphoreInfo.pNext = nullptr;
440 semaphoreInfo.flags = 0;
441 VkSemaphore semaphore;
442 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
443 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
444 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500445
Stan Iliev197843d2019-03-21 11:34:15 -0400446 VkImportSemaphoreFdInfoKHR importInfo;
447 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
448 importInfo.pNext = nullptr;
449 importInfo.semaphore = semaphore;
450 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
451 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
452 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500453
Stan Iliev197843d2019-03-21 11:34:15 -0400454 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
455 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500456
Stan Iliev197843d2019-03-21 11:34:15 -0400457 GrBackendSemaphore backendSemaphore;
458 backendSemaphore.initVulkan(semaphore);
459 bufferInfo->skSurface->wait(1, &backendSemaphore);
460 // The following flush blocks the GPU immediately instead of waiting for other
461 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700462 // TODO: remove the flush after finding why backendSemaphore is not working.
Greg Danielc7ad4082020-05-14 15:38:26 -0400463 bufferInfo->skSurface->flushAndSubmit();
Stan Iliev197843d2019-03-21 11:34:15 -0400464 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500465 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500466 }
467
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500468 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
469 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500470}
471
Greg Danield92a9b12019-04-23 10:11:04 -0400472struct DestroySemaphoreInfo {
473 PFN_vkDestroySemaphore mDestroyFunction;
474 VkDevice mDevice;
475 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400476 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
477 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
478 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
479 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
480 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
481 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
482 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400483
484 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400485 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400486 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
487};
488
489static void destroy_semaphore(void* context) {
490 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400491 --info->mRefs;
492 if (!info->mRefs) {
493 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
494 delete info;
495 }
Greg Danield92a9b12019-04-23 10:11:04 -0400496}
497
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500498void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
499 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
500 ATRACE_NAME("Finishing GPU work");
501 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500502 }
503
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400504 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
505 if (!bufferInfo) {
506 // If VulkanSurface::dequeueNativeBuffer failed earlier, then swapBuffers is a no-op.
507 return;
508 }
509
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500510 VkExportSemaphoreCreateInfo exportInfo;
511 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
512 exportInfo.pNext = nullptr;
513 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500514
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500515 VkSemaphoreCreateInfo semaphoreInfo;
516 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
517 semaphoreInfo.pNext = &exportInfo;
518 semaphoreInfo.flags = 0;
519 VkSemaphore semaphore;
520 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
521 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500522
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500523 GrBackendSemaphore backendSemaphore;
524 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500525
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500526 int fenceFd = -1;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400527 DestroySemaphoreInfo* destroyInfo =
528 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400529 GrFlushInfo flushInfo;
530 flushInfo.fNumSemaphores = 1;
531 flushInfo.fSignalSemaphores = &backendSemaphore;
532 flushInfo.fFinishedProc = destroy_semaphore;
533 flushInfo.fFinishedContext = destroyInfo;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400534 GrSemaphoresSubmitted submitted = bufferInfo->skSurface->flush(
Greg Danielc7ad4082020-05-14 15:38:26 -0400535 SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
536 ALOGE_IF(!bufferInfo->skSurface->getContext(), "Surface is not backed by gpu");
537 bufferInfo->skSurface->getContext()->submit();
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500538 if (submitted == GrSemaphoresSubmitted::kYes) {
539 VkSemaphoreGetFdInfoKHR getFdInfo;
540 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
541 getFdInfo.pNext = nullptr;
542 getFdInfo.semaphore = semaphore;
543 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500544
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500545 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
546 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
547 } else {
548 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
549 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500550 }
Greg Danielfd429392019-05-09 15:44:56 -0400551 destroy_semaphore(destroyInfo);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500552
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500553 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500554}
555
556void VulkanManager::destroySurface(VulkanSurface* surface) {
557 // Make sure all submit commands have finished before starting to destroy objects.
558 if (VK_NULL_HANDLE != mPresentQueue) {
559 mQueueWaitIdle(mPresentQueue);
560 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400561 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500562
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500563 delete surface;
564}
565
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400566VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
567 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800568 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400569 SkColorType surfaceColorType,
570 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700571 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500572 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500573 if (!window) {
574 return nullptr;
575 }
576
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500577 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700578 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500579}
580
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400581status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400582 if (!hasVkContext()) {
583 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
584 return INVALID_OPERATION;
585 }
586
Stan Iliev7a081272018-10-26 17:54:18 -0400587 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400588 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400589 if (fenceFd == -1) {
590 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
591 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000592 }
Stan Iliev7a081272018-10-26 17:54:18 -0400593
594 VkSemaphoreCreateInfo semaphoreInfo;
595 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
596 semaphoreInfo.pNext = nullptr;
597 semaphoreInfo.flags = 0;
598 VkSemaphore semaphore;
599 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
600 if (VK_SUCCESS != err) {
601 ALOGE("Failed to create import semaphore, err: %d", err);
602 return UNKNOWN_ERROR;
603 }
604 VkImportSemaphoreFdInfoKHR importInfo;
605 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
606 importInfo.pNext = nullptr;
607 importInfo.semaphore = semaphore;
608 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
609 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
610 importInfo.fd = fenceFd;
611
612 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
613 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400614 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400615 ALOGE("Failed to import semaphore, err: %d", err);
616 return UNKNOWN_ERROR;
617 }
618
Greg Danield92a9b12019-04-23 10:11:04 -0400619 GrBackendSemaphore beSemaphore;
620 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400621
Greg Danield92a9b12019-04-23 10:11:04 -0400622 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
623 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400624 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400625
Stan Iliev564ca3e2018-09-04 22:00:00 +0000626 return OK;
627}
628
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400629status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400630 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400631 if (!hasVkContext()) {
632 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
633 return INVALID_OPERATION;
634 }
635
Greg Daniel26e0dca2018-09-18 10:33:19 -0400636 VkExportSemaphoreCreateInfo exportInfo;
637 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
638 exportInfo.pNext = nullptr;
639 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
640
641 VkSemaphoreCreateInfo semaphoreInfo;
642 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
643 semaphoreInfo.pNext = &exportInfo;
644 semaphoreInfo.flags = 0;
645 VkSemaphore semaphore;
646 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
647 if (VK_SUCCESS != err) {
648 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
649 return INVALID_OPERATION;
650 }
651
Greg Danield92a9b12019-04-23 10:11:04 -0400652 GrBackendSemaphore backendSemaphore;
653 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400654
Stan Ilievaaa9e832019-09-17 14:07:23 -0400655 DestroySemaphoreInfo* destroyInfo =
656 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400657 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
658 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
659 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400660 GrFlushInfo flushInfo;
661 flushInfo.fNumSemaphores = 1;
662 flushInfo.fSignalSemaphores = &backendSemaphore;
663 flushInfo.fFinishedProc = destroy_semaphore;
664 flushInfo.fFinishedContext = destroyInfo;
665 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
666 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400667
Greg Danield92a9b12019-04-23 10:11:04 -0400668 if (submitted == GrSemaphoresSubmitted::kNo) {
669 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400670 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400671 return INVALID_OPERATION;
672 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400673
674 VkSemaphoreGetFdInfoKHR getFdInfo;
675 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
676 getFdInfo.pNext = nullptr;
677 getFdInfo.semaphore = semaphore;
678 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
679
680 int fenceFd = 0;
681
682 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400683 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400684 if (VK_SUCCESS != err) {
685 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
686 return INVALID_OPERATION;
687 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400688 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400689
Stan Iliev564ca3e2018-09-04 22:00:00 +0000690 return OK;
691}
692
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500693} /* namespace renderthread */
694} /* namespace uirenderer */
695} /* namespace android */