Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "VulkanManager.h" |
| 18 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 19 | #include <android/sync.h> |
Stan Iliev | 305e13a | 2018-11-13 11:14:48 -0500 | [diff] [blame] | 20 | #include <gui/Surface.h> |
| 21 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 22 | #include "Properties.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 23 | #include "RenderThread.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 24 | #include "renderstate/RenderState.h" |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 25 | #include "utils/FatVector.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 26 | #include "utils/TraceUtils.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 27 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 28 | #include <GrBackendSemaphore.h> |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 29 | #include <GrBackendSurface.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 30 | #include <GrContext.h> |
| 31 | #include <GrTypes.h> |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 32 | #include <vk/GrVkExtensions.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 33 | #include <vk/GrVkTypes.h> |
| 34 | |
| 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | namespace renderthread { |
| 38 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 39 | static void free_features_extensions_structs(const VkPhysicalDeviceFeatures2& features) { |
| 40 | // All Vulkan structs that could be part of the features chain will start with the |
| 41 | // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader |
| 42 | // so we can get access to the pNext for the next struct. |
| 43 | struct CommonVulkanHeader { |
| 44 | VkStructureType sType; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 45 | void* pNext; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | void* pNext = features.pNext; |
| 49 | while (pNext) { |
| 50 | void* current = pNext; |
| 51 | pNext = static_cast<CommonVulkanHeader*>(current)->pNext; |
| 52 | free(current); |
| 53 | } |
| 54 | } |
| 55 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 56 | #define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(VK_NULL_HANDLE, "vk" #F) |
| 57 | #define GET_INST_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(mInstance, "vk" #F) |
| 58 | #define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(mDevice, "vk" #F) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 59 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 60 | void VulkanManager::destroy() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 61 | if (VK_NULL_HANDLE != mCommandPool) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 62 | mDestroyCommandPool(mDevice, mCommandPool, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 63 | mCommandPool = VK_NULL_HANDLE; |
| 64 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 65 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 66 | if (mDevice != VK_NULL_HANDLE) { |
| 67 | mDeviceWaitIdle(mDevice); |
| 68 | mDestroyDevice(mDevice, nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 70 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 71 | if (mInstance != VK_NULL_HANDLE) { |
| 72 | mDestroyInstance(mInstance, nullptr); |
| 73 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 74 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 75 | mGraphicsQueue = VK_NULL_HANDLE; |
| 76 | mPresentQueue = VK_NULL_HANDLE; |
| 77 | mDevice = VK_NULL_HANDLE; |
| 78 | mPhysicalDevice = VK_NULL_HANDLE; |
| 79 | mInstance = VK_NULL_HANDLE; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 80 | mInstanceExtensionsOwner.clear(); |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 81 | mInstanceExtensions.clear(); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 82 | mDeviceExtensionsOwner.clear(); |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 83 | mDeviceExtensions.clear(); |
| 84 | free_features_extensions_structs(mPhysicalDeviceFeatures2); |
| 85 | mPhysicalDeviceFeatures2 = {}; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 86 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 87 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 88 | void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 89 | VkResult err; |
| 90 | |
| 91 | constexpr VkApplicationInfo app_info = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 92 | VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType |
| 93 | nullptr, // pNext |
| 94 | "android framework", // pApplicationName |
| 95 | 0, // applicationVersion |
| 96 | "android framework", // pEngineName |
| 97 | 0, // engineVerison |
| 98 | mAPIVersion, // apiVersion |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 99 | }; |
| 100 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 101 | { |
| 102 | GET_PROC(EnumerateInstanceExtensionProperties); |
| 103 | |
| 104 | uint32_t extensionCount = 0; |
| 105 | err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 106 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 107 | mInstanceExtensionsOwner.resize(extensionCount); |
| 108 | err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, |
| 109 | mInstanceExtensionsOwner.data()); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 110 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 111 | bool hasKHRSurfaceExtension = false; |
| 112 | bool hasKHRAndroidSurfaceExtension = false; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 113 | for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) { |
| 114 | mInstanceExtensions.push_back(extension.extensionName); |
| 115 | if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 116 | hasKHRSurfaceExtension = true; |
| 117 | } |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 118 | if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 119 | hasKHRAndroidSurfaceExtension = true; |
| 120 | } |
| 121 | } |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 122 | LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | const VkInstanceCreateInfo instance_create = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 126 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType |
| 127 | nullptr, // pNext |
| 128 | 0, // flags |
| 129 | &app_info, // pApplicationInfo |
| 130 | 0, // enabledLayerNameCount |
| 131 | nullptr, // ppEnabledLayerNames |
| 132 | (uint32_t)mInstanceExtensions.size(), // enabledExtensionNameCount |
| 133 | mInstanceExtensions.data(), // ppEnabledExtensionNames |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | GET_PROC(CreateInstance); |
| 137 | err = mCreateInstance(&instance_create, nullptr, &mInstance); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 138 | LOG_ALWAYS_FATAL_IF(err < 0); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 139 | |
| 140 | GET_INST_PROC(DestroyInstance); |
| 141 | GET_INST_PROC(EnumeratePhysicalDevices); |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 142 | GET_INST_PROC(GetPhysicalDeviceProperties); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 143 | GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 144 | GET_INST_PROC(GetPhysicalDeviceFeatures2); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 145 | GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 146 | GET_INST_PROC(CreateDevice); |
| 147 | GET_INST_PROC(EnumerateDeviceExtensionProperties); |
| 148 | GET_INST_PROC(CreateAndroidSurfaceKHR); |
| 149 | GET_INST_PROC(DestroySurfaceKHR); |
| 150 | GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR); |
| 151 | GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 152 | GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR); |
| 153 | GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR); |
| 154 | |
| 155 | uint32_t gpuCount; |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 156 | LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr)); |
| 157 | LOG_ALWAYS_FATAL_IF(!gpuCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 158 | // Just returning the first physical device instead of getting the whole array. Since there |
| 159 | // should only be one device on android. |
| 160 | gpuCount = 1; |
| 161 | err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice); |
| 162 | // VK_INCOMPLETE is returned when the count we provide is less than the total device count. |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 163 | LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 164 | |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 165 | VkPhysicalDeviceProperties physDeviceProperties; |
| 166 | mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 167 | LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0)); |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 168 | mDriverVersion = physDeviceProperties.driverVersion; |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 169 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 170 | // query to get the initial queue props size |
| 171 | uint32_t queueCount; |
| 172 | mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 173 | LOG_ALWAYS_FATAL_IF(!queueCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 174 | |
| 175 | // now get the actual queue props |
| 176 | std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]); |
| 177 | mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get()); |
| 178 | |
| 179 | // iterate to find the graphics queue |
| 180 | mGraphicsQueueIndex = queueCount; |
| 181 | for (uint32_t i = 0; i < queueCount; i++) { |
| 182 | if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
| 183 | mGraphicsQueueIndex = i; |
| 184 | break; |
| 185 | } |
| 186 | } |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 187 | LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 188 | |
| 189 | // All physical devices and queue families on Android must be capable of |
| 190 | // presentation with any native window. So just use the first one. |
| 191 | mPresentQueueIndex = 0; |
| 192 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 193 | { |
| 194 | uint32_t extensionCount = 0; |
| 195 | err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 196 | nullptr); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 197 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 198 | mDeviceExtensionsOwner.resize(extensionCount); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 199 | err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 200 | mDeviceExtensionsOwner.data()); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 201 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 202 | bool hasKHRSwapchainExtension = false; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 203 | for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) { |
| 204 | mDeviceExtensions.push_back(extension.extensionName); |
| 205 | if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) { |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 206 | hasKHRSwapchainExtension = true; |
| 207 | } |
| 208 | } |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 209 | LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 210 | } |
| 211 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 212 | auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) { |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 213 | if (device != VK_NULL_HANDLE) { |
| 214 | return vkGetDeviceProcAddr(device, proc_name); |
| 215 | } |
| 216 | return vkGetInstanceProcAddr(instance, proc_name); |
| 217 | }; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 218 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 219 | grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(), |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 220 | mInstanceExtensions.data(), mDeviceExtensions.size(), |
| 221 | mDeviceExtensions.data()); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 222 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 223 | LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1)); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 224 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 225 | memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2)); |
| 226 | features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 227 | features.pNext = nullptr; |
| 228 | |
| 229 | // Setup all extension feature structs we may want to use. |
| 230 | void** tailPNext = &features.pNext; |
| 231 | |
| 232 | if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) { |
| 233 | VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 234 | blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc( |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 235 | sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT)); |
| 236 | LOG_ALWAYS_FATAL_IF(!blend); |
| 237 | blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT; |
| 238 | blend->pNext = nullptr; |
| 239 | *tailPNext = blend; |
| 240 | tailPNext = &blend->pNext; |
| 241 | } |
| 242 | |
Greg Daniel | 0503617 | 2018-11-28 17:08:04 -0500 | [diff] [blame] | 243 | VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 244 | ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc( |
Greg Daniel | 0503617 | 2018-11-28 17:08:04 -0500 | [diff] [blame] | 245 | sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures)); |
| 246 | LOG_ALWAYS_FATAL_IF(!ycbcrFeature); |
| 247 | ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES; |
| 248 | ycbcrFeature->pNext = nullptr; |
| 249 | *tailPNext = ycbcrFeature; |
| 250 | tailPNext = &ycbcrFeature->pNext; |
| 251 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 252 | // query to get the physical device features |
| 253 | mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 254 | // this looks like it would slow things down, |
| 255 | // and we can't depend on it on all platforms |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 256 | features.features.robustBufferAccess = VK_FALSE; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 257 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 258 | float queuePriorities[1] = {0.0}; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 259 | |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 260 | void* queueNextPtr = nullptr; |
| 261 | |
| 262 | VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo; |
| 263 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 264 | if (Properties::contextPriority != 0 && |
| 265 | grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) { |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 266 | memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT)); |
| 267 | queuePriorityCreateInfo.sType = |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 268 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT; |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 269 | queuePriorityCreateInfo.pNext = nullptr; |
| 270 | switch (Properties::contextPriority) { |
| 271 | case EGL_CONTEXT_PRIORITY_LOW_IMG: |
| 272 | queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT; |
| 273 | break; |
| 274 | case EGL_CONTEXT_PRIORITY_MEDIUM_IMG: |
| 275 | queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT; |
| 276 | break; |
| 277 | case EGL_CONTEXT_PRIORITY_HIGH_IMG: |
| 278 | queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT; |
| 279 | break; |
| 280 | default: |
| 281 | LOG_ALWAYS_FATAL("Unsupported context priority"); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 282 | } |
| 283 | queueNextPtr = &queuePriorityCreateInfo; |
Stan Iliev | 7e73336 | 2019-02-28 13:16:36 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 286 | const VkDeviceQueueCreateInfo queueInfo[2] = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 287 | { |
| 288 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 289 | queueNextPtr, // pNext |
| 290 | 0, // VkDeviceQueueCreateFlags |
| 291 | mGraphicsQueueIndex, // queueFamilyIndex |
| 292 | 1, // queueCount |
| 293 | queuePriorities, // pQueuePriorities |
| 294 | }, |
| 295 | { |
| 296 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 297 | queueNextPtr, // pNext |
| 298 | 0, // VkDeviceQueueCreateFlags |
| 299 | mPresentQueueIndex, // queueFamilyIndex |
| 300 | 1, // queueCount |
| 301 | queuePriorities, // pQueuePriorities |
| 302 | }}; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 303 | uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1; |
| 304 | |
| 305 | const VkDeviceCreateInfo deviceInfo = { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 306 | VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType |
| 307 | &features, // pNext |
| 308 | 0, // VkDeviceCreateFlags |
| 309 | queueInfoCount, // queueCreateInfoCount |
| 310 | queueInfo, // pQueueCreateInfos |
| 311 | 0, // layerCount |
| 312 | nullptr, // ppEnabledLayerNames |
| 313 | (uint32_t)mDeviceExtensions.size(), // extensionCount |
| 314 | mDeviceExtensions.data(), // ppEnabledExtensionNames |
| 315 | nullptr, // ppEnabledFeatures |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 316 | }; |
| 317 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 318 | LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice)); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 319 | |
| 320 | GET_DEV_PROC(GetDeviceQueue); |
| 321 | GET_DEV_PROC(DeviceWaitIdle); |
| 322 | GET_DEV_PROC(DestroyDevice); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 323 | GET_DEV_PROC(CreateCommandPool); |
| 324 | GET_DEV_PROC(DestroyCommandPool); |
| 325 | GET_DEV_PROC(AllocateCommandBuffers); |
| 326 | GET_DEV_PROC(FreeCommandBuffers); |
| 327 | GET_DEV_PROC(ResetCommandBuffer); |
| 328 | GET_DEV_PROC(BeginCommandBuffer); |
| 329 | GET_DEV_PROC(EndCommandBuffer); |
| 330 | GET_DEV_PROC(CmdPipelineBarrier); |
| 331 | GET_DEV_PROC(GetDeviceQueue); |
| 332 | GET_DEV_PROC(QueueSubmit); |
| 333 | GET_DEV_PROC(QueueWaitIdle); |
| 334 | GET_DEV_PROC(DeviceWaitIdle); |
| 335 | GET_DEV_PROC(CreateSemaphore); |
| 336 | GET_DEV_PROC(DestroySemaphore); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 337 | GET_DEV_PROC(ImportSemaphoreFdKHR); |
| 338 | GET_DEV_PROC(GetSemaphoreFdKHR); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 339 | GET_DEV_PROC(CreateFence); |
| 340 | GET_DEV_PROC(DestroyFence); |
| 341 | GET_DEV_PROC(WaitForFences); |
| 342 | GET_DEV_PROC(ResetFences); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void VulkanManager::initialize() { |
| 346 | if (mDevice != VK_NULL_HANDLE) { |
| 347 | return; |
| 348 | } |
| 349 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 350 | GET_PROC(EnumerateInstanceVersion); |
Greg Daniel | eaf310e | 2019-01-28 16:10:32 -0500 | [diff] [blame] | 351 | uint32_t instanceVersion; |
| 352 | LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion)); |
| 353 | LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0)); |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 354 | |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 355 | this->setupDevice(mExtensions, mPhysicalDeviceFeatures2); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 356 | |
| 357 | mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue); |
| 358 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 359 | // create the command pool for the command buffers |
| 360 | if (VK_NULL_HANDLE == mCommandPool) { |
| 361 | VkCommandPoolCreateInfo commandPoolInfo; |
| 362 | memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); |
| 363 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 364 | // this needs to be on the render queue |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 365 | commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 366 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 367 | SkDEBUGCODE(VkResult res =) |
| 368 | mCreateCommandPool(mDevice, &commandPoolInfo, nullptr, &mCommandPool); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 369 | SkASSERT(VK_SUCCESS == res); |
| 370 | } |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 371 | LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE); |
| 372 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 373 | mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 374 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 375 | if (Properties::enablePartialUpdates && Properties::useBufferAge) { |
| 376 | mSwapBehavior = SwapBehavior::BufferAge; |
| 377 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 380 | sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 381 | auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 382 | if (device != VK_NULL_HANDLE) { |
| 383 | return vkGetDeviceProcAddr(device, proc_name); |
| 384 | } |
| 385 | return vkGetInstanceProcAddr(instance, proc_name); |
| 386 | }; |
| 387 | |
| 388 | GrVkBackendContext backendContext; |
| 389 | backendContext.fInstance = mInstance; |
| 390 | backendContext.fPhysicalDevice = mPhysicalDevice; |
| 391 | backendContext.fDevice = mDevice; |
| 392 | backendContext.fQueue = mGraphicsQueue; |
| 393 | backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex; |
| 394 | backendContext.fMaxAPIVersion = mAPIVersion; |
| 395 | backendContext.fVkExtensions = &mExtensions; |
| 396 | backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2; |
| 397 | backendContext.fGetProc = std::move(getProc); |
| 398 | |
| 399 | return GrContext::MakeVulkan(backendContext, options); |
| 400 | } |
| 401 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 402 | VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const { |
| 403 | return VkFunctorInitParams{ |
| 404 | .instance = mInstance, |
| 405 | .physical_device = mPhysicalDevice, |
| 406 | .device = mDevice, |
| 407 | .queue = mGraphicsQueue, |
| 408 | .graphics_queue_index = mGraphicsQueueIndex, |
Greg Daniel | eaf310e | 2019-01-28 16:10:32 -0500 | [diff] [blame] | 409 | .api_version = mAPIVersion, |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 410 | .enabled_instance_extension_names = mInstanceExtensions.data(), |
| 411 | .enabled_instance_extension_names_length = |
| 412 | static_cast<uint32_t>(mInstanceExtensions.size()), |
| 413 | .enabled_device_extension_names = mDeviceExtensions.data(), |
| 414 | .enabled_device_extension_names_length = |
| 415 | static_cast<uint32_t>(mDeviceExtensions.size()), |
| 416 | .device_features_2 = &mPhysicalDeviceFeatures2, |
| 417 | }; |
| 418 | } |
| 419 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 420 | Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) { |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 421 | VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer(); |
| 422 | |
| 423 | if (bufferInfo == nullptr) { |
| 424 | ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!"); |
| 425 | return Frame(-1, -1, 0); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 428 | LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 429 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 430 | if (bufferInfo->dequeue_fence != -1) { |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 431 | struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence); |
| 432 | bool isSignalPending = false; |
| 433 | if (finfo != NULL) { |
| 434 | isSignalPending = finfo->status != 1; |
| 435 | sync_file_info_free(finfo); |
| 436 | } |
| 437 | if (isSignalPending) { |
| 438 | int fence_clone = dup(bufferInfo->dequeue_fence); |
| 439 | if (fence_clone == -1) { |
| 440 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno), |
| 441 | errno); |
| 442 | sync_wait(bufferInfo->dequeue_fence, -1 /* forever */); |
| 443 | } else { |
| 444 | VkSemaphoreCreateInfo semaphoreInfo; |
| 445 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 446 | semaphoreInfo.pNext = nullptr; |
| 447 | semaphoreInfo.flags = 0; |
| 448 | VkSemaphore semaphore; |
| 449 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 450 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d", |
| 451 | err); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 452 | |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 453 | VkImportSemaphoreFdInfoKHR importInfo; |
| 454 | importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR; |
| 455 | importInfo.pNext = nullptr; |
| 456 | importInfo.semaphore = semaphore; |
| 457 | importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT; |
| 458 | importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 459 | importInfo.fd = fence_clone; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 460 | |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 461 | err = mImportSemaphoreFdKHR(mDevice, &importInfo); |
| 462 | LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 463 | |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 464 | GrBackendSemaphore backendSemaphore; |
| 465 | backendSemaphore.initVulkan(semaphore); |
| 466 | bufferInfo->skSurface->wait(1, &backendSemaphore); |
| 467 | // The following flush blocks the GPU immediately instead of waiting for other |
| 468 | // drawing ops. It seems dequeue_fence is not respected otherwise. |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 469 | // TODO: remove the flush after finding why backendSemaphore is not working. |
Stan Iliev | 197843d | 2019-03-21 11:34:15 -0400 | [diff] [blame] | 470 | bufferInfo->skSurface->flush(); |
| 471 | } |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 472 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 475 | int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge(); |
| 476 | return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 477 | } |
| 478 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 479 | struct DestroySemaphoreInfo { |
| 480 | PFN_vkDestroySemaphore mDestroyFunction; |
| 481 | VkDevice mDevice; |
| 482 | VkSemaphore mSemaphore; |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 483 | // We need to make sure we don't delete the VkSemaphore until it is done being used by both Skia |
| 484 | // (including by the GPU) and inside the VulkanManager. So we always start with two refs, one |
| 485 | // owned by Skia and one owned by the VulkanManager. The refs are decremented each time |
| 486 | // destroy_semaphore is called with this object. Skia will call destroy_semaphore once it is |
| 487 | // done with the semaphore and the GPU has finished work on the semaphore. The VulkanManager |
| 488 | // calls destroy_semaphore after sending the semaphore to Skia and exporting it if need be. |
| 489 | int mRefs = 2; |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 490 | |
| 491 | DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device, |
| 492 | VkSemaphore semaphore) |
| 493 | : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {} |
| 494 | }; |
| 495 | |
| 496 | static void destroy_semaphore(void* context) { |
| 497 | DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context); |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 498 | --info->mRefs; |
| 499 | if (!info->mRefs) { |
| 500 | info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr); |
| 501 | delete info; |
| 502 | } |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 505 | void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) { |
| 506 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
| 507 | ATRACE_NAME("Finishing GPU work"); |
| 508 | mDeviceWaitIdle(mDevice); |
Stan Iliev | 305e13a | 2018-11-13 11:14:48 -0500 | [diff] [blame] | 509 | } |
| 510 | |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 511 | VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo(); |
| 512 | if (!bufferInfo) { |
| 513 | // If VulkanSurface::dequeueNativeBuffer failed earlier, then swapBuffers is a no-op. |
| 514 | return; |
| 515 | } |
| 516 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 517 | VkExportSemaphoreCreateInfo exportInfo; |
| 518 | exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO; |
| 519 | exportInfo.pNext = nullptr; |
| 520 | exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 521 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 522 | VkSemaphoreCreateInfo semaphoreInfo; |
| 523 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 524 | semaphoreInfo.pNext = &exportInfo; |
| 525 | semaphoreInfo.flags = 0; |
| 526 | VkSemaphore semaphore; |
| 527 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 528 | ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore"); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 529 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 530 | GrBackendSemaphore backendSemaphore; |
| 531 | backendSemaphore.initVulkan(semaphore); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 532 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 533 | int fenceFd = -1; |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 534 | DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, |
| 535 | semaphore); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 536 | GrSemaphoresSubmitted submitted = |
| 537 | bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent, |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 538 | kNone_GrFlushFlags, 1, &backendSemaphore, |
| 539 | destroy_semaphore, destroyInfo); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 540 | if (submitted == GrSemaphoresSubmitted::kYes) { |
| 541 | VkSemaphoreGetFdInfoKHR getFdInfo; |
| 542 | getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR; |
| 543 | getFdInfo.pNext = nullptr; |
| 544 | getFdInfo.semaphore = semaphore; |
| 545 | getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 546 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 547 | err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd); |
| 548 | ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd"); |
| 549 | } else { |
| 550 | ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed"); |
| 551 | mQueueWaitIdle(mGraphicsQueue); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 552 | } |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 553 | destroy_semaphore(destroyInfo); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 554 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 555 | surface->presentCurrentBuffer(dirtyRect, fenceFd); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void VulkanManager::destroySurface(VulkanSurface* surface) { |
| 559 | // Make sure all submit commands have finished before starting to destroy objects. |
| 560 | if (VK_NULL_HANDLE != mPresentQueue) { |
| 561 | mQueueWaitIdle(mPresentQueue); |
| 562 | } |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 563 | mDeviceWaitIdle(mDevice); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 564 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 565 | delete surface; |
| 566 | } |
| 567 | |
Stan Iliev | 987a80c | 2018-12-04 10:07:21 -0500 | [diff] [blame] | 568 | VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode, |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 569 | sk_sp<SkColorSpace> surfaceColorSpace, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 570 | SkColorType surfaceColorType, GrContext* grContext, |
| 571 | uint32_t extraBuffers) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 572 | LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized"); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 573 | if (!window) { |
| 574 | return nullptr; |
| 575 | } |
| 576 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 577 | return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 578 | *this, extraBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 579 | } |
| 580 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 581 | status_t VulkanManager::fenceWait(sp<Fence>& fence, GrContext* grContext) { |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 582 | if (!hasVkContext()) { |
| 583 | ALOGE("VulkanManager::fenceWait: VkDevice not initialized"); |
| 584 | return INVALID_OPERATION; |
| 585 | } |
| 586 | |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 587 | // Block GPU on the fence. |
| 588 | int fenceFd = fence->dup(); |
| 589 | if (fenceFd == -1) { |
| 590 | ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno); |
| 591 | return -errno; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 592 | } |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 593 | |
| 594 | VkSemaphoreCreateInfo semaphoreInfo; |
| 595 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 596 | semaphoreInfo.pNext = nullptr; |
| 597 | semaphoreInfo.flags = 0; |
| 598 | VkSemaphore semaphore; |
| 599 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 600 | if (VK_SUCCESS != err) { |
| 601 | ALOGE("Failed to create import semaphore, err: %d", err); |
| 602 | return UNKNOWN_ERROR; |
| 603 | } |
| 604 | VkImportSemaphoreFdInfoKHR importInfo; |
| 605 | importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR; |
| 606 | importInfo.pNext = nullptr; |
| 607 | importInfo.semaphore = semaphore; |
| 608 | importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT; |
| 609 | importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 610 | importInfo.fd = fenceFd; |
| 611 | |
| 612 | err = mImportSemaphoreFdKHR(mDevice, &importInfo); |
| 613 | if (VK_SUCCESS != err) { |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 614 | mDestroySemaphore(mDevice, semaphore, nullptr); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 615 | ALOGE("Failed to import semaphore, err: %d", err); |
| 616 | return UNKNOWN_ERROR; |
| 617 | } |
| 618 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 619 | GrBackendSemaphore beSemaphore; |
| 620 | beSemaphore.initVulkan(semaphore); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 621 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 622 | // Skia takes ownership of the semaphore and will delete it once the wait has finished. |
| 623 | grContext->wait(1, &beSemaphore); |
| 624 | grContext->flush(); |
Stan Iliev | 7a08127 | 2018-10-26 17:54:18 -0400 | [diff] [blame] | 625 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 626 | return OK; |
| 627 | } |
| 628 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 629 | status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext) { |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 630 | if (!hasVkContext()) { |
| 631 | ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized"); |
| 632 | return INVALID_OPERATION; |
| 633 | } |
| 634 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 635 | VkExportSemaphoreCreateInfo exportInfo; |
| 636 | exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO; |
| 637 | exportInfo.pNext = nullptr; |
| 638 | exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 639 | |
| 640 | VkSemaphoreCreateInfo semaphoreInfo; |
| 641 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 642 | semaphoreInfo.pNext = &exportInfo; |
| 643 | semaphoreInfo.flags = 0; |
| 644 | VkSemaphore semaphore; |
| 645 | VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore); |
| 646 | if (VK_SUCCESS != err) { |
| 647 | ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore"); |
| 648 | return INVALID_OPERATION; |
| 649 | } |
| 650 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 651 | GrBackendSemaphore backendSemaphore; |
| 652 | backendSemaphore.initVulkan(semaphore); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 653 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 654 | DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice, |
| 655 | semaphore); |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 656 | // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback |
| 657 | // which will remove its ref to the semaphore. The VulkanManager must still release its ref, |
| 658 | // when it is done with the semaphore. |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 659 | GrSemaphoresSubmitted submitted = |
| 660 | grContext->flush(kNone_GrFlushFlags, 1, &backendSemaphore, |
| 661 | destroy_semaphore, destroyInfo); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 662 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 663 | if (submitted == GrSemaphoresSubmitted::kNo) { |
| 664 | ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore"); |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 665 | destroy_semaphore(destroyInfo); |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 666 | return INVALID_OPERATION; |
| 667 | } |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 668 | |
| 669 | VkSemaphoreGetFdInfoKHR getFdInfo; |
| 670 | getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR; |
| 671 | getFdInfo.pNext = nullptr; |
| 672 | getFdInfo.semaphore = semaphore; |
| 673 | getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT; |
| 674 | |
| 675 | int fenceFd = 0; |
| 676 | |
| 677 | err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd); |
Greg Daniel | fd42939 | 2019-05-09 15:44:56 -0400 | [diff] [blame] | 678 | destroy_semaphore(destroyInfo); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 679 | if (VK_SUCCESS != err) { |
| 680 | ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd"); |
| 681 | return INVALID_OPERATION; |
| 682 | } |
| 683 | nativeFence = new Fence(fenceFd); |
| 684 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 685 | return OK; |
| 686 | } |
| 687 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 688 | } /* namespace renderthread */ |
| 689 | } /* namespace uirenderer */ |
| 690 | } /* namespace android */ |