blob: 7f979d6c3e53c55568988afb563039ea374b24cd [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 <GrTypes.h>
33#include <vk/GrVkExtensions.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034#include <vk/GrVkTypes.h>
35
36namespace android {
37namespace uirenderer {
38namespace renderthread {
39
Bo Liu7b8c1eb2019-01-08 20:17:55 -080040static 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;
46 void* pNext;
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 Daniel2ff202712018-06-14 11:50:10 -040057#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 Sollenberger0e3cba32016-11-09 11:58:36 -050060
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050061void VulkanManager::destroy() {
Greg Daniel26e0dca2018-09-18 10:33:19 -040062 // We don't need to explicitly free the command buffer since it automatically gets freed when we
63 // delete the VkCommandPool below.
64 mDummyCB = VK_NULL_HANDLE;
65
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050066 if (VK_NULL_HANDLE != mCommandPool) {
Greg Daniel2ff202712018-06-14 11:50:10 -040067 mDestroyCommandPool(mDevice, mCommandPool, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050068 mCommandPool = VK_NULL_HANDLE;
69 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050070
Greg Daniel2ff202712018-06-14 11:50:10 -040071 if (mDevice != VK_NULL_HANDLE) {
72 mDeviceWaitIdle(mDevice);
73 mDestroyDevice(mDevice, nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070074 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050075
Greg Daniel2ff202712018-06-14 11:50:10 -040076 if (mInstance != VK_NULL_HANDLE) {
77 mDestroyInstance(mInstance, nullptr);
78 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050079
Greg Daniel2ff202712018-06-14 11:50:10 -040080 mGraphicsQueue = VK_NULL_HANDLE;
81 mPresentQueue = VK_NULL_HANDLE;
82 mDevice = VK_NULL_HANDLE;
83 mPhysicalDevice = VK_NULL_HANDLE;
84 mInstance = VK_NULL_HANDLE;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080085 mInstanceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080086 mInstanceExtensions.clear();
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080087 mDeviceExtensionsOwner.clear();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080088 mDeviceExtensions.clear();
89 free_features_extensions_structs(mPhysicalDeviceFeatures2);
90 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040091}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050092
Stan Iliev90276c82019-02-03 18:01:02 -050093void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -040094 VkResult err;
95
96 constexpr VkApplicationInfo app_info = {
97 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
98 nullptr, // pNext
99 "android framework", // pApplicationName
100 0, // applicationVersion
101 "android framework", // pEngineName
102 0, // engineVerison
Greg Danieleaf310e2019-01-28 16:10:32 -0500103 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400104 };
105
Greg Daniel2ff202712018-06-14 11:50:10 -0400106 {
107 GET_PROC(EnumerateInstanceExtensionProperties);
108
109 uint32_t extensionCount = 0;
110 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500111 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800112 mInstanceExtensionsOwner.resize(extensionCount);
113 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount,
114 mInstanceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500115 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400116 bool hasKHRSurfaceExtension = false;
117 bool hasKHRAndroidSurfaceExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800118 for (const VkExtensionProperties& extension : mInstanceExtensionsOwner) {
119 mInstanceExtensions.push_back(extension.extensionName);
120 if (!strcmp(extension.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400121 hasKHRSurfaceExtension = true;
122 }
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800123 if (!strcmp(extension.extensionName, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400124 hasKHRAndroidSurfaceExtension = true;
125 }
126 }
Stan Iliev90276c82019-02-03 18:01:02 -0500127 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400128 }
129
130 const VkInstanceCreateInfo instance_create = {
131 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
132 nullptr, // pNext
133 0, // flags
134 &app_info, // pApplicationInfo
135 0, // enabledLayerNameCount
136 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800137 (uint32_t) mInstanceExtensions.size(), // enabledExtensionNameCount
138 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400139 };
140
141 GET_PROC(CreateInstance);
142 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500143 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400144
145 GET_INST_PROC(DestroyInstance);
146 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniel96259622018-10-01 14:42:56 -0400147 GET_INST_PROC(GetPhysicalDeviceProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400148 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniela227dbb2018-08-20 09:19:48 -0400149 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500150 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400151 GET_INST_PROC(CreateDevice);
152 GET_INST_PROC(EnumerateDeviceExtensionProperties);
153 GET_INST_PROC(CreateAndroidSurfaceKHR);
154 GET_INST_PROC(DestroySurfaceKHR);
155 GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR);
156 GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
157 GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
158 GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
159
160 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500161 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
162 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400163 // Just returning the first physical device instead of getting the whole array. Since there
164 // should only be one device on android.
165 gpuCount = 1;
166 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
167 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500168 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400169
Greg Daniel96259622018-10-01 14:42:56 -0400170 VkPhysicalDeviceProperties physDeviceProperties;
171 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500172 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel96259622018-10-01 14:42:56 -0400173
Greg Daniel2ff202712018-06-14 11:50:10 -0400174 // query to get the initial queue props size
175 uint32_t queueCount;
176 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500177 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400178
179 // now get the actual queue props
180 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
181 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
182
183 // iterate to find the graphics queue
184 mGraphicsQueueIndex = queueCount;
185 for (uint32_t i = 0; i < queueCount; i++) {
186 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
187 mGraphicsQueueIndex = i;
188 break;
189 }
190 }
Stan Iliev90276c82019-02-03 18:01:02 -0500191 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400192
193 // All physical devices and queue families on Android must be capable of
194 // presentation with any native window. So just use the first one.
195 mPresentQueueIndex = 0;
196
Greg Daniel2ff202712018-06-14 11:50:10 -0400197 {
198 uint32_t extensionCount = 0;
199 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
200 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500201 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800202 mDeviceExtensionsOwner.resize(extensionCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400203 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800204 mDeviceExtensionsOwner.data());
Stan Iliev90276c82019-02-03 18:01:02 -0500205 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400206 bool hasKHRSwapchainExtension = false;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800207 for (const VkExtensionProperties& extension : mDeviceExtensionsOwner) {
208 mDeviceExtensions.push_back(extension.extensionName);
209 if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel2ff202712018-06-14 11:50:10 -0400210 hasKHRSwapchainExtension = true;
211 }
212 }
Stan Iliev90276c82019-02-03 18:01:02 -0500213 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400214 }
215
Greg Daniela227dbb2018-08-20 09:19:48 -0400216 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
217 if (device != VK_NULL_HANDLE) {
218 return vkGetDeviceProcAddr(device, proc_name);
219 }
220 return vkGetInstanceProcAddr(instance, proc_name);
221 };
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800222
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800223 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
224 mInstanceExtensions.data(), mDeviceExtensions.size(), mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400225
Stan Iliev90276c82019-02-03 18:01:02 -0500226 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400227
Greg Daniela227dbb2018-08-20 09:19:48 -0400228 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
229 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
230 features.pNext = nullptr;
231
232 // Setup all extension feature structs we may want to use.
233 void** tailPNext = &features.pNext;
234
235 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
236 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
237 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*) malloc(
238 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
239 LOG_ALWAYS_FATAL_IF(!blend);
240 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
241 blend->pNext = nullptr;
242 *tailPNext = blend;
243 tailPNext = &blend->pNext;
244 }
245
Greg Daniel05036172018-11-28 17:08:04 -0500246 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
247 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*) malloc(
248 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
249 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
250 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
251 ycbcrFeature->pNext = nullptr;
252 *tailPNext = ycbcrFeature;
253 tailPNext = &ycbcrFeature->pNext;
254
Greg Daniela227dbb2018-08-20 09:19:48 -0400255 // query to get the physical device features
256 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400257 // this looks like it would slow things down,
258 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400259 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400260
261 float queuePriorities[1] = { 0.0 };
262
Stan Iliev7e733362019-02-28 13:16:36 -0500263 void* queueNextPtr = nullptr;
264
265 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
266
267 if (Properties::contextPriority != 0
268 && grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
269 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
270 queuePriorityCreateInfo.sType =
271 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
272 queuePriorityCreateInfo.pNext = nullptr;
273 switch (Properties::contextPriority) {
274 case EGL_CONTEXT_PRIORITY_LOW_IMG:
275 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
276 break;
277 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
278 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
279 break;
280 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
281 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
282 break;
283 default:
284 LOG_ALWAYS_FATAL("Unsupported context priority");
285 }
286 queueNextPtr = &queuePriorityCreateInfo;
287 }
288
Greg Daniel2ff202712018-06-14 11:50:10 -0400289 const VkDeviceQueueCreateInfo queueInfo[2] = {
290 {
291 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500292 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400293 0, // VkDeviceQueueCreateFlags
294 mGraphicsQueueIndex, // queueFamilyIndex
295 1, // queueCount
296 queuePriorities, // pQueuePriorities
297 },
298 {
299 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500300 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400301 0, // VkDeviceQueueCreateFlags
302 mPresentQueueIndex, // queueFamilyIndex
303 1, // queueCount
304 queuePriorities, // pQueuePriorities
305 }
306 };
307 uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1;
308
309 const VkDeviceCreateInfo deviceInfo = {
310 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
Greg Daniela227dbb2018-08-20 09:19:48 -0400311 &features, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400312 0, // VkDeviceCreateFlags
313 queueInfoCount, // queueCreateInfoCount
314 queueInfo, // pQueueCreateInfos
315 0, // layerCount
316 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800317 (uint32_t) mDeviceExtensions.size(), // extensionCount
318 mDeviceExtensions.data(), // ppEnabledExtensionNames
Greg Daniela227dbb2018-08-20 09:19:48 -0400319 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400320 };
321
Stan Iliev90276c82019-02-03 18:01:02 -0500322 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400323
324 GET_DEV_PROC(GetDeviceQueue);
325 GET_DEV_PROC(DeviceWaitIdle);
326 GET_DEV_PROC(DestroyDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500327 GET_DEV_PROC(CreateCommandPool);
328 GET_DEV_PROC(DestroyCommandPool);
329 GET_DEV_PROC(AllocateCommandBuffers);
330 GET_DEV_PROC(FreeCommandBuffers);
331 GET_DEV_PROC(ResetCommandBuffer);
332 GET_DEV_PROC(BeginCommandBuffer);
333 GET_DEV_PROC(EndCommandBuffer);
334 GET_DEV_PROC(CmdPipelineBarrier);
335 GET_DEV_PROC(GetDeviceQueue);
336 GET_DEV_PROC(QueueSubmit);
337 GET_DEV_PROC(QueueWaitIdle);
338 GET_DEV_PROC(DeviceWaitIdle);
339 GET_DEV_PROC(CreateSemaphore);
340 GET_DEV_PROC(DestroySemaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400341 GET_DEV_PROC(ImportSemaphoreFdKHR);
342 GET_DEV_PROC(GetSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500343 GET_DEV_PROC(CreateFence);
344 GET_DEV_PROC(DestroyFence);
345 GET_DEV_PROC(WaitForFences);
346 GET_DEV_PROC(ResetFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400347}
348
349void VulkanManager::initialize() {
350 if (mDevice != VK_NULL_HANDLE) {
351 return;
352 }
353
Greg Daniela227dbb2018-08-20 09:19:48 -0400354 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500355 uint32_t instanceVersion;
356 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
357 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400358
Stan Iliev981afe72019-02-13 14:24:33 -0500359 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400360
361 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
362
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500363 // create the command pool for the command buffers
364 if (VK_NULL_HANDLE == mCommandPool) {
365 VkCommandPoolCreateInfo commandPoolInfo;
366 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
367 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
368 // this needs to be on the render queue
Greg Daniel2ff202712018-06-14 11:50:10 -0400369 commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500370 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
Greg Daniel2ff202712018-06-14 11:50:10 -0400371 SkDEBUGCODE(VkResult res =) mCreateCommandPool(mDevice, &commandPoolInfo, nullptr,
372 &mCommandPool);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500373 SkASSERT(VK_SUCCESS == res);
374 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400375 LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE);
376
377 if (!setupDummyCommandBuffer()) {
378 this->destroy();
Stan Iliev90276c82019-02-03 18:01:02 -0500379 // Pass through will crash on next line.
Greg Daniel26e0dca2018-09-18 10:33:19 -0400380 }
381 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
382
Greg Daniel2ff202712018-06-14 11:50:10 -0400383 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500384
Greg Danielcd558522016-11-17 13:31:40 -0500385 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
386 mSwapBehavior = SwapBehavior::BufferAge;
387 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500388}
389
Stan Iliev898123b2019-02-14 14:57:44 -0500390sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) {
Stan Iliev981afe72019-02-13 14:24:33 -0500391 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
392 if (device != VK_NULL_HANDLE) {
393 return vkGetDeviceProcAddr(device, proc_name);
394 }
395 return vkGetInstanceProcAddr(instance, proc_name);
396 };
397
398 GrVkBackendContext backendContext;
399 backendContext.fInstance = mInstance;
400 backendContext.fPhysicalDevice = mPhysicalDevice;
401 backendContext.fDevice = mDevice;
402 backendContext.fQueue = mGraphicsQueue;
403 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
404 backendContext.fMaxAPIVersion = mAPIVersion;
405 backendContext.fVkExtensions = &mExtensions;
406 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
407 backendContext.fGetProc = std::move(getProc);
408
409 return GrContext::MakeVulkan(backendContext, options);
410}
411
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800412VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
413 return VkFunctorInitParams{
414 .instance = mInstance,
415 .physical_device = mPhysicalDevice,
416 .device = mDevice,
417 .queue = mGraphicsQueue,
418 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500419 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800420 .enabled_instance_extension_names = mInstanceExtensions.data(),
421 .enabled_instance_extension_names_length =
422 static_cast<uint32_t>(mInstanceExtensions.size()),
423 .enabled_device_extension_names = mDeviceExtensions.data(),
424 .enabled_device_extension_names_length =
425 static_cast<uint32_t>(mDeviceExtensions.size()),
426 .device_features_2 = &mPhysicalDeviceFeatures2,
427 };
428}
429
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500430Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500431
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500432 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
433
434 if (bufferInfo == nullptr) {
435 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
436 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500437 }
438
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500439 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500440
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500441 if (bufferInfo->dequeue_fence != -1) {
442 int fence_clone = dup(bufferInfo->dequeue_fence);
443 if (fence_clone == -1) {
444 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno), errno);
445 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
446 } else {
447 VkSemaphoreCreateInfo semaphoreInfo;
448 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
449 semaphoreInfo.pNext = nullptr;
450 semaphoreInfo.flags = 0;
451 VkSemaphore semaphore;
452 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
453 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
454 err);
455
456 VkImportSemaphoreFdInfoKHR importInfo;
457 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
458 importInfo.pNext = nullptr;
459 importInfo.semaphore = semaphore;
460 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
461 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
462 importInfo.fd = fence_clone;
463
464 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
465 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
466
467 GrBackendSemaphore backendSemaphore;
468 backendSemaphore.initVulkan(semaphore);
469 bufferInfo->skSurface->wait(1, &backendSemaphore);
Stan Iliev3e99fa72019-03-21 11:20:29 -0400470 // The following flush blocks the GPU immediately instead of waiting for other
471 // drawing ops. It seems dequeue_fence is not respected otherwise.
472 //TODO: remove the flush after finding why backendSemaphore is not working.
473 bufferInfo->skSurface->flush();
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
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500481void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
482 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
483 ATRACE_NAME("Finishing GPU work");
484 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500485 }
486
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500487 VkExportSemaphoreCreateInfo exportInfo;
488 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
489 exportInfo.pNext = nullptr;
490 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500491
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500492 VkSemaphoreCreateInfo semaphoreInfo;
493 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
494 semaphoreInfo.pNext = &exportInfo;
495 semaphoreInfo.flags = 0;
496 VkSemaphore semaphore;
497 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
498 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500499
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500500 GrBackendSemaphore backendSemaphore;
501 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500502
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500503 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500504
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500505 int fenceFd = -1;
506 GrSemaphoresSubmitted submitted =
507 bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
508 SkSurface::kNone_FlushFlags, 1, &backendSemaphore);
509 if (submitted == GrSemaphoresSubmitted::kYes) {
510 VkSemaphoreGetFdInfoKHR getFdInfo;
511 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
512 getFdInfo.pNext = nullptr;
513 getFdInfo.semaphore = semaphore;
514 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500515
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500516 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
517 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
518 } else {
519 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
520 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500521 }
522
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500523 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500524
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500525 // Exporting a semaphore with copy transference via vkGetSemaphoreFdKHR, has the same effect of
526 // destroying the semaphore and creating a new one with the same handle, and the payloads
527 // ownership is move to the Fd we created. Thus the semaphore is in a state that we can delete
528 // it and we don't need to wait on the command buffer we submitted to finish.
529 mDestroySemaphore(mDevice, semaphore, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500530}
531
532void VulkanManager::destroySurface(VulkanSurface* surface) {
533 // Make sure all submit commands have finished before starting to destroy objects.
534 if (VK_NULL_HANDLE != mPresentQueue) {
535 mQueueWaitIdle(mPresentQueue);
536 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400537 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500538
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500539 delete surface;
540}
541
Stan Iliev987a80c2018-12-04 10:07:21 -0500542VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800543 sk_sp<SkColorSpace> surfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500544 SkColorType surfaceColorType,
545 GrContext* grContext) {
546 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500547 if (!window) {
548 return nullptr;
549 }
550
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500551 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
552 *this);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500553}
554
Greg Daniel26e0dca2018-09-18 10:33:19 -0400555bool VulkanManager::setupDummyCommandBuffer() {
556 if (mDummyCB != VK_NULL_HANDLE) {
557 return true;
558 }
559
560 VkCommandBufferAllocateInfo commandBuffersInfo;
561 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
562 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
563 commandBuffersInfo.pNext = nullptr;
564 commandBuffersInfo.commandPool = mCommandPool;
565 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
566 commandBuffersInfo.commandBufferCount = 1;
567
568 VkResult err = mAllocateCommandBuffers(mDevice, &commandBuffersInfo, &mDummyCB);
569 if (err != VK_SUCCESS) {
570 // It is probably unnecessary to set this back to VK_NULL_HANDLE, but we set it anyways to
571 // make sure the driver didn't set a value and then return a failure.
572 mDummyCB = VK_NULL_HANDLE;
573 return false;
574 }
575
576 VkCommandBufferBeginInfo beginInfo;
577 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
578 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
579 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
580
581 mBeginCommandBuffer(mDummyCB, &beginInfo);
582 mEndCommandBuffer(mDummyCB);
583 return true;
584}
585
Stan Iliev564ca3e2018-09-04 22:00:00 +0000586status_t VulkanManager::fenceWait(sp<Fence>& fence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400587 if (!hasVkContext()) {
588 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
589 return INVALID_OPERATION;
590 }
591
Stan Iliev7a081272018-10-26 17:54:18 -0400592 // Block GPU on the fence.
593 int fenceFd = fence->dup();
594 if (fenceFd == -1) {
595 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
596 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000597 }
Stan Iliev7a081272018-10-26 17:54:18 -0400598
599 VkSemaphoreCreateInfo semaphoreInfo;
600 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
601 semaphoreInfo.pNext = nullptr;
602 semaphoreInfo.flags = 0;
603 VkSemaphore semaphore;
604 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
605 if (VK_SUCCESS != err) {
606 ALOGE("Failed to create import semaphore, err: %d", err);
607 return UNKNOWN_ERROR;
608 }
609 VkImportSemaphoreFdInfoKHR importInfo;
610 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
611 importInfo.pNext = nullptr;
612 importInfo.semaphore = semaphore;
613 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
614 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
615 importInfo.fd = fenceFd;
616
617 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
618 if (VK_SUCCESS != err) {
619 ALOGE("Failed to import semaphore, err: %d", err);
620 return UNKNOWN_ERROR;
621 }
622
623 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
624
625 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
626
627 VkSubmitInfo submitInfo;
628 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
629 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
630 submitInfo.waitSemaphoreCount = 1;
631 // Wait to make sure aquire semaphore set above has signaled.
632 submitInfo.pWaitSemaphores = &semaphore;
633 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
634 submitInfo.commandBufferCount = 1;
635 submitInfo.pCommandBuffers = &mDummyCB;
636 submitInfo.signalSemaphoreCount = 0;
637
638 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
639
640 // On Android when we import a semaphore, it is imported using temporary permanence. That
641 // means as soon as we queue the semaphore for a wait it reverts to its previous permanent
642 // state before importing. This means it will now be in an idle state with no pending
643 // signal or wait operations, so it is safe to immediately delete it.
644 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000645 return OK;
646}
647
648status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400649 if (!hasVkContext()) {
650 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
651 return INVALID_OPERATION;
652 }
653
Greg Daniel26e0dca2018-09-18 10:33:19 -0400654 VkExportSemaphoreCreateInfo exportInfo;
655 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
656 exportInfo.pNext = nullptr;
657 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
658
659 VkSemaphoreCreateInfo semaphoreInfo;
660 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
661 semaphoreInfo.pNext = &exportInfo;
662 semaphoreInfo.flags = 0;
663 VkSemaphore semaphore;
664 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
665 if (VK_SUCCESS != err) {
666 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
667 return INVALID_OPERATION;
668 }
669
670 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
671
672 VkSubmitInfo submitInfo;
673 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
674 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
675 submitInfo.waitSemaphoreCount = 0;
676 submitInfo.pWaitSemaphores = nullptr;
677 submitInfo.pWaitDstStageMask = nullptr;
678 submitInfo.commandBufferCount = 1;
679 submitInfo.pCommandBuffers = &mDummyCB;
680 submitInfo.signalSemaphoreCount = 1;
681 submitInfo.pSignalSemaphores = &semaphore;
682
683 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
684
685 VkSemaphoreGetFdInfoKHR getFdInfo;
686 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
687 getFdInfo.pNext = nullptr;
688 getFdInfo.semaphore = semaphore;
689 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
690
691 int fenceFd = 0;
692
693 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
694 if (VK_SUCCESS != err) {
695 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
696 return INVALID_OPERATION;
697 }
698 nativeFence = new Fence(fenceFd);
699
700 // Exporting a semaphore with copy transference via vkGetSemahporeFdKHR, has the same effect of
701 // destroying the semaphore and creating a new one with the same handle, and the payloads
702 // ownership is move to the Fd we created. Thus the semahpore is in a state that we can delete
703 // it and we don't need to wait on the command buffer we submitted to finish.
704 mDestroySemaphore(mDevice, semaphore, nullptr);
705
Stan Iliev564ca3e2018-09-04 22:00:00 +0000706 return OK;
707}
708
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500709} /* namespace renderthread */
710} /* namespace uirenderer */
711} /* namespace android */