xref: /aosp_15_r20/external/angle/src/common/android_util.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // android_util.h: Utilities for the using the Android platform
8 
9 #ifndef COMMON_ANDROIDUTIL_H_
10 #define COMMON_ANDROIDUTIL_H_
11 
12 #include <EGL/egl.h>
13 #include <EGL/eglext.h>
14 
15 #include <array>
16 #include <cstdint>
17 #include <string>
18 
19 #include "angle_gl.h"
20 
21 struct ANativeWindowBuffer;
22 struct AHardwareBuffer;
23 
24 namespace angle
25 {
26 
27 namespace android
28 {
29 
30 // clang-format off
31 /**
32  * Buffer pixel formats mirrored from Android to avoid unnecessary complications
33  * when trying to keep the enums defined, but not redefined, across various build
34  * systems and across various releases/branches.
35  *
36  * Taken from
37  * https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/master/graphics/common/aidl/android/hardware/graphics/common/PixelFormat.aidl
38  */
39 enum {
40     /**
41      * Corresponding formats:
42      *   Android: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM
43      *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
44      *   OpenGL ES: GL_RGBA8
45      */
46     ANGLE_AHB_FORMAT_R8G8B8A8_UNORM           = 1,
47 
48     /**
49      * 32 bits per pixel, 8 bits per channel format where alpha values are
50      * ignored (always opaque).
51      *
52      * Corresponding formats:
53      *   Android: AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
54      *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
55      *   OpenGL ES: GL_RGB8
56      */
57     ANGLE_AHB_FORMAT_R8G8B8X8_UNORM           = 2,
58 
59     /**
60      * Corresponding formats:
61      *   Android: AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM
62      *   Vulkan: VK_FORMAT_R8G8B8_UNORM
63      *   OpenGL ES: GL_RGB8
64      */
65     ANGLE_AHB_FORMAT_R8G8B8_UNORM             = 3,
66 
67     /**
68      * Corresponding formats:
69      *   Android: AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM
70      *   Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16
71      *   OpenGL ES: GL_RGB565
72      */
73     ANGLE_AHB_FORMAT_R5G6B5_UNORM             = 4,
74 
75     /**
76      * Corresponding formats:
77      *   Android: AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM (deprecated)
78      */
79     ANGLE_AHB_FORMAT_B8G8R8A8_UNORM           = 5,
80 
81     /**
82      * Corresponding formats:
83      *   Android: AHARDWAREBUFFER_FORMAT_B5G5R5A1_UNORM (deprecated)
84      */
85     ANGLE_AHB_FORMAT_B5G5R5A1_UNORM           = 6,
86 
87     /**
88      * Corresponding formats:
89      *   Android: AHARDWAREBUFFER_FORMAT_B4G4R4A4_UNORM (deprecated)
90      */
91     ANGLE_AHB_FORMAT_B4G4R4A4_UNORM           = 7,
92 
93     /**
94      * Corresponding formats:
95      *   Android: AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT
96      *   Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT
97      *   OpenGL ES: GL_RGBA16F
98      */
99     ANGLE_AHB_FORMAT_R16G16B16A16_FLOAT       = 0x16,
100 
101     /**
102      * Corresponding formats:
103      *   Android: AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM
104      *   Vulkan: VK_FORMAT_A2B10G10R10_UNORM_PACK32
105      *   OpenGL ES: GL_RGB10_A2
106      */
107     ANGLE_AHB_FORMAT_R10G10B10A2_UNORM        = 0x2b,
108 
109     /**
110      * An opaque binary blob format that must have height 1, with width equal to
111      * the buffer size in bytes.
112      *
113      * Corresponding formats:
114      *   Android: AHARDWAREBUFFER_FORMAT_BLOB
115      */
116     ANGLE_AHB_FORMAT_BLOB                     = 0x21,
117 
118     /**
119      * Corresponding formats:
120      *   Android: AHARDWAREBUFFER_FORMAT_D16_UNORM
121      *   Vulkan: VK_FORMAT_D16_UNORM
122      *   OpenGL ES: GL_DEPTH_COMPONENT16
123      */
124     ANGLE_AHB_FORMAT_D16_UNORM                = 0x30,
125 
126     /**
127      * Corresponding formats:
128      *   Android: AHARDWAREBUFFER_FORMAT_D24_UNORM
129      *   Vulkan: VK_FORMAT_X8_D24_UNORM_PACK32
130      *   OpenGL ES: GL_DEPTH_COMPONENT24
131      */
132     ANGLE_AHB_FORMAT_D24_UNORM                = 0x31,
133 
134     /**
135      * Corresponding formats:
136      *   Android: AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT
137      *   Vulkan: VK_FORMAT_D24_UNORM_S8_UINT
138      *   OpenGL ES: GL_DEPTH24_STENCIL8
139      */
140     ANGLE_AHB_FORMAT_D24_UNORM_S8_UINT        = 0x32,
141 
142     /**
143      * Corresponding formats:
144      *   Android: AHARDWAREBUFFER_FORMAT_D32_FLOAT
145      *   Vulkan: VK_FORMAT_D32_SFLOAT
146      *   OpenGL ES: GL_DEPTH_COMPONENT32F
147      */
148     ANGLE_AHB_FORMAT_D32_FLOAT                = 0x33,
149 
150     /**
151      * Corresponding formats:
152      *   Android: AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT
153      *   Vulkan: VK_FORMAT_D32_SFLOAT_S8_UINT
154      *   OpenGL ES: GL_DEPTH32F_STENCIL8
155      */
156     ANGLE_AHB_FORMAT_D32_FLOAT_S8_UINT        = 0x34,
157 
158     /**
159      * Corresponding formats:
160      *   Android: AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT
161      *   Vulkan: VK_FORMAT_S8_UINT
162      *   OpenGL ES: GL_STENCIL_INDEX8
163      */
164     ANGLE_AHB_FORMAT_S8_UINT                  = 0x35,
165 
166     /**
167      * YUV 420 888 format.
168      * Must have an even width and height. Can be accessed in OpenGL
169      * shaders through an external sampler. Does not support mip-maps
170      * cube-maps or multi-layered textures.
171      *
172      * Corresponding formats:
173      *   Android: AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420
174      */
175     ANGLE_AHB_FORMAT_Y8Cb8Cr8_420             = 0x23,
176 
177     /**
178      * Corresponding formats:
179      *   Android: AHARDWAREBUFFER_FORMAT_YV12
180      *   Vulkan: VK_FORMAT_S8_UINT
181      *   OpenGL ES: GL_STENCIL_INDEX8
182      */
183     ANGLE_AHB_FORMAT_YV12                     = 0x32315659,
184 
185     /**
186      * Corresponding formats:
187      *   Android: AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED
188      *   Vulkan: VK_FORMAT_S8_UINT
189      *   OpenGL ES: GL_STENCIL_INDEX8
190      */
191     ANGLE_AHB_FORMAT_IMPLEMENTATION_DEFINED   = 0x22,
192 
193     /**
194      * Corresponding formats:
195      *   Android: AHARDWAREBUFFER_FORMAT_R8_UNORM
196      *   Vulkan: VK_FORMAT_R8_UNORM
197      *   OpenGL ES: GL_R8
198      */
199     ANGLE_AHB_FORMAT_R8_UNORM   = 0x38,
200 };
201 // clang-format on
202 
203 constexpr std::array<GLenum, 3> kSupportedSizedInternalFormats = {GL_RGBA8, GL_RGB8, GL_RGB565};
204 
205 ANativeWindowBuffer *ClientBufferToANativeWindowBuffer(EGLClientBuffer clientBuffer);
206 EGLClientBuffer AHardwareBufferToClientBuffer(const AHardwareBuffer *hardwareBuffer);
207 AHardwareBuffer *ClientBufferToAHardwareBuffer(EGLClientBuffer clientBuffer);
208 
209 EGLClientBuffer CreateEGLClientBufferFromAHardwareBuffer(int width,
210                                                          int height,
211                                                          int depth,
212                                                          int androidFormat,
213                                                          int usage);
214 
215 void GetANativeWindowBufferProperties(const ANativeWindowBuffer *buffer,
216                                       int *width,
217                                       int *height,
218                                       int *depth,
219                                       int *pixelFormat,
220                                       uint64_t *usage);
221 GLenum NativePixelFormatToGLInternalFormat(int pixelFormat);
222 int GLInternalFormatToNativePixelFormat(GLenum internalFormat);
223 
224 bool NativePixelFormatIsYUV(int pixelFormat);
225 
226 AHardwareBuffer *ANativeWindowBufferToAHardwareBuffer(ANativeWindowBuffer *windowBuffer);
227 
228 uint64_t GetAHBUsage(int eglNativeBufferUsage);
229 
230 bool GetSystemProperty(const char *propertyName, std::string *value);
231 static constexpr const char *kManufacturerSystemPropertyName = "ro.product.manufacturer";
232 static constexpr const char *kModelSystemPropertyName        = "ro.product.model";
233 static constexpr const char *kSDKSystemPropertyName          = "ro.build.version.sdk";
234 
235 }  // namespace android
236 }  // namespace angle
237 
238 #endif  // COMMON_ANDROIDUTIL_H_
239