John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [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 <linux/ion.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <hardware/gralloc.h> |
| 31 | #include <cutils/native_handle.h> |
| 32 | #include "alloc_device.h" |
| 33 | #include <utils/Log.h> |
| 34 | |
| 35 | #include "mali_gralloc_formats.h" |
| 36 | #include "gralloc_helper.h" |
| 37 | |
| 38 | |
| 39 | /* NOTE: |
| 40 | * If your framebuffer device driver is integrated with dma_buf, you will have to |
| 41 | * change this IOCTL definition to reflect your integration with the framebuffer |
| 42 | * device. |
| 43 | * Expected return value is a structure filled with a file descriptor |
| 44 | * backing your framebuffer device memory. |
| 45 | */ |
| 46 | struct fb_dmabuf_export |
| 47 | { |
| 48 | __u32 fd; |
| 49 | __u32 flags; |
| 50 | }; |
| 51 | #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export) |
| 52 | |
| 53 | |
| 54 | /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0 |
| 55 | * 8 is big enough for "gpu0" & "fb0" currently |
| 56 | */ |
| 57 | #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8 |
| 58 | #define NUM_FB_BUFFERS 2 |
| 59 | |
| 60 | /* Define number of shared file descriptors */ |
| 61 | #define GRALLOC_ARM_NUM_FDS 2 |
| 62 | |
| 63 | #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds) |
| 64 | |
| 65 | #define SZ_4K 0x00001000 |
| 66 | #define SZ_2M 0x00200000 |
| 67 | |
| 68 | typedef enum |
| 69 | { |
| 70 | MALI_YUV_NO_INFO, |
| 71 | MALI_YUV_BT601_NARROW, |
| 72 | MALI_YUV_BT601_WIDE, |
| 73 | MALI_YUV_BT709_NARROW, |
| 74 | MALI_YUV_BT709_WIDE |
| 75 | } mali_gralloc_yuv_info; |
| 76 | |
| 77 | typedef enum |
| 78 | { |
| 79 | MALI_DPY_TYPE_UNKNOWN = 0, |
| 80 | MALI_DPY_TYPE_CLCD, |
| 81 | MALI_DPY_TYPE_HDLCD |
| 82 | } mali_dpy_type; |
| 83 | |
| 84 | struct private_handle_t; |
| 85 | |
| 86 | struct private_module_t |
| 87 | { |
| 88 | gralloc_module_t base; |
| 89 | |
| 90 | struct private_handle_t* framebuffer; |
| 91 | uint32_t flags; |
| 92 | uint32_t numBuffers; |
| 93 | uint32_t bufferMask; |
| 94 | pthread_mutex_t lock; |
| 95 | buffer_handle_t currentBuffer; |
| 96 | int ion_client; |
| 97 | mali_dpy_type dpy_type; |
| 98 | |
| 99 | struct fb_var_screeninfo info; |
| 100 | struct fb_fix_screeninfo finfo; |
| 101 | float xdpi; |
| 102 | float ydpi; |
| 103 | float fps; |
| 104 | int swapInterval; |
| 105 | |
| 106 | #ifdef __cplusplus |
| 107 | /* Never intended to be used from C code */ |
| 108 | enum |
| 109 | { |
| 110 | // flag to indicate we'll post this buffer |
| 111 | PRIV_USAGE_LOCKED_FOR_POST = 0x80000000 |
| 112 | }; |
| 113 | #endif |
| 114 | |
| 115 | #ifdef __cplusplus |
| 116 | /* default constructor */ |
| 117 | private_module_t(); |
| 118 | #endif |
| 119 | }; |
| 120 | |
| 121 | #ifndef __cplusplus |
| 122 | /* C99 with pedantic don't allow anonymous unions which is used in below struct |
| 123 | * Disable pedantic for C for this struct only. |
| 124 | */ |
| 125 | #pragma GCC diagnostic push |
| 126 | #pragma GCC diagnostic ignored "-Wpedantic" |
| 127 | #endif |
| 128 | |
| 129 | #ifdef __cplusplus |
| 130 | struct private_handle_t : public native_handle |
| 131 | { |
| 132 | #else |
| 133 | struct private_handle_t |
| 134 | { |
| 135 | struct native_handle nativeHandle; |
| 136 | #endif |
| 137 | |
| 138 | #ifdef __cplusplus |
| 139 | /* Never intended to be used from C code */ |
| 140 | enum |
| 141 | { |
| 142 | PRIV_FLAGS_FRAMEBUFFER = 0x00000001, |
| 143 | PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002, |
| 144 | PRIV_FLAGS_USES_ION = 0x00000004, |
| 145 | PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008 |
| 146 | }; |
| 147 | |
| 148 | enum |
| 149 | { |
| 150 | LOCK_STATE_WRITE = 1<<31, |
| 151 | LOCK_STATE_MAPPED = 1<<30, |
| 152 | LOCK_STATE_READ_MASK = 0x3FFFFFFF |
| 153 | }; |
| 154 | #endif |
| 155 | |
| 156 | /* |
| 157 | * Shared file descriptor for dma_buf sharing. This must be the first element in the |
| 158 | * structure so that binder knows where it is and can properly share it between |
| 159 | * processes. |
| 160 | * DO NOT MOVE THIS ELEMENT! |
| 161 | */ |
| 162 | int share_fd; |
| 163 | int share_attr_fd; |
| 164 | |
Chia-I Wu | a3db107 | 2017-05-08 12:55:28 -0700 | [diff] [blame] | 165 | ion_user_handle_t ion_hnd_UNUSED; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 166 | |
| 167 | // ints |
| 168 | int magic; |
| 169 | int req_format; |
| 170 | uint64_t internal_format; |
| 171 | int byte_stride; |
| 172 | int flags; |
| 173 | int usage; |
| 174 | int size; |
| 175 | int width; |
| 176 | int height; |
| 177 | int internalWidth; |
| 178 | int internalHeight; |
| 179 | int stride; |
| 180 | union { |
| 181 | void* base; |
| 182 | uint64_t padding; |
| 183 | }; |
| 184 | int lockState; |
| 185 | int writeOwner; |
Chia-I Wu | a3db107 | 2017-05-08 12:55:28 -0700 | [diff] [blame] | 186 | int pid_UNUSED; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 187 | |
| 188 | // locally mapped shared attribute area |
| 189 | union { |
| 190 | void* attr_base; |
| 191 | uint64_t padding3; |
| 192 | }; |
| 193 | |
| 194 | mali_gralloc_yuv_info yuv_info; |
| 195 | |
| 196 | // Following members is for framebuffer only |
Chia-I Wu | 5f82a37 | 2017-05-08 12:50:36 -0700 | [diff] [blame] | 197 | int shallow_fbdev_fd; // shallow copy, not dup'ed |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 198 | union { |
| 199 | off_t offset; |
| 200 | uint64_t padding4; |
| 201 | }; |
| 202 | |
| 203 | /* |
| 204 | * min_pgsz denotes minimum phys_page size used by this buffer. |
| 205 | * if buffer memory is physical contiguous set min_pgsz to buff->size |
| 206 | * if not sure buff's real phys_page size, you can use SZ_4K for safe. |
| 207 | */ |
| 208 | int min_pgsz; |
| 209 | #ifdef __cplusplus |
| 210 | /* |
| 211 | * We track the number of integers in the structure. There are 16 unconditional |
Chia-I Wu | 8853aae | 2017-07-14 08:34:25 -0700 | [diff] [blame] | 212 | * integers (magic - pid, yuv_info, shallow_fbdev_fd and offset). |
| 213 | * Note that the shallow_fbdev_fd element is |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 214 | * considered an int not an fd because it is not intended to be used outside the |
| 215 | * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the |
| 216 | * number of integers that are conditionally included. Similar considerations apply |
| 217 | * to the number of fds. |
| 218 | */ |
| 219 | static const int sNumFds = GRALLOC_ARM_NUM_FDS; |
| 220 | static const int sMagic = 0x3141592; |
| 221 | |
| 222 | private_handle_t(int _flags, int _usage, int _size, void *_base, int lock_state, int fb_file, off_t fb_offset): |
| 223 | share_fd(-1), |
| 224 | share_attr_fd(-1), |
Chia-I Wu | a3db107 | 2017-05-08 12:55:28 -0700 | [diff] [blame] | 225 | ion_hnd_UNUSED(-1), |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 226 | magic(sMagic), |
| 227 | flags(_flags), |
| 228 | usage(_usage), |
| 229 | size(_size), |
| 230 | width(0), |
| 231 | height(0), |
| 232 | stride(0), |
| 233 | base(_base), |
| 234 | lockState(lock_state), |
| 235 | writeOwner(0), |
Chia-I Wu | a3db107 | 2017-05-08 12:55:28 -0700 | [diff] [blame] | 236 | pid_UNUSED(-1), |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 237 | attr_base(MAP_FAILED), |
| 238 | yuv_info(MALI_YUV_NO_INFO), |
Chia-I Wu | 5f82a37 | 2017-05-08 12:50:36 -0700 | [diff] [blame] | 239 | shallow_fbdev_fd(fb_file), |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 240 | offset(fb_offset) |
| 241 | { |
| 242 | version = sizeof(native_handle); |
| 243 | numFds = sNumFds; |
| 244 | numInts = NUM_INTS_IN_PRIVATE_HANDLE; |
| 245 | } |
| 246 | |
| 247 | ~private_handle_t() |
| 248 | { |
| 249 | magic = 0; |
| 250 | } |
| 251 | |
| 252 | bool usesPhysicallyContiguousMemory() |
| 253 | { |
| 254 | return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false; |
| 255 | } |
| 256 | |
| 257 | static int validate(const native_handle* h) |
| 258 | { |
| 259 | const private_handle_t* hnd = (const private_handle_t*)h; |
| 260 | if (!h || |
| 261 | h->version != sizeof(native_handle) || |
| 262 | h->numInts != NUM_INTS_IN_PRIVATE_HANDLE || |
| 263 | h->numFds != sNumFds || |
| 264 | hnd->magic != sMagic) |
| 265 | { |
| 266 | return -EINVAL; |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static private_handle_t* dynamicCast(const native_handle* in) |
| 272 | { |
| 273 | if (validate(in) == 0) |
| 274 | { |
| 275 | return (private_handle_t*) in; |
| 276 | } |
| 277 | return NULL; |
| 278 | } |
| 279 | #endif |
| 280 | }; |
| 281 | #ifndef __cplusplus |
| 282 | /* Restore previous diagnostic for pedantic */ |
| 283 | #pragma GCC diagnostic pop |
| 284 | #endif |
| 285 | |
| 286 | #endif /* GRALLOC_PRIV_H_ */ |