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