blob: e3023937964ec9a680e35880d72cf193f51b9a98 [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 <android/sync.h>
Alec Mouri219997a2023-05-23 17:25:19 +000022#include <gui/TraceUtils.h>
Nolan Scobie572d8012024-08-30 13:43:34 -040023#include <include/gpu/ganesh/GrBackendSemaphore.h>
24#include <include/gpu/ganesh/GrBackendSurface.h>
25#include <include/gpu/ganesh/GrDirectContext.h>
26#include <include/gpu/ganesh/GrTypes.h>
Alec Mouri219997a2023-05-23 17:25:19 +000027#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>
Nolan Scobie572d8012024-08-30 13:43:34 -040031#include <include/gpu/ganesh/vk/GrVkTypes.h>
Kaylee Lubickab1f35f2024-06-20 17:19:31 +000032#include <include/gpu/vk/VulkanBackendContext.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000033#include <ui/FatVector.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 Daniele77ab7a2024-09-04 15:29:52 +0000321 if (grExtensions.hasExtension(VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME, 1)) {
322 VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT* formatFeatures =
323 new VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT;
324 formatFeatures->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RGBA10X6_FORMATS_FEATURES_EXT;
325 formatFeatures->pNext = nullptr;
326 *tailPNext = formatFeatures;
327 tailPNext = &formatFeatures->pNext;
328 }
329
Greg Daniela227dbb2018-08-20 09:19:48 -0400330 // query to get the physical device features
331 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400332 // this looks like it would slow things down,
333 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400334 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400335
Alec Mouri219997a2023-05-23 17:25:19 +0000336 float queuePriorities[kRequestedQueueCount] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400337
Stan Iliev7e733362019-02-28 13:16:36 -0500338 void* queueNextPtr = nullptr;
339
340 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
341
John Reck0fa0cbc2019-04-05 16:57:46 -0700342 if (Properties::contextPriority != 0 &&
343 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500344 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
345 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700346 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500347 queuePriorityCreateInfo.pNext = nullptr;
348 switch (Properties::contextPriority) {
349 case EGL_CONTEXT_PRIORITY_LOW_IMG:
350 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
351 break;
352 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
353 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
354 break;
355 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
356 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
357 break;
358 default:
359 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700360 }
361 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500362 }
363
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700364 const VkDeviceQueueCreateInfo queueInfo = {
365 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
366 queueNextPtr, // pNext
367 0, // VkDeviceQueueCreateFlags
368 mGraphicsQueueIndex, // queueFamilyIndex
Alec Mouri219997a2023-05-23 17:25:19 +0000369 kRequestedQueueCount, // queueCount
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700370 queuePriorities, // pQueuePriorities
371 };
Greg Daniel2ff202712018-06-14 11:50:10 -0400372
373 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700374 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
375 &features, // pNext
376 0, // VkDeviceCreateFlags
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700377 1, // queueCreateInfoCount
378 &queueInfo, // pQueueCreateInfos
John Reck0fa0cbc2019-04-05 16:57:46 -0700379 0, // layerCount
380 nullptr, // ppEnabledLayerNames
381 (uint32_t)mDeviceExtensions.size(), // extensionCount
382 mDeviceExtensions.data(), // ppEnabledExtensionNames
383 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400384 };
385
Stan Iliev90276c82019-02-03 18:01:02 -0500386 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400387
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500388 GET_DEV_PROC(AllocateCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500389 GET_DEV_PROC(BeginCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500390 GET_DEV_PROC(CmdPipelineBarrier);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700391 GET_DEV_PROC(CreateCommandPool);
392 GET_DEV_PROC(CreateFence);
393 GET_DEV_PROC(CreateSemaphore);
394 GET_DEV_PROC(DestroyCommandPool);
395 GET_DEV_PROC(DestroyDevice);
396 GET_DEV_PROC(DestroyFence);
397 GET_DEV_PROC(DestroySemaphore);
398 GET_DEV_PROC(DeviceWaitIdle);
399 GET_DEV_PROC(EndCommandBuffer);
400 GET_DEV_PROC(FreeCommandBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500401 GET_DEV_PROC(GetDeviceQueue);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700402 GET_DEV_PROC(GetSemaphoreFdKHR);
403 GET_DEV_PROC(ImportSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500404 GET_DEV_PROC(QueueSubmit);
405 GET_DEV_PROC(QueueWaitIdle);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700406 GET_DEV_PROC(ResetCommandBuffer);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500407 GET_DEV_PROC(ResetFences);
Yiwei Zhang0b9f0b82019-08-15 11:33:59 -0700408 GET_DEV_PROC(WaitForFences);
Hugues Evrardb9510a02021-03-01 14:35:22 +0000409 GET_DEV_PROC(FrameBoundaryANDROID);
Greg Daniel2ff202712018-06-14 11:50:10 -0400410}
411
412void VulkanManager::initialize() {
Alec Mouri671a9f62023-08-25 17:18:02 +0000413 std::call_once(mInitFlag, [&] {
414 GET_PROC(EnumerateInstanceVersion);
415 uint32_t instanceVersion;
416 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
417 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel1d9de712021-05-14 09:28:34 -0400418
Alec Mouri671a9f62023-08-25 17:18:02 +0000419 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400420
Alec Mouri671a9f62023-08-25 17:18:02 +0000421 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
422 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue);
Greg Daniela227dbb2018-08-20 09:19:48 -0400423
Alec Mouri671a9f62023-08-25 17:18:02 +0000424 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
425 mSwapBehavior = SwapBehavior::BufferAge;
426 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400427
Alec Mouri671a9f62023-08-25 17:18:02 +0000428 mInitialized = true;
429 });
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500430}
431
Nolan Scobieddc9c662024-01-30 17:38:42 -0500432namespace {
433void onVkDeviceFault(const std::string& contextLabel, const std::string& description,
434 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
435 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
436 const std::vector<std::byte>& vendorBinaryData) {
437 // The final crash string should contain as much differentiating info as possible, up to 1024
438 // bytes. As this final message is constructed, the same information is also dumped to the logs
439 // but in a more verbose format. Building the crash string is unsightly, so the clearer logging
440 // statement is always placed first to give context.
441 ALOGE("VK_ERROR_DEVICE_LOST (%s context): %s", contextLabel.c_str(), description.c_str());
442 std::stringstream crashMsg;
443 crashMsg << "VK_ERROR_DEVICE_LOST (" << contextLabel;
444
445 if (!addressInfos.empty()) {
446 ALOGE("%zu VkDeviceFaultAddressInfoEXT:", addressInfos.size());
447 crashMsg << ", " << addressInfos.size() << " address info (";
448 for (VkDeviceFaultAddressInfoEXT addressInfo : addressInfos) {
449 ALOGE(" addressType: %d", (int)addressInfo.addressType);
450 ALOGE(" reportedAddress: %" PRIu64, addressInfo.reportedAddress);
451 ALOGE(" addressPrecision: %" PRIu64, addressInfo.addressPrecision);
452 crashMsg << addressInfo.addressType << ":"
453 << addressInfo.reportedAddress << ":"
454 << addressInfo.addressPrecision << ", ";
455 }
456 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
457 crashMsg << ")";
458 }
459
460 if (!vendorInfos.empty()) {
461 ALOGE("%zu VkDeviceFaultVendorInfoEXT:", vendorInfos.size());
462 crashMsg << ", " << vendorInfos.size() << " vendor info (";
463 for (VkDeviceFaultVendorInfoEXT vendorInfo : vendorInfos) {
464 ALOGE(" description: %s", vendorInfo.description);
465 ALOGE(" vendorFaultCode: %" PRIu64, vendorInfo.vendorFaultCode);
466 ALOGE(" vendorFaultData: %" PRIu64, vendorInfo.vendorFaultData);
467 // Omit descriptions for individual vendor info structs in the crash string, as the
468 // fault code and fault data fields should be enough for clustering, and the verbosity
469 // isn't worth it. Additionally, vendors may just set the general description field of
470 // the overall fault to the description of the first element in this list, and that
471 // overall description will be placed at the end of the crash string.
472 crashMsg << vendorInfo.vendorFaultCode << ":"
473 << vendorInfo.vendorFaultData << ", ";
474 }
475 crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", "
476 crashMsg << ")";
477 }
478
479 if (!vendorBinaryData.empty()) {
480 // TODO: b/322830575 - Log in base64, or dump directly to a file that gets put in bugreports
481 ALOGE("%zu bytes of vendor-specific binary data (please notify Android's Core Graphics"
482 " Stack team if you observe this message).",
483 vendorBinaryData.size());
484 crashMsg << ", " << vendorBinaryData.size() << " bytes binary";
485 }
486
487 crashMsg << "): " << description;
488 LOG_ALWAYS_FATAL("%s", crashMsg.str().c_str());
489}
490
491void deviceLostProcRenderThread(void* callbackContext, const std::string& description,
492 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
493 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
494 const std::vector<std::byte>& vendorBinaryData) {
495 onVkDeviceFault("RenderThread", description, addressInfos, vendorInfos, vendorBinaryData);
496}
497void deviceLostProcUploadThread(void* callbackContext, const std::string& description,
498 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
499 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
500 const std::vector<std::byte>& vendorBinaryData) {
501 onVkDeviceFault("UploadThread", description, addressInfos, vendorInfos, vendorBinaryData);
502}
503} // anonymous namespace
504
John Reck9fc3d272023-05-01 16:33:22 -0400505static void onGrContextReleased(void* context) {
506 VulkanManager* manager = (VulkanManager*)context;
507 manager->decStrong((void*)onGrContextReleased);
508}
Stan Iliev981afe72019-02-13 14:24:33 -0500509
John Reck9fc3d272023-05-01 16:33:22 -0400510sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options,
511 ContextType contextType) {
Alec Mouri219997a2023-05-23 17:25:19 +0000512 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
513 if (device != VK_NULL_HANDLE) {
514 return vkGetDeviceProcAddr(device, proc_name);
515 }
516 return vkGetInstanceProcAddr(instance, proc_name);
517 };
518
Kaylee Lubickab1f35f2024-06-20 17:19:31 +0000519 skgpu::VulkanBackendContext backendContext;
Stan Iliev981afe72019-02-13 14:24:33 -0500520 backendContext.fInstance = mInstance;
521 backendContext.fPhysicalDevice = mPhysicalDevice;
522 backendContext.fDevice = mDevice;
Alec Mouri219997a2023-05-23 17:25:19 +0000523 backendContext.fQueue =
524 (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue;
Stan Iliev981afe72019-02-13 14:24:33 -0500525 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
526 backendContext.fMaxAPIVersion = mAPIVersion;
527 backendContext.fVkExtensions = &mExtensions;
528 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
Alec Mouri219997a2023-05-23 17:25:19 +0000529 backendContext.fGetProc = std::move(getProc);
Nolan Scobieddc9c662024-01-30 17:38:42 -0500530 backendContext.fDeviceLostContext = nullptr;
531 backendContext.fDeviceLostProc = (contextType == ContextType::kRenderThread)
532 ? deviceLostProcRenderThread
533 : deviceLostProcUploadThread;
Stan Iliev981afe72019-02-13 14:24:33 -0500534
John Reck9fc3d272023-05-01 16:33:22 -0400535 LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!");
536 this->incStrong((void*)onGrContextReleased);
537 options.fContextDeleteContext = this;
538 options.fContextDeleteProc = onGrContextReleased;
539
Kevin Lubickf6b21952023-10-13 13:12:17 +0000540 return GrDirectContexts::MakeVulkan(backendContext, options);
Stan Iliev981afe72019-02-13 14:24:33 -0500541}
542
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800543VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
544 return VkFunctorInitParams{
545 .instance = mInstance,
546 .physical_device = mPhysicalDevice,
547 .device = mDevice,
548 .queue = mGraphicsQueue,
549 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500550 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800551 .enabled_instance_extension_names = mInstanceExtensions.data(),
552 .enabled_instance_extension_names_length =
553 static_cast<uint32_t>(mInstanceExtensions.size()),
554 .enabled_device_extension_names = mDeviceExtensions.data(),
555 .enabled_device_extension_names_length =
556 static_cast<uint32_t>(mDeviceExtensions.size()),
557 .device_features_2 = &mPhysicalDeviceFeatures2,
558 };
559}
560
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500561Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500562 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
563
564 if (bufferInfo == nullptr) {
565 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
566 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500567 }
568
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500569 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500570
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500571 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400572 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
573 bool isSignalPending = false;
574 if (finfo != NULL) {
575 isSignalPending = finfo->status != 1;
576 sync_file_info_free(finfo);
577 }
578 if (isSignalPending) {
579 int fence_clone = dup(bufferInfo->dequeue_fence);
580 if (fence_clone == -1) {
581 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
582 errno);
583 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
584 } else {
585 VkSemaphoreCreateInfo semaphoreInfo;
586 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
587 semaphoreInfo.pNext = nullptr;
588 semaphoreInfo.flags = 0;
589 VkSemaphore semaphore;
590 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400591 if (err != VK_SUCCESS) {
592 ALOGE("Failed to create import semaphore, err: %d", err);
593 close(fence_clone);
594 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
595 } else {
596 VkImportSemaphoreFdInfoKHR importInfo;
597 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
598 importInfo.pNext = nullptr;
599 importInfo.semaphore = semaphore;
600 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
601 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
602 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500603
Greg Danield6670772021-06-09 12:01:12 -0400604 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
605 if (err != VK_SUCCESS) {
606 ALOGE("Failed to import semaphore, err: %d", err);
607 mDestroySemaphore(mDevice, semaphore, nullptr);
608 close(fence_clone);
609 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
610 } else {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000611 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Greg Danield6670772021-06-09 12:01:12 -0400612 // Skia will take ownership of the VkSemaphore and delete it once the wait
613 // has finished. The VkSemaphore also owns the imported fd, so it will
614 // close the fd when it is deleted.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000615 bufferInfo->skSurface->wait(1, &beSemaphore);
Greg Danield6670772021-06-09 12:01:12 -0400616 // The following flush blocks the GPU immediately instead of waiting for
617 // other drawing ops. It seems dequeue_fence is not respected otherwise.
Kevin Lubick913e9a42024-03-07 13:14:46 +0000618 // TODO: remove the flush after finding why beSemaphore is not working.
Kevin Lubickcae0b212023-09-12 18:33:10 +0000619 skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface.get());
Greg Danield6670772021-06-09 12:01:12 -0400620 }
621 }
Stan Iliev197843d2019-03-21 11:34:15 -0400622 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500623 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500624 }
625
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500626 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
627 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500628}
629
John Reck4db23fd2023-11-10 15:31:49 -0500630class SharedSemaphoreInfo : public LightRefBase<SharedSemaphoreInfo> {
Greg Danield92a9b12019-04-23 10:11:04 -0400631 PFN_vkDestroySemaphore mDestroyFunction;
632 VkDevice mDevice;
633 VkSemaphore mSemaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500634 GrBackendSemaphore mGrBackendSemaphore;
Greg Danield92a9b12019-04-23 10:11:04 -0400635
John Reck4db23fd2023-11-10 15:31:49 -0500636 SharedSemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
637 VkSemaphore semaphore)
Priyanka Advanidfca12a2024-03-06 22:25:04 +0000638 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {
Kevin Lubick913e9a42024-03-07 13:14:46 +0000639 mGrBackendSemaphore = GrBackendSemaphores::MakeVk(mSemaphore);
John Reck4db23fd2023-11-10 15:31:49 -0500640 }
John Reck5d3fac12023-11-08 23:08:10 -0500641
John Reck4db23fd2023-11-10 15:31:49 -0500642 ~SharedSemaphoreInfo() { mDestroyFunction(mDevice, mSemaphore, nullptr); }
643
644 friend class LightRefBase<SharedSemaphoreInfo>;
645 friend class sp<SharedSemaphoreInfo>;
646
647public:
648 VkSemaphore semaphore() const { return mSemaphore; }
649
650 GrBackendSemaphore* grBackendSemaphore() { return &mGrBackendSemaphore; }
Greg Danield92a9b12019-04-23 10:11:04 -0400651};
652
653static void destroy_semaphore(void* context) {
John Reck4db23fd2023-11-10 15:31:49 -0500654 SharedSemaphoreInfo* info = reinterpret_cast<SharedSemaphoreInfo*>(context);
655 info->decStrong(0);
Greg Danield92a9b12019-04-23 10:11:04 -0400656}
657
John Reck5d3fac12023-11-08 23:08:10 -0500658VulkanManager::VkDrawResult VulkanManager::finishFrame(SkSurface* surface) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500659 ATRACE_NAME("Vulkan finish frame");
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400660
John Reck4db23fd2023-11-10 15:31:49 -0500661 sp<SharedSemaphoreInfo> sharedSemaphore;
Greg Danielc7ad4082020-05-14 15:38:26 -0400662 GrFlushInfo flushInfo;
John Reck4db23fd2023-11-10 15:31:49 -0500663
664 {
665 VkExportSemaphoreCreateInfo exportInfo;
666 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
667 exportInfo.pNext = nullptr;
668 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
669
670 VkSemaphoreCreateInfo semaphoreInfo;
671 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
672 semaphoreInfo.pNext = &exportInfo;
673 semaphoreInfo.flags = 0;
674 VkSemaphore semaphore;
675 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
676 ALOGE_IF(VK_SUCCESS != err,
677 "VulkanManager::makeSwapSemaphore(): Failed to create semaphore");
678
679 if (err == VK_SUCCESS) {
680 sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
681 flushInfo.fNumSemaphores = 1;
682 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
683 flushInfo.fFinishedProc = destroy_semaphore;
684 sharedSemaphore->incStrong(0);
685 flushInfo.fFinishedContext = sharedSemaphore.get();
686 }
Greg Danielbe2803a2021-02-19 18:32:16 -0500687 }
John Reck4db23fd2023-11-10 15:31:49 -0500688
Greg Danielbe2803a2021-02-19 18:32:16 -0500689 GrDirectContext* context = GrAsDirectContext(surface->recordingContext());
Adlai Holler0375fee2020-09-15 12:31:22 -0400690 ALOGE_IF(!context, "Surface is not backed by gpu");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000691 GrSemaphoresSubmitted submitted = context->flush(
692 surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo);
Adlai Holler0375fee2020-09-15 12:31:22 -0400693 context->submit();
John Reck5d3fac12023-11-08 23:08:10 -0500694 VkDrawResult drawResult{
695 .submissionTime = systemTime(),
696 };
John Reck4db23fd2023-11-10 15:31:49 -0500697 if (sharedSemaphore) {
698 if (submitted == GrSemaphoresSubmitted::kYes && mFrameBoundaryANDROID) {
699 // retrieve VkImage used as render target
700 VkImage image = VK_NULL_HANDLE;
701 GrBackendRenderTarget backendRenderTarget = SkSurfaces::GetBackendRenderTarget(
702 surface, SkSurfaces::BackendHandleAccess::kFlushRead);
703 if (backendRenderTarget.isValid()) {
704 GrVkImageInfo info;
705 if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) {
706 image = info.fImage;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000707 } else {
John Reck4db23fd2023-11-10 15:31:49 -0500708 ALOGE("Frame boundary: backend is not vulkan");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000709 }
John Reck4db23fd2023-11-10 15:31:49 -0500710 } else {
711 ALOGE("Frame boundary: invalid backend render target");
Hugues Evrardb9510a02021-03-01 14:35:22 +0000712 }
John Reck4db23fd2023-11-10 15:31:49 -0500713 // frameBoundaryANDROID needs to know about mSwapSemaphore, but
714 // it won't wait on it.
715 mFrameBoundaryANDROID(mDevice, sharedSemaphore->semaphore(), image);
Greg Danielbe2803a2021-02-19 18:32:16 -0500716 }
John Reck5d3fac12023-11-08 23:08:10 -0500717 VkSemaphoreGetFdInfoKHR getFdInfo;
718 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
719 getFdInfo.pNext = nullptr;
John Reck4db23fd2023-11-10 15:31:49 -0500720 getFdInfo.semaphore = sharedSemaphore->semaphore();
John Reck5d3fac12023-11-08 23:08:10 -0500721 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
722
723 int fenceFd = -1;
John Reck4db23fd2023-11-10 15:31:49 -0500724 VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
John Reck5d3fac12023-11-08 23:08:10 -0500725 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
726 drawResult.presentFence.reset(fenceFd);
727 } else {
728 ALOGE("VulkanManager::finishFrame(): Semaphore submission failed");
729 mQueueWaitIdle(mGraphicsQueue);
Greg Danielbe2803a2021-02-19 18:32:16 -0500730 }
John Reck5d3fac12023-11-08 23:08:10 -0500731
Greg Danielbe2803a2021-02-19 18:32:16 -0500732 skiapipeline::ShaderCache::get().onVkFrameFlushed(context);
Alec Mouri3afb3972022-05-27 22:03:11 +0000733
John Reck5d3fac12023-11-08 23:08:10 -0500734 return drawResult;
Greg Danielbe2803a2021-02-19 18:32:16 -0500735}
736
John Reck5d3fac12023-11-08 23:08:10 -0500737void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect,
738 android::base::unique_fd&& presentFence) {
Greg Danielbe2803a2021-02-19 18:32:16 -0500739 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
740 ATRACE_NAME("Finishing GPU work");
741 mDeviceWaitIdle(mDevice);
742 }
743
John Reck5d3fac12023-11-08 23:08:10 -0500744 surface->presentCurrentBuffer(dirtyRect, presentFence.release());
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500745}
746
747void VulkanManager::destroySurface(VulkanSurface* surface) {
748 // Make sure all submit commands have finished before starting to destroy objects.
Yiwei Zhang276d5fc2020-09-03 12:00:00 -0700749 if (VK_NULL_HANDLE != mGraphicsQueue) {
750 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500751 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500752
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500753 delete surface;
754}
755
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400756VulkanSurface* VulkanManager::createSurface(ANativeWindow* window,
757 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800758 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400759 SkColorType surfaceColorType,
760 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700761 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500762 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500763 if (!window) {
764 return nullptr;
765 }
766
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500767 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700768 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500769}
770
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400771status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400772 if (!hasVkContext()) {
773 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
774 return INVALID_OPERATION;
775 }
776
Stan Iliev7a081272018-10-26 17:54:18 -0400777 // Block GPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400778 int fenceFd = ::dup(fence);
Stan Iliev7a081272018-10-26 17:54:18 -0400779 if (fenceFd == -1) {
780 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
781 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000782 }
Stan Iliev7a081272018-10-26 17:54:18 -0400783
784 VkSemaphoreCreateInfo semaphoreInfo;
785 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
786 semaphoreInfo.pNext = nullptr;
787 semaphoreInfo.flags = 0;
788 VkSemaphore semaphore;
789 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
790 if (VK_SUCCESS != err) {
Greg Danield6670772021-06-09 12:01:12 -0400791 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400792 ALOGE("Failed to create import semaphore, err: %d", err);
793 return UNKNOWN_ERROR;
794 }
795 VkImportSemaphoreFdInfoKHR importInfo;
796 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
797 importInfo.pNext = nullptr;
798 importInfo.semaphore = semaphore;
799 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
800 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
801 importInfo.fd = fenceFd;
802
803 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
804 if (VK_SUCCESS != err) {
Greg Danield92a9b12019-04-23 10:11:04 -0400805 mDestroySemaphore(mDevice, semaphore, nullptr);
Greg Danield6670772021-06-09 12:01:12 -0400806 close(fenceFd);
Stan Iliev7a081272018-10-26 17:54:18 -0400807 ALOGE("Failed to import semaphore, err: %d", err);
808 return UNKNOWN_ERROR;
809 }
810
Kevin Lubick913e9a42024-03-07 13:14:46 +0000811 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400812
Greg Danield6670772021-06-09 12:01:12 -0400813 // Skia will take ownership of the VkSemaphore and delete it once the wait has finished. The
814 // VkSemaphore also owns the imported fd, so it will close the fd when it is deleted.
Greg Danield92a9b12019-04-23 10:11:04 -0400815 grContext->wait(1, &beSemaphore);
Greg Danielc7ad4082020-05-14 15:38:26 -0400816 grContext->flushAndSubmit();
Stan Iliev7a081272018-10-26 17:54:18 -0400817
Stan Iliev564ca3e2018-09-04 22:00:00 +0000818 return OK;
819}
820
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400821status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) {
Stan Ilievaaa9e832019-09-17 14:07:23 -0400822 *nativeFence = -1;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400823 if (!hasVkContext()) {
824 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
825 return INVALID_OPERATION;
826 }
827
Greg Daniel26e0dca2018-09-18 10:33:19 -0400828 VkExportSemaphoreCreateInfo exportInfo;
829 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
830 exportInfo.pNext = nullptr;
831 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
832
833 VkSemaphoreCreateInfo semaphoreInfo;
834 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
835 semaphoreInfo.pNext = &exportInfo;
836 semaphoreInfo.flags = 0;
837 VkSemaphore semaphore;
838 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
839 if (VK_SUCCESS != err) {
840 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
841 return INVALID_OPERATION;
842 }
843
John Reck4db23fd2023-11-10 15:31:49 -0500844 auto sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400845
Greg Danielfd429392019-05-09 15:44:56 -0400846 // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
Greg Danielc7ad4082020-05-14 15:38:26 -0400847 GrFlushInfo flushInfo;
848 flushInfo.fNumSemaphores = 1;
John Reck4db23fd2023-11-10 15:31:49 -0500849 flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore();
Greg Danielc7ad4082020-05-14 15:38:26 -0400850 flushInfo.fFinishedProc = destroy_semaphore;
John Reck4db23fd2023-11-10 15:31:49 -0500851 sharedSemaphore->incStrong(0);
852 flushInfo.fFinishedContext = sharedSemaphore.get();
Greg Danielc7ad4082020-05-14 15:38:26 -0400853 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
854 grContext->submit();
Greg Daniel26e0dca2018-09-18 10:33:19 -0400855
Greg Danield92a9b12019-04-23 10:11:04 -0400856 if (submitted == GrSemaphoresSubmitted::kNo) {
857 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
Greg Danield92a9b12019-04-23 10:11:04 -0400858 return INVALID_OPERATION;
859 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400860
861 VkSemaphoreGetFdInfoKHR getFdInfo;
862 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
863 getFdInfo.pNext = nullptr;
864 getFdInfo.semaphore = semaphore;
865 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
866
867 int fenceFd = 0;
868
869 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
870 if (VK_SUCCESS != err) {
871 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
872 return INVALID_OPERATION;
873 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400874 *nativeFence = fenceFd;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400875
Stan Iliev564ca3e2018-09-04 22:00:00 +0000876 return OK;
877}
878
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500879} /* namespace renderthread */
880} /* namespace uirenderer */
881} /* namespace android */