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