blob: e93824dfbd30eaad0c7f39cc846f8cb9612cdfc9 [file] [log] [blame]
Derek Sollenberger0e3cba32016-11-09 11:58:36 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VulkanManager.h"
18
Alec Mouri6db59a62019-08-02 17:05:26 -070019#include <EGL/egl.h>
20#include <EGL/eglext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040021#include <GrBackendSemaphore.h>
22#include <GrBackendSurface.h>
Adlai Hollerf8c434e2020-07-27 11:42:45 -040023#include <GrDirectContext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040024#include <GrTypes.h>
25#include <android/sync.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000026#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040027#include <vk/GrVkExtensions.h>
28#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050029
Alec Mouriaa3e4982020-12-14 14:47:57 -080030#include <cstring>
31
Greg Danielcd558522016-11-17 13:31:40 -050032#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050033#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050034#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050035#include "renderstate/RenderState.h"
John Reck322b8ab2019-03-14 13:15:28 -070036#include "utils/TraceUtils.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038namespace android {
39namespace uirenderer {
40namespace renderthread {
41
Bo Liu7b8c1eb2019-01-08 20:17:55 -080042static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
43 // All Vulkan structs that could be part of the features chain will start with the
44 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
45 // so we can get access to the pNext for the next struct.
46 struct CommonVulkanHeader {
47 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070048 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080049 };
50
51 void* pNext = features.pNext;
52 while (pNext) {
53 void* current = pNext;
54 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
55 free(current);
56 }
57}
58
Alec Mouriaa3e4982020-12-14 14:47:57 -080059GrVkGetProc VulkanManager::sSkiaGetProp = [](const char* proc_name, VkInstance instance,
60 VkDevice device) {
61 if (device != VK_NULL_HANDLE) {
62 if (strcmp("vkQueueSubmit", proc_name) == 0) {
63 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueSubmit;
64 } else if (strcmp("vkQueueWaitIdle", proc_name) == 0) {
65 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueWaitIdle;
66 }
67 return vkGetDeviceProcAddr(device, proc_name);
68 }
69 return vkGetInstanceProcAddr(instance, proc_name);
70};
71
Greg Daniel2ff202712018-06-14 11:50:10 -040072#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
73#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
74#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050075
Derek Sollenberger802fefa2020-08-13 16:53:30 -040076sp<VulkanManager> VulkanManager::getInstance() {
77 // cache a weakptr to the context to enable a second thread to share the same vulkan state
78 static wp<VulkanManager> sWeakInstance = nullptr;
79 static std::mutex sLock;
80
81 std::lock_guard _lock{sLock};
82 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
83 if (!vulkanManager.get()) {
84 vulkanManager = new VulkanManager();
85 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050086 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050087
Derek Sollenberger802fefa2020-08-13 16:53:30 -040088 return vulkanManager;
89}
90
91VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -040092 if (mDevice != VK_NULL_HANDLE) {
93 mDeviceWaitIdle(mDevice);
94 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070095 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096
Greg Daniel2ff202712018-06-14 11:50:10 -040097 if (mInstance != VK_NULL_HANDLE) {
98 mDestroyInstance(mInstance, nullptr);
99 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100
Greg Daniel2ff202712018-06-14 11:50:10 -0400101 mGraphicsQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400102 mDevice = VK_NULL_HANDLE;
103 mPhysicalDevice = VK_NULL_HANDLE;
104 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800105 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800106 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800107 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800108 mDeviceExtensions.clear();
109 free_features_extensions_structs(mPhysicalDeviceFeatures2);
110 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400111}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500112
Stan Iliev90276c82019-02-03 18:01:02 -0500113void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400114 VkResult err;
115
116 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700117 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
118 nullptr, // pNext
119 "android framework", // pApplicationName
120 0, // applicationVersion
121 "android framework", // pEngineName
122 0, // engineVerison
123 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400124 };
125
Greg Daniel2ff202712018-06-14 11:50:10 -0400126 {
127 GET_PROC(EnumerateInstanceExtensionProperties);
128
129 uint32_t extensionCount = 0;
130 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500131 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800132 mInstanceExtensionsOwner.resize(extensionCount);
133 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
134 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500135 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400136 bool hasKHRSurfaceExtension = false;
137 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800138 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
139 mInstanceExtensions.push_back(extension.extensionName);
140 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400141 hasKHRSurfaceExtension = true;
142 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800143 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400144 hasKHRAndroidSurfaceExtension = true;
145 }
146 }
Stan Iliev90276c82019-02-03 18:01:02 -0500147 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400148 }
149
150 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700151 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
152 nullptr, // pNext
153 0, // flags
154 &app_info, // pApplicationInfo
155 0, // enabledLayerNameCount
156 nullptr, // ppEnabledLayerNames
157 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
158 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400159 };
160
161 GET_PROC(CreateInstance);
162 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500163 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400164
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700165 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400166 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700167 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400168 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400169 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500170 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700171 GET_INST_PROC(GetPhysicalDeviceProperties);
172 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400173
174 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500175 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
176 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400177 // Just returning the first physical device instead of getting the whole array. Since there
178 // should only be one device on android.
179 gpuCount = 1;
180 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
181 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500182 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400183
Greg Daniel96259622018-10-01 14:42:56 -0400184 VkPhysicalDeviceProperties physDeviceProperties;
185 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500186 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400187 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400188
Greg Daniel2ff202712018-06-14 11:50:10 -0400189 // query to get the initial queue props size
190 uint32_t queueCount;
191 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500192 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400193
194 // now get the actual queue props
195 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
196 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
197
198 // iterate to find the graphics queue
199 mGraphicsQueueIndex = queueCount;
200 for (uint32_t i = 0; i < queueCount; i++) {
201 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
202 mGraphicsQueueIndex = i;
203 break;
204 }
205 }
Stan Iliev90276c82019-02-03 18:01:02 -0500206 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400207
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 {
209 uint32_t extensionCount = 0;
210 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700211 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500212 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800213 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400214 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700215 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500216 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400217 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800218 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
219 mDeviceExtensions.push_back(extension.extensionName);
220 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400221 hasKHRSwapchainExtension = true;
222 }
223 }
Stan Iliev90276c82019-02-03 18:01:02 -0500224 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400225 }
226
Alec Mouriaa3e4982020-12-14 14:47:57 -0800227 grExtensions.init(sSkiaGetProp, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700228 mInstanceExtensions.data(), mDeviceExtensions.size(),
229 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400230
Stan Iliev90276c82019-02-03 18:01:02 -0500231 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400232
Greg Daniela227dbb2018-08-20 09:19:48 -0400233 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
234 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
235 features.pNext = nullptr;
236
237 // Setup all extension feature structs we may want to use.
238 void** tailPNext = &features.pNext;
239
240 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
241 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700242 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400243 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
244 LOG_ALWAYS_FATAL_IF(!blend);
245 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
246 blend->pNext = nullptr;
247 *tailPNext = blend;
248 tailPNext = &blend->pNext;
249 }
250
Greg Daniel05036172018-11-28 17:08:04 -0500251 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700252 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500253 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
254 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
255 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
256 ycbcrFeature->pNext = nullptr;
257 *tailPNext = ycbcrFeature;
258 tailPNext = &ycbcrFeature->pNext;
259
Greg Daniela227dbb2018-08-20 09:19:48 -0400260 // query to get the physical device features
261 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400262 // this looks like it would slow things down,
263 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400264 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400265
John Reck0fa0cbc2019-04-05 16:57:46 -0700266 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400267
Stan Iliev7e733362019-02-28 13:16:36 -0500268 void* queueNextPtr = nullptr;
269
270 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
271
John Reck0fa0cbc2019-04-05 16:57:46 -0700272 if (Properties::contextPriority != 0 &&
273 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500274 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
275 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700276 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500277 queuePriorityCreateInfo.pNext = nullptr;
278 switch (Properties::contextPriority) {
279 case EGL_CONTEXT_PRIORITY_LOW_IMG:
280 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
281 break;
282 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
283 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
284 break;
285 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
286 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
287 break;
288 default:
289 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700290 }
291 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500292 }
293
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700294 const VkDeviceQueueCreateInfo queueInfo = {
295 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
296 queueNextPtr, // pNext
297 0, // VkDeviceQueueCreateFlags
298 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouriaa3e4982020-12-14 14:47:57 -0800299 1, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700300 queuePriorities, // pQueuePriorities
301 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400302
303 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700304 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
305 &features, // pNext
306 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700307 1, // queueCreateInfoCount
308 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700309 0, // layerCount
310 nullptr, // ppEnabledLayerNames
311 (uint32_t)mDeviceExtensions.size(), // extensionCount
312 mDeviceExtensions.data(), // ppEnabledExtensionNames
313 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400314 };
315
Stan Iliev90276c82019-02-03 18:01:02 -0500316 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400317
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500318 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500319 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500320 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700321 GET_DEV_PROC(CreateCommandPool);
322 GET_DEV_PROC(CreateFence);
323 GET_DEV_PROC(CreateSemaphore);
324 GET_DEV_PROC(DestroyCommandPool);
325 GET_DEV_PROC(DestroyDevice);
326 GET_DEV_PROC(DestroyFence);
327 GET_DEV_PROC(DestroySemaphore);
328 GET_DEV_PROC(DeviceWaitIdle);
329 GET_DEV_PROC(EndCommandBuffer);
330 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500331 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700332 GET_DEV_PROC(GetSemaphoreFdKHR);
333 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500334 GET_DEV_PROC(QueueSubmit);
335 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700336 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500337 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700338 GET_DEV_PROC(WaitForFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400339}
340
341void VulkanManager::initialize() {
342 if (mDevice != VK_NULL_HANDLE) {
343 return;
344 }
345
Greg Daniela227dbb2018-08-20 09:19:48 -0400346 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500347 uint32_t instanceVersion;
348 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
349 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400350
Stan Iliev981afe72019-02-13 14:24:33 -0500351 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400352
353 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
354
Greg Danielcd558522016-11-17 13:31:40 -0500355 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
356 mSwapBehavior = SwapBehavior::BufferAge;
357 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500358}
359
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400360sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
361 ContextType contextType) {
Stan Iliev981afe72019-02-13 14:24:33 -0500362
363 GrVkBackendContext backendContext;
364 backendContext.fInstance = mInstance;
365 backendContext.fPhysicalDevice = mPhysicalDevice;
366 backendContext.fDevice = mDevice;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800367 backendContext.fQueue = mGraphicsQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500368 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
369 backendContext.fMaxAPIVersion = mAPIVersion;
370 backendContext.fVkExtensions = &mExtensions;
371 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800372 backendContext.fGetProc = sSkiaGetProp;
Stan Iliev981afe72019-02-13 14:24:33 -0500373
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400374 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500375}
376
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800377VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
378 return VkFunctorInitParams{
379 .instance = mInstance,
380 .physical_device = mPhysicalDevice,
381 .device = mDevice,
382 .queue = mGraphicsQueue,
383 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500384 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800385 .enabled_instance_extension_names = mInstanceExtensions.data(),
386 .enabled_instance_extension_names_length =
387 static_cast<uint32_t>(mInstanceExtensions.size()),
388 .enabled_device_extension_names = mDeviceExtensions.data(),
389 .enabled_device_extension_names_length =
390 static_cast<uint32_t>(mDeviceExtensions.size()),
391 .device_features_2 = &mPhysicalDeviceFeatures2,
392 };
393}
394
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500395Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500396 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
397
398 if (bufferInfo == nullptr) {
399 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
400 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500401 }
402
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500403 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500404
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500405 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400406 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
407 bool isSignalPending = false;
408 if (finfo != NULL) {
409 isSignalPending = finfo->status != 1;
410 sync_file_info_free(finfo);
411 }
412 if (isSignalPending) {
413 int fence_clone = dup(bufferInfo->dequeue_fence);
414 if (fence_clone == -1) {
415 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
416 errno);
417 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
418 } else {
419 VkSemaphoreCreateInfo semaphoreInfo;
420 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
421 semaphoreInfo.pNext = nullptr;
422 semaphoreInfo.flags = 0;
423 VkSemaphore semaphore;
424 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
425 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
426 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500427
Stan Iliev197843d2019-03-21 11:34:15 -0400428 VkImportSemaphoreFdInfoKHR importInfo;
429 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
430 importInfo.pNext = nullptr;
431 importInfo.semaphore = semaphore;
432 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
433 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
434 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500435
Stan Iliev197843d2019-03-21 11:34:15 -0400436 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
437 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500438
Stan Iliev197843d2019-03-21 11:34:15 -0400439 GrBackendSemaphore backendSemaphore;
440 backendSemaphore.initVulkan(semaphore);
441 bufferInfo->skSurface->wait(1, &backendSemaphore);
442 // The following flush blocks the GPU immediately instead of waiting for other
443 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700444 // TODO: remove the flush after finding why backendSemaphore is not working.
Greg Danielc7ad4082020-05-14 15:38:26 -0400445 bufferInfo->skSurface->flushAndSubmit();
Stan Iliev197843d2019-03-21 11:34:15 -0400446 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500447 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500448 }
449
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500450 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
451 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500452}
453
Greg Danield92a9b12019-04-23 10:11:04 -0400454struct DestroySemaphoreInfo {
455 PFN_vkDestroySemaphore mDestroyFunction;
456 VkDevice mDevice;
457 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400458 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
459 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
460 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
461 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
462 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
463 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
464 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400465
466 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400467 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400468 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
469};
470
471static void destroy_semaphore(void* context) {
472 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400473 --info->mRefs;
474 if (!info->mRefs) {
475 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
476 delete info;
477 }
Greg Danield92a9b12019-04-23 10:11:04 -0400478}
479
Greg Danielbe2803a2021-02-19 18:32:16 -0500480void VulkanManager::finishFrame(SkSurface* surface) {
481 ATRACE_NAME("Vulkan finish frame");
482 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
483 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400484
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500485 VkExportSemaphoreCreateInfo exportInfo;
486 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
487 exportInfo.pNext = nullptr;
488 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500489
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500490 VkSemaphoreCreateInfo semaphoreInfo;
491 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
492 semaphoreInfo.pNext = &exportInfo;
493 semaphoreInfo.flags = 0;
494 VkSemaphore semaphore;
495 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500496 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500497
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500498 GrBackendSemaphore backendSemaphore;
499 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500500
Greg Danielc7ad4082020-05-14 15:38:26 -0400501 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500502 if (err == VK_SUCCESS) {
503 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
504 flushInfo.fNumSemaphores = 1;
505 flushInfo.fSignalSemaphores = &backendSemaphore;
506 flushInfo.fFinishedProc = destroy_semaphore;
507 flushInfo.fFinishedContext = mDestroySemaphoreContext;
508 } else {
509 semaphore = VK_NULL_HANDLE;
510 }
511 GrSemaphoresSubmitted submitted =
512 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
513 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400514 ALOGE_IF(!context, "Surface is not backed by gpu");
515 context->submit();
Greg Danielbe2803a2021-02-19 18:32:16 -0500516 if (semaphore != VK_NULL_HANDLE) {
517 if (submitted == GrSemaphoresSubmitted::kYes) {
518 mSwapSemaphore = semaphore;
519 } else {
520 destroy_semaphore(mDestroySemaphoreContext);
521 mDestroySemaphoreContext = nullptr;
522 }
523 }
524 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
525}
526
527void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
528 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
529 ATRACE_NAME("Finishing GPU work");
530 mDeviceWaitIdle(mDevice);
531 }
532
533 int fenceFd = -1;
534 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500535 VkSemaphoreGetFdInfoKHR getFdInfo;
536 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
537 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500538 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500539 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500540
Greg Danielbe2803a2021-02-19 18:32:16 -0500541 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500542 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
543 } else {
544 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800545
546 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500547 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500548 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500549 destroy_semaphore(mDestroySemaphoreContext);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500550
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500551 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500552 mSwapSemaphore = VK_NULL_HANDLE;
553 mDestroySemaphoreContext = nullptr;
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.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700558 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800559 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700560 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500561 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400562 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500563
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500564 delete surface;
565}
566
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400567VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
568 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800569 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400570 SkColorType surfaceColorType,
571 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700572 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500573 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500574 if (!window) {
575 return nullptr;
576 }
577
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500578 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700579 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500580}
581
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400582status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400583 if (!hasVkContext()) {
584 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
585 return INVALID_OPERATION;
586 }
587
Stan Iliev7a081272018-10-26 17:54:18 -0400588 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400589 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400590 if (fenceFd == -1) {
591 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
592 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000593 }
Stan Iliev7a081272018-10-26 17:54:18 -0400594
595 VkSemaphoreCreateInfo semaphoreInfo;
596 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
597 semaphoreInfo.pNext = nullptr;
598 semaphoreInfo.flags = 0;
599 VkSemaphore semaphore;
600 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
601 if (VK_SUCCESS != err) {
602 ALOGE("Failed to create import semaphore, err: %d", err);
603 return UNKNOWN_ERROR;
604 }
605 VkImportSemaphoreFdInfoKHR importInfo;
606 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
607 importInfo.pNext = nullptr;
608 importInfo.semaphore = semaphore;
609 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
610 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
611 importInfo.fd = fenceFd;
612
613 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
614 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400615 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400616 ALOGE("Failed to import semaphore, err: %d", err);
617 return UNKNOWN_ERROR;
618 }
619
Greg Danield92a9b12019-04-23 10:11:04 -0400620 GrBackendSemaphore beSemaphore;
621 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400622
Greg Danield92a9b12019-04-23 10:11:04 -0400623 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
624 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400625 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400626
Stan Iliev564ca3e2018-09-04 22:00:00 +0000627 return OK;
628}
629
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400630status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400631 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400632 if (!hasVkContext()) {
633 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
634 return INVALID_OPERATION;
635 }
636
Greg Daniel26e0dca2018-09-18 10:33:19 -0400637 VkExportSemaphoreCreateInfo exportInfo;
638 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
639 exportInfo.pNext = nullptr;
640 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
641
642 VkSemaphoreCreateInfo semaphoreInfo;
643 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
644 semaphoreInfo.pNext = &exportInfo;
645 semaphoreInfo.flags = 0;
646 VkSemaphore semaphore;
647 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
648 if (VK_SUCCESS != err) {
649 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
650 return INVALID_OPERATION;
651 }
652
Greg Danield92a9b12019-04-23 10:11:04 -0400653 GrBackendSemaphore backendSemaphore;
654 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400655
Stan Ilievaaa9e832019-09-17 14:07:23 -0400656 DestroySemaphoreInfo* destroyInfo =
657 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400658 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
659 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
660 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400661 GrFlushInfo flushInfo;
662 flushInfo.fNumSemaphores = 1;
663 flushInfo.fSignalSemaphores = &backendSemaphore;
664 flushInfo.fFinishedProc = destroy_semaphore;
665 flushInfo.fFinishedContext = destroyInfo;
666 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
667 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400668
Greg Danield92a9b12019-04-23 10:11:04 -0400669 if (submitted == GrSemaphoresSubmitted::kNo) {
670 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400671 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400672 return INVALID_OPERATION;
673 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400674
675 VkSemaphoreGetFdInfoKHR getFdInfo;
676 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
677 getFdInfo.pNext = nullptr;
678 getFdInfo.semaphore = semaphore;
679 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
680
681 int fenceFd = 0;
682
683 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400684 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400685 if (VK_SUCCESS != err) {
686 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
687 return INVALID_OPERATION;
688 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400689 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400690
Stan Iliev564ca3e2018-09-04 22:00:00 +0000691 return OK;
692}
693
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500694} /* namespace renderthread */
695} /* namespace uirenderer */
696} /* namespace android */