blob: 14cb07a886077e9bd3a88a16b822d99112c66479 [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 */
John Stultz91d82132019-02-28 10:00:25 -0800173 /*unused but referenced in drm_hwc */
174 int internal_format;
John Stultz30941512018-01-04 14:03:55 -0800175 };
Vishal Bhoj78e90492015-12-07 01:36:32 +0530176 int stride;
177 union
178 {
179 void *base;
180 uint64_t padding;
181 };
182 int lockState;
183 int writeOwner;
184 int pid;
185
186 mali_gralloc_yuv_info yuv_info;
187
188 // Following members are for UMP memory only
189#if GRALLOC_ARM_UMP_MODULE
190 int ump_id;
191 int ump_mem_handle;
192#endif
193
194 // Following members is for framebuffer only
John Stultze5c5bb32018-03-20 18:16:49 -0700195 int fd; //Shallow copy, DO NOT duplicate
Vishal Bhoj78e90492015-12-07 01:36:32 +0530196 int offset;
John Stultze5c5bb32018-03-20 18:16:49 -0700197 union
198 {
199 void *fb_paddr;
200 uint64_t fb_paddr_padding;
201 };
John Stultz30941512018-01-04 14:03:55 -0800202 int byte_stride;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530203#if GRALLOC_ARM_DMA_BUF_MODULE
John Stultze5c5bb32018-03-20 18:16:49 -0700204 ion_user_handle_t ion_hnd;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530205#endif
Vishal Bhoj78e90492015-12-07 01:36:32 +0530206#if GRALLOC_ARM_DMA_BUF_MODULE
207#define GRALLOC_ARM_NUM_FDS 1
208#else
209#define GRALLOC_ARM_NUM_FDS 0
210#endif
211
212#ifdef __cplusplus
213 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
214 static const int sMagic = 0x3141592;
215
216#if GRALLOC_ARM_UMP_MODULE
217 private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle):
218#if GRALLOC_ARM_DMA_BUF_MODULE
219 share_fd(-1),
220#endif
221 magic(sMagic),
222 flags(flags),
223 usage(usage),
224 size(size),
225 width(0),
226 height(0),
227 format(0),
228 stride(0),
229 base(base),
230 lockState(lock_state),
231 writeOwner(0),
232 pid(getpid()),
233 yuv_info(MALI_YUV_NO_INFO),
234 ump_id((int)secure_id),
235 ump_mem_handle((int)handle),
John Stultze5c5bb32018-03-20 18:16:49 -0700236 fd(0),
237 offset(0),
238 fb_paddr(NULL)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530239#if GRALLOC_ARM_DMA_BUF_MODULE
240 ,
John Stultze5c5bb32018-03-20 18:16:49 -0700241 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530242#endif
243
244 {
245 version = sizeof(native_handle);
246 numFds = sNumFds;
247 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
248 }
249#endif
250
251#if GRALLOC_ARM_DMA_BUF_MODULE
252 private_handle_t(int flags, int usage, int size, void *base, int lock_state):
253 share_fd(-1),
254 magic(sMagic),
255 flags(flags),
256 usage(usage),
257 size(size),
258 width(0),
259 height(0),
260 format(0),
261 stride(0),
262 base(base),
263 lockState(lock_state),
264 writeOwner(0),
265 pid(getpid()),
266 yuv_info(MALI_YUV_NO_INFO),
267#if GRALLOC_ARM_UMP_MODULE
268 ump_id((int)UMP_INVALID_SECURE_ID),
269 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
270#endif
John Stultze5c5bb32018-03-20 18:16:49 -0700271 fd(0),
Vishal Bhoj78e90492015-12-07 01:36:32 +0530272 offset(0),
John Stultze5c5bb32018-03-20 18:16:49 -0700273 fb_paddr(NULL),
274 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530275
276 {
277 version = sizeof(native_handle);
278 numFds = sNumFds;
279 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
280 }
281
282#endif
283
John Stultze5c5bb32018-03-20 18:16:49 -0700284 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 +0530285#if GRALLOC_ARM_DMA_BUF_MODULE
286 share_fd(-1),
287#endif
288 magic(sMagic),
289 flags(flags),
290 usage(usage),
291 size(size),
292 width(0),
293 height(0),
294 format(0),
295 stride(0),
296 base(base),
297 lockState(lock_state),
298 writeOwner(0),
299 pid(getpid()),
300 yuv_info(MALI_YUV_NO_INFO),
301#if GRALLOC_ARM_UMP_MODULE
302 ump_id((int)UMP_INVALID_SECURE_ID),
303 ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
304#endif
John Stultze5c5bb32018-03-20 18:16:49 -0700305 fd(fb_file),
306 offset(fb_offset),
307 fb_paddr(fb_paddr)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530308#if GRALLOC_ARM_DMA_BUF_MODULE
309 ,
John Stultze5c5bb32018-03-20 18:16:49 -0700310 ion_hnd(ION_INVALID_HANDLE)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530311#endif
312
313 {
314 version = sizeof(native_handle);
315 numFds = sNumFds;
316 numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
317 }
318
319 ~private_handle_t()
320 {
321 magic = 0;
322 }
323
324 bool usesPhysicallyContiguousMemory()
325 {
326 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
327 }
328
329 static int validate(const native_handle *h)
330 {
331 const private_handle_t *hnd = (const private_handle_t *)h;
332
John Stultze5c5bb32018-03-20 18:16:49 -0700333 if (!hnd || hnd->version != sizeof(native_handle) || hnd->magic != sMagic)
Chia-I Wua12e37d2017-05-08 12:46:13 -0700334 {
335 return -EINVAL;
336 }
337
338 int numFds = sNumFds;
339 int numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
John Stultze5c5bb32018-03-20 18:16:49 -0700340
Chia-I Wua12e37d2017-05-08 12:46:13 -0700341#if GRALLOC_ARM_DMA_BUF_MODULE
John Stultze5c5bb32018-03-20 18:16:49 -0700342 if (hnd->share_fd < 0)
343 {
Chia-I Wua12e37d2017-05-08 12:46:13 -0700344 numFds--;
345 numInts++;
346 }
347#endif
348
John Stultze5c5bb32018-03-20 18:16:49 -0700349 if (hnd->numFds != numFds || hnd->numInts != numInts)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530350 {
351 return -EINVAL;
352 }
353
354 return 0;
355 }
356
357 static private_handle_t *dynamicCast(const native_handle *in)
358 {
359 if (validate(in) == 0)
360 {
361 return (private_handle_t *) in;
362 }
363
364 return NULL;
365 }
366#endif
367};
368
369#endif /* GRALLOC_PRIV_H_ */