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