blob: e706eb083ab66661ab0dc94a770752d2afe019a4 [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>
Alec Mouri219997a2023-05-23 17:25:19 +000026#include <gui/TraceUtils.h>
27#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Brian Osman734d8102023-08-29 18:07:56 +000028#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
Kevin Lubickf6b21952023-10-13 13:12:17 +000029#include <include/gpu/ganesh/vk/GrVkDirectContext.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000030#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040031#include <vk/GrVkExtensions.h>
32#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050033
Greg Danielcd558522016-11-17 13:31:40 -050034#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050035#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050036#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050037#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050039namespace android {
40namespace uirenderer {
41namespace renderthread {
42
Mattias Simonssone545f962023-07-17 08:03:14 +000043static std::array<std::string_view, 20> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040044 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
45 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
46 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
47 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
48 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
49 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
50 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
51 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
52 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
53 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
54 VK_KHR_SURFACE_EXTENSION_NAME,
55 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
56 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040057 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040058 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
59 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
60 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
61 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
62 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Mattias Simonssone545f962023-07-17 08:03:14 +000063 VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040064};
65
66static bool shouldEnableExtension(const std::string_view& extension) {
67 for (const auto& it : sEnableExtensions) {
68 if (it == extension) {
69 return true;
70 }
71 }
72 return false;
73}
74
Bo Liu7b8c1eb2019-01-08 20:17:55 -080075static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
76 // All Vulkan structs that could be part of the features chain will start with the
77 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
78 // so we can get access to the pNext for the next struct.
79 struct CommonVulkanHeader {
80 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070081 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080082 };
83
84 void* pNext = features.pNext;
85 while (pNext) {
86 void* current = pNext;
87 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
88 free(current);
89 }
90}
91
Greg Daniel2ff202712018-06-14 11:50:10 -040092#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
93#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
94#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050095
John Reck9fc3d272023-05-01 16:33:22 -040096// cache a weakptr to the context to enable a second thread to share the same vulkan state
97static wp<VulkanManager> sWeakInstance = nullptr;
98static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -040099
John Reck9fc3d272023-05-01 16:33:22 -0400100sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400101 std::lock_guard _lock{sLock};
102 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
103 if (!vulkanManager.get()) {
104 vulkanManager = new VulkanManager();
105 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500106 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500107
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400108 return vulkanManager;
109}
110
John Reck9fc3d272023-05-01 16:33:22 -0400111sp<VulkanManager> VulkanManager::peekInstance() {
112 std::lock_guard _lock{sLock};
113 return sWeakInstance.promote();
114}
115
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400116VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400117 if (mDevice != VK_NULL_HANDLE) {
118 mDeviceWaitIdle(mDevice);
119 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700120 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500121
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 if (mInstance != VK_NULL_HANDLE) {
123 mDestroyInstance(mInstance, nullptr);
124 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500125
Greg Daniel2ff202712018-06-14 11:50:10 -0400126 mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000127 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400128 mDevice = VK_NULL_HANDLE;
129 mPhysicalDevice = VK_NULL_HANDLE;
130 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800131 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800132 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800133 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800134 mDeviceExtensions.clear();
135 free_features_extensions_structs(mPhysicalDeviceFeatures2);
136 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400137}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500138
Stan Iliev90276c82019-02-03 18:01:02 -0500139void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400140 VkResult err;
141
142 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700143 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
144 nullptr, // pNext
145 "android framework", // pApplicationName
146 0, // applicationVersion
147 "android framework", // pEngineName
148 0, // engineVerison
149 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400150 };
151
Greg Daniel2ff202712018-06-14 11:50:10 -0400152 {
153 GET_PROC(EnumerateInstanceExtensionProperties);
154
155 uint32_t extensionCount = 0;
156 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500157 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800158 mInstanceExtensionsOwner.resize(extensionCount);
159 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
160 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500161 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400162 bool hasKHRSurfaceExtension = false;
163 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800164 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400165 if (!shouldEnableExtension(extension.extensionName)) {
166 ALOGV("Not enabling instance extension %s", extension.extensionName);
167 continue;
168 }
169 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800170 mInstanceExtensions.push_back(extension.extensionName);
171 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400172 hasKHRSurfaceExtension = true;
173 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800174 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400175 hasKHRAndroidSurfaceExtension = true;
176 }
177 }
Stan Iliev90276c82019-02-03 18:01:02 -0500178 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400179 }
180
181 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700182 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
183 nullptr, // pNext
184 0, // flags
185 &app_info, // pApplicationInfo
186 0, // enabledLayerNameCount
187 nullptr, // ppEnabledLayerNames
188 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
189 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400190 };
191
192 GET_PROC(CreateInstance);
193 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500194 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400195
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700196 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400197 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700198 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400199 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400200 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500201 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700202 GET_INST_PROC(GetPhysicalDeviceProperties);
203 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400204
205 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500206 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
207 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 // Just returning the first physical device instead of getting the whole array. Since there
209 // should only be one device on android.
210 gpuCount = 1;
211 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
212 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500213 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400214
Greg Daniel96259622018-10-01 14:42:56 -0400215 VkPhysicalDeviceProperties physDeviceProperties;
216 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500217 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400218 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400219
Greg Daniel2ff202712018-06-14 11:50:10 -0400220 // query to get the initial queue props size
Alec Mouri219997a2023-05-23 17:25:19 +0000221 uint32_t queueCount = 0;
Greg Daniel2ff202712018-06-14 11:50:10 -0400222 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500223 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400224
225 // now get the actual queue props
226 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
227 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
228
Alec Mouri219997a2023-05-23 17:25:19 +0000229 constexpr auto kRequestedQueueCount = 2;
230
Greg Daniel2ff202712018-06-14 11:50:10 -0400231 // iterate to find the graphics queue
232 mGraphicsQueueIndex = queueCount;
233 for (uint32_t i = 0; i < queueCount; i++) {
234 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
235 mGraphicsQueueIndex = i;
Alec Mouri219997a2023-05-23 17:25:19 +0000236 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400237 break;
238 }
239 }
Stan Iliev90276c82019-02-03 18:01:02 -0500240 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400241
Greg Daniel2ff202712018-06-14 11:50:10 -0400242 {
243 uint32_t extensionCount = 0;
244 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700245 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500246 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800247 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400248 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700249 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500250 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400251 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800252 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400253 if (!shouldEnableExtension(extension.extensionName)) {
254 ALOGV("Not enabling device extension %s", extension.extensionName);
255 continue;
256 }
257 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800258 mDeviceExtensions.push_back(extension.extensionName);
259 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400260 hasKHRSwapchainExtension = true;
261 }
262 }
Stan Iliev90276c82019-02-03 18:01:02 -0500263 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400264 }
265
Alec Mouri219997a2023-05-23 17:25:19 +0000266 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
267 if (device != VK_NULL_HANDLE) {
268 return vkGetDeviceProcAddr(device, proc_name);
269 }
270 return vkGetInstanceProcAddr(instance, proc_name);
271 };
272
273 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700274 mInstanceExtensions.data(), mDeviceExtensions.size(),
275 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400276
Stan Iliev90276c82019-02-03 18:01:02 -0500277 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400278
Greg Daniela227dbb2018-08-20 09:19:48 -0400279 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
280 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
281 features.pNext = nullptr;
282
283 // Setup all extension feature structs we may want to use.
284 void** tailPNext = &features.pNext;
285
286 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
287 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700288 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400289 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
290 LOG_ALWAYS_FATAL_IF(!blend);
291 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
292 blend->pNext = nullptr;
293 *tailPNext = blend;
294 tailPNext = &blend->pNext;
295 }
296
Greg Daniel05036172018-11-28 17:08:04 -0500297 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700298 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500299 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
300 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
301 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
302 ycbcrFeature->pNext = nullptr;
303 *tailPNext = ycbcrFeature;
304 tailPNext = &ycbcrFeature->pNext;
305
Greg Daniela227dbb2018-08-20 09:19:48 -0400306 // query to get the physical device features
307 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400308 // this looks like it would slow things down,
309 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400310 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400311
Alec Mouri219997a2023-05-23 17:25:19 +0000312 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400313
Stan Iliev7e733362019-02-28 13:16:36 -0500314 void* queueNextPtr = nullptr;
315
316 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
317
John Reck0fa0cbc2019-04-05 16:57:46 -0700318 if (Properties::contextPriority != 0 &&
319 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500320 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
321 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700322 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500323 queuePriorityCreateInfo.pNext = nullptr;
324 switch (Properties::contextPriority) {
325 case EGL_CONTEXT_PRIORITY_LOW_IMG:
326 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
327 break;
328 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
329 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
330 break;
331 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
332 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
333 break;
334 default:
335 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700336 }
337 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500338 }
339
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700340 const VkDeviceQueueCreateInfo queueInfo = {
341 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
342 queueNextPtr, // pNext
343 0, // VkDeviceQueueCreateFlags
344 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000345 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700346 queuePriorities, // pQueuePriorities
347 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400348
349 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700350 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
351 &features, // pNext
352 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700353 1, // queueCreateInfoCount
354 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700355 0, // layerCount
356 nullptr, // ppEnabledLayerNames
357 (uint32_t)mDeviceExtensions.size(), // extensionCount
358 mDeviceExtensions.data(), // ppEnabledExtensionNames
359 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400360 };
361
Stan Iliev90276c82019-02-03 18:01:02 -0500362 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400363
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500364 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500365 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500366 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700367 GET_DEV_PROC(CreateCommandPool);
368 GET_DEV_PROC(CreateFence);
369 GET_DEV_PROC(CreateSemaphore);
370 GET_DEV_PROC(DestroyCommandPool);
371 GET_DEV_PROC(DestroyDevice);
372 GET_DEV_PROC(DestroyFence);
373 GET_DEV_PROC(DestroySemaphore);
374 GET_DEV_PROC(DeviceWaitIdle);
375 GET_DEV_PROC(EndCommandBuffer);
376 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500377 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700378 GET_DEV_PROC(GetSemaphoreFdKHR);
379 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500380 GET_DEV_PROC(QueueSubmit);
381 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700382 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500383 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700384 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000385 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400386}
387
388void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000389 std::call_once(mInitFlag, [&] {
390 GET_PROC(EnumerateInstanceVersion);
391 uint32_t instanceVersion;
392 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
393 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400394
Alec Mouri671a9f62023-08-25 17:18:02 +0000395 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400396
Alec Mouri671a9f62023-08-25 17:18:02 +0000397 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
398 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400399
Alec Mouri671a9f62023-08-25 17:18:02 +0000400 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
401 mSwapBehavior = SwapBehavior::BufferAge;
402 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400403
Alec Mouri671a9f62023-08-25 17:18:02 +0000404 mInitialized = true;
405 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500406}
407
John Reck9fc3d272023-05-01 16:33:22 -0400408static void onGrContextReleased(void* context) {
409 VulkanManager* manager = (VulkanManager*)context;
410 manager->decStrong((void*)onGrContextReleased);
411}
Stan Iliev981afe72019-02-13 14:24:33 -0500412
John Reck9fc3d272023-05-01 16:33:22 -0400413sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
414 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000415 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
416 if (device != VK_NULL_HANDLE) {
417 return vkGetDeviceProcAddr(device, proc_name);
418 }
419 return vkGetInstanceProcAddr(instance, proc_name);
420 };
421
Stan Iliev981afe72019-02-13 14:24:33 -0500422 GrVkBackendContext backendContext;
423 backendContext.fInstance = mInstance;
424 backendContext.fPhysicalDevice = mPhysicalDevice;
425 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000426 backendContext.fQueue =
427 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500428 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
429 backendContext.fMaxAPIVersion = mAPIVersion;
430 backendContext.fVkExtensions = &mExtensions;
431 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000432 backendContext.fGetProc = std::move(getProc);
Stan Iliev981afe72019-02-13 14:24:33 -0500433
John Reck9fc3d272023-05-01 16:33:22 -0400434 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
435 this->incStrong((void*)onGrContextReleased);
436 options.fContextDeleteContext = this;
437 options.fContextDeleteProc = onGrContextReleased;
438
Kevin Lubickf6b21952023-10-13 13:12:17 +0000439 return GrDirectContexts::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500440}
441
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800442VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
443 return VkFunctorInitParams{
444 .instance = mInstance,
445 .physical_device = mPhysicalDevice,
446 .device = mDevice,
447 .queue = mGraphicsQueue,
448 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500449 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800450 .enabled_instance_extension_names = mInstanceExtensions.data(),
451 .enabled_instance_extension_names_length =
452 static_cast<uint32_t>(mInstanceExtensions.size()),
453 .enabled_device_extension_names = mDeviceExtensions.data(),
454 .enabled_device_extension_names_length =
455 static_cast<uint32_t>(mDeviceExtensions.size()),
456 .device_features_2 = &mPhysicalDeviceFeatures2,
457 };
458}
459
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500460Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500461 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
462
463 if (bufferInfo == nullptr) {
464 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
465 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500466 }
467
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500468 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500469
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500470 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400471 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
472 bool isSignalPending = false;
473 if (finfo != NULL) {
474 isSignalPending = finfo->status != 1;
475 sync_file_info_free(finfo);
476 }
477 if (isSignalPending) {
478 int fence_clone = dup(bufferInfo->dequeue_fence);
479 if (fence_clone == -1) {
480 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
481 errno);
482 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
483 } else {
484 VkSemaphoreCreateInfo semaphoreInfo;
485 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
486 semaphoreInfo.pNext = nullptr;
487 semaphoreInfo.flags = 0;
488 VkSemaphore semaphore;
489 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400490 if (err != VK_SUCCESS) {
491 ALOGE("Failed to create import semaphore, err: %d", err);
492 close(fence_clone);
493 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
494 } else {
495 VkImportSemaphoreFdInfoKHR importInfo;
496 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
497 importInfo.pNext = nullptr;
498 importInfo.semaphore = semaphore;
499 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
500 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
501 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500502
Greg Danield6670772021-06-09 12:01:12 -0400503 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
504 if (err != VK_SUCCESS) {
505 ALOGE("Failed to import semaphore, err: %d", err);
506 mDestroySemaphore(mDevice, semaphore, nullptr);
507 close(fence_clone);
508 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
509 } else {
510 GrBackendSemaphore backendSemaphore;
511 backendSemaphore.initVulkan(semaphore);
512 // Skia will take ownership of the VkSemaphore and delete it once the wait
513 // has finished. The VkSemaphore also owns the imported fd, so it will
514 // close the fd when it is deleted.
515 bufferInfo->skSurface->wait(1, &backendSemaphore);
516 // The following flush blocks the GPU immediately instead of waiting for
517 // other drawing ops. It seems dequeue_fence is not respected otherwise.
518 // TODO: remove the flush after finding why backendSemaphore is not working.
Kevin Lubickcae0b212023-09-12 18:33:10 +0000519 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface.get());
Greg Danield6670772021-06-09 12:01:12 -0400520 }
521 }
Stan Iliev197843d2019-03-21 11:34:15 -0400522 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500523 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500524 }
525
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500526 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
527 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500528}
529
Greg Danield92a9b12019-04-23 10:11:04 -0400530struct DestroySemaphoreInfo {
531 PFN_vkDestroySemaphore mDestroyFunction;
532 VkDevice mDevice;
533 VkSemaphore mSemaphore;
534
535 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400536 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400537 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
John Reck5d3fac12023-11-08 23:08:10 -0500538
539 ~DestroySemaphoreInfo() { mDestroyFunction(mDevice, mSemaphore, nullptr); }
Greg Danield92a9b12019-04-23 10:11:04 -0400540};
541
542static void destroy_semaphore(void* context) {
543 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
John Reck5d3fac12023-11-08 23:08:10 -0500544 delete info;
Greg Danield92a9b12019-04-23 10:11:04 -0400545}
546
John Reck5d3fac12023-11-08 23:08:10 -0500547VulkanManager::VkDrawResult VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500548 ATRACE_NAME("Vulkan finish frame");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400549
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500550 VkExportSemaphoreCreateInfo exportInfo;
551 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
552 exportInfo.pNext = nullptr;
553 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500554
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500555 VkSemaphoreCreateInfo semaphoreInfo;
556 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
557 semaphoreInfo.pNext = &exportInfo;
558 semaphoreInfo.flags = 0;
559 VkSemaphore semaphore;
560 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500561 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500562
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500563 GrBackendSemaphore backendSemaphore;
564 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500565
Greg Danielc7ad4082020-05-14 15:38:26 -0400566 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500567 if (err == VK_SUCCESS) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500568 flushInfo.fNumSemaphores = 1;
569 flushInfo.fSignalSemaphores = &backendSemaphore;
570 flushInfo.fFinishedProc = destroy_semaphore;
John Reck5d3fac12023-11-08 23:08:10 -0500571 flushInfo.fFinishedContext =
572 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500573 } else {
574 semaphore = VK_NULL_HANDLE;
575 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500576 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400577 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000578 GrSemaphoresSubmitted submitted = context->flush(
579 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400580 context->submit();
John Reck5d3fac12023-11-08 23:08:10 -0500581 VkDrawResult drawResult{
582 .submissionTime = systemTime(),
583 };
Greg Danielbe2803a2021-02-19 18:32:16 -0500584 if (semaphore != VK_NULL_HANDLE) {
585 if (submitted == GrSemaphoresSubmitted::kYes) {
Hugues Evrardb9510a02021-03-01 14:35:22 +0000586 if (mFrameBoundaryANDROID) {
587 // retrieve VkImage used as render target
588 VkImage image = VK_NULL_HANDLE;
589 GrBackendRenderTarget backendRenderTarget =
Kevin Lubick098ed4c2023-05-08 12:03:59 +0000590 SkSurfaces::GetBackendRenderTarget(
591 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000592 if (backendRenderTarget.isValid()) {
593 GrVkImageInfo info;
Brian Osman734d8102023-08-29 18:07:56 +0000594 if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) {
Hugues Evrardb9510a02021-03-01 14:35:22 +0000595 image = info.fImage;
596 } else {
597 ALOGE("Frame boundary: backend is not vulkan");
598 }
599 } else {
600 ALOGE("Frame boundary: invalid backend render target");
601 }
602 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
603 // it won't wait on it.
John Reck5d3fac12023-11-08 23:08:10 -0500604 mFrameBoundaryANDROID(mDevice, semaphore, image);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000605 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500606 }
John Reck5d3fac12023-11-08 23:08:10 -0500607 VkSemaphoreGetFdInfoKHR getFdInfo;
608 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
609 getFdInfo.pNext = nullptr;
610 getFdInfo.semaphore = semaphore;
611 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
612
613 int fenceFd = -1;
614 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
615 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
616 drawResult.presentFence.reset(fenceFd);
617 } else {
618 ALOGE("VulkanManager::finishFrame(): Semaphore submission failed");
619 mQueueWaitIdle(mGraphicsQueue);
Greg Danielbe2803a2021-02-19 18:32:16 -0500620 }
John Reck5d3fac12023-11-08 23:08:10 -0500621
Greg Danielbe2803a2021-02-19 18:32:16 -0500622 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000623
John Reck5d3fac12023-11-08 23:08:10 -0500624 return drawResult;
Greg Danielbe2803a2021-02-19 18:32:16 -0500625}
626
John Reck5d3fac12023-11-08 23:08:10 -0500627void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect,
628 android::base::unique_fd&& presentFence) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500629 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
630 ATRACE_NAME("Finishing GPU work");
631 mDeviceWaitIdle(mDevice);
632 }
633
John Reck5d3fac12023-11-08 23:08:10 -0500634 surface->presentCurrentBuffer(dirtyRect, presentFence.release());
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500635}
636
637void VulkanManager::destroySurface(VulkanSurface* surface) {
638 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700639 if (VK_NULL_HANDLE != mGraphicsQueue) {
640 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500641 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500642
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500643 delete surface;
644}
645
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400646VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
647 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800648 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400649 SkColorType surfaceColorType,
650 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700651 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500652 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500653 if (!window) {
654 return nullptr;
655 }
656
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500657 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700658 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500659}
660
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400661status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400662 if (!hasVkContext()) {
663 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
664 return INVALID_OPERATION;
665 }
666
Stan Iliev7a081272018-10-26 17:54:18 -0400667 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400668 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400669 if (fenceFd == -1) {
670 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
671 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000672 }
Stan Iliev7a081272018-10-26 17:54:18 -0400673
674 VkSemaphoreCreateInfo semaphoreInfo;
675 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
676 semaphoreInfo.pNext = nullptr;
677 semaphoreInfo.flags = 0;
678 VkSemaphore semaphore;
679 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
680 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400681 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400682 ALOGE("Failed to create import semaphore, err: %d", err);
683 return UNKNOWN_ERROR;
684 }
685 VkImportSemaphoreFdInfoKHR importInfo;
686 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
687 importInfo.pNext = nullptr;
688 importInfo.semaphore = semaphore;
689 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
690 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
691 importInfo.fd = fenceFd;
692
693 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
694 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400695 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400696 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400697 ALOGE("Failed to import semaphore, err: %d", err);
698 return UNKNOWN_ERROR;
699 }
700
Greg Danield92a9b12019-04-23 10:11:04 -0400701 GrBackendSemaphore beSemaphore;
702 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400703
Greg Danield6670772021-06-09 12:01:12 -0400704 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
705 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400706 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400707 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400708
Stan Iliev564ca3e2018-09-04 22:00:00 +0000709 return OK;
710}
711
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400712status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400713 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400714 if (!hasVkContext()) {
715 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
716 return INVALID_OPERATION;
717 }
718
Greg Daniel26e0dca2018-09-18 10:33:19 -0400719 VkExportSemaphoreCreateInfo exportInfo;
720 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
721 exportInfo.pNext = nullptr;
722 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
723
724 VkSemaphoreCreateInfo semaphoreInfo;
725 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
726 semaphoreInfo.pNext = &exportInfo;
727 semaphoreInfo.flags = 0;
728 VkSemaphore semaphore;
729 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
730 if (VK_SUCCESS != err) {
731 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
732 return INVALID_OPERATION;
733 }
734
Greg Danield92a9b12019-04-23 10:11:04 -0400735 GrBackendSemaphore backendSemaphore;
736 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400737
Greg Danielfd429392019-05-09 15:44:56 -0400738 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
Greg Danielc7ad4082020-05-14 15:38:26 -0400739 GrFlushInfo flushInfo;
740 flushInfo.fNumSemaphores = 1;
741 flushInfo.fSignalSemaphores = &backendSemaphore;
742 flushInfo.fFinishedProc = destroy_semaphore;
John Reck5d3fac12023-11-08 23:08:10 -0500743 flushInfo.fFinishedContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400744 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
745 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400746
Greg Danield92a9b12019-04-23 10:11:04 -0400747 if (submitted == GrSemaphoresSubmitted::kNo) {
748 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danield92a9b12019-04-23 10:11:04 -0400749 return INVALID_OPERATION;
750 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400751
752 VkSemaphoreGetFdInfoKHR getFdInfo;
753 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
754 getFdInfo.pNext = nullptr;
755 getFdInfo.semaphore = semaphore;
756 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
757
758 int fenceFd = 0;
759
760 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
761 if (VK_SUCCESS != err) {
762 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
763 return INVALID_OPERATION;
764 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400765 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400766
Stan Iliev564ca3e2018-09-04 22:00:00 +0000767 return OK;
768}
769
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500770} /* namespace renderthread */
771} /* namespace uirenderer */
772} /* namespace android */