Path: blob/21.2-virgl/include/android_stub/hardware/gralloc.h
7099 views
/*1* Copyright (C) 2008 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*/151617#ifndef ANDROID_GRALLOC_INTERFACE_H18#define ANDROID_GRALLOC_INTERFACE_H1920#include <system/graphics.h>21#include <hardware/hardware.h>2223#include <stdint.h>24#include <sys/cdefs.h>25#include <sys/types.h>2627#include <cutils/native_handle.h>2829#include <hardware/hardware.h>30#include <hardware/fb.h>3132__BEGIN_DECLS3334/**35* Module versioning information for the Gralloc hardware module, based on36* gralloc_module_t.common.module_api_version.37*38* Version History:39*40* GRALLOC_MODULE_API_VERSION_0_1:41* Initial Gralloc hardware module API.42*43* GRALLOC_MODULE_API_VERSION_0_2:44* Add support for flexible YCbCr format with (*lock_ycbcr)() method.45*46* GRALLOC_MODULE_API_VERSION_0_3:47* Add support for fence passing to/from lock/unlock.48*/4950#define GRALLOC_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)51#define GRALLOC_MODULE_API_VERSION_0_2 HARDWARE_MODULE_API_VERSION(0, 2)52#define GRALLOC_MODULE_API_VERSION_0_3 HARDWARE_MODULE_API_VERSION(0, 3)5354#define GRALLOC_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1)5556/**57* The id of this module58*/59#define GRALLOC_HARDWARE_MODULE_ID "gralloc"6061/**62* Name of the graphics device to open63*/6465#define GRALLOC_HARDWARE_GPU0 "gpu0"6667enum {68/* buffer is never read in software */69GRALLOC_USAGE_SW_READ_NEVER = 0x00000000U,70/* buffer is rarely read in software */71GRALLOC_USAGE_SW_READ_RARELY = 0x00000002U,72/* buffer is often read in software */73GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003U,74/* mask for the software read values */75GRALLOC_USAGE_SW_READ_MASK = 0x0000000FU,7677/* buffer is never written in software */78GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000U,79/* buffer is rarely written in software */80GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020U,81/* buffer is often written in software */82GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030U,83/* mask for the software write values */84GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0U,8586/* buffer will be used as an OpenGL ES texture */87GRALLOC_USAGE_HW_TEXTURE = 0x00000100U,88/* buffer will be used as an OpenGL ES render target */89GRALLOC_USAGE_HW_RENDER = 0x00000200U,90/* buffer will be used by the 2D hardware blitter */91GRALLOC_USAGE_HW_2D = 0x00000400U,92/* buffer will be used by the HWComposer HAL module */93GRALLOC_USAGE_HW_COMPOSER = 0x00000800U,94/* buffer will be used with the framebuffer device */95GRALLOC_USAGE_HW_FB = 0x00001000U,9697/* buffer should be displayed full-screen on an external display when98* possible */99GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000U,100101/* Must have a hardware-protected path to external display sink for102* this buffer. If a hardware-protected path is not available, then103* either don't composite only this buffer (preferred) to the104* external sink, or (less desirable) do not route the entire105* composition to the external sink. */106GRALLOC_USAGE_PROTECTED = 0x00004000U,107108/* buffer may be used as a cursor */109GRALLOC_USAGE_CURSOR = 0x00008000U,110111/* buffer will be used with the HW video encoder */112GRALLOC_USAGE_HW_VIDEO_ENCODER = 0x00010000U,113/* buffer will be written by the HW camera pipeline */114GRALLOC_USAGE_HW_CAMERA_WRITE = 0x00020000U,115/* buffer will be read by the HW camera pipeline */116GRALLOC_USAGE_HW_CAMERA_READ = 0x00040000U,117/* buffer will be used as part of zero-shutter-lag queue */118GRALLOC_USAGE_HW_CAMERA_ZSL = 0x00060000U,119/* mask for the camera access values */120GRALLOC_USAGE_HW_CAMERA_MASK = 0x00060000U,121/* mask for the software usage bit-mask */122GRALLOC_USAGE_HW_MASK = 0x00071F00U,123124/* buffer will be used as a RenderScript Allocation */125GRALLOC_USAGE_RENDERSCRIPT = 0x00100000U,126127/* Set by the consumer to indicate to the producer that they may attach a128* buffer that they did not detach from the BufferQueue. Will be filtered129* out by GRALLOC_USAGE_ALLOC_MASK, so gralloc modules will not need to130* handle this flag. */131GRALLOC_USAGE_FOREIGN_BUFFERS = 0x00200000U,132133/* buffer will be used as input to HW HEIC image encoder */134GRALLOC_USAGE_HW_IMAGE_ENCODER = 0x08000000U,135136/* Mask of all flags which could be passed to a gralloc module for buffer137* allocation. Any flags not in this mask do not need to be handled by138* gralloc modules. */139GRALLOC_USAGE_ALLOC_MASK = ~(GRALLOC_USAGE_FOREIGN_BUFFERS),140141/* implementation-specific private usage flags */142GRALLOC_USAGE_PRIVATE_0 = 0x10000000U,143GRALLOC_USAGE_PRIVATE_1 = 0x20000000U,144GRALLOC_USAGE_PRIVATE_2 = 0x40000000U,145GRALLOC_USAGE_PRIVATE_3 = 0x80000000U,146GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000U,147};148149/*****************************************************************************/150151/**152* Every hardware module must have a data structure named HAL_MODULE_INFO_SYM153* and the fields of this data structure must begin with hw_module_t154* followed by module specific information.155*/156typedef struct gralloc_module_t {157struct hw_module_t common;158159/*160* (*registerBuffer)() must be called before a buffer_handle_t that has not161* been created with (*alloc_device_t::alloc)() can be used.162*163* This is intended to be used with buffer_handle_t's that have been164* received in this process through IPC.165*166* This function checks that the handle is indeed a valid one and prepares167* it for use with (*lock)() and (*unlock)().168*169* It is not necessary to call (*registerBuffer)() on a handle created170* with (*alloc_device_t::alloc)().171*172* returns an error if this buffer_handle_t is not valid.173*/174int (*registerBuffer)(struct gralloc_module_t const* module,175buffer_handle_t handle);176177/*178* (*unregisterBuffer)() is called once this handle is no longer needed in179* this process. After this call, it is an error to call (*lock)(),180* (*unlock)(), or (*registerBuffer)().181*182* This function doesn't close or free the handle itself; this is done183* by other means, usually through libcutils's native_handle_close() and184* native_handle_free().185*186* It is an error to call (*unregisterBuffer)() on a buffer that wasn't187* explicitly registered first.188*/189int (*unregisterBuffer)(struct gralloc_module_t const* module,190buffer_handle_t handle);191192/*193* The (*lock)() method is called before a buffer is accessed for the194* specified usage. This call may block, for instance if the h/w needs195* to finish rendering or if CPU caches need to be synchronized.196*197* The caller promises to modify only pixels in the area specified198* by (l,t,w,h).199*200* The content of the buffer outside of the specified area is NOT modified201* by this call.202*203* If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address204* of the buffer in virtual memory.205*206* Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail207* and return -EINVAL. These buffers must be locked with (*lock_ycbcr)()208* instead.209*210* THREADING CONSIDERATIONS:211*212* It is legal for several different threads to lock a buffer from213* read access, none of the threads are blocked.214*215* However, locking a buffer simultaneously for write or read/write is216* undefined, but:217* - shall not result in termination of the process218* - shall not block the caller219* It is acceptable to return an error or to leave the buffer's content220* into an indeterminate state.221*222* If the buffer was created with a usage mask incompatible with the223* requested usage flags here, -EINVAL is returned.224*225*/226227int (*lock)(struct gralloc_module_t const* module,228buffer_handle_t handle, int usage,229int l, int t, int w, int h,230void** vaddr);231232233/*234* The (*unlock)() method must be called after all changes to the buffer235* are completed.236*/237238int (*unlock)(struct gralloc_module_t const* module,239buffer_handle_t handle);240241242/* reserved for future use */243int (*perform)(struct gralloc_module_t const* module,244int operation, ... );245246/*247* The (*lock_ycbcr)() method is like the (*lock)() method, with the248* difference that it fills a struct ycbcr with a description of the buffer249* layout, and zeroes out the reserved fields.250*251* If the buffer format is not compatible with a flexible YUV format (e.g.252* the buffer layout cannot be represented with the ycbcr struct), it253* will return -EINVAL.254*255* This method must work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888256* if supported by the device, as well as with any other format that is257* requested by the multimedia codecs when they are configured with a258* flexible-YUV-compatible color-format with android native buffers.259*260* Note that this method may also be called on buffers of other formats,261* including non-YUV formats.262*263* Added in GRALLOC_MODULE_API_VERSION_0_2.264*/265266int (*lock_ycbcr)(struct gralloc_module_t const* module,267buffer_handle_t handle, int usage,268int l, int t, int w, int h,269struct android_ycbcr *ycbcr);270271/*272* The (*lockAsync)() method is like the (*lock)() method except273* that the buffer's sync fence object is passed into the lock274* call instead of requiring the caller to wait for completion.275*276* The gralloc implementation takes ownership of the fenceFd and277* is responsible for closing it when no longer needed.278*279* Added in GRALLOC_MODULE_API_VERSION_0_3.280*/281int (*lockAsync)(struct gralloc_module_t const* module,282buffer_handle_t handle, int usage,283int l, int t, int w, int h,284void** vaddr, int fenceFd);285286/*287* The (*unlockAsync)() method is like the (*unlock)() method288* except that a buffer sync fence object is returned from the289* lock call, representing the completion of any pending work290* performed by the gralloc implementation.291*292* The caller takes ownership of the fenceFd and is responsible293* for closing it when no longer needed.294*295* Added in GRALLOC_MODULE_API_VERSION_0_3.296*/297int (*unlockAsync)(struct gralloc_module_t const* module,298buffer_handle_t handle, int* fenceFd);299300/*301* The (*lockAsync_ycbcr)() method is like the (*lock_ycbcr)()302* method except that the buffer's sync fence object is passed303* into the lock call instead of requiring the caller to wait for304* completion.305*306* The gralloc implementation takes ownership of the fenceFd and307* is responsible for closing it when no longer needed.308*309* Added in GRALLOC_MODULE_API_VERSION_0_3.310*/311int (*lockAsync_ycbcr)(struct gralloc_module_t const* module,312buffer_handle_t handle, int usage,313int l, int t, int w, int h,314struct android_ycbcr *ycbcr, int fenceFd);315316/* getTransportSize(..., outNumFds, outNumInts)317* This function is mandatory on devices running IMapper2.1 or higher.318*319* Get the transport size of a buffer. An imported buffer handle is a raw320* buffer handle with the process-local runtime data appended. This321* function, for example, allows a caller to omit the process-local322* runtime data at the tail when serializing the imported buffer handle.323*324* Note that a client might or might not omit the process-local runtime325* data when sending an imported buffer handle. The mapper must support326* both cases on the receiving end.327*/328int32_t (*getTransportSize)(329struct gralloc_module_t const* module, buffer_handle_t handle, uint32_t *outNumFds,330uint32_t *outNumInts);331332/* validateBufferSize(..., w, h, format, usage, stride)333* This function is mandatory on devices running IMapper2.1 or higher.334*335* Validate that the buffer can be safely accessed by a caller who assumes336* the specified width, height, format, usage, and stride. This must at least validate337* that the buffer size is large enough. Validating the buffer against338* individual buffer attributes is optional.339*/340int32_t (*validateBufferSize)(341struct gralloc_module_t const* device, buffer_handle_t handle,342uint32_t w, uint32_t h, int32_t format, int usage,343uint32_t stride);344345/* reserved for future use */346void* reserved_proc[1];347348} gralloc_module_t;349350/*****************************************************************************/351352/**353* Every device data structure must begin with hw_device_t354* followed by module specific public methods and attributes.355*/356357typedef struct alloc_device_t {358struct hw_device_t common;359360/*361* (*alloc)() Allocates a buffer in graphic memory with the requested362* parameters and returns a buffer_handle_t and the stride in pixels to363* allow the implementation to satisfy hardware constraints on the width364* of a pixmap (eg: it may have to be multiple of 8 pixels).365* The CALLER TAKES OWNERSHIP of the buffer_handle_t.366*367* If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be368* 0, since the actual strides are available from the android_ycbcr369* structure.370*371* Returns 0 on success or -errno on error.372*/373374int (*alloc)(struct alloc_device_t* dev,375int w, int h, int format, int usage,376buffer_handle_t* handle, int* stride);377378/*379* (*free)() Frees a previously allocated buffer.380* Behavior is undefined if the buffer is still mapped in any process,381* but shall not result in termination of the program or security breaches382* (allowing a process to get access to another process' buffers).383* THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes384* invalid after the call.385*386* Returns 0 on success or -errno on error.387*/388int (*free)(struct alloc_device_t* dev,389buffer_handle_t handle);390391/* This hook is OPTIONAL.392*393* If non NULL it will be caused by SurfaceFlinger on dumpsys394*/395void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);396397void* reserved_proc[7];398} alloc_device_t;399400401/** convenience API for opening and closing a supported device */402403static inline int gralloc_open(const struct hw_module_t* module,404struct alloc_device_t** device) {405return module->methods->open(module,406GRALLOC_HARDWARE_GPU0, TO_HW_DEVICE_T_OPEN(device));407}408409static inline int gralloc_close(struct alloc_device_t* device) {410return device->common.close(&device->common);411}412413/**414* map_usage_to_memtrack should be called after allocating a gralloc buffer.415*416* @param usage - it is the flag used when alloc function is called.417*418* This function maps the gralloc usage flags to appropriate memtrack bucket.419* GrallocHAL implementers and users should make an additional ION_IOCTL_TAG420* call using the memtrack tag returned by this function. This will help the421* in-kernel memtack to categorize the memory allocated by different processes422* according to their usage.423*424*/425static inline const char* map_usage_to_memtrack(uint32_t usage) {426usage &= GRALLOC_USAGE_ALLOC_MASK;427428if ((usage & GRALLOC_USAGE_HW_CAMERA_WRITE) != 0) {429return "camera";430} else if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0 ||431(usage & GRALLOC_USAGE_EXTERNAL_DISP) != 0) {432return "video";433} else if ((usage & GRALLOC_USAGE_HW_RENDER) != 0 ||434(usage & GRALLOC_USAGE_HW_TEXTURE) != 0) {435return "gl";436} else if ((usage & GRALLOC_USAGE_HW_CAMERA_READ) != 0) {437return "camera";438} else if ((usage & GRALLOC_USAGE_SW_READ_MASK) != 0 ||439(usage & GRALLOC_USAGE_SW_WRITE_MASK) != 0) {440return "cpu";441}442return "graphics";443}444445__END_DECLS446447#endif // ANDROID_GRALLOC_INTERFACE_H448449450