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