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