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