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 = 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->physical, 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 #endif
284
285 /*
286 * Called from anv_AllocateMemory when import AHardwareBuffer.
287 */
288 VkResult
anv_import_ahw_memory(VkDevice device_h,struct anv_device_memory * mem)289 anv_import_ahw_memory(VkDevice device_h,
290 struct anv_device_memory *mem)
291 {
292 #if ANDROID_API_LEVEL >= 26
293 ANV_FROM_HANDLE(anv_device, device, device_h);
294
295 /* Import from AHardwareBuffer to anv_device_memory. */
296 const native_handle_t *handle =
297 AHardwareBuffer_getNativeHandle(mem->vk.ahardware_buffer);
298
299 /* NOTE - We support buffers with only one handle but do not error on
300 * multiple handle case. Reason is that we want to support YUV formats
301 * where we have many logical planes but they all point to the same
302 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
303 */
304 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
305 if (dma_buf < 0)
306 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
307
308 VkResult result = anv_device_import_bo(device, dma_buf,
309 ANV_BO_ALLOC_EXTERNAL,
310 0 /* client_address */,
311 &mem->bo);
312 assert(result == VK_SUCCESS);
313
314 return VK_SUCCESS;
315 #else
316 return VK_ERROR_EXTENSION_NOT_PRESENT;
317 #endif
318 }
319
320 VkResult
anv_android_get_tiling(struct anv_device * device,struct u_gralloc_buffer_handle * gr_handle,enum isl_tiling * tiling_out)321 anv_android_get_tiling(struct anv_device *device,
322 struct u_gralloc_buffer_handle *gr_handle,
323 enum isl_tiling *tiling_out)
324 {
325 assert(device->u_gralloc);
326
327 struct u_gralloc_buffer_basic_info buf_info;
328 if (u_gralloc_get_buffer_basic_info(device->u_gralloc, gr_handle, &buf_info))
329 return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
330 "failed to get tiling from gralloc buffer info");
331
332 const struct isl_drm_modifier_info *mod_info =
333 isl_drm_modifier_get_info(buf_info.modifier);
334 if (!mod_info) {
335 return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
336 "invalid drm modifier from VkNativeBufferANDROID "
337 "gralloc buffer info 0x%"PRIx64"", buf_info.modifier);
338 }
339
340 *tiling_out = mod_info->tiling;
341 return VK_SUCCESS;
342 }
343
344 VkResult
anv_image_init_from_gralloc(struct anv_device * device,struct anv_image * image,const VkImageCreateInfo * base_info,const VkNativeBufferANDROID * gralloc_info)345 anv_image_init_from_gralloc(struct anv_device *device,
346 struct anv_image *image,
347 const VkImageCreateInfo *base_info,
348 const VkNativeBufferANDROID *gralloc_info)
349 {
350 struct anv_bo *bo = NULL;
351 VkResult result;
352
353 struct anv_image_create_info anv_info = {
354 .vk_info = base_info,
355 .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
356 };
357
358 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
359 * must exceed that of the gralloc handle, and we do not own the gralloc
360 * handle.
361 */
362 int dma_buf = gralloc_info->handle->data[0];
363
364 /* If this function fails and if the imported bo was resident in the cache,
365 * we should avoid updating the bo's flags. Therefore, we defer updating
366 * the flags until success is certain.
367 *
368 */
369 result = anv_device_import_bo(device, dma_buf,
370 ANV_BO_ALLOC_EXTERNAL,
371 0 /* client_address */,
372 &bo);
373 if (result != VK_SUCCESS) {
374 return vk_errorf(device, result,
375 "failed to import dma-buf from VkNativeBufferANDROID");
376 }
377
378 enum isl_tiling tiling;
379 if (device->u_gralloc) {
380 struct u_gralloc_buffer_handle gr_handle = {
381 .handle = gralloc_info->handle,
382 .hal_format = gralloc_info->format,
383 .pixel_stride = gralloc_info->stride,
384 };
385 result = anv_android_get_tiling(device, &gr_handle, &tiling);
386 if (result != VK_SUCCESS)
387 return result;
388 } else {
389 /* Fallback to get_tiling API. */
390 result = anv_device_get_bo_tiling(device, bo, &tiling);
391 if (result != VK_SUCCESS) {
392 return vk_errorf(device, result,
393 "failed to get tiling from VkNativeBufferANDROID");
394 }
395 }
396 anv_info.isl_tiling_flags = 1u << tiling;
397
398 anv_info.stride = gralloc_info->stride;
399
400 result = anv_image_init(device, image, &anv_info);
401 if (result != VK_SUCCESS)
402 goto fail_init;
403
404 VkMemoryRequirements2 mem_reqs = {
405 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
406 };
407
408 anv_image_get_memory_requirements(device, image, image->vk.aspects,
409 &mem_reqs);
410
411 VkDeviceSize aligned_image_size =
412 align64(mem_reqs.memoryRequirements.size,
413 mem_reqs.memoryRequirements.alignment);
414
415 if (bo->size < aligned_image_size) {
416 result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
417 "dma-buf from VkNativeBufferANDROID is too small for "
418 "VkImage: %"PRIu64"B < %"PRIu64"B",
419 bo->size, aligned_image_size);
420 goto fail_size;
421 }
422
423 assert(!image->disjoint);
424 assert(image->n_planes == 1);
425 assert(image->planes[0].primary_surface.memory_range.binding ==
426 ANV_IMAGE_MEMORY_BINDING_MAIN);
427 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo == NULL);
428 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.offset == 0);
429 image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo = bo;
430 image->from_gralloc = true;
431
432 return VK_SUCCESS;
433
434 fail_size:
435 anv_image_finish(image);
436 fail_init:
437 anv_device_release_bo(device, bo);
438
439 return result;
440 }
441
442 VkResult
anv_image_bind_from_gralloc(struct anv_device * device,struct anv_image * image,const VkNativeBufferANDROID * gralloc_info)443 anv_image_bind_from_gralloc(struct anv_device *device,
444 struct anv_image *image,
445 const VkNativeBufferANDROID *gralloc_info)
446 {
447 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
448 * must exceed that of the gralloc handle, and we do not own the gralloc
449 * handle.
450 */
451 int dma_buf = gralloc_info->handle->data[0];
452
453 /* If this function fails and if the imported bo was resident in the cache,
454 * we should avoid updating the bo's flags. Therefore, we defer updating
455 * the flags until success is certain.
456 *
457 */
458 struct anv_bo *bo = NULL;
459 VkResult result = anv_device_import_bo(device, dma_buf,
460 ANV_BO_ALLOC_EXTERNAL,
461 0 /* client_address */,
462 &bo);
463 if (result != VK_SUCCESS) {
464 return vk_errorf(device, result,
465 "failed to import dma-buf from VkNativeBufferANDROID");
466 }
467
468 VkMemoryRequirements2 mem_reqs = {
469 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
470 };
471
472 anv_image_get_memory_requirements(device, image, image->vk.aspects,
473 &mem_reqs);
474
475 VkDeviceSize aligned_image_size =
476 align64(mem_reqs.memoryRequirements.size,
477 mem_reqs.memoryRequirements.alignment);
478
479 if (bo->size < aligned_image_size) {
480 result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
481 "dma-buf from VkNativeBufferANDROID is too small for "
482 "VkImage: %"PRIu64"B < %"PRIu64"B",
483 bo->size, aligned_image_size);
484 anv_device_release_bo(device, bo);
485 return result;
486 }
487
488 assert(!image->disjoint);
489 assert(image->n_planes == 1);
490 assert(image->planes[0].primary_surface.memory_range.binding ==
491 ANV_IMAGE_MEMORY_BINDING_MAIN);
492 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo == NULL);
493 assert(image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.offset == 0);
494 image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].address.bo = bo;
495 image->from_gralloc = true;
496
497 return VK_SUCCESS;
498 }
499
500 static VkResult
format_supported_with_usage(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage)501 format_supported_with_usage(VkDevice device_h, VkFormat format,
502 VkImageUsageFlags imageUsage)
503 {
504 ANV_FROM_HANDLE(anv_device, device, device_h);
505 VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(device->physical);
506 VkResult result;
507
508 const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
509 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
510 .format = format,
511 .type = VK_IMAGE_TYPE_2D,
512 .tiling = VK_IMAGE_TILING_OPTIMAL,
513 .usage = imageUsage,
514 };
515
516 VkImageFormatProperties2 image_format_props = {
517 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
518 };
519
520 /* Check that requested format and usage are supported. */
521 result = anv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h,
522 &image_format_info, &image_format_props);
523 if (result != VK_SUCCESS) {
524 return vk_errorf(device, result,
525 "anv_GetPhysicalDeviceImageFormatProperties2 failed "
526 "inside %s", __func__);
527 }
528 return VK_SUCCESS;
529 }
530
531
532 static VkResult
setup_gralloc0_usage(struct anv_device * device,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)533 setup_gralloc0_usage(struct anv_device *device, VkFormat format,
534 VkImageUsageFlags imageUsage, int *grallocUsage)
535 {
536 /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
537 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
538 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
539 *
540 * TODO(jessehall): I think these are right, but haven't thought hard
541 * about it. Do we need to query the driver for support of any of
542 * these?
543 *
544 * Any disagreement between this function and the hardcoded
545 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
546 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
547 */
548
549 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
550 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
551 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
552
553 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
554 VK_IMAGE_USAGE_SAMPLED_BIT |
555 VK_IMAGE_USAGE_STORAGE_BIT |
556 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
557 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
558
559 /* All VkImageUsageFlags not explicitly checked here are unsupported for
560 * gralloc swapchains.
561 */
562 if (imageUsage != 0) {
563 return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED,
564 "unsupported VkImageUsageFlags(0x%x) for gralloc "
565 "swapchain", imageUsage);
566 }
567
568 /* The below formats support GRALLOC_USAGE_HW_FB (that is, display
569 * scanout). This short list of formats is univserally supported on Intel
570 * but is incomplete. The full set of supported formats is dependent on
571 * kernel and hardware.
572 *
573 * FINISHME: Advertise all display-supported formats.
574 */
575 switch (format) {
576 case VK_FORMAT_B8G8R8A8_UNORM:
577 case VK_FORMAT_R5G6B5_UNORM_PACK16:
578 case VK_FORMAT_R8G8B8A8_UNORM:
579 case VK_FORMAT_R8G8B8A8_SRGB:
580 *grallocUsage |= GRALLOC_USAGE_HW_FB |
581 GRALLOC_USAGE_HW_COMPOSER |
582 GRALLOC_USAGE_EXTERNAL_DISP;
583 break;
584 default:
585 mesa_logw("%s: unsupported format=%d", __func__, format);
586 }
587
588 if (*grallocUsage == 0)
589 return VK_ERROR_FORMAT_NOT_SUPPORTED;
590
591 return VK_SUCCESS;
592 }
593
594 #if ANDROID_API_LEVEL >= 26
anv_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,uint64_t * grallocConsumerUsage,uint64_t * grallocProducerUsage)595 VkResult anv_GetSwapchainGrallocUsage2ANDROID(
596 VkDevice device_h,
597 VkFormat format,
598 VkImageUsageFlags imageUsage,
599 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
600 uint64_t* grallocConsumerUsage,
601 uint64_t* grallocProducerUsage)
602 {
603 ANV_FROM_HANDLE(anv_device, device, device_h);
604 VkResult result;
605
606 *grallocConsumerUsage = 0;
607 *grallocProducerUsage = 0;
608 mesa_logd("%s: format=%d, usage=0x%x, swapchainUsage=0x%x", __func__, format,
609 imageUsage, swapchainImageUsage);
610
611 result = format_supported_with_usage(device_h, format, imageUsage);
612 if (result != VK_SUCCESS)
613 return result;
614
615 int32_t grallocUsage = 0;
616 result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage);
617 if (result != VK_SUCCESS)
618 return result;
619
620 /* Setup gralloc1 usage flags from gralloc0 flags. */
621
622 if (grallocUsage & GRALLOC_USAGE_HW_RENDER) {
623 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
624 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET;
625 }
626
627 if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) {
628 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
629 }
630
631 if (grallocUsage & (GRALLOC_USAGE_HW_FB |
632 GRALLOC_USAGE_HW_COMPOSER |
633 GRALLOC_USAGE_EXTERNAL_DISP)) {
634 *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
635 *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER;
636 }
637
638 if ((swapchainImageUsage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) &&
639 device->u_gralloc != NULL) {
640 uint64_t front_rendering_usage = 0;
641 u_gralloc_get_front_rendering_usage(device->u_gralloc, &front_rendering_usage);
642 *grallocProducerUsage |= front_rendering_usage;
643 }
644
645 return VK_SUCCESS;
646 }
647 #endif
648
anv_GetSwapchainGrallocUsageANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)649 VkResult anv_GetSwapchainGrallocUsageANDROID(
650 VkDevice device_h,
651 VkFormat format,
652 VkImageUsageFlags imageUsage,
653 int* grallocUsage)
654 {
655 ANV_FROM_HANDLE(anv_device, device, device_h);
656 VkResult result;
657
658 *grallocUsage = 0;
659 mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
660
661 result = format_supported_with_usage(device_h, format, imageUsage);
662 if (result != VK_SUCCESS)
663 return result;
664
665 return setup_gralloc0_usage(device, format, imageUsage, grallocUsage);
666 }
667