xref: /openbmc/qemu/ui/gtk-egl.c (revision 966f2ec3)
1 /*
2  * GTK UI -- egl opengl code.
3  *
4  * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget,
5  * which is GtkDrawingArea like widget with opengl rendering support.
6  *
7  * This code handles opengl support on older gtk versions, using egl
8  * to get a opengl context for the X11 window.
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 
17 #include "trace.h"
18 
19 #include "ui/console.h"
20 #include "ui/gtk.h"
21 #include "ui/egl-helpers.h"
22 #include "ui/shader.h"
23 
24 #include "sysemu/sysemu.h"
25 
26 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
27 {
28     if (vc->gfx.scanout_mode == scanout) {
29         return;
30     }
31 
32     vc->gfx.scanout_mode = scanout;
33     if (!vc->gfx.scanout_mode) {
34         egl_fb_destroy(&vc->gfx.guest_fb);
35         if (vc->gfx.surface) {
36             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
37             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
38         }
39     }
40 }
41 
42 /** DisplayState Callbacks (opengl version) **/
43 
44 void gd_egl_init(VirtualConsole *vc)
45 {
46     GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
47     if (!gdk_window) {
48         return;
49     }
50 
51     Window x11_window = gdk_x11_window_get_xid(gdk_window);
52     if (!x11_window) {
53         return;
54     }
55 
56     vc->gfx.ectx = qemu_egl_init_ctx();
57     vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window);
58 
59     assert(vc->gfx.esurface);
60 }
61 
62 void gd_egl_draw(VirtualConsole *vc)
63 {
64     GdkWindow *window;
65     int ww, wh;
66 
67     if (!vc->gfx.gls) {
68         return;
69     }
70 
71     if (vc->gfx.scanout_mode) {
72         gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
73     } else {
74         if (!vc->gfx.ds) {
75             return;
76         }
77         eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
78                        vc->gfx.esurface, vc->gfx.ectx);
79 
80         window = gtk_widget_get_window(vc->gfx.drawing_area);
81         ww = gdk_window_get_width(window);
82         wh = gdk_window_get_height(window);
83         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
84         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
85 
86         eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
87     }
88 }
89 
90 void gd_egl_update(DisplayChangeListener *dcl,
91                    int x, int y, int w, int h)
92 {
93     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
94 
95     if (!vc->gfx.gls || !vc->gfx.ds) {
96         return;
97     }
98 
99     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
100                    vc->gfx.esurface, vc->gfx.ectx);
101     surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
102     vc->gfx.glupdates++;
103 }
104 
105 void gd_egl_refresh(DisplayChangeListener *dcl)
106 {
107     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
108 
109     if (!vc->gfx.esurface) {
110         gd_egl_init(vc);
111         if (!vc->gfx.esurface) {
112             return;
113         }
114         vc->gfx.gls = qemu_gl_init_shader();
115         if (vc->gfx.ds) {
116             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
117         }
118     }
119 
120     graphic_hw_update(dcl->con);
121 
122     if (vc->gfx.glupdates) {
123         vc->gfx.glupdates = 0;
124         gtk_egl_set_scanout_mode(vc, false);
125         gd_egl_draw(vc);
126     }
127 }
128 
129 void gd_egl_switch(DisplayChangeListener *dcl,
130                    DisplaySurface *surface)
131 {
132     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
133     bool resized = true;
134 
135     trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
136 
137     if (vc->gfx.ds &&
138         surface_width(vc->gfx.ds) == surface_width(surface) &&
139         surface_height(vc->gfx.ds) == surface_height(surface)) {
140         resized = false;
141     }
142 
143     surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
144     vc->gfx.ds = surface;
145     if (vc->gfx.gls) {
146         surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
147     }
148 
149     if (resized) {
150         gd_update_windowsize(vc);
151     }
152 }
153 
154 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
155                                     QEMUGLParams *params)
156 {
157     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
158 
159     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
160                    vc->gfx.esurface, vc->gfx.ectx);
161     return qemu_egl_create_context(dcl, params);
162 }
163 
164 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
165 {
166     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
167 
168     vc->gfx.w = 0;
169     vc->gfx.h = 0;
170     gtk_egl_set_scanout_mode(vc, false);
171 }
172 
173 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
174                             uint32_t backing_id, bool backing_y_0_top,
175                             uint32_t backing_width, uint32_t backing_height,
176                             uint32_t x, uint32_t y,
177                             uint32_t w, uint32_t h)
178 {
179     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
180 
181     vc->gfx.x = x;
182     vc->gfx.y = y;
183     vc->gfx.w = w;
184     vc->gfx.h = h;
185     vc->gfx.y0_top = backing_y_0_top;
186 
187     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
188                    vc->gfx.esurface, vc->gfx.ectx);
189 
190     gtk_egl_set_scanout_mode(vc, true);
191     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
192                          backing_id, false);
193 }
194 
195 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
196                            QemuDmaBuf *dmabuf)
197 {
198 #ifdef CONFIG_OPENGL_DMABUF
199     egl_dmabuf_import_texture(dmabuf);
200     if (!dmabuf->texture) {
201         return;
202     }
203 
204     gd_egl_scanout_texture(dcl, dmabuf->texture,
205                            false, dmabuf->width, dmabuf->height,
206                            0, 0, dmabuf->width, dmabuf->height);
207 #endif
208 }
209 
210 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
211                           QemuDmaBuf *dmabuf, bool have_hot,
212                           uint32_t hot_x, uint32_t hot_y)
213 {
214 #ifdef CONFIG_OPENGL_DMABUF
215     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
216 
217     if (dmabuf) {
218         egl_dmabuf_import_texture(dmabuf);
219         if (!dmabuf->texture) {
220             return;
221         }
222         egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
223                              dmabuf->texture, false);
224     } else {
225         egl_fb_destroy(&vc->gfx.cursor_fb);
226     }
227 #endif
228 }
229 
230 void gd_egl_cursor_position(DisplayChangeListener *dcl,
231                             uint32_t pos_x, uint32_t pos_y)
232 {
233     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
234 
235     vc->gfx.cursor_x = pos_x;
236     vc->gfx.cursor_y = pos_y;
237 }
238 
239 void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
240                            QemuDmaBuf *dmabuf)
241 {
242 #ifdef CONFIG_OPENGL_DMABUF
243     egl_dmabuf_release_texture(dmabuf);
244 #endif
245 }
246 
247 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
248                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
249 {
250     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
251     GdkWindow *window;
252     int ww, wh;
253 
254     if (!vc->gfx.scanout_mode) {
255         return;
256     }
257     if (!vc->gfx.guest_fb.framebuffer) {
258         return;
259     }
260 
261     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
262                    vc->gfx.esurface, vc->gfx.ectx);
263 
264     window = gtk_widget_get_window(vc->gfx.drawing_area);
265     ww = gdk_window_get_width(window);
266     wh = gdk_window_get_height(window);
267     egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
268     if (vc->gfx.cursor_fb.texture) {
269         egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
270                          vc->gfx.y0_top);
271         egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
272                           vc->gfx.y0_top,
273                           vc->gfx.cursor_x, vc->gfx.cursor_y);
274     } else {
275         egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
276     }
277 
278     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
279 }
280 
281 void gtk_egl_init(DisplayGLMode mode)
282 {
283     GdkDisplay *gdk_display = gdk_display_get_default();
284     Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
285 
286     if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
287         return;
288     }
289 
290     display_opengl = 1;
291 }
292 
293 int gd_egl_make_current(DisplayChangeListener *dcl,
294                         QEMUGLContext ctx)
295 {
296     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
297 
298     return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
299                           vc->gfx.esurface, ctx);
300 }
301