BootLinuxSshTest: Test some userspace commands on Malta
[qemu/kevin.git] / ui / gtk-egl.c
blob42801b688bb56f37dacc3902a81d21faa5ef089e
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 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 int ww, wh;
68 if (!vc->gfx.gls) {
69 return;
72 window = gtk_widget_get_window(vc->gfx.drawing_area);
73 ww = gdk_window_get_width(window);
74 wh = gdk_window_get_height(window);
76 if (vc->gfx.scanout_mode) {
77 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
79 vc->gfx.scale_x = (double)ww / vc->gfx.w;
80 vc->gfx.scale_y = (double)wh / vc->gfx.h;
81 } else {
82 if (!vc->gfx.ds) {
83 return;
85 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
86 vc->gfx.esurface, vc->gfx.ectx);
88 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
89 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
91 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
93 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
94 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
98 void gd_egl_update(DisplayChangeListener *dcl,
99 int x, int y, int w, int h)
101 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
103 if (!vc->gfx.gls || !vc->gfx.ds) {
104 return;
107 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
108 vc->gfx.esurface, vc->gfx.ectx);
109 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
110 vc->gfx.glupdates++;
113 void gd_egl_refresh(DisplayChangeListener *dcl)
115 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
117 if (!vc->gfx.esurface) {
118 gd_egl_init(vc);
119 if (!vc->gfx.esurface) {
120 return;
122 vc->gfx.gls = qemu_gl_init_shader();
123 if (vc->gfx.ds) {
124 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
128 graphic_hw_update(dcl->con);
130 if (vc->gfx.glupdates) {
131 vc->gfx.glupdates = 0;
132 gtk_egl_set_scanout_mode(vc, false);
133 gd_egl_draw(vc);
137 void gd_egl_switch(DisplayChangeListener *dcl,
138 DisplaySurface *surface)
140 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
141 bool resized = true;
143 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
145 if (vc->gfx.ds &&
146 surface_width(vc->gfx.ds) == surface_width(surface) &&
147 surface_height(vc->gfx.ds) == surface_height(surface)) {
148 resized = false;
151 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
152 vc->gfx.ds = surface;
153 if (vc->gfx.gls) {
154 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
157 if (resized) {
158 gd_update_windowsize(vc);
162 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
163 QEMUGLParams *params)
165 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
167 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
168 vc->gfx.esurface, vc->gfx.ectx);
169 return qemu_egl_create_context(dcl, params);
172 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
174 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
176 vc->gfx.w = 0;
177 vc->gfx.h = 0;
178 gtk_egl_set_scanout_mode(vc, false);
181 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
182 uint32_t backing_id, bool backing_y_0_top,
183 uint32_t backing_width, uint32_t backing_height,
184 uint32_t x, uint32_t y,
185 uint32_t w, uint32_t h)
187 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
189 vc->gfx.x = x;
190 vc->gfx.y = y;
191 vc->gfx.w = w;
192 vc->gfx.h = h;
193 vc->gfx.y0_top = backing_y_0_top;
195 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
196 vc->gfx.esurface, vc->gfx.ectx);
198 gtk_egl_set_scanout_mode(vc, true);
199 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
200 backing_id, false);
203 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
204 QemuDmaBuf *dmabuf)
206 #ifdef CONFIG_OPENGL_DMABUF
207 egl_dmabuf_import_texture(dmabuf);
208 if (!dmabuf->texture) {
209 return;
212 gd_egl_scanout_texture(dcl, dmabuf->texture,
213 false, dmabuf->width, dmabuf->height,
214 0, 0, dmabuf->width, dmabuf->height);
215 #endif
218 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
219 QemuDmaBuf *dmabuf, bool have_hot,
220 uint32_t hot_x, uint32_t hot_y)
222 #ifdef CONFIG_OPENGL_DMABUF
223 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
225 if (dmabuf) {
226 egl_dmabuf_import_texture(dmabuf);
227 if (!dmabuf->texture) {
228 return;
230 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
231 dmabuf->texture, false);
232 } else {
233 egl_fb_destroy(&vc->gfx.cursor_fb);
235 #endif
238 void gd_egl_cursor_position(DisplayChangeListener *dcl,
239 uint32_t pos_x, uint32_t pos_y)
241 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
243 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
244 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
247 void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
248 QemuDmaBuf *dmabuf)
250 #ifdef CONFIG_OPENGL_DMABUF
251 egl_dmabuf_release_texture(dmabuf);
252 #endif
255 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
256 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
258 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
259 GdkWindow *window;
260 int ww, wh;
262 if (!vc->gfx.scanout_mode) {
263 return;
265 if (!vc->gfx.guest_fb.framebuffer) {
266 return;
269 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
270 vc->gfx.esurface, vc->gfx.ectx);
272 window = gtk_widget_get_window(vc->gfx.drawing_area);
273 ww = gdk_window_get_width(window);
274 wh = gdk_window_get_height(window);
275 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
276 if (vc->gfx.cursor_fb.texture) {
277 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
278 vc->gfx.y0_top);
279 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
280 vc->gfx.y0_top,
281 vc->gfx.cursor_x, vc->gfx.cursor_y,
282 vc->gfx.scale_x, vc->gfx.scale_y);
283 } else {
284 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
287 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
290 void gtk_egl_init(DisplayGLMode mode)
292 GdkDisplay *gdk_display = gdk_display_get_default();
293 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
295 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
296 return;
299 display_opengl = 1;
302 int gd_egl_make_current(DisplayChangeListener *dcl,
303 QEMUGLContext ctx)
305 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
307 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
308 vc->gfx.esurface, ctx);