blob: 6dacc7a0ea073d07cdbd5ec9e608bd76bd200ada [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"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050026
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050027#include <GrBackendSemaphore.h>
Greg Danielac2d2322017-07-12 11:30:15 -040028#include <GrBackendSurface.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050029#include <GrContext.h>
30#include <GrTypes.h>
Greg Daniela227dbb2018-08-20 09:19:48 -040031#include <GrTypes.h>
32#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;
45 void* pNext;
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 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() {
Greg Daniel26e0dca2018-09-18 10:33:19 -040061 // We don't need to explicitly free the command buffer since it automatically gets freed when we
62 // delete the VkCommandPool below.
63 mDummyCB = VK_NULL_HANDLE;
64
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065 if (VK_NULL_HANDLE != mCommandPool) {
Greg Daniel2ff202712018-06-14 11:50:10 -040066 mDestroyCommandPool(mDevice, mCommandPool, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050067 mCommandPool = VK_NULL_HANDLE;
68 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069
Greg Daniel2ff202712018-06-14 11:50:10 -040070 if (mDevice != VK_NULL_HANDLE) {
71 mDeviceWaitIdle(mDevice);
72 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070073 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074
Greg Daniel2ff202712018-06-14 11:50:10 -040075 if (mInstance != VK_NULL_HANDLE) {
76 mDestroyInstance(mInstance, nullptr);
77 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078
Greg Daniel2ff202712018-06-14 11:50:10 -040079 mGraphicsQueue = VK_NULL_HANDLE;
80 mPresentQueue = VK_NULL_HANDLE;
81 mDevice = VK_NULL_HANDLE;
82 mPhysicalDevice = VK_NULL_HANDLE;
83 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080084 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080085 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080086 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080087 mDeviceExtensions.clear();
88 free_features_extensions_structs(mPhysicalDeviceFeatures2);
89 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040090}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050091
Stan Iliev90276c82019-02-03 18:01:02 -050092void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -040093 VkResult err;
94
95 constexpr VkApplicationInfo app_info = {
96 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
97 nullptr, // pNext
98 "android framework", // pApplicationName
99 0, // applicationVersion
100 "android framework", // pEngineName
101 0, // engineVerison
Greg Danieleaf310e2019-01-28 16:10:32 -0500102 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400103 };
104
Greg Daniel2ff202712018-06-14 11:50:10 -0400105 {
106 GET_PROC(EnumerateInstanceExtensionProperties);
107
108 uint32_t extensionCount = 0;
109 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500110 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800111 mInstanceExtensionsOwner.resize(extensionCount);
112 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
113 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500114 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400115 bool hasKHRSurfaceExtension = false;
116 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800117 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
118 mInstanceExtensions.push_back(extension.extensionName);
119 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400120 hasKHRSurfaceExtension = true;
121 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800122 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400123 hasKHRAndroidSurfaceExtension = true;
124 }
125 }
Stan Iliev90276c82019-02-03 18:01:02 -0500126 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 }
128
129 const VkInstanceCreateInfo instance_create = {
130 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
131 nullptr, // pNext
132 0, // flags
133 &app_info, // pApplicationInfo
134 0, // enabledLayerNameCount
135 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800136 (uint32_t) mInstanceExtensions.size(), // enabledExtensionNameCount
137 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400138 };
139
140 GET_PROC(CreateInstance);
141 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500142 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400143
144 GET_INST_PROC(DestroyInstance);
145 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniel96259622018-10-01 14:42:56 -0400146 GET_INST_PROC(GetPhysicalDeviceProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400147 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniela227dbb2018-08-20 09:19:48 -0400148 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500149 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400150 GET_INST_PROC(CreateDevice);
151 GET_INST_PROC(EnumerateDeviceExtensionProperties);
152 GET_INST_PROC(CreateAndroidSurfaceKHR);
153 GET_INST_PROC(DestroySurfaceKHR);
154 GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR);
155 GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
156 GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
157 GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
158
159 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500160 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
161 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400162 // Just returning the first physical device instead of getting the whole array. Since there
163 // should only be one device on android.
164 gpuCount = 1;
165 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
166 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500167 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400168
Greg Daniel96259622018-10-01 14:42:56 -0400169 VkPhysicalDeviceProperties physDeviceProperties;
170 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500171 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel96259622018-10-01 14:42:56 -0400172
Greg Daniel2ff202712018-06-14 11:50:10 -0400173 // query to get the initial queue props size
174 uint32_t queueCount;
175 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500176 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400177
178 // now get the actual queue props
179 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
180 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
181
182 // iterate to find the graphics queue
183 mGraphicsQueueIndex = queueCount;
184 for (uint32_t i = 0; i < queueCount; i++) {
185 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
186 mGraphicsQueueIndex = i;
187 break;
188 }
189 }
Stan Iliev90276c82019-02-03 18:01:02 -0500190 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400191
192 // All physical devices and queue families on Android must be capable of
193 // presentation with any native window. So just use the first one.
194 mPresentQueueIndex = 0;
195
Greg Daniel2ff202712018-06-14 11:50:10 -0400196 {
197 uint32_t extensionCount = 0;
198 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
199 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500200 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800201 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400202 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800203 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500204 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400205 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800206 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
207 mDeviceExtensions.push_back(extension.extensionName);
208 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400209 hasKHRSwapchainExtension = true;
210 }
211 }
Stan Iliev90276c82019-02-03 18:01:02 -0500212 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400213 }
214
Greg Daniela227dbb2018-08-20 09:19:48 -0400215 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
216 if (device != VK_NULL_HANDLE) {
217 return vkGetDeviceProcAddr(device, proc_name);
218 }
219 return vkGetInstanceProcAddr(instance, proc_name);
220 };
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800221
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800222 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
223 mInstanceExtensions.data(), mDeviceExtensions.size(), 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;
236 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*) malloc(
237 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;
246 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*) malloc(
247 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
260 float queuePriorities[1] = { 0.0 };
261
Stan Iliev7e733362019-02-28 13:16:36 -0500262 void* queueNextPtr = nullptr;
263
264 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
265
266 if (Properties::contextPriority != 0
267 && grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
268 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
269 queuePriorityCreateInfo.sType =
270 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
271 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");
284 }
285 queueNextPtr = &queuePriorityCreateInfo;
286 }
287
Greg Daniel2ff202712018-06-14 11:50:10 -0400288 const VkDeviceQueueCreateInfo queueInfo[2] = {
289 {
290 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500291 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400292 0, // VkDeviceQueueCreateFlags
293 mGraphicsQueueIndex, // queueFamilyIndex
294 1, // queueCount
295 queuePriorities, // pQueuePriorities
296 },
297 {
298 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500299 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400300 0, // VkDeviceQueueCreateFlags
301 mPresentQueueIndex, // queueFamilyIndex
302 1, // queueCount
303 queuePriorities, // pQueuePriorities
304 }
305 };
306 uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1;
307
308 const VkDeviceCreateInfo deviceInfo = {
309 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
Greg Daniela227dbb2018-08-20 09:19:48 -0400310 &features, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400311 0, // VkDeviceCreateFlags
312 queueInfoCount, // queueCreateInfoCount
313 queueInfo, // pQueueCreateInfos
314 0, // layerCount
315 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800316 (uint32_t) mDeviceExtensions.size(), // extensionCount
317 mDeviceExtensions.data(), // ppEnabledExtensionNames
Greg Daniela227dbb2018-08-20 09:19:48 -0400318 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400319 };
320
Stan Iliev90276c82019-02-03 18:01:02 -0500321 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400322
323 GET_DEV_PROC(GetDeviceQueue);
324 GET_DEV_PROC(DeviceWaitIdle);
325 GET_DEV_PROC(DestroyDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500326 GET_DEV_PROC(CreateCommandPool);
327 GET_DEV_PROC(DestroyCommandPool);
328 GET_DEV_PROC(AllocateCommandBuffers);
329 GET_DEV_PROC(FreeCommandBuffers);
330 GET_DEV_PROC(ResetCommandBuffer);
331 GET_DEV_PROC(BeginCommandBuffer);
332 GET_DEV_PROC(EndCommandBuffer);
333 GET_DEV_PROC(CmdPipelineBarrier);
334 GET_DEV_PROC(GetDeviceQueue);
335 GET_DEV_PROC(QueueSubmit);
336 GET_DEV_PROC(QueueWaitIdle);
337 GET_DEV_PROC(DeviceWaitIdle);
338 GET_DEV_PROC(CreateSemaphore);
339 GET_DEV_PROC(DestroySemaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400340 GET_DEV_PROC(ImportSemaphoreFdKHR);
341 GET_DEV_PROC(GetSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500342 GET_DEV_PROC(CreateFence);
343 GET_DEV_PROC(DestroyFence);
344 GET_DEV_PROC(WaitForFences);
345 GET_DEV_PROC(ResetFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400346}
347
348void VulkanManager::initialize() {
349 if (mDevice != VK_NULL_HANDLE) {
350 return;
351 }
352
Greg Daniela227dbb2018-08-20 09:19:48 -0400353 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500354 uint32_t instanceVersion;
355 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
356 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400357
Stan Iliev981afe72019-02-13 14:24:33 -0500358 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400359
360 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
361
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500362 // create the command pool for the command buffers
363 if (VK_NULL_HANDLE == mCommandPool) {
364 VkCommandPoolCreateInfo commandPoolInfo;
365 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
366 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
367 // this needs to be on the render queue
Greg Daniel2ff202712018-06-14 11:50:10 -0400368 commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500369 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
Greg Daniel2ff202712018-06-14 11:50:10 -0400370 SkDEBUGCODE(VkResult res =) mCreateCommandPool(mDevice, &commandPoolInfo, nullptr,
371 &mCommandPool);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500372 SkASSERT(VK_SUCCESS == res);
373 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400374 LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE);
375
376 if (!setupDummyCommandBuffer()) {
377 this->destroy();
Stan Iliev90276c82019-02-03 18:01:02 -0500378 // Pass through will crash on next line.
Greg Daniel26e0dca2018-09-18 10:33:19 -0400379 }
380 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
381
Greg Daniel2ff202712018-06-14 11:50:10 -0400382 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500383
Greg Danielcd558522016-11-17 13:31:40 -0500384 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
385 mSwapBehavior = SwapBehavior::BufferAge;
386 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500387}
388
Stan Iliev898123b2019-02-14 14:57:44 -0500389sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) {
Stan Iliev981afe72019-02-13 14:24:33 -0500390 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
391 if (device != VK_NULL_HANDLE) {
392 return vkGetDeviceProcAddr(device, proc_name);
393 }
394 return vkGetInstanceProcAddr(instance, proc_name);
395 };
396
397 GrVkBackendContext backendContext;
398 backendContext.fInstance = mInstance;
399 backendContext.fPhysicalDevice = mPhysicalDevice;
400 backendContext.fDevice = mDevice;
401 backendContext.fQueue = mGraphicsQueue;
402 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
403 backendContext.fMaxAPIVersion = mAPIVersion;
404 backendContext.fVkExtensions = &mExtensions;
405 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
406 backendContext.fGetProc = std::move(getProc);
407
408 return GrContext::MakeVulkan(backendContext, options);
409}
410
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800411VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
412 return VkFunctorInitParams{
413 .instance = mInstance,
414 .physical_device = mPhysicalDevice,
415 .device = mDevice,
416 .queue = mGraphicsQueue,
417 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500418 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800419 .enabled_instance_extension_names = mInstanceExtensions.data(),
420 .enabled_instance_extension_names_length =
421 static_cast<uint32_t>(mInstanceExtensions.size()),
422 .enabled_device_extension_names = mDeviceExtensions.data(),
423 .enabled_device_extension_names_length =
424 static_cast<uint32_t>(mDeviceExtensions.size()),
425 .device_features_2 = &mPhysicalDeviceFeatures2,
426 };
427}
428
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500429Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500430
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500431 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
432
433 if (bufferInfo == nullptr) {
434 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
435 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500436 }
437
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500438 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500439
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500440 if (bufferInfo->dequeue_fence != -1) {
441 int fence_clone = dup(bufferInfo->dequeue_fence);
442 if (fence_clone == -1) {
443 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno), 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);
454
455 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;
462
463 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
464 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
465
466 GrBackendSemaphore backendSemaphore;
467 backendSemaphore.initVulkan(semaphore);
468 bufferInfo->skSurface->wait(1, &backendSemaphore);
469 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500470 }
471
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500472 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
473 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500474}
475
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500476void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
477 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
478 ATRACE_NAME("Finishing GPU work");
479 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500480 }
481
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500482 VkExportSemaphoreCreateInfo exportInfo;
483 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
484 exportInfo.pNext = nullptr;
485 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500486
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500487 VkSemaphoreCreateInfo semaphoreInfo;
488 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
489 semaphoreInfo.pNext = &exportInfo;
490 semaphoreInfo.flags = 0;
491 VkSemaphore semaphore;
492 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
493 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500494
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500495 GrBackendSemaphore backendSemaphore;
496 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500497
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500498 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500499
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500500 int fenceFd = -1;
501 GrSemaphoresSubmitted submitted =
502 bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
503 SkSurface::kNone_FlushFlags, 1, &backendSemaphore);
504 if (submitted == GrSemaphoresSubmitted::kYes) {
505 VkSemaphoreGetFdInfoKHR getFdInfo;
506 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
507 getFdInfo.pNext = nullptr;
508 getFdInfo.semaphore = semaphore;
509 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500510
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500511 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
512 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
513 } else {
514 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
515 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500516 }
517
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500518 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500519
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500520 // Exporting a semaphore with copy transference via vkGetSemaphoreFdKHR, has the same effect of
521 // destroying the semaphore and creating a new one with the same handle, and the payloads
522 // ownership is move to the Fd we created. Thus the semaphore is in a state that we can delete
523 // it and we don't need to wait on the command buffer we submitted to finish.
524 mDestroySemaphore(mDevice, semaphore, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500525}
526
527void VulkanManager::destroySurface(VulkanSurface* surface) {
528 // Make sure all submit commands have finished before starting to destroy objects.
529 if (VK_NULL_HANDLE != mPresentQueue) {
530 mQueueWaitIdle(mPresentQueue);
531 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400532 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500533
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500534 delete surface;
535}
536
Stan Iliev987a80c2018-12-04 10:07:21 -0500537VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800538 sk_sp<SkColorSpace> surfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500539 SkColorType surfaceColorType,
540 GrContext* grContext) {
541 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500542 if (!window) {
543 return nullptr;
544 }
545
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500546 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
547 *this);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500548}
549
Greg Daniel26e0dca2018-09-18 10:33:19 -0400550bool VulkanManager::setupDummyCommandBuffer() {
551 if (mDummyCB != VK_NULL_HANDLE) {
552 return true;
553 }
554
555 VkCommandBufferAllocateInfo commandBuffersInfo;
556 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
557 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
558 commandBuffersInfo.pNext = nullptr;
559 commandBuffersInfo.commandPool = mCommandPool;
560 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
561 commandBuffersInfo.commandBufferCount = 1;
562
563 VkResult err = mAllocateCommandBuffers(mDevice, &commandBuffersInfo, &mDummyCB);
564 if (err != VK_SUCCESS) {
565 // It is probably unnecessary to set this back to VK_NULL_HANDLE, but we set it anyways to
566 // make sure the driver didn't set a value and then return a failure.
567 mDummyCB = VK_NULL_HANDLE;
568 return false;
569 }
570
571 VkCommandBufferBeginInfo beginInfo;
572 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
573 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
574 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
575
576 mBeginCommandBuffer(mDummyCB, &beginInfo);
577 mEndCommandBuffer(mDummyCB);
578 return true;
579}
580
Stan Iliev564ca3e2018-09-04 22:00:00 +0000581status_t VulkanManager::fenceWait(sp<Fence>& fence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400582 if (!hasVkContext()) {
583 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
584 return INVALID_OPERATION;
585 }
586
Stan Iliev7a081272018-10-26 17:54:18 -0400587 // Block GPU on the fence.
588 int fenceFd = fence->dup();
589 if (fenceFd == -1) {
590 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
591 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000592 }
Stan Iliev7a081272018-10-26 17:54:18 -0400593
594 VkSemaphoreCreateInfo semaphoreInfo;
595 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
596 semaphoreInfo.pNext = nullptr;
597 semaphoreInfo.flags = 0;
598 VkSemaphore semaphore;
599 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
600 if (VK_SUCCESS != err) {
601 ALOGE("Failed to create import semaphore, err: %d", err);
602 return UNKNOWN_ERROR;
603 }
604 VkImportSemaphoreFdInfoKHR importInfo;
605 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
606 importInfo.pNext = nullptr;
607 importInfo.semaphore = semaphore;
608 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
609 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
610 importInfo.fd = fenceFd;
611
612 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
613 if (VK_SUCCESS != err) {
614 ALOGE("Failed to import semaphore, err: %d", err);
615 return UNKNOWN_ERROR;
616 }
617
618 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
619
620 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
621
622 VkSubmitInfo submitInfo;
623 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
624 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
625 submitInfo.waitSemaphoreCount = 1;
626 // Wait to make sure aquire semaphore set above has signaled.
627 submitInfo.pWaitSemaphores = &semaphore;
628 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
629 submitInfo.commandBufferCount = 1;
630 submitInfo.pCommandBuffers = &mDummyCB;
631 submitInfo.signalSemaphoreCount = 0;
632
633 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
634
635 // On Android when we import a semaphore, it is imported using temporary permanence. That
636 // means as soon as we queue the semaphore for a wait it reverts to its previous permanent
637 // state before importing. This means it will now be in an idle state with no pending
638 // signal or wait operations, so it is safe to immediately delete it.
639 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000640 return OK;
641}
642
643status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400644 if (!hasVkContext()) {
645 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
646 return INVALID_OPERATION;
647 }
648
Greg Daniel26e0dca2018-09-18 10:33:19 -0400649 VkExportSemaphoreCreateInfo exportInfo;
650 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
651 exportInfo.pNext = nullptr;
652 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
653
654 VkSemaphoreCreateInfo semaphoreInfo;
655 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
656 semaphoreInfo.pNext = &exportInfo;
657 semaphoreInfo.flags = 0;
658 VkSemaphore semaphore;
659 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
660 if (VK_SUCCESS != err) {
661 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
662 return INVALID_OPERATION;
663 }
664
665 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
666
667 VkSubmitInfo submitInfo;
668 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
669 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
670 submitInfo.waitSemaphoreCount = 0;
671 submitInfo.pWaitSemaphores = nullptr;
672 submitInfo.pWaitDstStageMask = nullptr;
673 submitInfo.commandBufferCount = 1;
674 submitInfo.pCommandBuffers = &mDummyCB;
675 submitInfo.signalSemaphoreCount = 1;
676 submitInfo.pSignalSemaphores = &semaphore;
677
678 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
679
680 VkSemaphoreGetFdInfoKHR getFdInfo;
681 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
682 getFdInfo.pNext = nullptr;
683 getFdInfo.semaphore = semaphore;
684 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
685
686 int fenceFd = 0;
687
688 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
689 if (VK_SUCCESS != err) {
690 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
691 return INVALID_OPERATION;
692 }
693 nativeFence = new Fence(fenceFd);
694
695 // Exporting a semaphore with copy transference via vkGetSemahporeFdKHR, has the same effect of
696 // destroying the semaphore and creating a new one with the same handle, and the payloads
697 // ownership is move to the Fd we created. Thus the semahpore is in a state that we can delete
698 // it and we don't need to wait on the command buffer we submitted to finish.
699 mDestroySemaphore(mDevice, semaphore, nullptr);
700
Stan Iliev564ca3e2018-09-04 22:00:00 +0000701 return OK;
702}
703
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500704} /* namespace renderthread */
705} /* namespace uirenderer */
706} /* namespace android */