blob: 606b36542272f00d137c5dfccfa1e56ce6cc1e3d [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;
Sumit Semwal4f9a6852017-12-07 23:00:49 +053097 int system_heap_id;
98 bool gralloc_legacy_ion;
John Stultz16100f62017-05-03 11:12:18 -070099 mali_dpy_type dpy_type;
100
101 struct fb_var_screeninfo info;
102 struct fb_fix_screeninfo finfo;
103 float xdpi;
104 float ydpi;
105 float fps;
106 int swapInterval;
107
108#ifdef __cplusplus
109 /* Never intended to be used from C code */
110 enum
111 {
112 // flag to indicate we'll post this buffer
113 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
114 };
115#endif
116
117#ifdef __cplusplus
118 /* default constructor */
119 private_module_t();
120#endif
121};
122
123#ifndef __cplusplus
124/* C99 with pedantic don't allow anonymous unions which is used in below struct
125 * Disable pedantic for C for this struct only.
126 */
127#pragma GCC diagnostic push
128#pragma GCC diagnostic ignored "-Wpedantic"
129#endif
130
131#ifdef __cplusplus
132struct private_handle_t : public native_handle
133{
134#else
135struct private_handle_t
136{
137 struct native_handle nativeHandle;
138#endif
139
140#ifdef __cplusplus
141 /* Never intended to be used from C code */
142 enum
143 {
144 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
145 PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002,
146 PRIV_FLAGS_USES_ION = 0x00000004,
147 PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008
148 };
149
150 enum
151 {
152 LOCK_STATE_WRITE = 1<<31,
153 LOCK_STATE_MAPPED = 1<<30,
154 LOCK_STATE_READ_MASK = 0x3FFFFFFF
155 };
156#endif
157
158 /*
159 * Shared file descriptor for dma_buf sharing. This must be the first element in the
160 * structure so that binder knows where it is and can properly share it between
161 * processes.
162 * DO NOT MOVE THIS ELEMENT!
163 */
164 int share_fd;
165 int share_attr_fd;
166
Chia-I Wua3db1072017-05-08 12:55:28 -0700167 ion_user_handle_t ion_hnd_UNUSED;
John Stultz16100f62017-05-03 11:12:18 -0700168
169 // ints
170 int magic;
171 int req_format;
172 uint64_t internal_format;
173 int byte_stride;
174 int flags;
175 int usage;
176 int size;
177 int width;
178 int height;
179 int internalWidth;
180 int internalHeight;
181 int stride;
182 union {
183 void* base;
184 uint64_t padding;
185 };
186 int lockState;
187 int writeOwner;
Chia-I Wua3db1072017-05-08 12:55:28 -0700188 int pid_UNUSED;
John Stultz16100f62017-05-03 11:12:18 -0700189
190 // locally mapped shared attribute area
191 union {
192 void* attr_base;
193 uint64_t padding3;
194 };
195
196 mali_gralloc_yuv_info yuv_info;
197
198 // Following members is for framebuffer only
Chia-I Wu5f82a372017-05-08 12:50:36 -0700199 int shallow_fbdev_fd; // shallow copy, not dup'ed
John Stultz16100f62017-05-03 11:12:18 -0700200 union {
201 off_t offset;
202 uint64_t padding4;
203 };
204
205 /*
206 * min_pgsz denotes minimum phys_page size used by this buffer.
207 * if buffer memory is physical contiguous set min_pgsz to buff->size
208 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
209 */
210 int min_pgsz;
211#ifdef __cplusplus
212 /*
213 * We track the number of integers in the structure. There are 16 unconditional
Chia-I Wu8853aae2017-07-14 08:34:25 -0700214 * integers (magic - pid, yuv_info, shallow_fbdev_fd and offset).
215 * Note that the shallow_fbdev_fd element is
John Stultz16100f62017-05-03 11:12:18 -0700216 * considered an int not an fd because it is not intended to be used outside the
217 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
218 * number of integers that are conditionally included. Similar considerations apply
219 * to the number of fds.
220 */
221 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
222 static const int sMagic = 0x3141592;
223
224 private_handle_t(int _flags, int _usage, int _size, void *_base, int lock_state, int fb_file, off_t fb_offset):
225 share_fd(-1),
226 share_attr_fd(-1),
Chia-I Wua3db1072017-05-08 12:55:28 -0700227 ion_hnd_UNUSED(-1),
John Stultz16100f62017-05-03 11:12:18 -0700228 magic(sMagic),
229 flags(_flags),
230 usage(_usage),
231 size(_size),
232 width(0),
233 height(0),
234 stride(0),
235 base(_base),
236 lockState(lock_state),
237 writeOwner(0),
Chia-I Wua3db1072017-05-08 12:55:28 -0700238 pid_UNUSED(-1),
John Stultz16100f62017-05-03 11:12:18 -0700239 attr_base(MAP_FAILED),
240 yuv_info(MALI_YUV_NO_INFO),
Chia-I Wu5f82a372017-05-08 12:50:36 -0700241 shallow_fbdev_fd(fb_file),
John Stultz16100f62017-05-03 11:12:18 -0700242 offset(fb_offset)
243 {
244 version = sizeof(native_handle);
245 numFds = sNumFds;
246 numInts = NUM_INTS_IN_PRIVATE_HANDLE;
247 }
248
249 ~private_handle_t()
250 {
251 magic = 0;
252 }
253
254 bool usesPhysicallyContiguousMemory()
255 {
256 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
257 }
258
259 static int validate(const native_handle* h)
260 {
261 const private_handle_t* hnd = (const private_handle_t*)h;
262 if (!h ||
263 h->version != sizeof(native_handle) ||
264 h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
265 h->numFds != sNumFds ||
266 hnd->magic != sMagic)
267 {
268 return -EINVAL;
269 }
270 return 0;
271 }
272
273 static private_handle_t* dynamicCast(const native_handle* in)
274 {
275 if (validate(in) == 0)
276 {
277 return (private_handle_t*) in;
278 }
279 return NULL;
280 }
281#endif
282};
283#ifndef __cplusplus
284/* Restore previous diagnostic for pedantic */
285#pragma GCC diagnostic pop
286#endif
287
288#endif /* GRALLOC_PRIV_H_ */