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"
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 #if GTK_CHECK_VERSION(3, 0, 0)
52 Window x11_window
= gdk_x11_window_get_xid(gdk_window
);
54 Window x11_window
= gdk_x11_drawable_get_xid(gdk_window
);
60 vc
->gfx
.ectx
= qemu_egl_init_ctx();
61 vc
->gfx
.esurface
= qemu_egl_init_surface_x11(vc
->gfx
.ectx
, x11_window
);
63 assert(vc
->gfx
.esurface
);
66 void gd_egl_draw(VirtualConsole
*vc
)
75 if (vc
->gfx
.scanout_mode
) {
76 gd_egl_scanout_flush(&vc
->gfx
.dcl
, 0, 0, vc
->gfx
.w
, vc
->gfx
.h
);
81 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
82 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
84 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
85 gdk_drawable_get_size(window
, &ww
, &wh
);
86 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
87 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
89 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
93 void gd_egl_update(DisplayChangeListener
*dcl
,
94 int x
, int y
, int w
, int h
)
96 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
98 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
102 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
103 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
104 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
108 void gd_egl_refresh(DisplayChangeListener
*dcl
)
110 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
112 if (!vc
->gfx
.esurface
) {
114 if (!vc
->gfx
.esurface
) {
117 vc
->gfx
.gls
= qemu_gl_init_shader();
119 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
123 graphic_hw_update(dcl
->con
);
125 if (vc
->gfx
.glupdates
) {
126 vc
->gfx
.glupdates
= 0;
127 gtk_egl_set_scanout_mode(vc
, false);
132 void gd_egl_switch(DisplayChangeListener
*dcl
,
133 DisplaySurface
*surface
)
135 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
138 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
141 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
142 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
146 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
147 vc
->gfx
.ds
= surface
;
149 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
153 gd_update_windowsize(vc
);
157 QEMUGLContext
gd_egl_create_context(DisplayChangeListener
*dcl
,
158 QEMUGLParams
*params
)
160 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
162 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
163 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
164 return qemu_egl_create_context(dcl
, params
);
167 void gd_egl_scanout_disable(DisplayChangeListener
*dcl
)
169 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
173 gtk_egl_set_scanout_mode(vc
, false);
176 void gd_egl_scanout_texture(DisplayChangeListener
*dcl
,
177 uint32_t backing_id
, bool backing_y_0_top
,
178 uint32_t backing_width
, uint32_t backing_height
,
179 uint32_t x
, uint32_t y
,
180 uint32_t w
, uint32_t h
)
182 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
188 vc
->gfx
.y0_top
= backing_y_0_top
;
190 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
191 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
193 gtk_egl_set_scanout_mode(vc
, true);
194 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
198 void gd_egl_scanout_dmabuf(DisplayChangeListener
*dcl
,
201 #ifdef CONFIG_OPENGL_DMABUF
202 egl_dmabuf_import_texture(dmabuf
);
203 if (!dmabuf
->texture
) {
207 gd_egl_scanout_texture(dcl
, dmabuf
->texture
,
208 false, dmabuf
->width
, dmabuf
->height
,
209 0, 0, dmabuf
->width
, dmabuf
->height
);
213 void gd_egl_cursor_dmabuf(DisplayChangeListener
*dcl
,
214 QemuDmaBuf
*dmabuf
, bool have_hot
,
215 uint32_t hot_x
, uint32_t hot_y
)
217 #ifdef CONFIG_OPENGL_DMABUF
218 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
221 egl_dmabuf_import_texture(dmabuf
);
222 if (!dmabuf
->texture
) {
225 egl_fb_setup_for_tex(&vc
->gfx
.cursor_fb
, dmabuf
->width
, dmabuf
->height
,
226 dmabuf
->texture
, false);
228 egl_fb_destroy(&vc
->gfx
.cursor_fb
);
233 void gd_egl_cursor_position(DisplayChangeListener
*dcl
,
234 uint32_t pos_x
, uint32_t pos_y
)
236 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
238 vc
->gfx
.cursor_x
= pos_x
;
239 vc
->gfx
.cursor_y
= pos_y
;
242 void gd_egl_release_dmabuf(DisplayChangeListener
*dcl
,
245 #ifdef CONFIG_OPENGL_DMABUF
246 egl_dmabuf_release_texture(dmabuf
);
250 void gd_egl_scanout_flush(DisplayChangeListener
*dcl
,
251 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
253 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
257 if (!vc
->gfx
.scanout_mode
) {
260 if (!vc
->gfx
.guest_fb
.framebuffer
) {
264 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
265 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
267 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
268 gdk_drawable_get_size(window
, &ww
, &wh
);
269 egl_fb_setup_default(&vc
->gfx
.win_fb
, ww
, wh
);
270 if (vc
->gfx
.cursor_fb
.texture
) {
271 egl_texture_blit(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
,
273 egl_texture_blend(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.cursor_fb
,
275 vc
->gfx
.cursor_x
, vc
->gfx
.cursor_y
);
277 egl_fb_blit(&vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
, !vc
->gfx
.y0_top
);
280 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
283 void gtk_egl_init(DisplayGLMode mode
)
285 GdkDisplay
*gdk_display
= gdk_display_get_default();
286 Display
*x11_display
= gdk_x11_display_get_xdisplay(gdk_display
);
288 if (qemu_egl_init_dpy_x11(x11_display
, mode
) < 0) {
295 int gd_egl_make_current(DisplayChangeListener
*dcl
,
298 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
300 return eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
301 vc
->gfx
.esurface
, ctx
);