]> git.proxmox.com Git - mirror_qemu.git/blob - ui/gtk-egl.c
ui/gtk-egl: make sure the right context is set as the current
[mirror_qemu.git] / ui / gtk-egl.c
1 /*
2 * GTK UI -- egl opengl code.
3 *
4 * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget,
5 * which is GtkDrawingArea like widget with opengl rendering support.
6 *
7 * This code handles opengl support on older gtk versions, using egl
8 * to get a opengl context for the X11 window.
9 *
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.
12 */
13
14 #include "qemu/osdep.h"
15 #include "qemu/main-loop.h"
16
17 #include "trace.h"
18
19 #include "ui/console.h"
20 #include "ui/gtk.h"
21 #include "ui/egl-helpers.h"
22 #include "ui/shader.h"
23
24 #include "sysemu/sysemu.h"
25
26 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
27 {
28 if (vc->gfx.scanout_mode == scanout) {
29 return;
30 }
31
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);
38 }
39 }
40 }
41
42 /** DisplayState Callbacks (opengl version) **/
43
44 void gd_egl_init(VirtualConsole *vc)
45 {
46 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
47 if (!gdk_window) {
48 return;
49 }
50
51 Window x11_window = gdk_x11_window_get_xid(gdk_window);
52 if (!x11_window) {
53 return;
54 }
55
56 vc->gfx.ectx = qemu_egl_init_ctx();
57 vc->gfx.esurface = qemu_egl_init_surface_x11
58 (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
59
60 assert(vc->gfx.esurface);
61 }
62
63 void gd_egl_draw(VirtualConsole *vc)
64 {
65 GdkWindow *window;
66 #ifdef CONFIG_GBM
67 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
68 #endif
69 int ww, wh;
70
71 if (!vc->gfx.gls) {
72 return;
73 }
74
75 window = gtk_widget_get_window(vc->gfx.drawing_area);
76 ww = gdk_window_get_width(window);
77 wh = gdk_window_get_height(window);
78
79 if (vc->gfx.scanout_mode) {
80 #ifdef CONFIG_GBM
81 if (dmabuf) {
82 if (!dmabuf->draw_submitted) {
83 return;
84 } else {
85 dmabuf->draw_submitted = false;
86 }
87 }
88 #endif
89 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
90
91 vc->gfx.scale_x = (double)ww / vc->gfx.w;
92 vc->gfx.scale_y = (double)wh / vc->gfx.h;
93
94 glFlush();
95 #ifdef CONFIG_GBM
96 if (dmabuf) {
97 egl_dmabuf_create_fence(dmabuf);
98 if (dmabuf->fence_fd > 0) {
99 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
100 return;
101 }
102 graphic_hw_gl_block(vc->gfx.dcl.con, false);
103 }
104 #endif
105 } else {
106 if (!vc->gfx.ds) {
107 return;
108 }
109 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
110 vc->gfx.esurface, vc->gfx.ectx);
111
112 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
113 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
114
115 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
116
117 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
118 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
119
120 glFlush();
121 }
122
123 graphic_hw_gl_flushed(vc->gfx.dcl.con);
124 }
125
126 void gd_egl_update(DisplayChangeListener *dcl,
127 int x, int y, int w, int h)
128 {
129 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
130
131 if (!vc->gfx.gls || !vc->gfx.ds) {
132 return;
133 }
134
135 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
136 vc->gfx.esurface, vc->gfx.ectx);
137 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
138 vc->gfx.glupdates++;
139 }
140
141 void gd_egl_refresh(DisplayChangeListener *dcl)
142 {
143 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
144
145 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
146 vc->window ? vc->window : vc->gfx.drawing_area);
147
148 if (!vc->gfx.esurface) {
149 gd_egl_init(vc);
150 if (!vc->gfx.esurface) {
151 return;
152 }
153 vc->gfx.gls = qemu_gl_init_shader();
154 if (vc->gfx.ds) {
155 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
156 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
157 }
158 }
159
160 graphic_hw_update(dcl->con);
161
162 if (vc->gfx.glupdates) {
163 vc->gfx.glupdates = 0;
164 gtk_egl_set_scanout_mode(vc, false);
165 gd_egl_draw(vc);
166 }
167 }
168
169 void gd_egl_switch(DisplayChangeListener *dcl,
170 DisplaySurface *surface)
171 {
172 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
173 bool resized = true;
174
175 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
176
177 if (vc->gfx.ds &&
178 surface_width(vc->gfx.ds) == surface_width(surface) &&
179 surface_height(vc->gfx.ds) == surface_height(surface)) {
180 resized = false;
181 }
182 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
183 vc->gfx.esurface, vc->gfx.ectx);
184
185 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
186 vc->gfx.ds = surface;
187 if (vc->gfx.gls) {
188 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
189 }
190
191 if (resized) {
192 gd_update_windowsize(vc);
193 }
194 }
195
196 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
197 QEMUGLParams *params)
198 {
199 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
200
201 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
202 vc->gfx.esurface, vc->gfx.ectx);
203 return qemu_egl_create_context(dcl, params);
204 }
205
206 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
207 {
208 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
209
210 vc->gfx.w = 0;
211 vc->gfx.h = 0;
212 gtk_egl_set_scanout_mode(vc, false);
213 }
214
215 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
216 uint32_t backing_id, bool backing_y_0_top,
217 uint32_t backing_width, uint32_t backing_height,
218 uint32_t x, uint32_t y,
219 uint32_t w, uint32_t h)
220 {
221 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
222
223 vc->gfx.x = x;
224 vc->gfx.y = y;
225 vc->gfx.w = w;
226 vc->gfx.h = h;
227 vc->gfx.y0_top = backing_y_0_top;
228
229 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
230 vc->gfx.esurface, vc->gfx.ectx);
231
232 gtk_egl_set_scanout_mode(vc, true);
233 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
234 backing_id, false);
235 }
236
237 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
238 QemuDmaBuf *dmabuf)
239 {
240 #ifdef CONFIG_GBM
241 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
242
243 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
244 vc->gfx.esurface, vc->gfx.ectx);
245
246 egl_dmabuf_import_texture(dmabuf);
247 if (!dmabuf->texture) {
248 return;
249 }
250
251 gd_egl_scanout_texture(dcl, dmabuf->texture,
252 false, dmabuf->width, dmabuf->height,
253 0, 0, dmabuf->width, dmabuf->height);
254
255 if (dmabuf->allow_fences) {
256 vc->gfx.guest_fb.dmabuf = dmabuf;
257 }
258 #endif
259 }
260
261 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
262 QemuDmaBuf *dmabuf, bool have_hot,
263 uint32_t hot_x, uint32_t hot_y)
264 {
265 #ifdef CONFIG_GBM
266 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
267
268 if (dmabuf) {
269 egl_dmabuf_import_texture(dmabuf);
270 if (!dmabuf->texture) {
271 return;
272 }
273 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
274 dmabuf->texture, false);
275 } else {
276 egl_fb_destroy(&vc->gfx.cursor_fb);
277 }
278 #endif
279 }
280
281 void gd_egl_cursor_position(DisplayChangeListener *dcl,
282 uint32_t pos_x, uint32_t pos_y)
283 {
284 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
285
286 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
287 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
288 }
289
290 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
291 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
292 {
293 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
294 GdkWindow *window;
295 int ww, wh;
296
297 if (!vc->gfx.scanout_mode) {
298 return;
299 }
300 if (!vc->gfx.guest_fb.framebuffer) {
301 return;
302 }
303
304 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
305 vc->gfx.esurface, vc->gfx.ectx);
306
307 window = gtk_widget_get_window(vc->gfx.drawing_area);
308 ww = gdk_window_get_width(window);
309 wh = gdk_window_get_height(window);
310 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
311 if (vc->gfx.cursor_fb.texture) {
312 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
313 vc->gfx.y0_top);
314 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
315 vc->gfx.y0_top,
316 vc->gfx.cursor_x, vc->gfx.cursor_y,
317 vc->gfx.scale_x, vc->gfx.scale_y);
318 } else {
319 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
320 }
321
322 #ifdef CONFIG_GBM
323 if (vc->gfx.guest_fb.dmabuf) {
324 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
325 }
326 #endif
327
328 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
329 }
330
331 void gd_egl_flush(DisplayChangeListener *dcl,
332 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
333 {
334 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
335 GtkWidget *area = vc->gfx.drawing_area;
336
337 if (vc->gfx.guest_fb.dmabuf) {
338 graphic_hw_gl_block(vc->gfx.dcl.con, true);
339 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
340 gtk_widget_queue_draw_area(area, x, y, w, h);
341 return;
342 }
343
344 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
345 }
346
347 void gtk_egl_init(DisplayGLMode mode)
348 {
349 GdkDisplay *gdk_display = gdk_display_get_default();
350 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
351
352 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
353 return;
354 }
355
356 display_opengl = 1;
357 }
358
359 int gd_egl_make_current(DisplayChangeListener *dcl,
360 QEMUGLContext ctx)
361 {
362 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
363
364 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
365 vc->gfx.esurface, ctx);
366 }