• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #ifndef MALI_GRALLOC_BUFFER_H_
19 #define MALI_GRALLOC_BUFFER_H_
20 
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 
26 #include "mali_gralloc_private_interface_types.h"
27 
28 /* NOTE:
29  * If your framebuffer device driver is integrated with dma_buf, you will have to
30  * change this IOCTL definition to reflect your integration with the framebuffer
31  * device.
32  * Expected return value is a structure filled with a file descriptor
33  * backing your framebuffer device memory.
34  */
35 struct fb_dmabuf_export
36 {
37 	__u32 fd;
38 	__u32 flags;
39 };
40 #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)
41 
42 /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
43  * 8 is big enough for "gpu0" & "fb0" currently
44  */
45 #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
46 #define NUM_FB_BUFFERS 2
47 
48 /* Define number of shared file descriptors */
49 #define GRALLOC_ARM_NUM_FDS 2
50 
51 #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds)
52 
53 #define SZ_4K 0x00001000
54 #define SZ_2M 0x00200000
55 
56 struct private_handle_t;
57 
58 #ifndef __cplusplus
59 /* C99 with pedantic don't allow anonymous unions which is used in below struct
60  * Disable pedantic for C for this struct only.
61  */
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wpedantic"
64 #endif
65 
66 #ifdef __cplusplus
67 struct private_handle_t : public native_handle
68 {
69 #else
70 struct private_handle_t
71 {
72 	struct native_handle nativeHandle;
73 #endif
74 
75 #ifdef __cplusplus
76 	/* Never intended to be used from C code */
77 	enum
78 	{
79 		PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
80 		PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002,
81 		PRIV_FLAGS_USES_ION = 0x00000004,
82 		PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008
83 	};
84 
85 	enum
86 	{
87 		LOCK_STATE_WRITE = 1 << 31,
88 		LOCK_STATE_MAPPED = 1 << 30,
89 		LOCK_STATE_READ_MASK = 0x3FFFFFFF
90 	};
91 #endif
92 
93 	/*
94 	 * Shared file descriptor for dma_buf sharing. This must be the first element in the
95 	 * structure so that binder knows where it is and can properly share it between
96 	 * processes.
97 	 * DO NOT MOVE THIS ELEMENT!
98 	 */
99 	int share_fd;
100 	int share_attr_fd;
101 
102 	// ints
103 	int magic;
104 	int req_format;
105 	uint64_t internal_format;
106 	int byte_stride;
107 	int flags;
108 	int size;
109 	int width;
110 	int height;
111 	int format;
112 	int internalWidth;
113 	int internalHeight;
114 	int stride;
115 
116 	/*
117 	 * Multi-layer buffers.
118 	 *
119 	 * Gralloc 1.0 supports multiple image layers within the same
120 	 * buffer allocation, where GRALLOC1_CAPABILITY_LAYERED_BUFFERS is enabled.
121 	 * 'layer_count' defines the number of layers that have been allocated.
122 	 * All layers are the same size (in bytes) and 'size' defines the
123 	 * number of bytes in the whole allocation.
124 	 * Size of each layer = 'size' / 'layer_count'.
125 	 * Offset to nth layer = n * ('size' / 'layer_count'),
126 	 * where n=0 for the first layer.
127 	 */
128 	uint32_t layer_count;
129 	union
130 	{
131 		void *base;
132 		uint64_t padding;
133 	};
134 	int      usage;
135 	uint64_t consumer_usage;
136 	uint64_t producer_usage;
137 	uint64_t backing_store_id;
138 	int backing_store_size;
139 	int writeOwner;
140 	int allocating_pid;
141 	int remote_pid;
142 	int ref_count;
143 	// locally mapped shared attribute area
144 	union
145 	{
146 		void *attr_base;
147 		uint64_t padding3;
148 	};
149 
150 	mali_gralloc_yuv_info yuv_info;
151 
152 	// Following members is for framebuffer only
153 	int fd;
154 	union
155 	{
156 		off_t offset;
157 		uint64_t padding4;
158 	};
159 
160 	/*
161 	 * min_pgsz denotes minimum phys_page size used by this buffer.
162 	 * if buffer memory is physical contiguous set min_pgsz to buff->size
163 	 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
164 	 */
165 	int min_pgsz;
166 #ifdef __cplusplus
167 	/*
168 	 * We track the number of integers in the structure. There are 16 unconditional
169 	 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is
170 	 * considered an int not an fd because it is not intended to be used outside the
171 	 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
172 	 * number of integers that are conditionally included. Similar considerations apply
173 	 * to the number of fds.
174 	 */
175 	static const int sNumFds = GRALLOC_ARM_NUM_FDS;
176 	static const int sMagic = 0x3141592;
177 
private_handle_tprivate_handle_t178 	private_handle_t(int _flags, int _size, void *_base, uint64_t _consumer_usage, uint64_t _producer_usage,
179 	                 int fb_file, off_t fb_offset)
180 	    : share_fd(-1)
181 	    , share_attr_fd(-1)
182 	    , magic(sMagic)
183 	    , flags(_flags)
184 	    , size(_size)
185 	    , width(0)
186 	    , height(0)
187 	    , stride(0)
188 	    , layer_count(0)
189 	    , base(_base)
190 	    , consumer_usage(_consumer_usage)
191 	    , producer_usage(_producer_usage)
192 	    , backing_store_id(0x0)
193 	    , backing_store_size(0)
194 	    , writeOwner(0)
195 	    , allocating_pid(getpid())
196 	    , remote_pid(-1)
197 	    , ref_count(1)
198 	    , attr_base(MAP_FAILED)
199 	    , yuv_info(MALI_YUV_NO_INFO)
200 	    , fd(fb_file)
201 	    , offset(fb_offset)
202 	{
203 		version = sizeof(native_handle);
204 		numFds = sNumFds;
205 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
206 	}
207 
private_handle_tprivate_handle_t208 	private_handle_t(int _flags, int _size, int _min_pgsz, uint64_t _consumer_usage, uint64_t _producer_usage,
209 	                 int _shared_fd, int _req_format, uint64_t _internal_format, int _byte_stride, int _width,
210 	                 int _height, int _stride, int _internalWidth, int _internalHeight, int _backing_store_size,
211 			 uint64_t _layer_count)
212 	    : share_fd(_shared_fd)
213 	    , share_attr_fd(-1)
214 	    , magic(sMagic)
215 	    , req_format(_req_format)
216 	    , internal_format(_internal_format)
217 	    , byte_stride(_byte_stride)
218 	    , flags(_flags)
219 	    , size(_size)
220 	    , width(_width)
221 	    , height(_height)
222 	    , internalWidth(_internalWidth)
223 	    , internalHeight(_internalHeight)
224 	    , stride(_stride)
225 	    , layer_count(_layer_count)
226 	    , base(NULL)
227 	    , consumer_usage(_consumer_usage)
228 	    , producer_usage(_producer_usage)
229 	    , backing_store_id(0x0)
230 	    , backing_store_size(_backing_store_size)
231 	    , writeOwner(0)
232 	    , allocating_pid(getpid())
233 	    , remote_pid(-1)
234 	    , ref_count(1)
235 	    , attr_base(MAP_FAILED)
236 	    , yuv_info(MALI_YUV_NO_INFO)
237 	    , fd(-1)
238 	    , offset(0)
239 	    , min_pgsz(_min_pgsz)
240 	{
241 		version = sizeof(native_handle);
242 		numFds = sNumFds;
243 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
244 		format  = _req_format;
245 	}
246 
~private_handle_tprivate_handle_t247 	~private_handle_t()
248 	{
249 		magic = 0;
250 	}
251 
usesPhysicallyContiguousMemoryprivate_handle_t252 	bool usesPhysicallyContiguousMemory()
253 	{
254 		return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
255 	}
256 
validateprivate_handle_t257 	static int validate(const native_handle *h)
258 	{
259 		const private_handle_t *hnd = (const private_handle_t *)h;
260 
261 		if (!h || h->version != sizeof(native_handle) || h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
262 		    h->numFds != sNumFds || hnd->magic != sMagic)
263 		{
264 			return -EINVAL;
265 		}
266 
267 		return 0;
268 	}
269 
dynamicCastprivate_handle_t270 	static private_handle_t *dynamicCast(const native_handle *in)
271 	{
272 		if (validate(in) == 0)
273 		{
274 			return (private_handle_t *)in;
275 		}
276 
277 		return NULL;
278 	}
279 #endif
280 };
281 #ifndef __cplusplus
282 /* Restore previous diagnostic for pedantic */
283 #pragma GCC diagnostic pop
284 #endif
285 
286 #endif /* MALI_GRALLOC_BUFFER_H_ */
287