1 /*
2 * Copyright © 2017, Google Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "tu_private.h"
25
26 #include <hardware/gralloc.h>
27
28 #if ANDROID_API_LEVEL >= 26
29 #include <hardware/gralloc1.h>
30 #endif
31
32 #include <hardware/hardware.h>
33 #include <hardware/hwvulkan.h>
34
35 #include <vulkan/vk_android_native_buffer.h>
36 #include <vulkan/vk_icd.h>
37
38 #include "drm-uapi/drm_fourcc.h"
39
40 #include "util/libsync.h"
41 #include "util/os_file.h"
42
43 static int
44 tu_hal_open(const struct hw_module_t *mod,
45 const char *id,
46 struct hw_device_t **dev);
47 static int
48 tu_hal_close(struct hw_device_t *dev);
49
50 static void UNUSED
static_asserts(void)51 static_asserts(void)
52 {
53 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC);
54 }
55
56 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
57 .common =
58 {
59 .tag = HARDWARE_MODULE_TAG,
60 .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
61 .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
62 .id = HWVULKAN_HARDWARE_MODULE_ID,
63 .name = "AMD Vulkan HAL",
64 .author = "Google",
65 .methods =
66 &(hw_module_methods_t){
67 .open = tu_hal_open,
68 },
69 },
70 };
71
72 /* If any bits in test_mask are set, then unset them and return true. */
73 static inline bool
unmask32(uint32_t * inout_mask,uint32_t test_mask)74 unmask32(uint32_t *inout_mask, uint32_t test_mask)
75 {
76 uint32_t orig_mask = *inout_mask;
77 *inout_mask &= ~test_mask;
78 return *inout_mask != orig_mask;
79 }
80
81 static int
tu_hal_open(const struct hw_module_t * mod,const char * id,struct hw_device_t ** dev)82 tu_hal_open(const struct hw_module_t *mod,
83 const char *id,
84 struct hw_device_t **dev)
85 {
86 assert(mod == &HAL_MODULE_INFO_SYM.common);
87 assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
88
89 hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
90 if (!hal_dev)
91 return -1;
92
93 *hal_dev = (hwvulkan_device_t){
94 .common =
95 {
96 .tag = HARDWARE_DEVICE_TAG,
97 .version = HWVULKAN_DEVICE_API_VERSION_0_1,
98 .module = &HAL_MODULE_INFO_SYM.common,
99 .close = tu_hal_close,
100 },
101 .EnumerateInstanceExtensionProperties =
102 tu_EnumerateInstanceExtensionProperties,
103 .CreateInstance = tu_CreateInstance,
104 .GetInstanceProcAddr = tu_GetInstanceProcAddr,
105 };
106
107 *dev = &hal_dev->common;
108 return 0;
109 }
110
111 static int
tu_hal_close(struct hw_device_t * dev)112 tu_hal_close(struct hw_device_t *dev)
113 {
114 /* hwvulkan.h claims that hw_device_t::close() is never called. */
115 return -1;
116 }
117
118 /* get dma-buf and modifier from gralloc info */
119 VkResult
tu_gralloc_info(struct tu_device * device,const VkNativeBufferANDROID * gralloc_info,int * dma_buf,uint64_t * modifier)120 tu_gralloc_info(struct tu_device *device,
121 const VkNativeBufferANDROID *gralloc_info,
122 int *dma_buf,
123 uint64_t *modifier)
124
125 {
126 const uint32_t *handle_fds = (uint32_t *)gralloc_info->handle->data;
127 const uint32_t *handle_data = &handle_fds[gralloc_info->handle->numFds];
128 bool ubwc = false;
129
130 if (gralloc_info->handle->numFds == 1) {
131 /* gbm_gralloc. TODO: modifiers support */
132 *dma_buf = handle_fds[0];
133 } else if (gralloc_info->handle->numFds == 2) {
134 /* Qualcomm gralloc, find it at:
135 *
136 * https://android.googlesource.com/platform/hardware/qcom/display/.
137 *
138 * The gralloc_info->handle is a pointer to a struct private_handle_t
139 * from your platform's gralloc. On msm8996 (a5xx) and newer grallocs
140 * that's libgralloc1/gr_priv_handle.h, while previously it was
141 * libgralloc/gralloc_priv.h.
142 */
143
144 if (gralloc_info->handle->numInts < 2) {
145 return vk_errorf(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE,
146 "VkNativeBufferANDROID::handle::numInts is %d, "
147 "expected at least 2 for qcom gralloc",
148 gralloc_info->handle->numFds);
149 }
150
151 uint32_t gmsm = ('g' << 24) | ('m' << 16) | ('s' << 8) | 'm';
152 if (handle_data[0] != gmsm) {
153 return vk_errorf(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE,
154 "private_handle_t::magic is %x, expected %x",
155 handle_data[0], gmsm);
156 }
157
158 /* This UBWC flag was introduced in a5xx. */
159 ubwc = handle_data[1] & 0x08000000;
160
161 /* QCOM gralloc has two fds passed in: the actual GPU buffer, and a buffer
162 * of CPU-side metadata. I haven't found any need for the metadata buffer
163 * yet. See qdMetaData.h for what's in the metadata fd.
164 */
165 *dma_buf = handle_fds[0];
166 } else {
167 return vk_errorf(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE,
168 "VkNativeBufferANDROID::handle::numFds is %d, "
169 "expected 1 (gbm_gralloc) or 2 (qcom gralloc)",
170 gralloc_info->handle->numFds);
171 }
172
173 *modifier = ubwc ? DRM_FORMAT_MOD_QCOM_COMPRESSED : DRM_FORMAT_MOD_LINEAR;
174 return VK_SUCCESS;
175
176
177 }
178
179 /**
180 * Creates the VkImage using the gralloc handle in *gralloc_info.
181 *
182 * We support two different grallocs here, gbm_gralloc, and the qcom gralloc
183 * used on Android phones.
184 */
185 VkResult
tu_import_memory_from_gralloc_handle(VkDevice device_h,int dma_buf,const VkAllocationCallbacks * alloc,VkImage image_h)186 tu_import_memory_from_gralloc_handle(VkDevice device_h,
187 int dma_buf,
188 const VkAllocationCallbacks *alloc,
189 VkImage image_h)
190
191 {
192 struct tu_image *image = NULL;
193 VkResult result;
194
195 image = tu_image_from_handle(image_h);
196
197 VkDeviceMemory memory_h;
198
199 const VkMemoryDedicatedAllocateInfo ded_alloc = {
200 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
201 .pNext = NULL,
202 .buffer = VK_NULL_HANDLE,
203 .image = image_h
204 };
205
206 const VkImportMemoryFdInfoKHR import_info = {
207 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
208 .pNext = &ded_alloc,
209 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
210 .fd = os_dupfd_cloexec(dma_buf),
211 };
212
213 result =
214 tu_AllocateMemory(device_h,
215 &(VkMemoryAllocateInfo) {
216 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
217 .pNext = &import_info,
218 .allocationSize = image->total_size,
219 .memoryTypeIndex = 0,
220 },
221 alloc, &memory_h);
222 if (result != VK_SUCCESS)
223 goto fail_create_image;
224
225 tu_BindImageMemory(device_h, image_h, memory_h, 0);
226
227 image->owned_memory = memory_h;
228
229 return VK_SUCCESS;
230
231 fail_create_image:
232 tu_DestroyImage(device_h, image_h, alloc);
233
234 return result;
235 }
236
237 static VkResult
format_supported_with_usage(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage)238 format_supported_with_usage(VkDevice device_h, VkFormat format,
239 VkImageUsageFlags imageUsage)
240 {
241 TU_FROM_HANDLE(tu_device, device, device_h);
242 struct tu_physical_device *phys_dev = device->physical_device;
243 VkPhysicalDevice phys_dev_h = tu_physical_device_to_handle(phys_dev);
244 VkResult result;
245
246 /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags
247 * returned to applications via
248 * VkSurfaceCapabilitiesKHR::supportedUsageFlags.
249 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
250 *
251 * TODO(jessehall): I think these are right, but haven't thought hard
252 * about it. Do we need to query the driver for support of any of
253 * these?
254 *
255 * Any disagreement between this function and the hardcoded
256 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
257 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
258 */
259
260 const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
261 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
262 .format = format,
263 .type = VK_IMAGE_TYPE_2D,
264 .tiling = VK_IMAGE_TILING_OPTIMAL,
265 .usage = imageUsage,
266 };
267
268 VkImageFormatProperties2 image_format_props = {
269 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
270 };
271
272 /* Check that requested format and usage are supported. */
273 result = tu_GetPhysicalDeviceImageFormatProperties2(
274 phys_dev_h, &image_format_info, &image_format_props);
275 if (result != VK_SUCCESS) {
276 return vk_errorf(device->instance, result,
277 "tu_GetPhysicalDeviceImageFormatProperties2 failed "
278 "inside %s",
279 __func__);
280 }
281
282 return VK_SUCCESS;
283 }
284
285 static VkResult
setup_gralloc0_usage(struct tu_device * device,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)286 setup_gralloc0_usage(struct tu_device *device, VkFormat format,
287 VkImageUsageFlags imageUsage, int *grallocUsage)
288 {
289 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
290 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
291 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
292
293 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
294 VK_IMAGE_USAGE_SAMPLED_BIT |
295 VK_IMAGE_USAGE_STORAGE_BIT |
296 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
297 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
298
299 /* All VkImageUsageFlags not explicitly checked here are unsupported for
300 * gralloc swapchains.
301 */
302 if (imageUsage != 0) {
303 return vk_errorf(device->instance, VK_ERROR_FORMAT_NOT_SUPPORTED,
304 "unsupported VkImageUsageFlags(0x%x) for gralloc "
305 "swapchain",
306 imageUsage);
307 }
308
309 /*
310 * FINISHME: Advertise all display-supported formats. Mostly
311 * DRM_FORMAT_ARGB2101010 and DRM_FORMAT_ABGR2101010, but need to check
312 * what we need for 30-bit colors.
313 */
314 if (format == VK_FORMAT_B8G8R8A8_UNORM ||
315 format == VK_FORMAT_B5G6R5_UNORM_PACK16) {
316 *grallocUsage |= GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_COMPOSER |
317 GRALLOC_USAGE_EXTERNAL_DISP;
318 }
319
320 if (*grallocUsage == 0)
321 return VK_ERROR_FORMAT_NOT_SUPPORTED;
322
323 return VK_SUCCESS;
324 }
325
326 VkResult
tu_GetSwapchainGrallocUsageANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)327 tu_GetSwapchainGrallocUsageANDROID(VkDevice device_h,
328 VkFormat format,
329 VkImageUsageFlags imageUsage,
330 int *grallocUsage)
331 {
332 TU_FROM_HANDLE(tu_device, device, device_h);
333 VkResult result;
334
335 result = format_supported_with_usage(device_h, format, imageUsage);
336 if (result != VK_SUCCESS)
337 return result;
338
339 *grallocUsage = 0;
340 return setup_gralloc0_usage(device, format, imageUsage, grallocUsage);
341 }
342
343 #if ANDROID_API_LEVEL >= 26
344 VkResult
tu_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,uint64_t * grallocConsumerUsage,uint64_t * grallocProducerUsage)345 tu_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h,
346 VkFormat format,
347 VkImageUsageFlags imageUsage,
348 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
349 uint64_t *grallocConsumerUsage,
350 uint64_t *grallocProducerUsage)
351 {
352 TU_FROM_HANDLE(tu_device, device, device_h);
353 VkResult result;
354
355 *grallocConsumerUsage = 0;
356 *grallocProducerUsage = 0;
357 mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
358
359 result = format_supported_with_usage(device_h, format, imageUsage);
360 if (result != VK_SUCCESS)
361 return result;
362
363 int32_t grallocUsage = 0;
364 result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage);
365 if (result != VK_SUCCESS)
366 return result;
367
368 /* Setup gralloc1 usage flags from gralloc0 flags. */
369
370 if (grallocUsage & GRALLOC_USAGE_HW_RENDER) {
371 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
372 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET;
373 }
374
375 if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) {
376 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
377 }
378
379 if (grallocUsage & (GRALLOC_USAGE_HW_FB |
380 GRALLOC_USAGE_HW_COMPOSER |
381 GRALLOC_USAGE_EXTERNAL_DISP)) {
382 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
383 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER;
384 }
385
386 return VK_SUCCESS;
387 }
388 #endif
389
390 VkResult
tu_AcquireImageANDROID(VkDevice device,VkImage image_h,int nativeFenceFd,VkSemaphore semaphore,VkFence fence)391 tu_AcquireImageANDROID(VkDevice device,
392 VkImage image_h,
393 int nativeFenceFd,
394 VkSemaphore semaphore,
395 VkFence fence)
396 {
397 VkResult semaphore_result = VK_SUCCESS, fence_result = VK_SUCCESS;
398
399 if (semaphore != VK_NULL_HANDLE) {
400 int semaphore_fd =
401 nativeFenceFd >= 0 ? os_dupfd_cloexec(nativeFenceFd) : nativeFenceFd;
402 semaphore_result = tu_ImportSemaphoreFdKHR(
403 device, &(VkImportSemaphoreFdInfoKHR) {
404 .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
405 .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT,
406 .fd = semaphore_fd,
407 .semaphore = semaphore,
408 });
409 }
410
411 if (fence != VK_NULL_HANDLE) {
412 int fence_fd = nativeFenceFd >= 0 ? os_dupfd_cloexec(nativeFenceFd) : nativeFenceFd;
413 fence_result = tu_ImportFenceFdKHR(
414 device, &(VkImportFenceFdInfoKHR) {
415 .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
416 .flags = VK_FENCE_IMPORT_TEMPORARY_BIT,
417 .fd = fence_fd,
418 .fence = fence,
419 });
420 }
421
422 close(nativeFenceFd);
423
424 if (semaphore_result != VK_SUCCESS)
425 return semaphore_result;
426 return fence_result;
427 }
428