Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 1 | /* |
| 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 Mouri | 6db59a6 | 2019-08-02 17:05:26 -0700 | [diff] [blame] | 19 | #include <EGL/egl.h> |
| 20 | #include <EGL/eglext.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 21 | #include <GrBackendSemaphore.h> |
| 22 | #include <GrBackendSurface.h> |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 23 | #include <GrDirectContext.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 24 | #include <GrTypes.h> |
| 25 | #include <android/sync.h> |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 26 | #include <gui/TraceUtils.h> |
| 27 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
Brian Osman | 734d810 | 2023-08-29 18:07:56 +0000 | [diff] [blame] | 28 | #include <include/gpu/ganesh/vk/GrVkBackendSurface.h> |
Kevin Lubick | f6b2195 | 2023-10-13 13:12:17 +0000 | [diff] [blame] | 29 | #include <include/gpu/ganesh/vk/GrVkDirectContext.h> |
Jagadeesh Pakaravoor | b624af3 | 2020-05-01 00:01:40 +0000 | [diff] [blame] | 30 | #include <ui/FatVector.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 31 | #include <vk/GrVkExtensions.h> |
| 32 | #include <vk/GrVkTypes.h> |
Stan Iliev | 305e13a | 2018-11-13 11:14:48 -0500 | [diff] [blame] | 33 | |
Nolan Scobie | ddc9c66 | 2024-01-30 17:38:42 -0500 | [diff] [blame^] | 34 | #include <sstream> |
| 35 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 36 | #include "Properties.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 37 | #include "RenderThread.h" |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 38 | #include "pipeline/skia/ShaderCache.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 39 | #include "renderstate/RenderState.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 40 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace uirenderer { |
| 43 | namespace renderthread { |
| 44 | |
Nolan Scobie | ddc9c66 | 2024-01-30 17:38:42 -0500 | [diff] [blame^] | 45 | // Not all of these are strictly required, but are all enabled if present. |
| 46 | static std::array<std::string_view, 21> sEnableExtensions{ |
John Reck | f6067df | 2023-04-11 16:27:51 -0400 | [diff] [blame] | 47 | VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, |
| 48 | VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, |
| 49 | VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, |
| 50 | VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, |
| 51 | VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, |
| 52 | VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, |
| 53 | VK_KHR_MAINTENANCE1_EXTENSION_NAME, |
| 54 | VK_KHR_MAINTENANCE2_EXTENSION_NAME, |
| 55 | VK_KHR_MAINTENANCE3_EXTENSION_NAME, |
| 56 | VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, |
| 57 | VK_KHR_SURFACE_EXTENSION_NAME, |
| 58 | VK_KHR_SWAPCHAIN_EXTENSION_NAME, |
| 59 | VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, |
John Reck | 90b244d | 2023-04-28 15:41:55 -0400 | [diff] [blame] | 60 | VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, |
John Reck | f6067df | 2023-04-11 16:27:51 -0400 | [diff] [blame] | 61 | VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, |
| 62 | VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, |
| 63 | VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, |
| 64 | VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, |
| 65 | VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
Mattias Simonsson | e545f96 | 2023-07-17 08:03:14 +0000 | [diff] [blame] | 66 | VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, |
Nolan Scobie | ddc9c66 | 2024-01-30 17:38:42 -0500 | [diff] [blame^] | 67 | VK_EXT_DEVICE_FAULT_EXTENSION_NAME, |
John Reck | f6067df | 2023-04-11 16:27:51 -0400 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static bool shouldEnableExtension(const std::string_view& extension) { |
| 71 | for (const auto& it : sEnableExtensions) { |
| 72 | if (it == extension) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 79 | static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) { |
| 80 | // All Vulkan structs that could be part of the features chain will start with the |
| 81 | // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader |
| 82 | // so we can get access to the pNext for the next struct. |
| 83 | struct CommonVulkanHeader { |
| 84 | VkStructureType sType; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 85 | void* pNext; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | void* pNext = features.pNext; |
| 89 | while (pNext) { |
| 90 | void* current = pNext; |
| 91 | pNext = static_cast<CommonVulkanHeader*>(current)->pNext; |
| 92 | free(current); |
| 93 | } |
| 94 | } |
| 95 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 96 | #define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F) |
| 97 | #define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F) |
| 98 | #define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 99 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 100 | // cache a weakptr to the context to enable a second thread to share the same vulkan state |
| 101 | static wp<VulkanManager> sWeakInstance = nullptr; |
| 102 | static std::mutex sLock; |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 103 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 104 | sp<VulkanManager> VulkanManager::getInstance() { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 105 | std::lock_guard _lock{sLock}; |
| 106 | sp<VulkanManager> vulkanManager = sWeakInstance.promote(); |
| 107 | if (!vulkanManager.get()) { |
| 108 | vulkanManager = new VulkanManager(); |
| 109 | sWeakInstance = vulkanManager; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 110 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 111 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 112 | return vulkanManager; |
| 113 | } |
| 114 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 115 | sp<VulkanManager> VulkanManager::peekInstance() { |
| 116 | std::lock_guard _lock{sLock}; |
| 117 | return sWeakInstance.promote(); |
| 118 | } |
| 119 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 120 | VulkanManager::~VulkanManager() { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 121 | if (mDevice != VK_NULL_HANDLE) { |
| 122 | mDeviceWaitIdle(mDevice); |
| 123 | mDestroyDevice(mDevice, nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 125 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 126 | if (mInstance != VK_NULL_HANDLE) { |
| 127 | mDestroyInstance(mInstance, nullptr); |
| 128 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 129 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 130 | mGraphicsQueue = VK_NULL_HANDLE; |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 131 | mAHBUploadQueue = VK_NULL_HANDLE; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 132 | mDevice = VK_NULL_HANDLE; |
| 133 | mPhysicalDevice = VK_NULL_HANDLE; |
| 134 | mInstance = VK_NULL_HANDLE; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 135 | mInstanceExtensionsOwner.clear(); |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 136 | mInstanceExtensions.clear(); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 137 | mDeviceExtensionsOwner.clear(); |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 138 | mDeviceExtensions.clear(); |
| 139 | free_features_extensions_structs(mPhysicalDeviceFeatures2); |
| 140 | mPhysicalDeviceFeatures2 = {}; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 141 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 142 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 143 | void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 144 | VkResult err; |
| 145 | |
| 146 | constexpr VkApplicationInfo app_info = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 147 | VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType |
| 148 | nullptr, // pNext |
| 149 | "android framework", // pApplicationName |
| 150 | 0, // applicationVersion |
| 151 | "android framework", // pEngineName |
| 152 | 0, // engineVerison |
| 153 | mAPIVersion, // apiVersion |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 154 | }; |
| 155 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 156 | { |
| 157 | GET_PROC(EnumerateInstanceExtensionProperties); |
| 158 | |
| 159 | uint32_t extensionCount = 0; |
| 160 | err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 161 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 162 | mInstanceExtensionsOwner.resize(extensionCount); |
| 163 | err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, |
| 164 | mInstanceExtensionsOwner.data()); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 165 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 166 | bool hasKHRSurfaceExtension = false; |
| 167 | bool hasKHRAndroidSurfaceExtension = false; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 168 | for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) { |
John Reck | f6067df | 2023-04-11 16:27:51 -0400 | [diff] [blame] | 169 | if (!shouldEnableExtension(extension.extensionName)) { |
| 170 | ALOGV("Not enabling instance extension %s", extension.extensionName); |
| 171 | continue; |
| 172 | } |
| 173 | ALOGV("Enabling instance extension %s", extension.extensionName); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 174 | mInstanceExtensions.push_back(extension.extensionName); |
| 175 | if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 176 | hasKHRSurfaceExtension = true; |
| 177 | } |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 178 | if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 179 | hasKHRAndroidSurfaceExtension = true; |
| 180 | } |
| 181 | } |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 182 | LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | const VkInstanceCreateInfo instance_create = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 186 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType |
| 187 | nullptr, // pNext |
| 188 | 0, // flags |
| 189 | &app_info, // pApplicationInfo |
| 190 | 0, // enabledLayerNameCount |
| 191 | nullptr, // ppEnabledLayerNames |
| 192 | (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount |
| 193 | mInstanceExtensions.data(), // ppEnabledExtensionNames |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | GET_PROC(CreateInstance); |
| 197 | err = mCreateInstance(&instance_create, nullptr, &mInstance); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 198 | LOG_ALWAYS_FATAL_IF(err < 0); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 199 | |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 200 | GET_INST_PROC(CreateDevice); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 201 | GET_INST_PROC(DestroyInstance); |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 202 | GET_INST_PROC(EnumerateDeviceExtensionProperties); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 203 | GET_INST_PROC(EnumeratePhysicalDevices); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 204 | GET_INST_PROC(GetPhysicalDeviceFeatures2); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 205 | GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2); |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 206 | GET_INST_PROC(GetPhysicalDeviceProperties); |
| 207 | GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 208 | |
| 209 | uint32_t gpuCount; |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 210 | LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr)); |
| 211 | LOG_ALWAYS_FATAL_IF(!gpuCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 212 | // Just returning the first physical device instead of getting the whole array. Since there |
| 213 | // should only be one device on android. |
| 214 | gpuCount = 1; |
| 215 | err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice); |
| 216 | // VK_INCOMPLETE is returned when the count we provide is less than the total device count. |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 217 | LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 218 | |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 219 | VkPhysicalDeviceProperties physDeviceProperties; |
| 220 | mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 221 | LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0)); |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 222 | mDriverVersion = physDeviceProperties.driverVersion; |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 223 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 224 | // query to get the initial queue props size |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 225 | uint32_t queueCount = 0; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 226 | mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 227 | LOG_ALWAYS_FATAL_IF(!queueCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 228 | |
| 229 | // now get the actual queue props |
| 230 | std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]); |
| 231 | mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get()); |
| 232 | |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 233 | constexpr auto kRequestedQueueCount = 2; |
| 234 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 235 | // iterate to find the graphics queue |
| 236 | mGraphicsQueueIndex = queueCount; |
| 237 | for (uint32_t i = 0; i < queueCount; i++) { |
| 238 | if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
| 239 | mGraphicsQueueIndex = i; |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 240 | LOG_ALWAYS_FATAL_IF(queueProps[i].queueCount < kRequestedQueueCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 244 | LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 245 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 246 | { |
| 247 | uint32_t extensionCount = 0; |
| 248 | err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 249 | nullptr); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 250 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 251 | mDeviceExtensionsOwner.resize(extensionCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 252 | err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 253 | mDeviceExtensionsOwner.data()); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 254 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 255 | bool hasKHRSwapchainExtension = false; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 256 | for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) { |
John Reck | f6067df | 2023-04-11 16:27:51 -0400 | [diff] [blame] | 257 | if (!shouldEnableExtension(extension.extensionName)) { |
| 258 | ALOGV("Not enabling device extension %s", extension.extensionName); |
| 259 | continue; |
| 260 | } |
| 261 | ALOGV("Enabling device extension %s", extension.extensionName); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 262 | mDeviceExtensions.push_back(extension.extensionName); |
| 263 | if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 264 | hasKHRSwapchainExtension = true; |
| 265 | } |
| 266 | } |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 267 | LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 270 | auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) { |
| 271 | if (device != VK_NULL_HANDLE) { |
| 272 | return vkGetDeviceProcAddr(device, proc_name); |
| 273 | } |
| 274 | return vkGetInstanceProcAddr(instance, proc_name); |
| 275 | }; |
| 276 | |
| 277 | grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(), |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 278 | mInstanceExtensions.data(), mDeviceExtensions.size(), |
| 279 | mDeviceExtensions.data()); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 280 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 281 | LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1)); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 282 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 283 | memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2)); |
| 284 | features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 285 | features.pNext = nullptr; |
| 286 | |
| 287 | // Setup all extension feature structs we may want to use. |
| 288 | void** tailPNext = &features.pNext; |
| 289 | |
| 290 | if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) { |
| 291 | VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 292 | blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc( |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 293 | sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT)); |
| 294 | LOG_ALWAYS_FATAL_IF(!blend); |
| 295 | blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT; |
| 296 | blend->pNext = nullptr; |
| 297 | *tailPNext = blend; |
| 298 | tailPNext = &blend->pNext; |
| 299 | } |
| 300 | |
Greg Daniel | 0503617 | 2018-11-28 17:08:04 -0500 | [diff] [blame] | 301 | VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 302 | ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc( |
Greg Daniel | 0503617 | 2018-11-28 17:08:04 -0500 | [diff] [blame] | 303 | sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures)); |
| 304 | LOG_ALWAYS_FATAL_IF(!ycbcrFeature); |
| 305 | ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES; |
| 306 | ycbcrFeature->pNext = nullptr; |
| 307 | *tailPNext = ycbcrFeature; |
| 308 | tailPNext = &ycbcrFeature->pNext; |
| 309 | |
Nolan Scobie | ddc9c66 | 2024-01-30 17:38:42 -0500 | [diff] [blame^] | 310 | if (grExtensions.hasExtension(VK_EXT_DEVICE_FAULT_EXTENSION_NAME, 1)) { |
| 311 | VkPhysicalDeviceFaultFeaturesEXT* deviceFaultFeatures = |
| 312 | new VkPhysicalDeviceFaultFeaturesEXT; |
| 313 | deviceFaultFeatures->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FAULT_FEATURES_EXT; |
| 314 | deviceFaultFeatures->pNext = nullptr; |
| 315 | *tailPNext = deviceFaultFeatures; |
| 316 | tailPNext = &deviceFaultFeatures->pNext; |
| 317 | } |
| 318 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 319 | // query to get the physical device features |
| 320 | mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 321 | // this looks like it would slow things down, |
| 322 | // and we can't depend on it on all platforms |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 323 | features.features.robustBufferAccess = VK_FALSE; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 324 | |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 325 | float queuePriorities[kRequestedQueueCount] = {0.0}; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 326 | |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 327 | void* queueNextPtr = nullptr; |
| 328 | |
| 329 | VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo; |
| 330 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 331 | if (Properties::contextPriority != 0 && |
| 332 | grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) { |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 333 | memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT)); |
| 334 | queuePriorityCreateInfo.sType = |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 335 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT; |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 336 | queuePriorityCreateInfo.pNext = nullptr; |
| 337 | switch (Properties::contextPriority) { |
| 338 | case EGL_CONTEXT_PRIORITY_LOW_IMG: |
| 339 | queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT; |
| 340 | break; |
| 341 | case EGL_CONTEXT_PRIORITY_MEDIUM_IMG: |
| 342 | queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT; |
| 343 | break; |
| 344 | case EGL_CONTEXT_PRIORITY_HIGH_IMG: |
| 345 | queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT; |
| 346 | break; |
| 347 | default: |
| 348 | LOG_ALWAYS_FATAL("Unsupported context priority"); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 349 | } |
| 350 | queueNextPtr = &queuePriorityCreateInfo; |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Yiwei Zhang | 276d5fc | 2020-09-03 12:00:00 -0700 | [diff] [blame] | 353 | const VkDeviceQueueCreateInfo queueInfo = { |
| 354 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 355 | queueNextPtr, // pNext |
| 356 | 0, // VkDeviceQueueCreateFlags |
| 357 | mGraphicsQueueIndex, // queueFamilyIndex |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 358 | kRequestedQueueCount, // queueCount |
Yiwei Zhang | 276d5fc | 2020-09-03 12:00:00 -0700 | [diff] [blame] | 359 | queuePriorities, // pQueuePriorities |
| 360 | }; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 361 | |
| 362 | const VkDeviceCreateInfo deviceInfo = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 363 | VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType |
| 364 | &features, // pNext |
| 365 | 0, // VkDeviceCreateFlags |
Yiwei Zhang | 276d5fc | 2020-09-03 12:00:00 -0700 | [diff] [blame] | 366 | 1, // queueCreateInfoCount |
| 367 | &queueInfo, // pQueueCreateInfos |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 368 | 0, // layerCount |
| 369 | nullptr, // ppEnabledLayerNames |
| 370 | (uint32_t)mDeviceExtensions.size(), // extensionCount |
| 371 | mDeviceExtensions.data(), // ppEnabledExtensionNames |
| 372 | nullptr, // ppEnabledFeatures |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 373 | }; |
| 374 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 375 | LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice)); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 376 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 377 | GET_DEV_PROC(AllocateCommandBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 378 | GET_DEV_PROC(BeginCommandBuffer); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 379 | GET_DEV_PROC(CmdPipelineBarrier); |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 380 | GET_DEV_PROC(CreateCommandPool); |
| 381 | GET_DEV_PROC(CreateFence); |
| 382 | GET_DEV_PROC(CreateSemaphore); |
| 383 | GET_DEV_PROC(DestroyCommandPool); |
| 384 | GET_DEV_PROC(DestroyDevice); |
| 385 | GET_DEV_PROC(DestroyFence); |
| 386 | GET_DEV_PROC(DestroySemaphore); |
| 387 | GET_DEV_PROC(DeviceWaitIdle); |
| 388 | GET_DEV_PROC(EndCommandBuffer); |
| 389 | GET_DEV_PROC(FreeCommandBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 390 | GET_DEV_PROC(GetDeviceQueue); |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 391 | GET_DEV_PROC(GetSemaphoreFdKHR); |
| 392 | GET_DEV_PROC(ImportSemaphoreFdKHR); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 393 | GET_DEV_PROC(QueueSubmit); |
| 394 | GET_DEV_PROC(QueueWaitIdle); |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 395 | GET_DEV_PROC(ResetCommandBuffer); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 396 | GET_DEV_PROC(ResetFences); |
Yiwei Zhang | 0b9f0b8 | 2019-08-15 11:33:59 -0700 | [diff] [blame] | 397 | GET_DEV_PROC(WaitForFences); |
Hugues Evrard | b9510a0 | 2021-03-01 14:35:22 +0000 | [diff] [blame] | 398 | GET_DEV_PROC(FrameBoundaryANDROID); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void VulkanManager::initialize() { |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 402 | std::call_once(mInitFlag, [&] { |
| 403 | GET_PROC(EnumerateInstanceVersion); |
| 404 | uint32_t instanceVersion; |
| 405 | LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion)); |
| 406 | LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0)); |
Greg Daniel | 1d9de71 | 2021-05-14 09:28:34 -0400 | [diff] [blame] | 407 | |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 408 | this->setupDevice(mExtensions, mPhysicalDeviceFeatures2); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 409 | |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 410 | mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue); |
| 411 | mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 1, &mAHBUploadQueue); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 412 | |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 413 | if (Properties::enablePartialUpdates && Properties::useBufferAge) { |
| 414 | mSwapBehavior = SwapBehavior::BufferAge; |
| 415 | } |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 416 | |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 417 | mInitialized = true; |
| 418 | }); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Nolan Scobie | ddc9c66 | 2024-01-30 17:38:42 -0500 | [diff] [blame^] | 421 | namespace { |
| 422 | void onVkDeviceFault(const std::string& contextLabel, const std::string& description, |
| 423 | const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos, |
| 424 | const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos, |
| 425 | const std::vector<std::byte>& vendorBinaryData) { |
| 426 | // The final crash string should contain as much differentiating info as possible, up to 1024 |
| 427 | // bytes. As this final message is constructed, the same information is also dumped to the logs |
| 428 | // but in a more verbose format. Building the crash string is unsightly, so the clearer logging |
| 429 | // statement is always placed first to give context. |
| 430 | ALOGE("VK_ERROR_DEVICE_LOST (%s context): %s", contextLabel.c_str(), description.c_str()); |
| 431 | std::stringstream crashMsg; |
| 432 | crashMsg << "VK_ERROR_DEVICE_LOST (" << contextLabel; |
| 433 | |
| 434 | if (!addressInfos.empty()) { |
| 435 | ALOGE("%zu VkDeviceFaultAddressInfoEXT:", addressInfos.size()); |
| 436 | crashMsg << ", " << addressInfos.size() << " address info ("; |
| 437 | for (VkDeviceFaultAddressInfoEXT addressInfo : addressInfos) { |
| 438 | ALOGE(" addressType: %d", (int)addressInfo.addressType); |
| 439 | ALOGE(" reportedAddress: %" PRIu64, addressInfo.reportedAddress); |
| 440 | ALOGE(" addressPrecision: %" PRIu64, addressInfo.addressPrecision); |
| 441 | crashMsg << addressInfo.addressType << ":" |
| 442 | << addressInfo.reportedAddress << ":" |
| 443 | << addressInfo.addressPrecision << ", "; |
| 444 | } |
| 445 | crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", " |
| 446 | crashMsg << ")"; |
| 447 | } |
| 448 | |
| 449 | if (!vendorInfos.empty()) { |
| 450 | ALOGE("%zu VkDeviceFaultVendorInfoEXT:", vendorInfos.size()); |
| 451 | crashMsg << ", " << vendorInfos.size() << " vendor info ("; |
| 452 | for (VkDeviceFaultVendorInfoEXT vendorInfo : vendorInfos) { |
| 453 | ALOGE(" description: %s", vendorInfo.description); |
| 454 | ALOGE(" vendorFaultCode: %" PRIu64, vendorInfo.vendorFaultCode); |
| 455 | ALOGE(" vendorFaultData: %" PRIu64, vendorInfo.vendorFaultData); |
| 456 | // Omit descriptions for individual vendor info structs in the crash string, as the |
| 457 | // fault code and fault data fields should be enough for clustering, and the verbosity |
| 458 | // isn't worth it. Additionally, vendors may just set the general description field of |
| 459 | // the overall fault to the description of the first element in this list, and that |
| 460 | // overall description will be placed at the end of the crash string. |
| 461 | crashMsg << vendorInfo.vendorFaultCode << ":" |
| 462 | << vendorInfo.vendorFaultData << ", "; |
| 463 | } |
| 464 | crashMsg.seekp(-2, crashMsg.cur); // Move back to overwrite trailing ", " |
| 465 | crashMsg << ")"; |
| 466 | } |
| 467 | |
| 468 | if (!vendorBinaryData.empty()) { |
| 469 | // TODO: b/322830575 - Log in base64, or dump directly to a file that gets put in bugreports |
| 470 | ALOGE("%zu bytes of vendor-specific binary data (please notify Android's Core Graphics" |
| 471 | " Stack team if you observe this message).", |
| 472 | vendorBinaryData.size()); |
| 473 | crashMsg << ", " << vendorBinaryData.size() << " bytes binary"; |
| 474 | } |
| 475 | |
| 476 | crashMsg << "): " << description; |
| 477 | LOG_ALWAYS_FATAL("%s", crashMsg.str().c_str()); |
| 478 | } |
| 479 | |
| 480 | void deviceLostProcRenderThread(void* callbackContext, const std::string& description, |
| 481 | const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos, |
| 482 | const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos, |
| 483 | const std::vector<std::byte>& vendorBinaryData) { |
| 484 | onVkDeviceFault("RenderThread", description, addressInfos, vendorInfos, vendorBinaryData); |
| 485 | } |
| 486 | void deviceLostProcUploadThread(void* callbackContext, const std::string& description, |
| 487 | const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos, |
| 488 | const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos, |
| 489 | const std::vector<std::byte>& vendorBinaryData) { |
| 490 | onVkDeviceFault("UploadThread", description, addressInfos, vendorInfos, vendorBinaryData); |
| 491 | } |
| 492 | } // anonymous namespace |
| 493 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 494 | static void onGrContextReleased(void* context) { |
| 495 | VulkanManager* manager = (VulkanManager*)context; |
| 496 | manager->decStrong((void*)onGrContextReleased); |
| 497 | } |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 498 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 499 | sk_sp<GrDirectContext> VulkanManager::createContext(GrContextOptions& options, |
| 500 | ContextType contextType) { |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 501 | auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) { |
| 502 | if (device != VK_NULL_HANDLE) { |
| 503 | return vkGetDeviceProcAddr(device, proc_name); |
| 504 | } |
| 505 | return vkGetInstanceProcAddr(instance, proc_name); |
| 506 | }; |
| 507 | |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 508 | GrVkBackendContext backendContext; |
| 509 | backendContext.fInstance = mInstance; |
| 510 | backendContext.fPhysicalDevice = mPhysicalDevice; |
| 511 | backendContext.fDevice = mDevice; |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 512 | backendContext.fQueue = |
| 513 | (contextType == ContextType::kRenderThread) ? mGraphicsQueue : mAHBUploadQueue; |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 514 | backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex; |
| 515 | backendContext.fMaxAPIVersion = mAPIVersion; |
| 516 | backendContext.fVkExtensions = &mExtensions; |
| 517 | backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2; |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 518 | backendContext.fGetProc = std::move(getProc); |
Nolan Scobie | ddc9c66 | 2024-01-30 17:38:42 -0500 | [diff] [blame^] | 519 | backendContext.fDeviceLostContext = nullptr; |
| 520 | backendContext.fDeviceLostProc = (contextType == ContextType::kRenderThread) |
| 521 | ? deviceLostProcRenderThread |
| 522 | : deviceLostProcUploadThread; |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 523 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 524 | LOG_ALWAYS_FATAL_IF(options.fContextDeleteProc != nullptr, "Conflicting fContextDeleteProcs!"); |
| 525 | this->incStrong((void*)onGrContextReleased); |
| 526 | options.fContextDeleteContext = this; |
| 527 | options.fContextDeleteProc = onGrContextReleased; |
| 528 | |
Kevin Lubick | f6b2195 | 2023-10-13 13:12:17 +0000 | [diff] [blame] | 529 | return GrDirectContexts::MakeVulkan(backendContext, options); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 530 | } |
| 531 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 532 | VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const { |
| 533 | return VkFunctorInitParams{ |
| 534 | .instance = mInstance, |
| 535 | .physical_device = mPhysicalDevice, |
| 536 | .device = mDevice, |
| 537 | .queue = mGraphicsQueue, |
| 538 | .graphics_queue_index = mGraphicsQueueIndex, |
Greg Daniel | eaf310e | 2019-01-28 16:10:32 -0500 | [diff] [blame] | 539 | .api_version = mAPIVersion, |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 540 | .enabled_instance_extension_names = mInstanceExtensions.data(), |
| 541 | .enabled_instance_extension_names_length = |
| 542 | static_cast<uint32_t>(mInstanceExtensions.size()), |
| 543 | .enabled_device_extension_names = mDeviceExtensions.data(), |
| 544 | .enabled_device_extension_names_length = |
| 545 | static_cast<uint32_t>(mDeviceExtensions.size()), |
| 546 | .device_features_2 = &mPhysicalDeviceFeatures2, |
| 547 | }; |
| 548 | } |
| 549 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 550 | Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) { |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 551 | VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer(); |
| 552 | |
| 553 | if (bufferInfo == nullptr) { |
| 554 | ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!"); |
| 555 | return Frame(-1, -1, 0); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 556 | } |
| 557 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 558 | LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 559 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 560 | if (bufferInfo->dequeue_fence != -1) { |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 561 | struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence); |
| 562 | bool isSignalPending = false; |
| 563 | if (finfo != NULL) { |
| 564 | isSignalPending = finfo->status != 1; |
| 565 | sync_file_info_free(finfo); |
| 566 | } |
| 567 | if (isSignalPending) { |
| 568 | int fence_clone = dup(bufferInfo->dequeue_fence); |
| 569 | if (fence_clone == -1) { |
| 570 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno), |
| 571 | errno); |
| 572 | sync_wait(bufferInfo->dequeue_fence, -1 /* forever */); |
| 573 | } else { |
| 574 | VkSemaphoreCreateInfo semaphoreInfo; |
| 575 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 576 | semaphoreInfo.pNext = nullptr; |
| 577 | semaphoreInfo.flags = 0; |
| 578 | VkSemaphore semaphore; |
| 579 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
Greg Daniel | d667077 | 2021-06-09 12:01:12 -0400 | [diff] [blame] | 580 | if (err != VK_SUCCESS) { |
| 581 | ALOGE("Failed to create import semaphore, err: %d", err); |
| 582 | close(fence_clone); |
| 583 | sync_wait(bufferInfo->dequeue_fence, -1 /* forever */); |
| 584 | } else { |
| 585 | VkImportSemaphoreFdInfoKHR importInfo; |
| 586 | importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR; |
| 587 | importInfo.pNext = nullptr; |
| 588 | importInfo.semaphore = semaphore; |
| 589 | importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT; |
| 590 | importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 591 | importInfo.fd = fence_clone; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 592 | |
Greg Daniel | d667077 | 2021-06-09 12:01:12 -0400 | [diff] [blame] | 593 | err = mImportSemaphoreFdKHR(mDevice, &importInfo); |
| 594 | if (err != VK_SUCCESS) { |
| 595 | ALOGE("Failed to import semaphore, err: %d", err); |
| 596 | mDestroySemaphore(mDevice, semaphore, nullptr); |
| 597 | close(fence_clone); |
| 598 | sync_wait(bufferInfo->dequeue_fence, -1 /* forever */); |
| 599 | } else { |
| 600 | GrBackendSemaphore backendSemaphore; |
| 601 | backendSemaphore.initVulkan(semaphore); |
| 602 | // Skia will take ownership of the VkSemaphore and delete it once the wait |
| 603 | // has finished. The VkSemaphore also owns the imported fd, so it will |
| 604 | // close the fd when it is deleted. |
| 605 | bufferInfo->skSurface->wait(1, &backendSemaphore); |
| 606 | // The following flush blocks the GPU immediately instead of waiting for |
| 607 | // other drawing ops. It seems dequeue_fence is not respected otherwise. |
| 608 | // TODO: remove the flush after finding why backendSemaphore is not working. |
Kevin Lubick | cae0b21 | 2023-09-12 18:33:10 +0000 | [diff] [blame] | 609 | skgpu::ganesh::FlushAndSubmit(bufferInfo->skSurface.get()); |
Greg Daniel | d667077 | 2021-06-09 12:01:12 -0400 | [diff] [blame] | 610 | } |
| 611 | } |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 612 | } |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 613 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 614 | } |
| 615 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 616 | int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge(); |
| 617 | return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 618 | } |
| 619 | |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 620 | class SharedSemaphoreInfo : public LightRefBase<SharedSemaphoreInfo> { |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 621 | PFN_vkDestroySemaphore mDestroyFunction; |
| 622 | VkDevice mDevice; |
| 623 | VkSemaphore mSemaphore; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 624 | GrBackendSemaphore mGrBackendSemaphore; |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 625 | |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 626 | SharedSemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device, |
| 627 | VkSemaphore semaphore) |
| 628 | : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) { |
| 629 | mGrBackendSemaphore.initVulkan(semaphore); |
| 630 | } |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 631 | |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 632 | ~SharedSemaphoreInfo() { mDestroyFunction(mDevice, mSemaphore, nullptr); } |
| 633 | |
| 634 | friend class LightRefBase<SharedSemaphoreInfo>; |
| 635 | friend class sp<SharedSemaphoreInfo>; |
| 636 | |
| 637 | public: |
| 638 | VkSemaphore semaphore() const { return mSemaphore; } |
| 639 | |
| 640 | GrBackendSemaphore* grBackendSemaphore() { return &mGrBackendSemaphore; } |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | static void destroy_semaphore(void* context) { |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 644 | SharedSemaphoreInfo* info = reinterpret_cast<SharedSemaphoreInfo*>(context); |
| 645 | info->decStrong(0); |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 646 | } |
| 647 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 648 | VulkanManager::VkDrawResult VulkanManager::finishFrame(SkSurface* surface) { |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 649 | ATRACE_NAME("Vulkan finish frame"); |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 650 | |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 651 | sp<SharedSemaphoreInfo> sharedSemaphore; |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 652 | GrFlushInfo flushInfo; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 653 | |
| 654 | { |
| 655 | VkExportSemaphoreCreateInfo exportInfo; |
| 656 | exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO; |
| 657 | exportInfo.pNext = nullptr; |
| 658 | exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 659 | |
| 660 | VkSemaphoreCreateInfo semaphoreInfo; |
| 661 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 662 | semaphoreInfo.pNext = &exportInfo; |
| 663 | semaphoreInfo.flags = 0; |
| 664 | VkSemaphore semaphore; |
| 665 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 666 | ALOGE_IF(VK_SUCCESS != err, |
| 667 | "VulkanManager::makeSwapSemaphore(): Failed to create semaphore"); |
| 668 | |
| 669 | if (err == VK_SUCCESS) { |
| 670 | sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore); |
| 671 | flushInfo.fNumSemaphores = 1; |
| 672 | flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore(); |
| 673 | flushInfo.fFinishedProc = destroy_semaphore; |
| 674 | sharedSemaphore->incStrong(0); |
| 675 | flushInfo.fFinishedContext = sharedSemaphore.get(); |
| 676 | } |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 677 | } |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 678 | |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 679 | GrDirectContext* context = GrAsDirectContext(surface->recordingContext()); |
Adlai Holler | 0375fee | 2020-09-15 12:31:22 -0400 | [diff] [blame] | 680 | ALOGE_IF(!context, "Surface is not backed by gpu"); |
Kevin Lubick | aa592d0 | 2023-05-30 15:07:06 +0000 | [diff] [blame] | 681 | GrSemaphoresSubmitted submitted = context->flush( |
| 682 | surface, SkSurfaces::BackendSurfaceAccess::kPresent, flushInfo); |
Adlai Holler | 0375fee | 2020-09-15 12:31:22 -0400 | [diff] [blame] | 683 | context->submit(); |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 684 | VkDrawResult drawResult{ |
| 685 | .submissionTime = systemTime(), |
| 686 | }; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 687 | if (sharedSemaphore) { |
| 688 | if (submitted == GrSemaphoresSubmitted::kYes && mFrameBoundaryANDROID) { |
| 689 | // retrieve VkImage used as render target |
| 690 | VkImage image = VK_NULL_HANDLE; |
| 691 | GrBackendRenderTarget backendRenderTarget = SkSurfaces::GetBackendRenderTarget( |
| 692 | surface, SkSurfaces::BackendHandleAccess::kFlushRead); |
| 693 | if (backendRenderTarget.isValid()) { |
| 694 | GrVkImageInfo info; |
| 695 | if (GrBackendRenderTargets::GetVkImageInfo(backendRenderTarget, &info)) { |
| 696 | image = info.fImage; |
Hugues Evrard | b9510a0 | 2021-03-01 14:35:22 +0000 | [diff] [blame] | 697 | } else { |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 698 | ALOGE("Frame boundary: backend is not vulkan"); |
Hugues Evrard | b9510a0 | 2021-03-01 14:35:22 +0000 | [diff] [blame] | 699 | } |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 700 | } else { |
| 701 | ALOGE("Frame boundary: invalid backend render target"); |
Hugues Evrard | b9510a0 | 2021-03-01 14:35:22 +0000 | [diff] [blame] | 702 | } |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 703 | // frameBoundaryANDROID needs to know about mSwapSemaphore, but |
| 704 | // it won't wait on it. |
| 705 | mFrameBoundaryANDROID(mDevice, sharedSemaphore->semaphore(), image); |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 706 | } |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 707 | VkSemaphoreGetFdInfoKHR getFdInfo; |
| 708 | getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR; |
| 709 | getFdInfo.pNext = nullptr; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 710 | getFdInfo.semaphore = sharedSemaphore->semaphore(); |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 711 | getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 712 | |
| 713 | int fenceFd = -1; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 714 | VkResult err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd); |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 715 | ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd"); |
| 716 | drawResult.presentFence.reset(fenceFd); |
| 717 | } else { |
| 718 | ALOGE("VulkanManager::finishFrame(): Semaphore submission failed"); |
| 719 | mQueueWaitIdle(mGraphicsQueue); |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 720 | } |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 721 | |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 722 | skiapipeline::ShaderCache::get().onVkFrameFlushed(context); |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 723 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 724 | return drawResult; |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 725 | } |
| 726 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 727 | void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect, |
| 728 | android::base::unique_fd&& presentFence) { |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 729 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
| 730 | ATRACE_NAME("Finishing GPU work"); |
| 731 | mDeviceWaitIdle(mDevice); |
| 732 | } |
| 733 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 734 | surface->presentCurrentBuffer(dirtyRect, presentFence.release()); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void VulkanManager::destroySurface(VulkanSurface* surface) { |
| 738 | // Make sure all submit commands have finished before starting to destroy objects. |
Yiwei Zhang | 276d5fc | 2020-09-03 12:00:00 -0700 | [diff] [blame] | 739 | if (VK_NULL_HANDLE != mGraphicsQueue) { |
| 740 | mQueueWaitIdle(mGraphicsQueue); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 741 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 742 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 743 | delete surface; |
| 744 | } |
| 745 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 746 | VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, |
| 747 | ColorMode colorMode, |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 748 | sk_sp<SkColorSpace> surfaceColorSpace, |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 749 | SkColorType surfaceColorType, |
| 750 | GrDirectContext* grContext, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 751 | uint32_t extraBuffers) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 752 | LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized"); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 753 | if (!window) { |
| 754 | return nullptr; |
| 755 | } |
| 756 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 757 | return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 758 | *this, extraBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 759 | } |
| 760 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 761 | status_t VulkanManager::fenceWait(int fence, GrDirectContext* grContext) { |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 762 | if (!hasVkContext()) { |
| 763 | ALOGE("VulkanManager::fenceWait: VkDevice not initialized"); |
| 764 | return INVALID_OPERATION; |
| 765 | } |
| 766 | |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 767 | // Block GPU on the fence. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 768 | int fenceFd = ::dup(fence); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 769 | if (fenceFd == -1) { |
| 770 | ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno); |
| 771 | return -errno; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 772 | } |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 773 | |
| 774 | VkSemaphoreCreateInfo semaphoreInfo; |
| 775 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 776 | semaphoreInfo.pNext = nullptr; |
| 777 | semaphoreInfo.flags = 0; |
| 778 | VkSemaphore semaphore; |
| 779 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 780 | if (VK_SUCCESS != err) { |
Greg Daniel | d667077 | 2021-06-09 12:01:12 -0400 | [diff] [blame] | 781 | close(fenceFd); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 782 | ALOGE("Failed to create import semaphore, err: %d", err); |
| 783 | return UNKNOWN_ERROR; |
| 784 | } |
| 785 | VkImportSemaphoreFdInfoKHR importInfo; |
| 786 | importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR; |
| 787 | importInfo.pNext = nullptr; |
| 788 | importInfo.semaphore = semaphore; |
| 789 | importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT; |
| 790 | importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 791 | importInfo.fd = fenceFd; |
| 792 | |
| 793 | err = mImportSemaphoreFdKHR(mDevice, &importInfo); |
| 794 | if (VK_SUCCESS != err) { |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 795 | mDestroySemaphore(mDevice, semaphore, nullptr); |
Greg Daniel | d667077 | 2021-06-09 12:01:12 -0400 | [diff] [blame] | 796 | close(fenceFd); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 797 | ALOGE("Failed to import semaphore, err: %d", err); |
| 798 | return UNKNOWN_ERROR; |
| 799 | } |
| 800 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 801 | GrBackendSemaphore beSemaphore; |
| 802 | beSemaphore.initVulkan(semaphore); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 803 | |
Greg Daniel | d667077 | 2021-06-09 12:01:12 -0400 | [diff] [blame] | 804 | // 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 Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 806 | grContext->wait(1, &beSemaphore); |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 807 | grContext->flushAndSubmit(); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 808 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 809 | return OK; |
| 810 | } |
| 811 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 812 | status_t VulkanManager::createReleaseFence(int* nativeFence, GrDirectContext* grContext) { |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 813 | *nativeFence = -1; |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 814 | if (!hasVkContext()) { |
| 815 | ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized"); |
| 816 | return INVALID_OPERATION; |
| 817 | } |
| 818 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 819 | 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 Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 835 | auto sharedSemaphore = sp<SharedSemaphoreInfo>::make(mDestroySemaphore, mDevice, semaphore); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 836 | |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 837 | // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 838 | GrFlushInfo flushInfo; |
| 839 | flushInfo.fNumSemaphores = 1; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 840 | flushInfo.fSignalSemaphores = sharedSemaphore->grBackendSemaphore(); |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 841 | flushInfo.fFinishedProc = destroy_semaphore; |
John Reck | 4db23fd | 2023-11-10 15:31:49 -0500 | [diff] [blame] | 842 | sharedSemaphore->incStrong(0); |
| 843 | flushInfo.fFinishedContext = sharedSemaphore.get(); |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 844 | GrSemaphoresSubmitted submitted = grContext->flush(flushInfo); |
| 845 | grContext->submit(); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 846 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 847 | if (submitted == GrSemaphoresSubmitted::kNo) { |
| 848 | ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore"); |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 849 | return INVALID_OPERATION; |
| 850 | } |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 851 | |
| 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 Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 865 | *nativeFence = fenceFd; |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 866 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 867 | return OK; |
| 868 | } |
| 869 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 870 | } /* namespace renderthread */ |
| 871 | } /* namespace uirenderer */ |
| 872 | } /* namespace android */ |