anv: Add an anv_physical_device field to anv_device
[mesa.git] / src / intel / vulkan / anv_android.c
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <hardware/gralloc.h>
25
26 #if ANDROID_API_LEVEL >= 26
27 #include <hardware/gralloc1.h>
28 #include <grallocusage/GrallocUsageConversion.h>
29 #endif
30
31 #include <hardware/hardware.h>
32 #include <hardware/hwvulkan.h>
33 #include <vulkan/vk_android_native_buffer.h>
34 #include <vulkan/vk_icd.h>
35 #include <sync/sync.h>
36
37 #include "anv_private.h"
38 #include "vk_format_info.h"
39 #include "vk_util.h"
40
41 static int anv_hal_open(const struct hw_module_t* mod, const char* id, struct hw_device_t** dev);
42 static int anv_hal_close(struct hw_device_t *dev);
43
44 static void UNUSED
45 static_asserts(void)
46 {
47 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC);
48 }
49
50 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
51 .common = {
52 .tag = HARDWARE_MODULE_TAG,
53 .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
54 .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
55 .id = HWVULKAN_HARDWARE_MODULE_ID,
56 .name = "Intel Vulkan HAL",
57 .author = "Intel",
58 .methods = &(hw_module_methods_t) {
59 .open = anv_hal_open,
60 },
61 },
62 };
63
64 /* If any bits in test_mask are set, then unset them and return true. */
65 static inline bool
66 unmask32(uint32_t *inout_mask, uint32_t test_mask)
67 {
68 uint32_t orig_mask = *inout_mask;
69 *inout_mask &= ~test_mask;
70 return *inout_mask != orig_mask;
71 }
72
73 static int
74 anv_hal_open(const struct hw_module_t* mod, const char* id,
75 struct hw_device_t** dev)
76 {
77 assert(mod == &HAL_MODULE_INFO_SYM.common);
78 assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
79
80 hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
81 if (!hal_dev)
82 return -1;
83
84 *hal_dev = (hwvulkan_device_t) {
85 .common = {
86 .tag = HARDWARE_DEVICE_TAG,
87 .version = HWVULKAN_DEVICE_API_VERSION_0_1,
88 .module = &HAL_MODULE_INFO_SYM.common,
89 .close = anv_hal_close,
90 },
91 .EnumerateInstanceExtensionProperties = anv_EnumerateInstanceExtensionProperties,
92 .CreateInstance = anv_CreateInstance,
93 .GetInstanceProcAddr = anv_GetInstanceProcAddr,
94 };
95
96 *dev = &hal_dev->common;
97 return 0;
98 }
99
100 static int
101 anv_hal_close(struct hw_device_t *dev)
102 {
103 /* hwvulkan.h claims that hw_device_t::close() is never called. */
104 return -1;
105 }
106
107 #if ANDROID_API_LEVEL >= 26
108 static VkResult
109 get_ahw_buffer_format_properties(
110 VkDevice device_h,
111 const struct AHardwareBuffer *buffer,
112 VkAndroidHardwareBufferFormatPropertiesANDROID *pProperties)
113 {
114 ANV_FROM_HANDLE(anv_device, device, device_h);
115
116 /* Get a description of buffer contents . */
117 AHardwareBuffer_Desc desc;
118 AHardwareBuffer_describe(buffer, &desc);
119
120 /* Verify description. */
121 uint64_t gpu_usage =
122 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
123 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
124 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
125
126 /* "Buffer must be a valid Android hardware buffer object with at least
127 * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
128 */
129 if (!(desc.usage & (gpu_usage)))
130 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
131
132 /* Fill properties fields based on description. */
133 VkAndroidHardwareBufferFormatPropertiesANDROID *p = pProperties;
134
135 p->format = vk_format_from_android(desc.format, desc.usage);
136
137 const struct anv_format *anv_format = anv_get_format(p->format);
138 p->externalFormat = (uint64_t) (uintptr_t) anv_format;
139
140 /* Default to OPTIMAL tiling but set to linear in case
141 * of AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER usage.
142 */
143 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
144
145 if (desc.usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER)
146 tiling = VK_IMAGE_TILING_LINEAR;
147
148 p->formatFeatures =
149 anv_get_image_format_features(&device->info, p->format, anv_format,
150 tiling);
151
152 /* "Images can be created with an external format even if the Android hardware
153 * buffer has a format which has an equivalent Vulkan format to enable
154 * consistent handling of images from sources that might use either category
155 * of format. However, all images created with an external format are subject
156 * to the valid usage requirements associated with external formats, even if
157 * the Android hardware buffer’s format has a Vulkan equivalent."
158 *
159 * "The formatFeatures member *must* include
160 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
161 * VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
162 * VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT"
163 */
164 p->formatFeatures |=
165 VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;
166
167 /* "Implementations may not always be able to determine the color model,
168 * numerical range, or chroma offsets of the image contents, so the values
169 * in VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
170 * Applications should treat these values as sensible defaults to use in
171 * the absence of more reliable information obtained through some other
172 * means."
173 */
174 p->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
175 p->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
176 p->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
177 p->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;
178
179 p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
180 p->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
181
182 p->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
183 p->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
184
185 return VK_SUCCESS;
186 }
187
188 VkResult
189 anv_GetAndroidHardwareBufferPropertiesANDROID(
190 VkDevice device_h,
191 const struct AHardwareBuffer *buffer,
192 VkAndroidHardwareBufferPropertiesANDROID *pProperties)
193 {
194 ANV_FROM_HANDLE(anv_device, dev, device_h);
195
196 VkAndroidHardwareBufferFormatPropertiesANDROID *format_prop =
197 vk_find_struct(pProperties->pNext,
198 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
199
200 /* Fill format properties of an Android hardware buffer. */
201 if (format_prop)
202 get_ahw_buffer_format_properties(device_h, buffer, format_prop);
203
204 /* NOTE - We support buffers with only one handle but do not error on
205 * multiple handle case. Reason is that we want to support YUV formats
206 * where we have many logical planes but they all point to the same
207 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
208 */
209 const native_handle_t *handle =
210 AHardwareBuffer_getNativeHandle(buffer);
211 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
212 if (dma_buf < 0)
213 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
214
215 /* All memory types. */
216 uint32_t memory_types = (1ull << dev->physical->memory.type_count) - 1;
217
218 pProperties->allocationSize = lseek(dma_buf, 0, SEEK_END);
219 pProperties->memoryTypeBits = memory_types;
220
221 return VK_SUCCESS;
222 }
223
224 VkResult
225 anv_GetMemoryAndroidHardwareBufferANDROID(
226 VkDevice device_h,
227 const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
228 struct AHardwareBuffer **pBuffer)
229 {
230 ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
231
232 /* Some quotes from Vulkan spec:
233 *
234 * "If the device memory was created by importing an Android hardware
235 * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
236 * Android hardware buffer object."
237 *
238 * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
239 * have been included in VkExportMemoryAllocateInfo::handleTypes when
240 * memory was created."
241 */
242 if (mem->ahw) {
243 *pBuffer = mem->ahw;
244 /* Increase refcount. */
245 AHardwareBuffer_acquire(mem->ahw);
246 return VK_SUCCESS;
247 }
248
249 return VK_ERROR_OUT_OF_HOST_MEMORY;
250 }
251
252 #endif
253
254 /* Construct ahw usage mask from image usage bits, see
255 * 'AHardwareBuffer Usage Equivalence' in Vulkan spec.
256 */
257 uint64_t
258 anv_ahw_usage_from_vk_usage(const VkImageCreateFlags vk_create,
259 const VkImageUsageFlags vk_usage)
260 {
261 uint64_t ahw_usage = 0;
262 #if ANDROID_API_LEVEL >= 26
263 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
264 ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
265
266 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
267 ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
268
269 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
270 ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
271
272 if (vk_create & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
273 ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP;
274
275 if (vk_create & VK_IMAGE_CREATE_PROTECTED_BIT)
276 ahw_usage |= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;
277
278 /* No usage bits set - set at least one GPU usage. */
279 if (ahw_usage == 0)
280 ahw_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
281 #endif
282 return ahw_usage;
283 }
284
285 /*
286 * Called from anv_AllocateMemory when import AHardwareBuffer.
287 */
288 VkResult
289 anv_import_ahw_memory(VkDevice device_h,
290 struct anv_device_memory *mem,
291 const VkImportAndroidHardwareBufferInfoANDROID *info)
292 {
293 #if ANDROID_API_LEVEL >= 26
294 ANV_FROM_HANDLE(anv_device, device, device_h);
295
296 /* Import from AHardwareBuffer to anv_device_memory. */
297 const native_handle_t *handle =
298 AHardwareBuffer_getNativeHandle(info->buffer);
299
300 /* NOTE - We support buffers with only one handle but do not error on
301 * multiple handle case. Reason is that we want to support YUV formats
302 * where we have many logical planes but they all point to the same
303 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
304 */
305 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
306 if (dma_buf < 0)
307 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
308
309 VkResult result = anv_device_import_bo(device, dma_buf, 0,
310 0 /* client_address */,
311 &mem->bo);
312 assert(VK_SUCCESS);
313
314 /* "If the vkAllocateMemory command succeeds, the implementation must
315 * acquire a reference to the imported hardware buffer, which it must
316 * release when the device memory object is freed. If the command fails,
317 * the implementation must not retain a reference."
318 */
319 AHardwareBuffer_acquire(info->buffer);
320 mem->ahw = info->buffer;
321
322 return VK_SUCCESS;
323 #else
324 return VK_ERROR_EXTENSION_NOT_PRESENT;
325 #endif
326 }
327
328 VkResult
329 anv_create_ahw_memory(VkDevice device_h,
330 struct anv_device_memory *mem,
331 const VkMemoryAllocateInfo *pAllocateInfo)
332 {
333 #if ANDROID_API_LEVEL >= 26
334 ANV_FROM_HANDLE(anv_device, dev, device_h);
335
336 const VkMemoryDedicatedAllocateInfo *dedicated_info =
337 vk_find_struct_const(pAllocateInfo->pNext,
338 MEMORY_DEDICATED_ALLOCATE_INFO);
339
340 uint32_t w = 0;
341 uint32_t h = 1;
342 uint32_t layers = 1;
343 uint32_t format = 0;
344 uint64_t usage = 0;
345
346 /* If caller passed dedicated information. */
347 if (dedicated_info && dedicated_info->image) {
348 ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
349 w = image->extent.width;
350 h = image->extent.height;
351 layers = image->array_size;
352 format = android_format_from_vk(image->vk_format);
353 usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
354 } else if (dedicated_info && dedicated_info->buffer) {
355 ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
356 w = buffer->size;
357 format = AHARDWAREBUFFER_FORMAT_BLOB;
358 usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
359 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
360 } else {
361 w = pAllocateInfo->allocationSize;
362 format = AHARDWAREBUFFER_FORMAT_BLOB;
363 usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
364 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
365 }
366
367 struct AHardwareBuffer *ahw = NULL;
368 struct AHardwareBuffer_Desc desc = {
369 .width = w,
370 .height = h,
371 .layers = layers,
372 .format = format,
373 .usage = usage,
374 };
375
376 if (AHardwareBuffer_allocate(&desc, &ahw) != 0)
377 return VK_ERROR_OUT_OF_HOST_MEMORY;
378
379 mem->ahw = ahw;
380 return VK_SUCCESS;
381 #else
382 return VK_ERROR_EXTENSION_NOT_PRESENT;
383 #endif
384
385 }
386
387 VkResult
388 anv_image_from_external(
389 VkDevice device_h,
390 const VkImageCreateInfo *base_info,
391 const VkExternalMemoryImageCreateInfo *create_info,
392 const VkAllocationCallbacks *alloc,
393 VkImage *out_image_h)
394 {
395 #if ANDROID_API_LEVEL >= 26
396 ANV_FROM_HANDLE(anv_device, device, device_h);
397
398 const VkExternalFormatANDROID *ext_info =
399 vk_find_struct_const(base_info->pNext, EXTERNAL_FORMAT_ANDROID);
400
401 if (ext_info && ext_info->externalFormat != 0) {
402 assert(base_info->format == VK_FORMAT_UNDEFINED);
403 assert(base_info->imageType == VK_IMAGE_TYPE_2D);
404 assert(base_info->usage == VK_IMAGE_USAGE_SAMPLED_BIT);
405 assert(base_info->tiling == VK_IMAGE_TILING_OPTIMAL);
406 }
407
408 struct anv_image_create_info anv_info = {
409 .vk_info = base_info,
410 .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
411 .external_format = true,
412 };
413
414 VkImage image_h;
415 VkResult result = anv_image_create(device_h, &anv_info, alloc, &image_h);
416 if (result != VK_SUCCESS)
417 return result;
418
419 *out_image_h = image_h;
420
421 return VK_SUCCESS;
422 #else
423 return VK_ERROR_EXTENSION_NOT_PRESENT;
424 #endif
425 }
426
427
428 VkResult
429 anv_image_from_gralloc(VkDevice device_h,
430 const VkImageCreateInfo *base_info,
431 const VkNativeBufferANDROID *gralloc_info,
432 const VkAllocationCallbacks *alloc,
433 VkImage *out_image_h)
434
435 {
436 ANV_FROM_HANDLE(anv_device, device, device_h);
437 VkImage image_h = VK_NULL_HANDLE;
438 struct anv_image *image = NULL;
439 struct anv_bo *bo = NULL;
440 VkResult result;
441
442 struct anv_image_create_info anv_info = {
443 .vk_info = base_info,
444 .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
445 };
446
447 if (gralloc_info->handle->numFds != 1) {
448 return vk_errorf(device->instance, device,
449 VK_ERROR_INVALID_EXTERNAL_HANDLE,
450 "VkNativeBufferANDROID::handle::numFds is %d, "
451 "expected 1", gralloc_info->handle->numFds);
452 }
453
454 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
455 * must exceed that of the gralloc handle, and we do not own the gralloc
456 * handle.
457 */
458 int dma_buf = gralloc_info->handle->data[0];
459
460 /* We need to set the WRITE flag on window system buffers so that GEM will
461 * know we're writing to them and synchronize uses on other rings (for
462 * example, if the display server uses the blitter ring).
463 *
464 * If this function fails and if the imported bo was resident in the cache,
465 * we should avoid updating the bo's flags. Therefore, we defer updating
466 * the flags until success is certain.
467 *
468 */
469 result = anv_device_import_bo(device, dma_buf,
470 ANV_BO_ALLOC_IMPLICIT_SYNC |
471 ANV_BO_ALLOC_IMPLICIT_WRITE,
472 0 /* client_address */,
473 &bo);
474 if (result != VK_SUCCESS) {
475 return vk_errorf(device->instance, device, result,
476 "failed to import dma-buf from VkNativeBufferANDROID");
477 }
478
479 int i915_tiling = anv_gem_get_tiling(device, bo->gem_handle);
480 switch (i915_tiling) {
481 case I915_TILING_NONE:
482 anv_info.isl_tiling_flags = ISL_TILING_LINEAR_BIT;
483 break;
484 case I915_TILING_X:
485 anv_info.isl_tiling_flags = ISL_TILING_X_BIT;
486 break;
487 case I915_TILING_Y:
488 anv_info.isl_tiling_flags = ISL_TILING_Y0_BIT;
489 break;
490 case -1:
491 result = vk_errorf(device->instance, device,
492 VK_ERROR_INVALID_EXTERNAL_HANDLE,
493 "DRM_IOCTL_I915_GEM_GET_TILING failed for "
494 "VkNativeBufferANDROID");
495 goto fail_tiling;
496 default:
497 result = vk_errorf(device->instance, device,
498 VK_ERROR_INVALID_EXTERNAL_HANDLE,
499 "DRM_IOCTL_I915_GEM_GET_TILING returned unknown "
500 "tiling %d for VkNativeBufferANDROID", i915_tiling);
501 goto fail_tiling;
502 }
503
504 enum isl_format format = anv_get_isl_format(&device->info,
505 base_info->format,
506 VK_IMAGE_ASPECT_COLOR_BIT,
507 base_info->tiling);
508 assert(format != ISL_FORMAT_UNSUPPORTED);
509
510 anv_info.stride = gralloc_info->stride *
511 (isl_format_get_layout(format)->bpb / 8);
512
513 result = anv_image_create(device_h, &anv_info, alloc, &image_h);
514 image = anv_image_from_handle(image_h);
515 if (result != VK_SUCCESS)
516 goto fail_create;
517
518 if (bo->size < image->size) {
519 result = vk_errorf(device->instance, device,
520 VK_ERROR_INVALID_EXTERNAL_HANDLE,
521 "dma-buf from VkNativeBufferANDROID is too small for "
522 "VkImage: %"PRIu64"B < %"PRIu64"B",
523 bo->size, image->size);
524 goto fail_size;
525 }
526
527 assert(image->n_planes == 1);
528 assert(image->planes[0].address.offset == 0);
529
530 image->planes[0].address.bo = bo;
531 image->planes[0].bo_is_owned = true;
532
533 /* Don't clobber the out-parameter until success is certain. */
534 *out_image_h = image_h;
535
536 return VK_SUCCESS;
537
538 fail_size:
539 anv_DestroyImage(device_h, image_h, alloc);
540 fail_create:
541 fail_tiling:
542 anv_device_release_bo(device, bo);
543
544 return result;
545 }
546
547 VkResult
548 format_supported_with_usage(VkDevice device_h, VkFormat format,
549 VkImageUsageFlags imageUsage)
550 {
551 ANV_FROM_HANDLE(anv_device, device, device_h);
552 VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(device->physical);
553 VkResult result;
554
555 const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
556 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
557 .format = format,
558 .type = VK_IMAGE_TYPE_2D,
559 .tiling = VK_IMAGE_TILING_OPTIMAL,
560 .usage = imageUsage,
561 };
562
563 VkImageFormatProperties2 image_format_props = {
564 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
565 };
566
567 /* Check that requested format and usage are supported. */
568 result = anv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h,
569 &image_format_info, &image_format_props);
570 if (result != VK_SUCCESS) {
571 return vk_errorf(device->instance, device, result,
572 "anv_GetPhysicalDeviceImageFormatProperties2 failed "
573 "inside %s", __func__);
574 }
575 return VK_SUCCESS;
576 }
577
578
579 static VkResult
580 setup_gralloc0_usage(VkFormat format, VkImageUsageFlags imageUsage,
581 int *grallocUsage)
582 {
583 /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
584 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
585 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
586 *
587 * TODO(jessehall): I think these are right, but haven't thought hard
588 * about it. Do we need to query the driver for support of any of
589 * these?
590 *
591 * Any disagreement between this function and the hardcoded
592 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
593 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
594 */
595
596 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
597 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
598 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
599
600 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
601 VK_IMAGE_USAGE_SAMPLED_BIT |
602 VK_IMAGE_USAGE_STORAGE_BIT |
603 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
604 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
605
606 /* All VkImageUsageFlags not explicitly checked here are unsupported for
607 * gralloc swapchains.
608 */
609 if (imageUsage != 0) {
610 return vk_errorf(device->instance, device, VK_ERROR_FORMAT_NOT_SUPPORTED,
611 "unsupported VkImageUsageFlags(0x%x) for gralloc "
612 "swapchain", imageUsage);
613 }
614
615 /* The below formats support GRALLOC_USAGE_HW_FB (that is, display
616 * scanout). This short list of formats is univserally supported on Intel
617 * but is incomplete. The full set of supported formats is dependent on
618 * kernel and hardware.
619 *
620 * FINISHME: Advertise all display-supported formats.
621 */
622 switch (format) {
623 case VK_FORMAT_B8G8R8A8_UNORM:
624 case VK_FORMAT_B5G6R5_UNORM_PACK16:
625 case VK_FORMAT_R8G8B8A8_UNORM:
626 case VK_FORMAT_R8G8B8A8_SRGB:
627 *grallocUsage |= GRALLOC_USAGE_HW_FB |
628 GRALLOC_USAGE_HW_COMPOSER |
629 GRALLOC_USAGE_EXTERNAL_DISP;
630 break;
631 default:
632 intel_logw("%s: unsupported format=%d", __func__, format);
633 }
634
635 if (*grallocUsage == 0)
636 return VK_ERROR_FORMAT_NOT_SUPPORTED;
637
638 return VK_SUCCESS;
639 }
640
641
642 #if ANDROID_API_LEVEL >= 26
643 VkResult anv_GetSwapchainGrallocUsage2ANDROID(
644 VkDevice device_h,
645 VkFormat format,
646 VkImageUsageFlags imageUsage,
647 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
648 uint64_t* grallocConsumerUsage,
649 uint64_t* grallocProducerUsage)
650 {
651 ANV_FROM_HANDLE(anv_device, device, device_h);
652 VkResult result;
653
654 *grallocConsumerUsage = 0;
655 *grallocProducerUsage = 0;
656 intel_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
657
658 result = format_supported_with_usage(device_h, format, imageUsage);
659 if (result != VK_SUCCESS)
660 return result;
661
662 int32_t grallocUsage = 0;
663 result = setup_gralloc0_usage(format, imageUsage, &grallocUsage);
664 if (result != VK_SUCCESS)
665 return result;
666
667 android_convertGralloc0To1Usage(grallocUsage, grallocProducerUsage,
668 grallocConsumerUsage);
669
670 return VK_SUCCESS;
671 }
672 #endif
673
674 VkResult anv_GetSwapchainGrallocUsageANDROID(
675 VkDevice device_h,
676 VkFormat format,
677 VkImageUsageFlags imageUsage,
678 int* grallocUsage)
679 {
680 VkResult result;
681
682 *grallocUsage = 0;
683 intel_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
684
685 result = format_supported_with_usage(device_h, format, imageUsage);
686 if (result != VK_SUCCESS)
687 return result;
688
689 return setup_gralloc0_usage(format, imageUsage, grallocUsage);
690 }
691
692 VkResult
693 anv_AcquireImageANDROID(
694 VkDevice device_h,
695 VkImage image_h,
696 int nativeFenceFd,
697 VkSemaphore semaphore_h,
698 VkFence fence_h)
699 {
700 ANV_FROM_HANDLE(anv_device, device, device_h);
701 VkResult result = VK_SUCCESS;
702
703 if (nativeFenceFd != -1) {
704 /* As a simple, firstpass implementation of VK_ANDROID_native_buffer, we
705 * block on the nativeFenceFd. This may introduce latency and is
706 * definitiely inefficient, yet it's correct.
707 *
708 * FINISHME(chadv): Import the nativeFenceFd into the VkSemaphore and
709 * VkFence.
710 */
711 if (sync_wait(nativeFenceFd, /*timeout*/ -1) < 0) {
712 result = vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
713 "%s: failed to wait on nativeFenceFd=%d",
714 __func__, nativeFenceFd);
715 }
716
717 /* From VK_ANDROID_native_buffer's pseudo spec
718 * (https://source.android.com/devices/graphics/implement-vulkan):
719 *
720 * The driver takes ownership of the fence fd and is responsible for
721 * closing it [...] even if vkAcquireImageANDROID fails and returns
722 * an error.
723 */
724 close(nativeFenceFd);
725
726 if (result != VK_SUCCESS)
727 return result;
728 }
729
730 if (semaphore_h || fence_h) {
731 /* Thanks to implicit sync, the image is ready for GPU access. But we
732 * must still put the semaphore into the "submit" state; otherwise the
733 * client may get unexpected behavior if the client later uses it as
734 * a wait semaphore.
735 *
736 * Because we blocked above on the nativeFenceFd, the image is also
737 * ready for foreign-device access (including CPU access). But we must
738 * still signal the fence; otherwise the client may get unexpected
739 * behavior if the client later waits on it.
740 *
741 * For some values of anv_semaphore_type, we must submit the semaphore
742 * to execbuf in order to signal it. Likewise for anv_fence_type.
743 * Instead of open-coding here the signal operation for each
744 * anv_semaphore_type and anv_fence_type, we piggy-back on
745 * vkQueueSubmit.
746 */
747 const VkSubmitInfo submit = {
748 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
749 .waitSemaphoreCount = 0,
750 .commandBufferCount = 0,
751 .signalSemaphoreCount = (semaphore_h ? 1 : 0),
752 .pSignalSemaphores = &semaphore_h,
753 };
754
755 result = anv_QueueSubmit(anv_queue_to_handle(&device->queue), 1,
756 &submit, fence_h);
757 if (result != VK_SUCCESS) {
758 return vk_errorf(device->instance, device, result,
759 "anv_QueueSubmit failed inside %s", __func__);
760 }
761 }
762
763 return VK_SUCCESS;
764 }
765
766 VkResult
767 anv_QueueSignalReleaseImageANDROID(
768 VkQueue queue,
769 uint32_t waitSemaphoreCount,
770 const VkSemaphore* pWaitSemaphores,
771 VkImage image,
772 int* pNativeFenceFd)
773 {
774 VkResult result;
775
776 if (waitSemaphoreCount == 0)
777 goto done;
778
779 result = anv_QueueSubmit(queue, 1,
780 &(VkSubmitInfo) {
781 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
782 .waitSemaphoreCount = 1,
783 .pWaitSemaphores = pWaitSemaphores,
784 },
785 (VkFence) VK_NULL_HANDLE);
786 if (result != VK_SUCCESS)
787 return result;
788
789 done:
790 if (pNativeFenceFd) {
791 /* We can rely implicit on sync because above we submitted all
792 * semaphores to the queue.
793 */
794 *pNativeFenceFd = -1;
795 }
796
797 return VK_SUCCESS;
798 }