1 /* 2 * Copyright (C) 2017-2019 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 #ifndef MALI_GRALLOC_BUFFER_H_ 19 #define MALI_GRALLOC_BUFFER_H_ 20 21 #include <inttypes.h> 22 #include <stdint.h> 23 #include <unistd.h> 24 #include <errno.h> 25 #include <string.h> 26 #include <sys/mman.h> 27 #include <cutils/native_handle.h> 28 #include <log/log.h> 29 #include <string.h> 30 //#include <linux/ion.h> 31 32 #include "gralloc_helper.h" 33 #include "mali_gralloc_private_interface_types.h" 34 35 typedef int ion_user_handle_t; 36 37 /* NOTE: 38 * If your framebuffer device driver is integrated with dma_buf, you will have to 39 * change this IOCTL definition to reflect your integration with the framebuffer 40 * device. 41 * Expected return value is a structure filled with a file descriptor 42 * backing your framebuffer device memory. 43 */ 44 struct fb_dmabuf_export 45 { 46 __u32 fd; 47 __u32 flags; 48 }; 49 #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export) 50 51 /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0 52 * 8 is big enough for "gpu0" & "fb0" currently 53 */ 54 #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8 55 #define NUM_FB_BUFFERS 2 56 57 /* Define number of shared file descriptors */ 58 #define GRALLOC_ARM_NUM_FDS 2 59 60 #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - GRALLOC_ARM_NUM_FDS) 61 62 #define SZ_4K 0x00001000 63 #define SZ_2M 0x00200000 64 65 /* 66 * Maximum number of pixel format planes. 67 * Plane [0]: Single plane formats (inc. RGB, YUV) and Y 68 * Plane [1]: U/V, UV 69 * Plane [2]: V/U 70 */ 71 #define MAX_PLANES 3 72 73 typedef struct plane_info { 74 75 /* 76 * Offset to plane (in bytes), 77 * from the start of the allocation. 78 */ 79 uint32_t offset; 80 81 /* 82 * Byte Stride: number of bytes between two vertically adjacent 83 * pixels in given plane. This can be mathematically described by: 84 * 85 * byte_stride = ALIGN((alloc_width * bpp)/8, alignment) 86 * 87 * where, 88 * 89 * alloc_width: width of plane in pixels (c.f. pixel_stride) 90 * bpp: average bits per pixel 91 * alignment (in bytes): dependent upon pixel format and usage 92 * 93 * For uncompressed allocations, byte_stride might contain additional 94 * padding beyond the alloc_width. For AFBC, alignment is zero. 95 */ 96 uint32_t byte_stride; 97 98 /* 99 * Dimensions of plane (in pixels). 100 * 101 * For single plane formats, pixels equates to luma samples. 102 * For multi-plane formats, pixels equates to the number of sample sites 103 * for the corresponding plane, even if subsampled. 104 * 105 * AFBC compressed formats: requested width/height are rounded-up 106 * to a whole AFBC superblock/tile (next superblock at minimum). 107 * Uncompressed formats: dimensions typically match width and height 108 * but might require pixel stride alignment. 109 * 110 * See 'byte_stride' for relationship between byte_stride and alloc_width. 111 * 112 * Any crop rectangle defined by GRALLOC_ARM_BUFFER_ATTR_CROP_RECT must 113 * be wholly within the allocation dimensions. The crop region top-left 114 * will be relative to the start of allocation. 115 */ 116 uint32_t alloc_width; 117 uint32_t alloc_height; 118 } plane_info_t; 119 120 struct private_handle_t; 121 122 #ifndef __cplusplus 123 /* C99 with pedantic don't allow anonymous unions which is used in below struct 124 * Disable pedantic for C for this struct only. 125 */ 126 #pragma GCC diagnostic push 127 #pragma GCC diagnostic ignored "-Wpedantic" 128 #endif 129 130 #ifdef __cplusplus 131 struct private_handle_t : public native_handle 132 { 133 #else 134 struct private_handle_t 135 { 136 struct native_handle nativeHandle; 137 #endif 138 139 #ifdef __cplusplus 140 /* Never intended to be used from C code */ 141 enum 142 { 143 PRIV_FLAGS_FRAMEBUFFER = 1U << 0, 144 PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 1U << 1, 145 PRIV_FLAGS_USES_ION = 1U << 2, 146 PRIV_FLAGS_USES_ION_DMA_HEAP = 1U << 3, 147 PRIV_FLAGS_USES_2PRIVATE_DATA = 1U << 4, 148 PRIV_FLAGS_USES_3PRIVATE_DATA = 1U << 5, 149 }; 150 151 enum 152 { 153 LOCK_STATE_WRITE = 1 << 31, 154 LOCK_STATE_MAPPED = 1 << 30, 155 LOCK_STATE_READ_MASK = 0x3FFFFFFF 156 }; 157 #endif 158 159 /* 160 * Shared file descriptor for dma_buf sharing. This must be the first element in the 161 * structure so that binder knows where it is and can properly share it between 162 * processes. 163 * DO NOT MOVE THIS ELEMENT! 164 */ 165 union 166 { 167 int share_fd; 168 int fds[5]; 169 }; 170 171 // ints 172 int magic; 173 int flags; 174 175 /* 176 * Input properties. 177 * 178 * req_format: Pixel format, base + private modifiers. 179 * width/height: Buffer dimensions. 180 * producer/consumer_usage: Buffer usage (indicates IP) 181 */ 182 int width; 183 int height; 184 int req_format; 185 uint64_t producer_usage; 186 uint64_t consumer_usage; 187 188 /* 189 * DEPRECATED members. 190 * Equivalent information can be obtained from other fields: 191 * 192 * - 'internal_format' --> alloc_format 193 * - 'stride' (pixel stride) ~= plane_info[0].alloc_width 194 * - 'byte_stride' ~= plane_info[0].byte_stride 195 * - 'internalWidth' ~= plane_info[0].alloc_width 196 * - 'internalHeight' ~= plane_info[0].alloc_height 197 * 198 * '~=' (approximately equal) is used because the fields were either previously 199 * incorrectly populated by gralloc or the meaning has slightly changed. 200 * 201 * NOTE: 'stride' values sometimes vary significantly from plane_info[0].alloc_width. 202 */ 203 union 204 { 205 uint32_t format; 206 uint64_t internal_format = 0; 207 }; 208 209 /* 210 * Allocation properties. 211 * 212 * alloc_format: Pixel format (base + modifiers). NOTE: base might differ from requested 213 * format (req_format) where fallback to single-plane format was required. 214 * plane_info: Per plane allocation information. 215 * size: Total bytes allocated for buffer (inc. all planes, layers. etc.). 216 * layer_count: Number of layers allocated to buffer. 217 * All layers are the same size (in bytes). 218 * Multi-layers supported in v1.0, where GRALLOC1_CAPABILITY_LAYERED_BUFFERS is enabled. 219 * Layer size: 'size' / 'layer_count'. 220 * Layer (n) offset: n * ('size' / 'layer_count'), n=0 for the first layer. 221 * 222 */ 223 uint64_t alloc_format; 224 plane_info_t plane_info[MAX_PLANES]; 225 union 226 { 227 int size; 228 int sizes[3]; 229 }; 230 uint32_t stride; 231 uint32_t layer_count; 232 233 union 234 { 235 void *base; 236 uint64_t bases[3]; 237 }; 238 239 uint64_t backing_store_id; 240 int backing_store_size; 241 int cpu_read; /**< Buffer is locked for CPU read when non-zero. */ 242 int cpu_write; /**< Buffer is locked for CPU write when non-zero. */ 243 int allocating_pid; 244 int remote_pid; 245 int ref_count; 246 // locally mapped shared attribute area 247 union 248 { 249 void *attr_base; 250 uint64_t padding3; 251 }; 252 253 /* 254 * Deprecated. 255 * Use GRALLOC_ARM_BUFFER_ATTR_DATASPACE 256 * instead. 257 */ 258 mali_gralloc_yuv_info yuv_info; 259 260 // Following members is for framebuffer only 261 int fb_fd; 262 union 263 { 264 off_t offset; 265 uint64_t padding4; 266 }; 267 268 /* 269 * min_pgsz denotes minimum phys_page size used by this buffer. 270 * if buffer memory is physical contiguous set min_pgsz to buff->size 271 * if not sure buff's real phys_page size, you can use SZ_4K for safe. 272 */ 273 int min_pgsz = SZ_4K; 274 275 int is_compressible = 0; 276 277 ion_user_handle_t ion_handles[3] = {0, 0, 0}; 278 279 int PRIVATE_1 = 0; 280 int PRIVATE_2 = 0; 281 int plane_count = 1; 282 283 #ifdef __cplusplus 284 /* 285 * We track the number of integers in the structure. There are 16 unconditional 286 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is 287 * considered an int not an fd because it is not intended to be used outside the 288 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the 289 * number of integers that are conditionally included. Similar considerations apply 290 * to the number of fds. 291 */ 292 static const int sNumFds = GRALLOC_ARM_NUM_FDS; 293 static const int sMagic = 0x3141592; 294 private_handle_tprivate_handle_t295 private_handle_t(int _flags, int _size, void *_base, uint64_t _consumer_usage, uint64_t _producer_usage, 296 int fb_file, off_t fb_offset, int _byte_stride, int _width, int _height, uint64_t _alloc_format) 297 : share_fd(-1) 298 , magic(sMagic) 299 , flags(_flags) 300 , width(0) 301 , height(0) 302 , req_format(0) 303 , producer_usage(_producer_usage) 304 , consumer_usage(_consumer_usage) 305 , alloc_format(_alloc_format) 306 , size(_size) 307 , stride(0) 308 , layer_count(0) 309 , base(_base) 310 , backing_store_id(0x0) 311 , backing_store_size(0) 312 , cpu_read(0) 313 , cpu_write(0) 314 , allocating_pid(getpid()) 315 , remote_pid(-1) 316 , ref_count(1) 317 , attr_base(MAP_FAILED) 318 , yuv_info(MALI_YUV_NO_INFO) 319 , fb_fd(fb_file) 320 , offset(fb_offset) 321 , plane_count(1) 322 { 323 version = sizeof(native_handle); 324 325 fds[1] = fds[2] = fds[3] = fds[4] = -1; 326 sizes[1] = sizes[2] = 0; 327 328 numFds = sNumFds; 329 numInts = NUM_INTS_IN_PRIVATE_HANDLE; 330 memset(plane_info, 0, sizeof(plane_info_t) * MAX_PLANES); 331 332 bases[1] = 0; 333 bases[2] = 0; 334 335 plane_info[0].offset = fb_offset; 336 plane_info[0].byte_stride = _byte_stride; 337 plane_info[0].alloc_width = _width; 338 plane_info[0].alloc_height = _height; 339 } 340 private_handle_tprivate_handle_t341 private_handle_t(int _flags, int _size, int _size1, int _size2, int _min_pgsz, 342 uint64_t _consumer_usage, uint64_t _producer_usage, 343 int _shared_fd, int _fd_yuv1, int _fd_yuv2, 344 int _req_format, uint64_t _internal_format, uint64_t _alloc_format, 345 int _width, int _height, int _backing_store_size, uint32_t _stride, 346 uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES], int _is_compressible, int _plane_count) 347 : share_fd(_shared_fd) 348 , magic(sMagic) 349 , flags(_flags) 350 , width(_width) 351 , height(_height) 352 , req_format(_req_format) 353 , producer_usage(_producer_usage) 354 , consumer_usage(_consumer_usage) 355 , internal_format(_internal_format) 356 , alloc_format(_alloc_format) 357 , stride(_stride) 358 , layer_count(_layer_count) 359 , base(NULL) 360 , backing_store_id(0x0) 361 , backing_store_size(_backing_store_size) 362 , cpu_read(0) 363 , cpu_write(0) 364 , allocating_pid(getpid()) 365 , remote_pid(-1) 366 , ref_count(1) 367 , attr_base(MAP_FAILED) 368 , yuv_info(MALI_YUV_NO_INFO) 369 , fb_fd(-1) 370 , offset(0) 371 , min_pgsz(_min_pgsz) 372 , is_compressible(_is_compressible) 373 , plane_count(_plane_count) 374 { 375 version = sizeof(native_handle); 376 377 fds[1] = fds[2] = fds[3] = fds[4] = -1; 378 sizes[0] = _size; 379 sizes[1] = _size1; 380 sizes[2] = _size2; 381 382 numFds = sNumFds; 383 numInts = NUM_INTS_IN_PRIVATE_HANDLE; 384 memcpy(plane_info, _plane_info, sizeof(plane_info_t) * MAX_PLANES); 385 386 if (_fd_yuv1 != -1) 387 { 388 fds[1] = _fd_yuv1; 389 numFds++; 390 numInts--; 391 } 392 if (_fd_yuv2 != -1) 393 { 394 fds[2] = _fd_yuv2; 395 numFds++; 396 numInts--; 397 } 398 399 bases[1] = 0; 400 bases[2] = 0; 401 } 402 ~private_handle_tprivate_handle_t403 ~private_handle_t() 404 { 405 magic = 0; 406 } 407 dumpprivate_handle_t408 void dump(const char *str) const 409 { 410 ALOGI("[%s] " 411 "fd(%d %d %d %d) " 412 "flags(%d) " 413 "wh(%d %d) " 414 "req_format(0x%x) " 415 "usage_pc(0x%" PRIx64 " 0x%" PRIx64 ") " 416 "interal_format(0x%" PRIx64 ") " 417 "stride(%d)" 418 "plane[0] (offset(%d) byte_stride(%d) internal_wh(%d %d)) " 419 "plane[1] (offset(%d) byte_stride(%d) internal_wh(%d %d)) " 420 "plane[2] (offset(%d) byte_stride(%d) internal_wh(%d %d)) " 421 "alloc_format(0x%" PRIx64 ") " 422 "size(%d %d %d) " 423 "layer_count(%d) plane_count(%d)" 424 "bases(0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 ") " 425 "\n", 426 str, 427 fds[0], fds[1], fds[2], fds[3], 428 flags, 429 width, height, 430 req_format, 431 producer_usage, consumer_usage, 432 internal_format, 433 stride, 434 plane_info[0].offset, plane_info[0].byte_stride, plane_info[0].alloc_width, plane_info[0].alloc_height, 435 plane_info[1].offset, plane_info[1].byte_stride, plane_info[1].alloc_width, plane_info[1].alloc_height, 436 plane_info[2].offset, plane_info[2].byte_stride, plane_info[2].alloc_width, plane_info[2].alloc_height, 437 alloc_format, 438 sizes[0], sizes[1], sizes[2], 439 layer_count, plane_count, 440 bases[0], bases[1], bases[2] 441 ); 442 } 443 usesPhysicallyContiguousMemoryprivate_handle_t444 bool usesPhysicallyContiguousMemory() 445 { 446 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false; 447 } 448 validateprivate_handle_t449 static int validate(const native_handle *h) 450 { 451 const private_handle_t *hnd = (const private_handle_t *)h; 452 453 if (!h || h->version != sizeof(native_handle) || 454 hnd->numInts + hnd->numFds != NUM_INTS_IN_PRIVATE_HANDLE + sNumFds || 455 hnd->magic != sMagic) 456 { 457 return -EINVAL; 458 } 459 460 return 0; 461 } 462 is_multi_planeprivate_handle_t463 bool is_multi_plane() const 464 { 465 /* For multi-plane, the byte stride for the second plane will always be non-zero. */ 466 return plane_count > 1; 467 } 468 dynamicCastprivate_handle_t469 static private_handle_t *dynamicCast(const native_handle *in) 470 { 471 if (validate(in) == 0) 472 { 473 return (private_handle_t *)in; 474 } 475 476 return NULL; 477 } 478 get_share_attr_fdprivate_handle_t479 int get_share_attr_fd() const 480 { 481 return fds[numFds - 1]; 482 } 483 set_share_attr_fdprivate_handle_t484 void set_share_attr_fd(int _share_attr_fd) 485 { 486 fds[numFds - 1] = _share_attr_fd; 487 } 488 get_num_ion_fdsprivate_handle_t489 int get_num_ion_fds() const 490 { 491 return numFds - 1; 492 } 493 #endif 494 }; 495 #ifndef __cplusplus 496 /* Restore previous diagnostic for pedantic */ 497 #pragma GCC diagnostic pop 498 #endif 499 500 #endif /* MALI_GRALLOC_BUFFER_H_ */ 501