17ec681f3Smrg/*
27ec681f3Smrg * Copyright 2017 The Android Open Source Project
37ec681f3Smrg *
47ec681f3Smrg * Licensed under the Apache License, Version 2.0 (the "License");
57ec681f3Smrg * you may not use this file except in compliance with the License.
67ec681f3Smrg * You may obtain a copy of the License at
77ec681f3Smrg *
87ec681f3Smrg *      http://www.apache.org/licenses/LICENSE-2.0
97ec681f3Smrg *
107ec681f3Smrg * Unless required by applicable law or agreed to in writing, software
117ec681f3Smrg * distributed under the License is distributed on an "AS IS" BASIS,
127ec681f3Smrg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec681f3Smrg * See the License for the specific language governing permissions and
147ec681f3Smrg * limitations under the License.
157ec681f3Smrg */
167ec681f3Smrg
177ec681f3Smrg/**
187ec681f3Smrg * @file hardware_buffer.h
197ec681f3Smrg * @brief API for native hardware buffers.
207ec681f3Smrg */
217ec681f3Smrg/**
227ec681f3Smrg * @defgroup AHardwareBuffer Native Hardware Buffer
237ec681f3Smrg *
247ec681f3Smrg * AHardwareBuffer objects represent chunks of memory that can be
257ec681f3Smrg * accessed by various hardware components in the system. It can be
267ec681f3Smrg * easily converted to the Java counterpart
277ec681f3Smrg * android.hardware.HardwareBuffer and passed between processes using
287ec681f3Smrg * Binder. All operations involving AHardwareBuffer and HardwareBuffer
297ec681f3Smrg * are zero-copy, i.e., passing AHardwareBuffer to another process
307ec681f3Smrg * creates a shared view of the same region of memory.
317ec681f3Smrg *
327ec681f3Smrg * AHardwareBuffers can be bound to EGL/OpenGL and Vulkan primitives.
337ec681f3Smrg * For EGL, use the extension function eglGetNativeClientBufferANDROID
347ec681f3Smrg * to obtain an EGLClientBuffer and pass it directly to
357ec681f3Smrg * eglCreateImageKHR. Refer to the EGL extensions
367ec681f3Smrg * EGL_ANDROID_get_native_client_buffer and
377ec681f3Smrg * EGL_ANDROID_image_native_buffer for more information. In Vulkan,
387ec681f3Smrg * the contents of the AHardwareBuffer can be accessed as external
397ec681f3Smrg * memory. See the VK_ANDROID_external_memory_android_hardware_buffer
407ec681f3Smrg * extension for details.
417ec681f3Smrg *
427ec681f3Smrg * @{
437ec681f3Smrg */
447ec681f3Smrg
457ec681f3Smrg#ifndef ANDROID_HARDWARE_BUFFER_H
467ec681f3Smrg#define ANDROID_HARDWARE_BUFFER_H
477ec681f3Smrg
487ec681f3Smrg#include <inttypes.h>
497ec681f3Smrg
507ec681f3Smrg#include <sys/cdefs.h>
517ec681f3Smrg
527ec681f3Smrg#include <android/rect.h>
537ec681f3Smrg
547ec681f3Smrg__BEGIN_DECLS
557ec681f3Smrg
567ec681f3Smrg/**
577ec681f3Smrg * Buffer pixel formats.
587ec681f3Smrg */
597ec681f3Smrgenum AHardwareBuffer_Format {
607ec681f3Smrg    /**
617ec681f3Smrg     * Corresponding formats:
627ec681f3Smrg     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
637ec681f3Smrg     *   OpenGL ES: GL_RGBA8
647ec681f3Smrg     */
657ec681f3Smrg    AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM           = 1,
667ec681f3Smrg
677ec681f3Smrg    /**
687ec681f3Smrg     * 32 bits per pixel, 8 bits per channel format where alpha values are
697ec681f3Smrg     * ignored (always opaque).
707ec681f3Smrg     * Corresponding formats:
717ec681f3Smrg     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
727ec681f3Smrg     *   OpenGL ES: GL_RGB8
737ec681f3Smrg     */
747ec681f3Smrg    AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM           = 2,
757ec681f3Smrg
767ec681f3Smrg    /**
777ec681f3Smrg     * Corresponding formats:
787ec681f3Smrg     *   Vulkan: VK_FORMAT_R8G8B8_UNORM
797ec681f3Smrg     *   OpenGL ES: GL_RGB8
807ec681f3Smrg     */
817ec681f3Smrg    AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM             = 3,
827ec681f3Smrg
837ec681f3Smrg    /**
847ec681f3Smrg     * Corresponding formats:
857ec681f3Smrg     *   Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16
867ec681f3Smrg     *   OpenGL ES: GL_RGB565
877ec681f3Smrg     */
887ec681f3Smrg    AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM             = 4,
897ec681f3Smrg
907ec681f3Smrg    /**
917ec681f3Smrg     * Corresponding formats:
927ec681f3Smrg     *   Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT
937ec681f3Smrg     *   OpenGL ES: GL_RGBA16F
947ec681f3Smrg     */
957ec681f3Smrg    AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT       = 0x16,
967ec681f3Smrg
977ec681f3Smrg    /**
987ec681f3Smrg     * Corresponding formats:
997ec681f3Smrg     *   Vulkan: VK_FORMAT_A2B10G10R10_UNORM_PACK32
1007ec681f3Smrg     *   OpenGL ES: GL_RGB10_A2
1017ec681f3Smrg     */
1027ec681f3Smrg    AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM        = 0x2b,
1037ec681f3Smrg
1047ec681f3Smrg    /**
1057ec681f3Smrg     * Opaque binary blob format.
1067ec681f3Smrg     * Must have height 1 and one layer, with width equal to the buffer
1077ec681f3Smrg     * size in bytes. Corresponds to Vulkan buffers and OpenGL buffer
1087ec681f3Smrg     * objects. Can be bound to the latter using GL_EXT_external_buffer.
1097ec681f3Smrg     */
1107ec681f3Smrg    AHARDWAREBUFFER_FORMAT_BLOB                     = 0x21,
1117ec681f3Smrg
1127ec681f3Smrg    /**
1137ec681f3Smrg     * Corresponding formats:
1147ec681f3Smrg     *   Vulkan: VK_FORMAT_D16_UNORM
1157ec681f3Smrg     *   OpenGL ES: GL_DEPTH_COMPONENT16
1167ec681f3Smrg     */
1177ec681f3Smrg    AHARDWAREBUFFER_FORMAT_D16_UNORM                = 0x30,
1187ec681f3Smrg
1197ec681f3Smrg    /**
1207ec681f3Smrg     * Corresponding formats:
1217ec681f3Smrg     *   Vulkan: VK_FORMAT_X8_D24_UNORM_PACK32
1227ec681f3Smrg     *   OpenGL ES: GL_DEPTH_COMPONENT24
1237ec681f3Smrg     */
1247ec681f3Smrg    AHARDWAREBUFFER_FORMAT_D24_UNORM                = 0x31,
1257ec681f3Smrg
1267ec681f3Smrg    /**
1277ec681f3Smrg     * Corresponding formats:
1287ec681f3Smrg     *   Vulkan: VK_FORMAT_D24_UNORM_S8_UINT
1297ec681f3Smrg     *   OpenGL ES: GL_DEPTH24_STENCIL8
1307ec681f3Smrg     */
1317ec681f3Smrg    AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT        = 0x32,
1327ec681f3Smrg
1337ec681f3Smrg    /**
1347ec681f3Smrg     * Corresponding formats:
1357ec681f3Smrg     *   Vulkan: VK_FORMAT_D32_SFLOAT
1367ec681f3Smrg     *   OpenGL ES: GL_DEPTH_COMPONENT32F
1377ec681f3Smrg     */
1387ec681f3Smrg    AHARDWAREBUFFER_FORMAT_D32_FLOAT                = 0x33,
1397ec681f3Smrg
1407ec681f3Smrg    /**
1417ec681f3Smrg     * Corresponding formats:
1427ec681f3Smrg     *   Vulkan: VK_FORMAT_D32_SFLOAT_S8_UINT
1437ec681f3Smrg     *   OpenGL ES: GL_DEPTH32F_STENCIL8
1447ec681f3Smrg     */
1457ec681f3Smrg    AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT        = 0x34,
1467ec681f3Smrg
1477ec681f3Smrg    /**
1487ec681f3Smrg     * Corresponding formats:
1497ec681f3Smrg     *   Vulkan: VK_FORMAT_S8_UINT
1507ec681f3Smrg     *   OpenGL ES: GL_STENCIL_INDEX8
1517ec681f3Smrg     */
1527ec681f3Smrg    AHARDWAREBUFFER_FORMAT_S8_UINT                  = 0x35,
1537ec681f3Smrg
1547ec681f3Smrg    /**
1557ec681f3Smrg     * YUV 420 888 format.
1567ec681f3Smrg     * Must have an even width and height. Can be accessed in OpenGL
1577ec681f3Smrg     * shaders through an external sampler. Does not support mip-maps
1587ec681f3Smrg     * cube-maps or multi-layered textures.
1597ec681f3Smrg     */
1607ec681f3Smrg    AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420             = 0x23,
1617ec681f3Smrg};
1627ec681f3Smrg
1637ec681f3Smrg/**
1647ec681f3Smrg * Buffer usage flags, specifying how the buffer will be accessed.
1657ec681f3Smrg */
1667ec681f3Smrgenum AHardwareBuffer_UsageFlags {
1677ec681f3Smrg    /// The buffer will never be locked for direct CPU reads using the
1687ec681f3Smrg    /// AHardwareBuffer_lock() function. Note that reading the buffer
1697ec681f3Smrg    /// using OpenGL or Vulkan functions or memory mappings is still
1707ec681f3Smrg    /// allowed.
1717ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_READ_NEVER        = 0UL,
1727ec681f3Smrg    /// The buffer will sometimes be locked for direct CPU reads using
1737ec681f3Smrg    /// the AHardwareBuffer_lock() function. Note that reading the
1747ec681f3Smrg    /// buffer using OpenGL or Vulkan functions or memory mappings
1757ec681f3Smrg    /// does not require the presence of this flag.
1767ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_READ_RARELY       = 2UL,
1777ec681f3Smrg    /// The buffer will often be locked for direct CPU reads using
1787ec681f3Smrg    /// the AHardwareBuffer_lock() function. Note that reading the
1797ec681f3Smrg    /// buffer using OpenGL or Vulkan functions or memory mappings
1807ec681f3Smrg    /// does not require the presence of this flag.
1817ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN        = 3UL,
1827ec681f3Smrg    /// CPU read value mask.
1837ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_READ_MASK         = 0xFUL,
1847ec681f3Smrg
1857ec681f3Smrg    /// The buffer will never be locked for direct CPU writes using the
1867ec681f3Smrg    /// AHardwareBuffer_lock() function. Note that writing the buffer
1877ec681f3Smrg    /// using OpenGL or Vulkan functions or memory mappings is still
1887ec681f3Smrg    /// allowed.
1897ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER       = 0UL << 4,
1907ec681f3Smrg    /// The buffer will sometimes be locked for direct CPU writes using
1917ec681f3Smrg    /// the AHardwareBuffer_lock() function. Note that writing the
1927ec681f3Smrg    /// buffer using OpenGL or Vulkan functions or memory mappings
1937ec681f3Smrg    /// does not require the presence of this flag.
1947ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY      = 2UL << 4,
1957ec681f3Smrg    /// The buffer will often be locked for direct CPU writes using
1967ec681f3Smrg    /// the AHardwareBuffer_lock() function. Note that writing the
1977ec681f3Smrg    /// buffer using OpenGL or Vulkan functions or memory mappings
1987ec681f3Smrg    /// does not require the presence of this flag.
1997ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN       = 3UL << 4,
2007ec681f3Smrg    /// CPU write value mask.
2017ec681f3Smrg    AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK        = 0xFUL << 4,
2027ec681f3Smrg
2037ec681f3Smrg    /// The buffer will be read from by the GPU as a texture.
2047ec681f3Smrg    AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE      = 1UL << 8,
2057ec681f3Smrg    /// The buffer will be written to by the GPU as a framebuffer attachment.
2067ec681f3Smrg    AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER        = 1UL << 9,
2077ec681f3Smrg    /**
2087ec681f3Smrg     * The buffer will be written to by the GPU as a framebuffer
2097ec681f3Smrg     * attachment.
2107ec681f3Smrg     *
2117ec681f3Smrg     * Note that the name of this flag is somewhat misleading: it does
2127ec681f3Smrg     * not imply that the buffer contains a color format. A buffer with
2137ec681f3Smrg     * depth or stencil format that will be used as a framebuffer
2147ec681f3Smrg     * attachment should also have this flag. Use the equivalent flag
2157ec681f3Smrg     * AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER to avoid this confusion.
2167ec681f3Smrg     */
2177ec681f3Smrg    AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT       = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
2187ec681f3Smrg    /**
2197ec681f3Smrg     * The buffer will be used as a composer HAL overlay layer.
2207ec681f3Smrg     *
2217ec681f3Smrg     * This flag is currently only needed when using ASurfaceTransaction_setBuffer
2227ec681f3Smrg     * to set a buffer. In all other cases, the framework adds this flag
2237ec681f3Smrg     * internally to buffers that could be presented in a composer overlay.
2247ec681f3Smrg     * ASurfaceTransaction_setBuffer is special because it uses buffers allocated
2257ec681f3Smrg     * directly through AHardwareBuffer_allocate instead of buffers allocated
2267ec681f3Smrg     * by the framework.
2277ec681f3Smrg     */
2287ec681f3Smrg    AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY       = 1ULL << 11,
2297ec681f3Smrg    /**
2307ec681f3Smrg     * The buffer is protected from direct CPU access or being read by
2317ec681f3Smrg     * non-secure hardware, such as video encoders.
2327ec681f3Smrg     *
2337ec681f3Smrg     * This flag is incompatible with CPU read and write flags. It is
2347ec681f3Smrg     * mainly used when handling DRM video. Refer to the EGL extension
2357ec681f3Smrg     * EGL_EXT_protected_content and GL extension
2367ec681f3Smrg     * GL_EXT_protected_textures for more information on how these
2377ec681f3Smrg     * buffers are expected to behave.
2387ec681f3Smrg     */
2397ec681f3Smrg    AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT      = 1UL << 14,
2407ec681f3Smrg    /// The buffer will be read by a hardware video encoder.
2417ec681f3Smrg    AHARDWAREBUFFER_USAGE_VIDEO_ENCODE           = 1UL << 16,
2427ec681f3Smrg    /**
2437ec681f3Smrg     * The buffer will be used for direct writes from sensors.
2447ec681f3Smrg     * When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB.
2457ec681f3Smrg     */
2467ec681f3Smrg    AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA     = 1UL << 23,
2477ec681f3Smrg    /**
2487ec681f3Smrg     * The buffer will be used as a shader storage or uniform buffer object.
2497ec681f3Smrg     * When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB.
2507ec681f3Smrg     */
2517ec681f3Smrg    AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER        = 1UL << 24,
2527ec681f3Smrg    /**
2537ec681f3Smrg     * The buffer will be used as a cube map texture.
2547ec681f3Smrg     * When this flag is present, the buffer must have a layer count
2557ec681f3Smrg     * that is a multiple of 6. Note that buffers with this flag must be
2567ec681f3Smrg     * bound to OpenGL textures using the extension
2577ec681f3Smrg     * GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image.
2587ec681f3Smrg     */
2597ec681f3Smrg    AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP               = 1UL << 25,
2607ec681f3Smrg    /**
2617ec681f3Smrg     * The buffer contains a complete mipmap hierarchy.
2627ec681f3Smrg     * Note that buffers with this flag must be bound to OpenGL textures using
2637ec681f3Smrg     * the extension GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image.
2647ec681f3Smrg     */
2657ec681f3Smrg    AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE        = 1UL << 26,
2667ec681f3Smrg
2677ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_0  = 1ULL << 28,
2687ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_1  = 1ULL << 29,
2697ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_2  = 1ULL << 30,
2707ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_3  = 1ULL << 31,
2717ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_4  = 1ULL << 48,
2727ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_5  = 1ULL << 49,
2737ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_6  = 1ULL << 50,
2747ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_7  = 1ULL << 51,
2757ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_8  = 1ULL << 52,
2767ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_9  = 1ULL << 53,
2777ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_10 = 1ULL << 54,
2787ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_11 = 1ULL << 55,
2797ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_12 = 1ULL << 56,
2807ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_13 = 1ULL << 57,
2817ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_14 = 1ULL << 58,
2827ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_15 = 1ULL << 59,
2837ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_16 = 1ULL << 60,
2847ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_17 = 1ULL << 61,
2857ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_18 = 1ULL << 62,
2867ec681f3Smrg    AHARDWAREBUFFER_USAGE_VENDOR_19 = 1ULL << 63,
2877ec681f3Smrg};
2887ec681f3Smrg
2897ec681f3Smrg/**
2907ec681f3Smrg * Buffer description. Used for allocating new buffers and querying
2917ec681f3Smrg * parameters of existing ones.
2927ec681f3Smrg */
2937ec681f3Smrgtypedef struct AHardwareBuffer_Desc {
2947ec681f3Smrg    uint32_t    width;      ///< Width in pixels.
2957ec681f3Smrg    uint32_t    height;     ///< Height in pixels.
2967ec681f3Smrg    /**
2977ec681f3Smrg     * Number of images in an image array. AHardwareBuffers with one
2987ec681f3Smrg     * layer correspond to regular 2D textures. AHardwareBuffers with
2997ec681f3Smrg     * more than layer correspond to texture arrays. If the layer count
3007ec681f3Smrg     * is a multiple of 6 and the usage flag
3017ec681f3Smrg     * AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP is present, the buffer is
3027ec681f3Smrg     * a cube map or a cube map array.
3037ec681f3Smrg     */
3047ec681f3Smrg    uint32_t    layers;
3057ec681f3Smrg    uint32_t    format;     ///< One of AHardwareBuffer_Format.
3067ec681f3Smrg    uint64_t    usage;      ///< Combination of AHardwareBuffer_UsageFlags.
3077ec681f3Smrg    uint32_t    stride;     ///< Row stride in pixels, ignored for AHardwareBuffer_allocate()
3087ec681f3Smrg    uint32_t    rfu0;       ///< Initialize to zero, reserved for future use.
3097ec681f3Smrg    uint64_t    rfu1;       ///< Initialize to zero, reserved for future use.
3107ec681f3Smrg} AHardwareBuffer_Desc;
3117ec681f3Smrg
3127ec681f3Smrg/**
3137ec681f3Smrg * Holds data for a single image plane.
3147ec681f3Smrg */
3157ec681f3Smrgtypedef struct AHardwareBuffer_Plane {
3167ec681f3Smrg    void*       data;        ///< Points to first byte in plane
3177ec681f3Smrg    uint32_t    pixelStride; ///< Distance in bytes from the color channel of one pixel to the next
3187ec681f3Smrg    uint32_t    rowStride;   ///< Distance in bytes from the first value of one row of the image to
3197ec681f3Smrg                             ///  the first value of the next row.
3207ec681f3Smrg} AHardwareBuffer_Plane;
3217ec681f3Smrg
3227ec681f3Smrg/**
3237ec681f3Smrg * Holds all image planes that contain the pixel data.
3247ec681f3Smrg */
3257ec681f3Smrgtypedef struct AHardwareBuffer_Planes {
3267ec681f3Smrg    uint32_t               planeCount; ///< Number of distinct planes
3277ec681f3Smrg    AHardwareBuffer_Plane  planes[4];     ///< Array of image planes
3287ec681f3Smrg} AHardwareBuffer_Planes;
3297ec681f3Smrg
3307ec681f3Smrg/**
3317ec681f3Smrg * Opaque handle for a native hardware buffer.
3327ec681f3Smrg */
3337ec681f3Smrgtypedef struct AHardwareBuffer AHardwareBuffer;
3347ec681f3Smrg
3357ec681f3Smrg/**
3367ec681f3Smrg * Allocates a buffer that matches the passed AHardwareBuffer_Desc.
3377ec681f3Smrg *
3387ec681f3Smrg * If allocation succeeds, the buffer can be used according to the
3397ec681f3Smrg * usage flags specified in its description. If a buffer is used in ways
3407ec681f3Smrg * not compatible with its usage flags, the results are undefined and
3417ec681f3Smrg * may include program termination.
3427ec681f3Smrg *
3437ec681f3Smrg * Available since API level 26.
3447ec681f3Smrg *
3457ec681f3Smrg * \return 0 on success, or an error number of the allocation fails for
3467ec681f3Smrg * any reason. The returned buffer has a reference count of 1.
3477ec681f3Smrg */
3487ec681f3Smrgint AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc,
3497ec681f3Smrg        AHardwareBuffer** outBuffer) __INTRODUCED_IN(26);
3507ec681f3Smrg/**
3517ec681f3Smrg * Acquire a reference on the given AHardwareBuffer object.
3527ec681f3Smrg *
3537ec681f3Smrg * This prevents the object from being deleted until the last reference
3547ec681f3Smrg * is removed.
3557ec681f3Smrg *
3567ec681f3Smrg * Available since API level 26.
3577ec681f3Smrg */
3587ec681f3Smrgvoid AHardwareBuffer_acquire(AHardwareBuffer* buffer) __INTRODUCED_IN(26);
3597ec681f3Smrg
3607ec681f3Smrg/**
3617ec681f3Smrg * Remove a reference that was previously acquired with
3627ec681f3Smrg * AHardwareBuffer_acquire() or AHardwareBuffer_allocate().
3637ec681f3Smrg *
3647ec681f3Smrg * Available since API level 26.
3657ec681f3Smrg */
3667ec681f3Smrgvoid AHardwareBuffer_release(AHardwareBuffer* buffer) __INTRODUCED_IN(26);
3677ec681f3Smrg
3687ec681f3Smrg/**
3697ec681f3Smrg * Return a description of the AHardwareBuffer in the passed
3707ec681f3Smrg * AHardwareBuffer_Desc struct.
3717ec681f3Smrg *
3727ec681f3Smrg * Available since API level 26.
3737ec681f3Smrg */
3747ec681f3Smrgvoid AHardwareBuffer_describe(const AHardwareBuffer* buffer,
3757ec681f3Smrg        AHardwareBuffer_Desc* outDesc) __INTRODUCED_IN(26);
3767ec681f3Smrg
3777ec681f3Smrg/**
3787ec681f3Smrg * Lock the AHardwareBuffer for direct CPU access.
3797ec681f3Smrg *
3807ec681f3Smrg * This function can lock the buffer for either reading or writing.
3817ec681f3Smrg * It may block if the hardware needs to finish rendering, if CPU caches
3827ec681f3Smrg * need to be synchronized, or possibly for other implementation-
3837ec681f3Smrg * specific reasons.
3847ec681f3Smrg *
3857ec681f3Smrg * The passed AHardwareBuffer must have one layer, otherwise the call
3867ec681f3Smrg * will fail.
3877ec681f3Smrg *
3887ec681f3Smrg * If \a fence is not negative, it specifies a fence file descriptor on
3897ec681f3Smrg * which to wait before locking the buffer. If it's negative, the caller
3907ec681f3Smrg * is responsible for ensuring that writes to the buffer have completed
3917ec681f3Smrg * before calling this function.  Using this parameter is more efficient
3927ec681f3Smrg * than waiting on the fence and then calling this function.
3937ec681f3Smrg *
3947ec681f3Smrg * The \a usage parameter may only specify AHARDWAREBUFFER_USAGE_CPU_*.
3957ec681f3Smrg * If set, then outVirtualAddress is filled with the address of the
3967ec681f3Smrg * buffer in virtual memory. The flags must also be compatible with
3977ec681f3Smrg * usage flags specified at buffer creation: if a read flag is passed,
3987ec681f3Smrg * the buffer must have been created with
3997ec681f3Smrg * AHARDWAREBUFFER_USAGE_CPU_READ_RARELY or
4007ec681f3Smrg * AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN. If a write flag is passed, it
4017ec681f3Smrg * must have been created with AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY or
4027ec681f3Smrg * AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN.
4037ec681f3Smrg *
4047ec681f3Smrg * If \a rect is not NULL, the caller promises to modify only data in
4057ec681f3Smrg * the area specified by rect. If rect is NULL, the caller may modify
4067ec681f3Smrg * the contents of the entire buffer. The content of the buffer outside
4077ec681f3Smrg * of the specified rect is NOT modified by this call.
4087ec681f3Smrg *
4097ec681f3Smrg * It is legal for several different threads to lock a buffer for read
4107ec681f3Smrg * access; none of the threads are blocked.
4117ec681f3Smrg *
4127ec681f3Smrg * Locking a buffer simultaneously for write or read/write is undefined,
4137ec681f3Smrg * but will neither terminate the process nor block the caller.
4147ec681f3Smrg * AHardwareBuffer_lock may return an error or leave the buffer's
4157ec681f3Smrg * content in an indeterminate state.
4167ec681f3Smrg *
4177ec681f3Smrg * If the buffer has AHARDWAREBUFFER_FORMAT_BLOB, it is legal lock it
4187ec681f3Smrg * for reading and writing in multiple threads and/or processes
4197ec681f3Smrg * simultaneously, and the contents of the buffer behave like shared
4207ec681f3Smrg * memory.
4217ec681f3Smrg *
4227ec681f3Smrg * Available since API level 26.
4237ec681f3Smrg *
4247ec681f3Smrg * \return 0 on success. -EINVAL if \a buffer is NULL, the usage flags
4257ec681f3Smrg * are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer
4267ec681f3Smrg * has more than one layer. Error number if the lock fails for any other
4277ec681f3Smrg * reason.
4287ec681f3Smrg */
4297ec681f3Smrgint AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage,
4307ec681f3Smrg        int32_t fence, const ARect* rect, void** outVirtualAddress) __INTRODUCED_IN(26);
4317ec681f3Smrg
4327ec681f3Smrg/**
4337ec681f3Smrg * Lock a potentially multi-planar AHardwareBuffer for direct CPU access.
4347ec681f3Smrg *
4357ec681f3Smrg * This function is similar to AHardwareBuffer_lock, but can lock multi-planar
4367ec681f3Smrg * formats. The locked planes are returned in the \a outPlanes argument. Note,
4377ec681f3Smrg * that multi-planar should not be confused with multi-layer images, which this
4387ec681f3Smrg * locking function does not support.
4397ec681f3Smrg *
4407ec681f3Smrg * YUV formats are always represented by three separate planes of data, one for
4417ec681f3Smrg * each color plane. The order of planes in the array is guaranteed such that
4427ec681f3Smrg * plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V
4437ec681f3Smrg * (Cr). All other formats are represented by a single plane.
4447ec681f3Smrg *
4457ec681f3Smrg * Additional information always accompanies the buffers, describing the row
4467ec681f3Smrg * stride and the pixel stride for each plane.
4477ec681f3Smrg *
4487ec681f3Smrg * In case the buffer cannot be locked, \a outPlanes will contain zero planes.
4497ec681f3Smrg *
4507ec681f3Smrg * See the AHardwareBuffer_lock documentation for all other locking semantics.
4517ec681f3Smrg *
4527ec681f3Smrg * Available since API level 29.
4537ec681f3Smrg *
4547ec681f3Smrg * \return 0 on success. -EINVAL if \a buffer is NULL, the usage flags
4557ec681f3Smrg * are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer
4567ec681f3Smrg * has more than one layer. Error number if the lock fails for any other
4577ec681f3Smrg * reason.
4587ec681f3Smrg */
4597ec681f3Smrgint AHardwareBuffer_lockPlanes(AHardwareBuffer* buffer, uint64_t usage,
4607ec681f3Smrg        int32_t fence, const ARect* rect, AHardwareBuffer_Planes* outPlanes) __INTRODUCED_IN(29);
4617ec681f3Smrg
4627ec681f3Smrg/**
4637ec681f3Smrg * Unlock the AHardwareBuffer from direct CPU access.
4647ec681f3Smrg *
4657ec681f3Smrg * Must be called after all changes to the buffer are completed by the
4667ec681f3Smrg * caller.  If \a fence is NULL, the function will block until all work
4677ec681f3Smrg * is completed.  Otherwise, \a fence will be set either to a valid file
4687ec681f3Smrg * descriptor or to -1.  The file descriptor will become signaled once
4697ec681f3Smrg * the unlocking is complete and buffer contents are updated.
4707ec681f3Smrg * The caller is responsible for closing the file descriptor once it's
4717ec681f3Smrg * no longer needed.  The value -1 indicates that unlocking has already
4727ec681f3Smrg * completed before the function returned and no further operations are
4737ec681f3Smrg * necessary.
4747ec681f3Smrg *
4757ec681f3Smrg * Available since API level 26.
4767ec681f3Smrg *
4777ec681f3Smrg * \return 0 on success. -EINVAL if \a buffer is NULL. Error number if
4787ec681f3Smrg * the unlock fails for any reason.
4797ec681f3Smrg */
4807ec681f3Smrgint AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) __INTRODUCED_IN(26);
4817ec681f3Smrg
4827ec681f3Smrg/**
4837ec681f3Smrg * Send the AHardwareBuffer to an AF_UNIX socket.
4847ec681f3Smrg *
4857ec681f3Smrg * Available since API level 26.
4867ec681f3Smrg *
4877ec681f3Smrg * \return 0 on success, -EINVAL if \a buffer is NULL, or an error
4887ec681f3Smrg * number if the operation fails for any reason.
4897ec681f3Smrg */
4907ec681f3Smrgint AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) __INTRODUCED_IN(26);
4917ec681f3Smrg
4927ec681f3Smrg/**
4937ec681f3Smrg * Receive an AHardwareBuffer from an AF_UNIX socket.
4947ec681f3Smrg *
4957ec681f3Smrg * Available since API level 26.
4967ec681f3Smrg *
4977ec681f3Smrg * \return 0 on success, -EINVAL if \a outBuffer is NULL, or an error
4987ec681f3Smrg * number if the operation fails for any reason.
4997ec681f3Smrg */
5007ec681f3Smrgint AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) __INTRODUCED_IN(26);
5017ec681f3Smrg
5027ec681f3Smrg/**
5037ec681f3Smrg * Test whether the given format and usage flag combination is
5047ec681f3Smrg * allocatable.
5057ec681f3Smrg *
5067ec681f3Smrg * If this function returns true, it means that a buffer with the given
5077ec681f3Smrg * description can be allocated on this implementation, unless resource
5087ec681f3Smrg * exhaustion occurs. If this function returns false, it means that the
5097ec681f3Smrg * allocation of the given description will never succeed.
5107ec681f3Smrg *
5117ec681f3Smrg * The return value of this function may depend on all fields in the
5127ec681f3Smrg * description, except stride, which is always ignored. For example,
5137ec681f3Smrg * some implementations have implementation-defined limits on texture
5147ec681f3Smrg * size and layer count.
5157ec681f3Smrg *
5167ec681f3Smrg * Available since API level 29.
5177ec681f3Smrg *
5187ec681f3Smrg * \return 1 if the format and usage flag combination is allocatable,
5197ec681f3Smrg *     0 otherwise.
5207ec681f3Smrg */
5217ec681f3Smrgint AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) __INTRODUCED_IN(29);
5227ec681f3Smrg
5237ec681f3Smrg/**
5247ec681f3Smrg * Lock an AHardwareBuffer for direct CPU access.
5257ec681f3Smrg *
5267ec681f3Smrg * This function is the same as the above lock function, but passes back
5277ec681f3Smrg * additional information about the bytes per pixel and the bytes per stride
5287ec681f3Smrg * of the locked buffer.  If the bytes per pixel or bytes per stride are unknown
5297ec681f3Smrg * or variable, or if the underlying mapper implementation does not support returning
5307ec681f3Smrg * additional information, then this call will fail with INVALID_OPERATION
5317ec681f3Smrg *
5327ec681f3Smrg * Available since API level 29.
5337ec681f3Smrg */
5347ec681f3Smrgint AHardwareBuffer_lockAndGetInfo(AHardwareBuffer* buffer, uint64_t usage,
5357ec681f3Smrg        int32_t fence, const ARect* rect, void** outVirtualAddress,
5367ec681f3Smrg        int32_t* outBytesPerPixel, int32_t* outBytesPerStride) __INTRODUCED_IN(29);
5377ec681f3Smrg
5387ec681f3Smrg__END_DECLS
5397ec681f3Smrg
5407ec681f3Smrg#endif // ANDROID_HARDWARE_BUFFER_H
5417ec681f3Smrg
5427ec681f3Smrg/** @} */
543