blob: a5f8c8b0d129326e0c84d4f31b864afec6c4d365 [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;
116 bool gralloc_legacy_ion;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530117
118 struct fb_var_screeninfo info;
119 struct fb_fix_screeninfo finfo;
120 float xdpi;
121 float ydpi;
122 float fps;
123
124 enum
125 {
126 // flag to indicate we'll post this buffer
127 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
128 };
129
130 /* default constructor */
131 private_module_t();
132};
133
134#ifdef __cplusplus
135struct private_handle_t : public native_handle
136{
137#else
138struct private_handle_t
139{
140 struct native_handle nativeHandle;
141#endif
142
143 enum
144 {
145 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
146 PRIV_FLAGS_USES_UMP = 0x00000002,
147 PRIV_FLAGS_USES_ION = 0x00000004,
148 };
149
150 enum
151 {
152 LOCK_STATE_WRITE = 1 << 31,
153 LOCK_STATE_MAPPED = 1 << 30,
John Stultz37287452016-01-20 20:08:46 -0800154 LOCK_STATE_UNREGISTERED = 1 << 29,
John Stultze5c5bb32018-03-20 18:16:49 -0700155 LOCK_STATE_READ_MASK = 0x3FFFFFFF
Vishal Bhoj78e90492015-12-07 01:36:32 +0530156 };
157
158 // ints
159#if GRALLOC_ARM_DMA_BUF_MODULE
160 /*shared file descriptor for dma_buf sharing*/
161 int share_fd;
162#endif
163 int magic;
164 int flags;
165 int usage;
166 int size;
167 int width;
168 int height;
John Stultz30941512018-01-04 14:03:55 -0800169 union {
170 int format;
171 int req_format; /* same name as gralloc960 */
172 };
Vishal Bhoj78e90492015-12-07 01:36:32 +0530173 int stride;
174 union
175 {
176 void *base;
177 uint64_t padding;
178 };
179 int lockState;
180 int writeOwner;
181 int pid;
182
183 mali_gralloc_yuv_info yuv_info;
184
185 // Following members are for UMP memory only
186#if GRALLOC_ARM_UMP_MODULE
187 int ump_id;
188 int ump_mem_handle;
189#endif
190
191 // Following members is for framebuffer only
John Stultze5c5bb32018-03-20 18:16:49 -0700192 int fd; //Shallow copy, DO NOT duplicate
Vishal Bhoj78e90492015-12-07 01:36:32 +0530193 int offset;
John Stultze5c5bb32018-03-20 18:16:49 -0700194 union
195 {
196 void *fb_paddr;
197 uint64_t fb_paddr_padding;
198 };
John Stultz30941512018-01-04 14:03:55 -0800199 int byte_stride;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530200#if GRALLOC_ARM_DMA_BUF_MODULE
John Stultze5c5bb32018-03-20 18:16:49 -0700201 ion_user_handle_t ion_hnd;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530202#endif
Vishal Bhoj78e90492015-12-07 01:36:32 +0530203#if GRALLOC_ARM_DMA_BUF_MODULE
204#define GRALLOC_ARM_NUM_FDS 1
205#else
206#define GRALLOC_ARM_NUM_FDS 0
207#endif
208
209#ifdef __cplusplus
210 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
211 static const int sMagic = 0x3141592;
212
213#if GRALLOC_ARM_UMP_MODULE
214 private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle):
215#if GRALLOC_ARM_DMA_BUF_MODULE
216 share_fd(-1),
217#endif
218 magic(sMagic),
219 flags(flags),
220 usage(usage),
221 size(size),
222 width(0),
223 height(0),
224 format(0),
225 stride(0),
226 base(base),
227 lockState(lock_state),
228 writeOwner(0),
229 pid(getpid()),
230 yuv_info(MALI_YUV_NO_INFO),
231 ump_id((int)secure_id),
232 ump_mem_handle((int)handle),
John Stultze5c5bb32018-03-20 18:16:49 -0700233 fd(0),
234 offset(0),
235 fb_paddr(NULL)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530236#if GRALLOC_ARM_DMA_BUF_MODULE
237 ,
John Stultze5c5bb32018-03-20 18:16:49 -0700238 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530239#endif
240
241 {
242 version = sizeof(native_handle);
243 numFds = sNumFds;
244 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
245 }
246#endif
247
248#if GRALLOC_ARM_DMA_BUF_MODULE
249 private_handle_t(int flags, int usage, int size, void *base, int lock_state):
250 share_fd(-1),
251 magic(sMagic),
252 flags(flags),
253 usage(usage),
254 size(size),
255 width(0),
256 height(0),
257 format(0),
258 stride(0),
259 base(base),
260 lockState(lock_state),
261 writeOwner(0),
262 pid(getpid()),
263 yuv_info(MALI_YUV_NO_INFO),
264#if GRALLOC_ARM_UMP_MODULE
265 ump_id((int)UMP_INVALID_SECURE_ID),
266 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
267#endif
John Stultze5c5bb32018-03-20 18:16:49 -0700268 fd(0),
Vishal Bhoj78e90492015-12-07 01:36:32 +0530269 offset(0),
John Stultze5c5bb32018-03-20 18:16:49 -0700270 fb_paddr(NULL),
271 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530272
273 {
274 version = sizeof(native_handle);
275 numFds = sNumFds;
276 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
277 }
278
279#endif
280
John Stultze5c5bb32018-03-20 18:16:49 -0700281 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 +0530282#if GRALLOC_ARM_DMA_BUF_MODULE
283 share_fd(-1),
284#endif
285 magic(sMagic),
286 flags(flags),
287 usage(usage),
288 size(size),
289 width(0),
290 height(0),
291 format(0),
292 stride(0),
293 base(base),
294 lockState(lock_state),
295 writeOwner(0),
296 pid(getpid()),
297 yuv_info(MALI_YUV_NO_INFO),
298#if GRALLOC_ARM_UMP_MODULE
299 ump_id((int)UMP_INVALID_SECURE_ID),
300 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
301#endif
John Stultze5c5bb32018-03-20 18:16:49 -0700302 fd(fb_file),
303 offset(fb_offset),
304 fb_paddr(fb_paddr)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530305#if GRALLOC_ARM_DMA_BUF_MODULE
306 ,
John Stultze5c5bb32018-03-20 18:16:49 -0700307 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530308#endif
309
310 {
311 version = sizeof(native_handle);
312 numFds = sNumFds;
313 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
314 }
315
316 ~private_handle_t()
317 {
318 magic = 0;
319 }
320
321 bool usesPhysicallyContiguousMemory()
322 {
323 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
324 }
325
326 static int validate(const native_handle *h)
327 {
328 const private_handle_t *hnd = (const private_handle_t *)h;
329
John Stultze5c5bb32018-03-20 18:16:49 -0700330 if (!hnd || hnd->version != sizeof(native_handle) || hnd->magic != sMagic)
Chia-I Wua12e37d2017-05-08 12:46:13 -0700331 {
332 return -EINVAL;
333 }
334
335 int numFds = sNumFds;
336 int numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
John Stultze5c5bb32018-03-20 18:16:49 -0700337
Chia-I Wua12e37d2017-05-08 12:46:13 -0700338#if GRALLOC_ARM_DMA_BUF_MODULE
John Stultze5c5bb32018-03-20 18:16:49 -0700339 if (hnd->share_fd < 0)
340 {
Chia-I Wua12e37d2017-05-08 12:46:13 -0700341 numFds--;
342 numInts++;
343 }
344#endif
345
John Stultze5c5bb32018-03-20 18:16:49 -0700346 if (hnd->numFds != numFds || hnd->numInts != numInts)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530347 {
348 return -EINVAL;
349 }
350
351 return 0;
352 }
353
354 static private_handle_t *dynamicCast(const native_handle *in)
355 {
356 if (validate(in) == 0)
357 {
358 return (private_handle_t *) in;
359 }
360
361 return NULL;
362 }
363#endif
364};
365
366#endif /* GRALLOC_PRIV_H_ */