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