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