1 /*
2  * Copyright © 2016 Intel Corporation
3  * Copyright © 2019 The Android Open Source Project
4  * Copyright © 2019 Google Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  */
25 
26 #ifndef VK_FORMAT_INFO_H
27 #define VK_FORMAT_INFO_H
28 
29 #include <stdbool.h>
30 #ifdef VK_USE_PLATFORM_ANDROID_KHR
31 #include <system/graphics.h>
32 #endif
33 #include <vulkan/vulkan.h>
34 #include <vndk/hardware_buffer.h>
35 
36 /* See i915_private_android_types.h in minigbm. */
37 #define HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL 0x100
38 
39 static inline VkFormat
vk_format_from_android(unsigned android_format)40 vk_format_from_android(unsigned android_format)
41 {
42    switch (android_format) {
43    case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
44       return VK_FORMAT_R8G8B8A8_UNORM;
45    case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
46       return VK_FORMAT_R8G8B8A8_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 HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL:
56       return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
57 #ifdef VK_USE_PLATFORM_ANDROID_KHR
58    case HAL_PIXEL_FORMAT_YV12:
59       // YUV converter will convert this format to R8G8B8A8
60       // TODO: should we use R8G8B8A8 for other YUV format as well?
61       return VK_FORMAT_R8G8B8A8_UNORM;
62    case AHARDWAREBUFFER_FORMAT_BLOB:
63 #endif
64    default:
65       return VK_FORMAT_UNDEFINED;
66    }
67 }
68 
69 static inline unsigned
android_format_from_vk(VkFormat vk_format)70 android_format_from_vk(VkFormat 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       return HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL;
85    default:
86       return AHARDWAREBUFFER_FORMAT_BLOB;
87    }
88 }
89 
90 static inline bool
android_format_is_yuv(unsigned android_format)91 android_format_is_yuv(unsigned android_format)
92 {
93    switch (android_format) {
94    case HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL:
95       return true;
96 
97    default:
98       return false;
99    }
100 }
101 
102 static inline VkImageAspectFlags
vk_format_aspects(VkFormat format)103 vk_format_aspects(VkFormat format)
104 {
105    switch (format) {
106    case VK_FORMAT_UNDEFINED:
107       return 0;
108 
109    case VK_FORMAT_S8_UINT:
110       return VK_IMAGE_ASPECT_STENCIL_BIT;
111 
112    case VK_FORMAT_D16_UNORM_S8_UINT:
113    case VK_FORMAT_D24_UNORM_S8_UINT:
114    case VK_FORMAT_D32_SFLOAT_S8_UINT:
115       return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
116 
117    case VK_FORMAT_D16_UNORM:
118    case VK_FORMAT_X8_D24_UNORM_PACK32:
119    case VK_FORMAT_D32_SFLOAT:
120       return VK_IMAGE_ASPECT_DEPTH_BIT;
121 
122    case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
123    case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
124    case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
125    case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
126    case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
127    case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
128    case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
129    case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
130    case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
131    case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
132    case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
133    case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
134       return (VK_IMAGE_ASPECT_PLANE_0_BIT |
135               VK_IMAGE_ASPECT_PLANE_1_BIT |
136               VK_IMAGE_ASPECT_PLANE_2_BIT);
137 
138    case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
139    case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
140    case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
141    case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
142    case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
143    case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
144    case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
145    case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
146       return (VK_IMAGE_ASPECT_PLANE_0_BIT |
147               VK_IMAGE_ASPECT_PLANE_1_BIT);
148 
149    default:
150       return VK_IMAGE_ASPECT_COLOR_BIT;
151    }
152 }
153 
154 static inline bool
vk_format_is_color(VkFormat format)155 vk_format_is_color(VkFormat format)
156 {
157    return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
158 }
159 
160 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)161 vk_format_is_depth_or_stencil(VkFormat format)
162 {
163    const VkImageAspectFlags aspects = vk_format_aspects(format);
164    return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
165 }
166 
167 static inline bool
vk_format_has_depth(VkFormat format)168 vk_format_has_depth(VkFormat format)
169 {
170    const VkImageAspectFlags aspects = vk_format_aspects(format);
171    return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
172 }
173 
174 #endif /* VK_FORMAT_INFO_H */
175