1 /* 2 * Copyright (C) 2017 ARM Limited. All rights reserved. 3 * 4 * Copyright (C) 2008 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef GRALLOC3_PRIV_H_ 20 #define GRALLOC3_PRIV_H_ 21 22 #include <linux/fb.h> 23 #include <hardware/gralloc1.h> 24 #include <inttypes.h> 25 #include <log/log.h> 26 27 #define AFBC_INFO_SIZE (sizeof(int)) 28 #define AFBC_ENABLE (0xafbc) 29 30 /* Exynos specific usages */ 31 #define GRALLOC1_PRODUCER_USAGE_PRIVATE_NONSECURE GRALLOC1_PRODUCER_USAGE_PRIVATE_8 32 #define GRALLOC1_PRODUCER_USAGE_NOZEROED GRALLOC1_PRODUCER_USAGE_PRIVATE_9 33 34 #define GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA GRALLOC1_CONSUMER_USAGE_PRIVATE_7 35 36 #define GRALLOC_USAGE_GOOGLE_IP_BO GRALLOC1_CONSUMER_USAGE_PRIVATE_16 37 #define GRALLOC_USAGE_GOOGLE_IP_MFC GRALLOC1_CONSUMER_USAGE_PRIVATE_17 38 39 #define GS101_GRALLOC_USAGE_TPU_INPUT GRALLOC1_CONSUMER_USAGE_PRIVATE_5 40 #define GS101_GRALLOC_USAGE_TPU_OUTPUT GRALLOC1_PRODUCER_USAGE_PRIVATE_3 41 #define GS101_GRALLOC_USAGE_CAMERA_STATS GRALLOC1_PRODUCER_USAGE_PRIVATE_2 42 43 /* for legacy */ 44 #define GRALLOC_USAGE_PRIVATE_NONSECURE GRALLOC1_PRODUCER_USAGE_PRIVATE_NONSECURE 45 46 typedef int ion_user_handle_t; 47 48 typedef enum 49 { 50 /* 51 * Allocation will be used as a front-buffer, which 52 * supports concurrent producer-consumer access. 53 * 54 * NOTE: Must not be used with MALI_GRALLOC_USAGE_FORCE_BACKBUFFER 55 */ 56 MALI_GRALLOC_USAGE_FRONTBUFFER = GRALLOC1_PRODUCER_USAGE_PRIVATE_12, 57 58 /* 59 * Allocation will be used as a back-buffer. 60 * Use when switching from front-buffer as a workaround for Android 61 * buffer queue, which does not re-allocate for a sub-set of 62 * existing usage. 63 * 64 * NOTE: Must not be used with MALI_GRALLOC_USAGE_FRONTBUFFER. 65 */ 66 MALI_GRALLOC_USAGE_FORCE_BACKBUFFER = GRALLOC1_PRODUCER_USAGE_PRIVATE_13, 67 68 /* 69 * Buffer will not be allocated with AFBC. 70 * 71 * NOTE: Not compatible with MALI_GRALLOC_USAGE_FORCE_BACKBUFFER so cannot be 72 * used when switching from front-buffer to back-buffer. 73 */ 74 MALI_GRALLOC_USAGE_NO_AFBC = GRALLOC1_PRODUCER_USAGE_PRIVATE_1, 75 76 /* Custom alignment for AFBC headers. 77 * 78 * NOTE: due to usage flag overlap, AFBC_PADDING cannot be used with FORCE_BACKBUFFER. 79 */ 80 MALI_GRALLOC_USAGE_AFBC_PADDING = GRALLOC1_PRODUCER_USAGE_PRIVATE_14, 81 /* Private format usage. 82 * 'format' argument to allocation function will be interpreted in a 83 * private manner and must be constructed via GRALLOC_PRIVATE_FORMAT_WRAPPER_* 84 * macros which pack base format and AFBC format modifiers into 32-bit value. 85 */ 86 MALI_GRALLOC_USAGE_PRIVATE_FORMAT = GRALLOC1_PRODUCER_USAGE_PRIVATE_15, 87 88 /* YUV only. */ 89 MALI_GRALLOC_USAGE_YUV_COLOR_SPACE_DEFAULT = 0, 90 MALI_GRALLOC_USAGE_YUV_COLOR_SPACE_BT601 = GRALLOC1_PRODUCER_USAGE_PRIVATE_18, 91 MALI_GRALLOC_USAGE_YUV_COLOR_SPACE_BT709 = GRALLOC1_PRODUCER_USAGE_PRIVATE_19, 92 MALI_GRALLOC_USAGE_YUV_COLOR_SPACE_BT2020 = (GRALLOC1_PRODUCER_USAGE_PRIVATE_18 | GRALLOC1_PRODUCER_USAGE_PRIVATE_19), 93 MALI_GRALLOC_USAGE_YUV_COLOR_SPACE_MASK = MALI_GRALLOC_USAGE_YUV_COLOR_SPACE_BT2020, 94 95 MALI_GRALLOC_USAGE_RANGE_DEFAULT = 0, 96 MALI_GRALLOC_USAGE_RANGE_NARROW = GRALLOC1_PRODUCER_USAGE_PRIVATE_16, 97 MALI_GRALLOC_USAGE_RANGE_WIDE = GRALLOC1_PRODUCER_USAGE_PRIVATE_17, 98 MALI_GRALLOC_USAGE_RANGE_MASK = (GRALLOC1_PRODUCER_USAGE_PRIVATE_16 | GRALLOC1_PRODUCER_USAGE_PRIVATE_17), 99 } mali_gralloc_usage_type; 100 101 typedef struct 102 { 103 struct hw_module_t common; 104 } gralloc1_module_t; 105 106 typedef enum 107 { 108 MALI_DPY_TYPE_UNKNOWN = 0, 109 MALI_DPY_TYPE_CLCD, 110 MALI_DPY_TYPE_HDLCD 111 } mali_dpy_type; 112 113 typedef enum 114 { 115 MALI_YUV_NO_INFO, 116 MALI_YUV_BT601_NARROW, 117 MALI_YUV_BT601_WIDE, 118 MALI_YUV_BT709_NARROW, 119 MALI_YUV_BT709_WIDE, 120 MALI_YUV_BT2020_NARROW, 121 MALI_YUV_BT2020_WIDE 122 } mali_gralloc_yuv_info; 123 124 typedef struct private_module 125 { 126 gralloc1_module_t base; 127 128 struct private_handle_t *framebuffer; 129 uint32_t flags; 130 uint32_t numBuffers; 131 uint32_t bufferMask; 132 pthread_mutex_t lock; 133 buffer_handle_t currentBuffer; 134 mali_dpy_type dpy_type; 135 136 struct fb_var_screeninfo info; 137 struct fb_fix_screeninfo finfo; 138 float xdpi; 139 float ydpi; 140 float fps; 141 int swapInterval; 142 uint64_t fbdev_format; 143 int ionfd; 144 } private_module_t; 145 146 /* 147 * Maximum number of pixel format planes. 148 * Plane [0]: Single plane formats (inc. RGB, YUV) and Y 149 * Plane [1]: U/V, UV 150 * Plane [2]: V/U 151 */ 152 #define MAX_PLANES 3 153 154 typedef struct plane_info { 155 156 /* 157 * Offset to plane (in bytes), 158 * from the start of the allocation. 159 */ 160 uint32_t offset; 161 162 /* 163 * Byte Stride: number of bytes between two vertically adjacent 164 * pixels in given plane. This can be mathematically described by: 165 * 166 * byte_stride = ALIGN((alloc_width * bpp)/8, alignment) 167 * 168 * where, 169 * 170 * alloc_width: width of plane in pixels (c.f. pixel_stride) 171 * bpp: average bits per pixel 172 * alignment (in bytes): dependent upon pixel format and usage 173 * 174 * For uncompressed allocations, byte_stride might contain additional 175 * padding beyond the alloc_width. For AFBC, alignment is zero. 176 */ 177 uint32_t byte_stride; 178 179 /* 180 * Dimensions of plane (in pixels). 181 * 182 * For single plane formats, pixels equates to luma samples. 183 * For multi-plane formats, pixels equates to the number of sample sites 184 * for the corresponding plane, even if subsampled. 185 * 186 * AFBC compressed formats: requested width/height are rounded-up 187 * to a whole AFBC superblock/tile (next superblock at minimum). 188 * Uncompressed formats: dimensions typically match width and height 189 * but might require pixel stride alignment. 190 * 191 * See 'byte_stride' for relationship between byte_stride and alloc_width. 192 * 193 * Any crop rectangle defined by GRALLOC_ARM_BUFFER_ATTR_CROP_RECT must 194 * be wholly within the allocation dimensions. The crop region top-left 195 * will be relative to the start of allocation. 196 */ 197 uint32_t alloc_width; 198 uint32_t alloc_height; 199 } plane_info_t; 200 201 struct private_handle_t : public native_handle 202 { 203 enum 204 { 205 PRIV_FLAGS_FRAMEBUFFER = 1U << 0, 206 PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 1U << 1, 207 PRIV_FLAGS_USES_ION = 1U << 2, 208 PRIV_FLAGS_USES_ION_DMA_HEAP = 1U << 3, 209 PRIV_FLAGS_USES_2PRIVATE_DATA = 1U << 4, 210 PRIV_FLAGS_USES_3PRIVATE_DATA = 1U << 5, 211 }; 212 213 /* 214 * Shared file descriptor for dma_buf sharing. This must be the first element in the 215 * structure so that binder knows where it is and can properly share it between 216 * processes. 217 * DO NOT MOVE THIS ELEMENT! 218 */ 219 int fd; 220 int fd1; 221 int fd2; 222 int fd3; 223 int fd4; 224 225 // ints 226 int magic; 227 int flags; 228 229 /* 230 * Input properties. 231 * 232 * req_format: Pixel format, base + private modifiers. 233 * width/height: Buffer dimensions. 234 * producer/consumer_usage: Buffer usage (indicates IP) 235 */ 236 int width; 237 int height; 238 /* LSI integration: Needed by Camera */ 239 int frameworkFormat; 240 241 uint64_t producer_usage; 242 uint64_t consumer_usage; 243 244 union 245 { 246 int format; 247 uint64_t internal_format; 248 }; 249 250 /* 251 * Allocation properties. 252 * 253 * alloc_format: Pixel format (base + modifiers). NOTE: base might differ from requested 254 * format (req_format) where fallback to single-plane format was required. 255 * plane_info: Per plane allocation information. 256 * size: Total bytes allocated for buffer (inc. all planes, layers. etc.). 257 * layer_count: Number of layers allocated to buffer. 258 * All layers are the same size (in bytes). 259 * Multi-layers supported in v1.0, where GRALLOC1_CAPABILITY_LAYERED_BUFFERS is enabled. 260 * Layer size: 'size' / 'layer_count'. 261 * Layer (n) offset: n * ('size' / 'layer_count'), n=0 for the first layer. 262 * 263 */ 264 uint64_t alloc_format; 265 union 266 { 267 plane_info_t plane_info[MAX_PLANES]; 268 struct 269 { 270 int plane_offset; 271 int byte_stride; 272 int alloc_width; 273 int vstride; 274 }; 275 }; 276 int size; 277 int size1; 278 int size2; 279 uint32_t stride; 280 uint32_t layer_count; 281 282 union 283 { 284 void *base; 285 uint64_t bases[3]; 286 }; 287 288 uint64_t backing_store_id; 289 int backing_store_size; 290 int cpu_read; /**< Buffer is locked for CPU read when non-zero. */ 291 int cpu_write; /**< Buffer is locked for CPU write when non-zero. */ 292 int allocating_pid; 293 int remote_pid; 294 int ref_count; 295 // locally mapped shared attribute area 296 union 297 { 298 void *attr_base; 299 uint64_t padding3; 300 }; 301 302 mali_gralloc_yuv_info yuv_info; 303 304 // Following members is for framebuffer only 305 int fb_fd; 306 union 307 { 308 off_t offset; 309 uint64_t padding4; 310 }; 311 312 /* 313 * min_pgsz denotes minimum phys_page size used by this buffer. 314 * if buffer memory is physical contiguous set min_pgsz to buff->size 315 * if not sure buff's real phys_page size, you can use SZ_4K for safe. 316 */ 317 int min_pgsz; 318 319 int is_compressible; 320 321 ion_user_handle_t ion_handles[3]; 322 323 int PRIVATE_1 = 0; 324 int PRIVATE_2 = 0; 325 int plane_count = 0; 326 validateprivate_handle_t327 static int validate(const native_handle *h) 328 { 329 #define GRALLOC_ARM_NUM_FDS 2 330 #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - GRALLOC_ARM_NUM_FDS) 331 332 static const int sNumFds = GRALLOC_ARM_NUM_FDS; 333 static const int sMagic = 0x3141592; 334 const private_handle_t *hnd = (const private_handle_t *)h; 335 336 if (!h || h->version != sizeof(native_handle) || 337 hnd->numInts + hnd->numFds != NUM_INTS_IN_PRIVATE_HANDLE + sNumFds || 338 hnd->magic != sMagic) 339 { 340 return -EINVAL; 341 } 342 343 return 0; 344 } 345 dynamicCastprivate_handle_t346 static private_handle_t *dynamicCast(const native_handle *in) 347 { 348 if (validate(in) == 0) 349 { 350 return (private_handle_t *)in; 351 } 352 353 return NULL; 354 } 355 get_num_ion_fdsprivate_handle_t356 int get_num_ion_fds() const 357 { 358 return numFds - 1; 359 } 360 dumpprivate_handle_t361 void dump(const char *str) const 362 { 363 ALOGD("[%s] " 364 "fd(%d %d %d %d) " 365 "flags(%d) " 366 "wh(%d %d) " 367 "req_format(0x%x) " 368 "usage_pc(0x%" PRIx64 " 0x%" PRIx64 ") " 369 "format(0x%x) " 370 "interal_format(0x%" PRIx64 ") " 371 "stride(%d) byte_stride(%d) internal_wh(%d %d) " 372 "alloc_format(0x%" PRIx64 ") " 373 "size(%d %d %d) " 374 "layer_count(%d) plane_count(%d)" 375 "bases(0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 ") " 376 "\n", 377 str, 378 fd, fd1, fd2, fd3, 379 flags, 380 width, height, 381 frameworkFormat, 382 producer_usage, consumer_usage, 383 format, internal_format, 384 stride, plane_info[0].byte_stride, plane_info[0].alloc_width, plane_info[0].alloc_height, 385 alloc_format, 386 size, size1, size2, 387 layer_count, plane_count, 388 bases[0], bases[1], bases[2] 389 ); 390 } 391 }; 392 393 #endif /* GRALLOC3_PRIV_H_ */ 394