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