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 egl_fb_destroy(&vc
->gfx
.guest_fb
);
36 if (vc
->gfx
.surface
) {
37 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
38 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
43 /** DisplayState Callbacks (opengl version) **/
45 void gd_egl_init(VirtualConsole
*vc
)
47 GdkWindow
*gdk_window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
52 Window x11_window
= gdk_x11_window_get_xid(gdk_window
);
57 vc
->gfx
.ectx
= qemu_egl_init_ctx();
58 vc
->gfx
.esurface
= qemu_egl_init_surface_x11
59 (vc
->gfx
.ectx
, (EGLNativeWindowType
)x11_window
);
61 assert(vc
->gfx
.esurface
);
64 void gd_egl_draw(VirtualConsole
*vc
)
68 QemuDmaBuf
*dmabuf
= vc
->gfx
.guest_fb
.dmabuf
;
76 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
77 ww
= gdk_window_get_width(window
);
78 wh
= gdk_window_get_height(window
);
80 if (vc
->gfx
.scanout_mode
) {
83 if (!dmabuf
->draw_submitted
) {
86 dmabuf
->draw_submitted
= false;
90 gd_egl_scanout_flush(&vc
->gfx
.dcl
, 0, 0, vc
->gfx
.w
, vc
->gfx
.h
);
92 vc
->gfx
.scale_x
= (double)ww
/ surface_width(vc
->gfx
.ds
);
93 vc
->gfx
.scale_y
= (double)wh
/ surface_height(vc
->gfx
.ds
);
98 egl_dmabuf_create_fence(dmabuf
);
99 if (dmabuf
->fence_fd
> 0) {
100 qemu_set_fd_handler(dmabuf
->fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
103 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
110 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
111 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
113 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
114 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
116 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
118 vc
->gfx
.scale_x
= (double)ww
/ surface_width(vc
->gfx
.ds
);
119 vc
->gfx
.scale_y
= (double)wh
/ surface_height(vc
->gfx
.ds
);
125 void gd_egl_update(DisplayChangeListener
*dcl
,
126 int x
, int y
, int w
, int h
)
128 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
130 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
134 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
135 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
136 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
140 void gd_egl_refresh(DisplayChangeListener
*dcl
)
142 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
144 gd_update_monitor_refresh_rate(
145 vc
, vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
147 if (!vc
->gfx
.esurface
) {
149 if (!vc
->gfx
.esurface
) {
152 vc
->gfx
.gls
= qemu_gl_init_shader();
154 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
155 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
158 if (vc
->gfx
.guest_fb
.dmabuf
) {
159 egl_dmabuf_release_texture(vc
->gfx
.guest_fb
.dmabuf
);
160 gd_egl_scanout_dmabuf(dcl
, vc
->gfx
.guest_fb
.dmabuf
);
165 graphic_hw_update(dcl
->con
);
167 if (vc
->gfx
.glupdates
) {
168 vc
->gfx
.glupdates
= 0;
169 gtk_egl_set_scanout_mode(vc
, false);
174 void gd_egl_switch(DisplayChangeListener
*dcl
,
175 DisplaySurface
*surface
)
177 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
180 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
183 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
184 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
187 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
188 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
190 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
191 vc
->gfx
.ds
= surface
;
193 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
197 gd_update_windowsize(vc
);
200 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
204 QEMUGLContext
gd_egl_create_context(DisplayGLCtx
*dgc
,
205 QEMUGLParams
*params
)
207 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
209 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
210 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
211 return qemu_egl_create_context(dgc
, params
);
214 void gd_egl_scanout_disable(DisplayChangeListener
*dcl
)
216 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
220 gtk_egl_set_scanout_mode(vc
, false);
223 void gd_egl_scanout_texture(DisplayChangeListener
*dcl
,
224 uint32_t backing_id
, bool backing_y_0_top
,
225 uint32_t backing_width
, uint32_t backing_height
,
226 uint32_t x
, uint32_t y
,
227 uint32_t w
, uint32_t h
,
230 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
236 vc
->gfx
.y0_top
= backing_y_0_top
;
238 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
239 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
241 gtk_egl_set_scanout_mode(vc
, true);
242 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
246 void gd_egl_scanout_dmabuf(DisplayChangeListener
*dcl
,
250 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
252 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
253 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
255 egl_dmabuf_import_texture(dmabuf
);
256 if (!dmabuf
->texture
) {
260 gd_egl_scanout_texture(dcl
, dmabuf
->texture
,
261 dmabuf
->y0_top
, dmabuf
->width
, dmabuf
->height
,
262 dmabuf
->x
, dmabuf
->y
, dmabuf
->scanout_width
,
263 dmabuf
->scanout_height
, NULL
);
265 if (dmabuf
->allow_fences
) {
266 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
271 void gd_egl_cursor_dmabuf(DisplayChangeListener
*dcl
,
272 QemuDmaBuf
*dmabuf
, bool have_hot
,
273 uint32_t hot_x
, uint32_t hot_y
)
276 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
279 egl_dmabuf_import_texture(dmabuf
);
280 if (!dmabuf
->texture
) {
283 egl_fb_setup_for_tex(&vc
->gfx
.cursor_fb
, dmabuf
->width
, dmabuf
->height
,
284 dmabuf
->texture
, false);
286 egl_fb_destroy(&vc
->gfx
.cursor_fb
);
291 void gd_egl_cursor_position(DisplayChangeListener
*dcl
,
292 uint32_t pos_x
, uint32_t pos_y
)
294 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
296 vc
->gfx
.cursor_x
= pos_x
* vc
->gfx
.scale_x
;
297 vc
->gfx
.cursor_y
= pos_y
* vc
->gfx
.scale_y
;
300 void gd_egl_scanout_flush(DisplayChangeListener
*dcl
,
301 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
303 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
307 if (!vc
->gfx
.scanout_mode
) {
310 if (!vc
->gfx
.guest_fb
.framebuffer
) {
314 eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
315 vc
->gfx
.esurface
, vc
->gfx
.ectx
);
317 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
318 ww
= gdk_window_get_width(window
);
319 wh
= gdk_window_get_height(window
);
320 egl_fb_setup_default(&vc
->gfx
.win_fb
, ww
, wh
);
321 if (vc
->gfx
.cursor_fb
.texture
) {
322 egl_texture_blit(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
,
324 egl_texture_blend(vc
->gfx
.gls
, &vc
->gfx
.win_fb
, &vc
->gfx
.cursor_fb
,
326 vc
->gfx
.cursor_x
, vc
->gfx
.cursor_y
,
327 vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
329 egl_fb_blit(&vc
->gfx
.win_fb
, &vc
->gfx
.guest_fb
, !vc
->gfx
.y0_top
);
333 if (vc
->gfx
.guest_fb
.dmabuf
) {
334 egl_dmabuf_create_sync(vc
->gfx
.guest_fb
.dmabuf
);
338 eglSwapBuffers(qemu_egl_display
, vc
->gfx
.esurface
);
341 void gd_egl_flush(DisplayChangeListener
*dcl
,
342 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
344 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
345 GtkWidget
*area
= vc
->gfx
.drawing_area
;
347 if (vc
->gfx
.guest_fb
.dmabuf
&& !vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
) {
348 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
349 vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
= true;
350 gtk_widget_queue_draw_area(area
, x
, y
, w
, h
);
354 gd_egl_scanout_flush(&vc
->gfx
.dcl
, x
, y
, w
, h
);
357 void gtk_egl_init(DisplayGLMode mode
)
359 GdkDisplay
*gdk_display
= gdk_display_get_default();
360 Display
*x11_display
= gdk_x11_display_get_xdisplay(gdk_display
);
362 if (qemu_egl_init_dpy_x11(x11_display
, mode
) < 0) {
369 int gd_egl_make_current(DisplayGLCtx
*dgc
,
372 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
374 if (!eglMakeCurrent(qemu_egl_display
, vc
->gfx
.esurface
,
375 vc
->gfx
.esurface
, ctx
)) {
376 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());