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