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