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