xref: /openbmc/qemu/ui/gtk-egl.c (revision a8da2e14)
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/main-loop.h"
16 #include "qemu/error-report.h"
17 
18 #include "trace.h"
19 
20 #include "ui/console.h"
21 #include "ui/gtk.h"
22 #include "ui/egl-helpers.h"
23 #include "ui/shader.h"
24 
25 #include "sysemu/sysemu.h"
26 
27 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
28 {
29     if (vc->gfx.scanout_mode == scanout) {
30         return;
31     }
32 
33     vc->gfx.scanout_mode = scanout;
34     if (!vc->gfx.scanout_mode) {
35         egl_fb_destroy(&vc->gfx.guest_fb);
36         if (vc->gfx.surface) {
37             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
38             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
39         }
40     }
41 }
42 
43 /** DisplayState Callbacks (opengl version) **/
44 
45 void gd_egl_init(VirtualConsole *vc)
46 {
47     GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
48     if (!gdk_window) {
49         return;
50     }
51 
52     Window x11_window = gdk_x11_window_get_xid(gdk_window);
53     if (!x11_window) {
54         return;
55     }
56 
57     vc->gfx.ectx = qemu_egl_init_ctx();
58     vc->gfx.esurface = qemu_egl_init_surface_x11
59         (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
60 
61     assert(vc->gfx.esurface);
62 }
63 
64 void gd_egl_draw(VirtualConsole *vc)
65 {
66     GdkWindow *window;
67 #ifdef CONFIG_GBM
68     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
69 #endif
70     int ww, wh;
71 
72     if (!vc->gfx.gls) {
73         return;
74     }
75 
76     window = gtk_widget_get_window(vc->gfx.drawing_area);
77     ww = gdk_window_get_width(window);
78     wh = gdk_window_get_height(window);
79 
80     if (vc->gfx.scanout_mode) {
81 #ifdef CONFIG_GBM
82         if (dmabuf) {
83             if (!dmabuf->draw_submitted) {
84                 return;
85             } else {
86                 dmabuf->draw_submitted = false;
87             }
88         }
89 #endif
90         gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
91 
92         vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
93         vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
94 
95         glFlush();
96 #ifdef CONFIG_GBM
97         if (dmabuf) {
98             egl_dmabuf_create_fence(dmabuf);
99             if (dmabuf->fence_fd > 0) {
100                 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
101                 return;
102             }
103             graphic_hw_gl_block(vc->gfx.dcl.con, false);
104         }
105 #endif
106     } else {
107         if (!vc->gfx.ds) {
108             return;
109         }
110         eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
111                        vc->gfx.esurface, vc->gfx.ectx);
112 
113         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
114         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
115 
116         eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
117 
118         vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
119         vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
120 
121         glFlush();
122     }
123 }
124 
125 void gd_egl_update(DisplayChangeListener *dcl,
126                    int x, int y, int w, int h)
127 {
128     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
129 
130     if (!vc->gfx.gls || !vc->gfx.ds) {
131         return;
132     }
133 
134     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
135                    vc->gfx.esurface, vc->gfx.ectx);
136     surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
137     vc->gfx.glupdates++;
138 }
139 
140 void gd_egl_refresh(DisplayChangeListener *dcl)
141 {
142     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
143 
144     gd_update_monitor_refresh_rate(
145             vc, vc->window ? vc->window : vc->gfx.drawing_area);
146 
147     if (!vc->gfx.esurface) {
148         gd_egl_init(vc);
149         if (!vc->gfx.esurface) {
150             return;
151         }
152         vc->gfx.gls = qemu_gl_init_shader();
153         if (vc->gfx.ds) {
154             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
155             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
156         }
157 #ifdef CONFIG_GBM
158         if (vc->gfx.guest_fb.dmabuf) {
159             egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
160             gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
161         }
162 #endif
163     }
164 
165     graphic_hw_update(dcl->con);
166 
167     if (vc->gfx.glupdates) {
168         vc->gfx.glupdates = 0;
169         gtk_egl_set_scanout_mode(vc, false);
170         gd_egl_draw(vc);
171     }
172 }
173 
174 void gd_egl_switch(DisplayChangeListener *dcl,
175                    DisplaySurface *surface)
176 {
177     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
178     bool resized = true;
179 
180     trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
181 
182     if (vc->gfx.ds &&
183         surface_width(vc->gfx.ds) == surface_width(surface) &&
184         surface_height(vc->gfx.ds) == surface_height(surface)) {
185         resized = false;
186     }
187     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
188                    vc->gfx.esurface, vc->gfx.ectx);
189 
190     surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
191     vc->gfx.ds = surface;
192     if (vc->gfx.gls) {
193         surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
194     }
195 
196     if (resized) {
197         gd_update_windowsize(vc);
198     }
199 
200     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
201                    EGL_NO_CONTEXT);
202 }
203 
204 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
205                                     QEMUGLParams *params)
206 {
207     VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
208 
209     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
210                    vc->gfx.esurface, vc->gfx.ectx);
211     return qemu_egl_create_context(dgc, params);
212 }
213 
214 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
215 {
216     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
217 
218     vc->gfx.w = 0;
219     vc->gfx.h = 0;
220     gtk_egl_set_scanout_mode(vc, false);
221 }
222 
223 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
224                             uint32_t backing_id, bool backing_y_0_top,
225                             uint32_t backing_width, uint32_t backing_height,
226                             uint32_t x, uint32_t y,
227                             uint32_t w, uint32_t h,
228                             void *d3d_tex2d)
229 {
230     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
231 
232     vc->gfx.x = x;
233     vc->gfx.y = y;
234     vc->gfx.w = w;
235     vc->gfx.h = h;
236     vc->gfx.y0_top = backing_y_0_top;
237 
238     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
239                    vc->gfx.esurface, vc->gfx.ectx);
240 
241     gtk_egl_set_scanout_mode(vc, true);
242     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
243                          backing_id, false);
244 }
245 
246 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
247                            QemuDmaBuf *dmabuf)
248 {
249 #ifdef CONFIG_GBM
250     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
251 
252     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
253                    vc->gfx.esurface, vc->gfx.ectx);
254 
255     egl_dmabuf_import_texture(dmabuf);
256     if (!dmabuf->texture) {
257         return;
258     }
259 
260     gd_egl_scanout_texture(dcl, dmabuf->texture,
261                            dmabuf->y0_top, dmabuf->width, dmabuf->height,
262                            dmabuf->x, dmabuf->y, dmabuf->scanout_width,
263                            dmabuf->scanout_height, NULL);
264 
265     if (dmabuf->allow_fences) {
266         vc->gfx.guest_fb.dmabuf = dmabuf;
267     }
268 #endif
269 }
270 
271 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
272                           QemuDmaBuf *dmabuf, bool have_hot,
273                           uint32_t hot_x, uint32_t hot_y)
274 {
275 #ifdef CONFIG_GBM
276     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
277 
278     if (dmabuf) {
279         egl_dmabuf_import_texture(dmabuf);
280         if (!dmabuf->texture) {
281             return;
282         }
283         egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
284                              dmabuf->texture, false);
285     } else {
286         egl_fb_destroy(&vc->gfx.cursor_fb);
287     }
288 #endif
289 }
290 
291 void gd_egl_cursor_position(DisplayChangeListener *dcl,
292                             uint32_t pos_x, uint32_t pos_y)
293 {
294     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
295 
296     vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
297     vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
298 }
299 
300 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
301                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
302 {
303     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
304     GdkWindow *window;
305     int ww, wh;
306 
307     if (!vc->gfx.scanout_mode) {
308         return;
309     }
310     if (!vc->gfx.guest_fb.framebuffer) {
311         return;
312     }
313 
314     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
315                    vc->gfx.esurface, vc->gfx.ectx);
316 
317     window = gtk_widget_get_window(vc->gfx.drawing_area);
318     ww = gdk_window_get_width(window);
319     wh = gdk_window_get_height(window);
320     egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
321     if (vc->gfx.cursor_fb.texture) {
322         egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
323                          vc->gfx.y0_top);
324         egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
325                           vc->gfx.y0_top,
326                           vc->gfx.cursor_x, vc->gfx.cursor_y,
327                           vc->gfx.scale_x, vc->gfx.scale_y);
328     } else {
329         egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
330     }
331 
332 #ifdef CONFIG_GBM
333     if (vc->gfx.guest_fb.dmabuf) {
334         egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
335     }
336 #endif
337 
338     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
339 }
340 
341 void gd_egl_flush(DisplayChangeListener *dcl,
342                   uint32_t x, uint32_t y, uint32_t w, uint32_t h)
343 {
344     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
345     GtkWidget *area = vc->gfx.drawing_area;
346 
347     if (vc->gfx.guest_fb.dmabuf && !vc->gfx.guest_fb.dmabuf->draw_submitted) {
348         graphic_hw_gl_block(vc->gfx.dcl.con, true);
349         vc->gfx.guest_fb.dmabuf->draw_submitted = true;
350         gtk_widget_queue_draw_area(area, x, y, w, h);
351         return;
352     }
353 
354     gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
355 }
356 
357 void gtk_egl_init(DisplayGLMode mode)
358 {
359     GdkDisplay *gdk_display = gdk_display_get_default();
360     Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
361 
362     if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
363         return;
364     }
365 
366     display_opengl = 1;
367 }
368 
369 int gd_egl_make_current(DisplayGLCtx *dgc,
370                         QEMUGLContext ctx)
371 {
372     VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
373 
374     if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
375                         vc->gfx.esurface, ctx)) {
376         error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());
377         return -1;
378     }
379 
380     return 0;
381 }
382