blob: 7c4b8359100d740bdb52f1992c32f400003cc182 [file] [log] [blame]
John Stultz16100f62017-05-03 11:12:18 -07001/*
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 <linux/ion.h>
27#include <sys/types.h>
28#include <unistd.h>
29#include <sys/mman.h>
30#include <hardware/gralloc.h>
31#include <cutils/native_handle.h>
32#include "alloc_device.h"
33#include <utils/Log.h>
34
35#include "mali_gralloc_formats.h"
36#include "gralloc_helper.h"
37
38
39/* NOTE:
40 * If your framebuffer device driver is integrated with dma_buf, you will have to
41 * change this IOCTL definition to reflect your integration with the framebuffer
42 * device.
43 * Expected return value is a structure filled with a file descriptor
44 * backing your framebuffer device memory.
45 */
46struct fb_dmabuf_export
47{
48 __u32 fd;
49 __u32 flags;
50};
51#define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)
52
53
54/* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
55 * 8 is big enough for "gpu0" & "fb0" currently
56 */
57#define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
58#define NUM_FB_BUFFERS 2
59
60/* Define number of shared file descriptors */
61#define GRALLOC_ARM_NUM_FDS 2
62
63#define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds)
64
65#define SZ_4K 0x00001000
66#define SZ_2M 0x00200000
67
68typedef enum
69{
70 MALI_YUV_NO_INFO,
71 MALI_YUV_BT601_NARROW,
72 MALI_YUV_BT601_WIDE,
73 MALI_YUV_BT709_NARROW,
74 MALI_YUV_BT709_WIDE
75} mali_gralloc_yuv_info;
76
77typedef enum
78{
79 MALI_DPY_TYPE_UNKNOWN = 0,
80 MALI_DPY_TYPE_CLCD,
81 MALI_DPY_TYPE_HDLCD
82} mali_dpy_type;
83
84struct private_handle_t;
85
86struct private_module_t
87{
88 gralloc_module_t base;
89
90 struct private_handle_t* framebuffer;
91 uint32_t flags;
92 uint32_t numBuffers;
93 uint32_t bufferMask;
94 pthread_mutex_t lock;
95 buffer_handle_t currentBuffer;
96 int ion_client;
97 mali_dpy_type dpy_type;
98
99 struct fb_var_screeninfo info;
100 struct fb_fix_screeninfo finfo;
101 float xdpi;
102 float ydpi;
103 float fps;
104 int swapInterval;
105
106#ifdef __cplusplus
107 /* Never intended to be used from C code */
108 enum
109 {
110 // flag to indicate we'll post this buffer
111 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
112 };
113#endif
114
115#ifdef __cplusplus
116 /* default constructor */
117 private_module_t();
118#endif
119};
120
121#ifndef __cplusplus
122/* C99 with pedantic don't allow anonymous unions which is used in below struct
123 * Disable pedantic for C for this struct only.
124 */
125#pragma GCC diagnostic push
126#pragma GCC diagnostic ignored "-Wpedantic"
127#endif
128
129#ifdef __cplusplus
130struct private_handle_t : public native_handle
131{
132#else
133struct private_handle_t
134{
135 struct native_handle nativeHandle;
136#endif
137
138#ifdef __cplusplus
139 /* Never intended to be used from C code */
140 enum
141 {
142 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
143 PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002,
144 PRIV_FLAGS_USES_ION = 0x00000004,
145 PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008
146 };
147
148 enum
149 {
150 LOCK_STATE_WRITE = 1<<31,
151 LOCK_STATE_MAPPED = 1<<30,
152 LOCK_STATE_READ_MASK = 0x3FFFFFFF
153 };
154#endif
155
156 /*
157 * Shared file descriptor for dma_buf sharing. This must be the first element in the
158 * structure so that binder knows where it is and can properly share it between
159 * processes.
160 * DO NOT MOVE THIS ELEMENT!
161 */
162 int share_fd;
163 int share_attr_fd;
164
Chia-I Wua3db1072017-05-08 12:55:28 -0700165 ion_user_handle_t ion_hnd_UNUSED;
John Stultz16100f62017-05-03 11:12:18 -0700166
167 // ints
168 int magic;
169 int req_format;
170 uint64_t internal_format;
171 int byte_stride;
172 int flags;
173 int usage;
174 int size;
175 int width;
176 int height;
177 int internalWidth;
178 int internalHeight;
179 int stride;
180 union {
181 void* base;
182 uint64_t padding;
183 };
184 int lockState;
185 int writeOwner;
Chia-I Wua3db1072017-05-08 12:55:28 -0700186 int pid_UNUSED;
John Stultz16100f62017-05-03 11:12:18 -0700187
188 // locally mapped shared attribute area
189 union {
190 void* attr_base;
191 uint64_t padding3;
192 };
193
194 mali_gralloc_yuv_info yuv_info;
195
196 // Following members is for framebuffer only
Chia-I Wu5f82a372017-05-08 12:50:36 -0700197 int shallow_fbdev_fd; // shallow copy, not dup'ed
John Stultz16100f62017-05-03 11:12:18 -0700198 union {
199 off_t offset;
200 uint64_t padding4;
201 };
202
203 /*
204 * min_pgsz denotes minimum phys_page size used by this buffer.
205 * if buffer memory is physical contiguous set min_pgsz to buff->size
206 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
207 */
208 int min_pgsz;
209#ifdef __cplusplus
210 /*
211 * We track the number of integers in the structure. There are 16 unconditional
212 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is
213 * considered an int not an fd because it is not intended to be used outside the
214 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
215 * number of integers that are conditionally included. Similar considerations apply
216 * to the number of fds.
217 */
218 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
219 static const int sMagic = 0x3141592;
220
221 private_handle_t(int _flags, int _usage, int _size, void *_base, int lock_state, int fb_file, off_t fb_offset):
222 share_fd(-1),
223 share_attr_fd(-1),
Chia-I Wua3db1072017-05-08 12:55:28 -0700224 ion_hnd_UNUSED(-1),
John Stultz16100f62017-05-03 11:12:18 -0700225 magic(sMagic),
226 flags(_flags),
227 usage(_usage),
228 size(_size),
229 width(0),
230 height(0),
231 stride(0),
232 base(_base),
233 lockState(lock_state),
234 writeOwner(0),
Chia-I Wua3db1072017-05-08 12:55:28 -0700235 pid_UNUSED(-1),
John Stultz16100f62017-05-03 11:12:18 -0700236 attr_base(MAP_FAILED),
237 yuv_info(MALI_YUV_NO_INFO),
Chia-I Wu5f82a372017-05-08 12:50:36 -0700238 shallow_fbdev_fd(fb_file),
John Stultz16100f62017-05-03 11:12:18 -0700239 offset(fb_offset)
240 {
241 version = sizeof(native_handle);
242 numFds = sNumFds;
243 numInts = NUM_INTS_IN_PRIVATE_HANDLE;
244 }
245
246 ~private_handle_t()
247 {
248 magic = 0;
249 }
250
251 bool usesPhysicallyContiguousMemory()
252 {
253 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
254 }
255
256 static int validate(const native_handle* h)
257 {
258 const private_handle_t* hnd = (const private_handle_t*)h;
259 if (!h ||
260 h->version != sizeof(native_handle) ||
261 h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
262 h->numFds != sNumFds ||
263 hnd->magic != sMagic)
264 {
265 return -EINVAL;
266 }
267 return 0;
268 }
269
270 static private_handle_t* dynamicCast(const native_handle* in)
271 {
272 if (validate(in) == 0)
273 {
274 return (private_handle_t*) in;
275 }
276 return NULL;
277 }
278#endif
279};
280#ifndef __cplusplus
281/* Restore previous diagnostic for pedantic */
282#pragma GCC diagnostic pop
283#endif
284
285#endif /* GRALLOC_PRIV_H_ */