OSDN Git Service

897fc61afa3a58fdc272047092569aca415651f7
[android-x86/external-drm_gralloc.git] / gralloc_drm.h
1 /*
2  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3  * Copyright (C) 2010-2011 LunarG Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #ifndef _GRALLOC_DRM_H_
25 #define _GRALLOC_DRM_H_
26
27 #include <hardware/gralloc.h>
28 #include "gralloc_drm_formats.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
35 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
36
37 struct gralloc_drm_t;
38 struct gralloc_drm_bo_t;
39
40 enum {
41         GRALLOC_MODULE_PERFORM_GET_DRM_FD                = 0x40000002,
42         GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC             = 0x80000003,
43         GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC            = 0x80000004,
44         GRALLOC_MODULE_PERFORM_ENTER_VT                  = 0x80000005,
45         GRALLOC_MODULE_PERFORM_LEAVE_VT                  = 0x80000006,
46 };
47
48 struct gralloc_drm_t *gralloc_drm_create(void);
49 void gralloc_drm_destroy(struct gralloc_drm_t *drm);
50
51 int gralloc_drm_get_fd(struct gralloc_drm_t *drm);
52 int gralloc_drm_get_magic(struct gralloc_drm_t *drm, int32_t *magic);
53 int gralloc_drm_auth_magic(struct gralloc_drm_t *drm, int32_t magic);
54 int gralloc_drm_set_master(struct gralloc_drm_t *drm);
55 void gralloc_drm_drop_master(struct gralloc_drm_t *drm);
56
57 int gralloc_drm_init_kms(struct gralloc_drm_t *drm);
58 void gralloc_drm_fini_kms(struct gralloc_drm_t *drm);
59 int gralloc_drm_is_kms_initialized(struct gralloc_drm_t *drm);
60
61 void gralloc_drm_get_kms_info(struct gralloc_drm_t *drm, struct framebuffer_device_t *fb);
62 int gralloc_drm_is_kms_pipelined(struct gralloc_drm_t *drm);
63
64 static inline int gralloc_drm_get_bpp(int format)
65 {
66         int bpp;
67
68         switch (format) {
69         case HAL_PIXEL_FORMAT_RGBA_8888:
70         case HAL_PIXEL_FORMAT_RGBX_8888:
71         case HAL_PIXEL_FORMAT_BGRA_8888:
72                 bpp = 4;
73                 break;
74         case HAL_PIXEL_FORMAT_RGB_888:
75                 bpp = 3;
76                 break;
77         case HAL_PIXEL_FORMAT_RGB_565:
78         case HAL_PIXEL_FORMAT_YCbCr_422_I:
79                 bpp = 2;
80                 break;
81         /* planar; only Y is considered */
82         case HAL_PIXEL_FORMAT_YV12:
83         case HAL_PIXEL_FORMAT_DRM_NV12:
84         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
85         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
86                 bpp = 1;
87                 break;
88         default:
89                 bpp = 0;
90                 break;
91         }
92
93         return bpp;
94 }
95
96 static inline void gralloc_drm_align_geometry(int format, int *width, int *height)
97 {
98         int align_w = 1, align_h = 1, extra_height_div = 0;
99
100         switch (format) {
101         case HAL_PIXEL_FORMAT_DRM_NV12:
102         case HAL_PIXEL_FORMAT_YV12:
103                 align_w = 32;
104                 align_h = 2;
105                 extra_height_div = 2;
106                 break;
107         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
108                 align_w = 2;
109                 extra_height_div = 1;
110                 break;
111         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
112                 align_w = 2;
113                 align_h = 2;
114                 extra_height_div = 2;
115                 break;
116         case HAL_PIXEL_FORMAT_YCbCr_422_I:
117                 align_w = 2;
118                 break;
119         }
120
121         *width = ALIGN(*width, align_w);
122         *height = ALIGN(*height, align_h);
123
124         if (extra_height_div)
125                 *height += *height / extra_height_div;
126 }
127
128 int gralloc_drm_handle_register(buffer_handle_t handle, struct gralloc_drm_t *drm);
129 int gralloc_drm_handle_unregister(buffer_handle_t handle);
130
131 struct gralloc_drm_bo_t *gralloc_drm_bo_create(struct gralloc_drm_t *drm, int width, int height, int format, int usage);
132 void gralloc_drm_bo_decref(struct gralloc_drm_bo_t *bo);
133
134 struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle);
135 buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride);
136 int gralloc_drm_get_prime_fd(buffer_handle_t _handle);
137 int gralloc_drm_get_gem_handle(buffer_handle_t handle);
138 void gralloc_drm_resolve_format(buffer_handle_t _handle, uint32_t *pitches, uint32_t *offsets, uint32_t *handles);
139 unsigned int planes_for_format(struct gralloc_drm_t *drm, int hal_format);
140
141 int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo, int x, int y, int w, int h, int enable_write, void **addr);
142 void gralloc_drm_bo_unlock(struct gralloc_drm_bo_t *bo);
143
144 int gralloc_drm_bo_need_fb(const struct gralloc_drm_bo_t *bo);
145 int gralloc_drm_bo_add_fb(struct gralloc_drm_bo_t *bo);
146 void gralloc_drm_bo_rm_fb(struct gralloc_drm_bo_t *bo);
147 int gralloc_drm_bo_post(struct gralloc_drm_bo_t *bo);
148
149 int gralloc_drm_reserve_plane(struct gralloc_drm_t *drm,
150         buffer_handle_t handle, uint32_t id,
151         uint32_t dst_x, uint32_t dst_y, uint32_t dst_w, uint32_t dst_h,
152         uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h);
153 void gralloc_drm_disable_planes(struct gralloc_drm_t *mod);
154 int gralloc_drm_set_plane_handle(struct gralloc_drm_t *drm,
155         uint32_t id, buffer_handle_t handle);
156
157 #ifdef __cplusplus
158 }
159 #endif
160 #endif /* _GRALLOC_DRM_H_ */