linux-user: Split TARGET_PROT_* out of syscall_defs.h
[qemu/armbru.git] / ui / gtk-egl.c
blobd59b8cd7d714f649e2092e1cd35a21fdb73cade6
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"
16 #include "qemu/error-report.h"
18 #include "trace.h"
20 #include "ui/console.h"
21 #include "ui/gtk.h"
22 #include "ui/egl-helpers.h"
23 #include "ui/shader.h"
25 #include "sysemu/sysemu.h"
27 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
29 if (vc->gfx.scanout_mode == scanout) {
30 return;
33 vc->gfx.scanout_mode = scanout;
34 if (!vc->gfx.scanout_mode) {
35 egl_fb_destroy(&vc->gfx.guest_fb);
36 if (vc->gfx.surface) {
37 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
38 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
43 /** DisplayState Callbacks (opengl version) **/
45 void gd_egl_init(VirtualConsole *vc)
47 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
48 if (!gdk_window) {
49 return;
52 Window x11_window = gdk_x11_window_get_xid(gdk_window);
53 if (!x11_window) {
54 return;
57 vc->gfx.ectx = qemu_egl_init_ctx();
58 vc->gfx.esurface = qemu_egl_init_surface_x11
59 (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
61 assert(vc->gfx.esurface);
64 void gd_egl_draw(VirtualConsole *vc)
66 GdkWindow *window;
67 #ifdef CONFIG_GBM
68 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
69 #endif
70 int ww, wh;
72 if (!vc->gfx.gls) {
73 return;
76 window = gtk_widget_get_window(vc->gfx.drawing_area);
77 ww = gdk_window_get_width(window);
78 wh = gdk_window_get_height(window);
80 if (vc->gfx.scanout_mode) {
81 #ifdef CONFIG_GBM
82 if (dmabuf) {
83 if (!dmabuf->draw_submitted) {
84 return;
85 } else {
86 dmabuf->draw_submitted = false;
89 #endif
90 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
92 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
93 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
95 glFlush();
96 #ifdef CONFIG_GBM
97 if (dmabuf) {
98 egl_dmabuf_create_fence(dmabuf);
99 if (dmabuf->fence_fd > 0) {
100 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
101 return;
103 graphic_hw_gl_block(vc->gfx.dcl.con, false);
105 #endif
106 } else {
107 if (!vc->gfx.ds) {
108 return;
110 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
111 vc->gfx.esurface, vc->gfx.ectx);
113 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
114 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
116 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
118 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
119 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
121 glFlush();
125 void gd_egl_update(DisplayChangeListener *dcl,
126 int x, int y, int w, int h)
128 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
130 if (!vc->gfx.gls || !vc->gfx.ds) {
131 return;
134 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
135 vc->gfx.esurface, vc->gfx.ectx);
136 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
137 vc->gfx.glupdates++;
140 void gd_egl_refresh(DisplayChangeListener *dcl)
142 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
144 gd_update_monitor_refresh_rate(
145 vc, vc->window ? vc->window : vc->gfx.drawing_area);
147 if (!vc->gfx.esurface) {
148 gd_egl_init(vc);
149 if (!vc->gfx.esurface) {
150 return;
152 vc->gfx.gls = qemu_gl_init_shader();
153 if (vc->gfx.ds) {
154 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
155 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
157 #ifdef CONFIG_GBM
158 if (vc->gfx.guest_fb.dmabuf) {
159 egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
160 gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
162 #endif
165 graphic_hw_update(dcl->con);
167 if (vc->gfx.glupdates) {
168 vc->gfx.glupdates = 0;
169 gtk_egl_set_scanout_mode(vc, false);
170 gd_egl_draw(vc);
174 void gd_egl_switch(DisplayChangeListener *dcl,
175 DisplaySurface *surface)
177 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
178 bool resized = true;
180 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
182 if (vc->gfx.ds &&
183 surface_width(vc->gfx.ds) == surface_width(surface) &&
184 surface_height(vc->gfx.ds) == surface_height(surface)) {
185 resized = false;
187 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
188 vc->gfx.esurface, vc->gfx.ectx);
190 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
191 vc->gfx.ds = surface;
192 if (vc->gfx.gls) {
193 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
196 if (resized) {
197 gd_update_windowsize(vc);
200 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
201 EGL_NO_CONTEXT);
204 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
205 QEMUGLParams *params)
207 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
209 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
210 vc->gfx.esurface, vc->gfx.ectx);
211 return qemu_egl_create_context(dgc, params);
214 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
216 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
218 vc->gfx.w = 0;
219 vc->gfx.h = 0;
220 gtk_egl_set_scanout_mode(vc, false);
223 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
224 uint32_t backing_id, bool backing_y_0_top,
225 uint32_t backing_width, uint32_t backing_height,
226 uint32_t x, uint32_t y,
227 uint32_t w, uint32_t h,
228 void *d3d_tex2d)
230 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
232 vc->gfx.x = x;
233 vc->gfx.y = y;
234 vc->gfx.w = w;
235 vc->gfx.h = h;
236 vc->gfx.y0_top = backing_y_0_top;
238 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
239 vc->gfx.esurface, vc->gfx.ectx);
241 gtk_egl_set_scanout_mode(vc, true);
242 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
243 backing_id, false);
246 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
247 QemuDmaBuf *dmabuf)
249 #ifdef CONFIG_GBM
250 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
252 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
253 vc->gfx.esurface, vc->gfx.ectx);
255 egl_dmabuf_import_texture(dmabuf);
256 if (!dmabuf->texture) {
257 return;
260 gd_egl_scanout_texture(dcl, dmabuf->texture,
261 dmabuf->y0_top, dmabuf->width, dmabuf->height,
262 dmabuf->x, dmabuf->y, dmabuf->scanout_width,
263 dmabuf->scanout_height, NULL);
265 if (dmabuf->allow_fences) {
266 vc->gfx.guest_fb.dmabuf = dmabuf;
268 #endif
271 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
272 QemuDmaBuf *dmabuf, bool have_hot,
273 uint32_t hot_x, uint32_t hot_y)
275 #ifdef CONFIG_GBM
276 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
278 if (dmabuf) {
279 egl_dmabuf_import_texture(dmabuf);
280 if (!dmabuf->texture) {
281 return;
283 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
284 dmabuf->texture, false);
285 } else {
286 egl_fb_destroy(&vc->gfx.cursor_fb);
288 #endif
291 void gd_egl_cursor_position(DisplayChangeListener *dcl,
292 uint32_t pos_x, uint32_t pos_y)
294 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
296 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
297 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
300 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
301 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
303 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
304 GdkWindow *window;
305 int ww, wh;
307 if (!vc->gfx.scanout_mode) {
308 return;
310 if (!vc->gfx.guest_fb.framebuffer) {
311 return;
314 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
315 vc->gfx.esurface, vc->gfx.ectx);
317 window = gtk_widget_get_window(vc->gfx.drawing_area);
318 ww = gdk_window_get_width(window);
319 wh = gdk_window_get_height(window);
320 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
321 if (vc->gfx.cursor_fb.texture) {
322 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
323 vc->gfx.y0_top);
324 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
325 vc->gfx.y0_top,
326 vc->gfx.cursor_x, vc->gfx.cursor_y,
327 vc->gfx.scale_x, vc->gfx.scale_y);
328 } else {
329 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
332 #ifdef CONFIG_GBM
333 if (vc->gfx.guest_fb.dmabuf) {
334 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
336 #endif
338 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
341 void gd_egl_flush(DisplayChangeListener *dcl,
342 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
344 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
345 GtkWidget *area = vc->gfx.drawing_area;
347 if (vc->gfx.guest_fb.dmabuf && !vc->gfx.guest_fb.dmabuf->draw_submitted) {
348 graphic_hw_gl_block(vc->gfx.dcl.con, true);
349 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
350 gtk_widget_queue_draw_area(area, x, y, w, h);
351 return;
354 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
357 void gtk_egl_init(DisplayGLMode mode)
359 GdkDisplay *gdk_display = gdk_display_get_default();
360 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
362 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
363 return;
366 display_opengl = 1;
369 int gd_egl_make_current(DisplayGLCtx *dgc,
370 QEMUGLContext ctx)
372 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
374 if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
375 vc->gfx.esurface, ctx)) {
376 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());
377 return -1;
380 return 0;