blob: e9032afb445abbfff2d437d03764a9cf792a6d5e [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;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080085 mInstanceExtensions.clear();
86 mDeviceExtensions.clear();
87 free_features_extensions_structs(mPhysicalDeviceFeatures2);
88 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040089}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050090
Stan Iliev90276c82019-02-03 18:01:02 -050091void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -040092 VkResult err;
93
94 constexpr VkApplicationInfo app_info = {
95 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
96 nullptr, // pNext
97 "android framework", // pApplicationName
98 0, // applicationVersion
99 "android framework", // pEngineName
100 0, // engineVerison
Greg Danieleaf310e2019-01-28 16:10:32 -0500101 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400102 };
103
Greg Daniel2ff202712018-06-14 11:50:10 -0400104 {
105 GET_PROC(EnumerateInstanceExtensionProperties);
106
107 uint32_t extensionCount = 0;
108 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500109 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400110 std::unique_ptr<VkExtensionProperties[]> extensions(
111 new VkExtensionProperties[extensionCount]);
112 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.get());
Stan Iliev90276c82019-02-03 18:01:02 -0500113 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400114 bool hasKHRSurfaceExtension = false;
115 bool hasKHRAndroidSurfaceExtension = false;
116 for (uint32_t i = 0; i < extensionCount; ++i) {
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800117 mInstanceExtensions.push_back(extensions[i].extensionName);
Greg Daniel2ff202712018-06-14 11:50:10 -0400118 if (!strcmp(extensions[i].extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
119 hasKHRSurfaceExtension = true;
120 }
121 if (!strcmp(extensions[i].extensionName,VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
122 hasKHRAndroidSurfaceExtension = true;
123 }
124 }
Stan Iliev90276c82019-02-03 18:01:02 -0500125 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400126 }
127
128 const VkInstanceCreateInfo instance_create = {
129 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
130 nullptr, // pNext
131 0, // flags
132 &app_info, // pApplicationInfo
133 0, // enabledLayerNameCount
134 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800135 (uint32_t) mInstanceExtensions.size(), // enabledExtensionNameCount
136 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400137 };
138
139 GET_PROC(CreateInstance);
140 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500141 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400142
143 GET_INST_PROC(DestroyInstance);
144 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniel96259622018-10-01 14:42:56 -0400145 GET_INST_PROC(GetPhysicalDeviceProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400146 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniela227dbb2018-08-20 09:19:48 -0400147 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500148 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400149 GET_INST_PROC(CreateDevice);
150 GET_INST_PROC(EnumerateDeviceExtensionProperties);
151 GET_INST_PROC(CreateAndroidSurfaceKHR);
152 GET_INST_PROC(DestroySurfaceKHR);
153 GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR);
154 GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
155 GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
156 GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
157
158 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500159 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
160 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400161 // Just returning the first physical device instead of getting the whole array. Since there
162 // should only be one device on android.
163 gpuCount = 1;
164 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
165 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500166 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400167
Greg Daniel96259622018-10-01 14:42:56 -0400168 VkPhysicalDeviceProperties physDeviceProperties;
169 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500170 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel96259622018-10-01 14:42:56 -0400171
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,
198 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500199 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400200 std::unique_ptr<VkExtensionProperties[]> extensions(
201 new VkExtensionProperties[extensionCount]);
202 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
203 extensions.get());
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;
206 for (uint32_t i = 0; i < extensionCount; ++i) {
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800207 mDeviceExtensions.push_back(extensions[i].extensionName);
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 if (!strcmp(extensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
209 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 };
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800221 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
222 mInstanceExtensions.data(), mDeviceExtensions.size(), mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400223
Stan Iliev90276c82019-02-03 18:01:02 -0500224 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400225
Greg Daniela227dbb2018-08-20 09:19:48 -0400226 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
227 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
228 features.pNext = nullptr;
229
230 // Setup all extension feature structs we may want to use.
231 void** tailPNext = &features.pNext;
232
233 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
234 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
235 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*) malloc(
236 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
237 LOG_ALWAYS_FATAL_IF(!blend);
238 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
239 blend->pNext = nullptr;
240 *tailPNext = blend;
241 tailPNext = &blend->pNext;
242 }
243
Greg Daniel05036172018-11-28 17:08:04 -0500244 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
245 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*) malloc(
246 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
247 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
248 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
249 ycbcrFeature->pNext = nullptr;
250 *tailPNext = ycbcrFeature;
251 tailPNext = &ycbcrFeature->pNext;
252
Greg Daniela227dbb2018-08-20 09:19:48 -0400253 // query to get the physical device features
254 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400255 // this looks like it would slow things down,
256 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400257 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400258
259 float queuePriorities[1] = { 0.0 };
260
Stan Iliev7e733362019-02-28 13:16:36 -0500261 void* queueNextPtr = nullptr;
262
263 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
264
265 if (Properties::contextPriority != 0
266 && grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
267 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
268 queuePriorityCreateInfo.sType =
269 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
270 queuePriorityCreateInfo.pNext = nullptr;
271 switch (Properties::contextPriority) {
272 case EGL_CONTEXT_PRIORITY_LOW_IMG:
273 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
274 break;
275 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
276 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
277 break;
278 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
279 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
280 break;
281 default:
282 LOG_ALWAYS_FATAL("Unsupported context priority");
283 }
284 queueNextPtr = &queuePriorityCreateInfo;
285 }
286
Greg Daniel2ff202712018-06-14 11:50:10 -0400287 const VkDeviceQueueCreateInfo queueInfo[2] = {
288 {
289 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500290 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400291 0, // VkDeviceQueueCreateFlags
292 mGraphicsQueueIndex, // queueFamilyIndex
293 1, // queueCount
294 queuePriorities, // pQueuePriorities
295 },
296 {
297 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500298 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400299 0, // VkDeviceQueueCreateFlags
300 mPresentQueueIndex, // queueFamilyIndex
301 1, // queueCount
302 queuePriorities, // pQueuePriorities
303 }
304 };
305 uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1;
306
307 const VkDeviceCreateInfo deviceInfo = {
308 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
Greg Daniela227dbb2018-08-20 09:19:48 -0400309 &features, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400310 0, // VkDeviceCreateFlags
311 queueInfoCount, // queueCreateInfoCount
312 queueInfo, // pQueueCreateInfos
313 0, // layerCount
314 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800315 (uint32_t) mDeviceExtensions.size(), // extensionCount
316 mDeviceExtensions.data(), // ppEnabledExtensionNames
Greg Daniela227dbb2018-08-20 09:19:48 -0400317 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;
Greg Daniel2ff202712018-06-14 11:50:10 -0400369 SkDEBUGCODE(VkResult res =) mCreateCommandPool(mDevice, &commandPoolInfo, nullptr,
370 &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
375 if (!setupDummyCommandBuffer()) {
376 this->destroy();
Stan Iliev90276c82019-02-03 18:01:02 -0500377 // Pass through will crash on next line.
Greg Daniel26e0dca2018-09-18 10:33:19 -0400378 }
379 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
380
Greg Daniel2ff202712018-06-14 11:50:10 -0400381 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500382
Greg Danielcd558522016-11-17 13:31:40 -0500383 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
384 mSwapBehavior = SwapBehavior::BufferAge;
385 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500386}
387
Stan Iliev898123b2019-02-14 14:57:44 -0500388sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) {
Stan Iliev981afe72019-02-13 14:24:33 -0500389 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
390 if (device != VK_NULL_HANDLE) {
391 return vkGetDeviceProcAddr(device, proc_name);
392 }
393 return vkGetInstanceProcAddr(instance, proc_name);
394 };
395
396 GrVkBackendContext backendContext;
397 backendContext.fInstance = mInstance;
398 backendContext.fPhysicalDevice = mPhysicalDevice;
399 backendContext.fDevice = mDevice;
400 backendContext.fQueue = mGraphicsQueue;
401 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
402 backendContext.fMaxAPIVersion = mAPIVersion;
403 backendContext.fVkExtensions = &mExtensions;
404 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
405 backendContext.fGetProc = std::move(getProc);
406
407 return GrContext::MakeVulkan(backendContext, options);
408}
409
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800410VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
411 return VkFunctorInitParams{
412 .instance = mInstance,
413 .physical_device = mPhysicalDevice,
414 .device = mDevice,
415 .queue = mGraphicsQueue,
416 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500417 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800418 .enabled_instance_extension_names = mInstanceExtensions.data(),
419 .enabled_instance_extension_names_length =
420 static_cast<uint32_t>(mInstanceExtensions.size()),
421 .enabled_device_extension_names = mDeviceExtensions.data(),
422 .enabled_device_extension_names_length =
423 static_cast<uint32_t>(mDeviceExtensions.size()),
424 .device_features_2 = &mPhysicalDeviceFeatures2,
425 };
426}
427
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500428Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500429
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500430 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
431
432 if (bufferInfo == nullptr) {
433 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
434 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500435 }
436
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500437 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500438
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500439 if (bufferInfo->dequeue_fence != -1) {
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), errno);
443 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
444 } else {
445 VkSemaphoreCreateInfo semaphoreInfo;
446 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
447 semaphoreInfo.pNext = nullptr;
448 semaphoreInfo.flags = 0;
449 VkSemaphore semaphore;
450 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
451 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
452 err);
453
454 VkImportSemaphoreFdInfoKHR importInfo;
455 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
456 importInfo.pNext = nullptr;
457 importInfo.semaphore = semaphore;
458 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
459 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
460 importInfo.fd = fence_clone;
461
462 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
463 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
464
465 GrBackendSemaphore backendSemaphore;
466 backendSemaphore.initVulkan(semaphore);
467 bufferInfo->skSurface->wait(1, &backendSemaphore);
468 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500469 }
470
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500471 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
472 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500473}
474
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500475void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
476 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
477 ATRACE_NAME("Finishing GPU work");
478 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500479 }
480
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500481 VkExportSemaphoreCreateInfo exportInfo;
482 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
483 exportInfo.pNext = nullptr;
484 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500485
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500486 VkSemaphoreCreateInfo semaphoreInfo;
487 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
488 semaphoreInfo.pNext = &exportInfo;
489 semaphoreInfo.flags = 0;
490 VkSemaphore semaphore;
491 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
492 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500493
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500494 GrBackendSemaphore backendSemaphore;
495 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500496
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500497 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500498
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500499 int fenceFd = -1;
500 GrSemaphoresSubmitted submitted =
501 bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
502 SkSurface::kNone_FlushFlags, 1, &backendSemaphore);
503 if (submitted == GrSemaphoresSubmitted::kYes) {
504 VkSemaphoreGetFdInfoKHR getFdInfo;
505 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
506 getFdInfo.pNext = nullptr;
507 getFdInfo.semaphore = semaphore;
508 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500509
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500510 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
511 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
512 } else {
513 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
514 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500515 }
516
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500517 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500518
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500519 // Exporting a semaphore with copy transference via vkGetSemaphoreFdKHR, has the same effect of
520 // destroying the semaphore and creating a new one with the same handle, and the payloads
521 // ownership is move to the Fd we created. Thus the semaphore is in a state that we can delete
522 // it and we don't need to wait on the command buffer we submitted to finish.
523 mDestroySemaphore(mDevice, semaphore, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500524}
525
526void VulkanManager::destroySurface(VulkanSurface* surface) {
527 // Make sure all submit commands have finished before starting to destroy objects.
528 if (VK_NULL_HANDLE != mPresentQueue) {
529 mQueueWaitIdle(mPresentQueue);
530 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400531 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500532
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500533 delete surface;
534}
535
Stan Iliev987a80c02018-12-04 10:07:21 -0500536VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800537 sk_sp<SkColorSpace> surfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500538 SkColorType surfaceColorType,
539 GrContext* grContext) {
540 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500541 if (!window) {
542 return nullptr;
543 }
544
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500545 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
546 *this);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500547}
548
Greg Daniel26e0dca2018-09-18 10:33:19 -0400549bool VulkanManager::setupDummyCommandBuffer() {
550 if (mDummyCB != VK_NULL_HANDLE) {
551 return true;
552 }
553
554 VkCommandBufferAllocateInfo commandBuffersInfo;
555 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
556 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
557 commandBuffersInfo.pNext = nullptr;
558 commandBuffersInfo.commandPool = mCommandPool;
559 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
560 commandBuffersInfo.commandBufferCount = 1;
561
562 VkResult err = mAllocateCommandBuffers(mDevice, &commandBuffersInfo, &mDummyCB);
563 if (err != VK_SUCCESS) {
564 // It is probably unnecessary to set this back to VK_NULL_HANDLE, but we set it anyways to
565 // make sure the driver didn't set a value and then return a failure.
566 mDummyCB = VK_NULL_HANDLE;
567 return false;
568 }
569
570 VkCommandBufferBeginInfo beginInfo;
571 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
572 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
573 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
574
575 mBeginCommandBuffer(mDummyCB, &beginInfo);
576 mEndCommandBuffer(mDummyCB);
577 return true;
578}
579
Stan Iliev564ca3e2018-09-04 22:00:00 +0000580status_t VulkanManager::fenceWait(sp<Fence>& fence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400581 if (!hasVkContext()) {
582 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
583 return INVALID_OPERATION;
584 }
585
Stan Iliev7a081272018-10-26 17:54:18 -0400586 // Block GPU on the fence.
587 int fenceFd = fence->dup();
588 if (fenceFd == -1) {
589 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
590 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000591 }
Stan Iliev7a081272018-10-26 17:54:18 -0400592
593 VkSemaphoreCreateInfo semaphoreInfo;
594 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
595 semaphoreInfo.pNext = nullptr;
596 semaphoreInfo.flags = 0;
597 VkSemaphore semaphore;
598 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
599 if (VK_SUCCESS != err) {
600 ALOGE("Failed to create import semaphore, err: %d", err);
601 return UNKNOWN_ERROR;
602 }
603 VkImportSemaphoreFdInfoKHR importInfo;
604 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
605 importInfo.pNext = nullptr;
606 importInfo.semaphore = semaphore;
607 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
608 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
609 importInfo.fd = fenceFd;
610
611 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
612 if (VK_SUCCESS != err) {
613 ALOGE("Failed to import semaphore, err: %d", err);
614 return UNKNOWN_ERROR;
615 }
616
617 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
618
619 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
620
621 VkSubmitInfo submitInfo;
622 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
623 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
624 submitInfo.waitSemaphoreCount = 1;
625 // Wait to make sure aquire semaphore set above has signaled.
626 submitInfo.pWaitSemaphores = &semaphore;
627 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
628 submitInfo.commandBufferCount = 1;
629 submitInfo.pCommandBuffers = &mDummyCB;
630 submitInfo.signalSemaphoreCount = 0;
631
632 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
633
634 // On Android when we import a semaphore, it is imported using temporary permanence. That
635 // means as soon as we queue the semaphore for a wait it reverts to its previous permanent
636 // state before importing. This means it will now be in an idle state with no pending
637 // signal or wait operations, so it is safe to immediately delete it.
638 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000639 return OK;
640}
641
642status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400643 if (!hasVkContext()) {
644 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
645 return INVALID_OPERATION;
646 }
647
Greg Daniel26e0dca2018-09-18 10:33:19 -0400648 VkExportSemaphoreCreateInfo exportInfo;
649 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
650 exportInfo.pNext = nullptr;
651 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
652
653 VkSemaphoreCreateInfo semaphoreInfo;
654 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
655 semaphoreInfo.pNext = &exportInfo;
656 semaphoreInfo.flags = 0;
657 VkSemaphore semaphore;
658 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
659 if (VK_SUCCESS != err) {
660 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
661 return INVALID_OPERATION;
662 }
663
664 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
665
666 VkSubmitInfo submitInfo;
667 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
668 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
669 submitInfo.waitSemaphoreCount = 0;
670 submitInfo.pWaitSemaphores = nullptr;
671 submitInfo.pWaitDstStageMask = nullptr;
672 submitInfo.commandBufferCount = 1;
673 submitInfo.pCommandBuffers = &mDummyCB;
674 submitInfo.signalSemaphoreCount = 1;
675 submitInfo.pSignalSemaphores = &semaphore;
676
677 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
678
679 VkSemaphoreGetFdInfoKHR getFdInfo;
680 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
681 getFdInfo.pNext = nullptr;
682 getFdInfo.semaphore = semaphore;
683 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
684
685 int fenceFd = 0;
686
687 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
688 if (VK_SUCCESS != err) {
689 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
690 return INVALID_OPERATION;
691 }
692 nativeFence = new Fence(fenceFd);
693
694 // Exporting a semaphore with copy transference via vkGetSemahporeFdKHR, has the same effect of
695 // destroying the semaphore and creating a new one with the same handle, and the payloads
696 // ownership is move to the Fd we created. Thus the semahpore is in a state that we can delete
697 // it and we don't need to wait on the command buffer we submitted to finish.
698 mDestroySemaphore(mDevice, semaphore, nullptr);
699
Stan Iliev564ca3e2018-09-04 22:00:00 +0000700 return OK;
701}
702
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500703} /* namespace renderthread */
704} /* namespace uirenderer */
705} /* namespace android */