blob: 4d185c69c1722b0f78b916684a8995fe6c8f611d [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>
Kevin Lubick913e9a42024-03-07 13:14:46 +000028#include <include/gpu/ganesh/vk/GrVkBackendSemaphore.h>
Brian Osman734d8102023-08-29 18:07:56 +000029#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
Kevin Lubickf6b21952023-10-13 13:12:17 +000030#include <include/gpu/ganesh/vk/GrVkDirectContext.h>
Kaylee Lubickab1f35f2024-06-20 17:19:31 +000031#include <include/gpu/vk/VulkanBackendContext.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000032#include <ui/FatVector.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040033#include <vk/GrVkTypes.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050034
Nolan Scobieddc9c662024-01-30 17:38:42 -050035#include <sstream>
36
Greg Danielcd558522016-11-17 13:31:40 -050037#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038#include "RenderThread.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050039#include "pipeline/skia/ShaderCache.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050040#include "renderstate/RenderState.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050041
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042namespace android {
43namespace uirenderer {
44namespace renderthread {
45
Nolan Scobieddc9c662024-01-30 17:38:42 -050046// Not all of these are strictly required, but are all enabled if present.
47static std::array<std::string_view, 21> sEnableExtensions{
John Reckf6067df2023-04-11 16:27:51 -040048 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
49 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
50 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
51 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
52 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
53 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
54 VK_KHR_MAINTENANCE1_EXTENSION_NAME,
55 VK_KHR_MAINTENANCE2_EXTENSION_NAME,
56 VK_KHR_MAINTENANCE3_EXTENSION_NAME,
57 VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
58 VK_KHR_SURFACE_EXTENSION_NAME,
59 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
60 VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME,
John Reck90b244d2023-04-28 15:41:55 -040061 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040062 VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
63 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
64 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
65 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
66 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Mattias Simonssone545f962023-07-17 08:03:14 +000067 VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME,
Nolan Scobieddc9c662024-01-30 17:38:42 -050068 VK_EXT_DEVICE_FAULT_EXTENSION_NAME,
John Reckf6067df2023-04-11 16:27:51 -040069};
70
71static bool shouldEnableExtension(const std::string_view& extension) {
72 for (const auto& it : sEnableExtensions) {
73 if (it == extension) {
74 return true;
75 }
76 }
77 return false;
78}
79
Bo Liu7b8c1eb2019-01-08 20:17:55 -080080static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) {
81 // All Vulkan structs that could be part of the features chain will start with the
82 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
83 // so we can get access to the pNext for the next struct.
84 struct CommonVulkanHeader {
85 VkStructureType sType;
John Reck0fa0cbc2019-04-05 16:57:46 -070086 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080087 };
88
89 void* pNext = features.pNext;
90 while (pNext) {
91 void* current = pNext;
92 pNext = static_cast<CommonVulkanHeader*>(current)->pNext;
93 free(current);
94 }
95}
96
Greg Daniel2ff202712018-06-14 11:50:10 -040097#define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F)
98#define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F)
99#define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100
John Reck9fc3d272023-05-01 16:33:22 -0400101// cache a weakptr to the context to enable a second thread to share the same vulkan state
102static wp<VulkanManager> sWeakInstance = nullptr;
103static std::mutex sLock;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400104
John Reck9fc3d272023-05-01 16:33:22 -0400105sp<VulkanManager> VulkanManager::getInstance() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400106 std::lock_guard _lock{sLock};
107 sp<VulkanManager> vulkanManager = sWeakInstance.promote();
108 if (!vulkanManager.get()) {
109 vulkanManager = new VulkanManager();
110 sWeakInstance = vulkanManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500111 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500112
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400113 return vulkanManager;
114}
115
John Reck9fc3d272023-05-01 16:33:22 -0400116sp<VulkanManager> VulkanManager::peekInstance() {
117 std::lock_guard _lock{sLock};
118 return sWeakInstance.promote();
119}
120
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400121VulkanManager::~VulkanManager() {
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 if (mDevice != VK_NULL_HANDLE) {
123 mDeviceWaitIdle(mDevice);
124 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700125 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500126
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 if (mInstance != VK_NULL_HANDLE) {
128 mDestroyInstance(mInstance, nullptr);
129 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500130
Greg Daniel2ff202712018-06-14 11:50:10 -0400131 mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000132 mAHBUploadQueue = VK_NULL_HANDLE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400133 mDevice = VK_NULL_HANDLE;
134 mPhysicalDevice = VK_NULL_HANDLE;
135 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800136 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800137 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800138 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800139 mDeviceExtensions.clear();
140 free_features_extensions_structs(mPhysicalDeviceFeatures2);
141 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -0400142}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143
Kaylee Lubickab1f35f2024-06-20 17:19:31 +0000144void VulkanManager::setupDevice(skgpu::VulkanExtensions& grExtensions,
145 VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400146 VkResult err;
147
148 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700149 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
150 nullptr, // pNext
151 "android framework", // pApplicationName
152 0, // applicationVersion
153 "android framework", // pEngineName
154 0, // engineVerison
155 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400156 };
157
Greg Daniel2ff202712018-06-14 11:50:10 -0400158 {
159 GET_PROC(EnumerateInstanceExtensionProperties);
160
161 uint32_t extensionCount = 0;
162 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500163 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800164 mInstanceExtensionsOwner.resize(extensionCount);
165 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
166 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500167 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400168 bool hasKHRSurfaceExtension = false;
169 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800170 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400171 if (!shouldEnableExtension(extension.extensionName)) {
172 ALOGV("Not enabling instance extension %s", extension.extensionName);
173 continue;
174 }
175 ALOGV("Enabling instance extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800176 mInstanceExtensions.push_back(extension.extensionName);
177 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400178 hasKHRSurfaceExtension = true;
179 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800180 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400181 hasKHRAndroidSurfaceExtension = true;
182 }
183 }
Stan Iliev90276c82019-02-03 18:01:02 -0500184 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400185 }
186
187 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700188 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
189 nullptr, // pNext
190 0, // flags
191 &app_info, // pApplicationInfo
192 0, // enabledLayerNameCount
193 nullptr, // ppEnabledLayerNames
194 (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount
195 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400196 };
197
198 GET_PROC(CreateInstance);
199 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500200 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400201
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700202 GET_INST_PROC(CreateDevice);
Greg Daniel2ff202712018-06-14 11:50:10 -0400203 GET_INST_PROC(DestroyInstance);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700204 GET_INST_PROC(EnumerateDeviceExtensionProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400205 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniela227dbb2018-08-20 09:19:48 -0400206 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500207 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700208 GET_INST_PROC(GetPhysicalDeviceProperties);
209 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400210
211 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500212 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
213 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400214 // Just returning the first physical device instead of getting the whole array. Since there
215 // should only be one device on android.
216 gpuCount = 1;
217 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
218 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500219 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400220
Greg Daniel96259622018-10-01 14:42:56 -0400221 VkPhysicalDeviceProperties physDeviceProperties;
222 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500223 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400224 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400225
Greg Daniel2ff202712018-06-14 11:50:10 -0400226 // query to get the initial queue props size
Alec Mouri219997a2023-05-23 17:25:19 +0000227 uint32_t queueCount = 0;
Greg Daniel2ff202712018-06-14 11:50:10 -0400228 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500229 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400230
231 // now get the actual queue props
232 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
233 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
234
Alec Mouri219997a2023-05-23 17:25:19 +0000235 constexpr auto kRequestedQueueCount = 2;
236
Greg Daniel2ff202712018-06-14 11:50:10 -0400237 // iterate to find the graphics queue
238 mGraphicsQueueIndex = queueCount;
239 for (uint32_t i = 0; i < queueCount; i++) {
240 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
241 mGraphicsQueueIndex = i;
Alec Mouri219997a2023-05-23 17:25:19 +0000242 LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400243 break;
244 }
245 }
Stan Iliev90276c82019-02-03 18:01:02 -0500246 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400247
Greg Daniel2ff202712018-06-14 11:50:10 -0400248 {
249 uint32_t extensionCount = 0;
250 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700251 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500252 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800253 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400254 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700255 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500256 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400257 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800258 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
John Reckf6067df2023-04-11 16:27:51 -0400259 if (!shouldEnableExtension(extension.extensionName)) {
260 ALOGV("Not enabling device extension %s", extension.extensionName);
261 continue;
262 }
263 ALOGV("Enabling device extension %s", extension.extensionName);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800264 mDeviceExtensions.push_back(extension.extensionName);
265 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400266 hasKHRSwapchainExtension = true;
267 }
268 }
Stan Iliev90276c82019-02-03 18:01:02 -0500269 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400270 }
271
Alec Mouri219997a2023-05-23 17:25:19 +0000272 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
273 if (device != VK_NULL_HANDLE) {
274 return vkGetDeviceProcAddr(device, proc_name);
275 }
276 return vkGetInstanceProcAddr(instance, proc_name);
277 };
278
279 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700280 mInstanceExtensions.data(), mDeviceExtensions.size(),
281 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400282
Stan Iliev90276c82019-02-03 18:01:02 -0500283 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400284
Greg Daniela227dbb2018-08-20 09:19:48 -0400285 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
286 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
287 features.pNext = nullptr;
288
289 // Setup all extension feature structs we may want to use.
290 void** tailPNext = &features.pNext;
291
292 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
293 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700294 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400295 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
296 LOG_ALWAYS_FATAL_IF(!blend);
297 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
298 blend->pNext = nullptr;
299 *tailPNext = blend;
300 tailPNext = &blend->pNext;
301 }
302
Greg Daniel05036172018-11-28 17:08:04 -0500303 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700304 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500305 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
306 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
307 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
308 ycbcrFeature->pNext = nullptr;
309 *tailPNext = ycbcrFeature;
310 tailPNext = &ycbcrFeature->pNext;
311
Nolan Scobieddc9c662024-01-30 17:38:42 -0500312 if (grExtensions.hasExtension(VK_EXT_DEVICE_FAULT_EXTENSION_NAME, 1)) {
313 VkPhysicalDeviceFaultFeaturesEXT* deviceFaultFeatures =
314 new VkPhysicalDeviceFaultFeaturesEXT;
315 deviceFaultFeatures->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FAULT_FEATURES_EXT;
316 deviceFaultFeatures->pNext = nullptr;
317 *tailPNext = deviceFaultFeatures;
318 tailPNext = &deviceFaultFeatures->pNext;
319 }
320
Greg Daniela227dbb2018-08-20 09:19:48 -0400321 // query to get the physical device features
322 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400323 // this looks like it would slow things down,
324 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400325 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400326
Alec Mouri219997a2023-05-23 17:25:19 +0000327 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400328
Stan Iliev7e733362019-02-28 13:16:36 -0500329 void* queueNextPtr = nullptr;
330
331 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
332
John Reck0fa0cbc2019-04-05 16:57:46 -0700333 if (Properties::contextPriority != 0 &&
334 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500335 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
336 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700337 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500338 queuePriorityCreateInfo.pNext = nullptr;
339 switch (Properties::contextPriority) {
340 case EGL_CONTEXT_PRIORITY_LOW_IMG:
341 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
342 break;
343 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
344 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
345 break;
346 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
347 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
348 break;
349 default:
350 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700351 }
352 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500353 }
354
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700355 const VkDeviceQueueCreateInfo queueInfo = {
356 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
357 queueNextPtr, // pNext
358 0, // VkDeviceQueueCreateFlags
359 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000360 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700361 queuePriorities, // pQueuePriorities
362 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400363
364 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700365 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
366 &features, // pNext
367 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700368 1, // queueCreateInfoCount
369 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700370 0, // layerCount
371 nullptr, // ppEnabledLayerNames
372 (uint32_t)mDeviceExtensions.size(), // extensionCount
373 mDeviceExtensions.data(), // ppEnabledExtensionNames
374 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400375 };
376
Stan Iliev90276c82019-02-03 18:01:02 -0500377 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400378
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500379 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500380 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500381 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700382 GET_DEV_PROC(CreateCommandPool);
383 GET_DEV_PROC(CreateFence);
384 GET_DEV_PROC(CreateSemaphore);
385 GET_DEV_PROC(DestroyCommandPool);
386 GET_DEV_PROC(DestroyDevice);
387 GET_DEV_PROC(DestroyFence);
388 GET_DEV_PROC(DestroySemaphore);
389 GET_DEV_PROC(DeviceWaitIdle);
390 GET_DEV_PROC(EndCommandBuffer);
391 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500392 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700393 GET_DEV_PROC(GetSemaphoreFdKHR);
394 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500395 GET_DEV_PROC(QueueSubmit);
396 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700397 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500398 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700399 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000400 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400401}
402
403void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000404 std::call_once(mInitFlag, [&] {
405 GET_PROC(EnumerateInstanceVersion);
406 uint32_t instanceVersion;
407 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
408 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400409
Alec Mouri671a9f62023-08-25 17:18:02 +0000410 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400411
Alec Mouri671a9f62023-08-25 17:18:02 +0000412 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
413 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400414
Alec Mouri671a9f62023-08-25 17:18:02 +0000415 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
416 mSwapBehavior = SwapBehavior::BufferAge;
417 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400418
Alec Mouri671a9f62023-08-25 17:18:02 +0000419 mInitialized = true;
420 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500421}
422
Nolan Scobieddc9c662024-01-30 17:38:42 -0500423namespace {
424void onVkDeviceFault(const std::string& contextLabel, const std::string& description,
425 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
426 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
427 const std::vector<std::byte>& vendorBinaryData) {
428 // The final crash string should contain as much differentiating info as possible, up to 1024
429 // bytes. As this final message is constructed, the same information is also dumped to the logs
430 // but in a more verbose format. Building the crash string is unsightly, so the clearer logging
431 // statement is always placed first to give context.
432 ALOGE("VK_ERROR_DEVICE_LOST (%s context): %s", contextLabel.c_str(), description.c_str());
433 std::stringstream crashMsg;
434 crashMsg << "VK_ERROR_DEVICE_LOST (" << contextLabel;
435
436 if (!addressInfos.empty()) {
437 ALOGE("%zu VkDeviceFaultAddressInfoEXT:", addressInfos.size());
438 crashMsg << ", " << addressInfos.size() << " address info (";
439 for (VkDeviceFaultAddressInfoEXT addressInfo : addressInfos) {
440 ALOGE(" addressType: %d", (int)addressInfo.addressType);
441 ALOGE(" reportedAddress: %" PRIu64, addressInfo.reportedAddress);
442 ALOGE(" addressPrecision: %" PRIu64, addressInfo.addressPrecision);
443 crashMsg << addressInfo.addressType << ":"
444 << addressInfo.reportedAddress << ":"
445 << addressInfo.addressPrecision << ", ";
446 }
447 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
448 crashMsg << ")";
449 }
450
451 if (!vendorInfos.empty()) {
452 ALOGE("%zu VkDeviceFaultVendorInfoEXT:", vendorInfos.size());
453 crashMsg << ", " << vendorInfos.size() << " vendor info (";
454 for (VkDeviceFaultVendorInfoEXT vendorInfo : vendorInfos) {
455 ALOGE(" description: %s", vendorInfo.description);
456 ALOGE(" vendorFaultCode: %" PRIu64, vendorInfo.vendorFaultCode);
457 ALOGE(" vendorFaultData: %" PRIu64, vendorInfo.vendorFaultData);
458 // Omit descriptions for individual vendor info structs in the crash string, as the
459 // fault code and fault data fields should be enough for clustering, and the verbosity
460 // isn't worth it. Additionally, vendors may just set the general description field of
461 // the overall fault to the description of the first element in this list, and that
462 // overall description will be placed at the end of the crash string.
463 crashMsg << vendorInfo.vendorFaultCode << ":"
464 << vendorInfo.vendorFaultData << ", ";
465 }
466 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
467 crashMsg << ")";
468 }
469
470 if (!vendorBinaryData.empty()) {
471 // TODO: b/322830575 - Log in base64, or dump directly to a file that gets put in bugreports
472 ALOGE("%zu bytes of vendor-specific binary data (please notify Android's Core Graphics"
473 " Stack team if you observe this message).",
474 vendorBinaryData.size());
475 crashMsg << ", " << vendorBinaryData.size() << " bytes binary";
476 }
477
478 crashMsg << "): " << description;
479 LOG_ALWAYS_FATAL("%s", crashMsg.str().c_str());
480}
481
482void deviceLostProcRenderThread(void* callbackContext, const std::string& description,
483 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
484 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
485 const std::vector<std::byte>& vendorBinaryData) {
486 onVkDeviceFault("RenderThread", description, addressInfos, vendorInfos, vendorBinaryData);
487}
488void deviceLostProcUploadThread(void* callbackContext, const std::string& description,
489 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
490 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
491 const std::vector<std::byte>& vendorBinaryData) {
492 onVkDeviceFault("UploadThread", description, addressInfos, vendorInfos, vendorBinaryData);
493}
494} // anonymous namespace
495
John Reck9fc3d272023-05-01 16:33:22 -0400496static void onGrContextReleased(void* context) {
497 VulkanManager* manager = (VulkanManager*)context;
498 manager->decStrong((void*)onGrContextReleased);
499}
Stan Iliev981afe72019-02-13 14:24:33 -0500500
John Reck9fc3d272023-05-01 16:33:22 -0400501sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
502 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000503 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
504 if (device != VK_NULL_HANDLE) {
505 return vkGetDeviceProcAddr(device, proc_name);
506 }
507 return vkGetInstanceProcAddr(instance, proc_name);
508 };
509
Kaylee Lubickab1f35f2024-06-20 17:19:31 +0000510 skgpu::VulkanBackendContext backendContext;
Stan Iliev981afe72019-02-13 14:24:33 -0500511 backendContext.fInstance = mInstance;
512 backendContext.fPhysicalDevice = mPhysicalDevice;
513 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000514 backendContext.fQueue =
515 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500516 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
517 backendContext.fMaxAPIVersion = mAPIVersion;
518 backendContext.fVkExtensions = &mExtensions;
519 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000520 backendContext.fGetProc = std::move(getProc);
Nolan Scobieddc9c662024-01-30 17:38:42 -0500521 backendContext.fDeviceLostContext = nullptr;
522 backendContext.fDeviceLostProc = (contextType == ContextType::kRenderThread)
523 ? deviceLostProcRenderThread
524 : deviceLostProcUploadThread;
Stan Iliev981afe72019-02-13 14:24:33 -0500525
John Reck9fc3d272023-05-01 16:33:22 -0400526 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
527 this->incStrong((void*)onGrContextReleased);
528 options.fContextDeleteContext = this;
529 options.fContextDeleteProc = onGrContextReleased;
530
Kevin Lubickf6b21952023-10-13 13:12:17 +0000531 return GrDirectContexts::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500532}
533
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800534VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
535 return VkFunctorInitParams{
536 .instance = mInstance,
537 .physical_device = mPhysicalDevice,
538 .device = mDevice,
539 .queue = mGraphicsQueue,
540 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500541 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800542 .enabled_instance_extension_names = mInstanceExtensions.data(),
543 .enabled_instance_extension_names_length =
544 static_cast<uint32_t>(mInstanceExtensions.size()),
545 .enabled_device_extension_names = mDeviceExtensions.data(),
546 .enabled_device_extension_names_length =
547 static_cast<uint32_t>(mDeviceExtensions.size()),
548 .device_features_2 = &mPhysicalDeviceFeatures2,
549 };
550}
551
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500552Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500553 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
554
555 if (bufferInfo == nullptr) {
556 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
557 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500558 }
559
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500560 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500561
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500562 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400563 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
564 bool isSignalPending = false;
565 if (finfo != NULL) {
566 isSignalPending = finfo->status != 1;
567 sync_file_info_free(finfo);
568 }
569 if (isSignalPending) {
570 int fence_clone = dup(bufferInfo->dequeue_fence);
571 if (fence_clone == -1) {
572 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
573 errno);
574 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
575 } else {
576 VkSemaphoreCreateInfo semaphoreInfo;
577 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
578 semaphoreInfo.pNext = nullptr;
579 semaphoreInfo.flags = 0;
580 VkSemaphore semaphore;
581 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400582 if (err != VK_SUCCESS) {
583 ALOGE("Failed to create import semaphore, err: %d", err);
584 close(fence_clone);
585 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
586 } else {
587 VkImportSemaphoreFdInfoKHR importInfo;
588 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
589 importInfo.pNext = nullptr;
590 importInfo.semaphore = semaphore;
591 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
592 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
593 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500594
Greg Danield6670772021-06-09 12:01:12 -0400595 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
596 if (err != VK_SUCCESS) {
597 ALOGE("Failed to import semaphore, err: %d", err);
598 mDestroySemaphore(mDevice, semaphore, nullptr);
599 close(fence_clone);
600 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
601 } else {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000602 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400603 // Skia will take ownership of the VkSemaphore and delete it once the wait
604 // has finished. The VkSemaphore also owns the imported fd, so it will
605 // close the fd when it is deleted.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000606 bufferInfo->skSurface->wait(1, &beSemaphore);
Greg Danield6670772021-06-09 12:01:12 -0400607 // The following flush blocks the GPU immediately instead of waiting for
608 // other drawing ops. It seems dequeue_fence is not respected otherwise.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000609 // TODO: remove the flush after finding why beSemaphore is not working.
Kevin Lubickcae0b212023-09-12 18:33:10 +0000610 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface.get());
Greg Danield6670772021-06-09 12:01:12 -0400611 }
612 }
Stan Iliev197843d2019-03-21 11:34:15 -0400613 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500614 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500615 }
616
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500617 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
618 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500619}
620
John Reck4db23fd2023-11-10 15:31:49 -0500621class SharedSemaphoreInfo : public LightRefBase<SharedSemaphoreInfo> {
Greg Danield92a9b12019-04-23 10:11:04 -0400622 PFN_vkDestroySemaphore mDestroyFunction;
623 VkDevice mDevice;
624 VkSemaphore mSemaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500625 GrBackendSemaphore mGrBackendSemaphore;
Greg Danield92a9b12019-04-23 10:11:04 -0400626
John Reck4db23fd2023-11-10 15:31:49 -0500627 SharedSemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
628 VkSemaphore semaphore)
Priyanka Advanidfca12a2024-03-06 22:25:04 +0000629 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000630 mGrBackendSemaphore = GrBackendSemaphores::MakeVk(mSemaphore);
John Reck4db23fd2023-11-10 15:31:49 -0500631 }
John Reck5d3fac12023-11-08 23:08:10 -0500632
John Reck4db23fd2023-11-10 15:31:49 -0500633 ~SharedSemaphoreInfo() { mDestroyFunction(mDevice, mSemaphore, nullptr); }
634
635 friend class LightRefBase<SharedSemaphoreInfo>;
636 friend class sp<SharedSemaphoreInfo>;
637
638public:
639 VkSemaphore semaphore() const { return mSemaphore; }
640
641 GrBackendSemaphore* grBackendSemaphore() { return &mGrBackendSemaphore; }
Greg Danield92a9b12019-04-23 10:11:04 -0400642};
643
644static void destroy_semaphore(void* context) {
John Reck4db23fd2023-11-10 15:31:49 -0500645 SharedSemaphoreInfo* info = reinterpret_cast<SharedSemaphoreInfo*>(context);
646 info->decStrong(0);
Greg Danield92a9b12019-04-23 10:11:04 -0400647}
648
John Reck5d3fac12023-11-08 23:08:10 -0500649VulkanManager::VkDrawResult VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500650 ATRACE_NAME("Vulkan finish frame");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400651
John Reck4db23fd2023-11-10 15:31:49 -0500652 sp<SharedSemaphoreInfo> sharedSemaphore;
Greg Danielc7ad4082020-05-14 15:38:26 -0400653 GrFlushInfo flushInfo;
John Reck4db23fd2023-11-10 15:31:49 -0500654
655 {
656 VkExportSemaphoreCreateInfo exportInfo;
657 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
658 exportInfo.pNext = nullptr;
659 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
660
661 VkSemaphoreCreateInfo semaphoreInfo;
662 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
663 semaphoreInfo.pNext = &exportInfo;
664 semaphoreInfo.flags = 0;
665 VkSemaphore semaphore;
666 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
667 ALOGE_IF(VK_SUCCESS != err,
668 "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
669
670 if (err == VK_SUCCESS) {
671 sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
672 flushInfo.fNumSemaphores = 1;
673 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
674 flushInfo.fFinishedProc = destroy_semaphore;
675 sharedSemaphore->incStrong(0);
676 flushInfo.fFinishedContext = sharedSemaphore.get();
677 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500678 }
John Reck4db23fd2023-11-10 15:31:49 -0500679
Greg Danielbe2803a2021-02-19 18:32:16 -0500680 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400681 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000682 GrSemaphoresSubmitted submitted = context->flush(
683 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400684 context->submit();
John Reck5d3fac12023-11-08 23:08:10 -0500685 VkDrawResult drawResult{
686 .submissionTime = systemTime(),
687 };
John Reck4db23fd2023-11-10 15:31:49 -0500688 if (sharedSemaphore) {
689 if (submitted == GrSemaphoresSubmitted::kYes && mFrameBoundaryANDROID) {
690 // retrieve VkImage used as render target
691 VkImage image = VK_NULL_HANDLE;
692 GrBackendRenderTarget backendRenderTarget = SkSurfaces::GetBackendRenderTarget(
693 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
694 if (backendRenderTarget.isValid()) {
695 GrVkImageInfo info;
696 if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) {
697 image = info.fImage;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000698 } else {
John Reck4db23fd2023-11-10 15:31:49 -0500699 ALOGE("Frame boundary: backend is not vulkan");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000700 }
John Reck4db23fd2023-11-10 15:31:49 -0500701 } else {
702 ALOGE("Frame boundary: invalid backend render target");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000703 }
John Reck4db23fd2023-11-10 15:31:49 -0500704 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
705 // it won't wait on it.
706 mFrameBoundaryANDROID(mDevice, sharedSemaphore->semaphore(), image);
Greg Danielbe2803a2021-02-19 18:32:16 -0500707 }
John Reck5d3fac12023-11-08 23:08:10 -0500708 VkSemaphoreGetFdInfoKHR getFdInfo;
709 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
710 getFdInfo.pNext = nullptr;
John Reck4db23fd2023-11-10 15:31:49 -0500711 getFdInfo.semaphore = sharedSemaphore->semaphore();
John Reck5d3fac12023-11-08 23:08:10 -0500712 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
713
714 int fenceFd = -1;
John Reck4db23fd2023-11-10 15:31:49 -0500715 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
John Reck5d3fac12023-11-08 23:08:10 -0500716 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
717 drawResult.presentFence.reset(fenceFd);
718 } else {
719 ALOGE("VulkanManager::finishFrame(): Semaphore submission failed");
720 mQueueWaitIdle(mGraphicsQueue);
Greg Danielbe2803a2021-02-19 18:32:16 -0500721 }
John Reck5d3fac12023-11-08 23:08:10 -0500722
Greg Danielbe2803a2021-02-19 18:32:16 -0500723 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000724
John Reck5d3fac12023-11-08 23:08:10 -0500725 return drawResult;
Greg Danielbe2803a2021-02-19 18:32:16 -0500726}
727
John Reck5d3fac12023-11-08 23:08:10 -0500728void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect,
729 android::base::unique_fd&& presentFence) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500730 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
731 ATRACE_NAME("Finishing GPU work");
732 mDeviceWaitIdle(mDevice);
733 }
734
John Reck5d3fac12023-11-08 23:08:10 -0500735 surface->presentCurrentBuffer(dirtyRect, presentFence.release());
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500736}
737
738void VulkanManager::destroySurface(VulkanSurface* surface) {
739 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700740 if (VK_NULL_HANDLE != mGraphicsQueue) {
741 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500742 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500743
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500744 delete surface;
745}
746
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400747VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
748 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800749 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400750 SkColorType surfaceColorType,
751 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700752 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500753 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500754 if (!window) {
755 return nullptr;
756 }
757
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500758 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700759 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500760}
761
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400762status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400763 if (!hasVkContext()) {
764 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
765 return INVALID_OPERATION;
766 }
767
Stan Iliev7a081272018-10-26 17:54:18 -0400768 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400769 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400770 if (fenceFd == -1) {
771 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
772 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000773 }
Stan Iliev7a081272018-10-26 17:54:18 -0400774
775 VkSemaphoreCreateInfo semaphoreInfo;
776 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
777 semaphoreInfo.pNext = nullptr;
778 semaphoreInfo.flags = 0;
779 VkSemaphore semaphore;
780 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
781 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400782 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400783 ALOGE("Failed to create import semaphore, err: %d", err);
784 return UNKNOWN_ERROR;
785 }
786 VkImportSemaphoreFdInfoKHR importInfo;
787 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
788 importInfo.pNext = nullptr;
789 importInfo.semaphore = semaphore;
790 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
791 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
792 importInfo.fd = fenceFd;
793
794 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
795 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400796 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400797 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400798 ALOGE("Failed to import semaphore, err: %d", err);
799 return UNKNOWN_ERROR;
800 }
801
Kevin Lubick913e9a42024-03-07 13:14:46 +0000802 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400803
Greg Danield6670772021-06-09 12:01:12 -0400804 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
805 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400806 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400807 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400808
Stan Iliev564ca3e2018-09-04 22:00:00 +0000809 return OK;
810}
811
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400812status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400813 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400814 if (!hasVkContext()) {
815 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
816 return INVALID_OPERATION;
817 }
818
Greg Daniel26e0dca2018-09-18 10:33:19 -0400819 VkExportSemaphoreCreateInfo exportInfo;
820 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
821 exportInfo.pNext = nullptr;
822 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
823
824 VkSemaphoreCreateInfo semaphoreInfo;
825 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
826 semaphoreInfo.pNext = &exportInfo;
827 semaphoreInfo.flags = 0;
828 VkSemaphore semaphore;
829 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
830 if (VK_SUCCESS != err) {
831 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
832 return INVALID_OPERATION;
833 }
834
John Reck4db23fd2023-11-10 15:31:49 -0500835 auto sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400836
Greg Danielfd429392019-05-09 15:44:56 -0400837 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
Greg Danielc7ad4082020-05-14 15:38:26 -0400838 GrFlushInfo flushInfo;
839 flushInfo.fNumSemaphores = 1;
John Reck4db23fd2023-11-10 15:31:49 -0500840 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
Greg Danielc7ad4082020-05-14 15:38:26 -0400841 flushInfo.fFinishedProc = destroy_semaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500842 sharedSemaphore->incStrong(0);
843 flushInfo.fFinishedContext = sharedSemaphore.get();
Greg Danielc7ad4082020-05-14 15:38:26 -0400844 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
845 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400846
Greg Danield92a9b12019-04-23 10:11:04 -0400847 if (submitted == GrSemaphoresSubmitted::kNo) {
848 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danield92a9b12019-04-23 10:11:04 -0400849 return INVALID_OPERATION;
850 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400851
852 VkSemaphoreGetFdInfoKHR getFdInfo;
853 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
854 getFdInfo.pNext = nullptr;
855 getFdInfo.semaphore = semaphore;
856 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
857
858 int fenceFd = 0;
859
860 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
861 if (VK_SUCCESS != err) {
862 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
863 return INVALID_OPERATION;
864 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400865 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400866
Stan Iliev564ca3e2018-09-04 22:00:00 +0000867 return OK;
868}
869
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500870} /* namespace renderthread */
871} /* namespace uirenderer */
872} /* namespace android */