xref: /openbmc/qemu/ui/gtk-egl.c (revision 74de8d6b8431b3b16dff8e1235cb4f47f7feb3d1)
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 "system/system.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         eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
36                        vc->gfx.esurface, vc->gfx.ectx);
37         egl_fb_destroy(&vc->gfx.guest_fb);
38         if (vc->gfx.surface) {
39             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
40             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
41         }
42     }
43 }
44 
45 /** DisplayState Callbacks (opengl version) **/
46 
47 void gd_egl_init(VirtualConsole *vc)
48 {
49     GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
50     if (!gdk_window) {
51         return;
52     }
53 
54     Window x11_window = gdk_x11_window_get_xid(gdk_window);
55     if (!x11_window) {
56         return;
57     }
58 
59     vc->gfx.ectx = qemu_egl_init_ctx();
60     vc->gfx.esurface = qemu_egl_init_surface_x11
61         (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
62 
63     assert(vc->gfx.esurface);
64 }
65 
66 void gd_egl_draw(VirtualConsole *vc)
67 {
68     GdkWindow *window;
69 #ifdef CONFIG_GBM
70     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
71     int fence_fd;
72 #endif
73     int ww, wh, pw, ph, gs;
74 
75     if (!vc->gfx.gls || !vc->gfx.ds) {
76         return;
77     }
78 
79     window = gtk_widget_get_window(vc->gfx.drawing_area);
80     gs = gdk_window_get_scale_factor(window);
81     ww = gdk_window_get_width(window);
82     wh = gdk_window_get_height(window);
83     pw = ww * gs;
84     ph = wh * gs;
85 
86     if (vc->gfx.scanout_mode) {
87 #ifdef CONFIG_GBM
88         if (dmabuf) {
89             if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
90                 return;
91             } else {
92                 qemu_dmabuf_set_draw_submitted(dmabuf, false);
93             }
94         }
95 #endif
96         gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
97 
98         gd_update_scale(vc, ww, wh,
99                         surface_width(vc->gfx.ds),
100                         surface_height(vc->gfx.ds));
101 
102         glFlush();
103 #ifdef CONFIG_GBM
104         if (dmabuf) {
105             egl_dmabuf_create_fence(dmabuf);
106             fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
107             if (fence_fd >= 0) {
108                 qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
109                 return;
110             }
111             graphic_hw_gl_block(vc->gfx.dcl.con, false);
112         }
113 #endif
114     } else {
115         eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
116                        vc->gfx.esurface, vc->gfx.ectx);
117 
118         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, pw, ph);
119         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
120 
121         eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
122 
123         gd_update_scale(vc, ww, wh,
124                         surface_width(vc->gfx.ds),
125                         surface_height(vc->gfx.ds));
126 
127         glFlush();
128     }
129 }
130 
131 void gd_egl_update(DisplayChangeListener *dcl,
132                    int x, int y, int w, int h)
133 {
134     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
135 
136     if (!vc->gfx.gls || !vc->gfx.ds) {
137         return;
138     }
139 
140     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
141                    vc->gfx.esurface, vc->gfx.ectx);
142     surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
143     vc->gfx.glupdates++;
144     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
145                    EGL_NO_SURFACE, EGL_NO_CONTEXT);
146 }
147 
148 void gd_egl_refresh(DisplayChangeListener *dcl)
149 {
150     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
151 
152     gd_update_monitor_refresh_rate(
153             vc, vc->window ? vc->window : vc->gfx.drawing_area);
154 
155     if (vc->gfx.guest_fb.dmabuf &&
156         qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
157         gd_egl_draw(vc);
158         return;
159     }
160 
161     if (!vc->gfx.esurface) {
162         gd_egl_init(vc);
163         if (!vc->gfx.esurface) {
164             return;
165         }
166         vc->gfx.gls = qemu_gl_init_shader();
167         if (vc->gfx.ds) {
168             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
169             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
170         }
171 #ifdef CONFIG_GBM
172         if (vc->gfx.guest_fb.dmabuf) {
173             egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
174             gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
175         }
176 #endif
177     }
178 
179     graphic_hw_update(dcl->con);
180 
181     if (vc->gfx.glupdates) {
182         vc->gfx.glupdates = 0;
183         gtk_egl_set_scanout_mode(vc, false);
184         gd_egl_draw(vc);
185     }
186 }
187 
188 void gd_egl_switch(DisplayChangeListener *dcl,
189                    DisplaySurface *surface)
190 {
191     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
192     bool resized = true;
193 
194     trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
195 
196     if (vc->gfx.ds &&
197         surface_width(vc->gfx.ds) == surface_width(surface) &&
198         surface_height(vc->gfx.ds) == surface_height(surface)) {
199         resized = false;
200     }
201     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
202                    vc->gfx.esurface, vc->gfx.ectx);
203 
204     surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
205     vc->gfx.ds = surface;
206     if (vc->gfx.gls) {
207         surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
208     }
209 
210     if (resized) {
211         gd_update_windowsize(vc);
212     }
213 
214     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
215                    EGL_NO_CONTEXT);
216 }
217 
218 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
219                                     QEMUGLParams *params)
220 {
221     VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
222 
223     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
224                    vc->gfx.esurface, vc->gfx.ectx);
225     return qemu_egl_create_context(dgc, params);
226 }
227 
228 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
229 {
230     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
231 
232     vc->gfx.w = 0;
233     vc->gfx.h = 0;
234     gtk_egl_set_scanout_mode(vc, false);
235 }
236 
237 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
238                             uint32_t backing_id, bool backing_y_0_top,
239                             uint32_t backing_width, uint32_t backing_height,
240                             uint32_t x, uint32_t y,
241                             uint32_t w, uint32_t h,
242                             void *d3d_tex2d)
243 {
244     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
245 
246     vc->gfx.x = x;
247     vc->gfx.y = y;
248     vc->gfx.w = w;
249     vc->gfx.h = h;
250     vc->gfx.y0_top = backing_y_0_top;
251 
252     if (!vc->gfx.esurface) {
253         gd_egl_init(vc);
254         if (!vc->gfx.esurface) {
255             return;
256         }
257     }
258 
259     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
260                    vc->gfx.esurface, vc->gfx.ectx);
261 
262     gtk_egl_set_scanout_mode(vc, true);
263     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
264                          backing_id, false);
265 }
266 
267 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
268                            QemuDmaBuf *dmabuf)
269 {
270 #ifdef CONFIG_GBM
271     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
272     uint32_t x, y, width, height, backing_width, backing_height, texture;
273     bool y0_top;
274 
275     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
276                    vc->gfx.esurface, vc->gfx.ectx);
277 
278     egl_dmabuf_import_texture(dmabuf);
279     texture = qemu_dmabuf_get_texture(dmabuf);
280     if (!texture) {
281         return;
282     }
283 
284     x = qemu_dmabuf_get_x(dmabuf);
285     y = qemu_dmabuf_get_y(dmabuf);
286     width = qemu_dmabuf_get_width(dmabuf);
287     height = qemu_dmabuf_get_height(dmabuf);
288     backing_width = qemu_dmabuf_get_backing_width(dmabuf);
289     backing_height = qemu_dmabuf_get_backing_height(dmabuf);
290     y0_top = qemu_dmabuf_get_y0_top(dmabuf);
291 
292     gd_egl_scanout_texture(dcl, texture, y0_top, backing_width, backing_height,
293                            x, y, width, height, NULL);
294 
295     if (qemu_dmabuf_get_allow_fences(dmabuf)) {
296         vc->gfx.guest_fb.dmabuf = dmabuf;
297     }
298 #endif
299 }
300 
301 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
302                           QemuDmaBuf *dmabuf, bool have_hot,
303                           uint32_t hot_x, uint32_t hot_y)
304 {
305 #ifdef CONFIG_GBM
306     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
307     uint32_t backing_width, backing_height, texture;
308 
309     if (dmabuf) {
310         egl_dmabuf_import_texture(dmabuf);
311         texture = qemu_dmabuf_get_texture(dmabuf);
312         if (!texture) {
313             return;
314         }
315 
316         backing_width = qemu_dmabuf_get_backing_width(dmabuf);
317         backing_height = qemu_dmabuf_get_backing_height(dmabuf);
318         egl_fb_setup_for_tex(&vc->gfx.cursor_fb, backing_width, backing_height,
319                              texture, false);
320     } else {
321         egl_fb_destroy(&vc->gfx.cursor_fb);
322     }
323 #endif
324 }
325 
326 void gd_egl_cursor_position(DisplayChangeListener *dcl,
327                             uint32_t pos_x, uint32_t pos_y)
328 {
329     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
330 
331     vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
332     vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
333 }
334 
335 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
336                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
337 {
338     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
339     GdkWindow *window;
340     int px_offset, py_offset;
341     int gs;
342     int pw_widget, ph_widget, pw_surface, ph_surface;
343     int ww_widget, wh_widget, ww_surface, wh_surface;
344     int fbw, fbh;
345 
346     if (!vc->gfx.scanout_mode) {
347         return;
348     }
349     if (!vc->gfx.guest_fb.framebuffer) {
350         return;
351     }
352 
353     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
354                    vc->gfx.esurface, vc->gfx.ectx);
355 
356     window = gtk_widget_get_window(vc->gfx.drawing_area);
357     gs = gdk_window_get_scale_factor(window);
358     ww_widget = gdk_window_get_width(window);
359     wh_widget = gdk_window_get_height(window);
360     fbw = surface_width(vc->gfx.ds);
361     fbh = surface_height(vc->gfx.ds);
362 
363     gd_update_scale(vc, ww_widget, wh_widget, fbw, fbh);
364 
365     ww_surface = fbw * vc->gfx.scale_x;
366     wh_surface = fbh * vc->gfx.scale_y;
367     pw_widget = ww_widget * gs;
368     ph_widget = wh_widget * gs;
369     pw_surface = ww_surface * gs;
370     ph_surface = wh_surface * gs;
371 
372     px_offset = 0;
373     py_offset = 0;
374     if (pw_widget > pw_surface) {
375         px_offset = (pw_widget - pw_surface) / 2;
376     }
377     if (ph_widget > ph_surface) {
378         py_offset = (ph_widget - ph_surface) / 2;
379     }
380 
381     egl_fb_setup_default(&vc->gfx.win_fb, pw_surface, ph_surface,
382                          px_offset, py_offset);
383     if (vc->gfx.cursor_fb.texture) {
384         egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
385                          vc->gfx.y0_top);
386         egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
387                           vc->gfx.y0_top,
388                           vc->gfx.cursor_x, vc->gfx.cursor_y,
389                           vc->gfx.scale_x, vc->gfx.scale_y);
390     } else {
391         egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
392     }
393 
394 #ifdef CONFIG_GBM
395     if (vc->gfx.guest_fb.dmabuf) {
396         egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
397     }
398 #endif
399 
400     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
401 }
402 
403 void gd_egl_flush(DisplayChangeListener *dcl,
404                   uint32_t x, uint32_t y, uint32_t w, uint32_t h)
405 {
406     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
407     GtkWidget *area = vc->gfx.drawing_area;
408 
409     if (vc->gfx.guest_fb.dmabuf &&
410         !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
411         graphic_hw_gl_block(vc->gfx.dcl.con, true);
412         qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
413         gtk_egl_set_scanout_mode(vc, true);
414         gtk_widget_queue_draw_area(area, x, y, w, h);
415         return;
416     }
417 
418     gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
419 }
420 
421 void gtk_egl_init(DisplayGLMode mode)
422 {
423     GdkDisplay *gdk_display = gdk_display_get_default();
424     Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
425 
426     if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
427         return;
428     }
429 
430     display_opengl = 1;
431 }
432 
433 int gd_egl_make_current(DisplayGLCtx *dgc,
434                         QEMUGLContext ctx)
435 {
436     VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
437 
438     if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
439                         vc->gfx.esurface, ctx)) {
440         error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());
441         return -1;
442     }
443 
444     return 0;
445 }
446