blob: 1193b1f7d78f4a9e638906a7f8da05b1b6b1f131 [file] [log] [blame]
Roman Stratiienko6a7ac122021-04-02 17:19:54 +03001// clang-format off
2/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19#ifndef ANDROID_GRALLOC_INTERFACE_H
20#define ANDROID_GRALLOC_INTERFACE_H
21
22#include <system/graphics.h>
23#include <hardware/hardware.h>
24
25#include <stdint.h>
26#include <sys/cdefs.h>
27#include <sys/types.h>
28
29#include <cutils/native_handle.h>
30
31#include <hardware/hardware.h>
32#include <hardware/fb.h>
33
34__BEGIN_DECLS
35
36/**
37 * Module versioning information for the Gralloc hardware module, based on
38 * gralloc_module_t.common.module_api_version.
39 *
40 * Version History:
41 *
42 * GRALLOC_MODULE_API_VERSION_0_1:
43 * Initial Gralloc hardware module API.
44 *
45 * GRALLOC_MODULE_API_VERSION_0_2:
46 * Add support for flexible YCbCr format with (*lock_ycbcr)() method.
47 *
48 * GRALLOC_MODULE_API_VERSION_0_3:
49 * Add support for fence passing to/from lock/unlock.
50 */
51
52#define GRALLOC_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
53#define GRALLOC_MODULE_API_VERSION_0_2 HARDWARE_MODULE_API_VERSION(0, 2)
54#define GRALLOC_MODULE_API_VERSION_0_3 HARDWARE_MODULE_API_VERSION(0, 3)
55
56#define GRALLOC_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1)
57
58/**
59 * The id of this module
60 */
61#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
62
63/**
64 * Name of the graphics device to open
65 */
66
67#define GRALLOC_HARDWARE_GPU0 "gpu0"
68
69enum {
70 /* buffer is never read in software */
71 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000U,
72 /* buffer is rarely read in software */
73 GRALLOC_USAGE_SW_READ_RARELY = 0x00000002U,
74 /* buffer is often read in software */
75 GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003U,
76 /* mask for the software read values */
77 GRALLOC_USAGE_SW_READ_MASK = 0x0000000FU,
78
79 /* buffer is never written in software */
80 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000U,
81 /* buffer is rarely written in software */
82 GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020U,
83 /* buffer is often written in software */
84 GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030U,
85 /* mask for the software write values */
86 GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0U,
87
88 /* buffer will be used as an OpenGL ES texture */
89 GRALLOC_USAGE_HW_TEXTURE = 0x00000100U,
90 /* buffer will be used as an OpenGL ES render target */
91 GRALLOC_USAGE_HW_RENDER = 0x00000200U,
92 /* buffer will be used by the 2D hardware blitter */
93 GRALLOC_USAGE_HW_2D = 0x00000400U,
94 /* buffer will be used by the HWComposer HAL module */
95 GRALLOC_USAGE_HW_COMPOSER = 0x00000800U,
96 /* buffer will be used with the framebuffer device */
97 GRALLOC_USAGE_HW_FB = 0x00001000U,
98
99 /* buffer should be displayed full-screen on an external display when
100 * possible */
101 GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000U,
102
103 /* Must have a hardware-protected path to external display sink for
104 * this buffer. If a hardware-protected path is not available, then
105 * either don't composite only this buffer (preferred) to the
106 * external sink, or (less desirable) do not route the entire
107 * composition to the external sink. */
108 GRALLOC_USAGE_PROTECTED = 0x00004000U,
109
110 /* buffer may be used as a cursor */
111 GRALLOC_USAGE_CURSOR = 0x00008000U,
112
113 /* buffer will be used with the HW video encoder */
114 GRALLOC_USAGE_HW_VIDEO_ENCODER = 0x00010000U,
115 /* buffer will be written by the HW camera pipeline */
116 GRALLOC_USAGE_HW_CAMERA_WRITE = 0x00020000U,
117 /* buffer will be read by the HW camera pipeline */
118 GRALLOC_USAGE_HW_CAMERA_READ = 0x00040000U,
119 /* buffer will be used as part of zero-shutter-lag queue */
120 GRALLOC_USAGE_HW_CAMERA_ZSL = 0x00060000U,
121 /* mask for the camera access values */
122 GRALLOC_USAGE_HW_CAMERA_MASK = 0x00060000U,
123 /* mask for the software usage bit-mask */
124 GRALLOC_USAGE_HW_MASK = 0x00071F00U,
125
126 /* buffer will be used as a RenderScript Allocation */
127 GRALLOC_USAGE_RENDERSCRIPT = 0x00100000U,
128
129 /* Set by the consumer to indicate to the producer that they may attach a
130 * buffer that they did not detach from the BufferQueue. Will be filtered
131 * out by GRALLOC_USAGE_ALLOC_MASK, so gralloc modules will not need to
132 * handle this flag. */
133 GRALLOC_USAGE_FOREIGN_BUFFERS = 0x00200000U,
134
135 /* buffer will be used as input to HW HEIC image encoder */
136 GRALLOC_USAGE_HW_IMAGE_ENCODER = 0x08000000U,
137
138 /* Mask of all flags which could be passed to a gralloc module for buffer
139 * allocation. Any flags not in this mask do not need to be handled by
140 * gralloc modules. */
141 GRALLOC_USAGE_ALLOC_MASK = ~(GRALLOC_USAGE_FOREIGN_BUFFERS),
142
143 /* implementation-specific private usage flags */
144 GRALLOC_USAGE_PRIVATE_0 = 0x10000000U,
145 GRALLOC_USAGE_PRIVATE_1 = 0x20000000U,
146 GRALLOC_USAGE_PRIVATE_2 = 0x40000000U,
147 GRALLOC_USAGE_PRIVATE_3 = 0x80000000U,
148 GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000U,
149};
150
151/*****************************************************************************/
152
153/**
154 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
155 * and the fields of this data structure must begin with hw_module_t
156 * followed by module specific information.
157 */
158typedef struct gralloc_module_t {
159 struct hw_module_t common;
160
161 /*
162 * (*registerBuffer)() must be called before a buffer_handle_t that has not
163 * been created with (*alloc_device_t::alloc)() can be used.
164 *
165 * This is intended to be used with buffer_handle_t's that have been
166 * received in this process through IPC.
167 *
168 * This function checks that the handle is indeed a valid one and prepares
169 * it for use with (*lock)() and (*unlock)().
170 *
171 * It is not necessary to call (*registerBuffer)() on a handle created
172 * with (*alloc_device_t::alloc)().
173 *
174 * returns an error if this buffer_handle_t is not valid.
175 */
176 int (*registerBuffer)(struct gralloc_module_t const* module,
177 buffer_handle_t handle);
178
179 /*
180 * (*unregisterBuffer)() is called once this handle is no longer needed in
181 * this process. After this call, it is an error to call (*lock)(),
182 * (*unlock)(), or (*registerBuffer)().
183 *
184 * This function doesn't close or free the handle itself; this is done
185 * by other means, usually through libcutils's native_handle_close() and
186 * native_handle_free().
187 *
188 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
189 * explicitly registered first.
190 */
191 int (*unregisterBuffer)(struct gralloc_module_t const* module,
192 buffer_handle_t handle);
193
194 /*
195 * The (*lock)() method is called before a buffer is accessed for the
196 * specified usage. This call may block, for instance if the h/w needs
197 * to finish rendering or if CPU caches need to be synchronized.
198 *
199 * The caller promises to modify only pixels in the area specified
200 * by (l,t,w,h).
201 *
202 * The content of the buffer outside of the specified area is NOT modified
203 * by this call.
204 *
205 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
206 * of the buffer in virtual memory.
207 *
208 * Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail
209 * and return -EINVAL. These buffers must be locked with (*lock_ycbcr)()
210 * instead.
211 *
212 * THREADING CONSIDERATIONS:
213 *
214 * It is legal for several different threads to lock a buffer from
215 * read access, none of the threads are blocked.
216 *
217 * However, locking a buffer simultaneously for write or read/write is
218 * undefined, but:
219 * - shall not result in termination of the process
220 * - shall not block the caller
221 * It is acceptable to return an error or to leave the buffer's content
222 * into an indeterminate state.
223 *
224 * If the buffer was created with a usage mask incompatible with the
225 * requested usage flags here, -EINVAL is returned.
226 *
227 */
228
229 int (*lock)(struct gralloc_module_t const* module,
230 buffer_handle_t handle, int usage,
231 int l, int t, int w, int h,
232 void** vaddr);
233
234
235 /*
236 * The (*unlock)() method must be called after all changes to the buffer
237 * are completed.
238 */
239
240 int (*unlock)(struct gralloc_module_t const* module,
241 buffer_handle_t handle);
242
243
244 /* reserved for future use */
245 int (*perform)(struct gralloc_module_t const* module,
246 int operation, ... );
247
248 /*
249 * The (*lock_ycbcr)() method is like the (*lock)() method, with the
250 * difference that it fills a struct ycbcr with a description of the buffer
251 * layout, and zeroes out the reserved fields.
252 *
253 * If the buffer format is not compatible with a flexible YUV format (e.g.
254 * the buffer layout cannot be represented with the ycbcr struct), it
255 * will return -EINVAL.
256 *
257 * This method must work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888
258 * if supported by the device, as well as with any other format that is
259 * requested by the multimedia codecs when they are configured with a
260 * flexible-YUV-compatible color-format with android native buffers.
261 *
262 * Note that this method may also be called on buffers of other formats,
263 * including non-YUV formats.
264 *
265 * Added in GRALLOC_MODULE_API_VERSION_0_2.
266 */
267
268 int (*lock_ycbcr)(struct gralloc_module_t const* module,
269 buffer_handle_t handle, int usage,
270 int l, int t, int w, int h,
271 struct android_ycbcr *ycbcr);
272
273 /*
274 * The (*lockAsync)() method is like the (*lock)() method except
275 * that the buffer's sync fence object is passed into the lock
276 * call instead of requiring the caller to wait for completion.
277 *
278 * The gralloc implementation takes ownership of the fenceFd and
279 * is responsible for closing it when no longer needed.
280 *
281 * Added in GRALLOC_MODULE_API_VERSION_0_3.
282 */
283 int (*lockAsync)(struct gralloc_module_t const* module,
284 buffer_handle_t handle, int usage,
285 int l, int t, int w, int h,
286 void** vaddr, int fenceFd);
287
288 /*
289 * The (*unlockAsync)() method is like the (*unlock)() method
290 * except that a buffer sync fence object is returned from the
291 * lock call, representing the completion of any pending work
292 * performed by the gralloc implementation.
293 *
294 * The caller takes ownership of the fenceFd and is responsible
295 * for closing it when no longer needed.
296 *
297 * Added in GRALLOC_MODULE_API_VERSION_0_3.
298 */
299 int (*unlockAsync)(struct gralloc_module_t const* module,
300 buffer_handle_t handle, int* fenceFd);
301
302 /*
303 * The (*lockAsync_ycbcr)() method is like the (*lock_ycbcr)()
304 * method except that the buffer's sync fence object is passed
305 * into the lock call instead of requiring the caller to wait for
306 * completion.
307 *
308 * The gralloc implementation takes ownership of the fenceFd and
309 * is responsible for closing it when no longer needed.
310 *
311 * Added in GRALLOC_MODULE_API_VERSION_0_3.
312 */
313 int (*lockAsync_ycbcr)(struct gralloc_module_t const* module,
314 buffer_handle_t handle, int usage,
315 int l, int t, int w, int h,
316 struct android_ycbcr *ycbcr, int fenceFd);
317
318 /* getTransportSize(..., outNumFds, outNumInts)
319 * This function is mandatory on devices running IMapper2.1 or higher.
320 *
321 * Get the transport size of a buffer. An imported buffer handle is a raw
322 * buffer handle with the process-local runtime data appended. This
323 * function, for example, allows a caller to omit the process-local
324 * runtime data at the tail when serializing the imported buffer handle.
325 *
326 * Note that a client might or might not omit the process-local runtime
327 * data when sending an imported buffer handle. The mapper must support
328 * both cases on the receiving end.
329 */
330 int32_t (*getTransportSize)(
331 struct gralloc_module_t const* module, buffer_handle_t handle, uint32_t *outNumFds,
332 uint32_t *outNumInts);
333
334 /* validateBufferSize(..., w, h, format, usage, stride)
335 * This function is mandatory on devices running IMapper2.1 or higher.
336 *
337 * Validate that the buffer can be safely accessed by a caller who assumes
338 * the specified width, height, format, usage, and stride. This must at least validate
339 * that the buffer size is large enough. Validating the buffer against
340 * individual buffer attributes is optional.
341 */
342 int32_t (*validateBufferSize)(
343 struct gralloc_module_t const* device, buffer_handle_t handle,
344 uint32_t w, uint32_t h, int32_t format, int usage,
345 uint32_t stride);
346
347 /* reserved for future use */
348 void* reserved_proc[1];
349
350} gralloc_module_t;
351
352/*****************************************************************************/
353
354/**
355 * Every device data structure must begin with hw_device_t
356 * followed by module specific public methods and attributes.
357 */
358
359typedef struct alloc_device_t {
360 struct hw_device_t common;
361
362 /*
363 * (*alloc)() Allocates a buffer in graphic memory with the requested
364 * parameters and returns a buffer_handle_t and the stride in pixels to
365 * allow the implementation to satisfy hardware constraints on the width
366 * of a pixmap (eg: it may have to be multiple of 8 pixels).
367 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
368 *
369 * If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be
370 * 0, since the actual strides are available from the android_ycbcr
371 * structure.
372 *
373 * Returns 0 on success or -errno on error.
374 */
375
376 int (*alloc)(struct alloc_device_t* dev,
377 int w, int h, int format, int usage,
378 buffer_handle_t* handle, int* stride);
379
380 /*
381 * (*free)() Frees a previously allocated buffer.
382 * Behavior is undefined if the buffer is still mapped in any process,
383 * but shall not result in termination of the program or security breaches
384 * (allowing a process to get access to another process' buffers).
385 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
386 * invalid after the call.
387 *
388 * Returns 0 on success or -errno on error.
389 */
390 int (*free)(struct alloc_device_t* dev,
391 buffer_handle_t handle);
392
393 /* This hook is OPTIONAL.
394 *
395 * If non NULL it will be caused by SurfaceFlinger on dumpsys
396 */
397 void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
398
399 void* reserved_proc[7];
400} alloc_device_t;
401
402
403/** convenience API for opening and closing a supported device */
404
405static inline int gralloc_open(const struct hw_module_t* module,
406 struct alloc_device_t** device) {
407 return module->methods->open(module,
408 GRALLOC_HARDWARE_GPU0, TO_HW_DEVICE_T_OPEN(device));
409}
410
411static inline int gralloc_close(struct alloc_device_t* device) {
412 return device->common.close(&device->common);
413}
414
415/**
416 * map_usage_to_memtrack should be called after allocating a gralloc buffer.
417 *
418 * @param usage - it is the flag used when alloc function is called.
419 *
420 * This function maps the gralloc usage flags to appropriate memtrack bucket.
421 * GrallocHAL implementers and users should make an additional ION_IOCTL_TAG
422 * call using the memtrack tag returned by this function. This will help the
423 * in-kernel memtack to categorize the memory allocated by different processes
424 * according to their usage.
425 *
426 */
427static inline const char* map_usage_to_memtrack(uint32_t usage) {
428 usage &= GRALLOC_USAGE_ALLOC_MASK;
429
430 if ((usage & GRALLOC_USAGE_HW_CAMERA_WRITE) != 0) {
431 return "camera";
432 } else if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0 ||
433 (usage & GRALLOC_USAGE_EXTERNAL_DISP) != 0) {
434 return "video";
435 } else if ((usage & GRALLOC_USAGE_HW_RENDER) != 0 ||
436 (usage & GRALLOC_USAGE_HW_TEXTURE) != 0) {
437 return "gl";
438 } else if ((usage & GRALLOC_USAGE_HW_CAMERA_READ) != 0) {
439 return "camera";
440 } else if ((usage & GRALLOC_USAGE_SW_READ_MASK) != 0 ||
441 (usage & GRALLOC_USAGE_SW_WRITE_MASK) != 0) {
442 return "cpu";
443 }
444 return "graphics";
445}
446
447__END_DECLS
448
449#endif // ANDROID_GRALLOC_INTERFACE_H