virtio-balloon: correct used length
[qemu/kevin.git] / ui / gtk-egl.c
blob45cb67712df03aff9db65a549118f6e397aea243
1 /*
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"
17 #include "trace.h"
19 #include "ui/console.h"
20 #include "ui/gtk.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) {
29 return;
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);
47 if (!gdk_window) {
48 return;
51 Window x11_window = gdk_x11_window_get_xid(gdk_window);
52 if (!x11_window) {
53 return;
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)
65 GdkWindow *window;
66 #ifdef CONFIG_GBM
67 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
68 #endif
69 int ww, wh;
71 if (!vc->gfx.gls) {
72 return;
75 window = gtk_widget_get_window(vc->gfx.drawing_area);
76 ww = gdk_window_get_width(window);
77 wh = gdk_window_get_height(window);
79 if (vc->gfx.scanout_mode) {
80 #ifdef CONFIG_GBM
81 if (dmabuf) {
82 if (!dmabuf->draw_submitted) {
83 return;
84 } else {
85 dmabuf->draw_submitted = false;
88 #endif
89 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
91 vc->gfx.scale_x = (double)ww / vc->gfx.w;
92 vc->gfx.scale_y = (double)wh / vc->gfx.h;
94 glFlush();
95 #ifdef CONFIG_GBM
96 if (dmabuf) {
97 egl_dmabuf_create_fence(dmabuf);
98 if (dmabuf->fence_fd > 0) {
99 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
100 return;
102 graphic_hw_gl_block(vc->gfx.dcl.con, false);
104 #endif
105 } else {
106 if (!vc->gfx.ds) {
107 return;
109 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
110 vc->gfx.esurface, vc->gfx.ectx);
112 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
113 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
115 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
117 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
118 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
120 glFlush();
123 graphic_hw_gl_flushed(vc->gfx.dcl.con);
126 void gd_egl_update(DisplayChangeListener *dcl,
127 int x, int y, int w, int h)
129 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
131 if (!vc->gfx.gls || !vc->gfx.ds) {
132 return;
135 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
136 vc->gfx.esurface, vc->gfx.ectx);
137 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
138 vc->gfx.glupdates++;
141 void gd_egl_refresh(DisplayChangeListener *dcl)
143 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
145 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
146 vc->window ? vc->window : vc->gfx.drawing_area);
148 if (!vc->gfx.esurface) {
149 gd_egl_init(vc);
150 if (!vc->gfx.esurface) {
151 return;
153 vc->gfx.gls = qemu_gl_init_shader();
154 if (vc->gfx.ds) {
155 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
156 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
158 #ifdef CONFIG_GBM
159 if (vc->gfx.guest_fb.dmabuf) {
160 egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
161 gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
163 #endif
166 graphic_hw_update(dcl->con);
168 if (vc->gfx.glupdates) {
169 vc->gfx.glupdates = 0;
170 gtk_egl_set_scanout_mode(vc, false);
171 gd_egl_draw(vc);
175 void gd_egl_switch(DisplayChangeListener *dcl,
176 DisplaySurface *surface)
178 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
179 bool resized = true;
181 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
183 if (vc->gfx.ds &&
184 surface_width(vc->gfx.ds) == surface_width(surface) &&
185 surface_height(vc->gfx.ds) == surface_height(surface)) {
186 resized = false;
188 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
189 vc->gfx.esurface, vc->gfx.ectx);
191 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
192 vc->gfx.ds = surface;
193 if (vc->gfx.gls) {
194 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
197 if (resized) {
198 gd_update_windowsize(vc);
202 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
203 QEMUGLParams *params)
205 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
207 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
208 vc->gfx.esurface, vc->gfx.ectx);
209 return qemu_egl_create_context(dcl, params);
212 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
214 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
216 vc->gfx.w = 0;
217 vc->gfx.h = 0;
218 gtk_egl_set_scanout_mode(vc, false);
221 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
222 uint32_t backing_id, bool backing_y_0_top,
223 uint32_t backing_width, uint32_t backing_height,
224 uint32_t x, uint32_t y,
225 uint32_t w, uint32_t h)
227 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
229 vc->gfx.x = x;
230 vc->gfx.y = y;
231 vc->gfx.w = w;
232 vc->gfx.h = h;
233 vc->gfx.y0_top = backing_y_0_top;
235 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
236 vc->gfx.esurface, vc->gfx.ectx);
238 gtk_egl_set_scanout_mode(vc, true);
239 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
240 backing_id, false);
243 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
244 QemuDmaBuf *dmabuf)
246 #ifdef CONFIG_GBM
247 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
249 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
250 vc->gfx.esurface, vc->gfx.ectx);
252 egl_dmabuf_import_texture(dmabuf);
253 if (!dmabuf->texture) {
254 return;
257 gd_egl_scanout_texture(dcl, dmabuf->texture,
258 false, dmabuf->width, dmabuf->height,
259 0, 0, dmabuf->width, dmabuf->height);
261 if (dmabuf->allow_fences) {
262 vc->gfx.guest_fb.dmabuf = dmabuf;
264 #endif
267 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
268 QemuDmaBuf *dmabuf, bool have_hot,
269 uint32_t hot_x, uint32_t hot_y)
271 #ifdef CONFIG_GBM
272 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
274 if (dmabuf) {
275 egl_dmabuf_import_texture(dmabuf);
276 if (!dmabuf->texture) {
277 return;
279 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
280 dmabuf->texture, false);
281 } else {
282 egl_fb_destroy(&vc->gfx.cursor_fb);
284 #endif
287 void gd_egl_cursor_position(DisplayChangeListener *dcl,
288 uint32_t pos_x, uint32_t pos_y)
290 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
292 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
293 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
296 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
297 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
299 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
300 GdkWindow *window;
301 int ww, wh;
303 if (!vc->gfx.scanout_mode) {
304 return;
306 if (!vc->gfx.guest_fb.framebuffer) {
307 return;
310 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
311 vc->gfx.esurface, vc->gfx.ectx);
313 window = gtk_widget_get_window(vc->gfx.drawing_area);
314 ww = gdk_window_get_width(window);
315 wh = gdk_window_get_height(window);
316 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
317 if (vc->gfx.cursor_fb.texture) {
318 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
319 vc->gfx.y0_top);
320 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
321 vc->gfx.y0_top,
322 vc->gfx.cursor_x, vc->gfx.cursor_y,
323 vc->gfx.scale_x, vc->gfx.scale_y);
324 } else {
325 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
328 #ifdef CONFIG_GBM
329 if (vc->gfx.guest_fb.dmabuf) {
330 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
332 #endif
334 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
337 void gd_egl_flush(DisplayChangeListener *dcl,
338 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
340 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
341 GtkWidget *area = vc->gfx.drawing_area;
343 if (vc->gfx.guest_fb.dmabuf) {
344 graphic_hw_gl_block(vc->gfx.dcl.con, true);
345 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
346 gtk_widget_queue_draw_area(area, x, y, w, h);
347 return;
350 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
353 void gtk_egl_init(DisplayGLMode mode)
355 GdkDisplay *gdk_display = gdk_display_get_default();
356 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
358 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
359 return;
362 display_opengl = 1;
365 int gd_egl_make_current(DisplayChangeListener *dcl,
366 QEMUGLContext ctx)
368 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
370 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
371 vc->gfx.esurface, ctx);