Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 GRALLOC_PRIV_H_ |
| 20 | #define GRALLOC_PRIV_H_ |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <pthread.h> |
| 24 | #include <errno.h> |
| 25 | #include <linux/fb.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <hardware/gralloc.h> |
| 30 | #include <cutils/native_handle.h> |
| 31 | #include <alloc_device.h> |
| 32 | #include <utils/Log.h> |
| 33 | |
| 34 | #ifdef MALI_600 |
| 35 | #define GRALLOC_ARM_UMP_MODULE 0 |
| 36 | #define GRALLOC_ARM_DMA_BUF_MODULE 1 |
| 37 | #else |
| 38 | |
| 39 | /* NOTE: |
| 40 | * If your framebuffer device driver is integrated with UMP, you will have to |
| 41 | * change this IOCTL definition to reflect your integration with the framebuffer |
| 42 | * device. |
| 43 | * Expected return value is a UMP secure id backing your framebuffer device memory. |
| 44 | */ |
| 45 | |
| 46 | /*#define IOCTL_GET_FB_UMP_SECURE_ID _IOR('F', 311, unsigned int)*/ |
| 47 | #define GRALLOC_ARM_UMP_MODULE 0 |
| 48 | #define GRALLOC_ARM_DMA_BUF_MODULE 1 |
| 49 | |
| 50 | /* NOTE: |
| 51 | * If your framebuffer device driver is integrated with dma_buf, you will have to |
| 52 | * change this IOCTL definition to reflect your integration with the framebuffer |
| 53 | * device. |
| 54 | * Expected return value is a structure filled with a file descriptor |
| 55 | * backing your framebuffer device memory. |
| 56 | */ |
| 57 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 58 | struct fb_dmabuf_export |
| 59 | { |
| 60 | __u32 fd; |
| 61 | __u32 flags; |
| 62 | }; |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 63 | |
| 64 | /* Un-comment this line to use dma_buf framebuffer */ |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 65 | /*#define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)*/ |
| 66 | |
| 67 | #if PLATFORM_SDK_VERSION >= 21 |
| 68 | typedef int ion_user_handle_t; |
| 69 | #define ION_INVALID_HANDLE 0 |
| 70 | #else |
| 71 | |
| 72 | typedef struct ion_handle *ion_user_handle_t; |
| 73 | |
| 74 | #define ION_INVALID_HANDLE NULL |
| 75 | #endif /* new libion */ |
| 76 | |
| 77 | #endif /* GRALLOC_ARM_DMA_BUF_MODULE */ |
| 78 | |
| 79 | |
| 80 | #endif |
| 81 | |
| 82 | /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0 |
| 83 | * 8 is big enough for "gpu0" & "fb0" currently |
| 84 | */ |
| 85 | #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8 |
| 86 | #define NUM_FB_BUFFERS 2 |
| 87 | |
| 88 | #if GRALLOC_ARM_UMP_MODULE |
| 89 | #include <ump/ump.h> |
| 90 | #endif |
| 91 | |
| 92 | #define MALI_IGNORE(x) (void)x |
| 93 | typedef enum |
| 94 | { |
| 95 | MALI_YUV_NO_INFO, |
| 96 | MALI_YUV_BT601_NARROW, |
| 97 | MALI_YUV_BT601_WIDE, |
| 98 | MALI_YUV_BT709_NARROW, |
| 99 | MALI_YUV_BT709_WIDE, |
| 100 | } mali_gralloc_yuv_info; |
| 101 | |
| 102 | struct private_handle_t; |
| 103 | |
| 104 | struct private_module_t |
| 105 | { |
| 106 | gralloc_module_t base; |
| 107 | |
| 108 | private_handle_t *framebuffer; |
| 109 | uint32_t flags; |
| 110 | uint32_t numBuffers; |
| 111 | uint32_t bufferMask; |
| 112 | pthread_mutex_t lock; |
| 113 | buffer_handle_t currentBuffer; |
| 114 | int ion_client; |
| 115 | |
| 116 | struct fb_var_screeninfo info; |
| 117 | struct fb_fix_screeninfo finfo; |
| 118 | float xdpi; |
| 119 | float ydpi; |
| 120 | float fps; |
| 121 | |
| 122 | enum |
| 123 | { |
| 124 | // flag to indicate we'll post this buffer |
| 125 | PRIV_USAGE_LOCKED_FOR_POST = 0x80000000 |
| 126 | }; |
| 127 | |
| 128 | /* default constructor */ |
| 129 | private_module_t(); |
| 130 | }; |
| 131 | |
| 132 | #ifdef __cplusplus |
| 133 | struct private_handle_t : public native_handle |
| 134 | { |
| 135 | #else |
| 136 | struct private_handle_t |
| 137 | { |
| 138 | struct native_handle nativeHandle; |
| 139 | #endif |
| 140 | |
| 141 | enum |
| 142 | { |
| 143 | PRIV_FLAGS_FRAMEBUFFER = 0x00000001, |
| 144 | PRIV_FLAGS_USES_UMP = 0x00000002, |
| 145 | PRIV_FLAGS_USES_ION = 0x00000004, |
| 146 | }; |
| 147 | |
| 148 | enum |
| 149 | { |
| 150 | LOCK_STATE_WRITE = 1 << 31, |
| 151 | LOCK_STATE_MAPPED = 1 << 30, |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 152 | LOCK_STATE_UNREGISTERED = 1 << 29, |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 153 | LOCK_STATE_READ_MASK = 0x3FFFFFFF |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | // ints |
| 157 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 158 | /*shared file descriptor for dma_buf sharing*/ |
| 159 | int share_fd; |
| 160 | #endif |
| 161 | int magic; |
| 162 | int flags; |
| 163 | int usage; |
| 164 | int size; |
| 165 | int width; |
| 166 | int height; |
| 167 | int format; |
| 168 | int stride; |
| 169 | union |
| 170 | { |
| 171 | void *base; |
| 172 | uint64_t padding; |
| 173 | }; |
| 174 | int lockState; |
| 175 | int writeOwner; |
| 176 | int pid; |
| 177 | |
| 178 | mali_gralloc_yuv_info yuv_info; |
| 179 | |
| 180 | // Following members are for UMP memory only |
| 181 | #if GRALLOC_ARM_UMP_MODULE |
| 182 | int ump_id; |
| 183 | int ump_mem_handle; |
| 184 | #endif |
| 185 | |
| 186 | // Following members is for framebuffer only |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 187 | int fd; //Shallow copy, DO NOT duplicate |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 188 | int offset; |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 189 | union |
| 190 | { |
| 191 | void *fb_paddr; |
| 192 | uint64_t fb_paddr_padding; |
| 193 | }; |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 194 | #if GRALLOC_ARM_DMA_BUF_MODULE |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 195 | ion_user_handle_t ion_hnd; |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 196 | #endif |
| 197 | |
| 198 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 199 | #define GRALLOC_ARM_NUM_FDS 1 |
| 200 | #else |
| 201 | #define GRALLOC_ARM_NUM_FDS 0 |
| 202 | #endif |
| 203 | |
| 204 | #ifdef __cplusplus |
| 205 | static const int sNumFds = GRALLOC_ARM_NUM_FDS; |
| 206 | static const int sMagic = 0x3141592; |
| 207 | |
| 208 | #if GRALLOC_ARM_UMP_MODULE |
| 209 | private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle): |
| 210 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 211 | share_fd(-1), |
| 212 | #endif |
| 213 | magic(sMagic), |
| 214 | flags(flags), |
| 215 | usage(usage), |
| 216 | size(size), |
| 217 | width(0), |
| 218 | height(0), |
| 219 | format(0), |
| 220 | stride(0), |
| 221 | base(base), |
| 222 | lockState(lock_state), |
| 223 | writeOwner(0), |
| 224 | pid(getpid()), |
| 225 | yuv_info(MALI_YUV_NO_INFO), |
| 226 | ump_id((int)secure_id), |
| 227 | ump_mem_handle((int)handle), |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 228 | fd(0), |
| 229 | offset(0), |
| 230 | fb_paddr(NULL) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 231 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 232 | , |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 233 | ion_hnd(ION_INVALID_HANDLE) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 234 | #endif |
| 235 | |
| 236 | { |
| 237 | version = sizeof(native_handle); |
| 238 | numFds = sNumFds; |
| 239 | numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds; |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 244 | private_handle_t(int flags, int usage, int size, void *base, int lock_state): |
| 245 | share_fd(-1), |
| 246 | magic(sMagic), |
| 247 | flags(flags), |
| 248 | usage(usage), |
| 249 | size(size), |
| 250 | width(0), |
| 251 | height(0), |
| 252 | format(0), |
| 253 | stride(0), |
| 254 | base(base), |
| 255 | lockState(lock_state), |
| 256 | writeOwner(0), |
| 257 | pid(getpid()), |
| 258 | yuv_info(MALI_YUV_NO_INFO), |
| 259 | #if GRALLOC_ARM_UMP_MODULE |
| 260 | ump_id((int)UMP_INVALID_SECURE_ID), |
| 261 | ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE), |
| 262 | #endif |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 263 | fd(0), |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 264 | offset(0), |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 265 | fb_paddr(NULL), |
| 266 | ion_hnd(ION_INVALID_HANDLE) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 267 | |
| 268 | { |
| 269 | version = sizeof(native_handle); |
| 270 | numFds = sNumFds; |
| 271 | numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds; |
| 272 | } |
| 273 | |
| 274 | #endif |
| 275 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 276 | private_handle_t(int flags, int usage, int size, void *base, int lock_state, int fb_file, int fb_offset, void *fb_paddr): |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 277 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 278 | share_fd(-1), |
| 279 | #endif |
| 280 | magic(sMagic), |
| 281 | flags(flags), |
| 282 | usage(usage), |
| 283 | size(size), |
| 284 | width(0), |
| 285 | height(0), |
| 286 | format(0), |
| 287 | stride(0), |
| 288 | base(base), |
| 289 | lockState(lock_state), |
| 290 | writeOwner(0), |
| 291 | pid(getpid()), |
| 292 | yuv_info(MALI_YUV_NO_INFO), |
| 293 | #if GRALLOC_ARM_UMP_MODULE |
| 294 | ump_id((int)UMP_INVALID_SECURE_ID), |
| 295 | ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE), |
| 296 | #endif |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 297 | fd(fb_file), |
| 298 | offset(fb_offset), |
| 299 | fb_paddr(fb_paddr) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 300 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 301 | , |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 302 | ion_hnd(ION_INVALID_HANDLE) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 303 | #endif |
| 304 | |
| 305 | { |
| 306 | version = sizeof(native_handle); |
| 307 | numFds = sNumFds; |
| 308 | numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds; |
| 309 | } |
| 310 | |
| 311 | ~private_handle_t() |
| 312 | { |
| 313 | magic = 0; |
| 314 | } |
| 315 | |
| 316 | bool usesPhysicallyContiguousMemory() |
| 317 | { |
| 318 | return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false; |
| 319 | } |
| 320 | |
| 321 | static int validate(const native_handle *h) |
| 322 | { |
| 323 | const private_handle_t *hnd = (const private_handle_t *)h; |
| 324 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 325 | if (!hnd || hnd->version != sizeof(native_handle) || hnd->magic != sMagic) |
Chia-I Wu | a12e37d | 2017-05-08 12:46:13 -0700 | [diff] [blame] | 326 | { |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | int numFds = sNumFds; |
| 331 | int numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds; |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 332 | |
Chia-I Wu | a12e37d | 2017-05-08 12:46:13 -0700 | [diff] [blame] | 333 | #if GRALLOC_ARM_DMA_BUF_MODULE |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 334 | if (hnd->share_fd < 0) |
| 335 | { |
Chia-I Wu | a12e37d | 2017-05-08 12:46:13 -0700 | [diff] [blame] | 336 | numFds--; |
| 337 | numInts++; |
| 338 | } |
| 339 | #endif |
| 340 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 341 | if (hnd->numFds != numFds || hnd->numInts != numInts) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 342 | { |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static private_handle_t *dynamicCast(const native_handle *in) |
| 350 | { |
| 351 | if (validate(in) == 0) |
| 352 | { |
| 353 | return (private_handle_t *) in; |
| 354 | } |
| 355 | |
| 356 | return NULL; |
| 357 | } |
| 358 | #endif |
| 359 | }; |
| 360 | |
| 361 | #endif /* GRALLOC_PRIV_H_ */ |