Home | History | Annotate | Download | only in hal
      1 /* Copyright (c) Imagination Technologies Ltd.
      2  *
      3  * The contents of this file are subject to the MIT license as set out below.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     21  * THE SOFTWARE.
     22  */
     23 
     24 #ifndef HAL_PUBLIC_H
     25 #define HAL_PUBLIC_H
     26 
     27 /* Authors of third party hardware composer (HWC) modules will need to include
     28  * this header to access functionality in the gralloc HAL.
     29  */
     30 
     31 #include <hardware/gralloc.h>
     32 
     33 #define ALIGN(x,a)	(((x) + (a) - 1L) & ~((a) - 1L))
     34 #define HW_ALIGN	32
     35 
     36 /* Use bits [0-3] of "vendor format" bits as real format. Customers should
     37  * use *only* the unassigned bits below for custom pixel formats, YUV or RGB.
     38  *
     39  * If there are no bits set in this part of the field, or other bits are set
     40  * in the format outside of the "vendor format" mask, the non-extension format
     41  * is used instead. Reserve 0 for this purpose.
     42  */
     43 
     44 #define HAL_PIXEL_FORMAT_VENDOR_EXT(fmt) (0x100 | (fmt & 0xF))
     45 
     46 /*      Reserved ** DO NOT USE **    HAL_PIXEL_FORMAT_VENDOR_EXT(0) */
     47 #define HAL_PIXEL_FORMAT_BGRX_8888   HAL_PIXEL_FORMAT_VENDOR_EXT(1)
     48 #define HAL_PIXEL_FORMAT_sBGR_A_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(2)
     49 #define HAL_PIXEL_FORMAT_sBGR_X_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(3)
     50 /*      HAL_PIXEL_FORMAT_RGB_565     HAL_PIXEL_FORMAT_VENDOR_EXT(4) */
     51 /*      HAL_PIXEL_FORMAT_BGRA_8888   HAL_PIXEL_FORMAT_VENDOR_EXT(5) */
     52 #define HAL_PIXEL_FORMAT_NV12        HAL_PIXEL_FORMAT_VENDOR_EXT(6)
     53 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(7) */
     54 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(8) */
     55 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(9) */
     56 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(10) */
     57 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(11) */
     58 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(12) */
     59 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(13) */
     60 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(14) */
     61 /*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(15) */
     62 
     63 /* One of the below compression modes is OR'ed into bits [4-6] of the 8 bit
     64  * "vendor format" field. If no bits are set in this "compression mask", the
     65  * normal memory format for the pixel format is used. Otherwise the pixel
     66  * data will be compressed in memory with the Rogue framebuffer compressor.
     67  */
     68 
     69 #define HAL_FB_COMPRESSION_NONE                0
     70 #define HAL_FB_COMPRESSION_DIRECT_8x8          1
     71 #define HAL_FB_COMPRESSION_DIRECT_16x4         2
     72 #define HAL_FB_COMPRESSION_DIRECT_32x2         3
     73 #define HAL_FB_COMPRESSION_INDIRECT_8x8        4
     74 #define HAL_FB_COMPRESSION_INDIRECT_16x4       5
     75 #define HAL_FB_COMPRESSION_INDIRECT_4TILE_8x8  6
     76 #define HAL_FB_COMPRESSION_INDIRECT_4TILE_16x4 7
     77 
     78 /* The memory layout is OR'ed into bit 7 (top bit) of the 8 bit "vendor
     79  * format" field. Only STRIDED and TWIDDLED are supported; there is no space
     80  * for PAGETILED.
     81  */
     82 #define HAL_FB_MEMLAYOUT_STRIDED               0
     83 #define HAL_FB_MEMLAYOUT_TWIDDLED              1
     84 
     85 /* This can be tuned down as appropriate for the SOC.
     86  *
     87  * IMG formats are usually a single sub-alloc.
     88  * Some OEM video formats are two sub-allocs (Y, UV planes).
     89  * Future OEM video formats might be three sub-allocs (Y, U, V planes).
     90  */
     91 #define MAX_SUB_ALLOCS (3)
     92 
     93 typedef struct
     94 {
     95 	native_handle_t base;
     96 
     97 	/* These fields can be sent cross process. They are also valid
     98 	 * to duplicate within the same process.
     99 	 *
    100 	 * A table is stored within psPrivateData on gralloc_module_t (this
    101 	 * is obviously per-process) which maps stamps to a mapped
    102 	 * PVRSRV_MEMDESC in that process. Each map entry has a lock
    103 	 * count associated with it, satisfying the requirements of the
    104 	 * Android API. This also prevents us from leaking maps/allocations.
    105 	 *
    106 	 * This table has entries inserted either by alloc()
    107 	 * (alloc_device_t) or map() (gralloc_module_t). Entries are removed
    108 	 * by free() (alloc_device_t) and unmap() (gralloc_module_t).
    109 	 */
    110 
    111 #define IMG_NATIVE_HANDLE_NUMFDS (MAX_SUB_ALLOCS)
    112 	/* The `fd' field is used to "export" a meminfo to another process.
    113 	 * Therefore, it is allocated by alloc_device_t, and consumed by
    114 	 * gralloc_module_t.
    115 	 */
    116 	int fd[IMG_NATIVE_HANDLE_NUMFDS];
    117 
    118 	/* This define should represent the number of packed 'int's required to
    119 	 * represent the fields following it. If you add a data type that is
    120 	 * 64-bit, for example using 'unsigned long long', you should write that
    121 	 * as "sizeof(unsigned long long) / sizeof(int)". Please keep the order
    122 	 * of the additions the same as the defined field order.
    123 	 */
    124 #define IMG_NATIVE_HANDLE_NUMINTS \
    125 	(sizeof(unsigned long long) / sizeof(int) + \
    126 	 6 + MAX_SUB_ALLOCS + MAX_SUB_ALLOCS + \
    127 	 sizeof(unsigned long long) / sizeof(int) * MAX_SUB_ALLOCS + \
    128 	 1)
    129 	/* A KERNEL unique identifier for any exported kernel meminfo. Each
    130 	 * exported kernel meminfo will have a unique stamp, but note that in
    131 	 * userspace, several meminfos across multiple processes could have
    132 	 * the same stamp. As the native_handle can be dup(2)'d, there could be
    133 	 * multiple handles with the same stamp but different file descriptors.
    134 	 */
    135 	unsigned long long ui64Stamp;
    136 
    137 	/* This is used for buffer usage validation */
    138 	int usage;
    139 
    140 	/* In order to do efficient cache flushes we need the buffer dimensions,
    141 	 * format and bits per pixel. There are ANativeWindow queries for the
    142 	 * width, height and format, but the graphics HAL might have remapped the
    143 	 * request to different values at allocation time. These are the 'true'
    144 	 * values of the buffer allocation.
    145 	 */
    146 	int iWidth;
    147 	int iHeight;
    148 	int iFormat;
    149 	unsigned int uiBpp;
    150 
    151 	/* Planes are not the same as the `fd' suballocs. A multi-planar YUV
    152 	 * allocation has different planes (interleaved = 1, semi-planar = 2,
    153 	 * fully-planar = 3) but might be spread across 1, 2 or 3 independent
    154 	 * memory allocations (or not).
    155 	 */
    156 	int iPlanes;
    157 
    158 	/* For multi-planar allocations, there will be multiple hstrides */
    159 	int aiStride[MAX_SUB_ALLOCS];
    160 
    161 	/* For multi-planar allocations, there will be multiple vstrides */
    162 	int aiVStride[MAX_SUB_ALLOCS];
    163 
    164 	/* These byte offsets are reconciled with the number of sub-allocs used
    165 	 * for a multi-planar allocation. If there is a 1:1 mapping between the
    166 	 * number of planes and the number of sub-allocs, these will all be zero.
    167 	 *
    168 	 * Otherwise, normally the zeroth entry will be zero, and the latter
    169 	 * entries will be non-zero.
    170 	 */
    171 	unsigned long long aulPlaneOffset[MAX_SUB_ALLOCS];
    172 
    173 	/* This records the number of MAX_SUB_ALLOCS fds actually used by the
    174 	 * buffer allocation. File descriptors up to fd[iNumSubAllocs - 1] are
    175 	 * guaranteed to be valid. (This does not have any bearing on the aiStride,
    176 	 * aiVStride or aulPlaneOffset fields, as `iPlanes' of those arrays should
    177 	 * be initialized, not `iNumSubAllocs'.)
    178 	 */
    179 	int iNumSubAllocs;
    180 }
    181 __attribute__((aligned(sizeof(int)),packed)) IMG_native_handle_t;
    182 
    183 typedef struct
    184 {
    185 	int l, t, w, h;
    186 }
    187 IMG_write_lock_rect_t;
    188 
    189 #define IMG_BFF_YUV					(1 << 0)
    190 #define IMG_BFF_UVCbCrORDERING		(1 << 1)
    191 #define IMG_BFF_CPU_CLEAR			(1 << 2)
    192 #define IMG_BFF_DONT_GPU_CLEAR		(1 << 3)
    193 #define IMG_BFF_PARTIAL_ALLOC		(1 << 4)
    194 #define IMG_BFF_NEVER_COMPRESS		(1 << 5)
    195 
    196 /* Keep this in sync with SGX */
    197 typedef struct IMG_buffer_format_public_t
    198 {
    199 	/* Buffer formats are returned as a linked list */
    200 	struct IMG_buffer_format_public_t *psNext;
    201 
    202 	/* HAL_PIXEL_FORMAT_... enumerant */
    203 	int iHalPixelFormat;
    204 
    205 	/* IMG_PIXFMT_... enumerant */
    206 	int iIMGPixelFormat;
    207 
    208 	/* Friendly name for format */
    209 	const char *const szName;
    210 
    211 	/* Bits (not bytes) per pixel */
    212 	unsigned int uiBpp;
    213 
    214 	/* Supported HW usage bits. If this is GRALLOC_USAGE_HW_MASK, all usages
    215 	 * are supported. Used for HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED.
    216 	 */
    217 	int iSupportedUsage;
    218 
    219 	/* Allocation description flags */
    220 	unsigned int uiFlags;
    221 }
    222 IMG_buffer_format_public_t;
    223 
    224 /* NOTE: This interface is deprecated. Use module->perform() instead. */
    225 typedef struct IMG_gralloc_module_public_t
    226 {
    227 	gralloc_module_t base;
    228 
    229 	/* Gets the head of the linked list of all registered formats */
    230 	const IMG_buffer_format_public_t *(*GetBufferFormats)(void);
    231 
    232 	/* Custom-blit components in lieu of overlay hardware */
    233 	int (*Blit)(struct IMG_gralloc_module_public_t const *module,
    234 				 buffer_handle_t src, buffer_handle_t dest,
    235 				 int w, int h, int x, int y, int transform,
    236 				 int iInputFenceFd, int *piOutputFenceFd);
    237 
    238 	int (*Blit3)(struct IMG_gralloc_module_public_t const *module,
    239 				 unsigned long long ui64SrcStamp, int iSrcWidth,
    240 				 int iSrcHeight, int iSrcFormat, int iSrcStrideInPixels,
    241 				 int eSrcRotation, buffer_handle_t dest, int eDestRotation,
    242 				 int iInputFenceFd, int *piOutputFenceFd);
    243 
    244 	/* Walk the above list and return only the specified format */
    245 	const IMG_buffer_format_public_t *(*GetBufferFormat)(int iFormat);
    246 }
    247 IMG_gralloc_module_public_t;
    248 
    249 /* Helpers for using the non-type-safe perform() extension functions. Use
    250  * these helpers instead of calling perform() directly in your application.
    251  */
    252 
    253 #define GRALLOC_MODULE_GET_BUFFER_FORMAT_IMG     1
    254 #define GRALLOC_MODULE_GET_BUFFER_FORMATS_IMG    2
    255 #define GRALLOC_MODULE_BLIT_HANDLE_TO_HANDLE_IMG 3
    256 #define GRALLOC_MODULE_BLIT_STAMP_TO_HANDLE_IMG  4
    257 
    258 static inline int
    259 gralloc_module_get_buffer_format_img(const gralloc_module_t *module,
    260 									 int format,
    261 									 const IMG_buffer_format_public_t **v)
    262 {
    263 	return module->perform(module, GRALLOC_MODULE_GET_BUFFER_FORMAT_IMG,
    264 						   format, v);
    265 }
    266 
    267 static inline int
    268 gralloc_module_get_buffer_formats_img(const gralloc_module_t *module,
    269 									  const IMG_buffer_format_public_t **v)
    270 {
    271 	return module->perform(module, GRALLOC_MODULE_GET_BUFFER_FORMATS_IMG, v);
    272 }
    273 
    274 static inline int
    275 gralloc_module_blit_handle_to_handle_img(const gralloc_module_t *module,
    276 										 buffer_handle_t src,
    277 										 buffer_handle_t dest,
    278 										 int w, int h, int x, int y,
    279 										 int transform, int input_fence,
    280 										 int *output_fence)
    281 {
    282 	return module->perform(module, GRALLOC_MODULE_BLIT_HANDLE_TO_HANDLE_IMG,
    283 						   src, dest, w, h, x, y, transform, input_fence,
    284 						   output_fence);
    285 }
    286 
    287 static inline int
    288 gralloc_module_blit_stamp_to_handle(const gralloc_module_t *module,
    289 									unsigned long long src_stamp,
    290 									int src_width, int src_height,
    291 									int src_format, int src_stride_in_pixels,
    292 									int src_rotation, buffer_handle_t dest,
    293 									int dest_rotation, int input_fence,
    294 									int *output_fence)
    295 {
    296 	return module->perform(module, GRALLOC_MODULE_BLIT_STAMP_TO_HANDLE_IMG,
    297 						   src_stamp, src_width, src_height, src_format,
    298 						   src_stride_in_pixels, src_rotation, dest,
    299 						   dest_rotation, input_fence, output_fence);
    300 }
    301 
    302 #endif /* HAL_PUBLIC_H */
    303