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-common.h"
19 #include "ui/console.h"
21 #include "ui/egl-helpers.h"
23 #include "sysemu/sysemu.h"
25 static void gtk_egl_set_scanout_mode(VirtualConsole
*vc
, bool scanout
)
27 if (vc
->gfx
.scanout_mode
== scanout
) {
31 vc
->gfx
.scanout_mode
= scanout
;
32 if (!vc
->gfx
.scanout_mode
) {
33 egl_fb_destroy(&vc
->gfx
.guest_fb
);
34 if (vc
->gfx
.surface
) {
35 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
36 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
41 /** DisplayState Callbacks (opengl version) **/
43 void gd_egl_init(VirtualConsole
*vc
)
45 GdkWindow
*gdk_window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
50 #if GTK_CHECK_VERSION(3, 0, 0)
51 Window x11_window
= gdk_x11_window_get_xid(gdk_window
);
53 Window x11_window
= gdk_x11_drawable_get_xid(gdk_window
);
59 vc
->gfx
.ectx
= qemu_egl_init_ctx();
60 vc
->gfx
.esurface
= qemu_egl_init_surface_x11(vc
->gfx
.ectx
, x11_window
);
62 assert(vc
->gfx
.esurface
);
65 void gd_egl_draw(VirtualConsole
*vc
)
74 if (vc
->gfx
.scanout_mode
) {
75 gd_egl_scanout_flush(&vc
->gfx
.dcl
, 0, 0, vc
->gfx
.w
, vc
->gfx
.h
);
80 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
81 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
83 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
84 gdk_drawable_get_size(window
, &ww
, &wh
);
85 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
86 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
88 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
92 void gd_egl_update(DisplayChangeListener
*dcl
,
93 int x
, int y
, int w
, int h
)
95 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
97 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
101 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
102 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
103 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
107 void gd_egl_refresh(DisplayChangeListener
*dcl
)
109 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
111 if (!vc
->gfx
.esurface
) {
113 if (!vc
->gfx
.esurface
) {
116 vc
->gfx
.gls
= qemu_gl_init_shader();
118 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
122 graphic_hw_update(dcl
->con
);
124 if (vc
->gfx
.glupdates
) {
125 vc
->gfx
.glupdates
= 0;
126 gtk_egl_set_scanout_mode(vc
, false);
131 void gd_egl_switch(DisplayChangeListener
*dcl
,
132 DisplaySurface
*surface
)
134 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
137 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
140 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
141 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
145 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
146 vc
->gfx
.ds
= surface
;
148 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
152 gd_update_windowsize(vc
);
156 QEMUGLContext
gd_egl_create_context(DisplayChangeListener
*dcl
,
157 QEMUGLParams
*params
)
159 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
161 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
162 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
163 return qemu_egl_create_context(dcl
, params
);
166 void gd_egl_scanout_disable(DisplayChangeListener
*dcl
)
168 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
172 gtk_egl_set_scanout_mode(vc
, false);
175 void gd_egl_scanout_texture(DisplayChangeListener
*dcl
,
176 uint32_t backing_id
, bool backing_y_0_top
,
177 uint32_t backing_width
, uint32_t backing_height
,
178 uint32_t x
, uint32_t y
,
179 uint32_t w
, uint32_t h
)
181 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
187 vc
->gfx
.y0_top
= backing_y_0_top
;
189 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
190 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
192 gtk_egl_set_scanout_mode(vc
, true);
193 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
197 void gd_egl_scanout_flush(DisplayChangeListener
*dcl
,
198 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
200 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
204 if (!vc
->gfx
.scanout_mode
) {
207 if (!vc
->gfx
.guest_fb
.framebuffer
) {
211 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
212 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
214 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
215 gdk_drawable_get_size(window
, &ww
, &wh
);
216 egl_fb_setup_default(&vc
->gfx
.win_fb
, ww
, wh
);
217 egl_fb_blit(&vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
, !vc
->gfx
.y0_top
);
219 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
222 void gtk_egl_init(void)
224 GdkDisplay
*gdk_display
= gdk_display_get_default();
225 Display
*x11_display
= gdk_x11_display_get_xdisplay(gdk_display
);
227 if (qemu_egl_init_dpy_x11(x11_display
) < 0) {
234 int gd_egl_make_current(DisplayChangeListener
*dcl
,
237 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
239 return eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
240 vc
->gfx
.esurface
, ctx
);