Path: blob/21.2-virgl/include/android_stub/android/hardware_buffer.h
4547 views
/*1* Copyright 2017 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516/**17* @file hardware_buffer.h18* @brief API for native hardware buffers.19*/20/**21* @defgroup AHardwareBuffer Native Hardware Buffer22*23* AHardwareBuffer objects represent chunks of memory that can be24* accessed by various hardware components in the system. It can be25* easily converted to the Java counterpart26* android.hardware.HardwareBuffer and passed between processes using27* Binder. All operations involving AHardwareBuffer and HardwareBuffer28* are zero-copy, i.e., passing AHardwareBuffer to another process29* creates a shared view of the same region of memory.30*31* AHardwareBuffers can be bound to EGL/OpenGL and Vulkan primitives.32* For EGL, use the extension function eglGetNativeClientBufferANDROID33* to obtain an EGLClientBuffer and pass it directly to34* eglCreateImageKHR. Refer to the EGL extensions35* EGL_ANDROID_get_native_client_buffer and36* EGL_ANDROID_image_native_buffer for more information. In Vulkan,37* the contents of the AHardwareBuffer can be accessed as external38* memory. See the VK_ANDROID_external_memory_android_hardware_buffer39* extension for details.40*41* @{42*/4344#ifndef ANDROID_HARDWARE_BUFFER_H45#define ANDROID_HARDWARE_BUFFER_H4647#include <inttypes.h>4849#include <sys/cdefs.h>5051#include <android/rect.h>5253__BEGIN_DECLS5455/**56* Buffer pixel formats.57*/58enum AHardwareBuffer_Format {59/**60* Corresponding formats:61* Vulkan: VK_FORMAT_R8G8B8A8_UNORM62* OpenGL ES: GL_RGBA863*/64AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM = 1,6566/**67* 32 bits per pixel, 8 bits per channel format where alpha values are68* ignored (always opaque).69* Corresponding formats:70* Vulkan: VK_FORMAT_R8G8B8A8_UNORM71* OpenGL ES: GL_RGB872*/73AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM = 2,7475/**76* Corresponding formats:77* Vulkan: VK_FORMAT_R8G8B8_UNORM78* OpenGL ES: GL_RGB879*/80AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM = 3,8182/**83* Corresponding formats:84* Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK1685* OpenGL ES: GL_RGB56586*/87AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM = 4,8889/**90* Corresponding formats:91* Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT92* OpenGL ES: GL_RGBA16F93*/94AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT = 0x16,9596/**97* Corresponding formats:98* Vulkan: VK_FORMAT_A2B10G10R10_UNORM_PACK3299* OpenGL ES: GL_RGB10_A2100*/101AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM = 0x2b,102103/**104* Opaque binary blob format.105* Must have height 1 and one layer, with width equal to the buffer106* size in bytes. Corresponds to Vulkan buffers and OpenGL buffer107* objects. Can be bound to the latter using GL_EXT_external_buffer.108*/109AHARDWAREBUFFER_FORMAT_BLOB = 0x21,110111/**112* Corresponding formats:113* Vulkan: VK_FORMAT_D16_UNORM114* OpenGL ES: GL_DEPTH_COMPONENT16115*/116AHARDWAREBUFFER_FORMAT_D16_UNORM = 0x30,117118/**119* Corresponding formats:120* Vulkan: VK_FORMAT_X8_D24_UNORM_PACK32121* OpenGL ES: GL_DEPTH_COMPONENT24122*/123AHARDWAREBUFFER_FORMAT_D24_UNORM = 0x31,124125/**126* Corresponding formats:127* Vulkan: VK_FORMAT_D24_UNORM_S8_UINT128* OpenGL ES: GL_DEPTH24_STENCIL8129*/130AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT = 0x32,131132/**133* Corresponding formats:134* Vulkan: VK_FORMAT_D32_SFLOAT135* OpenGL ES: GL_DEPTH_COMPONENT32F136*/137AHARDWAREBUFFER_FORMAT_D32_FLOAT = 0x33,138139/**140* Corresponding formats:141* Vulkan: VK_FORMAT_D32_SFLOAT_S8_UINT142* OpenGL ES: GL_DEPTH32F_STENCIL8143*/144AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT = 0x34,145146/**147* Corresponding formats:148* Vulkan: VK_FORMAT_S8_UINT149* OpenGL ES: GL_STENCIL_INDEX8150*/151AHARDWAREBUFFER_FORMAT_S8_UINT = 0x35,152153/**154* YUV 420 888 format.155* Must have an even width and height. Can be accessed in OpenGL156* shaders through an external sampler. Does not support mip-maps157* cube-maps or multi-layered textures.158*/159AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420 = 0x23,160};161162/**163* Buffer usage flags, specifying how the buffer will be accessed.164*/165enum AHardwareBuffer_UsageFlags {166/// The buffer will never be locked for direct CPU reads using the167/// AHardwareBuffer_lock() function. Note that reading the buffer168/// using OpenGL or Vulkan functions or memory mappings is still169/// allowed.170AHARDWAREBUFFER_USAGE_CPU_READ_NEVER = 0UL,171/// The buffer will sometimes be locked for direct CPU reads using172/// the AHardwareBuffer_lock() function. Note that reading the173/// buffer using OpenGL or Vulkan functions or memory mappings174/// does not require the presence of this flag.175AHARDWAREBUFFER_USAGE_CPU_READ_RARELY = 2UL,176/// The buffer will often be locked for direct CPU reads using177/// the AHardwareBuffer_lock() function. Note that reading the178/// buffer using OpenGL or Vulkan functions or memory mappings179/// does not require the presence of this flag.180AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN = 3UL,181/// CPU read value mask.182AHARDWAREBUFFER_USAGE_CPU_READ_MASK = 0xFUL,183184/// The buffer will never be locked for direct CPU writes using the185/// AHardwareBuffer_lock() function. Note that writing the buffer186/// using OpenGL or Vulkan functions or memory mappings is still187/// allowed.188AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER = 0UL << 4,189/// The buffer will sometimes be locked for direct CPU writes using190/// the AHardwareBuffer_lock() function. Note that writing the191/// buffer using OpenGL or Vulkan functions or memory mappings192/// does not require the presence of this flag.193AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY = 2UL << 4,194/// The buffer will often be locked for direct CPU writes using195/// the AHardwareBuffer_lock() function. Note that writing the196/// buffer using OpenGL or Vulkan functions or memory mappings197/// does not require the presence of this flag.198AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN = 3UL << 4,199/// CPU write value mask.200AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK = 0xFUL << 4,201202/// The buffer will be read from by the GPU as a texture.203AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE = 1UL << 8,204/// The buffer will be written to by the GPU as a framebuffer attachment.205AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER = 1UL << 9,206/**207* The buffer will be written to by the GPU as a framebuffer208* attachment.209*210* Note that the name of this flag is somewhat misleading: it does211* not imply that the buffer contains a color format. A buffer with212* depth or stencil format that will be used as a framebuffer213* attachment should also have this flag. Use the equivalent flag214* AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER to avoid this confusion.215*/216AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,217/**218* The buffer will be used as a composer HAL overlay layer.219*220* This flag is currently only needed when using ASurfaceTransaction_setBuffer221* to set a buffer. In all other cases, the framework adds this flag222* internally to buffers that could be presented in a composer overlay.223* ASurfaceTransaction_setBuffer is special because it uses buffers allocated224* directly through AHardwareBuffer_allocate instead of buffers allocated225* by the framework.226*/227AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY = 1ULL << 11,228/**229* The buffer is protected from direct CPU access or being read by230* non-secure hardware, such as video encoders.231*232* This flag is incompatible with CPU read and write flags. It is233* mainly used when handling DRM video. Refer to the EGL extension234* EGL_EXT_protected_content and GL extension235* GL_EXT_protected_textures for more information on how these236* buffers are expected to behave.237*/238AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT = 1UL << 14,239/// The buffer will be read by a hardware video encoder.240AHARDWAREBUFFER_USAGE_VIDEO_ENCODE = 1UL << 16,241/**242* The buffer will be used for direct writes from sensors.243* When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB.244*/245AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA = 1UL << 23,246/**247* The buffer will be used as a shader storage or uniform buffer object.248* When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB.249*/250AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER = 1UL << 24,251/**252* The buffer will be used as a cube map texture.253* When this flag is present, the buffer must have a layer count254* that is a multiple of 6. Note that buffers with this flag must be255* bound to OpenGL textures using the extension256* GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image.257*/258AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP = 1UL << 25,259/**260* The buffer contains a complete mipmap hierarchy.261* Note that buffers with this flag must be bound to OpenGL textures using262* the extension GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image.263*/264AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE = 1UL << 26,265266AHARDWAREBUFFER_USAGE_VENDOR_0 = 1ULL << 28,267AHARDWAREBUFFER_USAGE_VENDOR_1 = 1ULL << 29,268AHARDWAREBUFFER_USAGE_VENDOR_2 = 1ULL << 30,269AHARDWAREBUFFER_USAGE_VENDOR_3 = 1ULL << 31,270AHARDWAREBUFFER_USAGE_VENDOR_4 = 1ULL << 48,271AHARDWAREBUFFER_USAGE_VENDOR_5 = 1ULL << 49,272AHARDWAREBUFFER_USAGE_VENDOR_6 = 1ULL << 50,273AHARDWAREBUFFER_USAGE_VENDOR_7 = 1ULL << 51,274AHARDWAREBUFFER_USAGE_VENDOR_8 = 1ULL << 52,275AHARDWAREBUFFER_USAGE_VENDOR_9 = 1ULL << 53,276AHARDWAREBUFFER_USAGE_VENDOR_10 = 1ULL << 54,277AHARDWAREBUFFER_USAGE_VENDOR_11 = 1ULL << 55,278AHARDWAREBUFFER_USAGE_VENDOR_12 = 1ULL << 56,279AHARDWAREBUFFER_USAGE_VENDOR_13 = 1ULL << 57,280AHARDWAREBUFFER_USAGE_VENDOR_14 = 1ULL << 58,281AHARDWAREBUFFER_USAGE_VENDOR_15 = 1ULL << 59,282AHARDWAREBUFFER_USAGE_VENDOR_16 = 1ULL << 60,283AHARDWAREBUFFER_USAGE_VENDOR_17 = 1ULL << 61,284AHARDWAREBUFFER_USAGE_VENDOR_18 = 1ULL << 62,285AHARDWAREBUFFER_USAGE_VENDOR_19 = 1ULL << 63,286};287288/**289* Buffer description. Used for allocating new buffers and querying290* parameters of existing ones.291*/292typedef struct AHardwareBuffer_Desc {293uint32_t width; ///< Width in pixels.294uint32_t height; ///< Height in pixels.295/**296* Number of images in an image array. AHardwareBuffers with one297* layer correspond to regular 2D textures. AHardwareBuffers with298* more than layer correspond to texture arrays. If the layer count299* is a multiple of 6 and the usage flag300* AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP is present, the buffer is301* a cube map or a cube map array.302*/303uint32_t layers;304uint32_t format; ///< One of AHardwareBuffer_Format.305uint64_t usage; ///< Combination of AHardwareBuffer_UsageFlags.306uint32_t stride; ///< Row stride in pixels, ignored for AHardwareBuffer_allocate()307uint32_t rfu0; ///< Initialize to zero, reserved for future use.308uint64_t rfu1; ///< Initialize to zero, reserved for future use.309} AHardwareBuffer_Desc;310311/**312* Holds data for a single image plane.313*/314typedef struct AHardwareBuffer_Plane {315void* data; ///< Points to first byte in plane316uint32_t pixelStride; ///< Distance in bytes from the color channel of one pixel to the next317uint32_t rowStride; ///< Distance in bytes from the first value of one row of the image to318/// the first value of the next row.319} AHardwareBuffer_Plane;320321/**322* Holds all image planes that contain the pixel data.323*/324typedef struct AHardwareBuffer_Planes {325uint32_t planeCount; ///< Number of distinct planes326AHardwareBuffer_Plane planes[4]; ///< Array of image planes327} AHardwareBuffer_Planes;328329/**330* Opaque handle for a native hardware buffer.331*/332typedef struct AHardwareBuffer AHardwareBuffer;333334#if __ANDROID_API__ >= 26335336/**337* Allocates a buffer that matches the passed AHardwareBuffer_Desc.338*339* If allocation succeeds, the buffer can be used according to the340* usage flags specified in its description. If a buffer is used in ways341* not compatible with its usage flags, the results are undefined and342* may include program termination.343*344* Available since API level 26.345*346* \return 0 on success, or an error number of the allocation fails for347* any reason. The returned buffer has a reference count of 1.348*/349int AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc,350AHardwareBuffer** outBuffer) __INTRODUCED_IN(26);351/**352* Acquire a reference on the given AHardwareBuffer object.353*354* This prevents the object from being deleted until the last reference355* is removed.356*357* Available since API level 26.358*/359void AHardwareBuffer_acquire(AHardwareBuffer* buffer) __INTRODUCED_IN(26);360361/**362* Remove a reference that was previously acquired with363* AHardwareBuffer_acquire() or AHardwareBuffer_allocate().364*365* Available since API level 26.366*/367void AHardwareBuffer_release(AHardwareBuffer* buffer) __INTRODUCED_IN(26);368369/**370* Return a description of the AHardwareBuffer in the passed371* AHardwareBuffer_Desc struct.372*373* Available since API level 26.374*/375void AHardwareBuffer_describe(const AHardwareBuffer* buffer,376AHardwareBuffer_Desc* outDesc) __INTRODUCED_IN(26);377378/**379* Lock the AHardwareBuffer for direct CPU access.380*381* This function can lock the buffer for either reading or writing.382* It may block if the hardware needs to finish rendering, if CPU caches383* need to be synchronized, or possibly for other implementation-384* specific reasons.385*386* The passed AHardwareBuffer must have one layer, otherwise the call387* will fail.388*389* If \a fence is not negative, it specifies a fence file descriptor on390* which to wait before locking the buffer. If it's negative, the caller391* is responsible for ensuring that writes to the buffer have completed392* before calling this function. Using this parameter is more efficient393* than waiting on the fence and then calling this function.394*395* The \a usage parameter may only specify AHARDWAREBUFFER_USAGE_CPU_*.396* If set, then outVirtualAddress is filled with the address of the397* buffer in virtual memory. The flags must also be compatible with398* usage flags specified at buffer creation: if a read flag is passed,399* the buffer must have been created with400* AHARDWAREBUFFER_USAGE_CPU_READ_RARELY or401* AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN. If a write flag is passed, it402* must have been created with AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY or403* AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN.404*405* If \a rect is not NULL, the caller promises to modify only data in406* the area specified by rect. If rect is NULL, the caller may modify407* the contents of the entire buffer. The content of the buffer outside408* of the specified rect is NOT modified by this call.409*410* It is legal for several different threads to lock a buffer for read411* access; none of the threads are blocked.412*413* Locking a buffer simultaneously for write or read/write is undefined,414* but will neither terminate the process nor block the caller.415* AHardwareBuffer_lock may return an error or leave the buffer's416* content in an indeterminate state.417*418* If the buffer has AHARDWAREBUFFER_FORMAT_BLOB, it is legal lock it419* for reading and writing in multiple threads and/or processes420* simultaneously, and the contents of the buffer behave like shared421* memory.422*423* Available since API level 26.424*425* \return 0 on success. -EINVAL if \a buffer is NULL, the usage flags426* are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer427* has more than one layer. Error number if the lock fails for any other428* reason.429*/430int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage,431int32_t fence, const ARect* rect, void** outVirtualAddress) __INTRODUCED_IN(26);432433/**434* Lock a potentially multi-planar AHardwareBuffer for direct CPU access.435*436* This function is similar to AHardwareBuffer_lock, but can lock multi-planar437* formats. The locked planes are returned in the \a outPlanes argument. Note,438* that multi-planar should not be confused with multi-layer images, which this439* locking function does not support.440*441* YUV formats are always represented by three separate planes of data, one for442* each color plane. The order of planes in the array is guaranteed such that443* plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V444* (Cr). All other formats are represented by a single plane.445*446* Additional information always accompanies the buffers, describing the row447* stride and the pixel stride for each plane.448*449* In case the buffer cannot be locked, \a outPlanes will contain zero planes.450*451* See the AHardwareBuffer_lock documentation for all other locking semantics.452*453* Available since API level 29.454*455* \return 0 on success. -EINVAL if \a buffer is NULL, the usage flags456* are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer457* has more than one layer. Error number if the lock fails for any other458* reason.459*/460int AHardwareBuffer_lockPlanes(AHardwareBuffer* buffer, uint64_t usage,461int32_t fence, const ARect* rect, AHardwareBuffer_Planes* outPlanes) __INTRODUCED_IN(29);462463/**464* Unlock the AHardwareBuffer from direct CPU access.465*466* Must be called after all changes to the buffer are completed by the467* caller. If \a fence is NULL, the function will block until all work468* is completed. Otherwise, \a fence will be set either to a valid file469* descriptor or to -1. The file descriptor will become signaled once470* the unlocking is complete and buffer contents are updated.471* The caller is responsible for closing the file descriptor once it's472* no longer needed. The value -1 indicates that unlocking has already473* completed before the function returned and no further operations are474* necessary.475*476* Available since API level 26.477*478* \return 0 on success. -EINVAL if \a buffer is NULL. Error number if479* the unlock fails for any reason.480*/481int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) __INTRODUCED_IN(26);482483/**484* Send the AHardwareBuffer to an AF_UNIX socket.485*486* Available since API level 26.487*488* \return 0 on success, -EINVAL if \a buffer is NULL, or an error489* number if the operation fails for any reason.490*/491int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) __INTRODUCED_IN(26);492493/**494* Receive an AHardwareBuffer from an AF_UNIX socket.495*496* Available since API level 26.497*498* \return 0 on success, -EINVAL if \a outBuffer is NULL, or an error499* number if the operation fails for any reason.500*/501int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) __INTRODUCED_IN(26);502503#endif // __ANDROID_API__ >= 26504505#if __ANDROID_API__ >= 29506507/**508* Test whether the given format and usage flag combination is509* allocatable.510*511* If this function returns true, it means that a buffer with the given512* description can be allocated on this implementation, unless resource513* exhaustion occurs. If this function returns false, it means that the514* allocation of the given description will never succeed.515*516* The return value of this function may depend on all fields in the517* description, except stride, which is always ignored. For example,518* some implementations have implementation-defined limits on texture519* size and layer count.520*521* Available since API level 29.522*523* \return 1 if the format and usage flag combination is allocatable,524* 0 otherwise.525*/526int AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) __INTRODUCED_IN(29);527528/**529* Lock an AHardwareBuffer for direct CPU access.530*531* This function is the same as the above lock function, but passes back532* additional information about the bytes per pixel and the bytes per stride533* of the locked buffer. If the bytes per pixel or bytes per stride are unknown534* or variable, or if the underlying mapper implementation does not support returning535* additional information, then this call will fail with INVALID_OPERATION536*537* Available since API level 29.538*/539int AHardwareBuffer_lockAndGetInfo(AHardwareBuffer* buffer, uint64_t usage,540int32_t fence, const ARect* rect, void** outVirtualAddress,541int32_t* outBytesPerPixel, int32_t* outBytesPerStride) __INTRODUCED_IN(29);542#endif // __ANDROID_API__ >= 29543544__END_DECLS545546#endif // ANDROID_HARDWARE_BUFFER_H547548/** @} */549550551