• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "v3dv_private.h"
25 #include <hardware/gralloc.h>
26 
27 #if ANDROID_API_LEVEL >= 26
28 #include <hardware/gralloc1.h>
29 #endif
30 
31 #include "drm-uapi/drm_fourcc.h"
32 #include <hardware/hardware.h>
33 #include <hardware/hwvulkan.h>
34 
35 #include <vulkan/vk_android_native_buffer.h>
36 #include <vulkan/vk_icd.h>
37 
38 #include "vk_android.h"
39 #include "vulkan/util/vk_enum_defines.h"
40 
41 #include "util/libsync.h"
42 #include "util/log.h"
43 #include "util/os_file.h"
44 
45 static int
46 v3dv_hal_open(const struct hw_module_t *mod,
47               const char *id,
48               struct hw_device_t **dev);
49 static int
50 v3dv_hal_close(struct hw_device_t *dev);
51 
52 static_assert(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC, "");
53 
54 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
55    .common =
56      {
57        .tag = HARDWARE_MODULE_TAG,
58        .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
59        .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
60        .id = HWVULKAN_HARDWARE_MODULE_ID,
61        .name = "Broadcom Vulkan HAL",
62        .author = "Mesa3D",
63        .methods =
64          &(hw_module_methods_t) {
65            .open = v3dv_hal_open,
66          },
67      },
68 };
69 
70 /* If any bits in test_mask are set, then unset them and return true. */
71 static inline bool
unmask32(uint32_t * inout_mask,uint32_t test_mask)72 unmask32(uint32_t *inout_mask, uint32_t test_mask)
73 {
74    uint32_t orig_mask = *inout_mask;
75    *inout_mask &= ~test_mask;
76    return *inout_mask != orig_mask;
77 }
78 
79 static int
v3dv_hal_open(const struct hw_module_t * mod,const char * id,struct hw_device_t ** dev)80 v3dv_hal_open(const struct hw_module_t *mod,
81               const char *id,
82               struct hw_device_t **dev)
83 {
84    assert(mod == &HAL_MODULE_INFO_SYM.common);
85    assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
86 
87    hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
88    if (!hal_dev)
89       return -1;
90 
91    *hal_dev = (hwvulkan_device_t){
92       .common =
93         {
94           .tag = HARDWARE_DEVICE_TAG,
95           .version = HWVULKAN_DEVICE_API_VERSION_0_1,
96           .module = &HAL_MODULE_INFO_SYM.common,
97           .close = v3dv_hal_close,
98         },
99      .EnumerateInstanceExtensionProperties =
100         v3dv_EnumerateInstanceExtensionProperties,
101      .CreateInstance = v3dv_CreateInstance,
102      .GetInstanceProcAddr = v3dv_GetInstanceProcAddr,
103    };
104 
105    mesa_logi("v3dv: Warning: Android Vulkan implementation is experimental");
106 
107    *dev = &hal_dev->common;
108    return 0;
109 }
110 
111 static int
v3dv_hal_close(struct hw_device_t * dev)112 v3dv_hal_close(struct hw_device_t *dev)
113 {
114    /* hwvulkan.h claims that hw_device_t::close() is never called. */
115    return -1;
116 }
117 
118 VkResult
v3dv_gralloc_to_drm_explicit_layout(struct u_gralloc * gralloc,struct u_gralloc_buffer_handle * in_hnd,VkImageDrmFormatModifierExplicitCreateInfoEXT * out,VkSubresourceLayout * out_layouts,int max_planes)119 v3dv_gralloc_to_drm_explicit_layout(struct u_gralloc *gralloc,
120                                     struct u_gralloc_buffer_handle *in_hnd,
121                                     VkImageDrmFormatModifierExplicitCreateInfoEXT *out,
122                                     VkSubresourceLayout *out_layouts,
123                                     int max_planes)
124 {
125    struct u_gralloc_buffer_basic_info info;
126 
127    if (u_gralloc_get_buffer_basic_info(gralloc, in_hnd, &info) != 0)
128       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
129 
130    if (info.num_planes > max_planes)
131       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
132 
133    bool is_disjoint = false;
134    for (int i = 1; i < info.num_planes; i++) {
135       if (info.offsets[i] == 0) {
136          is_disjoint = true;
137          break;
138       }
139    }
140 
141    if (is_disjoint) {
142       /* We don't support disjoint planes yet */
143       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
144    }
145 
146    memset(out_layouts, 0, sizeof(*out_layouts) * info.num_planes);
147    memset(out, 0, sizeof(*out));
148 
149    out->sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT;
150    out->pPlaneLayouts = out_layouts;
151 
152    out->drmFormatModifier = info.modifier;
153    out->drmFormatModifierPlaneCount = info.num_planes;
154    for (int i = 0; i < info.num_planes; i++) {
155       out_layouts[i].offset = info.offsets[i];
156       out_layouts[i].rowPitch = info.strides[i];
157    }
158 
159    if (info.drm_fourcc == DRM_FORMAT_YVU420) {
160       /* Swap the U and V planes to match the VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM */
161       VkSubresourceLayout tmp = out_layouts[1];
162       out_layouts[1] = out_layouts[2];
163       out_layouts[2] = tmp;
164    }
165 
166    return VK_SUCCESS;
167 }
168 
169 VkResult
v3dv_import_native_buffer_fd(VkDevice device_h,int native_buffer_fd,const VkAllocationCallbacks * alloc,VkImage image_h)170 v3dv_import_native_buffer_fd(VkDevice device_h,
171                              int native_buffer_fd,
172                              const VkAllocationCallbacks *alloc,
173                              VkImage image_h)
174 {
175    VkResult result;
176 
177    VkDeviceMemory memory_h;
178 
179    const VkMemoryDedicatedAllocateInfo ded_alloc = {
180       .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
181       .pNext = NULL,
182       .buffer = VK_NULL_HANDLE,
183       .image = image_h
184    };
185 
186    const VkImportMemoryFdInfoKHR import_info = {
187       .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
188       .pNext = &ded_alloc,
189       .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
190       .fd = os_dupfd_cloexec(native_buffer_fd),
191    };
192 
193    result =
194       v3dv_AllocateMemory(device_h,
195                           &(VkMemoryAllocateInfo) {
196                              .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
197                              .pNext = &import_info,
198                              .allocationSize = lseek(native_buffer_fd, 0, SEEK_END),
199                              .memoryTypeIndex = 0,
200                           },
201                           alloc, &memory_h);
202 
203    if (result != VK_SUCCESS)
204       goto fail_create_image;
205 
206    VkBindImageMemoryInfo bind_info = {
207       .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
208       .image = image_h,
209       .memory = memory_h,
210       .memoryOffset = 0,
211    };
212    v3dv_BindImageMemory2(device_h, 1, &bind_info);
213 
214    return VK_SUCCESS;
215 
216 fail_create_image:
217    close(import_info.fd);
218 
219    return result;
220 }
221 
222 static VkResult
format_supported_with_usage(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage)223 format_supported_with_usage(VkDevice device_h,
224                             VkFormat format,
225                             VkImageUsageFlags imageUsage)
226 {
227    V3DV_FROM_HANDLE(v3dv_device, device, device_h);
228    struct v3dv_physical_device *phys_dev = device->pdevice;
229    VkPhysicalDevice phys_dev_h = v3dv_physical_device_to_handle(phys_dev);
230    VkResult result;
231 
232    const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
233       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
234       .format = format,
235       .type = VK_IMAGE_TYPE_2D,
236       .tiling = VK_IMAGE_TILING_OPTIMAL,
237       .usage = imageUsage,
238    };
239 
240    VkImageFormatProperties2 image_format_props = {
241       .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
242    };
243 
244    /* Check that requested format and usage are supported. */
245    result = v3dv_GetPhysicalDeviceImageFormatProperties2(
246       phys_dev_h, &image_format_info, &image_format_props);
247    if (result != VK_SUCCESS) {
248       return vk_errorf(device, result,
249                        "v3dv_GetPhysicalDeviceImageFormatProperties2 failed "
250                        "inside %s",
251                        __func__);
252    }
253 
254    return VK_SUCCESS;
255 }
256 
257 static VkResult
setup_gralloc0_usage(struct v3dv_device * device,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)258 setup_gralloc0_usage(struct v3dv_device *device,
259                      VkFormat format,
260                      VkImageUsageFlags imageUsage,
261                      int *grallocUsage)
262 {
263    if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
264                              VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
265       *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
266 
267    if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
268                              VK_IMAGE_USAGE_SAMPLED_BIT |
269                              VK_IMAGE_USAGE_STORAGE_BIT |
270                              VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
271       *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
272 
273    /* All VkImageUsageFlags not explicitly checked here are unsupported for
274     * gralloc swapchains.
275     */
276    if (imageUsage != 0) {
277       return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED,
278                        "unsupported VkImageUsageFlags(0x%x) for gralloc "
279                        "swapchain",
280                        imageUsage);
281    }
282 
283    /* Swapchain assumes direct displaying, therefore enable COMPOSER flag,
284     * In case format is not supported by display controller, gralloc will
285     * drop this flag and still allocate the buffer in VRAM
286     */
287    *grallocUsage |= GRALLOC_USAGE_HW_COMPOSER;
288 
289    if (*grallocUsage == 0)
290       return VK_ERROR_FORMAT_NOT_SUPPORTED;
291 
292    return VK_SUCCESS;
293 }
294 
295 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_GetSwapchainGrallocUsageANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,int * grallocUsage)296 v3dv_GetSwapchainGrallocUsageANDROID(VkDevice device_h,
297                                      VkFormat format,
298                                      VkImageUsageFlags imageUsage,
299                                      int *grallocUsage)
300 {
301    V3DV_FROM_HANDLE(v3dv_device, device, device_h);
302    VkResult result;
303 
304    result = format_supported_with_usage(device_h, format, imageUsage);
305    if (result != VK_SUCCESS)
306       return result;
307 
308    *grallocUsage = 0;
309    return setup_gralloc0_usage(device, format, imageUsage, grallocUsage);
310 }
311 
312 #if ANDROID_API_LEVEL >= 26
313 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h,VkFormat format,VkImageUsageFlags imageUsage,VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,uint64_t * grallocConsumerUsage,uint64_t * grallocProducerUsage)314 v3dv_GetSwapchainGrallocUsage2ANDROID(
315    VkDevice device_h,
316    VkFormat format,
317    VkImageUsageFlags imageUsage,
318    VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
319    uint64_t *grallocConsumerUsage,
320    uint64_t *grallocProducerUsage)
321 {
322    V3DV_FROM_HANDLE(v3dv_device, device, device_h);
323    VkResult result;
324 
325    *grallocConsumerUsage = 0;
326    *grallocProducerUsage = 0;
327    mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
328 
329    result = format_supported_with_usage(device_h, format, imageUsage);
330    if (result != VK_SUCCESS)
331       return result;
332 
333    int32_t grallocUsage = 0;
334    result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage);
335    if (result != VK_SUCCESS)
336       return result;
337 
338    /* Setup gralloc1 usage flags from gralloc0 flags. */
339 
340    if (grallocUsage & GRALLOC_USAGE_HW_RENDER) {
341       *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
342    }
343 
344    if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) {
345       *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
346    }
347 
348    if (grallocUsage & GRALLOC_USAGE_HW_COMPOSER) {
349       /* GPU composing case */
350       *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
351       /* Hardware composing case */
352       *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER;
353    }
354 
355    if (swapchainImageUsage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
356       uint64_t front_rendering_usage = 0;
357       u_gralloc_get_front_rendering_usage(device->gralloc, &front_rendering_usage);
358       *grallocProducerUsage |= front_rendering_usage;
359    }
360 
361    return VK_SUCCESS;
362 }
363 #endif
364 
365 /* ----------------------------- AHardwareBuffer --------------------------- */
366 
367 static VkResult
get_ahb_buffer_format_properties2(VkDevice device_h,const struct AHardwareBuffer * buffer,VkAndroidHardwareBufferFormatProperties2ANDROID * pProperties)368 get_ahb_buffer_format_properties2(VkDevice device_h, const struct AHardwareBuffer *buffer,
369                                   VkAndroidHardwareBufferFormatProperties2ANDROID *pProperties)
370 {
371    V3DV_FROM_HANDLE(v3dv_device, device, device_h);
372 
373    /* Get a description of buffer contents . */
374    AHardwareBuffer_Desc desc;
375    AHardwareBuffer_describe(buffer, &desc);
376 
377    /* Verify description. */
378    const uint64_t gpu_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
379                               AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
380                               AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
381 
382    /* "Buffer must be a valid Android hardware buffer object with at least
383     * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
384     */
385    if (!(desc.usage & (gpu_usage)))
386       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
387 
388    /* Fill properties fields based on description. */
389    VkAndroidHardwareBufferFormatProperties2ANDROID *p = pProperties;
390 
391    p->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
392    p->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
393    p->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
394    p->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;
395 
396    p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
397    p->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
398 
399    p->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
400    p->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
401 
402    VkFormatProperties2 format_properties = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2};
403 
404    p->format = vk_ahb_format_to_image_format(desc.format);
405 
406    VkFormat external_format = p->format;
407 
408    if (p->format != VK_FORMAT_UNDEFINED)
409       goto finish;
410 
411    /* External format only case
412     *
413     * From vkGetAndroidHardwareBufferPropertiesANDROID spec:
414     * "If the Android hardware buffer has one of the formats listed in the Format
415     * Equivalence table (see spec.), then format must have the equivalent Vulkan
416     * format listed in the table. Otherwise, format may be VK_FORMAT_UNDEFINED,
417     * indicating the Android hardware buffer can only be used with an external format."
418     *
419     * From SKIA source code analysis: p->format MUST be VK_FORMAT_UNDEFINED, if the
420     * format is not in the Equivalence table.
421     */
422 
423    struct u_gralloc_buffer_handle gr_handle = {
424       .handle = AHardwareBuffer_getNativeHandle(buffer),
425       .pixel_stride = desc.stride,
426       .hal_format = desc.format,
427    };
428 
429    struct u_gralloc_buffer_basic_info info;
430 
431    if (u_gralloc_get_buffer_basic_info(device->gralloc, &gr_handle, &info) != 0)
432       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
433 
434    switch (info.drm_fourcc) {
435    case DRM_FORMAT_YVU420:
436       /* Assuming that U and V planes are swapped earlier */
437       external_format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
438       break;
439    case DRM_FORMAT_NV12:
440       external_format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
441       break;
442    default:;
443       mesa_loge("Unsupported external DRM format: %d", info.drm_fourcc);
444       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
445    }
446 
447    struct u_gralloc_buffer_color_info color_info;
448    if (u_gralloc_get_buffer_color_info(device->gralloc, &gr_handle, &color_info) == 0) {
449       switch (color_info.yuv_color_space) {
450       case __DRI_YUV_COLOR_SPACE_ITU_REC601:
451          p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
452          break;
453       case __DRI_YUV_COLOR_SPACE_ITU_REC709:
454          p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
455          break;
456       case __DRI_YUV_COLOR_SPACE_ITU_REC2020:
457          p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020;
458          break;
459       default:
460          break;
461       }
462 
463       p->suggestedYcbcrRange = (color_info.sample_range == __DRI_YUV_NARROW_RANGE) ?
464          VK_SAMPLER_YCBCR_RANGE_ITU_NARROW : VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
465       p->suggestedXChromaOffset = (color_info.horizontal_siting == __DRI_YUV_CHROMA_SITING_0_5) ?
466          VK_CHROMA_LOCATION_MIDPOINT : VK_CHROMA_LOCATION_COSITED_EVEN;
467       p->suggestedYChromaOffset = (color_info.vertical_siting == __DRI_YUV_CHROMA_SITING_0_5) ?
468          VK_CHROMA_LOCATION_MIDPOINT : VK_CHROMA_LOCATION_COSITED_EVEN;
469    }
470 
471 finish:
472 
473    v3dv_GetPhysicalDeviceFormatProperties2(v3dv_physical_device_to_handle(device->pdevice),
474                                            external_format, &format_properties);
475 
476    /* v3dv doesn't support direct sampling from linear images but has a logic to copy
477     * from linear to tiled images implicitly before sampling. Therefore expose optimal
478     * features for both linear and optimal tiling.
479     */
480    p->formatFeatures = format_properties.formatProperties.optimalTilingFeatures;
481    p->externalFormat = external_format;
482 
483    /* From vkGetAndroidHardwareBufferPropertiesANDROID spec:
484     * "The formatFeatures member *must* include
485     *  VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT and at least one of
486     *  VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT or
487     *  VK_FORMAT_FEATURE_2_COSITED_CHROMA_SAMPLES_BIT"
488     */
489    p->formatFeatures |= VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
490 
491    return VK_SUCCESS;
492 }
493 
494 VkResult
v3dv_GetAndroidHardwareBufferPropertiesANDROID(VkDevice device_h,const struct AHardwareBuffer * buffer,VkAndroidHardwareBufferPropertiesANDROID * pProperties)495 v3dv_GetAndroidHardwareBufferPropertiesANDROID(VkDevice device_h,
496                                                const struct AHardwareBuffer *buffer,
497                                                VkAndroidHardwareBufferPropertiesANDROID *pProperties)
498 {
499    V3DV_FROM_HANDLE(v3dv_device, dev, device_h);
500    struct v3dv_physical_device *pdevice = dev->pdevice;
501 
502    VkResult result;
503 
504    VkAndroidHardwareBufferFormatPropertiesANDROID *format_prop =
505       vk_find_struct(pProperties->pNext, ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
506 
507    /* Fill format properties of an Android hardware buffer. */
508    if (format_prop) {
509       VkAndroidHardwareBufferFormatProperties2ANDROID format_prop2 = {
510          .sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID,
511       };
512       result = get_ahb_buffer_format_properties2(device_h, buffer, &format_prop2);
513       if (result != VK_SUCCESS)
514          return result;
515 
516       format_prop->format                 = format_prop2.format;
517       format_prop->externalFormat         = format_prop2.externalFormat;
518       format_prop->formatFeatures         =
519          vk_format_features2_to_features(format_prop2.formatFeatures);
520       format_prop->samplerYcbcrConversionComponents =
521          format_prop2.samplerYcbcrConversionComponents;
522       format_prop->suggestedYcbcrModel    = format_prop2.suggestedYcbcrModel;
523       format_prop->suggestedYcbcrRange    = format_prop2.suggestedYcbcrRange;
524       format_prop->suggestedXChromaOffset = format_prop2.suggestedXChromaOffset;
525       format_prop->suggestedYChromaOffset = format_prop2.suggestedYChromaOffset;
526    }
527 
528    VkAndroidHardwareBufferFormatProperties2ANDROID *format_prop2 =
529       vk_find_struct(pProperties->pNext, ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID);
530    if (format_prop2) {
531       result = get_ahb_buffer_format_properties2(device_h, buffer, format_prop2);
532       if (result != VK_SUCCESS)
533          return result;
534    }
535 
536    const native_handle_t *handle = AHardwareBuffer_getNativeHandle(buffer);
537    assert(handle && handle->numFds > 0);
538    pProperties->allocationSize = lseek(handle->data[0], 0, SEEK_END);
539 
540    /* All memory types. */
541    pProperties->memoryTypeBits = (1u << pdevice->memory.memoryTypeCount) - 1;
542 
543    return VK_SUCCESS;
544 }
545