1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2017 The Android Open Source Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5bf215546Sopenharmony_ci * you may not use this file except in compliance with the License.
6bf215546Sopenharmony_ci * You may obtain a copy of the License at
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
9bf215546Sopenharmony_ci *
10bf215546Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11bf215546Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12bf215546Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf215546Sopenharmony_ci * See the License for the specific language governing permissions and
14bf215546Sopenharmony_ci * limitations under the License.
15bf215546Sopenharmony_ci */
16bf215546Sopenharmony_ci
17bf215546Sopenharmony_ci/**
18bf215546Sopenharmony_ci * @file hardware_buffer.h
19bf215546Sopenharmony_ci * @brief API for native hardware buffers.
20bf215546Sopenharmony_ci */
21bf215546Sopenharmony_ci/**
22bf215546Sopenharmony_ci * @defgroup AHardwareBuffer Native Hardware Buffer
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci * AHardwareBuffer objects represent chunks of memory that can be
25bf215546Sopenharmony_ci * accessed by various hardware components in the system. It can be
26bf215546Sopenharmony_ci * easily converted to the Java counterpart
27bf215546Sopenharmony_ci * android.hardware.HardwareBuffer and passed between processes using
28bf215546Sopenharmony_ci * Binder. All operations involving AHardwareBuffer and HardwareBuffer
29bf215546Sopenharmony_ci * are zero-copy, i.e., passing AHardwareBuffer to another process
30bf215546Sopenharmony_ci * creates a shared view of the same region of memory.
31bf215546Sopenharmony_ci *
32bf215546Sopenharmony_ci * AHardwareBuffers can be bound to EGL/OpenGL and Vulkan primitives.
33bf215546Sopenharmony_ci * For EGL, use the extension function eglGetNativeClientBufferANDROID
34bf215546Sopenharmony_ci * to obtain an EGLClientBuffer and pass it directly to
35bf215546Sopenharmony_ci * eglCreateImageKHR. Refer to the EGL extensions
36bf215546Sopenharmony_ci * EGL_ANDROID_get_native_client_buffer and
37bf215546Sopenharmony_ci * EGL_ANDROID_image_native_buffer for more information. In Vulkan,
38bf215546Sopenharmony_ci * the contents of the AHardwareBuffer can be accessed as external
39bf215546Sopenharmony_ci * memory. See the VK_ANDROID_external_memory_android_hardware_buffer
40bf215546Sopenharmony_ci * extension for details.
41bf215546Sopenharmony_ci *
42bf215546Sopenharmony_ci * @{
43bf215546Sopenharmony_ci */
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci#ifndef ANDROID_HARDWARE_BUFFER_H
46bf215546Sopenharmony_ci#define ANDROID_HARDWARE_BUFFER_H
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#include <inttypes.h>
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci#include <sys/cdefs.h>
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci#include <android/rect.h>
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci__BEGIN_DECLS
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci/**
57bf215546Sopenharmony_ci * Buffer pixel formats.
58bf215546Sopenharmony_ci */
59bf215546Sopenharmony_cienum AHardwareBuffer_Format {
60bf215546Sopenharmony_ci    /**
61bf215546Sopenharmony_ci     * Corresponding formats:
62bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
63bf215546Sopenharmony_ci     *   OpenGL ES: GL_RGBA8
64bf215546Sopenharmony_ci     */
65bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM           = 1,
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci    /**
68bf215546Sopenharmony_ci     * 32 bits per pixel, 8 bits per channel format where alpha values are
69bf215546Sopenharmony_ci     * ignored (always opaque).
70bf215546Sopenharmony_ci     * Corresponding formats:
71bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
72bf215546Sopenharmony_ci     *   OpenGL ES: GL_RGB8
73bf215546Sopenharmony_ci     */
74bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM           = 2,
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci    /**
77bf215546Sopenharmony_ci     * Corresponding formats:
78bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_R8G8B8_UNORM
79bf215546Sopenharmony_ci     *   OpenGL ES: GL_RGB8
80bf215546Sopenharmony_ci     */
81bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM             = 3,
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci    /**
84bf215546Sopenharmony_ci     * Corresponding formats:
85bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16
86bf215546Sopenharmony_ci     *   OpenGL ES: GL_RGB565
87bf215546Sopenharmony_ci     */
88bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM             = 4,
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci    /**
91bf215546Sopenharmony_ci     * Corresponding formats:
92bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT
93bf215546Sopenharmony_ci     *   OpenGL ES: GL_RGBA16F
94bf215546Sopenharmony_ci     */
95bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT       = 0x16,
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci    /**
98bf215546Sopenharmony_ci     * Corresponding formats:
99bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_A2B10G10R10_UNORM_PACK32
100bf215546Sopenharmony_ci     *   OpenGL ES: GL_RGB10_A2
101bf215546Sopenharmony_ci     */
102bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM        = 0x2b,
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci    /**
105bf215546Sopenharmony_ci     * Opaque binary blob format.
106bf215546Sopenharmony_ci     * Must have height 1 and one layer, with width equal to the buffer
107bf215546Sopenharmony_ci     * size in bytes. Corresponds to Vulkan buffers and OpenGL buffer
108bf215546Sopenharmony_ci     * objects. Can be bound to the latter using GL_EXT_external_buffer.
109bf215546Sopenharmony_ci     */
110bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_BLOB                     = 0x21,
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci    /**
113bf215546Sopenharmony_ci     * Corresponding formats:
114bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_D16_UNORM
115bf215546Sopenharmony_ci     *   OpenGL ES: GL_DEPTH_COMPONENT16
116bf215546Sopenharmony_ci     */
117bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_D16_UNORM                = 0x30,
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci    /**
120bf215546Sopenharmony_ci     * Corresponding formats:
121bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_X8_D24_UNORM_PACK32
122bf215546Sopenharmony_ci     *   OpenGL ES: GL_DEPTH_COMPONENT24
123bf215546Sopenharmony_ci     */
124bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_D24_UNORM                = 0x31,
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci    /**
127bf215546Sopenharmony_ci     * Corresponding formats:
128bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_D24_UNORM_S8_UINT
129bf215546Sopenharmony_ci     *   OpenGL ES: GL_DEPTH24_STENCIL8
130bf215546Sopenharmony_ci     */
131bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT        = 0x32,
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci    /**
134bf215546Sopenharmony_ci     * Corresponding formats:
135bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_D32_SFLOAT
136bf215546Sopenharmony_ci     *   OpenGL ES: GL_DEPTH_COMPONENT32F
137bf215546Sopenharmony_ci     */
138bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_D32_FLOAT                = 0x33,
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci    /**
141bf215546Sopenharmony_ci     * Corresponding formats:
142bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_D32_SFLOAT_S8_UINT
143bf215546Sopenharmony_ci     *   OpenGL ES: GL_DEPTH32F_STENCIL8
144bf215546Sopenharmony_ci     */
145bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT        = 0x34,
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci    /**
148bf215546Sopenharmony_ci     * Corresponding formats:
149bf215546Sopenharmony_ci     *   Vulkan: VK_FORMAT_S8_UINT
150bf215546Sopenharmony_ci     *   OpenGL ES: GL_STENCIL_INDEX8
151bf215546Sopenharmony_ci     */
152bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_S8_UINT                  = 0x35,
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci    /**
155bf215546Sopenharmony_ci     * YUV 420 888 format.
156bf215546Sopenharmony_ci     * Must have an even width and height. Can be accessed in OpenGL
157bf215546Sopenharmony_ci     * shaders through an external sampler. Does not support mip-maps
158bf215546Sopenharmony_ci     * cube-maps or multi-layered textures.
159bf215546Sopenharmony_ci     */
160bf215546Sopenharmony_ci    AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420             = 0x23,
161bf215546Sopenharmony_ci};
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci/**
164bf215546Sopenharmony_ci * Buffer usage flags, specifying how the buffer will be accessed.
165bf215546Sopenharmony_ci */
166bf215546Sopenharmony_cienum AHardwareBuffer_UsageFlags {
167bf215546Sopenharmony_ci    /// The buffer will never be locked for direct CPU reads using the
168bf215546Sopenharmony_ci    /// AHardwareBuffer_lock() function. Note that reading the buffer
169bf215546Sopenharmony_ci    /// using OpenGL or Vulkan functions or memory mappings is still
170bf215546Sopenharmony_ci    /// allowed.
171bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_READ_NEVER        = 0UL,
172bf215546Sopenharmony_ci    /// The buffer will sometimes be locked for direct CPU reads using
173bf215546Sopenharmony_ci    /// the AHardwareBuffer_lock() function. Note that reading the
174bf215546Sopenharmony_ci    /// buffer using OpenGL or Vulkan functions or memory mappings
175bf215546Sopenharmony_ci    /// does not require the presence of this flag.
176bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_READ_RARELY       = 2UL,
177bf215546Sopenharmony_ci    /// The buffer will often be locked for direct CPU reads using
178bf215546Sopenharmony_ci    /// the AHardwareBuffer_lock() function. Note that reading the
179bf215546Sopenharmony_ci    /// buffer using OpenGL or Vulkan functions or memory mappings
180bf215546Sopenharmony_ci    /// does not require the presence of this flag.
181bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN        = 3UL,
182bf215546Sopenharmony_ci    /// CPU read value mask.
183bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_READ_MASK         = 0xFUL,
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci    /// The buffer will never be locked for direct CPU writes using the
186bf215546Sopenharmony_ci    /// AHardwareBuffer_lock() function. Note that writing the buffer
187bf215546Sopenharmony_ci    /// using OpenGL or Vulkan functions or memory mappings is still
188bf215546Sopenharmony_ci    /// allowed.
189bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER       = 0UL << 4,
190bf215546Sopenharmony_ci    /// The buffer will sometimes be locked for direct CPU writes using
191bf215546Sopenharmony_ci    /// the AHardwareBuffer_lock() function. Note that writing the
192bf215546Sopenharmony_ci    /// buffer using OpenGL or Vulkan functions or memory mappings
193bf215546Sopenharmony_ci    /// does not require the presence of this flag.
194bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY      = 2UL << 4,
195bf215546Sopenharmony_ci    /// The buffer will often be locked for direct CPU writes using
196bf215546Sopenharmony_ci    /// the AHardwareBuffer_lock() function. Note that writing the
197bf215546Sopenharmony_ci    /// buffer using OpenGL or Vulkan functions or memory mappings
198bf215546Sopenharmony_ci    /// does not require the presence of this flag.
199bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN       = 3UL << 4,
200bf215546Sopenharmony_ci    /// CPU write value mask.
201bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK        = 0xFUL << 4,
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci    /// The buffer will be read from by the GPU as a texture.
204bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE      = 1UL << 8,
205bf215546Sopenharmony_ci    /// The buffer will be written to by the GPU as a framebuffer attachment.
206bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER        = 1UL << 9,
207bf215546Sopenharmony_ci    /**
208bf215546Sopenharmony_ci     * The buffer will be written to by the GPU as a framebuffer
209bf215546Sopenharmony_ci     * attachment.
210bf215546Sopenharmony_ci     *
211bf215546Sopenharmony_ci     * Note that the name of this flag is somewhat misleading: it does
212bf215546Sopenharmony_ci     * not imply that the buffer contains a color format. A buffer with
213bf215546Sopenharmony_ci     * depth or stencil format that will be used as a framebuffer
214bf215546Sopenharmony_ci     * attachment should also have this flag. Use the equivalent flag
215bf215546Sopenharmony_ci     * AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER to avoid this confusion.
216bf215546Sopenharmony_ci     */
217bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT       = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
218bf215546Sopenharmony_ci    /**
219bf215546Sopenharmony_ci     * The buffer will be used as a composer HAL overlay layer.
220bf215546Sopenharmony_ci     *
221bf215546Sopenharmony_ci     * This flag is currently only needed when using ASurfaceTransaction_setBuffer
222bf215546Sopenharmony_ci     * to set a buffer. In all other cases, the framework adds this flag
223bf215546Sopenharmony_ci     * internally to buffers that could be presented in a composer overlay.
224bf215546Sopenharmony_ci     * ASurfaceTransaction_setBuffer is special because it uses buffers allocated
225bf215546Sopenharmony_ci     * directly through AHardwareBuffer_allocate instead of buffers allocated
226bf215546Sopenharmony_ci     * by the framework.
227bf215546Sopenharmony_ci     */
228bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY       = 1ULL << 11,
229bf215546Sopenharmony_ci    /**
230bf215546Sopenharmony_ci     * The buffer is protected from direct CPU access or being read by
231bf215546Sopenharmony_ci     * non-secure hardware, such as video encoders.
232bf215546Sopenharmony_ci     *
233bf215546Sopenharmony_ci     * This flag is incompatible with CPU read and write flags. It is
234bf215546Sopenharmony_ci     * mainly used when handling DRM video. Refer to the EGL extension
235bf215546Sopenharmony_ci     * EGL_EXT_protected_content and GL extension
236bf215546Sopenharmony_ci     * GL_EXT_protected_textures for more information on how these
237bf215546Sopenharmony_ci     * buffers are expected to behave.
238bf215546Sopenharmony_ci     */
239bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT      = 1UL << 14,
240bf215546Sopenharmony_ci    /// The buffer will be read by a hardware video encoder.
241bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VIDEO_ENCODE           = 1UL << 16,
242bf215546Sopenharmony_ci    /**
243bf215546Sopenharmony_ci     * The buffer will be used for direct writes from sensors.
244bf215546Sopenharmony_ci     * When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB.
245bf215546Sopenharmony_ci     */
246bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA     = 1UL << 23,
247bf215546Sopenharmony_ci    /**
248bf215546Sopenharmony_ci     * The buffer will be used as a shader storage or uniform buffer object.
249bf215546Sopenharmony_ci     * When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB.
250bf215546Sopenharmony_ci     */
251bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER        = 1UL << 24,
252bf215546Sopenharmony_ci    /**
253bf215546Sopenharmony_ci     * The buffer will be used as a cube map texture.
254bf215546Sopenharmony_ci     * When this flag is present, the buffer must have a layer count
255bf215546Sopenharmony_ci     * that is a multiple of 6. Note that buffers with this flag must be
256bf215546Sopenharmony_ci     * bound to OpenGL textures using the extension
257bf215546Sopenharmony_ci     * GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image.
258bf215546Sopenharmony_ci     */
259bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP               = 1UL << 25,
260bf215546Sopenharmony_ci    /**
261bf215546Sopenharmony_ci     * The buffer contains a complete mipmap hierarchy.
262bf215546Sopenharmony_ci     * Note that buffers with this flag must be bound to OpenGL textures using
263bf215546Sopenharmony_ci     * the extension GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image.
264bf215546Sopenharmony_ci     */
265bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE        = 1UL << 26,
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_0  = 1ULL << 28,
268bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_1  = 1ULL << 29,
269bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_2  = 1ULL << 30,
270bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_3  = 1ULL << 31,
271bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_4  = 1ULL << 48,
272bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_5  = 1ULL << 49,
273bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_6  = 1ULL << 50,
274bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_7  = 1ULL << 51,
275bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_8  = 1ULL << 52,
276bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_9  = 1ULL << 53,
277bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_10 = 1ULL << 54,
278bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_11 = 1ULL << 55,
279bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_12 = 1ULL << 56,
280bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_13 = 1ULL << 57,
281bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_14 = 1ULL << 58,
282bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_15 = 1ULL << 59,
283bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_16 = 1ULL << 60,
284bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_17 = 1ULL << 61,
285bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_18 = 1ULL << 62,
286bf215546Sopenharmony_ci    AHARDWAREBUFFER_USAGE_VENDOR_19 = 1ULL << 63,
287bf215546Sopenharmony_ci};
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci/**
290bf215546Sopenharmony_ci * Buffer description. Used for allocating new buffers and querying
291bf215546Sopenharmony_ci * parameters of existing ones.
292bf215546Sopenharmony_ci */
293bf215546Sopenharmony_citypedef struct AHardwareBuffer_Desc {
294bf215546Sopenharmony_ci    uint32_t    width;      ///< Width in pixels.
295bf215546Sopenharmony_ci    uint32_t    height;     ///< Height in pixels.
296bf215546Sopenharmony_ci    /**
297bf215546Sopenharmony_ci     * Number of images in an image array. AHardwareBuffers with one
298bf215546Sopenharmony_ci     * layer correspond to regular 2D textures. AHardwareBuffers with
299bf215546Sopenharmony_ci     * more than layer correspond to texture arrays. If the layer count
300bf215546Sopenharmony_ci     * is a multiple of 6 and the usage flag
301bf215546Sopenharmony_ci     * AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP is present, the buffer is
302bf215546Sopenharmony_ci     * a cube map or a cube map array.
303bf215546Sopenharmony_ci     */
304bf215546Sopenharmony_ci    uint32_t    layers;
305bf215546Sopenharmony_ci    uint32_t    format;     ///< One of AHardwareBuffer_Format.
306bf215546Sopenharmony_ci    uint64_t    usage;      ///< Combination of AHardwareBuffer_UsageFlags.
307bf215546Sopenharmony_ci    uint32_t    stride;     ///< Row stride in pixels, ignored for AHardwareBuffer_allocate()
308bf215546Sopenharmony_ci    uint32_t    rfu0;       ///< Initialize to zero, reserved for future use.
309bf215546Sopenharmony_ci    uint64_t    rfu1;       ///< Initialize to zero, reserved for future use.
310bf215546Sopenharmony_ci} AHardwareBuffer_Desc;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci/**
313bf215546Sopenharmony_ci * Holds data for a single image plane.
314bf215546Sopenharmony_ci */
315bf215546Sopenharmony_citypedef struct AHardwareBuffer_Plane {
316bf215546Sopenharmony_ci    void*       data;        ///< Points to first byte in plane
317bf215546Sopenharmony_ci    uint32_t    pixelStride; ///< Distance in bytes from the color channel of one pixel to the next
318bf215546Sopenharmony_ci    uint32_t    rowStride;   ///< Distance in bytes from the first value of one row of the image to
319bf215546Sopenharmony_ci                             ///  the first value of the next row.
320bf215546Sopenharmony_ci} AHardwareBuffer_Plane;
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci/**
323bf215546Sopenharmony_ci * Holds all image planes that contain the pixel data.
324bf215546Sopenharmony_ci */
325bf215546Sopenharmony_citypedef struct AHardwareBuffer_Planes {
326bf215546Sopenharmony_ci    uint32_t               planeCount; ///< Number of distinct planes
327bf215546Sopenharmony_ci    AHardwareBuffer_Plane  planes[4];     ///< Array of image planes
328bf215546Sopenharmony_ci} AHardwareBuffer_Planes;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci/**
331bf215546Sopenharmony_ci * Opaque handle for a native hardware buffer.
332bf215546Sopenharmony_ci */
333bf215546Sopenharmony_citypedef struct AHardwareBuffer AHardwareBuffer;
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci/**
336bf215546Sopenharmony_ci * Allocates a buffer that matches the passed AHardwareBuffer_Desc.
337bf215546Sopenharmony_ci *
338bf215546Sopenharmony_ci * If allocation succeeds, the buffer can be used according to the
339bf215546Sopenharmony_ci * usage flags specified in its description. If a buffer is used in ways
340bf215546Sopenharmony_ci * not compatible with its usage flags, the results are undefined and
341bf215546Sopenharmony_ci * may include program termination.
342bf215546Sopenharmony_ci *
343bf215546Sopenharmony_ci * Available since API level 26.
344bf215546Sopenharmony_ci *
345bf215546Sopenharmony_ci * \return 0 on success, or an error number of the allocation fails for
346bf215546Sopenharmony_ci * any reason. The returned buffer has a reference count of 1.
347bf215546Sopenharmony_ci */
348bf215546Sopenharmony_ciint AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc,
349bf215546Sopenharmony_ci        AHardwareBuffer** outBuffer) __INTRODUCED_IN(26);
350bf215546Sopenharmony_ci/**
351bf215546Sopenharmony_ci * Acquire a reference on the given AHardwareBuffer object.
352bf215546Sopenharmony_ci *
353bf215546Sopenharmony_ci * This prevents the object from being deleted until the last reference
354bf215546Sopenharmony_ci * is removed.
355bf215546Sopenharmony_ci *
356bf215546Sopenharmony_ci * Available since API level 26.
357bf215546Sopenharmony_ci */
358bf215546Sopenharmony_civoid AHardwareBuffer_acquire(AHardwareBuffer* buffer) __INTRODUCED_IN(26);
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci/**
361bf215546Sopenharmony_ci * Remove a reference that was previously acquired with
362bf215546Sopenharmony_ci * AHardwareBuffer_acquire() or AHardwareBuffer_allocate().
363bf215546Sopenharmony_ci *
364bf215546Sopenharmony_ci * Available since API level 26.
365bf215546Sopenharmony_ci */
366bf215546Sopenharmony_civoid AHardwareBuffer_release(AHardwareBuffer* buffer) __INTRODUCED_IN(26);
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci/**
369bf215546Sopenharmony_ci * Return a description of the AHardwareBuffer in the passed
370bf215546Sopenharmony_ci * AHardwareBuffer_Desc struct.
371bf215546Sopenharmony_ci *
372bf215546Sopenharmony_ci * Available since API level 26.
373bf215546Sopenharmony_ci */
374bf215546Sopenharmony_civoid AHardwareBuffer_describe(const AHardwareBuffer* buffer,
375bf215546Sopenharmony_ci        AHardwareBuffer_Desc* outDesc) __INTRODUCED_IN(26);
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci/**
378bf215546Sopenharmony_ci * Lock the AHardwareBuffer for direct CPU access.
379bf215546Sopenharmony_ci *
380bf215546Sopenharmony_ci * This function can lock the buffer for either reading or writing.
381bf215546Sopenharmony_ci * It may block if the hardware needs to finish rendering, if CPU caches
382bf215546Sopenharmony_ci * need to be synchronized, or possibly for other implementation-
383bf215546Sopenharmony_ci * specific reasons.
384bf215546Sopenharmony_ci *
385bf215546Sopenharmony_ci * The passed AHardwareBuffer must have one layer, otherwise the call
386bf215546Sopenharmony_ci * will fail.
387bf215546Sopenharmony_ci *
388bf215546Sopenharmony_ci * If \a fence is not negative, it specifies a fence file descriptor on
389bf215546Sopenharmony_ci * which to wait before locking the buffer. If it's negative, the caller
390bf215546Sopenharmony_ci * is responsible for ensuring that writes to the buffer have completed
391bf215546Sopenharmony_ci * before calling this function.  Using this parameter is more efficient
392bf215546Sopenharmony_ci * than waiting on the fence and then calling this function.
393bf215546Sopenharmony_ci *
394bf215546Sopenharmony_ci * The \a usage parameter may only specify AHARDWAREBUFFER_USAGE_CPU_*.
395bf215546Sopenharmony_ci * If set, then outVirtualAddress is filled with the address of the
396bf215546Sopenharmony_ci * buffer in virtual memory. The flags must also be compatible with
397bf215546Sopenharmony_ci * usage flags specified at buffer creation: if a read flag is passed,
398bf215546Sopenharmony_ci * the buffer must have been created with
399bf215546Sopenharmony_ci * AHARDWAREBUFFER_USAGE_CPU_READ_RARELY or
400bf215546Sopenharmony_ci * AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN. If a write flag is passed, it
401bf215546Sopenharmony_ci * must have been created with AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY or
402bf215546Sopenharmony_ci * AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN.
403bf215546Sopenharmony_ci *
404bf215546Sopenharmony_ci * If \a rect is not NULL, the caller promises to modify only data in
405bf215546Sopenharmony_ci * the area specified by rect. If rect is NULL, the caller may modify
406bf215546Sopenharmony_ci * the contents of the entire buffer. The content of the buffer outside
407bf215546Sopenharmony_ci * of the specified rect is NOT modified by this call.
408bf215546Sopenharmony_ci *
409bf215546Sopenharmony_ci * It is legal for several different threads to lock a buffer for read
410bf215546Sopenharmony_ci * access; none of the threads are blocked.
411bf215546Sopenharmony_ci *
412bf215546Sopenharmony_ci * Locking a buffer simultaneously for write or read/write is undefined,
413bf215546Sopenharmony_ci * but will neither terminate the process nor block the caller.
414bf215546Sopenharmony_ci * AHardwareBuffer_lock may return an error or leave the buffer's
415bf215546Sopenharmony_ci * content in an indeterminate state.
416bf215546Sopenharmony_ci *
417bf215546Sopenharmony_ci * If the buffer has AHARDWAREBUFFER_FORMAT_BLOB, it is legal lock it
418bf215546Sopenharmony_ci * for reading and writing in multiple threads and/or processes
419bf215546Sopenharmony_ci * simultaneously, and the contents of the buffer behave like shared
420bf215546Sopenharmony_ci * memory.
421bf215546Sopenharmony_ci *
422bf215546Sopenharmony_ci * Available since API level 26.
423bf215546Sopenharmony_ci *
424bf215546Sopenharmony_ci * \return 0 on success. -EINVAL if \a buffer is NULL, the usage flags
425bf215546Sopenharmony_ci * are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer
426bf215546Sopenharmony_ci * has more than one layer. Error number if the lock fails for any other
427bf215546Sopenharmony_ci * reason.
428bf215546Sopenharmony_ci */
429bf215546Sopenharmony_ciint AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage,
430bf215546Sopenharmony_ci        int32_t fence, const ARect* rect, void** outVirtualAddress) __INTRODUCED_IN(26);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci/**
433bf215546Sopenharmony_ci * Lock a potentially multi-planar AHardwareBuffer for direct CPU access.
434bf215546Sopenharmony_ci *
435bf215546Sopenharmony_ci * This function is similar to AHardwareBuffer_lock, but can lock multi-planar
436bf215546Sopenharmony_ci * formats. The locked planes are returned in the \a outPlanes argument. Note,
437bf215546Sopenharmony_ci * that multi-planar should not be confused with multi-layer images, which this
438bf215546Sopenharmony_ci * locking function does not support.
439bf215546Sopenharmony_ci *
440bf215546Sopenharmony_ci * YUV formats are always represented by three separate planes of data, one for
441bf215546Sopenharmony_ci * each color plane. The order of planes in the array is guaranteed such that
442bf215546Sopenharmony_ci * plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V
443bf215546Sopenharmony_ci * (Cr). All other formats are represented by a single plane.
444bf215546Sopenharmony_ci *
445bf215546Sopenharmony_ci * Additional information always accompanies the buffers, describing the row
446bf215546Sopenharmony_ci * stride and the pixel stride for each plane.
447bf215546Sopenharmony_ci *
448bf215546Sopenharmony_ci * In case the buffer cannot be locked, \a outPlanes will contain zero planes.
449bf215546Sopenharmony_ci *
450bf215546Sopenharmony_ci * See the AHardwareBuffer_lock documentation for all other locking semantics.
451bf215546Sopenharmony_ci *
452bf215546Sopenharmony_ci * Available since API level 29.
453bf215546Sopenharmony_ci *
454bf215546Sopenharmony_ci * \return 0 on success. -EINVAL if \a buffer is NULL, the usage flags
455bf215546Sopenharmony_ci * are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer
456bf215546Sopenharmony_ci * has more than one layer. Error number if the lock fails for any other
457bf215546Sopenharmony_ci * reason.
458bf215546Sopenharmony_ci */
459bf215546Sopenharmony_ciint AHardwareBuffer_lockPlanes(AHardwareBuffer* buffer, uint64_t usage,
460bf215546Sopenharmony_ci        int32_t fence, const ARect* rect, AHardwareBuffer_Planes* outPlanes) __INTRODUCED_IN(29);
461bf215546Sopenharmony_ci
462bf215546Sopenharmony_ci/**
463bf215546Sopenharmony_ci * Unlock the AHardwareBuffer from direct CPU access.
464bf215546Sopenharmony_ci *
465bf215546Sopenharmony_ci * Must be called after all changes to the buffer are completed by the
466bf215546Sopenharmony_ci * caller.  If \a fence is NULL, the function will block until all work
467bf215546Sopenharmony_ci * is completed.  Otherwise, \a fence will be set either to a valid file
468bf215546Sopenharmony_ci * descriptor or to -1.  The file descriptor will become signaled once
469bf215546Sopenharmony_ci * the unlocking is complete and buffer contents are updated.
470bf215546Sopenharmony_ci * The caller is responsible for closing the file descriptor once it's
471bf215546Sopenharmony_ci * no longer needed.  The value -1 indicates that unlocking has already
472bf215546Sopenharmony_ci * completed before the function returned and no further operations are
473bf215546Sopenharmony_ci * necessary.
474bf215546Sopenharmony_ci *
475bf215546Sopenharmony_ci * Available since API level 26.
476bf215546Sopenharmony_ci *
477bf215546Sopenharmony_ci * \return 0 on success. -EINVAL if \a buffer is NULL. Error number if
478bf215546Sopenharmony_ci * the unlock fails for any reason.
479bf215546Sopenharmony_ci */
480bf215546Sopenharmony_ciint AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) __INTRODUCED_IN(26);
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_ci/**
483bf215546Sopenharmony_ci * Send the AHardwareBuffer to an AF_UNIX socket.
484bf215546Sopenharmony_ci *
485bf215546Sopenharmony_ci * Available since API level 26.
486bf215546Sopenharmony_ci *
487bf215546Sopenharmony_ci * \return 0 on success, -EINVAL if \a buffer is NULL, or an error
488bf215546Sopenharmony_ci * number if the operation fails for any reason.
489bf215546Sopenharmony_ci */
490bf215546Sopenharmony_ciint AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) __INTRODUCED_IN(26);
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci/**
493bf215546Sopenharmony_ci * Receive an AHardwareBuffer from an AF_UNIX socket.
494bf215546Sopenharmony_ci *
495bf215546Sopenharmony_ci * Available since API level 26.
496bf215546Sopenharmony_ci *
497bf215546Sopenharmony_ci * \return 0 on success, -EINVAL if \a outBuffer is NULL, or an error
498bf215546Sopenharmony_ci * number if the operation fails for any reason.
499bf215546Sopenharmony_ci */
500bf215546Sopenharmony_ciint AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) __INTRODUCED_IN(26);
501bf215546Sopenharmony_ci
502bf215546Sopenharmony_ci/**
503bf215546Sopenharmony_ci * Test whether the given format and usage flag combination is
504bf215546Sopenharmony_ci * allocatable.
505bf215546Sopenharmony_ci *
506bf215546Sopenharmony_ci * If this function returns true, it means that a buffer with the given
507bf215546Sopenharmony_ci * description can be allocated on this implementation, unless resource
508bf215546Sopenharmony_ci * exhaustion occurs. If this function returns false, it means that the
509bf215546Sopenharmony_ci * allocation of the given description will never succeed.
510bf215546Sopenharmony_ci *
511bf215546Sopenharmony_ci * The return value of this function may depend on all fields in the
512bf215546Sopenharmony_ci * description, except stride, which is always ignored. For example,
513bf215546Sopenharmony_ci * some implementations have implementation-defined limits on texture
514bf215546Sopenharmony_ci * size and layer count.
515bf215546Sopenharmony_ci *
516bf215546Sopenharmony_ci * Available since API level 29.
517bf215546Sopenharmony_ci *
518bf215546Sopenharmony_ci * \return 1 if the format and usage flag combination is allocatable,
519bf215546Sopenharmony_ci *     0 otherwise.
520bf215546Sopenharmony_ci */
521bf215546Sopenharmony_ciint AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) __INTRODUCED_IN(29);
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci/**
524bf215546Sopenharmony_ci * Lock an AHardwareBuffer for direct CPU access.
525bf215546Sopenharmony_ci *
526bf215546Sopenharmony_ci * This function is the same as the above lock function, but passes back
527bf215546Sopenharmony_ci * additional information about the bytes per pixel and the bytes per stride
528bf215546Sopenharmony_ci * of the locked buffer.  If the bytes per pixel or bytes per stride are unknown
529bf215546Sopenharmony_ci * or variable, or if the underlying mapper implementation does not support returning
530bf215546Sopenharmony_ci * additional information, then this call will fail with INVALID_OPERATION
531bf215546Sopenharmony_ci *
532bf215546Sopenharmony_ci * Available since API level 29.
533bf215546Sopenharmony_ci */
534bf215546Sopenharmony_ciint AHardwareBuffer_lockAndGetInfo(AHardwareBuffer* buffer, uint64_t usage,
535bf215546Sopenharmony_ci        int32_t fence, const ARect* rect, void** outVirtualAddress,
536bf215546Sopenharmony_ci        int32_t* outBytesPerPixel, int32_t* outBytesPerStride) __INTRODUCED_IN(29);
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci__END_DECLS
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci#endif // ANDROID_HARDWARE_BUFFER_H
541bf215546Sopenharmony_ci
542bf215546Sopenharmony_ci/** @} */
543