xen: duplicate xen_disk.c as basis of dataplane/xen-block.c
[qemu.git] / ui / gtk-egl.c
blob5420c2362bb1887263eb867b216728b6d3c1e4d5
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(vc->gfx.ectx, x11_window);
59 assert(vc->gfx.esurface);
62 void gd_egl_draw(VirtualConsole *vc)
64 GdkWindow *window;
65 int ww, wh;
67 if (!vc->gfx.gls) {
68 return;
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;
80 } else {
81 if (!vc->gfx.ds) {
82 return;
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) {
103 return;
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);
109 vc->gfx.glupdates++;
112 void gd_egl_refresh(DisplayChangeListener *dcl)
114 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
116 if (!vc->gfx.esurface) {
117 gd_egl_init(vc);
118 if (!vc->gfx.esurface) {
119 return;
121 vc->gfx.gls = qemu_gl_init_shader();
122 if (vc->gfx.ds) {
123 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
127 graphic_hw_update(dcl->con);
129 if (vc->gfx.glupdates) {
130 vc->gfx.glupdates = 0;
131 gtk_egl_set_scanout_mode(vc, false);
132 gd_egl_draw(vc);
136 void gd_egl_switch(DisplayChangeListener *dcl,
137 DisplaySurface *surface)
139 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
140 bool resized = true;
142 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
144 if (vc->gfx.ds &&
145 surface_width(vc->gfx.ds) == surface_width(surface) &&
146 surface_height(vc->gfx.ds) == surface_height(surface)) {
147 resized = false;
150 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
151 vc->gfx.ds = surface;
152 if (vc->gfx.gls) {
153 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
156 if (resized) {
157 gd_update_windowsize(vc);
161 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
162 QEMUGLParams *params)
164 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
166 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
167 vc->gfx.esurface, vc->gfx.ectx);
168 return qemu_egl_create_context(dcl, params);
171 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
173 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
175 vc->gfx.w = 0;
176 vc->gfx.h = 0;
177 gtk_egl_set_scanout_mode(vc, false);
180 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
181 uint32_t backing_id, bool backing_y_0_top,
182 uint32_t backing_width, uint32_t backing_height,
183 uint32_t x, uint32_t y,
184 uint32_t w, uint32_t h)
186 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
188 vc->gfx.x = x;
189 vc->gfx.y = y;
190 vc->gfx.w = w;
191 vc->gfx.h = h;
192 vc->gfx.y0_top = backing_y_0_top;
194 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
195 vc->gfx.esurface, vc->gfx.ectx);
197 gtk_egl_set_scanout_mode(vc, true);
198 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
199 backing_id, false);
202 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
203 QemuDmaBuf *dmabuf)
205 #ifdef CONFIG_OPENGL_DMABUF
206 egl_dmabuf_import_texture(dmabuf);
207 if (!dmabuf->texture) {
208 return;
211 gd_egl_scanout_texture(dcl, dmabuf->texture,
212 false, dmabuf->width, dmabuf->height,
213 0, 0, dmabuf->width, dmabuf->height);
214 #endif
217 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
218 QemuDmaBuf *dmabuf, bool have_hot,
219 uint32_t hot_x, uint32_t hot_y)
221 #ifdef CONFIG_OPENGL_DMABUF
222 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
224 if (dmabuf) {
225 egl_dmabuf_import_texture(dmabuf);
226 if (!dmabuf->texture) {
227 return;
229 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
230 dmabuf->texture, false);
231 } else {
232 egl_fb_destroy(&vc->gfx.cursor_fb);
234 #endif
237 void gd_egl_cursor_position(DisplayChangeListener *dcl,
238 uint32_t pos_x, uint32_t pos_y)
240 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
242 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
243 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
246 void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
247 QemuDmaBuf *dmabuf)
249 #ifdef CONFIG_OPENGL_DMABUF
250 egl_dmabuf_release_texture(dmabuf);
251 #endif
254 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
255 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
257 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
258 GdkWindow *window;
259 int ww, wh;
261 if (!vc->gfx.scanout_mode) {
262 return;
264 if (!vc->gfx.guest_fb.framebuffer) {
265 return;
268 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
269 vc->gfx.esurface, vc->gfx.ectx);
271 window = gtk_widget_get_window(vc->gfx.drawing_area);
272 ww = gdk_window_get_width(window);
273 wh = gdk_window_get_height(window);
274 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
275 if (vc->gfx.cursor_fb.texture) {
276 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
277 vc->gfx.y0_top);
278 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
279 vc->gfx.y0_top,
280 vc->gfx.cursor_x, vc->gfx.cursor_y);
281 } else {
282 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
285 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
288 void gtk_egl_init(DisplayGLMode mode)
290 GdkDisplay *gdk_display = gdk_display_get_default();
291 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
293 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
294 return;
297 display_opengl = 1;
300 int gd_egl_make_current(DisplayChangeListener *dcl,
301 QEMUGLContext ctx)
303 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
305 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
306 vc->gfx.esurface, ctx);