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