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