xref: /aosp_15_r20/external/drm_hwcomposer/tests/test_include/mali_gralloc_buffer.h (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 // clang-format off
2 /*
3  * Copyright (C) 2017 ARM Limited. All rights reserved.
4  *
5  * Copyright (C) 2008 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 #ifndef MALI_GRALLOC_BUFFER_H_
20 #define MALI_GRALLOC_BUFFER_H_
21 
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 
27 #include "mali_gralloc_private_interface_types.h"
28 
29 /* NOTE:
30  * If your framebuffer device driver is integrated with dma_buf, you will have to
31  * change this IOCTL definition to reflect your integration with the framebuffer
32  * device.
33  * Expected return value is a structure filled with a file descriptor
34  * backing your framebuffer device memory.
35  */
36 struct fb_dmabuf_export
37 {
38 	__u32 fd;
39 	__u32 flags;
40 };
41 #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)
42 
43 /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
44  * 8 is big enough for "gpu0" & "fb0" currently
45  */
46 #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
47 #define NUM_FB_BUFFERS 2
48 
49 /* Define number of shared file descriptors */
50 #define GRALLOC_ARM_NUM_FDS 2
51 
52 #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds)
53 
54 #define SZ_4K 0x00001000
55 #define SZ_2M 0x00200000
56 
57 struct private_handle_t;
58 
59 #ifndef __cplusplus
60 /* C99 with pedantic don't allow anonymous unions which is used in below struct
61  * Disable pedantic for C for this struct only.
62  */
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wpedantic"
65 #endif
66 
67 #ifdef __cplusplus
68 struct private_handle_t : public native_handle
69 {
70 #else
71 struct private_handle_t
72 {
73 	struct native_handle nativeHandle;
74 #endif
75 
76 #ifdef __cplusplus
77 	/* Never intended to be used from C code */
78 	enum
79 	{
80 		PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
81 		PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002,
82 		PRIV_FLAGS_USES_ION = 0x00000004,
83 		PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008
84 	};
85 
86 	enum
87 	{
88 		LOCK_STATE_WRITE = 1 << 31,
89 		LOCK_STATE_MAPPED = 1 << 30,
90 		LOCK_STATE_READ_MASK = 0x3FFFFFFF
91 	};
92 #endif
93 
94 	/*
95 	 * Shared file descriptor for dma_buf sharing. This must be the first element in the
96 	 * structure so that binder knows where it is and can properly share it between
97 	 * processes.
98 	 * DO NOT MOVE THIS ELEMENT!
99 	 */
100 	int share_fd;
101 	int share_attr_fd;
102 
103 	// ints
104 	int magic;
105 	int req_format;
106 	uint64_t internal_format;
107 	int byte_stride;
108 	int flags;
109 	int size;
110 	int width;
111 	int height;
112 	int internalWidth;
113 	int internalHeight;
114 	int stride;
115 	union
116 	{
117 		void *base;
118 		uint64_t padding;
119 	};
120 	union {
121 		uint64_t consumer_usage;
122 		uint64_t usage;
123 	};
124 	uint64_t producer_usage;
125 	uint64_t backing_store_id;
126 	int backing_store_size;
127 	int writeOwner;
128 	int allocating_pid;
129 	int remote_pid;
130 	int ref_count;
131 	// locally mapped shared attribute area
132 	union
133 	{
134 		void *attr_base;
135 		uint64_t padding3;
136 	};
137 
138 	mali_gralloc_yuv_info yuv_info;
139 
140 	// Following members is for framebuffer only
141 	int fd;
142 	union
143 	{
144 		off_t offset;
145 		uint64_t padding4;
146 	};
147 
148 	/*
149 	 * min_pgsz denotes minimum phys_page size used by this buffer.
150 	 * if buffer memory is physical contiguous set min_pgsz to buff->size
151 	 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
152 	 */
153 	int min_pgsz;
154 #ifdef __cplusplus
155 	/*
156 	 * We track the number of integers in the structure. There are 16 unconditional
157 	 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is
158 	 * considered an int not an fd because it is not intended to be used outside the
159 	 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
160 	 * number of integers that are conditionally included. Similar considerations apply
161 	 * to the number of fds.
162 	 */
163 	static const int sNumFds = GRALLOC_ARM_NUM_FDS;
164 	static const int sMagic = 0x3141592;
165 
private_handle_tprivate_handle_t166 	private_handle_t(int _flags, int _size, void *_base, uint64_t _consumer_usage, uint64_t _producer_usage,
167 	                 int fb_file, off_t fb_offset)
168 	    : share_fd(-1)
169 	    , share_attr_fd(-1)
170 	    , magic(sMagic)
171 	    , flags(_flags)
172 	    , size(_size)
173 	    , width(0)
174 	    , height(0)
175 	    , stride(0)
176 	    , base(_base)
177 	    , consumer_usage(_consumer_usage)
178 	    , producer_usage(_producer_usage)
179 	    , backing_store_id(0x0)
180 	    , backing_store_size(0)
181 	    , writeOwner(0)
182 	    , allocating_pid(getpid())
183 	    , remote_pid(-1)
184 	    , ref_count(1)
185 	    , attr_base(MAP_FAILED)
186 	    , yuv_info(MALI_YUV_NO_INFO)
187 	    , fd(fb_file)
188 	    , offset(fb_offset)
189 	{
190 		version = sizeof(native_handle);
191 		numFds = sNumFds;
192 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
193 	}
194 
private_handle_tprivate_handle_t195 	private_handle_t(int _flags, int _size, int _min_pgsz, uint64_t _consumer_usage, uint64_t _producer_usage,
196 	                 int _shared_fd, int _req_format, uint64_t _internal_format, int _byte_stride, int _width,
197 	                 int _height, int _stride, int _internalWidth, int _internalHeight, int _backing_store_size)
198 	    : share_fd(_shared_fd)
199 	    , share_attr_fd(-1)
200 	    , magic(sMagic)
201 	    , req_format(_req_format)
202 	    , internal_format(_internal_format)
203 	    , byte_stride(_byte_stride)
204 	    , flags(_flags)
205 	    , size(_size)
206 	    , width(_width)
207 	    , height(_height)
208 	    , internalWidth(_internalWidth)
209 	    , internalHeight(_internalHeight)
210 	    , stride(_stride)
211 	    , base(NULL)
212 	    , consumer_usage(_consumer_usage)
213 	    , producer_usage(_producer_usage)
214 	    , backing_store_id(0x0)
215 	    , backing_store_size(_backing_store_size)
216 	    , writeOwner(0)
217 	    , allocating_pid(getpid())
218 	    , remote_pid(-1)
219 	    , ref_count(1)
220 	    , attr_base(MAP_FAILED)
221 	    , yuv_info(MALI_YUV_NO_INFO)
222 	    , fd(-1)
223 	    , offset(0)
224 	    , min_pgsz(_min_pgsz)
225 	{
226 		version = sizeof(native_handle);
227 		numFds = sNumFds;
228 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
229 	}
230 
~private_handle_tprivate_handle_t231 	~private_handle_t()
232 	{
233 		magic = 0;
234 	}
235 
usesPhysicallyContiguousMemoryprivate_handle_t236 	bool usesPhysicallyContiguousMemory()
237 	{
238 		return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
239 	}
240 
validateprivate_handle_t241 	static int validate(const native_handle *h)
242 	{
243 		const private_handle_t *hnd = (const private_handle_t *)h;
244 
245 		if (!h || h->version != sizeof(native_handle) || h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
246 		    h->numFds != sNumFds || hnd->magic != sMagic)
247 		{
248 			return -EINVAL;
249 		}
250 
251 		return 0;
252 	}
253 
dynamicCastprivate_handle_t254 	static private_handle_t *dynamicCast(const native_handle *in)
255 	{
256 		if (validate(in) == 0)
257 		{
258 			return (private_handle_t *)in;
259 		}
260 
261 		return NULL;
262 	}
263 #endif
264 };
265 #ifndef __cplusplus
266 /* Restore previous diagnostic for pedantic */
267 #pragma GCC diagnostic pop
268 #endif
269 
270 #endif /* MALI_GRALLOC_BUFFER_H_ */
271 // clang-format on
272