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 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 19 | #include <private/gui/SyncFeatures.h> |
| 20 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 21 | #include "Properties.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 22 | #include "RenderThread.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 23 | #include "renderstate/RenderState.h" |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 24 | #include "utils/FatVector.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 25 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 26 | #include <GrBackendSurface.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 27 | #include <GrContext.h> |
| 28 | #include <GrTypes.h> |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 29 | #include <GrTypes.h> |
| 30 | #include <vk/GrVkExtensions.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 31 | #include <vk/GrVkTypes.h> |
| 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace renderthread { |
| 36 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 37 | #define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F) |
| 38 | #define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F) |
| 39 | #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] | 40 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 41 | VulkanManager::VulkanManager(RenderThread& thread) : mRenderThread(thread) {} |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 42 | |
| 43 | void VulkanManager::destroy() { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 44 | mRenderThread.setGrContext(nullptr); |
| 45 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 46 | // We don't need to explicitly free the command buffer since it automatically gets freed when we |
| 47 | // delete the VkCommandPool below. |
| 48 | mDummyCB = VK_NULL_HANDLE; |
| 49 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 50 | if (VK_NULL_HANDLE != mCommandPool) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 51 | mDestroyCommandPool(mDevice, mCommandPool, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 52 | mCommandPool = VK_NULL_HANDLE; |
| 53 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 54 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 55 | if (mDevice != VK_NULL_HANDLE) { |
| 56 | mDeviceWaitIdle(mDevice); |
| 57 | mDestroyDevice(mDevice, nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 58 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 59 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 60 | if (mInstance != VK_NULL_HANDLE) { |
| 61 | mDestroyInstance(mInstance, nullptr); |
| 62 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 63 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 64 | mGraphicsQueue = VK_NULL_HANDLE; |
| 65 | mPresentQueue = VK_NULL_HANDLE; |
| 66 | mDevice = VK_NULL_HANDLE; |
| 67 | mPhysicalDevice = VK_NULL_HANDLE; |
| 68 | mInstance = VK_NULL_HANDLE; |
| 69 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 70 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 71 | bool VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 72 | VkResult err; |
| 73 | |
| 74 | constexpr VkApplicationInfo app_info = { |
| 75 | VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType |
| 76 | nullptr, // pNext |
| 77 | "android framework", // pApplicationName |
| 78 | 0, // applicationVersion |
| 79 | "android framework", // pEngineName |
| 80 | 0, // engineVerison |
| 81 | VK_MAKE_VERSION(1, 0, 0), // apiVersion |
| 82 | }; |
| 83 | |
| 84 | std::vector<const char*> instanceExtensions; |
| 85 | { |
| 86 | GET_PROC(EnumerateInstanceExtensionProperties); |
| 87 | |
| 88 | uint32_t extensionCount = 0; |
| 89 | err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); |
| 90 | if (VK_SUCCESS != err) { |
| 91 | return false; |
| 92 | } |
| 93 | std::unique_ptr<VkExtensionProperties[]> extensions( |
| 94 | new VkExtensionProperties[extensionCount]); |
| 95 | err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.get()); |
| 96 | if (VK_SUCCESS != err) { |
| 97 | return false; |
| 98 | } |
| 99 | bool hasKHRSurfaceExtension = false; |
| 100 | bool hasKHRAndroidSurfaceExtension = false; |
| 101 | for (uint32_t i = 0; i < extensionCount; ++i) { |
| 102 | instanceExtensions.push_back(extensions[i].extensionName); |
| 103 | if (!strcmp(extensions[i].extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) { |
| 104 | hasKHRSurfaceExtension = true; |
| 105 | } |
| 106 | if (!strcmp(extensions[i].extensionName,VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) { |
| 107 | hasKHRAndroidSurfaceExtension = true; |
| 108 | } |
| 109 | } |
| 110 | if (!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension) { |
| 111 | this->destroy(); |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | const VkInstanceCreateInfo instance_create = { |
| 117 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType |
| 118 | nullptr, // pNext |
| 119 | 0, // flags |
| 120 | &app_info, // pApplicationInfo |
| 121 | 0, // enabledLayerNameCount |
| 122 | nullptr, // ppEnabledLayerNames |
| 123 | (uint32_t) instanceExtensions.size(), // enabledExtensionNameCount |
| 124 | instanceExtensions.data(), // ppEnabledExtensionNames |
| 125 | }; |
| 126 | |
| 127 | GET_PROC(CreateInstance); |
| 128 | err = mCreateInstance(&instance_create, nullptr, &mInstance); |
| 129 | if (err < 0) { |
| 130 | this->destroy(); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | GET_INST_PROC(DestroyInstance); |
| 135 | GET_INST_PROC(EnumeratePhysicalDevices); |
| 136 | GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 137 | GET_INST_PROC(GetPhysicalDeviceFeatures2); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 138 | GET_INST_PROC(CreateDevice); |
| 139 | GET_INST_PROC(EnumerateDeviceExtensionProperties); |
| 140 | GET_INST_PROC(CreateAndroidSurfaceKHR); |
| 141 | GET_INST_PROC(DestroySurfaceKHR); |
| 142 | GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR); |
| 143 | GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 144 | GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR); |
| 145 | GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR); |
| 146 | |
| 147 | uint32_t gpuCount; |
| 148 | err = mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr); |
| 149 | if (err) { |
| 150 | this->destroy(); |
| 151 | return false; |
| 152 | } |
| 153 | if (!gpuCount) { |
| 154 | this->destroy(); |
| 155 | return false; |
| 156 | } |
| 157 | // Just returning the first physical device instead of getting the whole array. Since there |
| 158 | // should only be one device on android. |
| 159 | gpuCount = 1; |
| 160 | err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice); |
| 161 | // VK_INCOMPLETE is returned when the count we provide is less than the total device count. |
| 162 | if (err && VK_INCOMPLETE != err) { |
| 163 | this->destroy(); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // query to get the initial queue props size |
| 168 | uint32_t queueCount; |
| 169 | mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
| 170 | if (!queueCount) { |
| 171 | this->destroy(); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // now get the actual queue props |
| 176 | std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]); |
| 177 | mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get()); |
| 178 | |
| 179 | // iterate to find the graphics queue |
| 180 | mGraphicsQueueIndex = queueCount; |
| 181 | for (uint32_t i = 0; i < queueCount; i++) { |
| 182 | if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
| 183 | mGraphicsQueueIndex = i; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if (mGraphicsQueueIndex == queueCount) { |
| 188 | this->destroy(); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // All physical devices and queue families on Android must be capable of |
| 193 | // presentation with any native window. So just use the first one. |
| 194 | mPresentQueueIndex = 0; |
| 195 | |
| 196 | std::vector<const char*> deviceExtensions; |
| 197 | { |
| 198 | uint32_t extensionCount = 0; |
| 199 | err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount, |
| 200 | nullptr); |
| 201 | if (VK_SUCCESS != err) { |
| 202 | this->destroy(); |
| 203 | return false; |
| 204 | } |
| 205 | std::unique_ptr<VkExtensionProperties[]> extensions( |
| 206 | new VkExtensionProperties[extensionCount]); |
| 207 | err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount, |
| 208 | extensions.get()); |
| 209 | if (VK_SUCCESS != err) { |
| 210 | this->destroy(); |
| 211 | return false; |
| 212 | } |
| 213 | bool hasKHRSwapchainExtension = false; |
| 214 | for (uint32_t i = 0; i < extensionCount; ++i) { |
| 215 | deviceExtensions.push_back(extensions[i].extensionName); |
| 216 | if (!strcmp(extensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) { |
| 217 | hasKHRSwapchainExtension = true; |
| 218 | } |
| 219 | } |
| 220 | if (!hasKHRSwapchainExtension) { |
| 221 | this->destroy(); |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 226 | auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) { |
| 227 | if (device != VK_NULL_HANDLE) { |
| 228 | return vkGetDeviceProcAddr(device, proc_name); |
| 229 | } |
| 230 | return vkGetInstanceProcAddr(instance, proc_name); |
| 231 | }; |
| 232 | grExtensions.init(getProc, mInstance, mPhysicalDevice, instanceExtensions.size(), |
| 233 | instanceExtensions.data(), deviceExtensions.size(), deviceExtensions.data()); |
| 234 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 235 | if (!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1)) { |
| 236 | this->destroy(); |
| 237 | return false; |
| 238 | } |
| 239 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 240 | memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2)); |
| 241 | features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 242 | features.pNext = nullptr; |
| 243 | |
| 244 | // Setup all extension feature structs we may want to use. |
| 245 | void** tailPNext = &features.pNext; |
| 246 | |
| 247 | if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) { |
| 248 | VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend; |
| 249 | blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*) malloc( |
| 250 | sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT)); |
| 251 | LOG_ALWAYS_FATAL_IF(!blend); |
| 252 | blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT; |
| 253 | blend->pNext = nullptr; |
| 254 | *tailPNext = blend; |
| 255 | tailPNext = &blend->pNext; |
| 256 | } |
| 257 | |
| 258 | // query to get the physical device features |
| 259 | mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 260 | // this looks like it would slow things down, |
| 261 | // and we can't depend on it on all platforms |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 262 | features.features.robustBufferAccess = VK_FALSE; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 263 | |
| 264 | float queuePriorities[1] = { 0.0 }; |
| 265 | |
| 266 | const VkDeviceQueueCreateInfo queueInfo[2] = { |
| 267 | { |
| 268 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 269 | nullptr, // pNext |
| 270 | 0, // VkDeviceQueueCreateFlags |
| 271 | mGraphicsQueueIndex, // queueFamilyIndex |
| 272 | 1, // queueCount |
| 273 | queuePriorities, // pQueuePriorities |
| 274 | }, |
| 275 | { |
| 276 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 277 | nullptr, // pNext |
| 278 | 0, // VkDeviceQueueCreateFlags |
| 279 | mPresentQueueIndex, // queueFamilyIndex |
| 280 | 1, // queueCount |
| 281 | queuePriorities, // pQueuePriorities |
| 282 | } |
| 283 | }; |
| 284 | uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1; |
| 285 | |
| 286 | const VkDeviceCreateInfo deviceInfo = { |
| 287 | VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 288 | &features, // pNext |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 289 | 0, // VkDeviceCreateFlags |
| 290 | queueInfoCount, // queueCreateInfoCount |
| 291 | queueInfo, // pQueueCreateInfos |
| 292 | 0, // layerCount |
| 293 | nullptr, // ppEnabledLayerNames |
| 294 | (uint32_t) deviceExtensions.size(), // extensionCount |
| 295 | deviceExtensions.data(), // ppEnabledExtensionNames |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 296 | nullptr, // ppEnabledFeatures |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | err = mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice); |
| 300 | if (err) { |
| 301 | this->destroy(); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | GET_DEV_PROC(GetDeviceQueue); |
| 306 | GET_DEV_PROC(DeviceWaitIdle); |
| 307 | GET_DEV_PROC(DestroyDevice); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 308 | GET_DEV_PROC(CreateSwapchainKHR); |
| 309 | GET_DEV_PROC(DestroySwapchainKHR); |
| 310 | GET_DEV_PROC(GetSwapchainImagesKHR); |
| 311 | GET_DEV_PROC(AcquireNextImageKHR); |
| 312 | GET_DEV_PROC(QueuePresentKHR); |
| 313 | GET_DEV_PROC(CreateCommandPool); |
| 314 | GET_DEV_PROC(DestroyCommandPool); |
| 315 | GET_DEV_PROC(AllocateCommandBuffers); |
| 316 | GET_DEV_PROC(FreeCommandBuffers); |
| 317 | GET_DEV_PROC(ResetCommandBuffer); |
| 318 | GET_DEV_PROC(BeginCommandBuffer); |
| 319 | GET_DEV_PROC(EndCommandBuffer); |
| 320 | GET_DEV_PROC(CmdPipelineBarrier); |
| 321 | GET_DEV_PROC(GetDeviceQueue); |
| 322 | GET_DEV_PROC(QueueSubmit); |
| 323 | GET_DEV_PROC(QueueWaitIdle); |
| 324 | GET_DEV_PROC(DeviceWaitIdle); |
| 325 | GET_DEV_PROC(CreateSemaphore); |
| 326 | GET_DEV_PROC(DestroySemaphore); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 327 | GET_DEV_PROC(ImportSemaphoreFdKHR); |
| 328 | GET_DEV_PROC(GetSemaphoreFdKHR); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 329 | GET_DEV_PROC(CreateFence); |
| 330 | GET_DEV_PROC(DestroyFence); |
| 331 | GET_DEV_PROC(WaitForFences); |
| 332 | GET_DEV_PROC(ResetFences); |
| 333 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 337 | static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) { |
| 338 | // All Vulkan structs that could be part of the features chain will start with the |
| 339 | // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader |
| 340 | // so we can get access to the pNext for the next struct. |
| 341 | struct CommonVulkanHeader { |
| 342 | VkStructureType sType; |
| 343 | void* pNext; |
| 344 | }; |
| 345 | |
| 346 | void* pNext = features.pNext; |
| 347 | while (pNext) { |
| 348 | void* current = pNext; |
| 349 | pNext = static_cast<CommonVulkanHeader*>(current)->pNext; |
| 350 | free(current); |
| 351 | } |
| 352 | } |
| 353 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 354 | void VulkanManager::initialize() { |
| 355 | if (mDevice != VK_NULL_HANDLE) { |
| 356 | return; |
| 357 | } |
| 358 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 359 | GET_PROC(EnumerateInstanceVersion); |
| 360 | uint32_t instanceVersion = 0; |
| 361 | LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion)); |
| 362 | LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0)); |
| 363 | |
| 364 | GrVkExtensions extensions; |
| 365 | VkPhysicalDeviceFeatures2 features; |
| 366 | LOG_ALWAYS_FATAL_IF(!this->setupDevice(extensions, features)); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 367 | |
| 368 | mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue); |
| 369 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 370 | auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) { |
| 371 | if (device != VK_NULL_HANDLE) { |
| 372 | return vkGetDeviceProcAddr(device, proc_name); |
| 373 | } |
| 374 | return vkGetInstanceProcAddr(instance, proc_name); |
| 375 | }; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 376 | |
| 377 | GrVkBackendContext backendContext; |
| 378 | backendContext.fInstance = mInstance; |
| 379 | backendContext.fPhysicalDevice = mPhysicalDevice; |
| 380 | backendContext.fDevice = mDevice; |
| 381 | backendContext.fQueue = mGraphicsQueue; |
| 382 | backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex; |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 383 | backendContext.fInstanceVersion = instanceVersion; |
| 384 | backendContext.fVkExtensions = &extensions; |
| 385 | backendContext.fDeviceFeatures2 = &features; |
Greg Daniel | 4aa5867 | 2018-07-13 13:10:36 -0400 | [diff] [blame] | 386 | backendContext.fGetProc = std::move(getProc); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 387 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 388 | // create the command pool for the command buffers |
| 389 | if (VK_NULL_HANDLE == mCommandPool) { |
| 390 | VkCommandPoolCreateInfo commandPoolInfo; |
| 391 | memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); |
| 392 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 393 | // this needs to be on the render queue |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 394 | commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 395 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 396 | SkDEBUGCODE(VkResult res =) mCreateCommandPool(mDevice, &commandPoolInfo, nullptr, |
| 397 | &mCommandPool); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 398 | SkASSERT(VK_SUCCESS == res); |
| 399 | } |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 400 | LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE); |
| 401 | |
| 402 | if (!setupDummyCommandBuffer()) { |
| 403 | this->destroy(); |
| 404 | return; |
| 405 | } |
| 406 | LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE); |
| 407 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 408 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 409 | mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 410 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 411 | GrContextOptions options; |
| 412 | options.fDisableDistanceFieldPaths = true; |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 413 | // TODO: get a string describing the SPIR-V compiler version and use it here |
| 414 | mRenderThread.cacheManager().configureContext(&options, nullptr, 0); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 415 | sk_sp<GrContext> grContext(GrContext::MakeVulkan(backendContext, options)); |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 416 | LOG_ALWAYS_FATAL_IF(!grContext.get()); |
| 417 | mRenderThread.setGrContext(grContext); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 418 | |
| 419 | free_features_extensions_structs(features); |
| 420 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 421 | if (Properties::enablePartialUpdates && Properties::useBufferAge) { |
| 422 | mSwapBehavior = SwapBehavior::BufferAge; |
| 423 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Returns the next BackbufferInfo to use for the next draw. The function will make sure all |
| 427 | // previous uses have finished before returning. |
| 428 | VulkanSurface::BackbufferInfo* VulkanManager::getAvailableBackbuffer(VulkanSurface* surface) { |
| 429 | SkASSERT(surface->mBackbuffers); |
| 430 | |
| 431 | ++surface->mCurrentBackbufferIndex; |
| 432 | if (surface->mCurrentBackbufferIndex > surface->mImageCount) { |
| 433 | surface->mCurrentBackbufferIndex = 0; |
| 434 | } |
| 435 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 436 | VulkanSurface::BackbufferInfo* backbuffer = |
| 437 | surface->mBackbuffers + surface->mCurrentBackbufferIndex; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 438 | |
| 439 | // Before we reuse a backbuffer, make sure its fences have all signaled so that we can safely |
| 440 | // reuse its commands buffers. |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 441 | VkResult res = mWaitForFences(mDevice, 2, backbuffer->mUsageFences, true, UINT64_MAX); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 442 | if (res != VK_SUCCESS) { |
| 443 | return nullptr; |
| 444 | } |
| 445 | |
| 446 | return backbuffer; |
| 447 | } |
| 448 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 449 | SkSurface* VulkanManager::getBackbufferSurface(VulkanSurface* surface) { |
| 450 | VulkanSurface::BackbufferInfo* backbuffer = getAvailableBackbuffer(surface); |
| 451 | SkASSERT(backbuffer); |
| 452 | |
| 453 | VkResult res; |
| 454 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 455 | res = mResetFences(mDevice, 2, backbuffer->mUsageFences); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 456 | SkASSERT(VK_SUCCESS == res); |
| 457 | |
| 458 | // The acquire will signal the attached mAcquireSemaphore. We use this to know the image has |
| 459 | // finished presenting and that it is safe to begin sending new commands to the returned image. |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 460 | res = mAcquireNextImageKHR(mDevice, surface->mSwapchain, UINT64_MAX, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 461 | backbuffer->mAcquireSemaphore, VK_NULL_HANDLE, |
| 462 | &backbuffer->mImageIndex); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 463 | |
| 464 | if (VK_ERROR_SURFACE_LOST_KHR == res) { |
| 465 | // need to figure out how to create a new vkSurface without the platformData* |
| 466 | // maybe use attach somehow? but need a Window |
| 467 | return nullptr; |
| 468 | } |
| 469 | if (VK_ERROR_OUT_OF_DATE_KHR == res) { |
| 470 | // tear swapchain down and try again |
| 471 | if (!createSwapchain(surface)) { |
| 472 | return nullptr; |
| 473 | } |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 474 | backbuffer = getAvailableBackbuffer(surface); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 475 | res = mResetFences(mDevice, 2, backbuffer->mUsageFences); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 476 | SkASSERT(VK_SUCCESS == res); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 477 | |
| 478 | // acquire the image |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 479 | res = mAcquireNextImageKHR(mDevice, surface->mSwapchain, UINT64_MAX, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 480 | backbuffer->mAcquireSemaphore, VK_NULL_HANDLE, |
| 481 | &backbuffer->mImageIndex); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 482 | |
| 483 | if (VK_SUCCESS != res) { |
| 484 | return nullptr; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // set up layout transfer from initial to color attachment |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 489 | VkImageLayout layout = surface->mImageInfos[backbuffer->mImageIndex].mImageLayout; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 490 | SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 491 | VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) |
| 492 | ? VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT |
| 493 | : VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 494 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 495 | VkAccessFlags srcAccessMask = |
| 496 | (VK_IMAGE_LAYOUT_UNDEFINED == layout) ? 0 : VK_ACCESS_MEMORY_READ_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 497 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 498 | |
| 499 | VkImageMemoryBarrier imageMemoryBarrier = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 500 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 501 | NULL, // pNext |
| 502 | srcAccessMask, // outputMask |
| 503 | dstAccessMask, // inputMask |
| 504 | layout, // oldLayout |
| 505 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout |
| 506 | mPresentQueueIndex, // srcQueueFamilyIndex |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 507 | mGraphicsQueueIndex, // dstQueueFamilyIndex |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 508 | surface->mImages[backbuffer->mImageIndex], // image |
| 509 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} // subresourceRange |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 510 | }; |
| 511 | mResetCommandBuffer(backbuffer->mTransitionCmdBuffers[0], 0); |
| 512 | |
| 513 | VkCommandBufferBeginInfo info; |
| 514 | memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); |
| 515 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 516 | info.flags = 0; |
| 517 | mBeginCommandBuffer(backbuffer->mTransitionCmdBuffers[0], &info); |
| 518 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 519 | mCmdPipelineBarrier(backbuffer->mTransitionCmdBuffers[0], srcStageMask, dstStageMask, 0, 0, |
| 520 | nullptr, 0, nullptr, 1, &imageMemoryBarrier); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 521 | |
| 522 | mEndCommandBuffer(backbuffer->mTransitionCmdBuffers[0]); |
| 523 | |
| 524 | VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 525 | // insert the layout transfer into the queue and wait on the acquire |
| 526 | VkSubmitInfo submitInfo; |
| 527 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 528 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 529 | submitInfo.waitSemaphoreCount = 1; |
| 530 | // Wait to make sure aquire semaphore set above has signaled. |
| 531 | submitInfo.pWaitSemaphores = &backbuffer->mAcquireSemaphore; |
| 532 | submitInfo.pWaitDstStageMask = &waitDstStageFlags; |
| 533 | submitInfo.commandBufferCount = 1; |
| 534 | submitInfo.pCommandBuffers = &backbuffer->mTransitionCmdBuffers[0]; |
| 535 | submitInfo.signalSemaphoreCount = 0; |
| 536 | |
| 537 | // Attach first fence to submission here so we can track when the command buffer finishes. |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 538 | mQueueSubmit(mGraphicsQueue, 1, &submitInfo, backbuffer->mUsageFences[0]); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 539 | |
| 540 | // We need to notify Skia that we changed the layout of the wrapped VkImage |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 541 | sk_sp<SkSurface> skSurface = surface->mImageInfos[backbuffer->mImageIndex].mSurface; |
Greg Daniel | 1834a8c | 2018-04-12 12:22:43 -0400 | [diff] [blame] | 542 | GrBackendRenderTarget backendRT = skSurface->getBackendRenderTarget( |
| 543 | SkSurface::kFlushRead_BackendHandleAccess); |
| 544 | if (!backendRT.isValid()) { |
| 545 | SkASSERT(backendRT.isValid()); |
| 546 | return nullptr; |
| 547 | } |
| 548 | backendRT.setVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 549 | |
| 550 | surface->mBackbuffer = std::move(skSurface); |
| 551 | return surface->mBackbuffer.get(); |
| 552 | } |
| 553 | |
| 554 | void VulkanManager::destroyBuffers(VulkanSurface* surface) { |
| 555 | if (surface->mBackbuffers) { |
| 556 | for (uint32_t i = 0; i < surface->mImageCount + 1; ++i) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 557 | mWaitForFences(mDevice, 2, surface->mBackbuffers[i].mUsageFences, true, UINT64_MAX); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 558 | surface->mBackbuffers[i].mImageIndex = -1; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 559 | mDestroySemaphore(mDevice, surface->mBackbuffers[i].mAcquireSemaphore, nullptr); |
| 560 | mDestroySemaphore(mDevice, surface->mBackbuffers[i].mRenderSemaphore, nullptr); |
| 561 | mFreeCommandBuffers(mDevice, mCommandPool, 2, |
| 562 | surface->mBackbuffers[i].mTransitionCmdBuffers); |
| 563 | mDestroyFence(mDevice, surface->mBackbuffers[i].mUsageFences[0], 0); |
| 564 | mDestroyFence(mDevice, surface->mBackbuffers[i].mUsageFences[1], 0); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | delete[] surface->mBackbuffers; |
| 569 | surface->mBackbuffers = nullptr; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 570 | delete[] surface->mImageInfos; |
| 571 | surface->mImageInfos = nullptr; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 572 | delete[] surface->mImages; |
| 573 | surface->mImages = nullptr; |
| 574 | } |
| 575 | |
| 576 | void VulkanManager::destroySurface(VulkanSurface* surface) { |
| 577 | // Make sure all submit commands have finished before starting to destroy objects. |
| 578 | if (VK_NULL_HANDLE != mPresentQueue) { |
| 579 | mQueueWaitIdle(mPresentQueue); |
| 580 | } |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 581 | mDeviceWaitIdle(mDevice); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 582 | |
| 583 | destroyBuffers(surface); |
| 584 | |
| 585 | if (VK_NULL_HANDLE != surface->mSwapchain) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 586 | mDestroySwapchainKHR(mDevice, surface->mSwapchain, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 587 | surface->mSwapchain = VK_NULL_HANDLE; |
| 588 | } |
| 589 | |
| 590 | if (VK_NULL_HANDLE != surface->mVkSurface) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 591 | mDestroySurfaceKHR(mInstance, surface->mVkSurface, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 592 | surface->mVkSurface = VK_NULL_HANDLE; |
| 593 | } |
| 594 | delete surface; |
| 595 | } |
| 596 | |
| 597 | void VulkanManager::createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 598 | mGetSwapchainImagesKHR(mDevice, surface->mSwapchain, &surface->mImageCount, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 599 | SkASSERT(surface->mImageCount); |
| 600 | surface->mImages = new VkImage[surface->mImageCount]; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 601 | mGetSwapchainImagesKHR(mDevice, surface->mSwapchain, &surface->mImageCount, surface->mImages); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 602 | |
| 603 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 604 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 605 | // set up initial image layouts and create surfaces |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 606 | surface->mImageInfos = new VulkanSurface::ImageInfo[surface->mImageCount]; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 607 | for (uint32_t i = 0; i < surface->mImageCount; ++i) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 608 | GrVkImageInfo info; |
| 609 | info.fImage = surface->mImages[i]; |
Greg Daniel | c9a8945 | 2018-02-23 13:16:12 -0500 | [diff] [blame] | 610 | info.fAlloc = GrVkAlloc(); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 611 | info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 612 | info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 613 | info.fFormat = format; |
| 614 | info.fLevelCount = 1; |
| 615 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 616 | GrBackendRenderTarget backendRT(extent.width, extent.height, 0, 0, info); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 617 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 618 | VulkanSurface::ImageInfo& imageInfo = surface->mImageInfos[i]; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 619 | imageInfo.mSurface = SkSurface::MakeFromBackendRenderTarget( |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 620 | mRenderThread.getGrContext(), backendRT, kTopLeft_GrSurfaceOrigin, |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 621 | surface->mColorMode == ColorMode::WideColorGamut ? kRGBA_F16_SkColorType |
| 622 | : kRGBA_8888_SkColorType, nullptr, &props); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | SkASSERT(mCommandPool != VK_NULL_HANDLE); |
| 626 | |
| 627 | // set up the backbuffers |
| 628 | VkSemaphoreCreateInfo semaphoreInfo; |
| 629 | memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo)); |
| 630 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 631 | semaphoreInfo.pNext = nullptr; |
| 632 | semaphoreInfo.flags = 0; |
| 633 | VkCommandBufferAllocateInfo commandBuffersInfo; |
| 634 | memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo)); |
| 635 | commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 636 | commandBuffersInfo.pNext = nullptr; |
| 637 | commandBuffersInfo.commandPool = mCommandPool; |
| 638 | commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 639 | commandBuffersInfo.commandBufferCount = 2; |
| 640 | VkFenceCreateInfo fenceInfo; |
| 641 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 642 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 643 | fenceInfo.pNext = nullptr; |
| 644 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
| 645 | |
| 646 | // we create one additional backbuffer structure here, because we want to |
| 647 | // give the command buffers they contain a chance to finish before we cycle back |
| 648 | surface->mBackbuffers = new VulkanSurface::BackbufferInfo[surface->mImageCount + 1]; |
| 649 | for (uint32_t i = 0; i < surface->mImageCount + 1; ++i) { |
| 650 | SkDEBUGCODE(VkResult res); |
| 651 | surface->mBackbuffers[i].mImageIndex = -1; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 652 | SkDEBUGCODE(res =) mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 653 | &surface->mBackbuffers[i].mAcquireSemaphore); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 654 | SkDEBUGCODE(res =) mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 655 | &surface->mBackbuffers[i].mRenderSemaphore); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 656 | SkDEBUGCODE(res =) mAllocateCommandBuffers(mDevice, &commandBuffersInfo, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 657 | surface->mBackbuffers[i].mTransitionCmdBuffers); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 658 | SkDEBUGCODE(res =) mCreateFence(mDevice, &fenceInfo, nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 659 | &surface->mBackbuffers[i].mUsageFences[0]); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 660 | SkDEBUGCODE(res =) mCreateFence(mDevice, &fenceInfo, nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 661 | &surface->mBackbuffers[i].mUsageFences[1]); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 662 | SkASSERT(VK_SUCCESS == res); |
| 663 | } |
| 664 | surface->mCurrentBackbufferIndex = surface->mImageCount; |
| 665 | } |
| 666 | |
| 667 | bool VulkanManager::createSwapchain(VulkanSurface* surface) { |
| 668 | // check for capabilities |
| 669 | VkSurfaceCapabilitiesKHR caps; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 670 | VkResult res = mGetPhysicalDeviceSurfaceCapabilitiesKHR(mPhysicalDevice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 671 | surface->mVkSurface, &caps); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 672 | if (VK_SUCCESS != res) { |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | uint32_t surfaceFormatCount; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 677 | res = mGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDevice, surface->mVkSurface, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 678 | &surfaceFormatCount, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 679 | if (VK_SUCCESS != res) { |
| 680 | return false; |
| 681 | } |
| 682 | |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 683 | FatVector<VkSurfaceFormatKHR, 4> surfaceFormats(surfaceFormatCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 684 | res = mGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDevice, surface->mVkSurface, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 685 | &surfaceFormatCount, surfaceFormats.data()); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 686 | if (VK_SUCCESS != res) { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | uint32_t presentModeCount; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 691 | res = mGetPhysicalDeviceSurfacePresentModesKHR(mPhysicalDevice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 692 | surface->mVkSurface, &presentModeCount, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 693 | if (VK_SUCCESS != res) { |
| 694 | return false; |
| 695 | } |
| 696 | |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 697 | FatVector<VkPresentModeKHR, VK_PRESENT_MODE_RANGE_SIZE_KHR> presentModes(presentModeCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 698 | res = mGetPhysicalDeviceSurfacePresentModesKHR(mPhysicalDevice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 699 | surface->mVkSurface, &presentModeCount, |
| 700 | presentModes.data()); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 701 | if (VK_SUCCESS != res) { |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | VkExtent2D extent = caps.currentExtent; |
| 706 | // clamp width; to handle currentExtent of -1 and protect us from broken hints |
| 707 | if (extent.width < caps.minImageExtent.width) { |
| 708 | extent.width = caps.minImageExtent.width; |
| 709 | } |
| 710 | SkASSERT(extent.width <= caps.maxImageExtent.width); |
| 711 | // clamp height |
| 712 | if (extent.height < caps.minImageExtent.height) { |
| 713 | extent.height = caps.minImageExtent.height; |
| 714 | } |
| 715 | SkASSERT(extent.height <= caps.maxImageExtent.height); |
| 716 | |
| 717 | uint32_t imageCount = caps.minImageCount + 2; |
| 718 | if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) { |
| 719 | // Application must settle for fewer images than desired: |
| 720 | imageCount = caps.maxImageCount; |
| 721 | } |
| 722 | |
| 723 | // Currently Skia requires the images to be color attchments and support all transfer |
| 724 | // operations. |
| 725 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 726 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 727 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 728 | SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); |
| 729 | SkASSERT(caps.supportedTransforms & caps.currentTransform); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 730 | SkASSERT(caps.supportedCompositeAlpha & |
| 731 | (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 732 | VkCompositeAlphaFlagBitsKHR composite_alpha = |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 733 | (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) |
| 734 | ? VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
| 735 | : VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 736 | |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 737 | VkFormat surfaceFormat = VK_FORMAT_R8G8B8A8_UNORM; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 738 | VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 739 | if (surface->mColorMode == ColorMode::WideColorGamut) { |
| 740 | surfaceFormat = VK_FORMAT_R16G16B16A16_SFLOAT; |
| 741 | colorSpace = VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT; |
| 742 | } |
| 743 | bool foundSurfaceFormat = false; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 744 | for (uint32_t i = 0; i < surfaceFormatCount; ++i) { |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 745 | if (surfaceFormat == surfaceFormats[i].format |
| 746 | && colorSpace == surfaceFormats[i].colorSpace) { |
| 747 | foundSurfaceFormat = true; |
| 748 | break; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 752 | if (!foundSurfaceFormat) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 753 | return false; |
| 754 | } |
| 755 | |
| 756 | // If mailbox mode is available, use it, as it is the lowest-latency non- |
| 757 | // tearing mode. If not, fall back to FIFO which is always available. |
| 758 | VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; |
| 759 | for (uint32_t i = 0; i < presentModeCount; ++i) { |
| 760 | // use mailbox |
| 761 | if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) { |
| 762 | mode = presentModes[i]; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | VkSwapchainCreateInfoKHR swapchainCreateInfo; |
| 768 | memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR)); |
| 769 | swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 770 | swapchainCreateInfo.surface = surface->mVkSurface; |
| 771 | swapchainCreateInfo.minImageCount = imageCount; |
| 772 | swapchainCreateInfo.imageFormat = surfaceFormat; |
| 773 | swapchainCreateInfo.imageColorSpace = colorSpace; |
| 774 | swapchainCreateInfo.imageExtent = extent; |
| 775 | swapchainCreateInfo.imageArrayLayers = 1; |
| 776 | swapchainCreateInfo.imageUsage = usageFlags; |
| 777 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 778 | uint32_t queueFamilies[] = {mGraphicsQueueIndex, mPresentQueueIndex}; |
| 779 | if (mGraphicsQueueIndex != mPresentQueueIndex) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 780 | swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; |
| 781 | swapchainCreateInfo.queueFamilyIndexCount = 2; |
| 782 | swapchainCreateInfo.pQueueFamilyIndices = queueFamilies; |
| 783 | } else { |
| 784 | swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 785 | swapchainCreateInfo.queueFamilyIndexCount = 0; |
| 786 | swapchainCreateInfo.pQueueFamilyIndices = nullptr; |
| 787 | } |
| 788 | |
| 789 | swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 790 | swapchainCreateInfo.compositeAlpha = composite_alpha; |
| 791 | swapchainCreateInfo.presentMode = mode; |
| 792 | swapchainCreateInfo.clipped = true; |
| 793 | swapchainCreateInfo.oldSwapchain = surface->mSwapchain; |
| 794 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 795 | res = mCreateSwapchainKHR(mDevice, &swapchainCreateInfo, nullptr, &surface->mSwapchain); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 796 | if (VK_SUCCESS != res) { |
| 797 | return false; |
| 798 | } |
| 799 | |
| 800 | // destroy the old swapchain |
| 801 | if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 802 | mDeviceWaitIdle(mDevice); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 803 | |
| 804 | destroyBuffers(surface); |
| 805 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 806 | mDestroySwapchainKHR(mDevice, swapchainCreateInfo.oldSwapchain, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | createBuffers(surface, surfaceFormat, extent); |
| 810 | |
| 811 | return true; |
| 812 | } |
| 813 | |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 814 | VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 815 | initialize(); |
| 816 | |
| 817 | if (!window) { |
| 818 | return nullptr; |
| 819 | } |
| 820 | |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame^] | 821 | VulkanSurface* surface = new VulkanSurface(colorMode); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 822 | |
| 823 | VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo; |
| 824 | memset(&surfaceCreateInfo, 0, sizeof(VkAndroidSurfaceCreateInfoKHR)); |
| 825 | surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; |
| 826 | surfaceCreateInfo.pNext = nullptr; |
| 827 | surfaceCreateInfo.flags = 0; |
| 828 | surfaceCreateInfo.window = window; |
| 829 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 830 | VkResult res = mCreateAndroidSurfaceKHR(mInstance, &surfaceCreateInfo, nullptr, |
| 831 | &surface->mVkSurface); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 832 | if (VK_SUCCESS != res) { |
| 833 | delete surface; |
| 834 | return nullptr; |
| 835 | } |
| 836 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 837 | SkDEBUGCODE(VkBool32 supported; res = mGetPhysicalDeviceSurfaceSupportKHR( |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 838 | mPhysicalDevice, mPresentQueueIndex, surface->mVkSurface, &supported); |
| 839 | // All physical devices and queue families on Android must be capable of |
| 840 | // presentation with any native window. |
| 841 | SkASSERT(VK_SUCCESS == res && supported);); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 842 | |
| 843 | if (!createSwapchain(surface)) { |
| 844 | destroySurface(surface); |
| 845 | return nullptr; |
| 846 | } |
| 847 | |
| 848 | return surface; |
| 849 | } |
| 850 | |
| 851 | // Helper to know which src stage flags we need to set when transitioning to the present layout |
| 852 | static VkPipelineStageFlags layoutToPipelineStageFlags(const VkImageLayout layout) { |
| 853 | if (VK_IMAGE_LAYOUT_GENERAL == layout) { |
| 854 | return VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 855 | } else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout || |
| 856 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { |
| 857 | return VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 858 | } else if (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == layout || |
| 859 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL == layout || |
| 860 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL == layout || |
| 861 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) { |
| 862 | return VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; |
| 863 | } else if (VK_IMAGE_LAYOUT_PREINITIALIZED == layout) { |
| 864 | return VK_PIPELINE_STAGE_HOST_BIT; |
| 865 | } |
| 866 | |
| 867 | SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout); |
| 868 | return VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 869 | } |
| 870 | |
| 871 | // Helper to know which src access mask we need to set when transitioning to the present layout |
| 872 | static VkAccessFlags layoutToSrcAccessMask(const VkImageLayout layout) { |
| 873 | VkAccessFlags flags = 0; |
| 874 | if (VK_IMAGE_LAYOUT_GENERAL == layout) { |
| 875 | flags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 876 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT | |
| 877 | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_HOST_WRITE_BIT | |
| 878 | VK_ACCESS_HOST_READ_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 879 | } else if (VK_IMAGE_LAYOUT_PREINITIALIZED == layout) { |
| 880 | flags = VK_ACCESS_HOST_WRITE_BIT; |
| 881 | } else if (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == layout) { |
| 882 | flags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 883 | } else if (VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL == layout) { |
| 884 | flags = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 885 | } else if (VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { |
| 886 | flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 887 | } else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout) { |
| 888 | flags = VK_ACCESS_TRANSFER_READ_BIT; |
| 889 | } else if (VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) { |
| 890 | flags = VK_ACCESS_SHADER_READ_BIT; |
| 891 | } |
| 892 | return flags; |
| 893 | } |
| 894 | |
| 895 | void VulkanManager::swapBuffers(VulkanSurface* surface) { |
Greg Daniel | 4f70887 | 2017-02-03 10:23:39 -0500 | [diff] [blame] | 896 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
| 897 | ATRACE_NAME("Finishing GPU work"); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 898 | mDeviceWaitIdle(mDevice); |
Greg Daniel | 4f70887 | 2017-02-03 10:23:39 -0500 | [diff] [blame] | 899 | } |
| 900 | |
Greg Daniel | 74ea201 | 2017-11-10 11:32:58 -0500 | [diff] [blame] | 901 | SkASSERT(surface->mBackbuffers); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 902 | VulkanSurface::BackbufferInfo* backbuffer = |
| 903 | surface->mBackbuffers + surface->mCurrentBackbufferIndex; |
Greg Daniel | 1834a8c | 2018-04-12 12:22:43 -0400 | [diff] [blame] | 904 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 905 | SkSurface* skSurface = surface->mImageInfos[backbuffer->mImageIndex].mSurface.get(); |
Greg Daniel | 1834a8c | 2018-04-12 12:22:43 -0400 | [diff] [blame] | 906 | GrBackendRenderTarget backendRT = skSurface->getBackendRenderTarget( |
| 907 | SkSurface::kFlushRead_BackendHandleAccess); |
| 908 | SkASSERT(backendRT.isValid()); |
| 909 | |
| 910 | GrVkImageInfo imageInfo; |
| 911 | SkAssertResult(backendRT.getVkImageInfo(&imageInfo)); |
| 912 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 913 | // Check to make sure we never change the actually wrapped image |
Greg Daniel | 1834a8c | 2018-04-12 12:22:43 -0400 | [diff] [blame] | 914 | SkASSERT(imageInfo.fImage == surface->mImages[backbuffer->mImageIndex]); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 915 | |
| 916 | // We need to transition the image to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR and make sure that all |
| 917 | // previous work is complete for before presenting. So we first add the necessary barrier here. |
Greg Daniel | 1834a8c | 2018-04-12 12:22:43 -0400 | [diff] [blame] | 918 | VkImageLayout layout = imageInfo.fImageLayout; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 919 | VkPipelineStageFlags srcStageMask = layoutToPipelineStageFlags(layout); |
| 920 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 921 | VkAccessFlags srcAccessMask = layoutToSrcAccessMask(layout); |
| 922 | VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; |
| 923 | |
| 924 | VkImageMemoryBarrier imageMemoryBarrier = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 925 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 926 | NULL, // pNext |
| 927 | srcAccessMask, // outputMask |
| 928 | dstAccessMask, // inputMask |
| 929 | layout, // oldLayout |
| 930 | VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 931 | mGraphicsQueueIndex, // srcQueueFamilyIndex |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 932 | mPresentQueueIndex, // dstQueueFamilyIndex |
| 933 | surface->mImages[backbuffer->mImageIndex], // image |
| 934 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} // subresourceRange |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 935 | }; |
| 936 | |
| 937 | mResetCommandBuffer(backbuffer->mTransitionCmdBuffers[1], 0); |
| 938 | VkCommandBufferBeginInfo info; |
| 939 | memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); |
| 940 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 941 | info.flags = 0; |
| 942 | mBeginCommandBuffer(backbuffer->mTransitionCmdBuffers[1], &info); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 943 | mCmdPipelineBarrier(backbuffer->mTransitionCmdBuffers[1], srcStageMask, dstStageMask, 0, 0, |
| 944 | nullptr, 0, nullptr, 1, &imageMemoryBarrier); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 945 | mEndCommandBuffer(backbuffer->mTransitionCmdBuffers[1]); |
| 946 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 947 | surface->mImageInfos[backbuffer->mImageIndex].mImageLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 948 | |
| 949 | // insert the layout transfer into the queue and wait on the acquire |
| 950 | VkSubmitInfo submitInfo; |
| 951 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 952 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 953 | submitInfo.waitSemaphoreCount = 0; |
| 954 | submitInfo.pWaitDstStageMask = 0; |
| 955 | submitInfo.commandBufferCount = 1; |
| 956 | submitInfo.pCommandBuffers = &backbuffer->mTransitionCmdBuffers[1]; |
| 957 | submitInfo.signalSemaphoreCount = 1; |
| 958 | // When this command buffer finishes we will signal this semaphore so that we know it is now |
| 959 | // safe to present the image to the screen. |
| 960 | submitInfo.pSignalSemaphores = &backbuffer->mRenderSemaphore; |
| 961 | |
| 962 | // Attach second fence to submission here so we can track when the command buffer finishes. |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 963 | mQueueSubmit(mGraphicsQueue, 1, &submitInfo, backbuffer->mUsageFences[1]); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 964 | |
| 965 | // Submit present operation to present queue. We use a semaphore here to make sure all rendering |
| 966 | // to the image is complete and that the layout has been change to present on the graphics |
| 967 | // queue. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 968 | const VkPresentInfoKHR presentInfo = { |
| 969 | VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType |
| 970 | NULL, // pNext |
| 971 | 1, // waitSemaphoreCount |
| 972 | &backbuffer->mRenderSemaphore, // pWaitSemaphores |
| 973 | 1, // swapchainCount |
| 974 | &surface->mSwapchain, // pSwapchains |
| 975 | &backbuffer->mImageIndex, // pImageIndices |
| 976 | NULL // pResults |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 977 | }; |
| 978 | |
| 979 | mQueuePresentKHR(mPresentQueue, &presentInfo); |
| 980 | |
| 981 | surface->mBackbuffer.reset(); |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 982 | surface->mImageInfos[backbuffer->mImageIndex].mLastUsed = surface->mCurrentTime; |
| 983 | surface->mImageInfos[backbuffer->mImageIndex].mInvalid = false; |
| 984 | surface->mCurrentTime++; |
| 985 | } |
| 986 | |
| 987 | int VulkanManager::getAge(VulkanSurface* surface) { |
Greg Daniel | 74ea201 | 2017-11-10 11:32:58 -0500 | [diff] [blame] | 988 | SkASSERT(surface->mBackbuffers); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 989 | VulkanSurface::BackbufferInfo* backbuffer = |
| 990 | surface->mBackbuffers + surface->mCurrentBackbufferIndex; |
| 991 | if (mSwapBehavior == SwapBehavior::Discard || |
| 992 | surface->mImageInfos[backbuffer->mImageIndex].mInvalid) { |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 993 | return 0; |
| 994 | } |
| 995 | uint16_t lastUsed = surface->mImageInfos[backbuffer->mImageIndex].mLastUsed; |
| 996 | return surface->mCurrentTime - lastUsed; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 997 | } |
| 998 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 999 | bool VulkanManager::setupDummyCommandBuffer() { |
| 1000 | if (mDummyCB != VK_NULL_HANDLE) { |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
| 1004 | VkCommandBufferAllocateInfo commandBuffersInfo; |
| 1005 | memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo)); |
| 1006 | commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 1007 | commandBuffersInfo.pNext = nullptr; |
| 1008 | commandBuffersInfo.commandPool = mCommandPool; |
| 1009 | commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 1010 | commandBuffersInfo.commandBufferCount = 1; |
| 1011 | |
| 1012 | VkResult err = mAllocateCommandBuffers(mDevice, &commandBuffersInfo, &mDummyCB); |
| 1013 | if (err != VK_SUCCESS) { |
| 1014 | // It is probably unnecessary to set this back to VK_NULL_HANDLE, but we set it anyways to |
| 1015 | // make sure the driver didn't set a value and then return a failure. |
| 1016 | mDummyCB = VK_NULL_HANDLE; |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | VkCommandBufferBeginInfo beginInfo; |
| 1021 | memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 1022 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 1023 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; |
| 1024 | |
| 1025 | mBeginCommandBuffer(mDummyCB, &beginInfo); |
| 1026 | mEndCommandBuffer(mDummyCB); |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 1030 | status_t VulkanManager::fenceWait(sp<Fence>& fence) { |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 1031 | if (!hasVkContext()) { |
| 1032 | ALOGE("VulkanManager::fenceWait: VkDevice not initialized"); |
| 1033 | return INVALID_OPERATION; |
| 1034 | } |
| 1035 | |
| 1036 | if (SyncFeatures::getInstance().useWaitSync() && |
| 1037 | SyncFeatures::getInstance().useNativeFenceSync()) { |
| 1038 | // Block GPU on the fence. |
| 1039 | int fenceFd = fence->dup(); |
| 1040 | if (fenceFd == -1) { |
| 1041 | ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno); |
| 1042 | return -errno; |
| 1043 | } |
| 1044 | |
| 1045 | VkSemaphoreCreateInfo semaphoreInfo; |
| 1046 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 1047 | semaphoreInfo.pNext = nullptr; |
| 1048 | semaphoreInfo.flags = 0; |
| 1049 | VkSemaphore semaphore; |
| 1050 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 1051 | if (VK_SUCCESS != err) { |
| 1052 | ALOGE("Failed to create import semaphore, err: %d", err); |
| 1053 | return UNKNOWN_ERROR; |
| 1054 | } |
| 1055 | VkImportSemaphoreFdInfoKHR importInfo; |
| 1056 | importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR; |
| 1057 | importInfo.pNext = nullptr; |
| 1058 | importInfo.semaphore = semaphore; |
| 1059 | importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT; |
| 1060 | importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 1061 | importInfo.fd = fenceFd; |
| 1062 | |
| 1063 | err = mImportSemaphoreFdKHR(mDevice, &importInfo); |
| 1064 | if (VK_SUCCESS != err) { |
| 1065 | ALOGE("Failed to import semaphore, err: %d", err); |
| 1066 | return UNKNOWN_ERROR; |
| 1067 | } |
| 1068 | |
| 1069 | LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE); |
| 1070 | |
| 1071 | VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1072 | |
| 1073 | VkSubmitInfo submitInfo; |
| 1074 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 1075 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1076 | submitInfo.waitSemaphoreCount = 1; |
| 1077 | // Wait to make sure aquire semaphore set above has signaled. |
| 1078 | submitInfo.pWaitSemaphores = &semaphore; |
| 1079 | submitInfo.pWaitDstStageMask = &waitDstStageFlags; |
| 1080 | submitInfo.commandBufferCount = 1; |
| 1081 | submitInfo.pCommandBuffers = &mDummyCB; |
| 1082 | submitInfo.signalSemaphoreCount = 0; |
| 1083 | |
| 1084 | mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); |
| 1085 | |
| 1086 | // On Android when we import a semaphore, it is imported using temporary permanence. That |
| 1087 | // means as soon as we queue the semaphore for a wait it reverts to its previous permanent |
| 1088 | // state before importing. This means it will now be in an idle state with no pending |
| 1089 | // signal or wait operations, so it is safe to immediately delete it. |
| 1090 | mDestroySemaphore(mDevice, semaphore, nullptr); |
| 1091 | } else { |
| 1092 | // Block CPU on the fence. |
| 1093 | status_t err = fence->waitForever("VulkanManager::fenceWait"); |
| 1094 | if (err != NO_ERROR) { |
| 1095 | ALOGE("VulkanManager::fenceWait: error waiting for fence: %d", err); |
| 1096 | return err; |
| 1097 | } |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 1098 | } |
| 1099 | return OK; |
| 1100 | } |
| 1101 | |
| 1102 | status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) { |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 1103 | if (!hasVkContext()) { |
| 1104 | ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized"); |
| 1105 | return INVALID_OPERATION; |
| 1106 | } |
| 1107 | |
| 1108 | if (SyncFeatures::getInstance().useFenceSync()) { |
| 1109 | ALOGE("VulkanManager::createReleaseFence: Vk backend doesn't support non-native fences"); |
| 1110 | return INVALID_OPERATION; |
| 1111 | } |
| 1112 | |
| 1113 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
| 1114 | return OK; |
| 1115 | } |
| 1116 | |
| 1117 | VkExportSemaphoreCreateInfo exportInfo; |
| 1118 | exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO; |
| 1119 | exportInfo.pNext = nullptr; |
| 1120 | exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 1121 | |
| 1122 | VkSemaphoreCreateInfo semaphoreInfo; |
| 1123 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 1124 | semaphoreInfo.pNext = &exportInfo; |
| 1125 | semaphoreInfo.flags = 0; |
| 1126 | VkSemaphore semaphore; |
| 1127 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 1128 | if (VK_SUCCESS != err) { |
| 1129 | ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore"); |
| 1130 | return INVALID_OPERATION; |
| 1131 | } |
| 1132 | |
| 1133 | LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE); |
| 1134 | |
| 1135 | VkSubmitInfo submitInfo; |
| 1136 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 1137 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1138 | submitInfo.waitSemaphoreCount = 0; |
| 1139 | submitInfo.pWaitSemaphores = nullptr; |
| 1140 | submitInfo.pWaitDstStageMask = nullptr; |
| 1141 | submitInfo.commandBufferCount = 1; |
| 1142 | submitInfo.pCommandBuffers = &mDummyCB; |
| 1143 | submitInfo.signalSemaphoreCount = 1; |
| 1144 | submitInfo.pSignalSemaphores = &semaphore; |
| 1145 | |
| 1146 | mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); |
| 1147 | |
| 1148 | VkSemaphoreGetFdInfoKHR getFdInfo; |
| 1149 | getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR; |
| 1150 | getFdInfo.pNext = nullptr; |
| 1151 | getFdInfo.semaphore = semaphore; |
| 1152 | getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 1153 | |
| 1154 | int fenceFd = 0; |
| 1155 | |
| 1156 | err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd); |
| 1157 | if (VK_SUCCESS != err) { |
| 1158 | ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd"); |
| 1159 | return INVALID_OPERATION; |
| 1160 | } |
| 1161 | nativeFence = new Fence(fenceFd); |
| 1162 | |
| 1163 | // Exporting a semaphore with copy transference via vkGetSemahporeFdKHR, has the same effect of |
| 1164 | // destroying the semaphore and creating a new one with the same handle, and the payloads |
| 1165 | // ownership is move to the Fd we created. Thus the semahpore is in a state that we can delete |
| 1166 | // it and we don't need to wait on the command buffer we submitted to finish. |
| 1167 | mDestroySemaphore(mDevice, semaphore, nullptr); |
| 1168 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 1169 | return OK; |
| 1170 | } |
| 1171 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 1172 | } /* namespace renderthread */ |
| 1173 | } /* namespace uirenderer */ |
| 1174 | } /* namespace android */ |