blob: 3330eae8d3e6d4d67377ce8554d69662ec1be51d [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>
John Stultz25d61292017-12-05 16:14:04 -080031#include "alloc_device.h"
Vishal Bhoj78e90492015-12-07 01:36:32 +053032#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};
John Stultze5c5bb32018-03-20 18:16:49 -070063
64/* Un-comment this line to use dma_buf framebuffer */
Vishal Bhoj78e90492015-12-07 01:36:32 +053065/*#define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)*/
66
67#if PLATFORM_SDK_VERSION >= 21
68typedef int ion_user_handle_t;
69#define ION_INVALID_HANDLE 0
70#else
71
72typedef 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
93typedef 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
102struct private_handle_t;
103
104struct 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;
Laura Abbott311955b2017-06-30 11:39:35 +0530115 int system_heap_id;
John Stultzf3d868d2017-12-05 16:15:11 -0800116 int cma_heap_id;
Laura Abbott311955b2017-06-30 11:39:35 +0530117 bool gralloc_legacy_ion;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530118
119 struct fb_var_screeninfo info;
120 struct fb_fix_screeninfo finfo;
121 float xdpi;
122 float ydpi;
123 float fps;
124
125 enum
126 {
127 // flag to indicate we'll post this buffer
128 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
129 };
130
131 /* default constructor */
132 private_module_t();
133};
134
135#ifdef __cplusplus
136struct private_handle_t : public native_handle
137{
138#else
139struct private_handle_t
140{
141 struct native_handle nativeHandle;
142#endif
143
144 enum
145 {
146 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
147 PRIV_FLAGS_USES_UMP = 0x00000002,
148 PRIV_FLAGS_USES_ION = 0x00000004,
149 };
150
151 enum
152 {
153 LOCK_STATE_WRITE = 1 << 31,
154 LOCK_STATE_MAPPED = 1 << 30,
John Stultz37287452016-01-20 20:08:46 -0800155 LOCK_STATE_UNREGISTERED = 1 << 29,
John Stultze5c5bb32018-03-20 18:16:49 -0700156 LOCK_STATE_READ_MASK = 0x3FFFFFFF
Vishal Bhoj78e90492015-12-07 01:36:32 +0530157 };
158
159 // ints
160#if GRALLOC_ARM_DMA_BUF_MODULE
161 /*shared file descriptor for dma_buf sharing*/
162 int share_fd;
163#endif
164 int magic;
165 int flags;
166 int usage;
167 int size;
168 int width;
169 int height;
John Stultz30941512018-01-04 14:03:55 -0800170 union {
171 int format;
172 int req_format; /* same name as gralloc960 */
173 };
Vishal Bhoj78e90492015-12-07 01:36:32 +0530174 int stride;
175 union
176 {
177 void *base;
178 uint64_t padding;
179 };
180 int lockState;
181 int writeOwner;
182 int pid;
183
184 mali_gralloc_yuv_info yuv_info;
185
186 // Following members are for UMP memory only
187#if GRALLOC_ARM_UMP_MODULE
188 int ump_id;
189 int ump_mem_handle;
190#endif
191
192 // Following members is for framebuffer only
John Stultze5c5bb32018-03-20 18:16:49 -0700193 int fd; //Shallow copy, DO NOT duplicate
Vishal Bhoj78e90492015-12-07 01:36:32 +0530194 int offset;
John Stultze5c5bb32018-03-20 18:16:49 -0700195 union
196 {
197 void *fb_paddr;
198 uint64_t fb_paddr_padding;
199 };
John Stultz30941512018-01-04 14:03:55 -0800200 int byte_stride;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530201#if GRALLOC_ARM_DMA_BUF_MODULE
John Stultze5c5bb32018-03-20 18:16:49 -0700202 ion_user_handle_t ion_hnd;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530203#endif
Vishal Bhoj78e90492015-12-07 01:36:32 +0530204#if GRALLOC_ARM_DMA_BUF_MODULE
205#define GRALLOC_ARM_NUM_FDS 1
206#else
207#define GRALLOC_ARM_NUM_FDS 0
208#endif
209
210#ifdef __cplusplus
211 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
212 static const int sMagic = 0x3141592;
213
214#if GRALLOC_ARM_UMP_MODULE
215 private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle):
216#if GRALLOC_ARM_DMA_BUF_MODULE
217 share_fd(-1),
218#endif
219 magic(sMagic),
220 flags(flags),
221 usage(usage),
222 size(size),
223 width(0),
224 height(0),
225 format(0),
226 stride(0),
227 base(base),
228 lockState(lock_state),
229 writeOwner(0),
230 pid(getpid()),
231 yuv_info(MALI_YUV_NO_INFO),
232 ump_id((int)secure_id),
233 ump_mem_handle((int)handle),
John Stultze5c5bb32018-03-20 18:16:49 -0700234 fd(0),
235 offset(0),
236 fb_paddr(NULL)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530237#if GRALLOC_ARM_DMA_BUF_MODULE
238 ,
John Stultze5c5bb32018-03-20 18:16:49 -0700239 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530240#endif
241
242 {
243 version = sizeof(native_handle);
244 numFds = sNumFds;
245 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
246 }
247#endif
248
249#if GRALLOC_ARM_DMA_BUF_MODULE
250 private_handle_t(int flags, int usage, int size, void *base, int lock_state):
251 share_fd(-1),
252 magic(sMagic),
253 flags(flags),
254 usage(usage),
255 size(size),
256 width(0),
257 height(0),
258 format(0),
259 stride(0),
260 base(base),
261 lockState(lock_state),
262 writeOwner(0),
263 pid(getpid()),
264 yuv_info(MALI_YUV_NO_INFO),
265#if GRALLOC_ARM_UMP_MODULE
266 ump_id((int)UMP_INVALID_SECURE_ID),
267 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
268#endif
John Stultze5c5bb32018-03-20 18:16:49 -0700269 fd(0),
Vishal Bhoj78e90492015-12-07 01:36:32 +0530270 offset(0),
John Stultze5c5bb32018-03-20 18:16:49 -0700271 fb_paddr(NULL),
272 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530273
274 {
275 version = sizeof(native_handle);
276 numFds = sNumFds;
277 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
278 }
279
280#endif
281
John Stultze5c5bb32018-03-20 18:16:49 -0700282 private_handle_t(int flags, int usage, int size, void *base, int lock_state, int fb_file, int fb_offset, void *fb_paddr):
Vishal Bhoj78e90492015-12-07 01:36:32 +0530283#if GRALLOC_ARM_DMA_BUF_MODULE
284 share_fd(-1),
285#endif
286 magic(sMagic),
287 flags(flags),
288 usage(usage),
289 size(size),
290 width(0),
291 height(0),
292 format(0),
293 stride(0),
294 base(base),
295 lockState(lock_state),
296 writeOwner(0),
297 pid(getpid()),
298 yuv_info(MALI_YUV_NO_INFO),
299#if GRALLOC_ARM_UMP_MODULE
300 ump_id((int)UMP_INVALID_SECURE_ID),
301 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
302#endif
John Stultze5c5bb32018-03-20 18:16:49 -0700303 fd(fb_file),
304 offset(fb_offset),
305 fb_paddr(fb_paddr)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530306#if GRALLOC_ARM_DMA_BUF_MODULE
307 ,
John Stultze5c5bb32018-03-20 18:16:49 -0700308 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530309#endif
310
311 {
312 version = sizeof(native_handle);
313 numFds = sNumFds;
314 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
315 }
316
317 ~private_handle_t()
318 {
319 magic = 0;
320 }
321
322 bool usesPhysicallyContiguousMemory()
323 {
324 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
325 }
326
327 static int validate(const native_handle *h)
328 {
329 const private_handle_t *hnd = (const private_handle_t *)h;
330
John Stultze5c5bb32018-03-20 18:16:49 -0700331 if (!hnd || hnd->version != sizeof(native_handle) || hnd->magic != sMagic)
Chia-I Wua12e37d2017-05-08 12:46:13 -0700332 {
333 return -EINVAL;
334 }
335
336 int numFds = sNumFds;
337 int numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
John Stultze5c5bb32018-03-20 18:16:49 -0700338
Chia-I Wua12e37d2017-05-08 12:46:13 -0700339#if GRALLOC_ARM_DMA_BUF_MODULE
John Stultze5c5bb32018-03-20 18:16:49 -0700340 if (hnd->share_fd < 0)
341 {
Chia-I Wua12e37d2017-05-08 12:46:13 -0700342 numFds--;
343 numInts++;
344 }
345#endif
346
John Stultze5c5bb32018-03-20 18:16:49 -0700347 if (hnd->numFds != numFds || hnd->numInts != numInts)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530348 {
349 return -EINVAL;
350 }
351
352 return 0;
353 }
354
355 static private_handle_t *dynamicCast(const native_handle *in)
356 {
357 if (validate(in) == 0)
358 {
359 return (private_handle_t *) in;
360 }
361
362 return NULL;
363 }
364#endif
365};
366
367#endif /* GRALLOC_PRIV_H_ */