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
;
79 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
80 ws
= gdk_window_get_scale_factor(window
);
81 ww
= gdk_window_get_width(window
) * ws
;
82 wh
= gdk_window_get_height(window
) * ws
;
84 if (vc
->gfx
.scanout_mode
) {
87 if (!qemu_dmabuf_get_draw_submitted(dmabuf
)) {
90 qemu_dmabuf_set_draw_submitted(dmabuf
, false);
94 gd_egl_scanout_flush(&vc
->gfx
.dcl
, 0, 0, vc
->gfx
.w
, vc
->gfx
.h
);
96 vc
->gfx
.scale_x
= (double)ww
/ surface_width(vc
->gfx
.ds
);
97 vc
->gfx
.scale_y
= (double)wh
/ surface_height(vc
->gfx
.ds
);
102 egl_dmabuf_create_fence(dmabuf
);
103 fence_fd
= qemu_dmabuf_get_fence_fd(dmabuf
);
105 qemu_set_fd_handler(fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
108 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
115 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
116 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
118 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
119 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
121 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
123 vc
->gfx
.scale_x
= (double)ww
/ surface_width(vc
->gfx
.ds
);
124 vc
->gfx
.scale_y
= (double)wh
/ surface_height(vc
->gfx
.ds
);
130 void gd_egl_update(DisplayChangeListener
*dcl
,
131 int x
, int y
, int w
, int h
)
133 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
135 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
139 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
140 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
141 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
143 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
,
144 EGL_NO_SURFACE
, EGL_NO_CONTEXT
);
147 void gd_egl_refresh(DisplayChangeListener
*dcl
)
149 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
151 gd_update_monitor_refresh_rate(
152 vc
, vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
154 if (vc
->gfx
.guest_fb
.dmabuf
&&
155 qemu_dmabuf_get_draw_submitted(vc
->gfx
.guest_fb
.dmabuf
)) {
160 if (!vc
->gfx
.esurface
) {
162 if (!vc
->gfx
.esurface
) {
165 vc
->gfx
.gls
= qemu_gl_init_shader();
167 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
168 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
171 if (vc
->gfx
.guest_fb
.dmabuf
) {
172 egl_dmabuf_release_texture(vc
->gfx
.guest_fb
.dmabuf
);
173 gd_egl_scanout_dmabuf(dcl
, vc
->gfx
.guest_fb
.dmabuf
);
178 graphic_hw_update(dcl
->con
);
180 if (vc
->gfx
.glupdates
) {
181 vc
->gfx
.glupdates
= 0;
182 gtk_egl_set_scanout_mode(vc
, false);
187 void gd_egl_switch(DisplayChangeListener
*dcl
,
188 DisplaySurface
*surface
)
190 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
193 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
196 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
197 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
200 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
201 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
203 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
204 vc
->gfx
.ds
= surface
;
206 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
210 gd_update_windowsize(vc
);
213 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
217 QEMUGLContext
gd_egl_create_context(DisplayGLCtx
*dgc
,
218 QEMUGLParams
*params
)
220 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
222 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
223 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
224 return qemu_egl_create_context(dgc
, params
);
227 void gd_egl_scanout_disable(DisplayChangeListener
*dcl
)
229 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
233 gtk_egl_set_scanout_mode(vc
, false);
236 void gd_egl_scanout_texture(DisplayChangeListener
*dcl
,
237 uint32_t backing_id
, bool backing_y_0_top
,
238 uint32_t backing_width
, uint32_t backing_height
,
239 uint32_t x
, uint32_t y
,
240 uint32_t w
, uint32_t h
,
243 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
249 vc
->gfx
.y0_top
= backing_y_0_top
;
251 if (!vc
->gfx
.esurface
) {
253 if (!vc
->gfx
.esurface
) {
258 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
259 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
261 gtk_egl_set_scanout_mode(vc
, true);
262 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
266 void gd_egl_scanout_dmabuf(DisplayChangeListener
*dcl
,
270 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
271 uint32_t x
, y
, width
, height
, backing_width
, backing_height
, texture
;
274 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
275 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
277 egl_dmabuf_import_texture(dmabuf
);
278 texture
= qemu_dmabuf_get_texture(dmabuf
);
283 x
= qemu_dmabuf_get_x(dmabuf
);
284 y
= qemu_dmabuf_get_y(dmabuf
);
285 width
= qemu_dmabuf_get_width(dmabuf
);
286 height
= qemu_dmabuf_get_height(dmabuf
);
287 backing_width
= qemu_dmabuf_get_backing_width(dmabuf
);
288 backing_height
= qemu_dmabuf_get_backing_height(dmabuf
);
289 y0_top
= qemu_dmabuf_get_y0_top(dmabuf
);
291 gd_egl_scanout_texture(dcl
, texture
, y0_top
, backing_width
, backing_height
,
292 x
, y
, width
, height
, NULL
);
294 if (qemu_dmabuf_get_allow_fences(dmabuf
)) {
295 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
300 void gd_egl_cursor_dmabuf(DisplayChangeListener
*dcl
,
301 QemuDmaBuf
*dmabuf
, bool have_hot
,
302 uint32_t hot_x
, uint32_t hot_y
)
305 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
306 uint32_t backing_width
, backing_height
, texture
;
309 egl_dmabuf_import_texture(dmabuf
);
310 texture
= qemu_dmabuf_get_texture(dmabuf
);
315 backing_width
= qemu_dmabuf_get_backing_width(dmabuf
);
316 backing_height
= qemu_dmabuf_get_backing_height(dmabuf
);
317 egl_fb_setup_for_tex(&vc
->gfx
.cursor_fb
, backing_width
, backing_height
,
320 egl_fb_destroy(&vc
->gfx
.cursor_fb
);
325 void gd_egl_cursor_position(DisplayChangeListener
*dcl
,
326 uint32_t pos_x
, uint32_t pos_y
)
328 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
330 vc
->gfx
.cursor_x
= pos_x
* vc
->gfx
.scale_x
;
331 vc
->gfx
.cursor_y
= pos_y
* vc
->gfx
.scale_y
;
334 void gd_egl_scanout_flush(DisplayChangeListener
*dcl
,
335 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
337 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
341 if (!vc
->gfx
.scanout_mode
) {
344 if (!vc
->gfx
.guest_fb
.framebuffer
) {
348 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
349 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
351 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
352 ws
= gdk_window_get_scale_factor(window
);
353 ww
= gdk_window_get_width(window
) * ws
;
354 wh
= gdk_window_get_height(window
) * ws
;
355 egl_fb_setup_default(&vc
->gfx
.win_fb
, ww
, wh
);
356 if (vc
->gfx
.cursor_fb
.texture
) {
357 egl_texture_blit(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
,
359 egl_texture_blend(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.cursor_fb
,
361 vc
->gfx
.cursor_x
, vc
->gfx
.cursor_y
,
362 vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
364 egl_fb_blit(&vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
, !vc
->gfx
.y0_top
);
368 if (vc
->gfx
.guest_fb
.dmabuf
) {
369 egl_dmabuf_create_sync(vc
->gfx
.guest_fb
.dmabuf
);
373 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
376 void gd_egl_flush(DisplayChangeListener
*dcl
,
377 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
379 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
380 GtkWidget
*area
= vc
->gfx
.drawing_area
;
382 if (vc
->gfx
.guest_fb
.dmabuf
&&
383 !qemu_dmabuf_get_draw_submitted(vc
->gfx
.guest_fb
.dmabuf
)) {
384 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
385 qemu_dmabuf_set_draw_submitted(vc
->gfx
.guest_fb
.dmabuf
, true);
386 gtk_egl_set_scanout_mode(vc
, true);
387 gtk_widget_queue_draw_area(area
, x
, y
, w
, h
);
391 gd_egl_scanout_flush(&vc
->gfx
.dcl
, x
, y
, w
, h
);
394 void gtk_egl_init(DisplayGLMode mode
)
396 GdkDisplay
*gdk_display
= gdk_display_get_default();
397 Display
*x11_display
= gdk_x11_display_get_xdisplay(gdk_display
);
399 if (qemu_egl_init_dpy_x11(x11_display
, mode
) < 0) {
406 int gd_egl_make_current(DisplayGLCtx
*dgc
,
409 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
411 if (!eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
412 vc
->gfx
.esurface
, ctx
)) {
413 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());