2 * GTK UI -- egl opengl code.
4 * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget,
5 * which is GtkDrawingArea like widget with opengl rendering support.
7 * This code handles opengl support on older gtk versions, using egl
8 * to get a opengl context for the X11 window.
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.
14 #include "qemu/osdep.h"
15 #include "qemu/main-loop.h"
16 #include "qemu/error-report.h"
20 #include "ui/console.h"
22 #include "ui/egl-helpers.h"
23 #include "ui/shader.h"
25 #include "sysemu/sysemu.h"
27 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
29 if (vc->gfx.scanout_mode == scanout) {
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);
45 /** DisplayState Callbacks (opengl version) **/
47 void gd_egl_init(VirtualConsole *vc)
49 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
54 Window x11_window = gdk_x11_window_get_xid(gdk_window);
59 vc->gfx.ectx = qemu_egl_init_ctx();
60 vc->gfx.esurface = qemu_egl_init_surface_x11
61 (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
63 assert(vc->gfx.esurface);
66 void gd_egl_draw(VirtualConsole *vc)
70 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
78 window = gtk_widget_get_window(vc->gfx.drawing_area);
79 ws = gdk_window_get_scale_factor(window);
80 ww = gdk_window_get_width(window) * ws;
81 wh = gdk_window_get_height(window) * ws;
83 if (vc->gfx.scanout_mode) {
86 if (!dmabuf->draw_submitted) {
89 dmabuf->draw_submitted = false;
93 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
95 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
96 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
101 egl_dmabuf_create_fence(dmabuf);
102 if (dmabuf->fence_fd > 0) {
103 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
106 graphic_hw_gl_block(vc->gfx.dcl.con, false);
113 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
114 vc->gfx.esurface, vc->gfx.ectx);
116 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
117 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
119 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
121 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
122 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
128 void gd_egl_update(DisplayChangeListener *dcl,
129 int x, int y, int w, int h)
131 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
133 if (!vc->gfx.gls || !vc->gfx.ds) {
137 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
138 vc->gfx.esurface, vc->gfx.ectx);
139 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
141 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
142 EGL_NO_SURFACE, EGL_NO_CONTEXT);
145 void gd_egl_refresh(DisplayChangeListener *dcl)
147 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
149 gd_update_monitor_refresh_rate(
150 vc, vc->window ? vc->window : vc->gfx.drawing_area);
152 if (vc->gfx.guest_fb.dmabuf && vc->gfx.guest_fb.dmabuf->draw_submitted) {
156 if (!vc->gfx.esurface) {
158 if (!vc->gfx.esurface) {
161 vc->gfx.gls = qemu_gl_init_shader();
163 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
164 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
167 if (vc->gfx.guest_fb.dmabuf) {
168 egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
169 gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
174 graphic_hw_update(dcl->con);
176 if (vc->gfx.glupdates) {
177 vc->gfx.glupdates = 0;
178 gtk_egl_set_scanout_mode(vc, false);
183 void gd_egl_switch(DisplayChangeListener *dcl,
184 DisplaySurface *surface)
186 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
189 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
192 surface_width(vc->gfx.ds) == surface_width(surface) &&
193 surface_height(vc->gfx.ds) == surface_height(surface)) {
196 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
197 vc->gfx.esurface, vc->gfx.ectx);
199 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
200 vc->gfx.ds = surface;
202 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
206 gd_update_windowsize(vc);
209 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
213 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
214 QEMUGLParams *params)
216 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
218 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
219 vc->gfx.esurface, vc->gfx.ectx);
220 return qemu_egl_create_context(dgc, params);
223 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
225 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
229 gtk_egl_set_scanout_mode(vc, false);
232 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
233 uint32_t backing_id, bool backing_y_0_top,
234 uint32_t backing_width, uint32_t backing_height,
235 uint32_t x, uint32_t y,
236 uint32_t w, uint32_t h,
239 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
245 vc->gfx.y0_top = backing_y_0_top;
247 if (!vc->gfx.esurface) {
249 if (!vc->gfx.esurface) {
253 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
254 vc->gfx.esurface, vc->gfx.ectx);
256 gtk_egl_set_scanout_mode(vc, true);
257 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
262 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
266 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
268 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
269 vc->gfx.esurface, vc->gfx.ectx);
271 egl_dmabuf_import_texture(dmabuf);
272 if (!dmabuf->texture) {
276 gd_egl_scanout_texture(dcl, dmabuf->texture,
278 dmabuf->backing_width, dmabuf->backing_height,
279 dmabuf->x, dmabuf->y, dmabuf->width,
280 dmabuf->height, NULL);
282 if (dmabuf->allow_fences) {
283 vc->gfx.guest_fb.dmabuf = dmabuf;
288 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
289 QemuDmaBuf *dmabuf, bool have_hot,
290 uint32_t hot_x, uint32_t hot_y)
293 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
296 egl_dmabuf_import_texture(dmabuf);
297 if (!dmabuf->texture) {
300 egl_fb_setup_for_tex(&vc->gfx.cursor_fb,
301 dmabuf->backing_width, dmabuf->backing_height,
302 dmabuf->texture, false);
304 egl_fb_destroy(&vc->gfx.cursor_fb);
309 void gd_egl_cursor_position(DisplayChangeListener *dcl,
310 uint32_t pos_x, uint32_t pos_y)
312 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
314 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
315 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
318 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
319 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
321 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
325 if (!vc->gfx.scanout_mode) {
328 if (!vc->gfx.guest_fb.framebuffer) {
332 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
333 vc->gfx.esurface, vc->gfx.ectx);
335 window = gtk_widget_get_window(vc->gfx.drawing_area);
336 ws = gdk_window_get_scale_factor(window);
337 ww = gdk_window_get_width(window) * ws;
338 wh = gdk_window_get_height(window) * ws;
339 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
340 if (vc->gfx.cursor_fb.texture) {
341 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
343 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
345 vc->gfx.cursor_x, vc->gfx.cursor_y,
346 vc->gfx.scale_x, vc->gfx.scale_y);
348 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
352 if (vc->gfx.guest_fb.dmabuf) {
353 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
357 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
360 void gd_egl_flush(DisplayChangeListener *dcl,
361 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
363 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
364 GtkWidget *area = vc->gfx.drawing_area;
366 if (vc->gfx.guest_fb.dmabuf && !vc->gfx.guest_fb.dmabuf->draw_submitted) {
367 graphic_hw_gl_block(vc->gfx.dcl.con, true);
368 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
369 gtk_egl_set_scanout_mode(vc, true);
370 gtk_widget_queue_draw_area(area, x, y, w, h);
374 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
377 void gtk_egl_init(DisplayGLMode mode)
379 GdkDisplay *gdk_display = gdk_display_get_default();
380 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
382 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
389 int gd_egl_make_current(DisplayGLCtx *dgc,
392 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
394 if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
395 vc->gfx.esurface, ctx)) {
396 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());