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