blob: 0c5cf682e5668bd3f23c71a0181e610961939641 [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
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500364 // create the command pool for the command buffers
365 if (VK_NULL_HANDLE == mCommandPool) {
366 VkCommandPoolCreateInfo commandPoolInfo;
367 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
368 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
369 // this needs to be on the render queue
Greg Daniel2ff202712018-06-14 11:50:10 -0400370 commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500371 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
John Reck0fa0cbc2019-04-05 16:57:46 -0700372 SkDEBUGCODE(VkResult res =)
373 mCreateCommandPool(mDevice, &commandPoolInfo, nullptr, &mCommandPool);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500374 SkASSERT(VK_SUCCESS == res);
375 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400376 LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE);
377
Greg Daniel2ff202712018-06-14 11:50:10 -0400378 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500379
Greg Danielcd558522016-11-17 13:31:40 -0500380 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
381 mSwapBehavior = SwapBehavior::BufferAge;
382 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500383}
384
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400385sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
386 ContextType contextType) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700387 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
Stan Iliev981afe72019-02-13 14:24:33 -0500388 if (device != VK_NULL_HANDLE) {
389 return vkGetDeviceProcAddr(device, proc_name);
390 }
391 return vkGetInstanceProcAddr(instance, proc_name);
392 };
393
394 GrVkBackendContext backendContext;
395 backendContext.fInstance = mInstance;
396 backendContext.fPhysicalDevice = mPhysicalDevice;
397 backendContext.fDevice = mDevice;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400398 backendContext.fQueue = (contextType == ContextType::kRenderThread) ? mGraphicsQueue
399 : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500400 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
401 backendContext.fMaxAPIVersion = mAPIVersion;
402 backendContext.fVkExtensions = &mExtensions;
403 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
404 backendContext.fGetProc = std::move(getProc);
405
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400406 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500407}
408
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800409VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
410 return VkFunctorInitParams{
411 .instance = mInstance,
412 .physical_device = mPhysicalDevice,
413 .device = mDevice,
414 .queue = mGraphicsQueue,
415 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500416 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800417 .enabled_instance_extension_names = mInstanceExtensions.data(),
418 .enabled_instance_extension_names_length =
419 static_cast<uint32_t>(mInstanceExtensions.size()),
420 .enabled_device_extension_names = mDeviceExtensions.data(),
421 .enabled_device_extension_names_length =
422 static_cast<uint32_t>(mDeviceExtensions.size()),
423 .device_features_2 = &mPhysicalDeviceFeatures2,
424 };
425}
426
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500427Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500428 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
429
430 if (bufferInfo == nullptr) {
431 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
432 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500433 }
434
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500435 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500436
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500437 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400438 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
439 bool isSignalPending = false;
440 if (finfo != NULL) {
441 isSignalPending = finfo->status != 1;
442 sync_file_info_free(finfo);
443 }
444 if (isSignalPending) {
445 int fence_clone = dup(bufferInfo->dequeue_fence);
446 if (fence_clone == -1) {
447 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
448 errno);
449 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
450 } else {
451 VkSemaphoreCreateInfo semaphoreInfo;
452 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
453 semaphoreInfo.pNext = nullptr;
454 semaphoreInfo.flags = 0;
455 VkSemaphore semaphore;
456 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
457 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
458 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500459
Stan Iliev197843d2019-03-21 11:34:15 -0400460 VkImportSemaphoreFdInfoKHR importInfo;
461 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
462 importInfo.pNext = nullptr;
463 importInfo.semaphore = semaphore;
464 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
465 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
466 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500467
Stan Iliev197843d2019-03-21 11:34:15 -0400468 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
469 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500470
Stan Iliev197843d2019-03-21 11:34:15 -0400471 GrBackendSemaphore backendSemaphore;
472 backendSemaphore.initVulkan(semaphore);
473 bufferInfo->skSurface->wait(1, &backendSemaphore);
474 // The following flush blocks the GPU immediately instead of waiting for other
475 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700476 // TODO: remove the flush after finding why backendSemaphore is not working.
Greg Danielc7ad4082020-05-14 15:38:26 -0400477 bufferInfo->skSurface->flushAndSubmit();
Stan Iliev197843d2019-03-21 11:34:15 -0400478 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500479 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500480 }
481
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500482 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
483 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500484}
485
Greg Danield92a9b12019-04-23 10:11:04 -0400486struct DestroySemaphoreInfo {
487 PFN_vkDestroySemaphore mDestroyFunction;
488 VkDevice mDevice;
489 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400490 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
491 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
492 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
493 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
494 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
495 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
496 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400497
498 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400499 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400500 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
501};
502
503static void destroy_semaphore(void* context) {
504 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400505 --info->mRefs;
506 if (!info->mRefs) {
507 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
508 delete info;
509 }
Greg Danield92a9b12019-04-23 10:11:04 -0400510}
511
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500512void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
513 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
514 ATRACE_NAME("Finishing GPU work");
515 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500516 }
517
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400518 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
519 if (!bufferInfo) {
520 // If VulkanSurface::dequeueNativeBuffer failed earlier, then swapBuffers is a no-op.
521 return;
522 }
523
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500524 VkExportSemaphoreCreateInfo exportInfo;
525 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
526 exportInfo.pNext = nullptr;
527 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500528
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500529 VkSemaphoreCreateInfo semaphoreInfo;
530 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
531 semaphoreInfo.pNext = &exportInfo;
532 semaphoreInfo.flags = 0;
533 VkSemaphore semaphore;
534 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
535 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500536
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500537 GrBackendSemaphore backendSemaphore;
538 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500539
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500540 int fenceFd = -1;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400541 DestroySemaphoreInfo* destroyInfo =
542 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400543 GrFlushInfo flushInfo;
544 flushInfo.fNumSemaphores = 1;
545 flushInfo.fSignalSemaphores = &backendSemaphore;
546 flushInfo.fFinishedProc = destroy_semaphore;
547 flushInfo.fFinishedContext = destroyInfo;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400548 GrSemaphoresSubmitted submitted = bufferInfo->skSurface->flush(
Greg Danielc7ad4082020-05-14 15:38:26 -0400549 SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
550 ALOGE_IF(!bufferInfo->skSurface->getContext(), "Surface is not backed by gpu");
551 bufferInfo->skSurface->getContext()->submit();
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500552 if (submitted == GrSemaphoresSubmitted::kYes) {
553 VkSemaphoreGetFdInfoKHR getFdInfo;
554 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
555 getFdInfo.pNext = nullptr;
556 getFdInfo.semaphore = semaphore;
557 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500558
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500559 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
560 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
561 } else {
562 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
563 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500564 }
Greg Danielfd429392019-05-09 15:44:56 -0400565 destroy_semaphore(destroyInfo);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500566
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500567 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500568}
569
570void VulkanManager::destroySurface(VulkanSurface* surface) {
571 // Make sure all submit commands have finished before starting to destroy objects.
572 if (VK_NULL_HANDLE != mPresentQueue) {
573 mQueueWaitIdle(mPresentQueue);
574 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400575 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500576
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500577 delete surface;
578}
579
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400580VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
581 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800582 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400583 SkColorType surfaceColorType,
584 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700585 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500586 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500587 if (!window) {
588 return nullptr;
589 }
590
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500591 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700592 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500593}
594
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400595status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400596 if (!hasVkContext()) {
597 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
598 return INVALID_OPERATION;
599 }
600
Stan Iliev7a081272018-10-26 17:54:18 -0400601 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400602 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400603 if (fenceFd == -1) {
604 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
605 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000606 }
Stan Iliev7a081272018-10-26 17:54:18 -0400607
608 VkSemaphoreCreateInfo semaphoreInfo;
609 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
610 semaphoreInfo.pNext = nullptr;
611 semaphoreInfo.flags = 0;
612 VkSemaphore semaphore;
613 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
614 if (VK_SUCCESS != err) {
615 ALOGE("Failed to create import semaphore, err: %d", err);
616 return UNKNOWN_ERROR;
617 }
618 VkImportSemaphoreFdInfoKHR importInfo;
619 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
620 importInfo.pNext = nullptr;
621 importInfo.semaphore = semaphore;
622 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
623 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
624 importInfo.fd = fenceFd;
625
626 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
627 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400628 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400629 ALOGE("Failed to import semaphore, err: %d", err);
630 return UNKNOWN_ERROR;
631 }
632
Greg Danield92a9b12019-04-23 10:11:04 -0400633 GrBackendSemaphore beSemaphore;
634 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400635
Greg Danield92a9b12019-04-23 10:11:04 -0400636 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
637 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400638 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400639
Stan Iliev564ca3e2018-09-04 22:00:00 +0000640 return OK;
641}
642
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400643status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400644 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400645 if (!hasVkContext()) {
646 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
647 return INVALID_OPERATION;
648 }
649
Greg Daniel26e0dca2018-09-18 10:33:19 -0400650 VkExportSemaphoreCreateInfo exportInfo;
651 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
652 exportInfo.pNext = nullptr;
653 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
654
655 VkSemaphoreCreateInfo semaphoreInfo;
656 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
657 semaphoreInfo.pNext = &exportInfo;
658 semaphoreInfo.flags = 0;
659 VkSemaphore semaphore;
660 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
661 if (VK_SUCCESS != err) {
662 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
663 return INVALID_OPERATION;
664 }
665
Greg Danield92a9b12019-04-23 10:11:04 -0400666 GrBackendSemaphore backendSemaphore;
667 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400668
Stan Ilievaaa9e832019-09-17 14:07:23 -0400669 DestroySemaphoreInfo* destroyInfo =
670 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400671 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
672 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
673 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400674 GrFlushInfo flushInfo;
675 flushInfo.fNumSemaphores = 1;
676 flushInfo.fSignalSemaphores = &backendSemaphore;
677 flushInfo.fFinishedProc = destroy_semaphore;
678 flushInfo.fFinishedContext = destroyInfo;
679 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
680 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400681
Greg Danield92a9b12019-04-23 10:11:04 -0400682 if (submitted == GrSemaphoresSubmitted::kNo) {
683 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400684 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400685 return INVALID_OPERATION;
686 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400687
688 VkSemaphoreGetFdInfoKHR getFdInfo;
689 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
690 getFdInfo.pNext = nullptr;
691 getFdInfo.semaphore = semaphore;
692 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
693
694 int fenceFd = 0;
695
696 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400697 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400698 if (VK_SUCCESS != err) {
699 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
700 return INVALID_OPERATION;
701 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400702 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400703
Stan Iliev564ca3e2018-09-04 22:00:00 +0000704 return OK;
705}
706
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500707} /* namespace renderthread */
708} /* namespace uirenderer */
709} /* namespace android */