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