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 #endif
29
30 #include <hardware/hardware.h>
31 #include <hardware/hwvulkan.h>
32 #include <vulkan/vk_android_native_buffer.h>
33 #include <vulkan/vk_icd.h>
34 #include <sync/sync.h>
35
36 #include "anv_private.h"
37 #include "vk_android.h"
38 #include "vk_common_entrypoints.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_assert(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC, "");
45
46 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
47 .common = {
48 .tag = HARDWARE_MODULE_TAG,
49 .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
50 .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
51 .id = HWVULKAN_HARDWARE_MODULE_ID,
52 .name = "Intel Vulkan HAL",
53 .author = "Intel",
54 .methods = &(hw_module_methods_t) {
55 .open = anv_hal_open,
56 },
57 },
58 };
59
60 /* If any bits in test_mask are set, then unset them and return true. */
61 static inline bool
unmask32(uint32_t * inout_mask,uint32_t test_mask)62 unmask32(uint32_t *inout_mask, uint32_t test_mask)
63 {
64 uint32_t orig_mask = *inout_mask;
65 *inout_mask &= ~test_mask;
66 return *inout_mask != orig_mask;
67 }
68
69 static int
anv_hal_open(const struct hw_module_t * mod,const char * id,struct hw_device_t ** dev)70 anv_hal_open(const struct hw_module_t* mod, const char* id,
71 struct hw_device_t** dev)
72 {
73 assert(mod == &HAL_MODULE_INFO_SYM.common);
74 assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
75
76 hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
77 if (!hal_dev)
78 return -1;
79
80 *hal_dev = (hwvulkan_device_t) {
81 .common = {
82 .tag = HARDWARE_DEVICE_TAG,
83 .version = HWVULKAN_DEVICE_API_VERSION_0_1,
84 .module = &HAL_MODULE_INFO_SYM.common,
85 .close = anv_hal_close,
86 },
87 .EnumerateInstanceExtensionProperties = anv_EnumerateInstanceExtensionProperties,
88 .CreateInstance = anv_CreateInstance,
89 .GetInstanceProcAddr = anv_GetInstanceProcAddr,
90 };
91
92 *dev = &hal_dev->common;
93 return 0;
94 }
95
96 static int
anv_hal_close(struct hw_device_t * dev)97 anv_hal_close(struct hw_device_t *dev)
98 {
99 /* hwvulkan.h claims that hw_device_t::close() is never called. */
100 return -1;
101 }
102
103 #if ANDROID_API_LEVEL >= 26
104 #include <vndk/hardware_buffer.h>
105 /* See i915_private_android_types.h in minigbm. */
106 #define HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL 0x100
107
108 enum {
109 /* Usage bit equal to GRALLOC_USAGE_HW_CAMERA_MASK */
110 BUFFER_USAGE_CAMERA_MASK = 0x00060000U,
111 };
112
113 inline VkFormat
vk_format_from_android(unsigned android_format,unsigned android_usage)114 vk_format_from_android(unsigned android_format, unsigned android_usage)
115 {
116 switch (android_format) {
117 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
118 return VK_FORMAT_R8G8B8_UNORM;
119 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
120 case HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL:
121 return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
122 case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED:
123 if (android_usage & BUFFER_USAGE_CAMERA_MASK)
124 return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
125 else
126 return VK_FORMAT_R8G8B8_UNORM;
127 default:
128 return vk_ahb_format_to_image_format(android_format);
129 }
130 }
131
132 unsigned
anv_ahb_format_for_vk_format(VkFormat vk_format)133 anv_ahb_format_for_vk_format(VkFormat vk_format)
134 {
135 switch (vk_format) {
136 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
137 #ifdef HAVE_CROS_GRALLOC
138 return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
139 #else
140 return HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL;
141 #endif
142 default:
143 return vk_image_format_to_ahb_format(vk_format);
144 }
145 }
146
147 static VkResult
get_ahw_buffer_format_properties2(VkDevice device_h,const struct AHardwareBuffer * buffer,VkAndroidHardwareBufferFormatProperties2ANDROID * pProperties)148 get_ahw_buffer_format_properties2(
149 VkDevice device_h,
150 const struct AHardwareBuffer *buffer,
151 VkAndroidHardwareBufferFormatProperties2ANDROID *pProperties)
152 {
153 ANV_FROM_HANDLE(anv_device, device, device_h);
154
155 /* Get a description of buffer contents . */
156 AHardwareBuffer_Desc desc;
157 AHardwareBuffer_describe(buffer, &desc);
158
159 /* Verify description. */
160 uint64_t gpu_usage =
161 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
162 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
163 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
164
165 /* "Buffer must be a valid Android hardware buffer object with at least
166 * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
167 */
168 if (!(desc.usage & (gpu_usage)))
169 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
170
171 /* Fill properties fields based on description. */
172 VkAndroidHardwareBufferFormatProperties2ANDROID *p = pProperties;
173
174 p->format = vk_format_from_android(desc.format, desc.usage);
175 p->externalFormat = p->format;
176
177 const struct anv_format *anv_format = anv_get_format(p->format);
178
179 /* Default to OPTIMAL tiling but set to linear in case
180 * of AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER usage.
181 */
182 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
183
184 if (desc.usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER)
185 tiling = VK_IMAGE_TILING_LINEAR;
186
187 p->formatFeatures =
188 anv_get_image_format_features2(device->info, p->format, anv_format,
189 tiling, NULL);
190
191 /* "Images can be created with an external format even if the Android hardware
192 * buffer has a format which has an equivalent Vulkan format to enable
193 * consistent handling of images from sources that might use either category
194 * of format. However, all images created with an external format are subject
195 * to the valid usage requirements associated with external formats, even if
196 * the Android hardware buffer’s format has a Vulkan equivalent."
197 *
198 * "The formatFeatures member *must* include
199 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
200 * VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
201 * VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT"
202 */
203 p->formatFeatures |=
204 VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
205
206 /* "Implementations may not always be able to determine the color model,
207 * numerical range, or chroma offsets of the image contents, so the values
208 * in VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
209 * Applications should treat these values as sensible defaults to use in
210 * the absence of more reliable information obtained through some other
211 * means."
212 */
213 p->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
214 p->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
215 p->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
216 p->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;
217
218 p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
219 p->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
220
221 p->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
222 p->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
223
224 return VK_SUCCESS;
225 }
226
227 VkResult
anv_GetAndroidHardwareBufferPropertiesANDROID(VkDevice device_h,const struct AHardwareBuffer * buffer,VkAndroidHardwareBufferPropertiesANDROID * pProperties)228 anv_GetAndroidHardwareBufferPropertiesANDROID(
229 VkDevice device_h,
230 const struct AHardwareBuffer *buffer,
231 VkAndroidHardwareBufferPropertiesANDROID *pProperties)
232 {
233 ANV_FROM_HANDLE(anv_device, dev, device_h);
234
235 VkAndroidHardwareBufferFormatPropertiesANDROID *format_prop =
236 vk_find_struct(pProperties->pNext,
237 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
238 /* Fill format properties of an Android hardware buffer. */
239 if (format_prop) {
240 VkAndroidHardwareBufferFormatProperties2ANDROID format_prop2 = {
241 .sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID,
242 };
243 get_ahw_buffer_format_properties2(device_h, buffer, &format_prop2);
244
245 format_prop->format = format_prop2.format;
246 format_prop->externalFormat = format_prop2.externalFormat;
247 format_prop->formatFeatures =
248 vk_format_features2_to_features(format_prop2.formatFeatures);
249 format_prop->samplerYcbcrConversionComponents =
250 format_prop2.samplerYcbcrConversionComponents;
251 format_prop->suggestedYcbcrModel = format_prop2.suggestedYcbcrModel;
252 format_prop->suggestedYcbcrRange = format_prop2.suggestedYcbcrRange;
253 format_prop->suggestedXChromaOffset = format_prop2.suggestedXChromaOffset;
254 format_prop->suggestedYChromaOffset = format_prop2.suggestedYChromaOffset;
255 }
256
257 VkAndroidHardwareBufferFormatProperties2ANDROID *format_prop2 =
258 vk_find_struct(pProperties->pNext,
259 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID);
260 if (format_prop2)
261 get_ahw_buffer_format_properties2(device_h, buffer, format_prop2);
262
263 /* NOTE - We support buffers with only one handle but do not error on
264 * multiple handle case. Reason is that we want to support YUV formats
265 * where we have many logical planes but they all point to the same
266 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
267 */
268 const native_handle_t *handle =
269 AHardwareBuffer_getNativeHandle(buffer);
270 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
271 if (dma_buf < 0)
272 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
273
274 /* All memory types. */
275 uint32_t memory_types = (1ull << dev->physical->memory.type_count) - 1;
276
277 pProperties->allocationSize = lseek(dma_buf, 0, SEEK_END);
278 pProperties->memoryTypeBits = memory_types;
279
280 return VK_SUCCESS;
281 }
282
283 VkResult
anv_GetMemoryAndroidHardwareBufferANDROID(VkDevice device_h,const VkMemoryGetAndroidHardwareBufferInfoANDROID * pInfo,struct AHardwareBuffer ** pBuffer)284 anv_GetMemoryAndroidHardwareBufferANDROID(
285 VkDevice device_h,
286 const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
287 struct AHardwareBuffer **pBuffer)
288 {
289 ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
290
291 /* Some quotes from Vulkan spec:
292 *
293 * "If the device memory was created by importing an Android hardware
294 * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
295 * Android hardware buffer object."
296 *
297 * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
298 * have been included in VkExportMemoryAllocateInfo::handleTypes when
299 * memory was created."
300 */
301 if (mem->ahw) {
302 *pBuffer = mem->ahw;
303 /* Increase refcount. */
304 AHardwareBuffer_acquire(mem->ahw);
305 return VK_SUCCESS;
306 }
307
308 return VK_ERROR_OUT_OF_HOST_MEMORY;
309 }
310
311 #endif
312
313 /*
314 * Called from anv_AllocateMemory when import AHardwareBuffer.
315 */
316 VkResult
anv_import_ahw_memory(VkDevice device_h,struct anv_device_memory * mem,const VkImportAndroidHardwareBufferInfoANDROID * info)317 anv_import_ahw_memory(VkDevice device_h,
318 struct anv_device_memory *mem,
319 const VkImportAndroidHardwareBufferInfoANDROID *info)
320 {
321 #if ANDROID_API_LEVEL >= 26
322 ANV_FROM_HANDLE(anv_device, device, device_h);
323
324 /* Import from AHardwareBuffer to anv_device_memory. */
325 const native_handle_t *handle =
326 AHardwareBuffer_getNativeHandle(info->buffer);
327
328 /* NOTE - We support buffers with only one handle but do not error on
329 * multiple handle case. Reason is that we want to support YUV formats
330 * where we have many logical planes but they all point to the same
331 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
332 */
333 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
334 if (dma_buf < 0)
335 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
336
337 VkResult result = anv_device_import_bo(device, dma_buf, 0,
338 0 /* client_address */,
339 &mem->bo);
340 assert(result == VK_SUCCESS);
341
342 /* "If the vkAllocateMemory command succeeds, the implementation must
343 * acquire a reference to the imported hardware buffer, which it must
344 * release when the device memory object is freed. If the command fails,
345 * the implementation must not retain a reference."
346 */
347 AHardwareBuffer_acquire(info->buffer);
348 mem->ahw = info->buffer;
349
350 return VK_SUCCESS;
351 #else
352 return VK_ERROR_EXTENSION_NOT_PRESENT;
353 #endif
354 }
355
356 VkResult
anv_create_ahw_memory(VkDevice device_h,struct anv_device_memory * mem,const VkMemoryAllocateInfo * pAllocateInfo)357 anv_create_ahw_memory(VkDevice device_h,
358 struct anv_device_memory *mem,
359 const VkMemoryAllocateInfo *pAllocateInfo)
360 {
361 #if ANDROID_API_LEVEL >= 26
362 struct AHardwareBuffer *ahw = vk_alloc_ahardware_buffer(pAllocateInfo);
363 if (ahw == NULL)
364 return VK_ERROR_OUT_OF_HOST_MEMORY;
365
366 const VkImportAndroidHardwareBufferInfoANDROID import_info = {
367 .buffer = ahw,
368 };
369 VkResult result = anv_import_ahw_memory(device_h, mem, &import_info);
370
371 /* Release a reference to avoid leak for AHB allocation. */
372 AHardwareBuffer_release(ahw);
373
374 return result;
375 #else
376 return VK_ERROR_EXTENSION_NOT_PRESENT;
377 #endif
378
379 }
380
381 VkResult
anv_image_init_from_gralloc(struct anv_device * device,struct anv_image * image,const VkImageCreateInfo * base_info,const VkNativeBufferANDROID * gralloc_info)382 anv_image_init_from_gralloc(struct anv_device *device,
383 struct anv_image *image,
384 const VkImageCreateInfo *base_info,
385 const VkNativeBufferANDROID *gralloc_info)
386 {
387 struct anv_bo *bo = NULL;
388 VkResult result;
389
390 struct anv_image_create_info anv_info = {
391 .vk_info = base_info,
392 .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
393 };
394
395 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
396 * must exceed that of the gralloc handle, and we do not own the gralloc
397 * handle.
398 */
399 int dma_buf = gralloc_info->handle->data[0];
400
401 /* We need to set the WRITE flag on window system buffers so that GEM will
402 * know we're writing to them and synchronize uses on other rings (for
403 * example, if the display server uses the blitter ring).
404 *
405 * If this function fails and if the imported bo was resident in the cache,
406 * we should avoid updating the bo's flags. Therefore, we defer updating
407 * the flags until success is certain.
408 *
409 */
410 result = anv_device_import_bo(device, dma_buf,
411 ANV_BO_ALLOC_IMPLICIT_SYNC |
412 ANV_BO_ALLOC_IMPLICIT_WRITE,
413 0 /* client_address */,
414 &bo);
415 if (result != VK_SUCCESS) {
416 return vk_errorf(device, result,
417 "failed to import dma-buf from VkNativeBufferANDROID");
418 }
419
420 enum isl_tiling tiling;
421 result = anv_device_get_bo_tiling(device, bo, &tiling);
422 if (result != VK_SUCCESS) {
423 return vk_errorf(device, result,
424 "failed to get tiling from VkNativeBufferANDROID");
425 }
426 anv_info.isl_tiling_flags = 1u << tiling;
427
428 enum isl_format format = anv_get_isl_format(device->info,
429 base_info->format,
430 VK_IMAGE_ASPECT_COLOR_BIT,
431 base_info->tiling);
432 assert(format != ISL_FORMAT_UNSUPPORTED);
433
434 result = anv_image_init(device, image, &anv_info);
435 if (result != VK_SUCCESS)
436 goto fail_init;
437
438 VkMemoryRequirements2 mem_reqs = {
439 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
440 };
441
442 anv_image_get_memory_requirements(device, image, image->vk.aspects,
443 &mem_reqs);
444
445 VkDeviceSize aligned_image_size =
446 align64(mem_reqs.memoryRequirements.size,
447 mem_reqs.memoryRequirements.alignment);
448
449 if (bo->size < aligned_image_size) {
450 result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
451 "dma-buf from VkNativeBufferANDROID is too small for "
452 "VkImage: %"PRIu64"B < %"PRIu64"B",
453 bo->size, aligned_image_size);
454 goto fail_size;
455 }
456
457 assert(!image->disjoint);
458 assert(image->n_planes == 1);
459 assert(image->planes[0].primary_surface.memory_range.binding ==
460 ANV_IMAGE_MEMORY_BINDING_MAIN);
461 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo == NULL);
462 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.offset == 0);
463 image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo = bo;
464 image->from_gralloc = true;
465
466 return VK_SUCCESS;
467
468 fail_size:
469 anv_image_finish(image);
470 fail_init:
471 anv_device_release_bo(device, bo);
472
473 return result;
474 }
475
476 VkResult
anv_image_bind_from_gralloc(struct anv_device * device,struct anv_image * image,const VkNativeBufferANDROID * gralloc_info)477 anv_image_bind_from_gralloc(struct anv_device *device,
478 struct anv_image *image,
479 const VkNativeBufferANDROID *gralloc_info)
480 {
481 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
482 * must exceed that of the gralloc handle, and we do not own the gralloc
483 * handle.
484 */
485 int dma_buf = gralloc_info->handle->data[0];
486
487 /* We need to set the WRITE flag on window system buffers so that GEM will
488 * know we're writing to them and synchronize uses on other rings (for
489 * example, if the display server uses the blitter ring).
490 *
491 * If this function fails and if the imported bo was resident in the cache,
492 * we should avoid updating the bo's flags. Therefore, we defer updating
493 * the flags until success is certain.
494 *
495 */
496 struct anv_bo *bo = NULL;
497 VkResult result = anv_device_import_bo(device, dma_buf,
498 ANV_BO_ALLOC_IMPLICIT_SYNC |
499 ANV_BO_ALLOC_IMPLICIT_WRITE,
500 0 /* client_address */,
501 &bo);
502 if (result != VK_SUCCESS) {
503 return vk_errorf(device, result,
504 "failed to import dma-buf from VkNativeBufferANDROID");
505 }
506
507 uint64_t img_size = image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].memory_range.size;
508 if (img_size < bo->size) {
509 result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
510 "dma-buf from VkNativeBufferANDROID is too small for "
511 "VkImage: %"PRIu64"B < %"PRIu64"B",
512 bo->size, img_size);
513 anv_device_release_bo(device, bo);
514 return result;
515 }
516
517 assert(!image->disjoint);
518 assert(image->n_planes == 1);
519 assert(image->planes[0].primary_surface.memory_range.binding ==
520 ANV_IMAGE_MEMORY_BINDING_MAIN);
521 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo == NULL);
522 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.offset == 0);
523 image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo = bo;
524 image->from_gralloc = true;
525
526 return VK_SUCCESS;
527 }
528
529 static VkResult
format_supported_with_usage(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage)530 format_supported_with_usage(VkDevice device_h, VkFormat format,
531 VkImageUsageFlags imageUsage)
532 {
533 ANV_FROM_HANDLE(anv_device, device, device_h);
534 VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(device->physical);
535 VkResult result;
536
537 const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
538 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
539 .format = format,
540 .type = VK_IMAGE_TYPE_2D,
541 .tiling = VK_IMAGE_TILING_OPTIMAL,
542 .usage = imageUsage,
543 };
544
545 VkImageFormatProperties2 image_format_props = {
546 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
547 };
548
549 /* Check that requested format and usage are supported. */
550 result = anv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h,
551 &image_format_info, &image_format_props);
552 if (result != VK_SUCCESS) {
553 return vk_errorf(device, result,
554 "anv_GetPhysicalDeviceImageFormatProperties2 failed "
555 "inside %s", __func__);
556 }
557 return VK_SUCCESS;
558 }
559
560
561 static VkResult
setup_gralloc0_usage(struct anv_device * device,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)562 setup_gralloc0_usage(struct anv_device *device, VkFormat format,
563 VkImageUsageFlags imageUsage, int *grallocUsage)
564 {
565 /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
566 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
567 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
568 *
569 * TODO(jessehall): I think these are right, but haven't thought hard
570 * about it. Do we need to query the driver for support of any of
571 * these?
572 *
573 * Any disagreement between this function and the hardcoded
574 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
575 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
576 */
577
578 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
579 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
580 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
581
582 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
583 VK_IMAGE_USAGE_SAMPLED_BIT |
584 VK_IMAGE_USAGE_STORAGE_BIT |
585 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
586 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
587
588 /* All VkImageUsageFlags not explicitly checked here are unsupported for
589 * gralloc swapchains.
590 */
591 if (imageUsage != 0) {
592 return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED,
593 "unsupported VkImageUsageFlags(0x%x) for gralloc "
594 "swapchain", imageUsage);
595 }
596
597 /* The below formats support GRALLOC_USAGE_HW_FB (that is, display
598 * scanout). This short list of formats is univserally supported on Intel
599 * but is incomplete. The full set of supported formats is dependent on
600 * kernel and hardware.
601 *
602 * FINISHME: Advertise all display-supported formats.
603 */
604 switch (format) {
605 case VK_FORMAT_B8G8R8A8_UNORM:
606 case VK_FORMAT_R5G6B5_UNORM_PACK16:
607 case VK_FORMAT_R8G8B8A8_UNORM:
608 case VK_FORMAT_R8G8B8A8_SRGB:
609 *grallocUsage |= GRALLOC_USAGE_HW_FB |
610 GRALLOC_USAGE_HW_COMPOSER |
611 GRALLOC_USAGE_EXTERNAL_DISP;
612 break;
613 default:
614 mesa_logw("%s: unsupported format=%d", __func__, format);
615 }
616
617 if (*grallocUsage == 0)
618 return VK_ERROR_FORMAT_NOT_SUPPORTED;
619
620 return VK_SUCCESS;
621 }
622
623 #if ANDROID_API_LEVEL >= 26
anv_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,uint64_t * grallocConsumerUsage,uint64_t * grallocProducerUsage)624 VkResult anv_GetSwapchainGrallocUsage2ANDROID(
625 VkDevice device_h,
626 VkFormat format,
627 VkImageUsageFlags imageUsage,
628 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
629 uint64_t* grallocConsumerUsage,
630 uint64_t* grallocProducerUsage)
631 {
632 ANV_FROM_HANDLE(anv_device, device, device_h);
633 VkResult result;
634
635 *grallocConsumerUsage = 0;
636 *grallocProducerUsage = 0;
637 mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
638
639 result = format_supported_with_usage(device_h, format, imageUsage);
640 if (result != VK_SUCCESS)
641 return result;
642
643 int32_t grallocUsage = 0;
644 result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage);
645 if (result != VK_SUCCESS)
646 return result;
647
648 /* Setup gralloc1 usage flags from gralloc0 flags. */
649
650 if (grallocUsage & GRALLOC_USAGE_HW_RENDER) {
651 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
652 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET;
653 }
654
655 if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) {
656 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
657 }
658
659 if (grallocUsage & (GRALLOC_USAGE_HW_FB |
660 GRALLOC_USAGE_HW_COMPOSER |
661 GRALLOC_USAGE_EXTERNAL_DISP)) {
662 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
663 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER;
664 }
665
666 return VK_SUCCESS;
667 }
668 #endif
669
anv_GetSwapchainGrallocUsageANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)670 VkResult anv_GetSwapchainGrallocUsageANDROID(
671 VkDevice device_h,
672 VkFormat format,
673 VkImageUsageFlags imageUsage,
674 int* grallocUsage)
675 {
676 ANV_FROM_HANDLE(anv_device, device, device_h);
677 VkResult result;
678
679 *grallocUsage = 0;
680 mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
681
682 result = format_supported_with_usage(device_h, format, imageUsage);
683 if (result != VK_SUCCESS)
684 return result;
685
686 return setup_gralloc0_usage(device, format, imageUsage, grallocUsage);
687 }
688