OSDN Git Service

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