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