1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 2017 The Android Open Source Project.
10 Copyright (C) 2018-2019 Aurora Free Open Source Software.
11 
12 This file is part of the Aurora Free Open Source Software. This
13 organization promote free and open source software that you can
14 redistribute and/or modify under the terms of the GNU Lesser General
15 Public License Version 3 as published by the Free Software Foundation or
16 (at your option) any later version approved by the Aurora Free Open Source
17 Software Organization. The license is available in the package root path
18 as 'LICENSE' file. Please review the following information to ensure the
19 GNU Lesser General Public License version 3 requirements will be met:
20 https://www.gnu.org/licenses/lgpl.html .
21 
22 Alternatively, this file may be used under the terms of the GNU General
23 Public License version 3 or later as published by the Free Software
24 Foundation. Please review the following information to ensure the GNU
25 General Public License requirements will be met:
26 https://www.gnu.org/licenses/gpl-3.0.html.
27 
28 NOTE: All products, services or anything associated to trademarks and
29 service marks used or referenced on this file are the property of their
30 respective companies/owners or its subsidiaries. Other names and brands
31 may be claimed as the property of others.
32 
33 For more info about intellectual property visit: aurorafoss.org or
34 directly send an email to: contact (at) aurorafoss.org .
35 
36 This file has bindings for an existing code, part of The Android Open Source
37 Project implementation. Check it out at android.googlesource.com .
38 */
39 
40 module aurorafw.android.platform.hardware_buffer;
41 
42 /**
43  * @addtogroup NativeActivity Native Activity
44  * @{
45  */
46 
47 /**
48  * @file aurorafw/android/platform/hardware_buffer.d
49  */
50 
51 version (Android):
52 extern (C):
53 @system:
54 nothrow:
55 @nogc:
56 
57 /**
58  * Buffer pixel formats.
59  */
60 enum
61 {
62     /**
63      * Corresponding formats:
64      *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
65      *   OpenGL ES: GL_RGBA8
66      */
67     AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM = 1,
68 
69     /**
70      * 32 bits per pixel, 8 bits per channel format where alpha values are
71      * ignored (always opaque).
72      * Corresponding formats:
73      *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
74      *   OpenGL ES: GL_RGB8
75      */
76     AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM = 2,
77 
78     /**
79      * Corresponding formats:
80      *   Vulkan: VK_FORMAT_R8G8B8_UNORM
81      *   OpenGL ES: GL_RGB8
82      */
83     AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM = 3,
84 
85     /**
86      * Corresponding formats:
87      *   Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16
88      *   OpenGL ES: GL_RGB565
89      */
90     AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM = 4,
91 
92     /**
93      * Corresponding formats:
94      *   Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT
95      *   OpenGL ES: GL_RGBA16F
96      */
97     AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT = 22,
98 
99     /**
100      * Corresponding formats:
101      *   Vulkan: VK_FORMAT_A2B10G10R10_UNORM_PACK32
102      *   OpenGL ES: GL_RGB10_A2
103      */
104     AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM = 43,
105 
106     /**
107      * An opaque binary blob format that must have height 1, with width equal to
108      * the buffer size in bytes.
109      */
110     AHARDWAREBUFFER_FORMAT_BLOB = 33,
111 
112     /**
113      * Corresponding formats:
114      *   Vulkan: VK_FORMAT_D16_UNORM
115      *   OpenGL ES: GL_DEPTH_COMPONENT16
116      */
117     AHARDWAREBUFFER_FORMAT_D16_UNORM = 48,
118 
119     /**
120      * Corresponding formats:
121      *   Vulkan: VK_FORMAT_X8_D24_UNORM_PACK32
122      *   OpenGL ES: GL_DEPTH_COMPONENT24
123      */
124     AHARDWAREBUFFER_FORMAT_D24_UNORM = 49,
125 
126     /**
127      * Corresponding formats:
128      *   Vulkan: VK_FORMAT_D24_UNORM_S8_UINT
129      *   OpenGL ES: GL_DEPTH24_STENCIL8
130      */
131     AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT = 50,
132 
133     /**
134      * Corresponding formats:
135      *   Vulkan: VK_FORMAT_D32_SFLOAT
136      *   OpenGL ES: GL_DEPTH_COMPONENT32F
137      */
138     AHARDWAREBUFFER_FORMAT_D32_FLOAT = 51,
139 
140     /**
141      * Corresponding formats:
142      *   Vulkan: VK_FORMAT_D32_SFLOAT_S8_UINT
143      *   OpenGL ES: GL_DEPTH32F_STENCIL8
144      */
145     AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT = 52,
146 
147     /**
148      * Corresponding formats:
149      *   Vulkan: VK_FORMAT_S8_UINT
150      *   OpenGL ES: GL_STENCIL_INDEX8
151      */
152     AHARDWAREBUFFER_FORMAT_S8_UINT = 53
153 }
154 
155 /**
156  * Buffer usage flags, specifying how the buffer will be accessed.
157  */
158 enum
159 {
160     /// The buffer will never be read by the CPU.
161     AHARDWAREBUFFER_USAGE_CPU_READ_NEVER = 0,
162     /// The buffer will sometimes be read by the CPU.
163     AHARDWAREBUFFER_USAGE_CPU_READ_RARELY = 2,
164     /// The buffer will often be read by the CPU.
165     AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN = 3,
166     /// CPU read value mask.
167     AHARDWAREBUFFER_USAGE_CPU_READ_MASK = 15,
168 
169     /// The buffer will never be written by the CPU.
170     AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER = 0,
171     /// The buffer will sometimes be written to by the CPU.
172     AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY = 32,
173     /// The buffer will often be written to by the CPU.
174     AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN = 48,
175     /// CPU write value mask.
176     AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK = 240,
177 
178     /// The buffer will be read from by the GPU as a texture.
179     AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE = 256,
180     /**
181      * The buffer will be written to by the GPU as a framebuffer attachment.
182      * Note that the name of this flag is somewhat misleading: it does not imply
183      * that the buffer contains a color format. A buffer with depth or stencil
184      * format that will be used as a framebuffer attachment should also have
185      * this flag.
186      */
187     AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT = 512,
188     /// The buffer must not be used outside of a protected hardware path.
189     AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT = 16384,
190     /// The buffer will be read by a hardware video encoder.
191     AHARDWAREBUFFER_USAGE_VIDEO_ENCODE = 65536,
192     /// The buffer will be used for direct writes from sensors.
193     AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA = 8388608,
194     /// The buffer will be used as a shader storage or uniform buffer object.
195     AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER = 16777216,
196     /// The buffer will be used as a cube map texture.
197     AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP = 33554432,
198     /// The buffer contains a complete mipmap hierarchy.
199     AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE = 67108864,
200 
201     AHARDWAREBUFFER_USAGE_VENDOR_0 = 1UL << 28,
202     AHARDWAREBUFFER_USAGE_VENDOR_1 = 1UL << 29,
203     AHARDWAREBUFFER_USAGE_VENDOR_2 = 1UL << 30,
204     AHARDWAREBUFFER_USAGE_VENDOR_3 = 1UL << 31,
205     AHARDWAREBUFFER_USAGE_VENDOR_4 = 1UL << 48,
206     AHARDWAREBUFFER_USAGE_VENDOR_5 = 1UL << 49,
207     AHARDWAREBUFFER_USAGE_VENDOR_6 = 1UL << 50,
208     AHARDWAREBUFFER_USAGE_VENDOR_7 = 1UL << 51,
209     AHARDWAREBUFFER_USAGE_VENDOR_8 = 1UL << 52,
210     AHARDWAREBUFFER_USAGE_VENDOR_9 = 1UL << 53,
211     AHARDWAREBUFFER_USAGE_VENDOR_10 = 1UL << 54,
212     AHARDWAREBUFFER_USAGE_VENDOR_11 = 1UL << 55,
213     AHARDWAREBUFFER_USAGE_VENDOR_12 = 1UL << 56,
214     AHARDWAREBUFFER_USAGE_VENDOR_13 = 1UL << 57,
215     AHARDWAREBUFFER_USAGE_VENDOR_14 = 1UL << 58,
216     AHARDWAREBUFFER_USAGE_VENDOR_15 = 1UL << 59,
217     AHARDWAREBUFFER_USAGE_VENDOR_16 = 1UL << 60,
218     AHARDWAREBUFFER_USAGE_VENDOR_17 = 1UL << 61,
219     AHARDWAREBUFFER_USAGE_VENDOR_18 = 1UL << 62,
220     AHARDWAREBUFFER_USAGE_VENDOR_19 = 1UL << 63
221 }
222 
223 /**
224  * Buffer description. Used for allocating new buffers and querying parameters
225  * of existing ones.
226  */
227 struct AHardwareBuffer_Desc
228 {
229     uint width; ///< Width in pixels.
230     uint height; ///< Height in pixels.
231     uint layers; ///< Number of images in an image array.
232     uint format; ///< One of AHARDWAREBUFFER_FORMAT_*
233     ulong usage; ///< Combination of AHARDWAREBUFFER_USAGE_*
234     uint stride; ///< Row stride in pixels, ignored for AHardwareBuffer_allocate()
235     uint rfu0; ///< Initialize to zero, reserved for future use.
236     ulong rfu1; ///< Initialize to zero, reserved for future use.
237 }
238 
239 struct AHardwareBuffer;
240 
241 /**
242  * Allocates a buffer that backs an AHardwareBuffer using the passed
243  * AHardwareBuffer_Desc.
244  *
245  * \return 0 on success, or an error number of the allocation fails for
246  * any reason. The returned buffer has a reference count of 1.
247  */
248 int AHardwareBuffer_allocate (
249     const(AHardwareBuffer_Desc)* desc,
250     AHardwareBuffer** outBuffer);
251 /**
252  * Acquire a reference on the given AHardwareBuffer object.  This prevents the
253  * object from being deleted until the last reference is removed.
254  */
255 void AHardwareBuffer_acquire (AHardwareBuffer* buffer);
256 
257 /**
258  * Remove a reference that was previously acquired with
259  * AHardwareBuffer_acquire().
260  */
261 void AHardwareBuffer_release (AHardwareBuffer* buffer);
262 
263 /**
264  * Return a description of the AHardwareBuffer in the passed
265  * AHardwareBuffer_Desc struct.
266  */
267 void AHardwareBuffer_describe (
268     const(AHardwareBuffer)* buffer,
269     AHardwareBuffer_Desc* outDesc);
270 
271 /**
272  * Lock the AHardwareBuffer for reading or writing, depending on the usage flags
273  * passed.  This call may block if the hardware needs to finish rendering or if
274  * CPU caches need to be synchronized, or possibly for other implementation-
275  * specific reasons.  If fence is not negative, then it specifies a fence file
276  * descriptor that will be signaled when the buffer is locked, otherwise the
277  * caller will block until the buffer is available.
278  *
279  * If \a rect is not NULL, the caller promises to modify only data in the area
280  * specified by rect. If rect is NULL, the caller may modify the contents of the
281  * entire buffer.
282  *
283  * The content of the buffer outside of the specified rect is NOT modified
284  * by this call.
285  *
286  * The \a usage parameter may only specify AHARDWAREBUFFER_USAGE_CPU_*. If set,
287  * then outVirtualAddress is filled with the address of the buffer in virtual
288  * memory.
289  *
290  * THREADING CONSIDERATIONS:
291  *
292  * It is legal for several different threads to lock a buffer for read access;
293  * none of the threads are blocked.
294  *
295  * Locking a buffer simultaneously for write or read/write is undefined, but
296  * will neither terminate the process nor block the caller; AHardwareBuffer_lock
297  * may return an error or leave the buffer's content into an indeterminate
298  * state.
299  *
300  * \return 0 on success, -EINVAL if \a buffer is NULL or if the usage
301  * flags are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or an error
302  * number of the lock fails for any reason.
303  */
304 int AHardwareBuffer_lock (
305     AHardwareBuffer* buffer,
306     ulong usage,
307     int fence,
308     const(ARect)* rect,
309     void** outVirtualAddress);
310 
311 /**
312  * Unlock the AHardwareBuffer; must be called after all changes to the buffer
313  * are completed by the caller. If fence is not NULL then it will be set to a
314  * file descriptor that is signaled when all pending work on the buffer is
315  * completed. The caller is responsible for closing the fence when it is no
316  * longer needed.
317  *
318  * \return 0 on success, -EINVAL if \a buffer is NULL, or an error
319  * number if the unlock fails for any reason.
320  */
321 int AHardwareBuffer_unlock (AHardwareBuffer* buffer, int* fence);
322 
323 /**
324  * Send the AHardwareBuffer to an AF_UNIX socket.
325  *
326  * \return 0 on success, -EINVAL if \a buffer is NULL, or an error
327  * number if the operation fails for any reason.
328  */
329 int AHardwareBuffer_sendHandleToUnixSocket (const(AHardwareBuffer)* buffer, int socketFd);
330 
331 /**
332  * Receive the AHardwareBuffer from an AF_UNIX socket.
333  *
334  * \return 0 on success, -EINVAL if \a outBuffer is NULL, or an error
335  * number if the operation fails for any reason.
336  */
337 int AHardwareBuffer_recvHandleFromUnixSocket (int socketFd, AHardwareBuffer** outBuffer);
338 
339 // __ANDROID_API__ >= 26
340 
341 // ANDROID_HARDWARE_BUFFER_H
342 
343 /** @} */