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"
19 #include "ui/console.h"
21 #include "ui/egl-helpers.h"
22 #include "ui/shader.h"
24 #include "sysemu/sysemu.h"
26 static void gtk_egl_set_scanout_mode(VirtualConsole
*vc
, bool scanout
)
28 if (vc
->gfx
.scanout_mode
== scanout
) {
32 vc
->gfx
.scanout_mode
= scanout
;
33 if (!vc
->gfx
.scanout_mode
) {
34 egl_fb_destroy(&vc
->gfx
.guest_fb
);
35 if (vc
->gfx
.surface
) {
36 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
37 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
42 /** DisplayState Callbacks (opengl version) **/
44 void gd_egl_init(VirtualConsole
*vc
)
46 GdkWindow
*gdk_window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
51 Window x11_window
= gdk_x11_window_get_xid(gdk_window
);
56 vc
->gfx
.ectx
= qemu_egl_init_ctx();
57 vc
->gfx
.esurface
= qemu_egl_init_surface_x11
58 (vc
->gfx
.ectx
, (EGLNativeWindowType
)x11_window
);
60 assert(vc
->gfx
.esurface
);
63 void gd_egl_draw(VirtualConsole
*vc
)
72 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
73 ww
= gdk_window_get_width(window
);
74 wh
= gdk_window_get_height(window
);
76 if (vc
->gfx
.scanout_mode
) {
77 gd_egl_scanout_flush(&vc
->gfx
.dcl
, 0, 0, vc
->gfx
.w
, vc
->gfx
.h
);
79 vc
->gfx
.scale_x
= (double)ww
/ vc
->gfx
.w
;
80 vc
->gfx
.scale_y
= (double)wh
/ vc
->gfx
.h
;
85 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
86 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
88 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
89 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
91 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
93 vc
->gfx
.scale_x
= (double)ww
/ surface_width(vc
->gfx
.ds
);
94 vc
->gfx
.scale_y
= (double)wh
/ surface_height(vc
->gfx
.ds
);
99 if (vc
->gfx
.guest_fb
.dmabuf
) {
100 QemuDmaBuf
*dmabuf
= vc
->gfx
.guest_fb
.dmabuf
;
102 egl_dmabuf_create_fence(dmabuf
);
103 if (dmabuf
->fence_fd
> 0) {
104 qemu_set_fd_handler(dmabuf
->fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
107 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
110 graphic_hw_gl_flushed(vc
->gfx
.dcl
.con
);
113 void gd_egl_update(DisplayChangeListener
*dcl
,
114 int x
, int y
, int w
, int h
)
116 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
118 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
122 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
123 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
124 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
128 void gd_egl_refresh(DisplayChangeListener
*dcl
)
130 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
132 vc
->gfx
.dcl
.update_interval
= gd_monitor_update_interval(
133 vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
135 if (!vc
->gfx
.esurface
) {
137 if (!vc
->gfx
.esurface
) {
140 vc
->gfx
.gls
= qemu_gl_init_shader();
142 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
146 graphic_hw_update(dcl
->con
);
148 if (vc
->gfx
.glupdates
) {
149 vc
->gfx
.glupdates
= 0;
150 gtk_egl_set_scanout_mode(vc
, false);
155 void gd_egl_switch(DisplayChangeListener
*dcl
,
156 DisplaySurface
*surface
)
158 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
161 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
164 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
165 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
169 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
170 vc
->gfx
.ds
= surface
;
172 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
176 gd_update_windowsize(vc
);
180 QEMUGLContext
gd_egl_create_context(DisplayChangeListener
*dcl
,
181 QEMUGLParams
*params
)
183 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
185 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
186 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
187 return qemu_egl_create_context(dcl
, params
);
190 void gd_egl_scanout_disable(DisplayChangeListener
*dcl
)
192 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
196 gtk_egl_set_scanout_mode(vc
, false);
199 void gd_egl_scanout_texture(DisplayChangeListener
*dcl
,
200 uint32_t backing_id
, bool backing_y_0_top
,
201 uint32_t backing_width
, uint32_t backing_height
,
202 uint32_t x
, uint32_t y
,
203 uint32_t w
, uint32_t h
)
205 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
211 vc
->gfx
.y0_top
= backing_y_0_top
;
213 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
214 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
216 gtk_egl_set_scanout_mode(vc
, true);
217 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
221 void gd_egl_scanout_dmabuf(DisplayChangeListener
*dcl
,
225 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
227 egl_dmabuf_import_texture(dmabuf
);
228 if (!dmabuf
->texture
) {
232 gd_egl_scanout_texture(dcl
, dmabuf
->texture
,
233 false, dmabuf
->width
, dmabuf
->height
,
234 0, 0, dmabuf
->width
, dmabuf
->height
);
236 if (dmabuf
->allow_fences
) {
237 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
242 void gd_egl_cursor_dmabuf(DisplayChangeListener
*dcl
,
243 QemuDmaBuf
*dmabuf
, bool have_hot
,
244 uint32_t hot_x
, uint32_t hot_y
)
247 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
250 egl_dmabuf_import_texture(dmabuf
);
251 if (!dmabuf
->texture
) {
254 egl_fb_setup_for_tex(&vc
->gfx
.cursor_fb
, dmabuf
->width
, dmabuf
->height
,
255 dmabuf
->texture
, false);
257 egl_fb_destroy(&vc
->gfx
.cursor_fb
);
262 void gd_egl_cursor_position(DisplayChangeListener
*dcl
,
263 uint32_t pos_x
, uint32_t pos_y
)
265 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
267 vc
->gfx
.cursor_x
= pos_x
* vc
->gfx
.scale_x
;
268 vc
->gfx
.cursor_y
= pos_y
* vc
->gfx
.scale_y
;
271 void gd_egl_scanout_flush(DisplayChangeListener
*dcl
,
272 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
274 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
278 if (!vc
->gfx
.scanout_mode
) {
281 if (!vc
->gfx
.guest_fb
.framebuffer
) {
285 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
286 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
288 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
289 ww
= gdk_window_get_width(window
);
290 wh
= gdk_window_get_height(window
);
291 egl_fb_setup_default(&vc
->gfx
.win_fb
, ww
, wh
);
292 if (vc
->gfx
.cursor_fb
.texture
) {
293 egl_texture_blit(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
,
295 egl_texture_blend(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.cursor_fb
,
297 vc
->gfx
.cursor_x
, vc
->gfx
.cursor_y
,
298 vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
300 egl_fb_blit(&vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
, !vc
->gfx
.y0_top
);
304 if (vc
->gfx
.guest_fb
.dmabuf
) {
305 egl_dmabuf_create_sync(vc
->gfx
.guest_fb
.dmabuf
);
309 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
312 void gd_egl_flush(DisplayChangeListener
*dcl
,
313 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
315 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
316 GtkWidget
*area
= vc
->gfx
.drawing_area
;
318 if (vc
->gfx
.guest_fb
.dmabuf
) {
319 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
320 gtk_widget_queue_draw_area(area
, x
, y
, w
, h
);
324 gd_egl_scanout_flush(&vc
->gfx
.dcl
, x
, y
, w
, h
);
327 void gtk_egl_init(DisplayGLMode mode
)
329 GdkDisplay
*gdk_display
= gdk_display_get_default();
330 Display
*x11_display
= gdk_x11_display_get_xdisplay(gdk_display
);
332 if (qemu_egl_init_dpy_x11(x11_display
, mode
) < 0) {
339 int gd_egl_make_current(DisplayChangeListener
*dcl
,
342 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
344 return eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
345 vc
->gfx
.esurface
, ctx
);