blob: a9ff2c60fdbe46c2a7c39c1843d81aa8556e4cf8 [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
rnleece9762b2021-05-21 15:40:53 -070032#include <gui/TraceUtils.h>
Greg Danielcd558522016-11-17 13:31:40 -050033#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050035#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050036#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037
Leon Scroggins III7ccb8a42021-11-30 14:17:28 -050038#undef LOG_TAG
39#define LOG_TAG "VulkanManager"
40
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050041namespace android {
42namespace uirenderer {
43namespace renderthread {
44
Bo Liu7b8c1eb2019-01-08 20:17:55 -080045static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
46 // All Vulkan structs that could be part of the features chain will start with the
47 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
48 // so we can get access to the pNext for the next struct.
49 struct CommonVulkanHeader {
50 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070051 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080052 };
53
54 void* pNext = features.pNext;
55 while (pNext) {
56 void* current = pNext;
57 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
58 free(current);
59 }
60}
61
Alec Mouriaa3e4982020-12-14 14:47:57 -080062GrVkGetProc VulkanManager::sSkiaGetProp = [](const char* proc_name, VkInstance instance,
63 VkDevice device) {
64 if (device != VK_NULL_HANDLE) {
65 if (strcmp("vkQueueSubmit", proc_name) == 0) {
66 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueSubmit;
67 } else if (strcmp("vkQueueWaitIdle", proc_name) == 0) {
68 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueWaitIdle;
69 }
70 return vkGetDeviceProcAddr(device, proc_name);
71 }
72 return vkGetInstanceProcAddr(instance, proc_name);
73};
74
Greg Daniel2ff202712018-06-14 11:50:10 -040075#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
76#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
77#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078
Derek Sollenberger802fefa2020-08-13 16:53:30 -040079sp<VulkanManager> VulkanManager::getInstance() {
80 // cache a weakptr to the context to enable a second thread to share the same vulkan state
81 static wp<VulkanManager> sWeakInstance = nullptr;
82 static std::mutex sLock;
83
84 std::lock_guard _lock{sLock};
85 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
86 if (!vulkanManager.get()) {
87 vulkanManager = new VulkanManager();
88 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050089 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050090
Derek Sollenberger802fefa2020-08-13 16:53:30 -040091 return vulkanManager;
92}
93
94VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -040095 if (mDevice != VK_NULL_HANDLE) {
96 mDeviceWaitIdle(mDevice);
97 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070098 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050099
Greg Daniel2ff202712018-06-14 11:50:10 -0400100 if (mInstance != VK_NULL_HANDLE) {
101 mDestroyInstance(mInstance, nullptr);
102 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500103
Greg Daniel2ff202712018-06-14 11:50:10 -0400104 mGraphicsQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400105 mDevice = VK_NULL_HANDLE;
106 mPhysicalDevice = VK_NULL_HANDLE;
107 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800108 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800109 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800110 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800111 mDeviceExtensions.clear();
112 free_features_extensions_structs(mPhysicalDeviceFeatures2);
113 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400114}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500115
Stan Iliev90276c82019-02-03 18:01:02 -0500116void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400117 VkResult err;
118
119 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700120 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
121 nullptr, // pNext
122 "android framework", // pApplicationName
123 0, // applicationVersion
124 "android framework", // pEngineName
125 0, // engineVerison
126 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 };
128
Greg Daniel2ff202712018-06-14 11:50:10 -0400129 {
130 GET_PROC(EnumerateInstanceExtensionProperties);
131
132 uint32_t extensionCount = 0;
133 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500134 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800135 mInstanceExtensionsOwner.resize(extensionCount);
136 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
137 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500138 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400139 bool hasKHRSurfaceExtension = false;
140 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800141 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
142 mInstanceExtensions.push_back(extension.extensionName);
143 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400144 hasKHRSurfaceExtension = true;
145 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800146 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400147 hasKHRAndroidSurfaceExtension = true;
148 }
149 }
Stan Iliev90276c82019-02-03 18:01:02 -0500150 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400151 }
152
153 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700154 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
155 nullptr, // pNext
156 0, // flags
157 &app_info, // pApplicationInfo
158 0, // enabledLayerNameCount
159 nullptr, // ppEnabledLayerNames
160 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
161 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400162 };
163
164 GET_PROC(CreateInstance);
165 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500166 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400167
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700168 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400169 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700170 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400171 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400172 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500173 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700174 GET_INST_PROC(GetPhysicalDeviceProperties);
175 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400176
177 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500178 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
179 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400180 // Just returning the first physical device instead of getting the whole array. Since there
181 // should only be one device on android.
182 gpuCount = 1;
183 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
184 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500185 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400186
Greg Daniel96259622018-10-01 14:42:56 -0400187 VkPhysicalDeviceProperties physDeviceProperties;
188 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500189 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400190 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400191
Greg Daniel2ff202712018-06-14 11:50:10 -0400192 // query to get the initial queue props size
193 uint32_t queueCount;
194 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500195 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400196
197 // now get the actual queue props
198 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
199 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
200
201 // iterate to find the graphics queue
202 mGraphicsQueueIndex = queueCount;
203 for (uint32_t i = 0; i < queueCount; i++) {
204 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
205 mGraphicsQueueIndex = i;
206 break;
207 }
208 }
Stan Iliev90276c82019-02-03 18:01:02 -0500209 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400210
Greg Daniel2ff202712018-06-14 11:50:10 -0400211 {
212 uint32_t extensionCount = 0;
213 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700214 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500215 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800216 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400217 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700218 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500219 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400220 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800221 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
222 mDeviceExtensions.push_back(extension.extensionName);
223 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400224 hasKHRSwapchainExtension = true;
225 }
226 }
Stan Iliev90276c82019-02-03 18:01:02 -0500227 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400228 }
229
Alec Mouriaa3e4982020-12-14 14:47:57 -0800230 grExtensions.init(sSkiaGetProp, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700231 mInstanceExtensions.data(), mDeviceExtensions.size(),
232 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400233
Stan Iliev90276c82019-02-03 18:01:02 -0500234 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400235
Greg Daniela227dbb2018-08-20 09:19:48 -0400236 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
237 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
238 features.pNext = nullptr;
239
240 // Setup all extension feature structs we may want to use.
241 void** tailPNext = &features.pNext;
242
243 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
244 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700245 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400246 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
247 LOG_ALWAYS_FATAL_IF(!blend);
248 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
249 blend->pNext = nullptr;
250 *tailPNext = blend;
251 tailPNext = &blend->pNext;
252 }
253
Greg Daniel05036172018-11-28 17:08:04 -0500254 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700255 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500256 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
257 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
258 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
259 ycbcrFeature->pNext = nullptr;
260 *tailPNext = ycbcrFeature;
261 tailPNext = &ycbcrFeature->pNext;
262
Greg Daniela227dbb2018-08-20 09:19:48 -0400263 // query to get the physical device features
264 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400265 // this looks like it would slow things down,
266 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400267 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400268
John Reck0fa0cbc2019-04-05 16:57:46 -0700269 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400270
Stan Iliev7e733362019-02-28 13:16:36 -0500271 void* queueNextPtr = nullptr;
272
273 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
274
John Reck0fa0cbc2019-04-05 16:57:46 -0700275 if (Properties::contextPriority != 0 &&
276 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500277 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
278 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700279 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500280 queuePriorityCreateInfo.pNext = nullptr;
281 switch (Properties::contextPriority) {
282 case EGL_CONTEXT_PRIORITY_LOW_IMG:
283 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
284 break;
285 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
286 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
287 break;
288 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
289 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
290 break;
291 default:
292 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700293 }
294 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500295 }
296
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700297 const VkDeviceQueueCreateInfo queueInfo = {
298 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
299 queueNextPtr, // pNext
300 0, // VkDeviceQueueCreateFlags
301 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouriaa3e4982020-12-14 14:47:57 -0800302 1, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700303 queuePriorities, // pQueuePriorities
304 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400305
306 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700307 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
308 &features, // pNext
309 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700310 1, // queueCreateInfoCount
311 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700312 0, // layerCount
313 nullptr, // ppEnabledLayerNames
314 (uint32_t)mDeviceExtensions.size(), // extensionCount
315 mDeviceExtensions.data(), // ppEnabledExtensionNames
316 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400317 };
318
Stan Iliev90276c82019-02-03 18:01:02 -0500319 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400320
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500321 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500322 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500323 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700324 GET_DEV_PROC(CreateCommandPool);
325 GET_DEV_PROC(CreateFence);
326 GET_DEV_PROC(CreateSemaphore);
327 GET_DEV_PROC(DestroyCommandPool);
328 GET_DEV_PROC(DestroyDevice);
329 GET_DEV_PROC(DestroyFence);
330 GET_DEV_PROC(DestroySemaphore);
331 GET_DEV_PROC(DeviceWaitIdle);
332 GET_DEV_PROC(EndCommandBuffer);
333 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500334 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700335 GET_DEV_PROC(GetSemaphoreFdKHR);
336 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500337 GET_DEV_PROC(QueueSubmit);
338 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700339 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500340 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700341 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000342 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400343}
344
345void VulkanManager::initialize() {
Greg Daniel1d9de712021-05-14 09:28:34 -0400346 std::lock_guard _lock{mInitializeLock};
347
Greg Daniel2ff202712018-06-14 11:50:10 -0400348 if (mDevice != VK_NULL_HANDLE) {
349 return;
350 }
351
Greg Daniela227dbb2018-08-20 09:19:48 -0400352 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500353 uint32_t instanceVersion;
354 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
355 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400356
Stan Iliev981afe72019-02-13 14:24:33 -0500357 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400358
359 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
360
Greg Danielcd558522016-11-17 13:31:40 -0500361 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
362 mSwapBehavior = SwapBehavior::BufferAge;
363 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500364}
365
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400366sk_sp<GrDirectContext> VulkanManager::createContext(const GrContextOptions& options,
367 ContextType contextType) {
Stan Iliev981afe72019-02-13 14:24:33 -0500368
369 GrVkBackendContext backendContext;
370 backendContext.fInstance = mInstance;
371 backendContext.fPhysicalDevice = mPhysicalDevice;
372 backendContext.fDevice = mDevice;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800373 backendContext.fQueue = mGraphicsQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500374 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
375 backendContext.fMaxAPIVersion = mAPIVersion;
376 backendContext.fVkExtensions = &mExtensions;
377 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800378 backendContext.fGetProc = sSkiaGetProp;
Stan Iliev981afe72019-02-13 14:24:33 -0500379
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400380 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500381}
382
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800383VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
384 return VkFunctorInitParams{
385 .instance = mInstance,
386 .physical_device = mPhysicalDevice,
387 .device = mDevice,
388 .queue = mGraphicsQueue,
389 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500390 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800391 .enabled_instance_extension_names = mInstanceExtensions.data(),
392 .enabled_instance_extension_names_length =
393 static_cast<uint32_t>(mInstanceExtensions.size()),
394 .enabled_device_extension_names = mDeviceExtensions.data(),
395 .enabled_device_extension_names_length =
396 static_cast<uint32_t>(mDeviceExtensions.size()),
397 .device_features_2 = &mPhysicalDeviceFeatures2,
398 };
399}
400
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500401Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500402 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
403
404 if (bufferInfo == nullptr) {
405 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
406 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500407 }
408
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500409 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500410
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500411 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400412 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
413 bool isSignalPending = false;
414 if (finfo != NULL) {
415 isSignalPending = finfo->status != 1;
416 sync_file_info_free(finfo);
417 }
418 if (isSignalPending) {
419 int fence_clone = dup(bufferInfo->dequeue_fence);
420 if (fence_clone == -1) {
421 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
422 errno);
423 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
424 } else {
425 VkSemaphoreCreateInfo semaphoreInfo;
426 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
427 semaphoreInfo.pNext = nullptr;
428 semaphoreInfo.flags = 0;
429 VkSemaphore semaphore;
430 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400431 if (err != VK_SUCCESS) {
432 ALOGE("Failed to create import semaphore, err: %d", err);
433 close(fence_clone);
434 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
435 } else {
436 VkImportSemaphoreFdInfoKHR importInfo;
437 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
438 importInfo.pNext = nullptr;
439 importInfo.semaphore = semaphore;
440 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
441 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
442 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500443
Greg Danield6670772021-06-09 12:01:12 -0400444 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
445 if (err != VK_SUCCESS) {
446 ALOGE("Failed to import semaphore, err: %d", err);
447 mDestroySemaphore(mDevice, semaphore, nullptr);
448 close(fence_clone);
449 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
450 } else {
451 GrBackendSemaphore backendSemaphore;
452 backendSemaphore.initVulkan(semaphore);
453 // Skia will take ownership of the VkSemaphore and delete it once the wait
454 // has finished. The VkSemaphore also owns the imported fd, so it will
455 // close the fd when it is deleted.
456 bufferInfo->skSurface->wait(1, &backendSemaphore);
457 // The following flush blocks the GPU immediately instead of waiting for
458 // other drawing ops. It seems dequeue_fence is not respected otherwise.
459 // TODO: remove the flush after finding why backendSemaphore is not working.
460 bufferInfo->skSurface->flushAndSubmit();
461 }
462 }
Stan Iliev197843d2019-03-21 11:34:15 -0400463 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500464 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500465 }
466
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500467 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
468 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500469}
470
Greg Danield92a9b12019-04-23 10:11:04 -0400471struct DestroySemaphoreInfo {
472 PFN_vkDestroySemaphore mDestroyFunction;
473 VkDevice mDevice;
474 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400475 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
476 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
477 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
478 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
479 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
480 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
481 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400482
483 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400484 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400485 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
486};
487
488static void destroy_semaphore(void* context) {
489 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400490 --info->mRefs;
491 if (!info->mRefs) {
492 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
493 delete info;
494 }
Greg Danield92a9b12019-04-23 10:11:04 -0400495}
496
Greg Danielbe2803a2021-02-19 18:32:16 -0500497void VulkanManager::finishFrame(SkSurface* surface) {
498 ATRACE_NAME("Vulkan finish frame");
499 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
500 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400501
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500502 VkExportSemaphoreCreateInfo exportInfo;
503 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
504 exportInfo.pNext = nullptr;
505 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500506
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500507 VkSemaphoreCreateInfo semaphoreInfo;
508 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
509 semaphoreInfo.pNext = &exportInfo;
510 semaphoreInfo.flags = 0;
511 VkSemaphore semaphore;
512 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500513 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500514
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500515 GrBackendSemaphore backendSemaphore;
516 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500517
Greg Danielc7ad4082020-05-14 15:38:26 -0400518 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500519 if (err == VK_SUCCESS) {
520 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
521 flushInfo.fNumSemaphores = 1;
522 flushInfo.fSignalSemaphores = &backendSemaphore;
523 flushInfo.fFinishedProc = destroy_semaphore;
524 flushInfo.fFinishedContext = mDestroySemaphoreContext;
525 } else {
526 semaphore = VK_NULL_HANDLE;
527 }
528 GrSemaphoresSubmitted submitted =
529 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
530 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400531 ALOGE_IF(!context, "Surface is not backed by gpu");
532 context->submit();
Greg Danielbe2803a2021-02-19 18:32:16 -0500533 if (semaphore != VK_NULL_HANDLE) {
534 if (submitted == GrSemaphoresSubmitted::kYes) {
535 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000536 if (mFrameBoundaryANDROID) {
537 // retrieve VkImage used as render target
538 VkImage image = VK_NULL_HANDLE;
539 GrBackendRenderTarget backendRenderTarget =
540 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
541 if (backendRenderTarget.isValid()) {
542 GrVkImageInfo info;
543 if (backendRenderTarget.getVkImageInfo(&info)) {
544 image = info.fImage;
545 } else {
546 ALOGE("Frame boundary: backend is not vulkan");
547 }
548 } else {
549 ALOGE("Frame boundary: invalid backend render target");
550 }
551 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
552 // it won't wait on it.
553 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
554 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500555 } else {
556 destroy_semaphore(mDestroySemaphoreContext);
557 mDestroySemaphoreContext = nullptr;
558 }
559 }
560 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
561}
562
563void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
564 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
565 ATRACE_NAME("Finishing GPU work");
566 mDeviceWaitIdle(mDevice);
567 }
568
569 int fenceFd = -1;
570 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500571 VkSemaphoreGetFdInfoKHR getFdInfo;
572 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
573 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500574 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500575 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500576
Greg Danielbe2803a2021-02-19 18:32:16 -0500577 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500578 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
579 } else {
580 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800581
582 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500583 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500584 }
Greg Daniele8dc3972021-07-15 13:38:44 -0400585 if (mDestroySemaphoreContext) {
586 destroy_semaphore(mDestroySemaphoreContext);
587 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500588
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500589 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500590 mSwapSemaphore = VK_NULL_HANDLE;
591 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500592}
593
594void VulkanManager::destroySurface(VulkanSurface* surface) {
595 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700596 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800597 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700598 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500599 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400600 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500601
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500602 delete surface;
603}
604
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400605VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
606 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800607 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400608 SkColorType surfaceColorType,
609 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700610 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500611 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500612 if (!window) {
613 return nullptr;
614 }
615
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500616 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700617 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500618}
619
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400620status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400621 if (!hasVkContext()) {
622 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
623 return INVALID_OPERATION;
624 }
625
Stan Iliev7a081272018-10-26 17:54:18 -0400626 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400627 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400628 if (fenceFd == -1) {
629 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
630 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000631 }
Stan Iliev7a081272018-10-26 17:54:18 -0400632
633 VkSemaphoreCreateInfo semaphoreInfo;
634 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
635 semaphoreInfo.pNext = nullptr;
636 semaphoreInfo.flags = 0;
637 VkSemaphore semaphore;
638 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
639 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400640 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400641 ALOGE("Failed to create import semaphore, err: %d", err);
642 return UNKNOWN_ERROR;
643 }
644 VkImportSemaphoreFdInfoKHR importInfo;
645 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
646 importInfo.pNext = nullptr;
647 importInfo.semaphore = semaphore;
648 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
649 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
650 importInfo.fd = fenceFd;
651
652 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
653 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400654 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400655 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400656 ALOGE("Failed to import semaphore, err: %d", err);
657 return UNKNOWN_ERROR;
658 }
659
Greg Danield92a9b12019-04-23 10:11:04 -0400660 GrBackendSemaphore beSemaphore;
661 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400662
Greg Danield6670772021-06-09 12:01:12 -0400663 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
664 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400665 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400666 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400667
Stan Iliev564ca3e2018-09-04 22:00:00 +0000668 return OK;
669}
670
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400671status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400672 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400673 if (!hasVkContext()) {
674 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
675 return INVALID_OPERATION;
676 }
677
Greg Daniel26e0dca2018-09-18 10:33:19 -0400678 VkExportSemaphoreCreateInfo exportInfo;
679 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
680 exportInfo.pNext = nullptr;
681 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
682
683 VkSemaphoreCreateInfo semaphoreInfo;
684 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
685 semaphoreInfo.pNext = &exportInfo;
686 semaphoreInfo.flags = 0;
687 VkSemaphore semaphore;
688 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
689 if (VK_SUCCESS != err) {
690 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
691 return INVALID_OPERATION;
692 }
693
Greg Danield92a9b12019-04-23 10:11:04 -0400694 GrBackendSemaphore backendSemaphore;
695 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400696
Stan Ilievaaa9e832019-09-17 14:07:23 -0400697 DestroySemaphoreInfo* destroyInfo =
698 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400699 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
700 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
701 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400702 GrFlushInfo flushInfo;
703 flushInfo.fNumSemaphores = 1;
704 flushInfo.fSignalSemaphores = &backendSemaphore;
705 flushInfo.fFinishedProc = destroy_semaphore;
706 flushInfo.fFinishedContext = destroyInfo;
707 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
708 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400709
Greg Danield92a9b12019-04-23 10:11:04 -0400710 if (submitted == GrSemaphoresSubmitted::kNo) {
711 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400712 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400713 return INVALID_OPERATION;
714 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400715
716 VkSemaphoreGetFdInfoKHR getFdInfo;
717 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
718 getFdInfo.pNext = nullptr;
719 getFdInfo.semaphore = semaphore;
720 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
721
722 int fenceFd = 0;
723
724 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400725 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400726 if (VK_SUCCESS != err) {
727 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
728 return INVALID_OPERATION;
729 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400730 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400731
Stan Iliev564ca3e2018-09-04 22:00:00 +0000732 return OK;
733}
734
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500735} /* namespace renderthread */
736} /* namespace uirenderer */
737} /* namespace android */