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 IMG_GRALLOC_COMMON_PUBLIC_H 25 #define IMG_GRALLOC_COMMON_PUBLIC_H 26 27 #include <cutils/native_handle.h> 28 #include <system/graphics.h> 29 #include <linux/ion.h> 30 31 #define ALIGN(x,a) ((((x) + (a) - 1L) / (a)) * (a)) 32 #define HW_ALIGN 32 33 34 /* Use bits [0-3] of "vendor format" bits as real format. Customers should 35 * use *only* the unassigned bits below for custom pixel formats, YUV or RGB. 36 * 37 * If there are no bits set in this part of the field, or other bits are set 38 * in the format outside of the "vendor format" mask, the non-extension format 39 * is used instead. Reserve 0 for this purpose. 40 */ 41 42 #define HAL_PIXEL_FORMAT_VENDOR_EXT(fmt) (0x100 | (fmt & 0xF)) 43 44 /* Reserved ** DO NOT USE ** HAL_PIXEL_FORMAT_VENDOR_EXT(0) */ 45 #define HAL_PIXEL_FORMAT_BGRX_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(1) 46 #define HAL_PIXEL_FORMAT_sBGR_A_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(2) 47 #define HAL_PIXEL_FORMAT_sBGR_X_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(3) 48 /* HAL_PIXEL_FORMAT_RGB_565 HAL_PIXEL_FORMAT_VENDOR_EXT(4) */ 49 /* HAL_PIXEL_FORMAT_BGRA_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(5) */ 50 #define HAL_PIXEL_FORMAT_NV12 HAL_PIXEL_FORMAT_VENDOR_EXT(6) 51 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(7) */ 52 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(8) */ 53 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(9) */ 54 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(10) */ 55 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(11) */ 56 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(12) */ 57 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(13) */ 58 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(14) */ 59 /* Free for customer use HAL_PIXEL_FORMAT_VENDOR_EXT(15) */ 60 61 /* One of the below compression modes is OR'ed into bits [4-6] of the 8 bit 62 * "vendor format" field. If no bits are set in this "compression mask", the 63 * normal memory format for the pixel format is used. Otherwise the pixel 64 * data will be compressed in memory with the Rogue framebuffer compressor. 65 */ 66 67 #define HAL_FB_COMPRESSION_NONE 0 68 #define HAL_FB_COMPRESSION_DIRECT_8x8 1 69 #define HAL_FB_COMPRESSION_DIRECT_16x4 2 70 #define HAL_FB_COMPRESSION_DIRECT_32x2 3 71 #define HAL_FB_COMPRESSION_INDIRECT_8x8 4 72 #define HAL_FB_COMPRESSION_INDIRECT_16x4 5 73 #define HAL_FB_COMPRESSION_INDIRECT_4TILE_8x8 6 74 #define HAL_FB_COMPRESSION_INDIRECT_4TILE_16x4 7 75 76 /* The memory layout is OR'ed into bit 7 (top bit) of the 8 bit "vendor 77 * format" field. Only STRIDED and TWIDDLED are supported; there is no space 78 * for PAGETILED. 79 */ 80 #define HAL_FB_MEMLAYOUT_STRIDED 0 81 #define HAL_FB_MEMLAYOUT_TWIDDLED 1 82 83 /* This can be tuned down as appropriate for the SOC. 84 * 85 * IMG formats are usually a single sub-alloc. 86 * Some OEM video formats are two sub-allocs (Y, UV planes). 87 * Future OEM video formats might be three sub-allocs (Y, U, V planes). 88 */ 89 #define MAX_SUB_ALLOCS (3) 90 91 typedef struct 92 { 93 native_handle_t base; 94 95 /* These fields can be sent cross process. They are also valid 96 * to duplicate within the same process. 97 * 98 * A table is stored within the gralloc implementation's private data 99 * structure (which is per-process) which maps stamps to a mapped 100 * PVRSRV_MEMDESC in that process. Each map entry has a lock count 101 * associated with it, satisfying the requirements of the gralloc API. 102 * This also prevents us from leaking maps/allocations. 103 */ 104 105 #define IMG_NATIVE_HANDLE_NUMFDS (MAX_SUB_ALLOCS) 106 /* The `fd' field is used to "export" a meminfo to another process. */ 107 int fd[IMG_NATIVE_HANDLE_NUMFDS]; 108 109 /* This define should represent the number of packed 'int's required to 110 * represent the fields following it. If you add a data type that is 111 * 64-bit, for example using 'unsigned long long', you should write that 112 * as "sizeof(unsigned long long) / sizeof(int)". Please keep the order 113 * of the additions the same as the defined field order. 114 */ 115 #define IMG_NATIVE_HANDLE_NUMINTS \ 116 (sizeof(unsigned long long) / sizeof(int) + \ 117 6 + MAX_SUB_ALLOCS + MAX_SUB_ALLOCS + \ 118 sizeof(unsigned long long) / sizeof(int) * MAX_SUB_ALLOCS + \ 119 1) 120 /* A KERNEL unique identifier for any exported kernel memdesc. Each 121 * exported kernel memdesc will have a unique stamp, but note that in 122 * userspace, several memdescs across multiple processes could have 123 * the same stamp. As the native_handle can be dup(2)'d, there could be 124 * multiple handles with the same stamp but different file descriptors. 125 */ 126 unsigned long long ui64Stamp; 127 128 /* This is used for buffer usage validation */ 129 int usage; 130 131 /* In order to do efficient cache flushes we need the buffer dimensions, 132 * format and bits per pixel. There are ANativeWindow queries for the 133 * width, height and format, but the graphics HAL might have remapped the 134 * request to different values at allocation time. These are the 'true' 135 * values of the buffer allocation. 136 */ 137 int iWidth; 138 int iHeight; 139 int iFormat; 140 unsigned int uiBpp; 141 142 /* Planes are not the same as the `fd' suballocs. A multi-planar YUV 143 * allocation has different planes (interleaved = 1, semi-planar = 2, 144 * fully-planar = 3) but might be spread across 1, 2 or 3 independent 145 * memory allocations (or not). 146 */ 147 int iPlanes; 148 149 /* For multi-planar allocations, there will be multiple hstrides */ 150 int aiStride[MAX_SUB_ALLOCS]; 151 152 /* For multi-planar allocations, there will be multiple vstrides */ 153 int aiVStride[MAX_SUB_ALLOCS]; 154 155 /* These byte offsets are reconciled with the number of sub-allocs used 156 * for a multi-planar allocation. If there is a 1:1 mapping between the 157 * number of planes and the number of sub-allocs, these will all be zero. 158 * 159 * Otherwise, normally the zeroth entry will be zero, and the latter 160 * entries will be non-zero. 161 */ 162 unsigned long long aulPlaneOffset[MAX_SUB_ALLOCS]; 163 164 /* This records the number of MAX_SUB_ALLOCS fds actually used by the 165 * buffer allocation. File descriptors up to fd[iNumSubAllocs - 1] are 166 * guaranteed to be valid. (This does not have any bearing on the aiStride, 167 * aiVStride or aulPlaneOffset fields, as `iPlanes' of those arrays should 168 * be initialized, not `iNumSubAllocs'.) 169 */ 170 int iNumSubAllocs; 171 } 172 __attribute__((aligned(sizeof(int)),packed)) IMG_native_handle_t; 173 174 /* Channel encoding of buffer data. 175 * 176 * If the buffer has only one plane, the ENCODING bits should be interpreted 177 * as a definition of the interleaving pattern. Only two of the possible four 178 * permutations are defined; this is because the YVYU and VYUY patterns are 179 * not seen in the wild. 180 * 181 * If the buffer has more than one plane, the ENCODING bits should be 182 * interpreted as a definition of the plane order in memory. Assuming a YUV 183 * format, Y is always first, but U and V may be defined in 'V then U' or 184 * 'U then V' orders. 185 * 186 * Some bits are not used, to maximize compatibility with older DDKs which 187 * used them in semantically different ways. 188 */ 189 #define IMG_BFF_ENCODING_MASK (3 << 0) 190 /* For uiPlanes == 1 **********************************/ 191 /* Reserved for VYUY (check IsYUV if used) (0 << 0) */ 192 #define IMG_BFF_ENCODING_INTERLEAVED_YUYV (1 << 0) 193 /* Reserved for YVYU (2 << 0) */ 194 #define IMG_BFF_ENCODING_INTERLEAVED_UYVY (3 << 0) 195 /* For uiPlanes > 1 ***********************************/ 196 /* Unused (check IsYUV if used) (0 << 0) */ 197 #define IMG_BFF_ENCODING_VUCrCb (1 << 0) 198 /* Unused (2 << 0) */ 199 #define IMG_BFF_ENCODING_UVCbCr (3 << 0) 200 201 /* Whether the buffer should be cleared to zero from userspace, or via the 202 * PowerVR services at import time. This is deprecated functionality as most 203 * platforms use dma-buf or ion now, and for security reasons these allocators 204 * should never return uncleared memory. 205 */ 206 #define IMG_BFF_CPU_CLEAR (1 << 2) 207 208 /* Deprecated, do not use */ 209 #define IMG_BFF_DONT_GPU_CLEAR (1 << 3) 210 211 /* Deprecated, do not use */ 212 #define IMG_BFF_PARTIAL_ALLOC (1 << 4) 213 214 /* Guarantee that GPU framebuffer compression is never used for buffers in 215 * this format, even if the format is supported by the compressor. This might 216 * be useful if the buffer is being fed to hardware blocks that cannot handle 217 * the framebuffer compression encoding, and the existing HAL overrides are 218 * not sufficiently expressive. 219 */ 220 #define IMG_BFF_NEVER_COMPRESS (1 << 5) 221 222 /* Indicates that the buffer should be mapped into the GPU 'tiling range' 223 * heaps, rather than the 'linear' general heap. This implies that the raw 224 * buffer data is tiled in physical memory. (The GPU BIF will de-tile it, so 225 * this is distinct from 'tiled texture' support.) The graphics HAL will 226 * select the correct 'tiling range' based on the buffer dimensions. 227 */ 228 #define IMG_BFF_BIFTILED (1 << 6) 229 230 /* YUV subsampling encoding of buffer data. 231 * Many YUV formats have less chroma information than luma information. If 232 * this is not the case, use SUBSAMPLING_4_4_4. If each of the U and V channel 233 * data are 1/4 the size of the Y channel data, use SUBSAMPLING_4_2_0. 234 * Otherwise, use SUBSAMPLING_4_2_2. 235 */ 236 #define IMG_BFF_YUV_SUBSAMPLING_MASK (3 << 7) 237 #define IMG_BFF_YUV_SUBSAMPLING_4_2_0 (0 << 7) 238 /* Unused: 4:1:1, 4:2:1, 4:1:0, 3:1:1? (1 << 7) */ 239 #define IMG_BFF_YUV_SUBSAMPLING_4_2_2 (2 << 7) 240 #define IMG_BFF_YUV_SUBSAMPLING_4_4_4 (3 << 7) 241 242 /* Backwards compatibility */ 243 #define IMG_BFF_YUV IMG_BFF_ENCODING_VUCrCb 244 #define IMG_BFF_UVCbCrORDERING IMG_BFF_ENCODING_UVCbCr 245 246 /* Keep this in sync with SGX */ 247 typedef struct IMG_buffer_format_public_t 248 { 249 /* Buffer formats are returned as a linked list */ 250 struct IMG_buffer_format_public_t *psNext; 251 252 /* HAL_PIXEL_FORMAT_... enumerant */ 253 int iHalPixelFormat; 254 255 /* IMG_PIXFMT_... enumerant */ 256 int iIMGPixelFormat; 257 258 /* Friendly name for format */ 259 const char *const szName; 260 261 /* Bits (not bytes) per pixel */ 262 unsigned int uiBpp; 263 264 /* Supported HW usage bits. If this is GRALLOC_USAGE_HW_MASK, all usages 265 * are supported. Used for HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED. 266 */ 267 int iSupportedUsage; 268 269 /* Allocation description flags */ 270 unsigned int uiFlags; 271 } 272 IMG_buffer_format_public_t; 273 274 typedef struct 275 { 276 enum 277 { 278 IMG_BUFFER_HANDLE_TYPE_ION = 0, 279 IMG_BUFFER_HANDLE_TYPE_DMABUF = 1, 280 } 281 eType; 282 283 union 284 { 285 ion_user_handle_t aiIonUserHandle[MAX_SUB_ALLOCS]; 286 int aiDmaBufShareFd[MAX_SUB_ALLOCS]; 287 }; 288 } 289 IMG_buffer_handle_t; 290 291 /* Public extensions, common to v0 and v1 HALs */ 292 293 #define GRALLOC_GET_BUFFER_FORMAT_IMG 1 294 #define GRALLOC_GET_BUFFER_FORMATS_IMG 2 295 #define GRALLOC_BLIT_HANDLE_TO_HANDLE_IMG 3 296 #define GRALLOC_BLIT_STAMP_TO_HANDLE_IMG 4 297 #define GRALLOC_SET_DATA_SPACE_IMG 5 298 #define GRALLOC_GET_ION_CLIENT_IMG 6 299 #define GRALLOC_GET_BUFFER_HANDLE_IMG 7 300 301 #if !defined(PVR_ANDROID_HAS_SET_BUFFERS_DATASPACE) 302 303 enum 304 { 305 HAL_DATASPACE_SRGB_LINEAR = 0x200, 306 HAL_DATASPACE_SRGB = 0x201, 307 HAL_DATASPACE_BT601_625 = 0x102, 308 HAL_DATASPACE_BT601_525 = 0x103, 309 HAL_DATASPACE_BT709 = 0x104, 310 }; 311 312 #endif /* !defined(PVR_ANDROID_HAS_SET_BUFFERS_DATASPACE) */ 313 314 #if !defined(PVR_ANDROID_HAS_SET_BUFFERS_DATASPACE_2) 315 316 enum 317 { 318 HAL_DATASPACE_STANDARD_SHIFT = 16, 319 HAL_DATASPACE_TRANSFER_SHIFT = 22, 320 HAL_DATASPACE_RANGE_SHIFT = 27, 321 322 HAL_DATASPACE_STANDARD_BT2020 = 6 << HAL_DATASPACE_STANDARD_SHIFT, 323 324 HAL_DATASPACE_TRANSFER_SMPTE_170M = 3 << HAL_DATASPACE_TRANSFER_SHIFT, 325 326 HAL_DATASPACE_RANGE_MASK = 7 << HAL_DATASPACE_RANGE_SHIFT, 327 HAL_DATASPACE_RANGE_FULL = 1 << HAL_DATASPACE_RANGE_SHIFT, 328 HAL_DATASPACE_RANGE_LIMITED = 2 << HAL_DATASPACE_RANGE_SHIFT, 329 }; 330 331 #endif /* !defined(PVR_ANDROID_HAS_SET_BUFFERS_DATASPACE_2) */ 332 333 /* We want to add BT.2020 and 'full range' versions of the existing dataspace 334 * enums. These are extensions, so define a new android_dataspace_ext_t. 335 * If you only have an android_dataspace_t, you can simply cast it. 336 */ 337 typedef enum 338 { 339 /* Identical to upstream enum android_dataspace */ 340 HAL_DATASPACE_EXT_UNKNOWN = HAL_DATASPACE_UNKNOWN, 341 HAL_DATASPACE_EXT_SRGB_LINEAR = HAL_DATASPACE_SRGB_LINEAR, 342 HAL_DATASPACE_EXT_SRGB = HAL_DATASPACE_SRGB, 343 HAL_DATASPACE_EXT_BT601_625 = HAL_DATASPACE_BT601_625, 344 HAL_DATASPACE_EXT_BT601_525 = HAL_DATASPACE_BT601_525, 345 HAL_DATASPACE_EXT_BT709 = HAL_DATASPACE_BT709, 346 347 /* IMG extension for BT.2020 support */ 348 HAL_DATASPACE_EXT_BT2020 = HAL_DATASPACE_STANDARD_BT2020 | 349 HAL_DATASPACE_TRANSFER_SMPTE_170M | 350 HAL_DATASPACE_RANGE_LIMITED, 351 352 /* IMG extensions for 'full range' versions of previous enums */ 353 HAL_DATASPACE_EXT_BT601_625_FULL = ( HAL_DATASPACE_BT601_625 & 354 ~HAL_DATASPACE_RANGE_MASK) | 355 HAL_DATASPACE_RANGE_FULL, 356 HAL_DATASPACE_EXT_BT601_525_FULL = ( HAL_DATASPACE_BT601_525 & 357 ~HAL_DATASPACE_RANGE_MASK) | 358 HAL_DATASPACE_RANGE_FULL, 359 HAL_DATASPACE_EXT_BT709_FULL = ( HAL_DATASPACE_BT709 & 360 ~HAL_DATASPACE_RANGE_MASK) | 361 HAL_DATASPACE_RANGE_FULL, 362 HAL_DATASPACE_EXT_BT2020_FULL = ( HAL_DATASPACE_EXT_BT2020 & 363 ~HAL_DATASPACE_RANGE_MASK) | 364 HAL_DATASPACE_RANGE_FULL, 365 } 366 android_dataspace_ext_t; 367 368 #endif /* IMG_GRALLOC_COMMON_PUBLIC_H */ 369