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