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