• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /* Mask of all flags which could be passed to a gralloc module for buffer
135      * allocation. Any flags not in this mask do not need to be handled by
136      * gralloc modules. */
137     GRALLOC_USAGE_ALLOC_MASK            = ~(GRALLOC_USAGE_FOREIGN_BUFFERS),
138 
139     /* implementation-specific private usage flags */
140     GRALLOC_USAGE_PRIVATE_0             = 0x10000000U,
141     GRALLOC_USAGE_PRIVATE_1             = 0x20000000U,
142     GRALLOC_USAGE_PRIVATE_2             = 0x40000000U,
143     GRALLOC_USAGE_PRIVATE_3             = 0x80000000U,
144     GRALLOC_USAGE_PRIVATE_MASK          = 0xF0000000U,
145 };
146 
147 /*****************************************************************************/
148 
149 /**
150  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
151  * and the fields of this data structure must begin with hw_module_t
152  * followed by module specific information.
153  */
154 typedef struct gralloc_module_t {
155     struct hw_module_t common;
156 
157     /*
158      * (*registerBuffer)() must be called before a buffer_handle_t that has not
159      * been created with (*alloc_device_t::alloc)() can be used.
160      *
161      * This is intended to be used with buffer_handle_t's that have been
162      * received in this process through IPC.
163      *
164      * This function checks that the handle is indeed a valid one and prepares
165      * it for use with (*lock)() and (*unlock)().
166      *
167      * It is not necessary to call (*registerBuffer)() on a handle created
168      * with (*alloc_device_t::alloc)().
169      *
170      * returns an error if this buffer_handle_t is not valid.
171      */
172     int (*registerBuffer)(struct gralloc_module_t const* module,
173             buffer_handle_t handle);
174 
175     /*
176      * (*unregisterBuffer)() is called once this handle is no longer needed in
177      * this process. After this call, it is an error to call (*lock)(),
178      * (*unlock)(), or (*registerBuffer)().
179      *
180      * This function doesn't close or free the handle itself; this is done
181      * by other means, usually through libcutils's native_handle_close() and
182      * native_handle_free().
183      *
184      * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
185      * explicitly registered first.
186      */
187     int (*unregisterBuffer)(struct gralloc_module_t const* module,
188             buffer_handle_t handle);
189 
190     /*
191      * The (*lock)() method is called before a buffer is accessed for the
192      * specified usage. This call may block, for instance if the h/w needs
193      * to finish rendering or if CPU caches need to be synchronized.
194      *
195      * The caller promises to modify only pixels in the area specified
196      * by (l,t,w,h).
197      *
198      * The content of the buffer outside of the specified area is NOT modified
199      * by this call.
200      *
201      * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
202      * of the buffer in virtual memory.
203      *
204      * Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail
205      * and return -EINVAL.  These buffers must be locked with (*lock_ycbcr)()
206      * instead.
207      *
208      * THREADING CONSIDERATIONS:
209      *
210      * It is legal for several different threads to lock a buffer from
211      * read access, none of the threads are blocked.
212      *
213      * However, locking a buffer simultaneously for write or read/write is
214      * undefined, but:
215      * - shall not result in termination of the process
216      * - shall not block the caller
217      * It is acceptable to return an error or to leave the buffer's content
218      * into an indeterminate state.
219      *
220      * If the buffer was created with a usage mask incompatible with the
221      * requested usage flags here, -EINVAL is returned.
222      *
223      */
224 
225     int (*lock)(struct gralloc_module_t const* module,
226             buffer_handle_t handle, int usage,
227             int l, int t, int w, int h,
228             void** vaddr);
229 
230 
231     /*
232      * The (*unlock)() method must be called after all changes to the buffer
233      * are completed.
234      */
235 
236     int (*unlock)(struct gralloc_module_t const* module,
237             buffer_handle_t handle);
238 
239 
240     /* reserved for future use */
241     int (*perform)(struct gralloc_module_t const* module,
242             int operation, ... );
243 
244     /*
245      * The (*lock_ycbcr)() method is like the (*lock)() method, with the
246      * difference that it fills a struct ycbcr with a description of the buffer
247      * layout, and zeroes out the reserved fields.
248      *
249      * If the buffer format is not compatible with a flexible YUV format (e.g.
250      * the buffer layout cannot be represented with the ycbcr struct), it
251      * will return -EINVAL.
252      *
253      * This method must work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888
254      * if supported by the device, as well as with any other format that is
255      * requested by the multimedia codecs when they are configured with a
256      * flexible-YUV-compatible color-format with android native buffers.
257      *
258      * Note that this method may also be called on buffers of other formats,
259      * including non-YUV formats.
260      *
261      * Added in GRALLOC_MODULE_API_VERSION_0_2.
262      */
263 
264     int (*lock_ycbcr)(struct gralloc_module_t const* module,
265             buffer_handle_t handle, int usage,
266             int l, int t, int w, int h,
267             struct android_ycbcr *ycbcr);
268 
269     /*
270      * The (*lockAsync)() method is like the (*lock)() method except
271      * that the buffer's sync fence object is passed into the lock
272      * call instead of requiring the caller to wait for completion.
273      *
274      * The gralloc implementation takes ownership of the fenceFd and
275      * is responsible for closing it when no longer needed.
276      *
277      * Added in GRALLOC_MODULE_API_VERSION_0_3.
278      */
279     int (*lockAsync)(struct gralloc_module_t const* module,
280             buffer_handle_t handle, int usage,
281             int l, int t, int w, int h,
282             void** vaddr, int fenceFd);
283 
284     /*
285      * The (*unlockAsync)() method is like the (*unlock)() method
286      * except that a buffer sync fence object is returned from the
287      * lock call, representing the completion of any pending work
288      * performed by the gralloc implementation.
289      *
290      * The caller takes ownership of the fenceFd and is responsible
291      * for closing it when no longer needed.
292      *
293      * Added in GRALLOC_MODULE_API_VERSION_0_3.
294      */
295     int (*unlockAsync)(struct gralloc_module_t const* module,
296             buffer_handle_t handle, int* fenceFd);
297 
298     /*
299      * The (*lockAsync_ycbcr)() method is like the (*lock_ycbcr)()
300      * method except that the buffer's sync fence object is passed
301      * into the lock call instead of requiring the caller to wait for
302      * completion.
303      *
304      * The gralloc implementation takes ownership of the fenceFd and
305      * is responsible for closing it when no longer needed.
306      *
307      * Added in GRALLOC_MODULE_API_VERSION_0_3.
308      */
309     int (*lockAsync_ycbcr)(struct gralloc_module_t const* module,
310             buffer_handle_t handle, int usage,
311             int l, int t, int w, int h,
312             struct android_ycbcr *ycbcr, int fenceFd);
313 
314     /* reserved for future use */
315     void* reserved_proc[3];
316 } gralloc_module_t;
317 
318 /*****************************************************************************/
319 
320 /**
321  * Every device data structure must begin with hw_device_t
322  * followed by module specific public methods and attributes.
323  */
324 
325 typedef struct alloc_device_t {
326     struct hw_device_t common;
327 
328     /*
329      * (*alloc)() Allocates a buffer in graphic memory with the requested
330      * parameters and returns a buffer_handle_t and the stride in pixels to
331      * allow the implementation to satisfy hardware constraints on the width
332      * of a pixmap (eg: it may have to be multiple of 8 pixels).
333      * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
334      *
335      * If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be
336      * 0, since the actual strides are available from the android_ycbcr
337      * structure.
338      *
339      * Returns 0 on success or -errno on error.
340      */
341 
342     int (*alloc)(struct alloc_device_t* dev,
343             int w, int h, int format, int usage,
344             buffer_handle_t* handle, int* stride);
345 
346     /*
347      * (*free)() Frees a previously allocated buffer.
348      * Behavior is undefined if the buffer is still mapped in any process,
349      * but shall not result in termination of the program or security breaches
350      * (allowing a process to get access to another process' buffers).
351      * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
352      * invalid after the call.
353      *
354      * Returns 0 on success or -errno on error.
355      */
356     int (*free)(struct alloc_device_t* dev,
357             buffer_handle_t handle);
358 
359     /* This hook is OPTIONAL.
360      *
361      * If non NULL it will be caused by SurfaceFlinger on dumpsys
362      */
363     void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
364 
365     void* reserved_proc[7];
366 } alloc_device_t;
367 
368 
369 /** convenience API for opening and closing a supported device */
370 
gralloc_open(const struct hw_module_t * module,struct alloc_device_t ** device)371 static inline int gralloc_open(const struct hw_module_t* module,
372         struct alloc_device_t** device) {
373     return module->methods->open(module,
374             GRALLOC_HARDWARE_GPU0, TO_HW_DEVICE_T_OPEN(device));
375 }
376 
gralloc_close(struct alloc_device_t * device)377 static inline int gralloc_close(struct alloc_device_t* device) {
378     return device->common.close(&device->common);
379 }
380 
381 /**
382  * map_usage_to_memtrack should be called after allocating a gralloc buffer.
383  *
384  * @param usage - it is the flag used when alloc function is called.
385  *
386  * This function maps the gralloc usage flags to appropriate memtrack bucket.
387  * GrallocHAL implementers and users should make an additional ION_IOCTL_TAG
388  * call using the memtrack tag returned by this function. This will help the
389  * in-kernel memtack to categorize the memory allocated by different processes
390  * according to their usage.
391  *
392  */
map_usage_to_memtrack(uint32_t usage)393 static inline const char* map_usage_to_memtrack(uint32_t usage) {
394     usage &= GRALLOC_USAGE_ALLOC_MASK;
395 
396     if ((usage & GRALLOC_USAGE_HW_CAMERA_WRITE) != 0) {
397         return "camera";
398     } else if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0 ||
399             (usage & GRALLOC_USAGE_EXTERNAL_DISP) != 0) {
400         return "video";
401     } else if ((usage & GRALLOC_USAGE_HW_RENDER) != 0 ||
402             (usage & GRALLOC_USAGE_HW_TEXTURE) != 0) {
403         return "gl";
404     } else if ((usage & GRALLOC_USAGE_HW_CAMERA_READ) != 0) {
405         return "camera";
406     } else if ((usage & GRALLOC_USAGE_SW_READ_MASK) != 0 ||
407             (usage & GRALLOC_USAGE_SW_WRITE_MASK) != 0) {
408         return "cpu";
409     }
410     return "graphics";
411 }
412 
413 __END_DECLS
414 
415 #endif  // ANDROID_GRALLOC_INTERFACE_H
416