blob: 763bc63bb5417df353f2f1745cf0ba5c46c81b84 [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>
Kevin Lubick098ed4c2023-05-08 12:03:59 +000025#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040026#include <android/sync.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000027#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040028#include <vk/GrVkExtensions.h>
29#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050030
Alec Mouriaa3e4982020-12-14 14:47:57 -080031#include <cstring>
32
rnleece9762b2021-05-21 15:40:53 -070033#include <gui/TraceUtils.h>
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
Leon Scroggins III7ccb8a42021-11-30 14:17:28 -050039#undef LOG_TAG
40#define LOG_TAG "VulkanManager"
41
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042namespace android {
43namespace uirenderer {
44namespace renderthread {
45
John Reck90b244d2023-04-28 15:41:55 -040046static std::array<std::string_view, 19> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040047 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
48 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
49 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
50 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
51 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
52 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
53 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
54 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
55 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
56 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
57 VK_KHR_SURFACE_EXTENSION_NAME,
58 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
59 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040060 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040061 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
62 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
63 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
64 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
65 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
66};
67
68static bool shouldEnableExtension(const std::string_view& extension) {
69 for (const auto& it : sEnableExtensions) {
70 if (it == extension) {
71 return true;
72 }
73 }
74 return false;
75}
76
Bo Liu7b8c1eb2019-01-08 20:17:55 -080077static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
78 // All Vulkan structs that could be part of the features chain will start with the
79 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
80 // so we can get access to the pNext for the next struct.
81 struct CommonVulkanHeader {
82 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070083 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080084 };
85
86 void* pNext = features.pNext;
87 while (pNext) {
88 void* current = pNext;
89 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
90 free(current);
91 }
92}
93
Alec Mouriaa3e4982020-12-14 14:47:57 -080094GrVkGetProc VulkanManager::sSkiaGetProp = [](const char* proc_name, VkInstance instance,
95 VkDevice device) {
96 if (device != VK_NULL_HANDLE) {
97 if (strcmp("vkQueueSubmit", proc_name) == 0) {
98 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueSubmit;
99 } else if (strcmp("vkQueueWaitIdle", proc_name) == 0) {
100 return (PFN_vkVoidFunction)VulkanManager::interceptedVkQueueWaitIdle;
101 }
102 return vkGetDeviceProcAddr(device, proc_name);
103 }
104 return vkGetInstanceProcAddr(instance, proc_name);
105};
106
Greg Daniel2ff202712018-06-14 11:50:10 -0400107#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
108#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
109#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500110
John Reck9fc3d272023-05-01 16:33:22 -0400111// cache a weakptr to the context to enable a second thread to share the same vulkan state
112static wp<VulkanManager> sWeakInstance = nullptr;
113static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400114
John Reck9fc3d272023-05-01 16:33:22 -0400115sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400116 std::lock_guard _lock{sLock};
117 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
118 if (!vulkanManager.get()) {
119 vulkanManager = new VulkanManager();
120 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500121 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500122
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400123 return vulkanManager;
124}
125
John Reck9fc3d272023-05-01 16:33:22 -0400126sp<VulkanManager> VulkanManager::peekInstance() {
127 std::lock_guard _lock{sLock};
128 return sWeakInstance.promote();
129}
130
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400131VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400132 if (mDevice != VK_NULL_HANDLE) {
133 mDeviceWaitIdle(mDevice);
134 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700135 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136
Greg Daniel2ff202712018-06-14 11:50:10 -0400137 if (mInstance != VK_NULL_HANDLE) {
138 mDestroyInstance(mInstance, nullptr);
139 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500140
Greg Daniel2ff202712018-06-14 11:50:10 -0400141 mGraphicsQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400142 mDevice = VK_NULL_HANDLE;
143 mPhysicalDevice = VK_NULL_HANDLE;
144 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800145 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800146 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800147 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800148 mDeviceExtensions.clear();
149 free_features_extensions_structs(mPhysicalDeviceFeatures2);
150 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400151}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500152
Stan Iliev90276c82019-02-03 18:01:02 -0500153void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400154 VkResult err;
155
156 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700157 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
158 nullptr, // pNext
159 "android framework", // pApplicationName
160 0, // applicationVersion
161 "android framework", // pEngineName
162 0, // engineVerison
163 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400164 };
165
Greg Daniel2ff202712018-06-14 11:50:10 -0400166 {
167 GET_PROC(EnumerateInstanceExtensionProperties);
168
169 uint32_t extensionCount = 0;
170 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500171 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800172 mInstanceExtensionsOwner.resize(extensionCount);
173 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
174 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500175 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400176 bool hasKHRSurfaceExtension = false;
177 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800178 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400179 if (!shouldEnableExtension(extension.extensionName)) {
180 ALOGV("Not enabling instance extension %s", extension.extensionName);
181 continue;
182 }
183 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800184 mInstanceExtensions.push_back(extension.extensionName);
185 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400186 hasKHRSurfaceExtension = true;
187 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800188 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400189 hasKHRAndroidSurfaceExtension = true;
190 }
191 }
Stan Iliev90276c82019-02-03 18:01:02 -0500192 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400193 }
194
195 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700196 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
197 nullptr, // pNext
198 0, // flags
199 &app_info, // pApplicationInfo
200 0, // enabledLayerNameCount
201 nullptr, // ppEnabledLayerNames
202 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
203 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400204 };
205
206 GET_PROC(CreateInstance);
207 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500208 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400209
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700210 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400211 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700212 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400213 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400214 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500215 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700216 GET_INST_PROC(GetPhysicalDeviceProperties);
217 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400218
219 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500220 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
221 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400222 // Just returning the first physical device instead of getting the whole array. Since there
223 // should only be one device on android.
224 gpuCount = 1;
225 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
226 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500227 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400228
Greg Daniel96259622018-10-01 14:42:56 -0400229 VkPhysicalDeviceProperties physDeviceProperties;
230 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500231 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400232 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400233
Greg Daniel2ff202712018-06-14 11:50:10 -0400234 // query to get the initial queue props size
235 uint32_t queueCount;
236 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500237 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400238
239 // now get the actual queue props
240 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
241 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
242
243 // iterate to find the graphics queue
244 mGraphicsQueueIndex = queueCount;
245 for (uint32_t i = 0; i < queueCount; i++) {
246 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
247 mGraphicsQueueIndex = i;
248 break;
249 }
250 }
Stan Iliev90276c82019-02-03 18:01:02 -0500251 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400252
Greg Daniel2ff202712018-06-14 11:50:10 -0400253 {
254 uint32_t extensionCount = 0;
255 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700256 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500257 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800258 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400259 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700260 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500261 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400262 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800263 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400264 if (!shouldEnableExtension(extension.extensionName)) {
265 ALOGV("Not enabling device extension %s", extension.extensionName);
266 continue;
267 }
268 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800269 mDeviceExtensions.push_back(extension.extensionName);
270 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400271 hasKHRSwapchainExtension = true;
272 }
273 }
Stan Iliev90276c82019-02-03 18:01:02 -0500274 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400275 }
276
Alec Mouriaa3e4982020-12-14 14:47:57 -0800277 grExtensions.init(sSkiaGetProp, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700278 mInstanceExtensions.data(), mDeviceExtensions.size(),
279 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400280
Stan Iliev90276c82019-02-03 18:01:02 -0500281 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400282
Greg Daniela227dbb2018-08-20 09:19:48 -0400283 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
284 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
285 features.pNext = nullptr;
286
287 // Setup all extension feature structs we may want to use.
288 void** tailPNext = &features.pNext;
289
290 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
291 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700292 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400293 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
294 LOG_ALWAYS_FATAL_IF(!blend);
295 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
296 blend->pNext = nullptr;
297 *tailPNext = blend;
298 tailPNext = &blend->pNext;
299 }
300
Greg Daniel05036172018-11-28 17:08:04 -0500301 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700302 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500303 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
304 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
305 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
306 ycbcrFeature->pNext = nullptr;
307 *tailPNext = ycbcrFeature;
308 tailPNext = &ycbcrFeature->pNext;
309
Greg Daniela227dbb2018-08-20 09:19:48 -0400310 // query to get the physical device features
311 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400312 // this looks like it would slow things down,
313 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400314 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400315
John Reck0fa0cbc2019-04-05 16:57:46 -0700316 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400317
Stan Iliev7e733362019-02-28 13:16:36 -0500318 void* queueNextPtr = nullptr;
319
320 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
321
John Reck0fa0cbc2019-04-05 16:57:46 -0700322 if (Properties::contextPriority != 0 &&
323 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500324 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
325 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700326 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500327 queuePriorityCreateInfo.pNext = nullptr;
328 switch (Properties::contextPriority) {
329 case EGL_CONTEXT_PRIORITY_LOW_IMG:
330 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
331 break;
332 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
333 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
334 break;
335 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
336 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
337 break;
338 default:
339 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700340 }
341 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500342 }
343
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700344 const VkDeviceQueueCreateInfo queueInfo = {
345 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
346 queueNextPtr, // pNext
347 0, // VkDeviceQueueCreateFlags
348 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouriaa3e4982020-12-14 14:47:57 -0800349 1, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700350 queuePriorities, // pQueuePriorities
351 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400352
353 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700354 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
355 &features, // pNext
356 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700357 1, // queueCreateInfoCount
358 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700359 0, // layerCount
360 nullptr, // ppEnabledLayerNames
361 (uint32_t)mDeviceExtensions.size(), // extensionCount
362 mDeviceExtensions.data(), // ppEnabledExtensionNames
363 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400364 };
365
Stan Iliev90276c82019-02-03 18:01:02 -0500366 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400367
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500368 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500369 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500370 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700371 GET_DEV_PROC(CreateCommandPool);
372 GET_DEV_PROC(CreateFence);
373 GET_DEV_PROC(CreateSemaphore);
374 GET_DEV_PROC(DestroyCommandPool);
375 GET_DEV_PROC(DestroyDevice);
376 GET_DEV_PROC(DestroyFence);
377 GET_DEV_PROC(DestroySemaphore);
378 GET_DEV_PROC(DeviceWaitIdle);
379 GET_DEV_PROC(EndCommandBuffer);
380 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500381 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700382 GET_DEV_PROC(GetSemaphoreFdKHR);
383 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500384 GET_DEV_PROC(QueueSubmit);
385 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700386 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500387 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700388 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000389 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400390}
391
392void VulkanManager::initialize() {
Greg Daniel1d9de712021-05-14 09:28:34 -0400393 std::lock_guard _lock{mInitializeLock};
394
Greg Daniel2ff202712018-06-14 11:50:10 -0400395 if (mDevice != VK_NULL_HANDLE) {
396 return;
397 }
398
Greg Daniela227dbb2018-08-20 09:19:48 -0400399 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500400 uint32_t instanceVersion;
401 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
402 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400403
Stan Iliev981afe72019-02-13 14:24:33 -0500404 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400405
406 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
407
Greg Danielcd558522016-11-17 13:31:40 -0500408 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
409 mSwapBehavior = SwapBehavior::BufferAge;
410 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500411}
412
John Reck9fc3d272023-05-01 16:33:22 -0400413static void onGrContextReleased(void* context) {
414 VulkanManager* manager = (VulkanManager*)context;
415 manager->decStrong((void*)onGrContextReleased);
416}
Stan Iliev981afe72019-02-13 14:24:33 -0500417
John Reck9fc3d272023-05-01 16:33:22 -0400418sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
419 ContextType contextType) {
Stan Iliev981afe72019-02-13 14:24:33 -0500420 GrVkBackendContext backendContext;
421 backendContext.fInstance = mInstance;
422 backendContext.fPhysicalDevice = mPhysicalDevice;
423 backendContext.fDevice = mDevice;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800424 backendContext.fQueue = mGraphicsQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500425 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
426 backendContext.fMaxAPIVersion = mAPIVersion;
427 backendContext.fVkExtensions = &mExtensions;
428 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800429 backendContext.fGetProc = sSkiaGetProp;
Stan Iliev981afe72019-02-13 14:24:33 -0500430
John Reck9fc3d272023-05-01 16:33:22 -0400431 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
432 this->incStrong((void*)onGrContextReleased);
433 options.fContextDeleteContext = this;
434 options.fContextDeleteProc = onGrContextReleased;
435
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400436 return GrDirectContext::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500437}
438
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800439VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
440 return VkFunctorInitParams{
441 .instance = mInstance,
442 .physical_device = mPhysicalDevice,
443 .device = mDevice,
444 .queue = mGraphicsQueue,
445 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500446 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800447 .enabled_instance_extension_names = mInstanceExtensions.data(),
448 .enabled_instance_extension_names_length =
449 static_cast<uint32_t>(mInstanceExtensions.size()),
450 .enabled_device_extension_names = mDeviceExtensions.data(),
451 .enabled_device_extension_names_length =
452 static_cast<uint32_t>(mDeviceExtensions.size()),
453 .device_features_2 = &mPhysicalDeviceFeatures2,
454 };
455}
456
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500457Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500458 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
459
460 if (bufferInfo == nullptr) {
461 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
462 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500463 }
464
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500465 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500466
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500467 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400468 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
469 bool isSignalPending = false;
470 if (finfo != NULL) {
471 isSignalPending = finfo->status != 1;
472 sync_file_info_free(finfo);
473 }
474 if (isSignalPending) {
475 int fence_clone = dup(bufferInfo->dequeue_fence);
476 if (fence_clone == -1) {
477 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
478 errno);
479 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
480 } else {
481 VkSemaphoreCreateInfo semaphoreInfo;
482 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
483 semaphoreInfo.pNext = nullptr;
484 semaphoreInfo.flags = 0;
485 VkSemaphore semaphore;
486 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400487 if (err != VK_SUCCESS) {
488 ALOGE("Failed to create import semaphore, err: %d", err);
489 close(fence_clone);
490 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
491 } else {
492 VkImportSemaphoreFdInfoKHR importInfo;
493 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
494 importInfo.pNext = nullptr;
495 importInfo.semaphore = semaphore;
496 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
497 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
498 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500499
Greg Danield6670772021-06-09 12:01:12 -0400500 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
501 if (err != VK_SUCCESS) {
502 ALOGE("Failed to import semaphore, err: %d", err);
503 mDestroySemaphore(mDevice, semaphore, nullptr);
504 close(fence_clone);
505 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
506 } else {
507 GrBackendSemaphore backendSemaphore;
508 backendSemaphore.initVulkan(semaphore);
509 // Skia will take ownership of the VkSemaphore and delete it once the wait
510 // has finished. The VkSemaphore also owns the imported fd, so it will
511 // close the fd when it is deleted.
512 bufferInfo->skSurface->wait(1, &backendSemaphore);
513 // The following flush blocks the GPU immediately instead of waiting for
514 // other drawing ops. It seems dequeue_fence is not respected otherwise.
515 // TODO: remove the flush after finding why backendSemaphore is not working.
516 bufferInfo->skSurface->flushAndSubmit();
517 }
518 }
Stan Iliev197843d2019-03-21 11:34:15 -0400519 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500520 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500521 }
522
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500523 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
524 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500525}
526
Greg Danield92a9b12019-04-23 10:11:04 -0400527struct DestroySemaphoreInfo {
528 PFN_vkDestroySemaphore mDestroyFunction;
529 VkDevice mDevice;
530 VkSemaphore mSemaphore;
Greg Danielfd429392019-05-09 15:44:56 -0400531 // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia
532 // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one
533 // owned by Skia and one owned by the VulkanManager. The refs are decremented each time
534 // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is
535 // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager
536 // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be.
537 int mRefs = 2;
Greg Danield92a9b12019-04-23 10:11:04 -0400538
539 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
Stan Ilievaaa9e832019-09-17 14:07:23 -0400540 VkSemaphore semaphore)
Greg Danield92a9b12019-04-23 10:11:04 -0400541 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
542};
543
544static void destroy_semaphore(void* context) {
545 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
Greg Danielfd429392019-05-09 15:44:56 -0400546 --info->mRefs;
547 if (!info->mRefs) {
548 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
549 delete info;
550 }
Greg Danield92a9b12019-04-23 10:11:04 -0400551}
552
Alec Mouri3afb3972022-05-27 22:03:11 +0000553nsecs_t VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500554 ATRACE_NAME("Vulkan finish frame");
555 ALOGE_IF(mSwapSemaphore != VK_NULL_HANDLE || mDestroySemaphoreContext != nullptr,
556 "finishFrame already has an outstanding semaphore");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400557
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500558 VkExportSemaphoreCreateInfo exportInfo;
559 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
560 exportInfo.pNext = nullptr;
561 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500562
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500563 VkSemaphoreCreateInfo semaphoreInfo;
564 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
565 semaphoreInfo.pNext = &exportInfo;
566 semaphoreInfo.flags = 0;
567 VkSemaphore semaphore;
568 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danielbe2803a2021-02-19 18:32:16 -0500569 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500570
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500571 GrBackendSemaphore backendSemaphore;
572 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500573
Greg Danielc7ad4082020-05-14 15:38:26 -0400574 GrFlushInfo flushInfo;
Greg Danielbe2803a2021-02-19 18:32:16 -0500575 if (err == VK_SUCCESS) {
576 mDestroySemaphoreContext = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
577 flushInfo.fNumSemaphores = 1;
578 flushInfo.fSignalSemaphores = &backendSemaphore;
579 flushInfo.fFinishedProc = destroy_semaphore;
580 flushInfo.fFinishedContext = mDestroySemaphoreContext;
581 } else {
582 semaphore = VK_NULL_HANDLE;
583 }
584 GrSemaphoresSubmitted submitted =
585 surface->flush(SkSurface::BackendSurfaceAccess::kPresent, flushInfo);
586 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400587 ALOGE_IF(!context, "Surface is not backed by gpu");
588 context->submit();
Alec Mouri3afb3972022-05-27 22:03:11 +0000589 const nsecs_t submissionTime = systemTime();
Greg Danielbe2803a2021-02-19 18:32:16 -0500590 if (semaphore != VK_NULL_HANDLE) {
591 if (submitted == GrSemaphoresSubmitted::kYes) {
592 mSwapSemaphore = semaphore;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000593 if (mFrameBoundaryANDROID) {
594 // retrieve VkImage used as render target
595 VkImage image = VK_NULL_HANDLE;
596 GrBackendRenderTarget backendRenderTarget =
Kevin Lubick098ed4c2023-05-08 12:03:59 +0000597 SkSurfaces::GetBackendRenderTarget(
598 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000599 if (backendRenderTarget.isValid()) {
600 GrVkImageInfo info;
601 if (backendRenderTarget.getVkImageInfo(&info)) {
602 image = info.fImage;
603 } else {
604 ALOGE("Frame boundary: backend is not vulkan");
605 }
606 } else {
607 ALOGE("Frame boundary: invalid backend render target");
608 }
609 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
610 // it won't wait on it.
611 mFrameBoundaryANDROID(mDevice, mSwapSemaphore, image);
612 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500613 } else {
614 destroy_semaphore(mDestroySemaphoreContext);
615 mDestroySemaphoreContext = nullptr;
616 }
617 }
618 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000619
620 return submissionTime;
Greg Danielbe2803a2021-02-19 18:32:16 -0500621}
622
623void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
624 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
625 ATRACE_NAME("Finishing GPU work");
626 mDeviceWaitIdle(mDevice);
627 }
628
629 int fenceFd = -1;
630 if (mSwapSemaphore != VK_NULL_HANDLE) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500631 VkSemaphoreGetFdInfoKHR getFdInfo;
632 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
633 getFdInfo.pNext = nullptr;
Greg Danielbe2803a2021-02-19 18:32:16 -0500634 getFdInfo.semaphore = mSwapSemaphore;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500635 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500636
Greg Danielbe2803a2021-02-19 18:32:16 -0500637 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500638 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
639 } else {
640 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
Alec Mouriaa3e4982020-12-14 14:47:57 -0800641
642 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500643 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500644 }
Greg Daniele8dc3972021-07-15 13:38:44 -0400645 if (mDestroySemaphoreContext) {
646 destroy_semaphore(mDestroySemaphoreContext);
647 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500648
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500649 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Greg Danielbe2803a2021-02-19 18:32:16 -0500650 mSwapSemaphore = VK_NULL_HANDLE;
651 mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500652}
653
654void VulkanManager::destroySurface(VulkanSurface* surface) {
655 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700656 if (VK_NULL_HANDLE != mGraphicsQueue) {
Alec Mouriaa3e4982020-12-14 14:47:57 -0800657 std::lock_guard<std::mutex> lock(mGraphicsQueueMutex);
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700658 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500659 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400660 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500661
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500662 delete surface;
663}
664
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400665VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
666 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800667 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400668 SkColorType surfaceColorType,
669 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700670 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500671 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500672 if (!window) {
673 return nullptr;
674 }
675
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500676 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700677 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500678}
679
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400680status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400681 if (!hasVkContext()) {
682 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
683 return INVALID_OPERATION;
684 }
685
Stan Iliev7a081272018-10-26 17:54:18 -0400686 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400687 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400688 if (fenceFd == -1) {
689 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
690 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000691 }
Stan Iliev7a081272018-10-26 17:54:18 -0400692
693 VkSemaphoreCreateInfo semaphoreInfo;
694 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
695 semaphoreInfo.pNext = nullptr;
696 semaphoreInfo.flags = 0;
697 VkSemaphore semaphore;
698 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
699 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400700 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400701 ALOGE("Failed to create import semaphore, err: %d", err);
702 return UNKNOWN_ERROR;
703 }
704 VkImportSemaphoreFdInfoKHR importInfo;
705 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
706 importInfo.pNext = nullptr;
707 importInfo.semaphore = semaphore;
708 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
709 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
710 importInfo.fd = fenceFd;
711
712 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
713 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400714 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400715 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400716 ALOGE("Failed to import semaphore, err: %d", err);
717 return UNKNOWN_ERROR;
718 }
719
Greg Danield92a9b12019-04-23 10:11:04 -0400720 GrBackendSemaphore beSemaphore;
721 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400722
Greg Danield6670772021-06-09 12:01:12 -0400723 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
724 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400725 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400726 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400727
Stan Iliev564ca3e2018-09-04 22:00:00 +0000728 return OK;
729}
730
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400731status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400732 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400733 if (!hasVkContext()) {
734 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
735 return INVALID_OPERATION;
736 }
737
Greg Daniel26e0dca2018-09-18 10:33:19 -0400738 VkExportSemaphoreCreateInfo exportInfo;
739 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
740 exportInfo.pNext = nullptr;
741 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
742
743 VkSemaphoreCreateInfo semaphoreInfo;
744 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
745 semaphoreInfo.pNext = &exportInfo;
746 semaphoreInfo.flags = 0;
747 VkSemaphore semaphore;
748 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
749 if (VK_SUCCESS != err) {
750 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
751 return INVALID_OPERATION;
752 }
753
Greg Danield92a9b12019-04-23 10:11:04 -0400754 GrBackendSemaphore backendSemaphore;
755 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400756
Stan Ilievaaa9e832019-09-17 14:07:23 -0400757 DestroySemaphoreInfo* destroyInfo =
758 new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
Greg Danielfd429392019-05-09 15:44:56 -0400759 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
760 // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
761 // when it is done with the semaphore.
Greg Danielc7ad4082020-05-14 15:38:26 -0400762 GrFlushInfo flushInfo;
763 flushInfo.fNumSemaphores = 1;
764 flushInfo.fSignalSemaphores = &backendSemaphore;
765 flushInfo.fFinishedProc = destroy_semaphore;
766 flushInfo.fFinishedContext = destroyInfo;
767 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
768 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400769
Greg Danield92a9b12019-04-23 10:11:04 -0400770 if (submitted == GrSemaphoresSubmitted::kNo) {
771 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danielfd429392019-05-09 15:44:56 -0400772 destroy_semaphore(destroyInfo);
Greg Danield92a9b12019-04-23 10:11:04 -0400773 return INVALID_OPERATION;
774 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400775
776 VkSemaphoreGetFdInfoKHR getFdInfo;
777 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
778 getFdInfo.pNext = nullptr;
779 getFdInfo.semaphore = semaphore;
780 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
781
782 int fenceFd = 0;
783
784 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
Greg Danielfd429392019-05-09 15:44:56 -0400785 destroy_semaphore(destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400786 if (VK_SUCCESS != err) {
787 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
788 return INVALID_OPERATION;
789 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400790 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400791
Stan Iliev564ca3e2018-09-04 22:00:00 +0000792 return OK;
793}
794
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500795} /* namespace renderthread */
796} /* namespace uirenderer */
797} /* namespace android */