blob: 62fd489408703d4c1a95f65ca02d15e79d9e7a13 [file] [log] [blame]
Derek Sollenberger0e3cba32016-11-09 11:58:36 -05001/*
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 Sollenbergera19b71a2019-02-15 16:36:30 -050019#include <android/sync.h>
Stan Iliev305e13a2018-11-13 11:14:48 -050020#include <gui/Surface.h>
21
Greg Danielcd558522016-11-17 13:31:40 -050022#include "Properties.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050023#include "RenderThread.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050024#include "renderstate/RenderState.h"
Ben Wagnereec27d52017-01-11 15:32:07 -050025#include "utils/FatVector.h"
John Reck322b8ab2019-03-14 13:15:28 -070026#include "utils/TraceUtils.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050027
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050028#include <GrBackendSemaphore.h>
Greg Danielac2d2322017-07-12 11:30:15 -040029#include <GrBackendSurface.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030#include <GrContext.h>
31#include <GrTypes.h>
Greg Daniela227dbb2018-08-20 09:19:48 -040032#include <vk/GrVkExtensions.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050033#include <vk/GrVkTypes.h>
34
35namespace android {
36namespace uirenderer {
37namespace renderthread {
38
Bo Liu7b8c1eb2019-01-08 20:17:55 -080039static 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 Reck0fa0cbc2019-04-05 16:57:46 -070045 void* pNext;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080046 };
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 Daniel2ff202712018-06-14 11:50:10 -040056#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 Sollenberger0e3cba32016-11-09 11:58:36 -050059
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050060void VulkanManager::destroy() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050061 if (VK_NULL_HANDLE != mCommandPool) {
Greg Daniel2ff202712018-06-14 11:50:10 -040062 mDestroyCommandPool(mDevice, mCommandPool, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050063 mCommandPool = VK_NULL_HANDLE;
64 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065
Greg Daniel2ff202712018-06-14 11:50:10 -040066 if (mDevice != VK_NULL_HANDLE) {
67 mDeviceWaitIdle(mDevice);
68 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070069 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050070
Greg Daniel2ff202712018-06-14 11:50:10 -040071 if (mInstance != VK_NULL_HANDLE) {
72 mDestroyInstance(mInstance, nullptr);
73 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074
Greg Daniel2ff202712018-06-14 11:50:10 -040075 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 Kiryanov74ace839e2019-03-07 18:22:19 -080080 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080081 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080082 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080083 mDeviceExtensions.clear();
84 free_features_extensions_structs(mPhysicalDeviceFeatures2);
85 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040086}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050087
Stan Iliev90276c82019-02-03 18:01:02 -050088void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -040089 VkResult err;
90
91 constexpr VkApplicationInfo app_info = {
John Reck0fa0cbc2019-04-05 16:57:46 -070092 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 Daniel2ff202712018-06-14 11:50:10 -040099 };
100
Greg Daniel2ff202712018-06-14 11:50:10 -0400101 {
102 GET_PROC(EnumerateInstanceExtensionProperties);
103
104 uint32_t extensionCount = 0;
105 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500106 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800107 mInstanceExtensionsOwner.resize(extensionCount);
108 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
109 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500110 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400111 bool hasKHRSurfaceExtension = false;
112 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800113 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
114 mInstanceExtensions.push_back(extension.extensionName);
115 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400116 hasKHRSurfaceExtension = true;
117 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800118 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400119 hasKHRAndroidSurfaceExtension = true;
120 }
121 }
Stan Iliev90276c82019-02-03 18:01:02 -0500122 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400123 }
124
125 const VkInstanceCreateInfo instance_create = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700126 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 Daniel2ff202712018-06-14 11:50:10 -0400134 };
135
136 GET_PROC(CreateInstance);
137 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500138 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400139
140 GET_INST_PROC(DestroyInstance);
141 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniel96259622018-10-01 14:42:56 -0400142 GET_INST_PROC(GetPhysicalDeviceProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400143 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniela227dbb2018-08-20 09:19:48 -0400144 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500145 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400146 GET_INST_PROC(CreateDevice);
147 GET_INST_PROC(EnumerateDeviceExtensionProperties);
148 GET_INST_PROC(CreateAndroidSurfaceKHR);
149 GET_INST_PROC(DestroySurfaceKHR);
150 GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR);
151 GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
152 GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
153 GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
154
155 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500156 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
157 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400158 // Just returning the first physical device instead of getting the whole array. Since there
159 // should only be one device on android.
160 gpuCount = 1;
161 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
162 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500163 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400164
Greg Daniel96259622018-10-01 14:42:56 -0400165 VkPhysicalDeviceProperties physDeviceProperties;
166 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500167 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Stan Ilievbf99c442019-03-29 11:09:11 -0400168 mDriverVersion = physDeviceProperties.driverVersion;
Greg Daniel96259622018-10-01 14:42:56 -0400169
Greg Daniel2173f182019-04-01 09:29:44 -0400170 mIsQualcomm = physDeviceProperties.vendorID == 20803;
171
Greg Daniel2ff202712018-06-14 11:50:10 -0400172 // query to get the initial queue props size
173 uint32_t queueCount;
174 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500175 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400176
177 // now get the actual queue props
178 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
179 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
180
181 // iterate to find the graphics queue
182 mGraphicsQueueIndex = queueCount;
183 for (uint32_t i = 0; i < queueCount; i++) {
184 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
185 mGraphicsQueueIndex = i;
186 break;
187 }
188 }
Stan Iliev90276c82019-02-03 18:01:02 -0500189 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400190
191 // All physical devices and queue families on Android must be capable of
192 // presentation with any native window. So just use the first one.
193 mPresentQueueIndex = 0;
194
Greg Daniel2ff202712018-06-14 11:50:10 -0400195 {
196 uint32_t extensionCount = 0;
197 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700198 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500199 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800200 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400201 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
John Reck0fa0cbc2019-04-05 16:57:46 -0700202 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500203 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400204 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800205 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
206 mDeviceExtensions.push_back(extension.extensionName);
207 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 hasKHRSwapchainExtension = true;
209 }
210 }
Stan Iliev90276c82019-02-03 18:01:02 -0500211 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400212 }
213
John Reck0fa0cbc2019-04-05 16:57:46 -0700214 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
Greg Daniela227dbb2018-08-20 09:19:48 -0400215 if (device != VK_NULL_HANDLE) {
216 return vkGetDeviceProcAddr(device, proc_name);
217 }
218 return vkGetInstanceProcAddr(instance, proc_name);
219 };
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800220
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800221 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
John Reck0fa0cbc2019-04-05 16:57:46 -0700222 mInstanceExtensions.data(), mDeviceExtensions.size(),
223 mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400224
Stan Iliev90276c82019-02-03 18:01:02 -0500225 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400226
Greg Daniela227dbb2018-08-20 09:19:48 -0400227 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
228 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
229 features.pNext = nullptr;
230
231 // Setup all extension feature structs we may want to use.
232 void** tailPNext = &features.pNext;
233
234 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
235 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
John Reck0fa0cbc2019-04-05 16:57:46 -0700236 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*)malloc(
Greg Daniela227dbb2018-08-20 09:19:48 -0400237 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
238 LOG_ALWAYS_FATAL_IF(!blend);
239 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
240 blend->pNext = nullptr;
241 *tailPNext = blend;
242 tailPNext = &blend->pNext;
243 }
244
Greg Daniel05036172018-11-28 17:08:04 -0500245 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
John Reck0fa0cbc2019-04-05 16:57:46 -0700246 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)malloc(
Greg Daniel05036172018-11-28 17:08:04 -0500247 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
248 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
249 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
250 ycbcrFeature->pNext = nullptr;
251 *tailPNext = ycbcrFeature;
252 tailPNext = &ycbcrFeature->pNext;
253
Greg Daniela227dbb2018-08-20 09:19:48 -0400254 // query to get the physical device features
255 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400256 // this looks like it would slow things down,
257 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400258 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400259
John Reck0fa0cbc2019-04-05 16:57:46 -0700260 float queuePriorities[1] = {0.0};
Greg Daniel2ff202712018-06-14 11:50:10 -0400261
Stan Iliev7e733362019-02-28 13:16:36 -0500262 void* queueNextPtr = nullptr;
263
264 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
265
John Reck0fa0cbc2019-04-05 16:57:46 -0700266 if (Properties::contextPriority != 0 &&
267 grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
Stan Iliev7e733362019-02-28 13:16:36 -0500268 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
269 queuePriorityCreateInfo.sType =
John Reck0fa0cbc2019-04-05 16:57:46 -0700270 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
Stan Iliev7e733362019-02-28 13:16:36 -0500271 queuePriorityCreateInfo.pNext = nullptr;
272 switch (Properties::contextPriority) {
273 case EGL_CONTEXT_PRIORITY_LOW_IMG:
274 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
275 break;
276 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
277 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
278 break;
279 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
280 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
281 break;
282 default:
283 LOG_ALWAYS_FATAL("Unsupported context priority");
John Reck0fa0cbc2019-04-05 16:57:46 -0700284 }
285 queueNextPtr = &queuePriorityCreateInfo;
Stan Iliev7e733362019-02-28 13:16:36 -0500286 }
287
Greg Daniel2ff202712018-06-14 11:50:10 -0400288 const VkDeviceQueueCreateInfo queueInfo[2] = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700289 {
290 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
291 queueNextPtr, // pNext
292 0, // VkDeviceQueueCreateFlags
293 mGraphicsQueueIndex, // queueFamilyIndex
294 1, // queueCount
295 queuePriorities, // pQueuePriorities
296 },
297 {
298 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
299 queueNextPtr, // pNext
300 0, // VkDeviceQueueCreateFlags
301 mPresentQueueIndex, // queueFamilyIndex
302 1, // queueCount
303 queuePriorities, // pQueuePriorities
304 }};
Greg Daniel2ff202712018-06-14 11:50:10 -0400305 uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1;
306
307 const VkDeviceCreateInfo deviceInfo = {
John Reck0fa0cbc2019-04-05 16:57:46 -0700308 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
309 &features, // pNext
310 0, // VkDeviceCreateFlags
311 queueInfoCount, // queueCreateInfoCount
312 queueInfo, // pQueueCreateInfos
313 0, // layerCount
314 nullptr, // ppEnabledLayerNames
315 (uint32_t)mDeviceExtensions.size(), // extensionCount
316 mDeviceExtensions.data(), // ppEnabledExtensionNames
317 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400318 };
319
Stan Iliev90276c82019-02-03 18:01:02 -0500320 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400321
322 GET_DEV_PROC(GetDeviceQueue);
323 GET_DEV_PROC(DeviceWaitIdle);
324 GET_DEV_PROC(DestroyDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500325 GET_DEV_PROC(CreateCommandPool);
326 GET_DEV_PROC(DestroyCommandPool);
327 GET_DEV_PROC(AllocateCommandBuffers);
328 GET_DEV_PROC(FreeCommandBuffers);
329 GET_DEV_PROC(ResetCommandBuffer);
330 GET_DEV_PROC(BeginCommandBuffer);
331 GET_DEV_PROC(EndCommandBuffer);
332 GET_DEV_PROC(CmdPipelineBarrier);
333 GET_DEV_PROC(GetDeviceQueue);
334 GET_DEV_PROC(QueueSubmit);
335 GET_DEV_PROC(QueueWaitIdle);
336 GET_DEV_PROC(DeviceWaitIdle);
337 GET_DEV_PROC(CreateSemaphore);
338 GET_DEV_PROC(DestroySemaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400339 GET_DEV_PROC(ImportSemaphoreFdKHR);
340 GET_DEV_PROC(GetSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500341 GET_DEV_PROC(CreateFence);
342 GET_DEV_PROC(DestroyFence);
343 GET_DEV_PROC(WaitForFences);
344 GET_DEV_PROC(ResetFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400345}
346
347void VulkanManager::initialize() {
348 if (mDevice != VK_NULL_HANDLE) {
349 return;
350 }
351
Greg Daniela227dbb2018-08-20 09:19:48 -0400352 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500353 uint32_t instanceVersion;
354 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
355 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400356
Stan Iliev981afe72019-02-13 14:24:33 -0500357 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400358
359 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
360
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500361 // create the command pool for the command buffers
362 if (VK_NULL_HANDLE == mCommandPool) {
363 VkCommandPoolCreateInfo commandPoolInfo;
364 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
365 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
366 // this needs to be on the render queue
Greg Daniel2ff202712018-06-14 11:50:10 -0400367 commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500368 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
John Reck0fa0cbc2019-04-05 16:57:46 -0700369 SkDEBUGCODE(VkResult res =)
370 mCreateCommandPool(mDevice, &commandPoolInfo, nullptr, &mCommandPool);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500371 SkASSERT(VK_SUCCESS == res);
372 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400373 LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE);
374
Greg Daniel2ff202712018-06-14 11:50:10 -0400375 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500376
Greg Danielcd558522016-11-17 13:31:40 -0500377 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
378 mSwapBehavior = SwapBehavior::BufferAge;
379 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500380}
381
Stan Iliev898123b2019-02-14 14:57:44 -0500382sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700383 auto getProc = [](const char* proc_name, VkInstance instance, VkDevice device) {
Stan Iliev981afe72019-02-13 14:24:33 -0500384 if (device != VK_NULL_HANDLE) {
385 return vkGetDeviceProcAddr(device, proc_name);
386 }
387 return vkGetInstanceProcAddr(instance, proc_name);
388 };
389
390 GrVkBackendContext backendContext;
391 backendContext.fInstance = mInstance;
392 backendContext.fPhysicalDevice = mPhysicalDevice;
393 backendContext.fDevice = mDevice;
394 backendContext.fQueue = mGraphicsQueue;
395 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
396 backendContext.fMaxAPIVersion = mAPIVersion;
397 backendContext.fVkExtensions = &mExtensions;
398 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
399 backendContext.fGetProc = std::move(getProc);
400
401 return GrContext::MakeVulkan(backendContext, options);
402}
403
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800404VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
405 return VkFunctorInitParams{
406 .instance = mInstance,
407 .physical_device = mPhysicalDevice,
408 .device = mDevice,
409 .queue = mGraphicsQueue,
410 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500411 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800412 .enabled_instance_extension_names = mInstanceExtensions.data(),
413 .enabled_instance_extension_names_length =
414 static_cast<uint32_t>(mInstanceExtensions.size()),
415 .enabled_device_extension_names = mDeviceExtensions.data(),
416 .enabled_device_extension_names_length =
417 static_cast<uint32_t>(mDeviceExtensions.size()),
418 .device_features_2 = &mPhysicalDeviceFeatures2,
419 };
420}
421
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500422Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500423 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
424
425 if (bufferInfo == nullptr) {
426 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
427 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500428 }
429
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500430 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500431
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500432 if (bufferInfo->dequeue_fence != -1) {
Stan Iliev197843d2019-03-21 11:34:15 -0400433 struct sync_file_info* finfo = sync_file_info(bufferInfo->dequeue_fence);
434 bool isSignalPending = false;
435 if (finfo != NULL) {
436 isSignalPending = finfo->status != 1;
437 sync_file_info_free(finfo);
438 }
439 if (isSignalPending) {
440 int fence_clone = dup(bufferInfo->dequeue_fence);
441 if (fence_clone == -1) {
442 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno),
443 errno);
444 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
445 } else {
446 VkSemaphoreCreateInfo semaphoreInfo;
447 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
448 semaphoreInfo.pNext = nullptr;
449 semaphoreInfo.flags = 0;
450 VkSemaphore semaphore;
451 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
452 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
453 err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500454
Stan Iliev197843d2019-03-21 11:34:15 -0400455 VkImportSemaphoreFdInfoKHR importInfo;
456 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
457 importInfo.pNext = nullptr;
458 importInfo.semaphore = semaphore;
459 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
460 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
461 importInfo.fd = fence_clone;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500462
Stan Iliev197843d2019-03-21 11:34:15 -0400463 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
464 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500465
Stan Iliev197843d2019-03-21 11:34:15 -0400466 GrBackendSemaphore backendSemaphore;
467 backendSemaphore.initVulkan(semaphore);
468 bufferInfo->skSurface->wait(1, &backendSemaphore);
469 // The following flush blocks the GPU immediately instead of waiting for other
470 // drawing ops. It seems dequeue_fence is not respected otherwise.
John Reck0fa0cbc2019-04-05 16:57:46 -0700471 // TODO: remove the flush after finding why backendSemaphore is not working.
Stan Iliev197843d2019-03-21 11:34:15 -0400472 bufferInfo->skSurface->flush();
473 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500474 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500475 }
476
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500477 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
478 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500479}
480
Greg Danield92a9b12019-04-23 10:11:04 -0400481struct DestroySemaphoreInfo {
482 PFN_vkDestroySemaphore mDestroyFunction;
483 VkDevice mDevice;
484 VkSemaphore mSemaphore;
485
486 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
487 VkSemaphore semaphore)
488 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
489};
490
491static void destroy_semaphore(void* context) {
492 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context);
493 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr);
494 delete info;
495}
496
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500497void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
498 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
499 ATRACE_NAME("Finishing GPU work");
500 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500501 }
502
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400503 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 Sollenbergera19b71a2019-02-15 16:36:30 -0500509 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 Sollenberger0e3cba32016-11-09 11:58:36 -0500513
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500514 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 Sollenberger0e3cba32016-11-09 11:58:36 -0500521
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500522 GrBackendSemaphore backendSemaphore;
523 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500524
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500525 int fenceFd = -1;
Greg Danield92a9b12019-04-23 10:11:04 -0400526 DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice,
527 semaphore);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500528 GrSemaphoresSubmitted submitted =
529 bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
Greg Danield92a9b12019-04-23 10:11:04 -0400530 kNone_GrFlushFlags, 1, &backendSemaphore,
531 destroy_semaphore, destroyInfo);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500532 if (submitted == GrSemaphoresSubmitted::kYes) {
533 VkSemaphoreGetFdInfoKHR getFdInfo;
534 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
535 getFdInfo.pNext = nullptr;
536 getFdInfo.semaphore = semaphore;
537 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500538
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500539 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
540 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
541 } else {
542 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
543 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500544 }
545
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500546 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500547}
548
549void 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 Daniel2ff202712018-06-14 11:50:10 -0400554 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500555
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500556 delete surface;
557}
558
Stan Iliev987a80c02018-12-04 10:07:21 -0500559VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800560 sk_sp<SkColorSpace> surfaceColorSpace,
John Reck0fa0cbc2019-04-05 16:57:46 -0700561 SkColorType surfaceColorType, GrContext* grContext,
562 uint32_t extraBuffers) {
Stan Iliev981afe72019-02-13 14:24:33 -0500563 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500564 if (!window) {
565 return nullptr;
566 }
567
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500568 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -0700569 *this, extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500570}
571
Greg Danield92a9b12019-04-23 10:11:04 -0400572status_t VulkanManager::fenceWait(sp<Fence>& fence, GrContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400573 if (!hasVkContext()) {
574 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
575 return INVALID_OPERATION;
576 }
577
Stan Iliev7a081272018-10-26 17:54:18 -0400578 // Block GPU on the fence.
579 int fenceFd = fence->dup();
580 if (fenceFd == -1) {
581 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
582 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000583 }
Stan Iliev7a081272018-10-26 17:54:18 -0400584
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 Danield92a9b12019-04-23 10:11:04 -0400605 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev7a081272018-10-26 17:54:18 -0400606 ALOGE("Failed to import semaphore, err: %d", err);
607 return UNKNOWN_ERROR;
608 }
609
Greg Danield92a9b12019-04-23 10:11:04 -0400610 GrBackendSemaphore beSemaphore;
611 beSemaphore.initVulkan(semaphore);
Stan Iliev7a081272018-10-26 17:54:18 -0400612
Greg Danield92a9b12019-04-23 10:11:04 -0400613 // Skia takes ownership of the semaphore and will delete it once the wait has finished.
614 grContext->wait(1, &beSemaphore);
615 grContext->flush();
Stan Iliev7a081272018-10-26 17:54:18 -0400616
Stan Iliev564ca3e2018-09-04 22:00:00 +0000617 return OK;
618}
619
Greg Danield92a9b12019-04-23 10:11:04 -0400620status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400621 if (!hasVkContext()) {
622 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
623 return INVALID_OPERATION;
624 }
625
Greg Daniel26e0dca2018-09-18 10:33:19 -0400626 VkExportSemaphoreCreateInfo exportInfo;
627 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
628 exportInfo.pNext = nullptr;
629 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
630
631 VkSemaphoreCreateInfo semaphoreInfo;
632 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
633 semaphoreInfo.pNext = &exportInfo;
634 semaphoreInfo.flags = 0;
635 VkSemaphore semaphore;
636 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
637 if (VK_SUCCESS != err) {
638 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
639 return INVALID_OPERATION;
640 }
641
Greg Danield92a9b12019-04-23 10:11:04 -0400642 GrBackendSemaphore backendSemaphore;
643 backendSemaphore.initVulkan(semaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400644
Greg Danield92a9b12019-04-23 10:11:04 -0400645 DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice,
646 semaphore);
647 GrSemaphoresSubmitted submitted =
648 grContext->flush(kNone_GrFlushFlags, 1, &backendSemaphore,
649 destroy_semaphore, destroyInfo);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400650
Greg Danield92a9b12019-04-23 10:11:04 -0400651 if (submitted == GrSemaphoresSubmitted::kNo) {
652 ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
653 mDestroySemaphore(mDevice, semaphore, nullptr);
654 return INVALID_OPERATION;
655 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400656
657 VkSemaphoreGetFdInfoKHR getFdInfo;
658 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
659 getFdInfo.pNext = nullptr;
660 getFdInfo.semaphore = semaphore;
661 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
662
663 int fenceFd = 0;
664
665 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
666 if (VK_SUCCESS != err) {
667 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
668 return INVALID_OPERATION;
669 }
670 nativeFence = new Fence(fenceFd);
671
Stan Iliev564ca3e2018-09-04 22:00:00 +0000672 return OK;
673}
674
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500675} /* namespace renderthread */
676} /* namespace uirenderer */
677} /* namespace android */