acpi: build_hpet: use acpi_table_begin()/acpi_table_end() instead of build_header()
[qemu.git] / ui / gtk-egl.c
blob72ce5e1f8f4199dd723fc785e5e2b7ddac4d9666
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 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);
97 glFlush();
98 #ifdef CONFIG_GBM
99 if (vc->gfx.guest_fb.dmabuf) {
100 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
102 egl_dmabuf_create_fence(dmabuf);
103 if (dmabuf->fence_fd > 0) {
104 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
105 return;
107 graphic_hw_gl_block(vc->gfx.dcl.con, false);
109 #endif
110 graphic_hw_gl_flushed(vc->gfx.dcl.con);
113 void gd_egl_update(DisplayChangeListener *dcl,
114 int x, int y, int w, int h)
116 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
118 if (!vc->gfx.gls || !vc->gfx.ds) {
119 return;
122 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
123 vc->gfx.esurface, vc->gfx.ectx);
124 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
125 vc->gfx.glupdates++;
128 void gd_egl_refresh(DisplayChangeListener *dcl)
130 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
132 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
133 vc->window ? vc->window : vc->gfx.drawing_area);
135 if (!vc->gfx.esurface) {
136 gd_egl_init(vc);
137 if (!vc->gfx.esurface) {
138 return;
140 vc->gfx.gls = qemu_gl_init_shader();
141 if (vc->gfx.ds) {
142 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
146 graphic_hw_update(dcl->con);
148 if (vc->gfx.glupdates) {
149 vc->gfx.glupdates = 0;
150 gtk_egl_set_scanout_mode(vc, false);
151 gd_egl_draw(vc);
155 void gd_egl_switch(DisplayChangeListener *dcl,
156 DisplaySurface *surface)
158 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
159 bool resized = true;
161 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
163 if (vc->gfx.ds &&
164 surface_width(vc->gfx.ds) == surface_width(surface) &&
165 surface_height(vc->gfx.ds) == surface_height(surface)) {
166 resized = false;
169 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
170 vc->gfx.ds = surface;
171 if (vc->gfx.gls) {
172 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
175 if (resized) {
176 gd_update_windowsize(vc);
180 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
181 QEMUGLParams *params)
183 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
185 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
186 vc->gfx.esurface, vc->gfx.ectx);
187 return qemu_egl_create_context(dcl, params);
190 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
192 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
194 vc->gfx.w = 0;
195 vc->gfx.h = 0;
196 gtk_egl_set_scanout_mode(vc, false);
199 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
200 uint32_t backing_id, bool backing_y_0_top,
201 uint32_t backing_width, uint32_t backing_height,
202 uint32_t x, uint32_t y,
203 uint32_t w, uint32_t h)
205 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
207 vc->gfx.x = x;
208 vc->gfx.y = y;
209 vc->gfx.w = w;
210 vc->gfx.h = h;
211 vc->gfx.y0_top = backing_y_0_top;
213 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
214 vc->gfx.esurface, vc->gfx.ectx);
216 gtk_egl_set_scanout_mode(vc, true);
217 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
218 backing_id, false);
221 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
222 QemuDmaBuf *dmabuf)
224 #ifdef CONFIG_GBM
225 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
227 egl_dmabuf_import_texture(dmabuf);
228 if (!dmabuf->texture) {
229 return;
232 gd_egl_scanout_texture(dcl, dmabuf->texture,
233 false, dmabuf->width, dmabuf->height,
234 0, 0, dmabuf->width, dmabuf->height);
236 if (dmabuf->allow_fences) {
237 vc->gfx.guest_fb.dmabuf = dmabuf;
239 #endif
242 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
243 QemuDmaBuf *dmabuf, bool have_hot,
244 uint32_t hot_x, uint32_t hot_y)
246 #ifdef CONFIG_GBM
247 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
249 if (dmabuf) {
250 egl_dmabuf_import_texture(dmabuf);
251 if (!dmabuf->texture) {
252 return;
254 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
255 dmabuf->texture, false);
256 } else {
257 egl_fb_destroy(&vc->gfx.cursor_fb);
259 #endif
262 void gd_egl_cursor_position(DisplayChangeListener *dcl,
263 uint32_t pos_x, uint32_t pos_y)
265 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
267 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
268 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
271 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
272 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
274 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
275 GdkWindow *window;
276 int ww, wh;
278 if (!vc->gfx.scanout_mode) {
279 return;
281 if (!vc->gfx.guest_fb.framebuffer) {
282 return;
285 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
286 vc->gfx.esurface, vc->gfx.ectx);
288 window = gtk_widget_get_window(vc->gfx.drawing_area);
289 ww = gdk_window_get_width(window);
290 wh = gdk_window_get_height(window);
291 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
292 if (vc->gfx.cursor_fb.texture) {
293 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
294 vc->gfx.y0_top);
295 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
296 vc->gfx.y0_top,
297 vc->gfx.cursor_x, vc->gfx.cursor_y,
298 vc->gfx.scale_x, vc->gfx.scale_y);
299 } else {
300 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
303 #ifdef CONFIG_GBM
304 if (vc->gfx.guest_fb.dmabuf) {
305 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
307 #endif
309 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
312 void gd_egl_flush(DisplayChangeListener *dcl,
313 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
315 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
316 GtkWidget *area = vc->gfx.drawing_area;
318 if (vc->gfx.guest_fb.dmabuf) {
319 graphic_hw_gl_block(vc->gfx.dcl.con, true);
320 gtk_widget_queue_draw_area(area, x, y, w, h);
321 return;
324 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
327 void gtk_egl_init(DisplayGLMode mode)
329 GdkDisplay *gdk_display = gdk_display_get_default();
330 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
332 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
333 return;
336 display_opengl = 1;
339 int gd_egl_make_current(DisplayChangeListener *dcl,
340 QEMUGLContext ctx)
342 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
344 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
345 vc->gfx.esurface, ctx);