blob: d4c6eae37630eec51fffc12b38c7fc04376d159e [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;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080084 mInstanceExtensions.clear();
85 mDeviceExtensions.clear();
86 free_features_extensions_structs(mPhysicalDeviceFeatures2);
87 mPhysicalDeviceFeatures2 = {};
Greg Daniel2ff202712018-06-14 11:50:10 -040088}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050089
Stan Iliev90276c82019-02-03 18:01:02 -050090void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFeatures2& features) {
Greg Daniel2ff202712018-06-14 11:50:10 -040091 VkResult err;
92
93 constexpr VkApplicationInfo app_info = {
94 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
95 nullptr, // pNext
96 "android framework", // pApplicationName
97 0, // applicationVersion
98 "android framework", // pEngineName
99 0, // engineVerison
Greg Danieleaf310e2019-01-28 16:10:32 -0500100 mAPIVersion, // apiVersion
Greg Daniel2ff202712018-06-14 11:50:10 -0400101 };
102
Greg Daniel2ff202712018-06-14 11:50:10 -0400103 {
104 GET_PROC(EnumerateInstanceExtensionProperties);
105
106 uint32_t extensionCount = 0;
107 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500108 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400109 std::unique_ptr<VkExtensionProperties[]> extensions(
110 new VkExtensionProperties[extensionCount]);
111 err = mEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.get());
Stan Iliev90276c82019-02-03 18:01:02 -0500112 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400113 bool hasKHRSurfaceExtension = false;
114 bool hasKHRAndroidSurfaceExtension = false;
115 for (uint32_t i = 0; i < extensionCount; ++i) {
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800116 mInstanceExtensions.push_back(extensions[i].extensionName);
Greg Daniel2ff202712018-06-14 11:50:10 -0400117 if (!strcmp(extensions[i].extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) {
118 hasKHRSurfaceExtension = true;
119 }
120 if (!strcmp(extensions[i].extensionName,VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
121 hasKHRAndroidSurfaceExtension = true;
122 }
123 }
Stan Iliev90276c82019-02-03 18:01:02 -0500124 LOG_ALWAYS_FATAL_IF(!hasKHRSurfaceExtension || !hasKHRAndroidSurfaceExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400125 }
126
127 const VkInstanceCreateInfo instance_create = {
128 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
129 nullptr, // pNext
130 0, // flags
131 &app_info, // pApplicationInfo
132 0, // enabledLayerNameCount
133 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800134 (uint32_t) mInstanceExtensions.size(), // enabledExtensionNameCount
135 mInstanceExtensions.data(), // ppEnabledExtensionNames
Greg Daniel2ff202712018-06-14 11:50:10 -0400136 };
137
138 GET_PROC(CreateInstance);
139 err = mCreateInstance(&instance_create, nullptr, &mInstance);
Stan Iliev90276c82019-02-03 18:01:02 -0500140 LOG_ALWAYS_FATAL_IF(err < 0);
Greg Daniel2ff202712018-06-14 11:50:10 -0400141
142 GET_INST_PROC(DestroyInstance);
143 GET_INST_PROC(EnumeratePhysicalDevices);
Greg Daniel96259622018-10-01 14:42:56 -0400144 GET_INST_PROC(GetPhysicalDeviceProperties);
Greg Daniel2ff202712018-06-14 11:50:10 -0400145 GET_INST_PROC(GetPhysicalDeviceQueueFamilyProperties);
Greg Daniela227dbb2018-08-20 09:19:48 -0400146 GET_INST_PROC(GetPhysicalDeviceFeatures2);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500147 GET_INST_PROC(GetPhysicalDeviceImageFormatProperties2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400148 GET_INST_PROC(CreateDevice);
149 GET_INST_PROC(EnumerateDeviceExtensionProperties);
150 GET_INST_PROC(CreateAndroidSurfaceKHR);
151 GET_INST_PROC(DestroySurfaceKHR);
152 GET_INST_PROC(GetPhysicalDeviceSurfaceSupportKHR);
153 GET_INST_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
154 GET_INST_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
155 GET_INST_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
156
157 uint32_t gpuCount;
Stan Iliev90276c82019-02-03 18:01:02 -0500158 LOG_ALWAYS_FATAL_IF(mEnumeratePhysicalDevices(mInstance, &gpuCount, nullptr));
159 LOG_ALWAYS_FATAL_IF(!gpuCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400160 // Just returning the first physical device instead of getting the whole array. Since there
161 // should only be one device on android.
162 gpuCount = 1;
163 err = mEnumeratePhysicalDevices(mInstance, &gpuCount, &mPhysicalDevice);
164 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
Stan Iliev90276c82019-02-03 18:01:02 -0500165 LOG_ALWAYS_FATAL_IF(err && VK_INCOMPLETE != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400166
Greg Daniel96259622018-10-01 14:42:56 -0400167 VkPhysicalDeviceProperties physDeviceProperties;
168 mGetPhysicalDeviceProperties(mPhysicalDevice, &physDeviceProperties);
Stan Iliev90276c82019-02-03 18:01:02 -0500169 LOG_ALWAYS_FATAL_IF(physDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniel96259622018-10-01 14:42:56 -0400170
Greg Daniel2ff202712018-06-14 11:50:10 -0400171 // query to get the initial queue props size
172 uint32_t queueCount;
173 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500174 LOG_ALWAYS_FATAL_IF(!queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400175
176 // now get the actual queue props
177 std::unique_ptr<VkQueueFamilyProperties[]> queueProps(new VkQueueFamilyProperties[queueCount]);
178 mGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, queueProps.get());
179
180 // iterate to find the graphics queue
181 mGraphicsQueueIndex = queueCount;
182 for (uint32_t i = 0; i < queueCount; i++) {
183 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
184 mGraphicsQueueIndex = i;
185 break;
186 }
187 }
Stan Iliev90276c82019-02-03 18:01:02 -0500188 LOG_ALWAYS_FATAL_IF(mGraphicsQueueIndex == queueCount);
Greg Daniel2ff202712018-06-14 11:50:10 -0400189
190 // All physical devices and queue families on Android must be capable of
191 // presentation with any native window. So just use the first one.
192 mPresentQueueIndex = 0;
193
Greg Daniel2ff202712018-06-14 11:50:10 -0400194 {
195 uint32_t extensionCount = 0;
196 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
197 nullptr);
Stan Iliev90276c82019-02-03 18:01:02 -0500198 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400199 std::unique_ptr<VkExtensionProperties[]> extensions(
200 new VkExtensionProperties[extensionCount]);
201 err = mEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, &extensionCount,
202 extensions.get());
Stan Iliev90276c82019-02-03 18:01:02 -0500203 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err);
Greg Daniel2ff202712018-06-14 11:50:10 -0400204 bool hasKHRSwapchainExtension = false;
205 for (uint32_t i = 0; i < extensionCount; ++i) {
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800206 mDeviceExtensions.push_back(extensions[i].extensionName);
Greg Daniel2ff202712018-06-14 11:50:10 -0400207 if (!strcmp(extensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
208 hasKHRSwapchainExtension = true;
209 }
210 }
Stan Iliev90276c82019-02-03 18:01:02 -0500211 LOG_ALWAYS_FATAL_IF(!hasKHRSwapchainExtension);
Greg Daniel2ff202712018-06-14 11:50:10 -0400212 }
213
Greg Daniela227dbb2018-08-20 09:19:48 -0400214 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
215 if (device != VK_NULL_HANDLE) {
216 return vkGetDeviceProcAddr(device, proc_name);
217 }
218 return vkGetInstanceProcAddr(instance, proc_name);
219 };
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800220 grExtensions.init(getProc, mInstance, mPhysicalDevice, mInstanceExtensions.size(),
221 mInstanceExtensions.data(), mDeviceExtensions.size(), mDeviceExtensions.data());
Greg Daniela227dbb2018-08-20 09:19:48 -0400222
Stan Iliev90276c82019-02-03 18:01:02 -0500223 LOG_ALWAYS_FATAL_IF(!grExtensions.hasExtension(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, 1));
Greg Daniel26e0dca2018-09-18 10:33:19 -0400224
Greg Daniela227dbb2018-08-20 09:19:48 -0400225 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
226 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
227 features.pNext = nullptr;
228
229 // Setup all extension feature structs we may want to use.
230 void** tailPNext = &features.pNext;
231
232 if (grExtensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2)) {
233 VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT* blend;
234 blend = (VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*) malloc(
235 sizeof(VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT));
236 LOG_ALWAYS_FATAL_IF(!blend);
237 blend->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT;
238 blend->pNext = nullptr;
239 *tailPNext = blend;
240 tailPNext = &blend->pNext;
241 }
242
Greg Daniel05036172018-11-28 17:08:04 -0500243 VkPhysicalDeviceSamplerYcbcrConversionFeatures* ycbcrFeature;
244 ycbcrFeature = (VkPhysicalDeviceSamplerYcbcrConversionFeatures*) malloc(
245 sizeof(VkPhysicalDeviceSamplerYcbcrConversionFeatures));
246 LOG_ALWAYS_FATAL_IF(!ycbcrFeature);
247 ycbcrFeature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
248 ycbcrFeature->pNext = nullptr;
249 *tailPNext = ycbcrFeature;
250 tailPNext = &ycbcrFeature->pNext;
251
Greg Daniela227dbb2018-08-20 09:19:48 -0400252 // query to get the physical device features
253 mGetPhysicalDeviceFeatures2(mPhysicalDevice, &features);
Greg Daniel2ff202712018-06-14 11:50:10 -0400254 // this looks like it would slow things down,
255 // and we can't depend on it on all platforms
Greg Daniela227dbb2018-08-20 09:19:48 -0400256 features.features.robustBufferAccess = VK_FALSE;
Greg Daniel2ff202712018-06-14 11:50:10 -0400257
258 float queuePriorities[1] = { 0.0 };
259
Stan Iliev7e733362019-02-28 13:16:36 -0500260 void* queueNextPtr = nullptr;
261
262 VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
263
264 if (Properties::contextPriority != 0
265 && grExtensions.hasExtension(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME, 2)) {
266 memset(&queuePriorityCreateInfo, 0, sizeof(VkDeviceQueueGlobalPriorityCreateInfoEXT));
267 queuePriorityCreateInfo.sType =
268 VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
269 queuePriorityCreateInfo.pNext = nullptr;
270 switch (Properties::contextPriority) {
271 case EGL_CONTEXT_PRIORITY_LOW_IMG:
272 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT;
273 break;
274 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
275 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
276 break;
277 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
278 queuePriorityCreateInfo.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT;
279 break;
280 default:
281 LOG_ALWAYS_FATAL("Unsupported context priority");
282 }
283 queueNextPtr = &queuePriorityCreateInfo;
284 }
285
Greg Daniel2ff202712018-06-14 11:50:10 -0400286 const VkDeviceQueueCreateInfo queueInfo[2] = {
287 {
288 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500289 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400290 0, // VkDeviceQueueCreateFlags
291 mGraphicsQueueIndex, // queueFamilyIndex
292 1, // queueCount
293 queuePriorities, // pQueuePriorities
294 },
295 {
296 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
Stan Iliev7e733362019-02-28 13:16:36 -0500297 queueNextPtr, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400298 0, // VkDeviceQueueCreateFlags
299 mPresentQueueIndex, // queueFamilyIndex
300 1, // queueCount
301 queuePriorities, // pQueuePriorities
302 }
303 };
304 uint32_t queueInfoCount = (mPresentQueueIndex != mGraphicsQueueIndex) ? 2 : 1;
305
306 const VkDeviceCreateInfo deviceInfo = {
307 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
Greg Daniela227dbb2018-08-20 09:19:48 -0400308 &features, // pNext
Greg Daniel2ff202712018-06-14 11:50:10 -0400309 0, // VkDeviceCreateFlags
310 queueInfoCount, // queueCreateInfoCount
311 queueInfo, // pQueueCreateInfos
312 0, // layerCount
313 nullptr, // ppEnabledLayerNames
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800314 (uint32_t) mDeviceExtensions.size(), // extensionCount
315 mDeviceExtensions.data(), // ppEnabledExtensionNames
Greg Daniela227dbb2018-08-20 09:19:48 -0400316 nullptr, // ppEnabledFeatures
Greg Daniel2ff202712018-06-14 11:50:10 -0400317 };
318
Stan Iliev90276c82019-02-03 18:01:02 -0500319 LOG_ALWAYS_FATAL_IF(mCreateDevice(mPhysicalDevice, &deviceInfo, nullptr, &mDevice));
Greg Daniel2ff202712018-06-14 11:50:10 -0400320
321 GET_DEV_PROC(GetDeviceQueue);
322 GET_DEV_PROC(DeviceWaitIdle);
323 GET_DEV_PROC(DestroyDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500324 GET_DEV_PROC(CreateCommandPool);
325 GET_DEV_PROC(DestroyCommandPool);
326 GET_DEV_PROC(AllocateCommandBuffers);
327 GET_DEV_PROC(FreeCommandBuffers);
328 GET_DEV_PROC(ResetCommandBuffer);
329 GET_DEV_PROC(BeginCommandBuffer);
330 GET_DEV_PROC(EndCommandBuffer);
331 GET_DEV_PROC(CmdPipelineBarrier);
332 GET_DEV_PROC(GetDeviceQueue);
333 GET_DEV_PROC(QueueSubmit);
334 GET_DEV_PROC(QueueWaitIdle);
335 GET_DEV_PROC(DeviceWaitIdle);
336 GET_DEV_PROC(CreateSemaphore);
337 GET_DEV_PROC(DestroySemaphore);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400338 GET_DEV_PROC(ImportSemaphoreFdKHR);
339 GET_DEV_PROC(GetSemaphoreFdKHR);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500340 GET_DEV_PROC(CreateFence);
341 GET_DEV_PROC(DestroyFence);
342 GET_DEV_PROC(WaitForFences);
343 GET_DEV_PROC(ResetFences);
Greg Daniel2ff202712018-06-14 11:50:10 -0400344}
345
346void VulkanManager::initialize() {
347 if (mDevice != VK_NULL_HANDLE) {
348 return;
349 }
350
Greg Daniela227dbb2018-08-20 09:19:48 -0400351 GET_PROC(EnumerateInstanceVersion);
Greg Danieleaf310e2019-01-28 16:10:32 -0500352 uint32_t instanceVersion;
353 LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
354 LOG_ALWAYS_FATAL_IF(instanceVersion < VK_MAKE_VERSION(1, 1, 0));
Greg Daniela227dbb2018-08-20 09:19:48 -0400355
Stan Iliev981afe72019-02-13 14:24:33 -0500356 this->setupDevice(mExtensions, mPhysicalDeviceFeatures2);
Greg Daniel2ff202712018-06-14 11:50:10 -0400357
358 mGetDeviceQueue(mDevice, mGraphicsQueueIndex, 0, &mGraphicsQueue);
359
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500360 // create the command pool for the command buffers
361 if (VK_NULL_HANDLE == mCommandPool) {
362 VkCommandPoolCreateInfo commandPoolInfo;
363 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
364 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
365 // this needs to be on the render queue
Greg Daniel2ff202712018-06-14 11:50:10 -0400366 commandPoolInfo.queueFamilyIndex = mGraphicsQueueIndex;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500367 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
Greg Daniel2ff202712018-06-14 11:50:10 -0400368 SkDEBUGCODE(VkResult res =) mCreateCommandPool(mDevice, &commandPoolInfo, nullptr,
369 &mCommandPool);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500370 SkASSERT(VK_SUCCESS == res);
371 }
Greg Daniel26e0dca2018-09-18 10:33:19 -0400372 LOG_ALWAYS_FATAL_IF(mCommandPool == VK_NULL_HANDLE);
373
374 if (!setupDummyCommandBuffer()) {
375 this->destroy();
Stan Iliev90276c82019-02-03 18:01:02 -0500376 // Pass through will crash on next line.
Greg Daniel26e0dca2018-09-18 10:33:19 -0400377 }
378 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
379
Greg Daniel2ff202712018-06-14 11:50:10 -0400380 mGetDeviceQueue(mDevice, mPresentQueueIndex, 0, &mPresentQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500381
Greg Danielcd558522016-11-17 13:31:40 -0500382 if (Properties::enablePartialUpdates && Properties::useBufferAge) {
383 mSwapBehavior = SwapBehavior::BufferAge;
384 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500385}
386
Stan Iliev898123b2019-02-14 14:57:44 -0500387sk_sp<GrContext> VulkanManager::createContext(const GrContextOptions& options) {
Stan Iliev981afe72019-02-13 14:24:33 -0500388 auto getProc = [] (const char* proc_name, VkInstance instance, VkDevice device) {
389 if (device != VK_NULL_HANDLE) {
390 return vkGetDeviceProcAddr(device, proc_name);
391 }
392 return vkGetInstanceProcAddr(instance, proc_name);
393 };
394
395 GrVkBackendContext backendContext;
396 backendContext.fInstance = mInstance;
397 backendContext.fPhysicalDevice = mPhysicalDevice;
398 backendContext.fDevice = mDevice;
399 backendContext.fQueue = mGraphicsQueue;
400 backendContext.fGraphicsQueueIndex = mGraphicsQueueIndex;
401 backendContext.fMaxAPIVersion = mAPIVersion;
402 backendContext.fVkExtensions = &mExtensions;
403 backendContext.fDeviceFeatures2 = &mPhysicalDeviceFeatures2;
404 backendContext.fGetProc = std::move(getProc);
405
406 return GrContext::MakeVulkan(backendContext, options);
407}
408
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800409VkFunctorInitParams VulkanManager::getVkFunctorInitParams() const {
410 return VkFunctorInitParams{
411 .instance = mInstance,
412 .physical_device = mPhysicalDevice,
413 .device = mDevice,
414 .queue = mGraphicsQueue,
415 .graphics_queue_index = mGraphicsQueueIndex,
Greg Danieleaf310e2019-01-28 16:10:32 -0500416 .api_version = mAPIVersion,
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800417 .enabled_instance_extension_names = mInstanceExtensions.data(),
418 .enabled_instance_extension_names_length =
419 static_cast<uint32_t>(mInstanceExtensions.size()),
420 .enabled_device_extension_names = mDeviceExtensions.data(),
421 .enabled_device_extension_names_length =
422 static_cast<uint32_t>(mDeviceExtensions.size()),
423 .device_features_2 = &mPhysicalDeviceFeatures2,
424 };
425}
426
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500427Frame VulkanManager::dequeueNextBuffer(VulkanSurface* surface) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500428
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500429 VulkanSurface::NativeBufferInfo* bufferInfo = surface->dequeueNativeBuffer();
430
431 if (bufferInfo == nullptr) {
432 ALOGE("VulkanSurface::dequeueNativeBuffer called with an invalid surface!");
433 return Frame(-1, -1, 0);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500434 }
435
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500436 LOG_ALWAYS_FATAL_IF(!bufferInfo->dequeued);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500437
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500438 if (bufferInfo->dequeue_fence != -1) {
439 int fence_clone = dup(bufferInfo->dequeue_fence);
440 if (fence_clone == -1) {
441 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", strerror(errno), errno);
442 sync_wait(bufferInfo->dequeue_fence, -1 /* forever */);
443 } else {
444 VkSemaphoreCreateInfo semaphoreInfo;
445 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
446 semaphoreInfo.pNext = nullptr;
447 semaphoreInfo.flags = 0;
448 VkSemaphore semaphore;
449 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
450 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to create import semaphore, err: %d",
451 err);
452
453 VkImportSemaphoreFdInfoKHR importInfo;
454 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
455 importInfo.pNext = nullptr;
456 importInfo.semaphore = semaphore;
457 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
458 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
459 importInfo.fd = fence_clone;
460
461 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
462 LOG_ALWAYS_FATAL_IF(VK_SUCCESS != err, "Failed to import semaphore, err: %d", err);
463
464 GrBackendSemaphore backendSemaphore;
465 backendSemaphore.initVulkan(semaphore);
466 bufferInfo->skSurface->wait(1, &backendSemaphore);
467 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500468 }
469
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500470 int bufferAge = (mSwapBehavior == SwapBehavior::Discard) ? 0 : surface->getCurrentBuffersAge();
471 return Frame(surface->logicalWidth(), surface->logicalHeight(), bufferAge);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500472}
473
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500474void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect) {
475 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
476 ATRACE_NAME("Finishing GPU work");
477 mDeviceWaitIdle(mDevice);
Stan Iliev305e13a2018-11-13 11:14:48 -0500478 }
479
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500480 VkExportSemaphoreCreateInfo exportInfo;
481 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
482 exportInfo.pNext = nullptr;
483 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500484
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500485 VkSemaphoreCreateInfo semaphoreInfo;
486 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
487 semaphoreInfo.pNext = &exportInfo;
488 semaphoreInfo.flags = 0;
489 VkSemaphore semaphore;
490 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
491 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to create semaphore");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500492
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500493 GrBackendSemaphore backendSemaphore;
494 backendSemaphore.initVulkan(semaphore);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500495
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500496 VulkanSurface::NativeBufferInfo* bufferInfo = surface->getCurrentBufferInfo();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500497
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500498 int fenceFd = -1;
499 GrSemaphoresSubmitted submitted =
500 bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
501 SkSurface::kNone_FlushFlags, 1, &backendSemaphore);
502 if (submitted == GrSemaphoresSubmitted::kYes) {
503 VkSemaphoreGetFdInfoKHR getFdInfo;
504 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
505 getFdInfo.pNext = nullptr;
506 getFdInfo.semaphore = semaphore;
507 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500508
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500509 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
510 ALOGE_IF(VK_SUCCESS != err, "VulkanManager::swapBuffers(): Failed to get semaphore Fd");
511 } else {
512 ALOGE("VulkanManager::swapBuffers(): Semaphore submission failed");
513 mQueueWaitIdle(mGraphicsQueue);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500514 }
515
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500516 surface->presentCurrentBuffer(dirtyRect, fenceFd);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500517
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500518 // Exporting a semaphore with copy transference via vkGetSemaphoreFdKHR, has the same effect of
519 // destroying the semaphore and creating a new one with the same handle, and the payloads
520 // ownership is move to the Fd we created. Thus the semaphore is in a state that we can delete
521 // it and we don't need to wait on the command buffer we submitted to finish.
522 mDestroySemaphore(mDevice, semaphore, nullptr);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500523}
524
525void VulkanManager::destroySurface(VulkanSurface* surface) {
526 // Make sure all submit commands have finished before starting to destroy objects.
527 if (VK_NULL_HANDLE != mPresentQueue) {
528 mQueueWaitIdle(mPresentQueue);
529 }
Greg Daniel2ff202712018-06-14 11:50:10 -0400530 mDeviceWaitIdle(mDevice);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500531
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500532 delete surface;
533}
534
Stan Iliev987a80c2018-12-04 10:07:21 -0500535VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800536 sk_sp<SkColorSpace> surfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500537 SkColorType surfaceColorType,
538 GrContext* grContext) {
539 LOG_ALWAYS_FATAL_IF(!hasVkContext(), "Not initialized");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500540 if (!window) {
541 return nullptr;
542 }
543
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500544 return VulkanSurface::Create(window, colorMode, surfaceColorType, surfaceColorSpace, grContext,
545 *this);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500546}
547
Greg Daniel26e0dca2018-09-18 10:33:19 -0400548bool VulkanManager::setupDummyCommandBuffer() {
549 if (mDummyCB != VK_NULL_HANDLE) {
550 return true;
551 }
552
553 VkCommandBufferAllocateInfo commandBuffersInfo;
554 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
555 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
556 commandBuffersInfo.pNext = nullptr;
557 commandBuffersInfo.commandPool = mCommandPool;
558 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
559 commandBuffersInfo.commandBufferCount = 1;
560
561 VkResult err = mAllocateCommandBuffers(mDevice, &commandBuffersInfo, &mDummyCB);
562 if (err != VK_SUCCESS) {
563 // It is probably unnecessary to set this back to VK_NULL_HANDLE, but we set it anyways to
564 // make sure the driver didn't set a value and then return a failure.
565 mDummyCB = VK_NULL_HANDLE;
566 return false;
567 }
568
569 VkCommandBufferBeginInfo beginInfo;
570 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
571 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
572 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
573
574 mBeginCommandBuffer(mDummyCB, &beginInfo);
575 mEndCommandBuffer(mDummyCB);
576 return true;
577}
578
Stan Iliev564ca3e2018-09-04 22:00:00 +0000579status_t VulkanManager::fenceWait(sp<Fence>& fence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400580 if (!hasVkContext()) {
581 ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
582 return INVALID_OPERATION;
583 }
584
Stan Iliev7a081272018-10-26 17:54:18 -0400585 // Block GPU on the fence.
586 int fenceFd = fence->dup();
587 if (fenceFd == -1) {
588 ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
589 return -errno;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000590 }
Stan Iliev7a081272018-10-26 17:54:18 -0400591
592 VkSemaphoreCreateInfo semaphoreInfo;
593 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
594 semaphoreInfo.pNext = nullptr;
595 semaphoreInfo.flags = 0;
596 VkSemaphore semaphore;
597 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
598 if (VK_SUCCESS != err) {
599 ALOGE("Failed to create import semaphore, err: %d", err);
600 return UNKNOWN_ERROR;
601 }
602 VkImportSemaphoreFdInfoKHR importInfo;
603 importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
604 importInfo.pNext = nullptr;
605 importInfo.semaphore = semaphore;
606 importInfo.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
607 importInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
608 importInfo.fd = fenceFd;
609
610 err = mImportSemaphoreFdKHR(mDevice, &importInfo);
611 if (VK_SUCCESS != err) {
612 ALOGE("Failed to import semaphore, err: %d", err);
613 return UNKNOWN_ERROR;
614 }
615
616 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
617
618 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
619
620 VkSubmitInfo submitInfo;
621 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
622 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
623 submitInfo.waitSemaphoreCount = 1;
624 // Wait to make sure aquire semaphore set above has signaled.
625 submitInfo.pWaitSemaphores = &semaphore;
626 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
627 submitInfo.commandBufferCount = 1;
628 submitInfo.pCommandBuffers = &mDummyCB;
629 submitInfo.signalSemaphoreCount = 0;
630
631 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
632
633 // On Android when we import a semaphore, it is imported using temporary permanence. That
634 // means as soon as we queue the semaphore for a wait it reverts to its previous permanent
635 // state before importing. This means it will now be in an idle state with no pending
636 // signal or wait operations, so it is safe to immediately delete it.
637 mDestroySemaphore(mDevice, semaphore, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000638 return OK;
639}
640
641status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) {
Greg Daniel26e0dca2018-09-18 10:33:19 -0400642 if (!hasVkContext()) {
643 ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
644 return INVALID_OPERATION;
645 }
646
Greg Daniel26e0dca2018-09-18 10:33:19 -0400647 VkExportSemaphoreCreateInfo exportInfo;
648 exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO;
649 exportInfo.pNext = nullptr;
650 exportInfo.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
651
652 VkSemaphoreCreateInfo semaphoreInfo;
653 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
654 semaphoreInfo.pNext = &exportInfo;
655 semaphoreInfo.flags = 0;
656 VkSemaphore semaphore;
657 VkResult err = mCreateSemaphore(mDevice, &semaphoreInfo, nullptr, &semaphore);
658 if (VK_SUCCESS != err) {
659 ALOGE("VulkanManager::createReleaseFence: Failed to create semaphore");
660 return INVALID_OPERATION;
661 }
662
663 LOG_ALWAYS_FATAL_IF(mDummyCB == VK_NULL_HANDLE);
664
665 VkSubmitInfo submitInfo;
666 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
667 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
668 submitInfo.waitSemaphoreCount = 0;
669 submitInfo.pWaitSemaphores = nullptr;
670 submitInfo.pWaitDstStageMask = nullptr;
671 submitInfo.commandBufferCount = 1;
672 submitInfo.pCommandBuffers = &mDummyCB;
673 submitInfo.signalSemaphoreCount = 1;
674 submitInfo.pSignalSemaphores = &semaphore;
675
676 mQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
677
678 VkSemaphoreGetFdInfoKHR getFdInfo;
679 getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
680 getFdInfo.pNext = nullptr;
681 getFdInfo.semaphore = semaphore;
682 getFdInfo.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
683
684 int fenceFd = 0;
685
686 err = mGetSemaphoreFdKHR(mDevice, &getFdInfo, &fenceFd);
687 if (VK_SUCCESS != err) {
688 ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
689 return INVALID_OPERATION;
690 }
691 nativeFence = new Fence(fenceFd);
692
693 // Exporting a semaphore with copy transference via vkGetSemahporeFdKHR, has the same effect of
694 // destroying the semaphore and creating a new one with the same handle, and the payloads
695 // ownership is move to the Fd we created. Thus the semahpore is in a state that we can delete
696 // it and we don't need to wait on the command buffer we submitted to finish.
697 mDestroySemaphore(mDevice, semaphore, nullptr);
698
Stan Iliev564ca3e2018-09-04 22:00:00 +0000699 return OK;
700}
701
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500702} /* namespace renderthread */
703} /* namespace uirenderer */
704} /* namespace android */