uterm: drm: retry DRM wakeup after short timeout
[platform/upstream/kmscon.git] / src / uterm_video.h
1 /*
2  * uterm_video - Linux User-Space Terminal Video Handling
3  *
4  * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Video Control
28  * Linux provides 2 famous ways to access the video hardware: FBDEV and DRM.
29  * fbdev is the older one of both and is simply a mmap() of the framebuffer into
30  * main memory. It does not allow 3D acceleration and if you need 2D
31  * acceleration you should use libraries like cairo to draw into the framebuffer
32  * provided by this library.
33  * DRM is the new approach which provides 3D acceleration with mesa. It allows
34  * much more configuration as fbdev and is the recommended way to access video
35  * hardware on modern computers.
36  * Modern mesa provides 3D acceleration on fbdev, too. This is used in systems
37  * like Android. This will allow us to provide an fbdev backend here.
38  *
39  * Famous linux graphics systems like X.Org/X11 or Wayland use fbdev or DRM
40  * internally to access the video hardware. This API allows low-level access to
41  * fbdev and DRM without the need of X.Org/X11 or Wayland. If VT support is
42  * enabled in your kernel, each application can run on a different VT. For
43  * instance, X.Org may run on VT-7, Wayland on VT-8, your application on VT-9
44  * and default consoles on VT-1 to VT-6. You can switch between them with
45  * ctrl-alt-F1-F12.
46  * If VT support is not available you need other ways to switch between
47  * applications. See uterm_vt for more.
48  */
49
50 #ifndef UTERM_UTERM_VIDEO_H
51 #define UTERM_UTERM_VIDEO_H
52
53 #include <eloop.h>
54 #include <inttypes.h>
55 #include <stdbool.h>
56 #include <stdlib.h>
57
58 struct uterm_mode;
59 struct uterm_display;
60 struct uterm_video;
61 struct uterm_video_module;
62
63 enum uterm_display_state {
64         UTERM_DISPLAY_ACTIVE,
65         UTERM_DISPLAY_ASLEEP,
66         UTERM_DISPLAY_INACTIVE,
67         UTERM_DISPLAY_GONE,
68 };
69
70 enum uterm_display_dpms {
71         UTERM_DPMS_ON,
72         UTERM_DPMS_STANDBY,
73         UTERM_DPMS_SUSPEND,
74         UTERM_DPMS_OFF,
75         UTERM_DPMS_UNKNOWN,
76 };
77
78 enum uterm_video_action {
79         UTERM_WAKE_UP,
80         UTERM_SLEEP,
81         UTERM_NEW,
82         UTERM_GONE,
83         UTERM_REFRESH,
84 };
85
86 struct uterm_video_hotplug {
87         struct uterm_display *display;
88         int action;
89 };
90
91 enum uterm_display_action {
92         UTERM_PAGE_FLIP,
93 };
94
95 struct uterm_display_event {
96         int action;
97 };
98
99 enum uterm_video_format {
100         UTERM_FORMAT_GREY       = 0x01,
101         UTERM_FORMAT_XRGB32     = 0x02,
102         UTERM_FORMAT_RGB16      = 0x04,
103 };
104
105 struct uterm_video_buffer {
106         unsigned int width;
107         unsigned int height;
108         unsigned int stride;
109         unsigned int format;
110         uint8_t *data;
111 };
112
113 struct uterm_video_blend_req {
114         const struct uterm_video_buffer *buf;
115         unsigned int x;
116         unsigned int y;
117         uint8_t fr;
118         uint8_t fg;
119         uint8_t fb;
120         uint8_t br;
121         uint8_t bg;
122         uint8_t bb;
123 };
124
125 typedef void (*uterm_video_cb) (struct uterm_video *video,
126                                 struct uterm_video_hotplug *arg,
127                                 void *data);
128 typedef void (*uterm_display_cb) (struct uterm_display *disp,
129                                   struct uterm_display_event *arg,
130                                   void *data);
131
132 /* misc */
133
134 const char *uterm_dpms_to_name(int dpms);
135 bool uterm_video_available(const struct uterm_video_module *mod);
136
137 /* display modes interface */
138
139 void uterm_mode_ref(struct uterm_mode *mode);
140 void uterm_mode_unref(struct uterm_mode *mode);
141 struct uterm_mode *uterm_mode_next(struct uterm_mode *mode);
142
143 const char *uterm_mode_get_name(const struct uterm_mode *mode);
144 unsigned int uterm_mode_get_width(const struct uterm_mode *mode);
145 unsigned int uterm_mode_get_height(const struct uterm_mode *mode);
146
147 /* display interface */
148
149 void uterm_display_ref(struct uterm_display *disp);
150 void uterm_display_unref(struct uterm_display *disp);
151 struct uterm_display *uterm_display_next(struct uterm_display *disp);
152
153 int uterm_display_register_cb(struct uterm_display *disp, uterm_display_cb cb,
154                               void *data);
155 void uterm_display_unregister_cb(struct uterm_display *disp,
156                                  uterm_display_cb cb, void *data);
157
158 struct uterm_mode *uterm_display_get_modes(struct uterm_display *disp);
159 struct uterm_mode *uterm_display_get_current(struct uterm_display *disp);
160 struct uterm_mode *uterm_display_get_default(struct uterm_display *disp);
161
162 int uterm_display_get_state(struct uterm_display *disp);
163 int uterm_display_activate(struct uterm_display *disp, struct uterm_mode *mode);
164 void uterm_display_deactivate(struct uterm_display *disp);
165 int uterm_display_set_dpms(struct uterm_display *disp, int state);
166 int uterm_display_get_dpms(const struct uterm_display *disp);
167
168 int uterm_display_use(struct uterm_display *disp, bool *opengl);
169 int uterm_display_get_buffers(struct uterm_display *disp,
170                               struct uterm_video_buffer *buffer,
171                               unsigned int formats);
172 int uterm_display_swap(struct uterm_display *disp, bool immediate);
173 bool uterm_display_is_swapping(struct uterm_display *disp);
174
175 int uterm_display_fill(struct uterm_display *disp,
176                        uint8_t r, uint8_t g, uint8_t b,
177                        unsigned int x, unsigned int y,
178                        unsigned int width, unsigned int height);
179 int uterm_display_blit(struct uterm_display *disp,
180                        const struct uterm_video_buffer *buf,
181                        unsigned int x, unsigned int y);
182 int uterm_display_fake_blend(struct uterm_display *disp,
183                              const struct uterm_video_buffer *buf,
184                              unsigned int x, unsigned int y,
185                              uint8_t fr, uint8_t fg, uint8_t fb,
186                              uint8_t br, uint8_t bg, uint8_t bb);
187 int uterm_display_fake_blendv(struct uterm_display *disp,
188                               const struct uterm_video_blend_req *req,
189                               size_t num);
190
191 /* video interface */
192
193 int uterm_video_new(struct uterm_video **out, struct ev_eloop *eloop,
194                     const char *node, const struct uterm_video_module *mod);
195 void uterm_video_ref(struct uterm_video *video);
196 void uterm_video_unref(struct uterm_video *video);
197
198 void uterm_video_segfault(struct uterm_video *video);
199 struct uterm_display *uterm_video_get_displays(struct uterm_video *video);
200 int uterm_video_register_cb(struct uterm_video *video, uterm_video_cb cb,
201                             void *data);
202 void uterm_video_unregister_cb(struct uterm_video *video, uterm_video_cb cb,
203                                void *data);
204
205 void uterm_video_sleep(struct uterm_video *video);
206 int uterm_video_wake_up(struct uterm_video *video);
207 bool uterm_video_is_awake(struct uterm_video *video);
208 void uterm_video_poll(struct uterm_video *video);
209
210 /* external modules */
211
212 #ifdef BUILD_ENABLE_VIDEO_FBDEV
213 extern const struct uterm_video_module *UTERM_VIDEO_FBDEV;
214 #else
215 #define UTERM_VIDEO_FBDEV NULL
216 #endif
217
218 #ifdef BUILD_ENABLE_VIDEO_DRM2D
219 extern const struct uterm_video_module *UTERM_VIDEO_DRM2D;
220 #else
221 #define UTERM_VIDEO_DRM2D NULL
222 #endif
223
224 #ifdef BUILD_ENABLE_VIDEO_DRM3D
225 extern const struct uterm_video_module *UTERM_VIDEO_DRM3D;
226 #else
227 #define UTERM_VIDEO_DRM3D NULL
228 #endif
229
230 #endif /* UTERM_UTERM_VIDEO_H */