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 #include <drm_fourcc.h>
31 #define DRM_FORMAT_YVU420_ANDROID fourcc_code('9', '9', '9', '7')
32 #ifdef VK_USE_PLATFORM_ANDROID_KHR
33 #include <system/graphics.h>
34 #else
35 /* See system/graphics.h. */
36 enum {
37 HAL_PIXEL_FORMAT_YV12 = 842094169,
38 };
39 #endif
40
41 #if !defined(__INTRODUCED_IN)
42 #define __INTRODUCED_IN(__api_level) /* nothing */
43 #endif
44 #include <vndk/hardware_buffer.h>
45 #include <vulkan/vulkan.h>
46 #include "util/log.h"
47
48 namespace gfxstream {
49 namespace vk {
50
51 /* See i915_private_android_types.h in minigbm. */
52 #define HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL 0x100
53
54 // TODO(b/167698976): We should not use OMX_COLOR_FormatYUV420Planar
55 // but we seem to miss a format translation somewhere.
56
57 #define OMX_COLOR_FormatYUV420Planar 0x13
58
vk_format_from_fourcc(unsigned fourcc_format)59 static inline VkFormat vk_format_from_fourcc(unsigned fourcc_format) {
60 switch (fourcc_format) {
61 case DRM_FORMAT_R8:
62 return VK_FORMAT_R8_UNORM;
63 case DRM_FORMAT_ABGR8888:
64 return VK_FORMAT_R8G8B8A8_UNORM;
65 case DRM_FORMAT_XBGR8888:
66 return VK_FORMAT_R8G8B8A8_UNORM;
67 case DRM_FORMAT_BGR888:
68 return VK_FORMAT_R8G8B8_UNORM;
69 case DRM_FORMAT_RGB565:
70 return VK_FORMAT_R5G6B5_UNORM_PACK16;
71 case DRM_FORMAT_ABGR16161616F:
72 return VK_FORMAT_R16G16B16A16_SFLOAT;
73 case DRM_FORMAT_ABGR2101010:
74 return VK_FORMAT_A2B10G10R10_UNORM_PACK32;
75 case DRM_FORMAT_P010:
76 return VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16;
77 case HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL:
78 case DRM_FORMAT_NV12:
79 case DRM_FORMAT_NV21:
80 return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
81 case DRM_FORMAT_YUV420:
82 case DRM_FORMAT_YVU420_ANDROID:
83 case DRM_FORMAT_YVU420:
84 return VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
85 default:
86 return VK_FORMAT_UNDEFINED;
87 }
88 }
89
android_format_from_vk(VkFormat vk_format)90 static inline unsigned android_format_from_vk(VkFormat vk_format) {
91 switch (vk_format) {
92 case VK_FORMAT_R8_UNORM:
93 return AHARDWAREBUFFER_FORMAT_R8_UNORM;
94 case VK_FORMAT_R8G8B8A8_UNORM:
95 return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
96 case VK_FORMAT_R8G8B8_UNORM:
97 return AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
98 case VK_FORMAT_R5G6B5_UNORM_PACK16:
99 return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
100 case VK_FORMAT_R16G16B16A16_SFLOAT:
101 return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
102 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
103 return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
104 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
105 return HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL;
106 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
107 return HAL_PIXEL_FORMAT_YV12;
108 default:
109 return AHARDWAREBUFFER_FORMAT_BLOB;
110 }
111 }
112
android_format_is_yuv(unsigned android_format)113 static inline bool android_format_is_yuv(unsigned android_format) {
114 switch (android_format) {
115 case AHARDWAREBUFFER_FORMAT_BLOB:
116 case AHARDWAREBUFFER_FORMAT_R8_UNORM:
117 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
118 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
119 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
120 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
121 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
122 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
123 case AHARDWAREBUFFER_FORMAT_D16_UNORM:
124 case AHARDWAREBUFFER_FORMAT_D24_UNORM:
125 case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
126 case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
127 case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
128 case AHARDWAREBUFFER_FORMAT_S8_UINT:
129 return false;
130 case HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL:
131 case OMX_COLOR_FormatYUV420Planar:
132 case HAL_PIXEL_FORMAT_YV12:
133 #if __ANDROID_API__ >= 30
134 case AHARDWAREBUFFER_FORMAT_YCbCr_P010:
135 #endif
136 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
137 return true;
138 default:
139 mesa_loge("%s: unhandled format: %d", __FUNCTION__, android_format);
140 return false;
141 }
142 }
143
vk_format_aspects(VkFormat format)144 static inline VkImageAspectFlags vk_format_aspects(VkFormat format) {
145 switch (format) {
146 case VK_FORMAT_UNDEFINED:
147 return 0;
148
149 case VK_FORMAT_S8_UINT:
150 return VK_IMAGE_ASPECT_STENCIL_BIT;
151
152 case VK_FORMAT_D16_UNORM_S8_UINT:
153 case VK_FORMAT_D24_UNORM_S8_UINT:
154 case VK_FORMAT_D32_SFLOAT_S8_UINT:
155 return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
156
157 case VK_FORMAT_D16_UNORM:
158 case VK_FORMAT_X8_D24_UNORM_PACK32:
159 case VK_FORMAT_D32_SFLOAT:
160 return VK_IMAGE_ASPECT_DEPTH_BIT;
161
162 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
163 case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
164 case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
165 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
166 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
167 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
168 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
169 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
170 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
171 case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
172 case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
173 case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
174 return (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT |
175 VK_IMAGE_ASPECT_PLANE_2_BIT);
176
177 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
178 case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
179 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
180 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
181 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
182 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
183 case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
184 case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
185 return (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT);
186
187 default:
188 return VK_IMAGE_ASPECT_COLOR_BIT;
189 }
190 }
191
vk_format_is_color(VkFormat format)192 static inline bool vk_format_is_color(VkFormat format) {
193 return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
194 }
195
vk_format_is_depth_or_stencil(VkFormat format)196 static inline bool vk_format_is_depth_or_stencil(VkFormat format) {
197 const VkImageAspectFlags aspects = vk_format_aspects(format);
198 return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
199 }
200
vk_format_has_depth(VkFormat format)201 static inline bool vk_format_has_depth(VkFormat format) {
202 const VkImageAspectFlags aspects = vk_format_aspects(format);
203 return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
204 }
205
206 } // namespace vk
207 } // namespace gfxstream
208
209 #endif /* VK_FORMAT_INFO_H */
210