xref: /openbmc/qemu/ui/gtk-egl.c (revision ba632924)
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
58         (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
59 
60     assert(vc->gfx.esurface);
61 }
62 
63 void gd_egl_draw(VirtualConsole *vc)
64 {
65     GdkWindow *window;
66     int ww, wh;
67 
68     if (!vc->gfx.gls) {
69         return;
70     }
71 
72     window = gtk_widget_get_window(vc->gfx.drawing_area);
73     ww = gdk_window_get_width(window);
74     wh = gdk_window_get_height(window);
75 
76     if (vc->gfx.scanout_mode) {
77         gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
78 
79         vc->gfx.scale_x = (double)ww / vc->gfx.w;
80         vc->gfx.scale_y = (double)wh / vc->gfx.h;
81     } else {
82         if (!vc->gfx.ds) {
83             return;
84         }
85         eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
86                        vc->gfx.esurface, vc->gfx.ectx);
87 
88         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
89         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
90 
91         eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
92 
93         vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
94         vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
95     }
96 }
97 
98 void gd_egl_update(DisplayChangeListener *dcl,
99                    int x, int y, int w, int h)
100 {
101     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
102 
103     if (!vc->gfx.gls || !vc->gfx.ds) {
104         return;
105     }
106 
107     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
108                    vc->gfx.esurface, vc->gfx.ectx);
109     surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
110     vc->gfx.glupdates++;
111 }
112 
113 void gd_egl_refresh(DisplayChangeListener *dcl)
114 {
115     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
116 
117     if (!vc->gfx.esurface) {
118         gd_egl_init(vc);
119         if (!vc->gfx.esurface) {
120             return;
121         }
122         vc->gfx.gls = qemu_gl_init_shader();
123         if (vc->gfx.ds) {
124             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
125         }
126     }
127 
128     graphic_hw_update(dcl->con);
129 
130     if (vc->gfx.glupdates) {
131         vc->gfx.glupdates = 0;
132         gtk_egl_set_scanout_mode(vc, false);
133         gd_egl_draw(vc);
134     }
135 }
136 
137 void gd_egl_switch(DisplayChangeListener *dcl,
138                    DisplaySurface *surface)
139 {
140     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
141     bool resized = true;
142 
143     trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
144 
145     if (vc->gfx.ds &&
146         surface_width(vc->gfx.ds) == surface_width(surface) &&
147         surface_height(vc->gfx.ds) == surface_height(surface)) {
148         resized = false;
149     }
150 
151     surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
152     vc->gfx.ds = surface;
153     if (vc->gfx.gls) {
154         surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
155     }
156 
157     if (resized) {
158         gd_update_windowsize(vc);
159     }
160 }
161 
162 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
163                                     QEMUGLParams *params)
164 {
165     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
166 
167     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
168                    vc->gfx.esurface, vc->gfx.ectx);
169     return qemu_egl_create_context(dcl, params);
170 }
171 
172 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
173 {
174     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
175 
176     vc->gfx.w = 0;
177     vc->gfx.h = 0;
178     gtk_egl_set_scanout_mode(vc, false);
179 }
180 
181 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
182                             uint32_t backing_id, bool backing_y_0_top,
183                             uint32_t backing_width, uint32_t backing_height,
184                             uint32_t x, uint32_t y,
185                             uint32_t w, uint32_t h)
186 {
187     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
188 
189     vc->gfx.x = x;
190     vc->gfx.y = y;
191     vc->gfx.w = w;
192     vc->gfx.h = h;
193     vc->gfx.y0_top = backing_y_0_top;
194 
195     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
196                    vc->gfx.esurface, vc->gfx.ectx);
197 
198     gtk_egl_set_scanout_mode(vc, true);
199     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
200                          backing_id, false);
201 }
202 
203 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
204                            QemuDmaBuf *dmabuf)
205 {
206 #ifdef CONFIG_OPENGL_DMABUF
207     egl_dmabuf_import_texture(dmabuf);
208     if (!dmabuf->texture) {
209         return;
210     }
211 
212     gd_egl_scanout_texture(dcl, dmabuf->texture,
213                            false, dmabuf->width, dmabuf->height,
214                            0, 0, dmabuf->width, dmabuf->height);
215 #endif
216 }
217 
218 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
219                           QemuDmaBuf *dmabuf, bool have_hot,
220                           uint32_t hot_x, uint32_t hot_y)
221 {
222 #ifdef CONFIG_OPENGL_DMABUF
223     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
224 
225     if (dmabuf) {
226         egl_dmabuf_import_texture(dmabuf);
227         if (!dmabuf->texture) {
228             return;
229         }
230         egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
231                              dmabuf->texture, false);
232     } else {
233         egl_fb_destroy(&vc->gfx.cursor_fb);
234     }
235 #endif
236 }
237 
238 void gd_egl_cursor_position(DisplayChangeListener *dcl,
239                             uint32_t pos_x, uint32_t pos_y)
240 {
241     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
242 
243     vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
244     vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
245 }
246 
247 void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
248                            QemuDmaBuf *dmabuf)
249 {
250 #ifdef CONFIG_OPENGL_DMABUF
251     egl_dmabuf_release_texture(dmabuf);
252 #endif
253 }
254 
255 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
256                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
257 {
258     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
259     GdkWindow *window;
260     int ww, wh;
261 
262     if (!vc->gfx.scanout_mode) {
263         return;
264     }
265     if (!vc->gfx.guest_fb.framebuffer) {
266         return;
267     }
268 
269     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
270                    vc->gfx.esurface, vc->gfx.ectx);
271 
272     window = gtk_widget_get_window(vc->gfx.drawing_area);
273     ww = gdk_window_get_width(window);
274     wh = gdk_window_get_height(window);
275     egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
276     if (vc->gfx.cursor_fb.texture) {
277         egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
278                          vc->gfx.y0_top);
279         egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
280                           vc->gfx.y0_top,
281                           vc->gfx.cursor_x, vc->gfx.cursor_y,
282                           vc->gfx.scale_x, vc->gfx.scale_y);
283     } else {
284         egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
285     }
286 
287     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
288 }
289 
290 void gtk_egl_init(DisplayGLMode mode)
291 {
292     GdkDisplay *gdk_display = gdk_display_get_default();
293     Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
294 
295     if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
296         return;
297     }
298 
299     display_opengl = 1;
300 }
301 
302 int gd_egl_make_current(DisplayChangeListener *dcl,
303                         QEMUGLContext ctx)
304 {
305     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
306 
307     return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
308                           vc->gfx.esurface, ctx);
309 }
310