blob: 582c48754c32fb3d331df6d5c71ed64219f8c141 [file] [log] [blame]
Vishal Bhoj78e90492015-12-07 01:36:32 +05301/*
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
58struct fb_dmabuf_export
59{
60 __u32 fd;
61 __u32 flags;
62};
63/*#define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)*/
64
65#if PLATFORM_SDK_VERSION >= 21
66typedef int ion_user_handle_t;
67#define ION_INVALID_HANDLE 0
68#else
69
70typedef struct ion_handle *ion_user_handle_t;
71
72#define ION_INVALID_HANDLE NULL
73#endif /* new libion */
74
75#endif /* GRALLOC_ARM_DMA_BUF_MODULE */
76
77
78#endif
79
80/* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
81 * 8 is big enough for "gpu0" & "fb0" currently
82 */
83#define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
84#define NUM_FB_BUFFERS 2
85
86#if GRALLOC_ARM_UMP_MODULE
87#include <ump/ump.h>
88#endif
89
90#define MALI_IGNORE(x) (void)x
91typedef enum
92{
93 MALI_YUV_NO_INFO,
94 MALI_YUV_BT601_NARROW,
95 MALI_YUV_BT601_WIDE,
96 MALI_YUV_BT709_NARROW,
97 MALI_YUV_BT709_WIDE,
98} mali_gralloc_yuv_info;
99
100struct private_handle_t;
101
102struct private_module_t
103{
104 gralloc_module_t base;
105
106 private_handle_t *framebuffer;
107 uint32_t flags;
108 uint32_t numBuffers;
109 uint32_t bufferMask;
110 pthread_mutex_t lock;
111 buffer_handle_t currentBuffer;
112 int ion_client;
Laura Abbott6ed00bc2017-06-30 11:39:35 +0530113 int system_heap_id;
114 bool gralloc_legacy_ion;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530115
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
133struct private_handle_t : public native_handle
134{
135#else
136struct 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 Stultz37287452016-01-20 20:08:46 -0800152 LOCK_STATE_UNREGISTERED = 1 << 29,
153 LOCK_STATE_READ_MASK = 0x1FFFFFFF
Vishal Bhoj78e90492015-12-07 01:36:32 +0530154 };
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
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700187 int shallow_fbdev_fd; // shallow copy, not dup'ed
Vishal Bhoj78e90492015-12-07 01:36:32 +0530188 int offset;
189
190#if GRALLOC_ARM_DMA_BUF_MODULE
Chia-I Wu06695a62017-05-08 12:55:28 -0700191 ion_user_handle_t ion_hnd_UNUSED;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530192#endif
193
194#if GRALLOC_ARM_DMA_BUF_MODULE
195#define GRALLOC_ARM_NUM_FDS 1
196#else
197#define GRALLOC_ARM_NUM_FDS 0
198#endif
199
200#ifdef __cplusplus
201 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
202 static const int sMagic = 0x3141592;
203
204#if GRALLOC_ARM_UMP_MODULE
205 private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle):
206#if GRALLOC_ARM_DMA_BUF_MODULE
207 share_fd(-1),
208#endif
209 magic(sMagic),
210 flags(flags),
211 usage(usage),
212 size(size),
213 width(0),
214 height(0),
215 format(0),
216 stride(0),
217 base(base),
218 lockState(lock_state),
219 writeOwner(0),
220 pid(getpid()),
221 yuv_info(MALI_YUV_NO_INFO),
222 ump_id((int)secure_id),
223 ump_mem_handle((int)handle),
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700224 shallow_fbdev_fd(0),
Vishal Bhoj78e90492015-12-07 01:36:32 +0530225 offset(0)
226#if GRALLOC_ARM_DMA_BUF_MODULE
227 ,
Chia-I Wu06695a62017-05-08 12:55:28 -0700228 ion_hnd_UNUSED(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530229#endif
230
231 {
232 version = sizeof(native_handle);
233 numFds = sNumFds;
234 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
235 }
236#endif
237
238#if GRALLOC_ARM_DMA_BUF_MODULE
239 private_handle_t(int flags, int usage, int size, void *base, int lock_state):
240 share_fd(-1),
241 magic(sMagic),
242 flags(flags),
243 usage(usage),
244 size(size),
245 width(0),
246 height(0),
247 format(0),
248 stride(0),
249 base(base),
250 lockState(lock_state),
251 writeOwner(0),
252 pid(getpid()),
253 yuv_info(MALI_YUV_NO_INFO),
254#if GRALLOC_ARM_UMP_MODULE
255 ump_id((int)UMP_INVALID_SECURE_ID),
256 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
257#endif
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700258 shallow_fbdev_fd(0),
Vishal Bhoj78e90492015-12-07 01:36:32 +0530259 offset(0),
Chia-I Wu06695a62017-05-08 12:55:28 -0700260 ion_hnd_UNUSED(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530261
262 {
263 version = sizeof(native_handle);
264 numFds = sNumFds;
265 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
266 }
267
268#endif
269
270 private_handle_t(int flags, int usage, int size, void *base, int lock_state, int fb_file, int fb_offset):
271#if GRALLOC_ARM_DMA_BUF_MODULE
272 share_fd(-1),
273#endif
274 magic(sMagic),
275 flags(flags),
276 usage(usage),
277 size(size),
278 width(0),
279 height(0),
280 format(0),
281 stride(0),
282 base(base),
283 lockState(lock_state),
284 writeOwner(0),
285 pid(getpid()),
286 yuv_info(MALI_YUV_NO_INFO),
287#if GRALLOC_ARM_UMP_MODULE
288 ump_id((int)UMP_INVALID_SECURE_ID),
289 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
290#endif
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700291 shallow_fbdev_fd(fb_file),
Vishal Bhoj78e90492015-12-07 01:36:32 +0530292 offset(fb_offset)
293#if GRALLOC_ARM_DMA_BUF_MODULE
294 ,
Chia-I Wu06695a62017-05-08 12:55:28 -0700295 ion_hnd_UNUSED(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530296#endif
297
298 {
299 version = sizeof(native_handle);
300 numFds = sNumFds;
301 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
302 }
303
304 ~private_handle_t()
305 {
306 magic = 0;
307 }
308
309 bool usesPhysicallyContiguousMemory()
310 {
311 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
312 }
313
314 static int validate(const native_handle *h)
315 {
316 const private_handle_t *hnd = (const private_handle_t *)h;
317
Chia-I Wua12e37d2017-05-08 12:46:13 -0700318 if (!h || h->version != sizeof(native_handle) || hnd->magic != sMagic)
319 {
320 return -EINVAL;
321 }
322
323 int numFds = sNumFds;
324 int numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
325#if GRALLOC_ARM_DMA_BUF_MODULE
326 if (hnd->share_fd < 0) {
327 numFds--;
328 numInts++;
329 }
330#endif
331
332 if (h->numFds != numFds || h->numInts != numInts)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530333 {
334 return -EINVAL;
335 }
336
337 return 0;
338 }
339
340 static private_handle_t *dynamicCast(const native_handle *in)
341 {
342 if (validate(in) == 0)
343 {
344 return (private_handle_t *) in;
345 }
346
347 return NULL;
348 }
349#endif
350};
351
352#endif /* GRALLOC_PRIV_H_ */