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