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_YV12:
123 return VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
124 case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED:
125 if (android_usage & BUFFER_USAGE_CAMERA_MASK)
126 return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
127 else
128 return VK_FORMAT_R8G8B8_UNORM;
129 default:
130 return vk_ahb_format_to_image_format(android_format);
131 }
132 }
133
134 unsigned
anv_ahb_format_for_vk_format(VkFormat vk_format)135 anv_ahb_format_for_vk_format(VkFormat vk_format)
136 {
137 switch (vk_format) {
138 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
139 #ifdef HAVE_CROS_GRALLOC
140 return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
141 #else
142 return HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL;
143 #endif
144 default:
145 return vk_image_format_to_ahb_format(vk_format);
146 }
147 }
148
149 static VkResult
get_ahw_buffer_format_properties2(VkDevice device_h,const struct AHardwareBuffer * buffer,VkAndroidHardwareBufferFormatProperties2ANDROID * pProperties)150 get_ahw_buffer_format_properties2(
151 VkDevice device_h,
152 const struct AHardwareBuffer *buffer,
153 VkAndroidHardwareBufferFormatProperties2ANDROID *pProperties)
154 {
155 ANV_FROM_HANDLE(anv_device, device, device_h);
156
157 /* Get a description of buffer contents . */
158 AHardwareBuffer_Desc desc;
159 AHardwareBuffer_describe(buffer, &desc);
160
161 /* Verify description. */
162 uint64_t gpu_usage =
163 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
164 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
165 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
166
167 /* "Buffer must be a valid Android hardware buffer object with at least
168 * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
169 */
170 if (!(desc.usage & (gpu_usage)))
171 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
172
173 /* Fill properties fields based on description. */
174 VkAndroidHardwareBufferFormatProperties2ANDROID *p = pProperties;
175
176 p->format = vk_format_from_android(desc.format, desc.usage);
177 p->externalFormat = p->format;
178
179 const struct anv_format *anv_format = anv_get_format(p->format);
180
181 /* Default to OPTIMAL tiling but set to linear in case
182 * of AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER usage.
183 */
184 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
185
186 if (desc.usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER)
187 tiling = VK_IMAGE_TILING_LINEAR;
188
189 p->formatFeatures =
190 anv_get_image_format_features2(device->physical, p->format, anv_format,
191 tiling, NULL);
192
193 /* "Images can be created with an external format even if the Android hardware
194 * buffer has a format which has an equivalent Vulkan format to enable
195 * consistent handling of images from sources that might use either category
196 * of format. However, all images created with an external format are subject
197 * to the valid usage requirements associated with external formats, even if
198 * the Android hardware buffer’s format has a Vulkan equivalent."
199 *
200 * "The formatFeatures member *must* include
201 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
202 * VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
203 * VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT"
204 */
205 p->formatFeatures |=
206 VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
207
208 /* "Implementations may not always be able to determine the color model,
209 * numerical range, or chroma offsets of the image contents, so the values
210 * in VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
211 * Applications should treat these values as sensible defaults to use in
212 * the absence of more reliable information obtained through some other
213 * means."
214 */
215 p->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
216 p->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
217 p->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
218 p->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;
219
220 p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
221 p->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
222
223 p->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
224 p->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
225
226 return VK_SUCCESS;
227 }
228
229 VkResult
anv_GetAndroidHardwareBufferPropertiesANDROID(VkDevice device_h,const struct AHardwareBuffer * buffer,VkAndroidHardwareBufferPropertiesANDROID * pProperties)230 anv_GetAndroidHardwareBufferPropertiesANDROID(
231 VkDevice device_h,
232 const struct AHardwareBuffer *buffer,
233 VkAndroidHardwareBufferPropertiesANDROID *pProperties)
234 {
235 ANV_FROM_HANDLE(anv_device, dev, device_h);
236
237 VkAndroidHardwareBufferFormatPropertiesANDROID *format_prop =
238 vk_find_struct(pProperties->pNext,
239 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
240 /* Fill format properties of an Android hardware buffer. */
241 if (format_prop) {
242 VkAndroidHardwareBufferFormatProperties2ANDROID format_prop2 = {
243 .sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID,
244 };
245 get_ahw_buffer_format_properties2(device_h, buffer, &format_prop2);
246
247 format_prop->format = format_prop2.format;
248 format_prop->externalFormat = format_prop2.externalFormat;
249 format_prop->formatFeatures =
250 vk_format_features2_to_features(format_prop2.formatFeatures);
251 format_prop->samplerYcbcrConversionComponents =
252 format_prop2.samplerYcbcrConversionComponents;
253 format_prop->suggestedYcbcrModel = format_prop2.suggestedYcbcrModel;
254 format_prop->suggestedYcbcrRange = format_prop2.suggestedYcbcrRange;
255 format_prop->suggestedXChromaOffset = format_prop2.suggestedXChromaOffset;
256 format_prop->suggestedYChromaOffset = format_prop2.suggestedYChromaOffset;
257 }
258
259 VkAndroidHardwareBufferFormatProperties2ANDROID *format_prop2 =
260 vk_find_struct(pProperties->pNext,
261 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID);
262 if (format_prop2)
263 get_ahw_buffer_format_properties2(device_h, buffer, format_prop2);
264
265 /* NOTE - We support buffers with only one handle but do not error on
266 * multiple handle case. Reason is that we want to support YUV formats
267 * where we have many logical planes but they all point to the same
268 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
269 */
270 const native_handle_t *handle =
271 AHardwareBuffer_getNativeHandle(buffer);
272 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
273 if (dma_buf < 0)
274 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
275
276 /* All memory types. */
277 uint32_t memory_types = (1ull << dev->physical->memory.type_count) - 1;
278
279 pProperties->allocationSize = lseek(dma_buf, 0, SEEK_END);
280 pProperties->memoryTypeBits = memory_types;
281
282 return VK_SUCCESS;
283 }
284
285 #endif
286
287 /*
288 * Called from anv_AllocateMemory when import AHardwareBuffer.
289 */
290 VkResult
anv_import_ahw_memory(VkDevice device_h,struct anv_device_memory * mem)291 anv_import_ahw_memory(VkDevice device_h,
292 struct anv_device_memory *mem)
293 {
294 #if ANDROID_API_LEVEL >= 26
295 ANV_FROM_HANDLE(anv_device, device, device_h);
296
297 /* Import from AHardwareBuffer to anv_device_memory. */
298 const native_handle_t *handle =
299 AHardwareBuffer_getNativeHandle(mem->vk.ahardware_buffer);
300
301 /* NOTE - We support buffers with only one handle but do not error on
302 * multiple handle case. Reason is that we want to support YUV formats
303 * where we have many logical planes but they all point to the same
304 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
305 */
306 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
307 if (dma_buf < 0)
308 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
309
310 VkResult result = anv_device_import_bo(device, dma_buf, 0,
311 0 /* client_address */,
312 &mem->bo);
313 assert(result == VK_SUCCESS);
314
315 return VK_SUCCESS;
316 #else
317 return VK_ERROR_EXTENSION_NOT_PRESENT;
318 #endif
319 }
320
321 VkResult
anv_image_init_from_gralloc(struct anv_device * device,struct anv_image * image,const VkImageCreateInfo * base_info,const VkNativeBufferANDROID * gralloc_info)322 anv_image_init_from_gralloc(struct anv_device *device,
323 struct anv_image *image,
324 const VkImageCreateInfo *base_info,
325 const VkNativeBufferANDROID *gralloc_info)
326 {
327 struct anv_bo *bo = NULL;
328 VkResult result;
329
330 struct anv_image_create_info anv_info = {
331 .vk_info = base_info,
332 .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
333 };
334
335 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
336 * must exceed that of the gralloc handle, and we do not own the gralloc
337 * handle.
338 */
339 int dma_buf = gralloc_info->handle->data[0];
340
341 /* We need to set the WRITE flag on window system buffers so that GEM will
342 * know we're writing to them and synchronize uses on other rings (for
343 * example, if the display server uses the blitter ring).
344 *
345 * If this function fails and if the imported bo was resident in the cache,
346 * we should avoid updating the bo's flags. Therefore, we defer updating
347 * the flags until success is certain.
348 *
349 */
350 result = anv_device_import_bo(device, dma_buf,
351 ANV_BO_ALLOC_EXTERNAL |
352 ANV_BO_ALLOC_IMPLICIT_SYNC |
353 ANV_BO_ALLOC_IMPLICIT_WRITE,
354 0 /* client_address */,
355 &bo);
356 if (result != VK_SUCCESS) {
357 return vk_errorf(device, result,
358 "failed to import dma-buf from VkNativeBufferANDROID");
359 }
360
361 enum isl_tiling tiling;
362 result = anv_device_get_bo_tiling(device, bo, &tiling);
363 if (result != VK_SUCCESS) {
364 return vk_errorf(device, result,
365 "failed to get tiling from VkNativeBufferANDROID");
366 }
367 anv_info.isl_tiling_flags = 1u << tiling;
368
369 anv_info.stride = gralloc_info->stride;
370
371 result = anv_image_init(device, image, &anv_info);
372 if (result != VK_SUCCESS)
373 goto fail_init;
374
375 VkMemoryRequirements2 mem_reqs = {
376 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
377 };
378
379 anv_image_get_memory_requirements(device, image, image->vk.aspects,
380 &mem_reqs);
381
382 VkDeviceSize aligned_image_size =
383 align64(mem_reqs.memoryRequirements.size,
384 mem_reqs.memoryRequirements.alignment);
385
386 if (bo->size < aligned_image_size) {
387 result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
388 "dma-buf from VkNativeBufferANDROID is too small for "
389 "VkImage: %"PRIu64"B < %"PRIu64"B",
390 bo->size, aligned_image_size);
391 goto fail_size;
392 }
393
394 assert(!image->disjoint);
395 assert(image->n_planes == 1);
396 assert(image->planes[0].primary_surface.memory_range.binding ==
397 ANV_IMAGE_MEMORY_BINDING_MAIN);
398 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo == NULL);
399 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.offset == 0);
400 image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo = bo;
401 image->from_gralloc = true;
402
403 return VK_SUCCESS;
404
405 fail_size:
406 anv_image_finish(image);
407 fail_init:
408 anv_device_release_bo(device, bo);
409
410 return result;
411 }
412
413 VkResult
anv_image_bind_from_gralloc(struct anv_device * device,struct anv_image * image,const VkNativeBufferANDROID * gralloc_info)414 anv_image_bind_from_gralloc(struct anv_device *device,
415 struct anv_image *image,
416 const VkNativeBufferANDROID *gralloc_info)
417 {
418 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
419 * must exceed that of the gralloc handle, and we do not own the gralloc
420 * handle.
421 */
422 int dma_buf = gralloc_info->handle->data[0];
423
424 /* We need to set the WRITE flag on window system buffers so that GEM will
425 * know we're writing to them and synchronize uses on other rings (for
426 * example, if the display server uses the blitter ring).
427 *
428 * If this function fails and if the imported bo was resident in the cache,
429 * we should avoid updating the bo's flags. Therefore, we defer updating
430 * the flags until success is certain.
431 *
432 */
433 struct anv_bo *bo = NULL;
434 VkResult result = anv_device_import_bo(device, dma_buf,
435 ANV_BO_ALLOC_EXTERNAL |
436 ANV_BO_ALLOC_IMPLICIT_SYNC |
437 ANV_BO_ALLOC_IMPLICIT_WRITE,
438 0 /* client_address */,
439 &bo);
440 if (result != VK_SUCCESS) {
441 return vk_errorf(device, result,
442 "failed to import dma-buf from VkNativeBufferANDROID");
443 }
444
445 uint64_t img_size = image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].memory_range.size;
446 if (img_size < bo->size) {
447 result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
448 "dma-buf from VkNativeBufferANDROID is too small for "
449 "VkImage: %"PRIu64"B < %"PRIu64"B",
450 bo->size, img_size);
451 anv_device_release_bo(device, bo);
452 return result;
453 }
454
455 assert(!image->disjoint);
456 assert(image->n_planes == 1);
457 assert(image->planes[0].primary_surface.memory_range.binding ==
458 ANV_IMAGE_MEMORY_BINDING_MAIN);
459 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo == NULL);
460 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.offset == 0);
461 image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo = bo;
462 image->from_gralloc = true;
463
464 return VK_SUCCESS;
465 }
466
467 static VkResult
format_supported_with_usage(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage)468 format_supported_with_usage(VkDevice device_h, VkFormat format,
469 VkImageUsageFlags imageUsage)
470 {
471 ANV_FROM_HANDLE(anv_device, device, device_h);
472 VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(device->physical);
473 VkResult result;
474
475 const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
476 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
477 .format = format,
478 .type = VK_IMAGE_TYPE_2D,
479 .tiling = VK_IMAGE_TILING_OPTIMAL,
480 .usage = imageUsage,
481 };
482
483 VkImageFormatProperties2 image_format_props = {
484 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
485 };
486
487 /* Check that requested format and usage are supported. */
488 result = anv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h,
489 &image_format_info, &image_format_props);
490 if (result != VK_SUCCESS) {
491 return vk_errorf(device, result,
492 "anv_GetPhysicalDeviceImageFormatProperties2 failed "
493 "inside %s", __func__);
494 }
495 return VK_SUCCESS;
496 }
497
498
499 static VkResult
setup_gralloc0_usage(struct anv_device * device,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)500 setup_gralloc0_usage(struct anv_device *device, VkFormat format,
501 VkImageUsageFlags imageUsage, int *grallocUsage)
502 {
503 /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
504 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
505 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
506 *
507 * TODO(jessehall): I think these are right, but haven't thought hard
508 * about it. Do we need to query the driver for support of any of
509 * these?
510 *
511 * Any disagreement between this function and the hardcoded
512 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
513 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
514 */
515
516 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
517 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
518 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
519
520 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
521 VK_IMAGE_USAGE_SAMPLED_BIT |
522 VK_IMAGE_USAGE_STORAGE_BIT |
523 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
524 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
525
526 /* All VkImageUsageFlags not explicitly checked here are unsupported for
527 * gralloc swapchains.
528 */
529 if (imageUsage != 0) {
530 return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED,
531 "unsupported VkImageUsageFlags(0x%x) for gralloc "
532 "swapchain", imageUsage);
533 }
534
535 /* The below formats support GRALLOC_USAGE_HW_FB (that is, display
536 * scanout). This short list of formats is univserally supported on Intel
537 * but is incomplete. The full set of supported formats is dependent on
538 * kernel and hardware.
539 *
540 * FINISHME: Advertise all display-supported formats.
541 */
542 switch (format) {
543 case VK_FORMAT_B8G8R8A8_UNORM:
544 case VK_FORMAT_R5G6B5_UNORM_PACK16:
545 case VK_FORMAT_R8G8B8A8_UNORM:
546 case VK_FORMAT_R8G8B8A8_SRGB:
547 *grallocUsage |= GRALLOC_USAGE_HW_FB |
548 GRALLOC_USAGE_HW_COMPOSER |
549 GRALLOC_USAGE_EXTERNAL_DISP;
550 break;
551 default:
552 mesa_logw("%s: unsupported format=%d", __func__, format);
553 }
554
555 if (*grallocUsage == 0)
556 return VK_ERROR_FORMAT_NOT_SUPPORTED;
557
558 return VK_SUCCESS;
559 }
560
561 #if ANDROID_API_LEVEL >= 26
anv_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,uint64_t * grallocConsumerUsage,uint64_t * grallocProducerUsage)562 VkResult anv_GetSwapchainGrallocUsage2ANDROID(
563 VkDevice device_h,
564 VkFormat format,
565 VkImageUsageFlags imageUsage,
566 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
567 uint64_t* grallocConsumerUsage,
568 uint64_t* grallocProducerUsage)
569 {
570 ANV_FROM_HANDLE(anv_device, device, device_h);
571 VkResult result;
572
573 *grallocConsumerUsage = 0;
574 *grallocProducerUsage = 0;
575 mesa_logd("%s: format=%d, usage=0x%x, swapchainUsage=0x%x", __func__, format,
576 imageUsage, swapchainImageUsage);
577
578 result = format_supported_with_usage(device_h, format, imageUsage);
579 if (result != VK_SUCCESS)
580 return result;
581
582 int32_t grallocUsage = 0;
583 result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage);
584 if (result != VK_SUCCESS)
585 return result;
586
587 /* Setup gralloc1 usage flags from gralloc0 flags. */
588
589 if (grallocUsage & GRALLOC_USAGE_HW_RENDER) {
590 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
591 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET;
592 }
593
594 if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) {
595 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
596 }
597
598 if (grallocUsage & (GRALLOC_USAGE_HW_FB |
599 GRALLOC_USAGE_HW_COMPOSER |
600 GRALLOC_USAGE_EXTERNAL_DISP)) {
601 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
602 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER;
603 }
604
605 if ((swapchainImageUsage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) &&
606 device->u_gralloc != NULL) {
607 uint64_t front_rendering_usage = 0;
608 u_gralloc_get_front_rendering_usage(device->u_gralloc, &front_rendering_usage);
609 *grallocProducerUsage |= front_rendering_usage;
610 }
611
612 return VK_SUCCESS;
613 }
614 #endif
615
anv_GetSwapchainGrallocUsageANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)616 VkResult anv_GetSwapchainGrallocUsageANDROID(
617 VkDevice device_h,
618 VkFormat format,
619 VkImageUsageFlags imageUsage,
620 int* grallocUsage)
621 {
622 ANV_FROM_HANDLE(anv_device, device, device_h);
623 VkResult result;
624
625 *grallocUsage = 0;
626 mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
627
628 result = format_supported_with_usage(device_h, format, imageUsage);
629 if (result != VK_SUCCESS)
630 return result;
631
632 return setup_gralloc0_usage(device, format, imageUsage, grallocUsage);
633 }
634