blob: 7c09a3f3112ba36e386b10bca22ca9eedacb8624
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 | #include <sys/mman.h> |
29 | |
30 | #include <utils/Log.h> |
31 | |
32 | #include <hardware/gralloc.h> |
33 | #include <cutils/native_handle.h> |
34 | #include "alloc_device.h" |
35 | #include "framebuffer.h" |
36 | #include <utils/Log.h> |
37 | |
38 | #include "format_chooser.h" |
39 | |
40 | #define HAL_PIXEL_FORMAT_YCbCr_420_SP 0x100 |
41 | //#define MALI_ION 1 |
42 | |
43 | #define GRALLOC_ARM_UMP_MODULE 0 |
44 | #define GRALLOC_ARM_DMA_BUF_MODULE 1 |
45 | #if MALI_AFBC_GRALLOC |
46 | #define MALI_AFBC_GRALLOC 1 |
47 | #endif |
48 | #if !defined(GRALLOC_OLD_ION_API) |
49 | /* new libion */ |
50 | typedef int ion_user_handle_t; |
51 | #define ION_INVALID_HANDLE 0 |
52 | #else |
53 | typedef struct ion_handle *ion_user_handle_t; |
54 | #define ION_INVALID_HANDLE NULL |
55 | #endif /* GRALLOC_OLD_ION_API */ |
56 | |
57 | /* NOTE: |
58 | * If your framebuffer device driver is integrated with UMP, you will have to |
59 | * change this IOCTL definition to reflect your integration with the framebuffer |
60 | * device. |
61 | * Expected return value is a UMP secure id backing your framebuffer device memory. |
62 | */ |
63 | #if GRALLOC_ARM_UMP_MODULE |
64 | #define IOCTL_GET_FB_UMP_SECURE_ID _IOWR('m', 0xF8, __u32) |
65 | #endif |
66 | |
67 | /* NOTE: |
68 | * If your framebuffer device driver is integrated with dma_buf, you will have to |
69 | * change this IOCTL definition to reflect your integration with the framebuffer |
70 | * device. |
71 | * Expected return value is a structure filled with a file descriptor |
72 | * backing your framebuffer device memory. |
73 | */ |
74 | #if GRALLOC_ARM_DMA_BUF_MODULE |
75 | struct fb_dmabuf_export |
76 | { |
77 | __u32 buffer_idx; |
78 | __u32 fd; |
79 | __u32 flags; |
80 | }; |
81 | #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export) |
82 | #define FBIOGET_OSD_DMABUF 0x46fc |
83 | #endif |
84 | |
85 | /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0 |
86 | * 8 is big enough for "gpu0" & "fb0" currently |
87 | */ |
88 | #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8 |
89 | |
90 | /* Define number of shared file descriptors */ |
91 | #if MALI_AFBC_GRALLOC == 1 && GRALLOC_ARM_DMA_BUF_MODULE |
92 | #define GRALLOC_ARM_NUM_FDS 2 |
93 | #elif MALI_AFBC_GRALLOC == 1 || GRALLOC_ARM_DMA_BUF_MODULE |
94 | #define GRALLOC_ARM_NUM_FDS 1 |
95 | #else |
96 | #define GRALLOC_ARM_NUM_FDS 0 |
97 | #endif |
98 | |
99 | |
100 | #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds) |
101 | |
102 | // This value is platform specific and should be set according to hardware YUV planes restrictions. |
103 | // Please note that EGL winsys platform config file needs to use the same value when importing buffers. |
104 | #define YUV_MALI_PLANE_ALIGN 32 |
105 | |
106 | // Default YUV stride aligment in Android |
107 | #define YUV_ANDROID_PLANE_ALIGN 16 |
108 | |
109 | |
110 | #if GRALLOC_ARM_UMP_MODULE |
111 | #include <ump/ump.h> |
112 | #endif |
113 | |
114 | #define SZ_4K 0x00001000 |
115 | #define SZ_2M 0x00200000 |
116 | |
117 | typedef enum |
118 | { |
119 | MALI_YUV_NO_INFO, |
120 | MALI_YUV_BT601_NARROW, |
121 | MALI_YUV_BT601_WIDE, |
122 | MALI_YUV_BT709_NARROW, |
123 | MALI_YUV_BT709_WIDE |
124 | } mali_gralloc_yuv_info; |
125 | |
126 | typedef enum |
127 | { |
128 | MALI_DPY_TYPE_UNKNOWN = 0, |
129 | MALI_DPY_TYPE_CLCD, |
130 | MALI_DPY_TYPE_HDLCD |
131 | } mali_dpy_type; |
132 | |
133 | struct private_handle_t; |
134 | |
135 | //used by gralloc mode to only. |
136 | struct framebuffer_mapper_t{ |
137 | private_handle_t* framebuffer; |
138 | framebuffer_info_t fb_info; |
139 | uint32_t numBuffers; |
140 | uint32_t bufferMask; |
141 | uint32_t bufferSize; |
142 | }; |
143 | |
144 | //TODO: NEED CHANGE NAME TO BE MORE READABLE!! |
145 | struct framebuffer_t{ |
146 | struct framebuffer_device_t base; |
147 | struct framebuffer_info_t fb_info; |
148 | struct private_handle_t* fb_hnd; |
149 | }; |
150 | |
151 | struct private_module_t |
152 | { |
153 | gralloc_module_t base; |
154 | |
155 | framebuffer_mapper_t fb_primary; |
156 | framebuffer_mapper_t fb_external; |
157 | pthread_mutex_t lock; |
158 | int ion_client; |
159 | mali_dpy_type dpy_type; |
160 | |
161 | int swapInterval; |
162 | |
163 | enum |
164 | { |
165 | // flag to indicate we'll post this buffer |
166 | PRIV_USAGE_LOCKED_FOR_POST = 0x80000000 |
167 | }; |
168 | |
169 | #ifdef __cplusplus |
170 | /* default constructor */ |
171 | private_module_t(); |
172 | #endif |
173 | }; |
174 | |
175 | |
176 | /* |
177 | ATTENTATION: don't add member in this struct, it is used by other modules. |
178 | */ |
179 | #ifdef __cplusplus |
180 | struct private_handle_t : public native_handle |
181 | { |
182 | #else |
183 | struct private_handle_t |
184 | { |
185 | struct native_handle nativeHandle; |
186 | #endif |
187 | |
188 | enum |
189 | { |
190 | PRIV_FLAGS_FRAMEBUFFER = 0x00000001, |
191 | PRIV_FLAGS_USES_UMP = 0x00000002, |
192 | PRIV_FLAGS_USES_ION = 0x00000004, |
193 | PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008, |
194 | PRIV_FLAGS_VIDEO_OVERLAY = 0x00000010, |
195 | PRIV_FLAGS_VIDEO_OMX = 0x00000020, |
196 | PRIV_FLAGS_CURSOR = 0x00000040, |
197 | PRIV_FLAGS_OSD_VIDEO_OMX = 0x00000080, |
198 | PRIV_FLAGS_CONTINUOUS_BUF = 0x00000100, |
199 | PRIV_FLAGS_SECURE_PROTECTED = 0x00000200, |
200 | }; |
201 | |
202 | enum |
203 | { |
204 | LOCK_STATE_WRITE = 1<<31, |
205 | LOCK_STATE_MAPPED = 1<<30, |
206 | LOCK_STATE_READ_MASK = 0x3FFFFFFF |
207 | }; |
208 | |
209 | #if GRALLOC_ARM_DMA_BUF_MODULE |
210 | /* |
211 | * Shared file descriptor for dma_buf sharing. This must be the first element in the |
212 | * structure so that binder knows where it is and can properly share it between |
213 | * processes. |
214 | * DO NOT MOVE THIS ELEMENT! |
215 | */ |
216 | int share_fd; |
217 | #endif |
218 | |
219 | int share_attr_fd; |
220 | |
221 | #if GRALLOC_ARM_DMA_BUF_MODULE |
222 | ion_user_handle_t ion_hnd; |
223 | #endif |
224 | |
225 | // ints |
226 | int magic; |
227 | int req_format; |
228 | uint64_t internal_format; |
229 | int byte_stride; |
230 | int flags; |
231 | int usage; |
232 | int size; |
233 | int width; |
234 | int height; |
235 | int format; |
236 | int internalWidth; |
237 | int internalHeight; |
238 | int stride; |
239 | union { |
240 | void* base; |
241 | uint64_t padding; |
242 | }; |
243 | int lockState; |
244 | int writeOwner; |
245 | int pid; |
246 | |
247 | // locally mapped shared attribute area |
248 | union { |
249 | void* attr_base; |
250 | uint64_t padding3; |
251 | }; |
252 | |
253 | mali_gralloc_yuv_info yuv_info; |
254 | |
255 | // Following members is for framebuffer only |
256 | int fd; //to mmap osd memory |
257 | //current buffer offset from framebuffer base |
258 | union { |
259 | off_t offset; |
260 | uint64_t padding4; |
261 | }; |
262 | |
263 | // Following members are for UMP memory only |
264 | #if GRALLOC_ARM_UMP_MODULE |
265 | ump_secure_id ump_id; |
266 | union { |
267 | void* ump_mem_handle; |
268 | uint64_t padding5; |
269 | }; |
270 | #endif |
271 | uint64_t padding_1; |
272 | uint64_t padding_2; |
273 | uint64_t padding_3; |
274 | int padding_4; |
275 | /* |
276 | * min_pgsz denotes minimum phys_page size used by this buffer. |
277 | * if buffer memory is physical contiguous set min_pgsz to buff->size |
278 | * if not sure buff's real phys_page size, you can use SZ_4K for safe. |
279 | */ |
280 | int min_pgsz; |
281 | |
282 | #ifdef __cplusplus |
283 | /* |
284 | * We track the number of integers in the structure. There are 16 unconditional |
285 | * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is |
286 | * considered an int not an fd because it is not intended to be used outside the |
287 | * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the |
288 | * number of integers that are conditionally included. Similar considerations apply |
289 | * to the number of fds. |
290 | */ |
291 | static const int sNumFds = GRALLOC_ARM_NUM_FDS; |
292 | static const int sMagic = 0x3141592; |
293 | |
294 | #if GRALLOC_ARM_UMP_MODULE |
295 | private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle): |
296 | share_attr_fd(-1), |
297 | magic(sMagic), |
298 | flags(flags), |
299 | usage(usage), |
300 | size(size), |
301 | width(0), |
302 | height(0), |
303 | format(0), |
304 | stride(0), |
305 | base(base), |
306 | lockState(lock_state), |
307 | writeOwner(0), |
308 | pid(getpid()), |
309 | attr_base(MAP_FAILED), |
310 | yuv_info(MALI_YUV_NO_INFO), |
311 | fd(0), |
312 | offset(0), |
313 | ump_id(secure_id), |
314 | ump_mem_handle(handle) |
315 | |
316 | { |
317 | version = sizeof(native_handle); |
318 | numFds = sNumFds; |
319 | numInts = NUM_INTS_IN_PRIVATE_HANDLE; |
320 | } |
321 | #endif |
322 | |
323 | private_handle_t(int flags, int usage, int size, void *base, int lock_state): |
324 | #if GRALLOC_ARM_DMA_BUF_MODULE |
325 | share_fd(-1), |
326 | #endif |
327 | share_attr_fd(-1), |
328 | #if GRALLOC_ARM_DMA_BUF_MODULE |
329 | ion_hnd(ION_INVALID_HANDLE), |
330 | #endif |
331 | magic(sMagic), |
332 | flags(flags), |
333 | usage(usage), |
334 | size(size), |
335 | width(0), |
336 | height(0), |
337 | format(0), |
338 | stride(0), |
339 | base(base), |
340 | lockState(lock_state), |
341 | writeOwner(0), |
342 | pid(getpid()), |
343 | attr_base(MAP_FAILED), |
344 | yuv_info(MALI_YUV_NO_INFO), |
345 | fd(0), |
346 | offset(0) |
347 | #if GRALLOC_ARM_UMP_MODULE |
348 | ,ump_id(UMP_INVALID_SECURE_ID), |
349 | ump_mem_handle(UMP_INVALID_MEMORY_HANDLE) |
350 | #endif |
351 | |
352 | |
353 | { |
354 | version = sizeof(native_handle); |
355 | numFds = sNumFds; |
356 | numInts = NUM_INTS_IN_PRIVATE_HANDLE; |
357 | } |
358 | |
359 | private_handle_t(int flags, int usage, int size, void *base, int lock_state, int fb_file, off_t fb_offset, int unused): |
360 | #if GRALLOC_ARM_DMA_BUF_MODULE |
361 | share_fd(-1), |
362 | #endif |
363 | share_attr_fd(-1), |
364 | #if GRALLOC_ARM_DMA_BUF_MODULE |
365 | ion_hnd(ION_INVALID_HANDLE), |
366 | #endif |
367 | magic(sMagic), |
368 | flags(flags), |
369 | usage(usage), |
370 | size(size), |
371 | width(0), |
372 | height(0), |
373 | format(0), |
374 | stride(0), |
375 | base(base), |
376 | lockState(lock_state), |
377 | writeOwner(0), |
378 | pid(getpid()), |
379 | attr_base(MAP_FAILED), |
380 | yuv_info(MALI_YUV_NO_INFO), |
381 | fd(fb_file), |
382 | offset(fb_offset) |
383 | |
384 | #if GRALLOC_ARM_UMP_MODULE |
385 | ,ump_id(UMP_INVALID_SECURE_ID), |
386 | ump_mem_handle(UMP_INVALID_MEMORY_HANDLE) |
387 | #endif |
388 | |
389 | { |
390 | version = sizeof(native_handle); |
391 | numFds = sNumFds; |
392 | numInts = NUM_INTS_IN_PRIVATE_HANDLE; |
393 | /* |
394 | * because -Werror option, need to remove this warning; |
395 | */ |
396 | (void)unused; |
397 | } |
398 | |
399 | ~private_handle_t() |
400 | { |
401 | magic = 0; |
402 | } |
403 | |
404 | bool usesPhysicallyContiguousMemory() |
405 | { |
406 | return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false; |
407 | } |
408 | |
409 | static int validate(const native_handle* h) |
410 | { |
411 | const private_handle_t* hnd = (const private_handle_t*)h; |
412 | if (!h || |
413 | h->version != sizeof(native_handle) || |
414 | h->numInts != NUM_INTS_IN_PRIVATE_HANDLE || |
415 | h->numFds != sNumFds || |
416 | hnd->magic != sMagic) |
417 | { |
418 | return -EINVAL; |
419 | } |
420 | return 0; |
421 | } |
422 | |
423 | static private_handle_t* dynamicCast(const native_handle* in) |
424 | { |
425 | if (validate(in) == 0) |
426 | { |
427 | return (private_handle_t*) in; |
428 | } |
429 | return NULL; |
430 | } |
431 | #endif |
432 | }; |
433 | |
434 | #endif /* GRALLOC_PRIV_H_ */ |
435 |