1 /*
2  * Copyright © 2016 Intel Corporation
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 #ifndef VK_FORMAT_INFO_H
25 #define VK_FORMAT_INFO_H
26 
27 #include <stdbool.h>
28 #include <vulkan/vulkan.h>
29 
30 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
31 #include <vndk/hardware_buffer.h>
32 /* See i915_private_android_types.h in minigbm. */
33 #define HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL 0x100
34 
35 enum {
36    /* Usage bit equal to GRALLOC_USAGE_HW_CAMERA_MASK */
37    AHARDWAREBUFFER_USAGE_CAMERA_MASK = 0x00060000U,
38 };
39 
40 static inline VkFormat
vk_format_from_android(unsigned android_format,unsigned android_usage)41 vk_format_from_android(unsigned android_format, unsigned android_usage)
42 {
43    switch (android_format) {
44    case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
45       return VK_FORMAT_R8G8B8A8_UNORM;
46    case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
47    case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
48       return VK_FORMAT_R8G8B8_UNORM;
49    case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
50       return VK_FORMAT_R5G6B5_UNORM_PACK16;
51    case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
52       return VK_FORMAT_R16G16B16A16_SFLOAT;
53    case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
54       return VK_FORMAT_A2B10G10R10_UNORM_PACK32;
55    case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
56    case HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL:
57       return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
58    case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED:
59       if (android_usage & AHARDWAREBUFFER_USAGE_CAMERA_MASK)
60          return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
61       else
62          return VK_FORMAT_R8G8B8_UNORM;
63    case AHARDWAREBUFFER_FORMAT_BLOB:
64    default:
65       return VK_FORMAT_UNDEFINED;
66    }
67 }
68 
69 static inline unsigned
android_format_from_vk(unsigned vk_format)70 android_format_from_vk(unsigned vk_format)
71 {
72    switch (vk_format) {
73    case VK_FORMAT_R8G8B8A8_UNORM:
74       return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
75    case VK_FORMAT_R8G8B8_UNORM:
76       return AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
77    case VK_FORMAT_R5G6B5_UNORM_PACK16:
78       return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
79    case VK_FORMAT_R16G16B16A16_SFLOAT:
80       return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
81    case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
82       return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
83    case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
84 #ifdef HAVE_CROS_GRALLOC
85       return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
86 #else
87       return HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL;
88 #endif
89    default:
90       return AHARDWAREBUFFER_FORMAT_BLOB;
91    }
92 }
93 #endif
94 
95 static inline VkImageAspectFlags
vk_format_aspects(VkFormat format)96 vk_format_aspects(VkFormat format)
97 {
98    switch (format) {
99    case VK_FORMAT_UNDEFINED:
100       return 0;
101 
102    case VK_FORMAT_S8_UINT:
103       return VK_IMAGE_ASPECT_STENCIL_BIT;
104 
105    case VK_FORMAT_D16_UNORM_S8_UINT:
106    case VK_FORMAT_D24_UNORM_S8_UINT:
107    case VK_FORMAT_D32_SFLOAT_S8_UINT:
108       return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
109 
110    case VK_FORMAT_D16_UNORM:
111    case VK_FORMAT_X8_D24_UNORM_PACK32:
112    case VK_FORMAT_D32_SFLOAT:
113       return VK_IMAGE_ASPECT_DEPTH_BIT;
114 
115    case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
116    case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
117    case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
118    case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
119    case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
120    case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
121    case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
122    case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
123    case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
124    case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
125    case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
126    case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
127       return (VK_IMAGE_ASPECT_PLANE_0_BIT |
128               VK_IMAGE_ASPECT_PLANE_1_BIT |
129               VK_IMAGE_ASPECT_PLANE_2_BIT);
130 
131    case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
132    case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
133    case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
134    case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
135    case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
136    case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
137    case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
138    case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
139       return (VK_IMAGE_ASPECT_PLANE_0_BIT |
140               VK_IMAGE_ASPECT_PLANE_1_BIT);
141 
142    default:
143       return VK_IMAGE_ASPECT_COLOR_BIT;
144    }
145 }
146 
147 static inline bool
vk_format_is_color(VkFormat format)148 vk_format_is_color(VkFormat format)
149 {
150    return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
151 }
152 
153 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)154 vk_format_is_depth_or_stencil(VkFormat format)
155 {
156    const VkImageAspectFlags aspects = vk_format_aspects(format);
157    return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
158 }
159 
160 static inline bool
vk_format_has_depth(VkFormat format)161 vk_format_has_depth(VkFormat format)
162 {
163    const VkImageAspectFlags aspects = vk_format_aspects(format);
164    return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
165 }
166 
167 static inline bool
vk_format_has_stencil(VkFormat format)168 vk_format_has_stencil(VkFormat format)
169 {
170    const VkImageAspectFlags aspects = vk_format_aspects(format);
171    return aspects & VK_IMAGE_ASPECT_STENCIL_BIT;
172 }
173 
174 #endif /* VK_FORMAT_INFO_H */
175