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