monitor: Fix tracepoint crash on JSON syntax error
[qemu/armbru.git] / ui / gtk-egl.c
blobfb00ad12ecb738aa08617b7ca565d64932dbfef5
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-common.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 #if GTK_CHECK_VERSION(3, 0, 0)
52 Window x11_window = gdk_x11_window_get_xid(gdk_window);
53 #else
54 Window x11_window = gdk_x11_drawable_get_xid(gdk_window);
55 #endif
56 if (!x11_window) {
57 return;
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)
68 GdkWindow *window;
69 int ww, wh;
71 if (!vc->gfx.gls) {
72 return;
75 if (vc->gfx.scanout_mode) {
76 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
77 } else {
78 if (!vc->gfx.ds) {
79 return;
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) {
99 return;
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);
105 vc->gfx.glupdates++;
108 void gd_egl_refresh(DisplayChangeListener *dcl)
110 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
112 if (!vc->gfx.esurface) {
113 gd_egl_init(vc);
114 if (!vc->gfx.esurface) {
115 return;
117 vc->gfx.gls = qemu_gl_init_shader();
118 if (vc->gfx.ds) {
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);
128 gd_egl_draw(vc);
132 void gd_egl_switch(DisplayChangeListener *dcl,
133 DisplaySurface *surface)
135 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
136 bool resized = true;
138 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
140 if (vc->gfx.ds &&
141 surface_width(vc->gfx.ds) == surface_width(surface) &&
142 surface_height(vc->gfx.ds) == surface_height(surface)) {
143 resized = false;
146 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
147 vc->gfx.ds = surface;
148 if (vc->gfx.gls) {
149 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
152 if (resized) {
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);
171 vc->gfx.w = 0;
172 vc->gfx.h = 0;
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);
184 vc->gfx.x = x;
185 vc->gfx.y = y;
186 vc->gfx.w = w;
187 vc->gfx.h = h;
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,
195 backing_id, false);
198 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
199 QemuDmaBuf *dmabuf)
201 #ifdef CONFIG_OPENGL_DMABUF
202 egl_dmabuf_import_texture(dmabuf);
203 if (!dmabuf->texture) {
204 return;
207 gd_egl_scanout_texture(dcl, dmabuf->texture,
208 false, dmabuf->width, dmabuf->height,
209 0, 0, dmabuf->width, dmabuf->height);
210 #endif
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);
220 if (dmabuf) {
221 egl_dmabuf_import_texture(dmabuf);
222 if (!dmabuf->texture) {
223 return;
225 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
226 dmabuf->texture, false);
227 } else {
228 egl_fb_destroy(&vc->gfx.cursor_fb);
230 #endif
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,
243 QemuDmaBuf *dmabuf)
245 #ifdef CONFIG_OPENGL_DMABUF
246 egl_dmabuf_release_texture(dmabuf);
247 #endif
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);
254 GdkWindow *window;
255 int ww, wh;
257 if (!vc->gfx.scanout_mode) {
258 return;
260 if (!vc->gfx.guest_fb.framebuffer) {
261 return;
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,
272 vc->gfx.y0_top);
273 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
274 vc->gfx.y0_top,
275 vc->gfx.cursor_x, vc->gfx.cursor_y);
276 } else {
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) {
289 return;
292 display_opengl = 1;
295 int gd_egl_make_current(DisplayChangeListener *dcl,
296 QEMUGLContext ctx)
298 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
300 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
301 vc->gfx.esurface, ctx);