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"
18 #include "ui/console.h"
20 #include "ui/egl-helpers.h"
21 #include "ui/shader.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 Window x11_window
= gdk_x11_window_get_xid(gdk_window
);
55 vc
->gfx
.ectx
= qemu_egl_init_ctx();
56 vc
->gfx
.esurface
= qemu_egl_init_surface_x11
57 (vc
->gfx
.ectx
, (EGLNativeWindowType
)x11_window
);
59 assert(vc
->gfx
.esurface
);
62 void gd_egl_draw(VirtualConsole
*vc
)
71 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
72 ww
= gdk_window_get_width(window
);
73 wh
= gdk_window_get_height(window
);
75 if (vc
->gfx
.scanout_mode
) {
76 gd_egl_scanout_flush(&vc
->gfx
.dcl
, 0, 0, vc
->gfx
.w
, vc
->gfx
.h
);
78 vc
->gfx
.scale_x
= (double)ww
/ vc
->gfx
.w
;
79 vc
->gfx
.scale_y
= (double)wh
/ vc
->gfx
.h
;
84 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
85 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
87 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
88 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
90 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
92 vc
->gfx
.scale_x
= (double)ww
/ surface_width(vc
->gfx
.ds
);
93 vc
->gfx
.scale_y
= (double)wh
/ surface_height(vc
->gfx
.ds
);
97 void gd_egl_update(DisplayChangeListener
*dcl
,
98 int x
, int y
, int w
, int h
)
100 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
102 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
106 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
107 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
108 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
112 void gd_egl_refresh(DisplayChangeListener
*dcl
)
114 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
116 vc
->gfx
.dcl
.update_interval
= gd_monitor_update_interval(
117 vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
119 if (!vc
->gfx
.esurface
) {
121 if (!vc
->gfx
.esurface
) {
124 vc
->gfx
.gls
= qemu_gl_init_shader();
126 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
130 graphic_hw_update(dcl
->con
);
132 if (vc
->gfx
.glupdates
) {
133 vc
->gfx
.glupdates
= 0;
134 gtk_egl_set_scanout_mode(vc
, false);
139 void gd_egl_switch(DisplayChangeListener
*dcl
,
140 DisplaySurface
*surface
)
142 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
145 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
148 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
149 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
153 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
154 vc
->gfx
.ds
= surface
;
156 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
160 gd_update_windowsize(vc
);
164 QEMUGLContext
gd_egl_create_context(DisplayChangeListener
*dcl
,
165 QEMUGLParams
*params
)
167 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
169 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
170 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
171 return qemu_egl_create_context(dcl
, params
);
174 void gd_egl_scanout_disable(DisplayChangeListener
*dcl
)
176 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
180 gtk_egl_set_scanout_mode(vc
, false);
183 void gd_egl_scanout_texture(DisplayChangeListener
*dcl
,
184 uint32_t backing_id
, bool backing_y_0_top
,
185 uint32_t backing_width
, uint32_t backing_height
,
186 uint32_t x
, uint32_t y
,
187 uint32_t w
, uint32_t h
)
189 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
195 vc
->gfx
.y0_top
= backing_y_0_top
;
197 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
198 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
200 gtk_egl_set_scanout_mode(vc
, true);
201 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
205 void gd_egl_scanout_dmabuf(DisplayChangeListener
*dcl
,
208 #ifdef CONFIG_OPENGL_DMABUF
209 egl_dmabuf_import_texture(dmabuf
);
210 if (!dmabuf
->texture
) {
214 gd_egl_scanout_texture(dcl
, dmabuf
->texture
,
215 false, dmabuf
->width
, dmabuf
->height
,
216 0, 0, dmabuf
->width
, dmabuf
->height
);
220 void gd_egl_cursor_dmabuf(DisplayChangeListener
*dcl
,
221 QemuDmaBuf
*dmabuf
, bool have_hot
,
222 uint32_t hot_x
, uint32_t hot_y
)
224 #ifdef CONFIG_OPENGL_DMABUF
225 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
228 egl_dmabuf_import_texture(dmabuf
);
229 if (!dmabuf
->texture
) {
232 egl_fb_setup_for_tex(&vc
->gfx
.cursor_fb
, dmabuf
->width
, dmabuf
->height
,
233 dmabuf
->texture
, false);
235 egl_fb_destroy(&vc
->gfx
.cursor_fb
);
240 void gd_egl_cursor_position(DisplayChangeListener
*dcl
,
241 uint32_t pos_x
, uint32_t pos_y
)
243 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
245 vc
->gfx
.cursor_x
= pos_x
* vc
->gfx
.scale_x
;
246 vc
->gfx
.cursor_y
= pos_y
* vc
->gfx
.scale_y
;
249 void gd_egl_release_dmabuf(DisplayChangeListener
*dcl
,
252 #ifdef CONFIG_OPENGL_DMABUF
253 egl_dmabuf_release_texture(dmabuf
);
257 void gd_egl_scanout_flush(DisplayChangeListener
*dcl
,
258 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
260 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
264 if (!vc
->gfx
.scanout_mode
) {
267 if (!vc
->gfx
.guest_fb
.framebuffer
) {
271 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
272 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
274 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
275 ww
= gdk_window_get_width(window
);
276 wh
= gdk_window_get_height(window
);
277 egl_fb_setup_default(&vc
->gfx
.win_fb
, ww
, wh
);
278 if (vc
->gfx
.cursor_fb
.texture
) {
279 egl_texture_blit(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
,
281 egl_texture_blend(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.cursor_fb
,
283 vc
->gfx
.cursor_x
, vc
->gfx
.cursor_y
,
284 vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
286 egl_fb_blit(&vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
, !vc
->gfx
.y0_top
);
289 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
292 void gtk_egl_init(DisplayGLMode mode
)
294 GdkDisplay
*gdk_display
= gdk_display_get_default();
295 Display
*x11_display
= gdk_x11_display_get_xdisplay(gdk_display
);
297 if (qemu_egl_init_dpy_x11(x11_display
, mode
) < 0) {
304 int gd_egl_make_current(DisplayChangeListener
*dcl
,
307 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
309 return eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
310 vc
->gfx
.esurface
, ctx
);