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 <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
58 struct 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
66 typedef int ion_user_handle_t;
67 #define ION_INVALID_HANDLE 0
68 #else
69 
70 typedef 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
91 typedef 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 
100 struct private_handle_t;
101 
102 struct 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;
113 
114 	struct fb_var_screeninfo info;
115 	struct fb_fix_screeninfo finfo;
116 	float xdpi;
117 	float ydpi;
118 	float fps;
119 
120 	enum
121 	{
122 		// flag to indicate we'll post this buffer
123 		PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
124 	};
125 
126 	/* default constructor */
127 	private_module_t();
128 };
129 
130 #ifdef __cplusplus
131 struct private_handle_t : public native_handle
132 {
133 #else
134 struct private_handle_t
135 {
136 	struct native_handle nativeHandle;
137 #endif
138 
139 	enum
140 	{
141 		PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
142 		PRIV_FLAGS_USES_UMP    = 0x00000002,
143 		PRIV_FLAGS_USES_ION    = 0x00000004,
144 	};
145 
146 	enum
147 	{
148 		LOCK_STATE_WRITE     =   1 << 31,
149 		LOCK_STATE_MAPPED    =   1 << 30,
150 		LOCK_STATE_UNREGISTERED  =   1 << 29,
151 		LOCK_STATE_READ_MASK =   0x1FFFFFFF
152 	};
153 
154 	// ints
155 #if GRALLOC_ARM_DMA_BUF_MODULE
156 	/*shared file descriptor for dma_buf sharing*/
157 	int     share_fd;
158 #endif
159 	int     magic;
160 	int     flags;
161 	int     usage;
162 	int     size;
163 	int     width;
164 	int     height;
165 	int     format;
166 	int     stride;
167 	union
168 	{
169 		void   *base;
170 		uint64_t padding;
171 	};
172 	int     lockState;
173 	int     writeOwner;
174 	int     pid;
175 
176 	mali_gralloc_yuv_info yuv_info;
177 
178 	// Following members are for UMP memory only
179 #if GRALLOC_ARM_UMP_MODULE
180 	int     ump_id;
181 	int     ump_mem_handle;
182 #endif
183 
184 	// Following members is for framebuffer only
185 	int     fd;
186 	int     offset;
187 
188 #if GRALLOC_ARM_DMA_BUF_MODULE
189 	ion_user_handle_t ion_hnd;
190 #endif
191 
192 #if GRALLOC_ARM_DMA_BUF_MODULE
193 #define GRALLOC_ARM_NUM_FDS 1
194 #else
195 #define GRALLOC_ARM_NUM_FDS 0
196 #endif
197 
198 #ifdef __cplusplus
199 	static const int sNumFds = GRALLOC_ARM_NUM_FDS;
200 	static const int sMagic = 0x3141592;
201 
202 #if GRALLOC_ARM_UMP_MODULE
private_handle_tprivate_handle_t203 	private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle):
204 #if GRALLOC_ARM_DMA_BUF_MODULE
205 		share_fd(-1),
206 #endif
207 		magic(sMagic),
208 		flags(flags),
209 		usage(usage),
210 		size(size),
211 		width(0),
212 		height(0),
213 		format(0),
214 		stride(0),
215 		base(base),
216 		lockState(lock_state),
217 		writeOwner(0),
218 		pid(getpid()),
219 		yuv_info(MALI_YUV_NO_INFO),
220 		ump_id((int)secure_id),
221 		ump_mem_handle((int)handle),
222 		fd(0),
223 		offset(0)
224 #if GRALLOC_ARM_DMA_BUF_MODULE
225 		,
226 		ion_hnd(ION_INVALID_HANDLE)
227 #endif
228 
229 	{
230 		version = sizeof(native_handle);
231 		numFds = sNumFds;
232 		numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
233 	}
234 #endif
235 
236 #if GRALLOC_ARM_DMA_BUF_MODULE
private_handle_tprivate_handle_t237 	private_handle_t(int flags, int usage, int size, void *base, int lock_state):
238 		share_fd(-1),
239 		magic(sMagic),
240 		flags(flags),
241 		usage(usage),
242 		size(size),
243 		width(0),
244 		height(0),
245 		format(0),
246 		stride(0),
247 		base(base),
248 		lockState(lock_state),
249 		writeOwner(0),
250 		pid(getpid()),
251 		yuv_info(MALI_YUV_NO_INFO),
252 #if GRALLOC_ARM_UMP_MODULE
253 		ump_id((int)UMP_INVALID_SECURE_ID),
254 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
255 #endif
256 		fd(0),
257 		offset(0),
258 		ion_hnd(ION_INVALID_HANDLE)
259 
260 	{
261 		version = sizeof(native_handle);
262 		numFds = sNumFds;
263 		numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
264 	}
265 
266 #endif
267 
private_handle_tprivate_handle_t268 	private_handle_t(int flags, int usage, int size, void *base, int lock_state, int fb_file, int fb_offset):
269 #if GRALLOC_ARM_DMA_BUF_MODULE
270 		share_fd(-1),
271 #endif
272 		magic(sMagic),
273 		flags(flags),
274 		usage(usage),
275 		size(size),
276 		width(0),
277 		height(0),
278 		format(0),
279 		stride(0),
280 		base(base),
281 		lockState(lock_state),
282 		writeOwner(0),
283 		pid(getpid()),
284 		yuv_info(MALI_YUV_NO_INFO),
285 #if GRALLOC_ARM_UMP_MODULE
286 		ump_id((int)UMP_INVALID_SECURE_ID),
287 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
288 #endif
289 		fd(fb_file),
290 		offset(fb_offset)
291 #if GRALLOC_ARM_DMA_BUF_MODULE
292 		,
293 		ion_hnd(ION_INVALID_HANDLE)
294 #endif
295 
296 	{
297 		version = sizeof(native_handle);
298 		numFds = sNumFds;
299 		numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
300 	}
301 
~private_handle_tprivate_handle_t302 	~private_handle_t()
303 	{
304 		magic = 0;
305 	}
306 
usesPhysicallyContiguousMemoryprivate_handle_t307 	bool usesPhysicallyContiguousMemory()
308 	{
309 		return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
310 	}
311 
validateprivate_handle_t312 	static int validate(const native_handle *h)
313 	{
314 		const private_handle_t *hnd = (const private_handle_t *)h;
315 
316 		if (!h || h->version != sizeof(native_handle) || h->numFds != sNumFds ||
317 		        h->numInts != (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds ||
318 		        hnd->magic != sMagic)
319 		{
320 			return -EINVAL;
321 		}
322 
323 		return 0;
324 	}
325 
dynamicCastprivate_handle_t326 	static private_handle_t *dynamicCast(const native_handle *in)
327 	{
328 		if (validate(in) == 0)
329 		{
330 			return (private_handle_t *) in;
331 		}
332 
333 		return NULL;
334 	}
335 #endif
336 };
337 
338 #endif /* GRALLOC_PRIV_H_ */
339