blob: 959653f08e79d3ec4886ff329eb30181d0bc5091 [file] [log] [blame]
John Stultz18814f62018-02-22 16:02:49 -08001/*
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 <linux/ion.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 */
36struct 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
57struct 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
68struct private_handle_t : public native_handle
69{
70#else
71struct 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 uint64_t consumer_usage;
121 uint64_t producer_usage;
122 uint64_t backing_store_id;
123 int backing_store_size;
124 int writeOwner;
125 int allocating_pid;
126 int remote_pid;
127 int ref_count;
128 // locally mapped shared attribute area
129 union
130 {
131 void *attr_base;
132 uint64_t padding3;
133 };
134
135 mali_gralloc_yuv_info yuv_info;
136
137 // Following members is for framebuffer only
138 int fd;
139 union
140 {
141 off_t offset;
142 uint64_t padding4;
143 };
144
145 /*
146 * min_pgsz denotes minimum phys_page size used by this buffer.
147 * if buffer memory is physical contiguous set min_pgsz to buff->size
148 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
149 */
150 int min_pgsz;
151#ifdef __cplusplus
152 /*
153 * We track the number of integers in the structure. There are 16 unconditional
154 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is
155 * considered an int not an fd because it is not intended to be used outside the
156 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
157 * number of integers that are conditionally included. Similar considerations apply
158 * to the number of fds.
159 */
160 static const int sNumFds = GRALLOC_ARM_NUM_FDS;
161 static const int sMagic = 0x3141592;
162
163 private_handle_t(int _flags, int _size, void *_base, uint64_t _consumer_usage, uint64_t _producer_usage,
164 int fb_file, off_t fb_offset)
165 : share_fd(-1)
166 , share_attr_fd(-1)
167 , magic(sMagic)
168 , flags(_flags)
169 , size(_size)
170 , width(0)
171 , height(0)
172 , stride(0)
173 , base(_base)
174 , consumer_usage(_consumer_usage)
175 , producer_usage(_producer_usage)
176 , backing_store_id(0x0)
177 , backing_store_size(0)
178 , writeOwner(0)
179 , allocating_pid(getpid())
180 , remote_pid(-1)
181 , ref_count(1)
182 , attr_base(MAP_FAILED)
183 , yuv_info(MALI_YUV_NO_INFO)
184 , fd(fb_file)
185 , offset(fb_offset)
186 {
187 version = sizeof(native_handle);
188 numFds = sNumFds;
189 numInts = NUM_INTS_IN_PRIVATE_HANDLE;
190 }
191
192 private_handle_t(int _flags, int _size, int _min_pgsz, uint64_t _consumer_usage, uint64_t _producer_usage,
193 int _shared_fd, int _req_format, uint64_t _internal_format, int _byte_stride, int _width,
194 int _height, int _stride, int _internalWidth, int _internalHeight, int _backing_store_size)
195 : share_fd(_shared_fd)
196 , share_attr_fd(-1)
197 , magic(sMagic)
198 , req_format(_req_format)
199 , internal_format(_internal_format)
200 , byte_stride(_byte_stride)
201 , flags(_flags)
202 , size(_size)
203 , width(_width)
204 , height(_height)
205 , internalWidth(_internalWidth)
206 , internalHeight(_internalHeight)
207 , stride(_stride)
208 , base(NULL)
209 , consumer_usage(_consumer_usage)
210 , producer_usage(_producer_usage)
211 , backing_store_id(0x0)
212 , backing_store_size(_backing_store_size)
213 , writeOwner(0)
214 , allocating_pid(getpid())
215 , remote_pid(-1)
216 , ref_count(1)
217 , attr_base(MAP_FAILED)
218 , yuv_info(MALI_YUV_NO_INFO)
219 , fd(-1)
220 , offset(0)
221 , min_pgsz(_min_pgsz)
222 {
223 version = sizeof(native_handle);
224 numFds = sNumFds;
225 numInts = NUM_INTS_IN_PRIVATE_HANDLE;
226 }
227
228 ~private_handle_t()
229 {
230 magic = 0;
231 }
232
233 bool usesPhysicallyContiguousMemory()
234 {
235 return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
236 }
237
238 static int validate(const native_handle *h)
239 {
240 const private_handle_t *hnd = (const private_handle_t *)h;
241
242 if (!h || h->version != sizeof(native_handle) || h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
243 h->numFds != sNumFds || hnd->magic != sMagic)
244 {
245 return -EINVAL;
246 }
247
248 return 0;
249 }
250
251 static private_handle_t *dynamicCast(const native_handle *in)
252 {
253 if (validate(in) == 0)
254 {
255 return (private_handle_t *)in;
256 }
257
258 return NULL;
259 }
260#endif
261};
262#ifndef __cplusplus
263/* Restore previous diagnostic for pedantic */
264#pragma GCC diagnostic pop
265#endif
266
267#endif /* MALI_GRALLOC_BUFFER_H_ */