]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk-egl.c
ui/gtk: set scanout mode in gd_egl/gd_gl_area_scanout_texture
[mirror_qemu.git] / ui / gtk-egl.c
CommitLineData
97edf3bd
GH
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
e16f4c87 14#include "qemu/osdep.h"
65b847d2 15#include "qemu/main-loop.h"
1d48c9fd 16#include "qemu/error-report.h"
97edf3bd
GH
17
18#include "trace.h"
19
20#include "ui/console.h"
21#include "ui/gtk.h"
22#include "ui/egl-helpers.h"
f1bd3132 23#include "ui/shader.h"
97edf3bd
GH
24
25#include "sysemu/sysemu.h"
26
4782aeb7
GH
27static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
28{
29 if (vc->gfx.scanout_mode == scanout) {
30 return;
31 }
32
33 vc->gfx.scanout_mode = scanout;
34 if (!vc->gfx.scanout_mode) {
83b4b236
DK
35 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
36 vc->gfx.esurface, vc->gfx.ectx);
a4f113fd 37 egl_fb_destroy(&vc->gfx.guest_fb);
4782aeb7
GH
38 if (vc->gfx.surface) {
39 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
40 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
41 }
42 }
43}
44
97edf3bd
GH
45/** DisplayState Callbacks (opengl version) **/
46
47void gd_egl_init(VirtualConsole *vc)
48{
49 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
50 if (!gdk_window) {
51 return;
52 }
53
97edf3bd 54 Window x11_window = gdk_x11_window_get_xid(gdk_window);
97edf3bd
GH
55 if (!x11_window) {
56 return;
57 }
58
59 vc->gfx.ectx = qemu_egl_init_ctx();
fbd57c75
AK
60 vc->gfx.esurface = qemu_egl_init_surface_x11
61 (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
97edf3bd
GH
62
63 assert(vc->gfx.esurface);
64}
65
66void gd_egl_draw(VirtualConsole *vc)
67{
68 GdkWindow *window;
55f4b767
DK
69#ifdef CONFIG_GBM
70 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
71#endif
97edf3bd
GH
72 int ww, wh;
73
4782aeb7 74 if (!vc->gfx.gls) {
97edf3bd
GH
75 return;
76 }
77
f1aba960
GH
78 window = gtk_widget_get_window(vc->gfx.drawing_area);
79 ww = gdk_window_get_width(window);
80 wh = gdk_window_get_height(window);
81
4782aeb7 82 if (vc->gfx.scanout_mode) {
55f4b767
DK
83#ifdef CONFIG_GBM
84 if (dmabuf) {
85 if (!dmabuf->draw_submitted) {
86 return;
87 } else {
88 dmabuf->draw_submitted = false;
89 }
90 }
91#endif
4782aeb7 92 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
f1aba960 93
f8a951bb
EN
94 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
95 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
55f4b767
DK
96
97 glFlush();
98#ifdef CONFIG_GBM
99 if (dmabuf) {
100 egl_dmabuf_create_fence(dmabuf);
101 if (dmabuf->fence_fd > 0) {
102 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
103 return;
104 }
105 graphic_hw_gl_block(vc->gfx.dcl.con, false);
106 }
107#endif
4782aeb7
GH
108 } else {
109 if (!vc->gfx.ds) {
110 return;
111 }
112 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
113 vc->gfx.esurface, vc->gfx.ectx);
97edf3bd 114
4782aeb7
GH
115 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
116 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
97edf3bd 117
4782aeb7 118 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
f1aba960
GH
119
120 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
121 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
65b847d2 122
55f4b767 123 glFlush();
65b847d2 124 }
97edf3bd
GH
125}
126
127void gd_egl_update(DisplayChangeListener *dcl,
128 int x, int y, int w, int h)
129{
130 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
131
132 if (!vc->gfx.gls || !vc->gfx.ds) {
133 return;
134 }
135
136 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
137 vc->gfx.esurface, vc->gfx.ectx);
138 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
139 vc->gfx.glupdates++;
83b4b236
DK
140 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
141 EGL_NO_SURFACE, EGL_NO_CONTEXT);
97edf3bd
GH
142}
143
144void gd_egl_refresh(DisplayChangeListener *dcl)
145{
146 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
147
aeffd071
AO
148 gd_update_monitor_refresh_rate(
149 vc, vc->window ? vc->window : vc->gfx.drawing_area);
cab82424 150
1be878eb
DK
151 if (vc->gfx.guest_fb.dmabuf && vc->gfx.guest_fb.dmabuf->draw_submitted) {
152 return;
153 }
154
97edf3bd
GH
155 if (!vc->gfx.esurface) {
156 gd_egl_init(vc);
157 if (!vc->gfx.esurface) {
158 return;
159 }
46e19e14 160 vc->gfx.gls = qemu_gl_init_shader();
97edf3bd 161 if (vc->gfx.ds) {
01eb4749 162 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
97edf3bd
GH
163 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
164 }
8b4ed0da 165#ifdef CONFIG_GBM
4872a023
DK
166 if (vc->gfx.guest_fb.dmabuf) {
167 egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
168 gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
169 }
8b4ed0da 170#endif
97edf3bd
GH
171 }
172
173 graphic_hw_update(dcl->con);
174
175 if (vc->gfx.glupdates) {
176 vc->gfx.glupdates = 0;
4782aeb7 177 gtk_egl_set_scanout_mode(vc, false);
97edf3bd
GH
178 gd_egl_draw(vc);
179 }
180}
181
182void gd_egl_switch(DisplayChangeListener *dcl,
183 DisplaySurface *surface)
184{
185 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
186 bool resized = true;
187
188 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
189
190 if (vc->gfx.ds &&
191 surface_width(vc->gfx.ds) == surface_width(surface) &&
192 surface_height(vc->gfx.ds) == surface_height(surface)) {
193 resized = false;
194 }
01eb4749
DK
195 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
196 vc->gfx.esurface, vc->gfx.ectx);
97edf3bd
GH
197
198 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
199 vc->gfx.ds = surface;
200 if (vc->gfx.gls) {
201 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
202 }
203
204 if (resized) {
205 gd_update_windowsize(vc);
206 }
604a8689
DK
207
208 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
209 EGL_NO_CONTEXT);
97edf3bd
GH
210}
211
5e79d516 212QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
4782aeb7
GH
213 QEMUGLParams *params)
214{
5e79d516 215 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
4782aeb7
GH
216
217 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
218 vc->gfx.esurface, vc->gfx.ectx);
5e79d516 219 return qemu_egl_create_context(dgc, params);
4782aeb7
GH
220}
221
543a7a16
GH
222void gd_egl_scanout_disable(DisplayChangeListener *dcl)
223{
224 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
225
226 vc->gfx.w = 0;
227 vc->gfx.h = 0;
543a7a16
GH
228 gtk_egl_set_scanout_mode(vc, false);
229}
230
f4c36bda
GH
231void gd_egl_scanout_texture(DisplayChangeListener *dcl,
232 uint32_t backing_id, bool backing_y_0_top,
233 uint32_t backing_width, uint32_t backing_height,
234 uint32_t x, uint32_t y,
bf41ab61
MAL
235 uint32_t w, uint32_t h,
236 void *d3d_tex2d)
4782aeb7
GH
237{
238 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
239
240 vc->gfx.x = x;
241 vc->gfx.y = y;
242 vc->gfx.w = w;
243 vc->gfx.h = h;
4782aeb7
GH
244 vc->gfx.y0_top = backing_y_0_top;
245
246 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
247 vc->gfx.esurface, vc->gfx.ectx);
248
58ea90f8 249 gtk_egl_set_scanout_mode(vc, true);
74083f9c
GH
250 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
251 backing_id, false);
4782aeb7
GH
252}
253
70763fea
GH
254void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
255 QemuDmaBuf *dmabuf)
256{
bc6a3565 257#ifdef CONFIG_GBM
65b847d2
VK
258 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
259
01eb4749
DK
260 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
261 vc->gfx.esurface, vc->gfx.ectx);
262
70763fea
GH
263 egl_dmabuf_import_texture(dmabuf);
264 if (!dmabuf->texture) {
265 return;
266 }
267
268 gd_egl_scanout_texture(dcl, dmabuf->texture,
9ac06df8
DK
269 dmabuf->y0_top,
270 dmabuf->backing_width, dmabuf->backing_height,
271 dmabuf->x, dmabuf->y, dmabuf->width,
272 dmabuf->height, NULL);
65b847d2
VK
273
274 if (dmabuf->allow_fences) {
275 vc->gfx.guest_fb.dmabuf = dmabuf;
276 }
70763fea
GH
277#endif
278}
279
f1bd3132
GH
280void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
281 QemuDmaBuf *dmabuf, bool have_hot,
282 uint32_t hot_x, uint32_t hot_y)
283{
bc6a3565 284#ifdef CONFIG_GBM
f1bd3132
GH
285 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
286
287 if (dmabuf) {
288 egl_dmabuf_import_texture(dmabuf);
289 if (!dmabuf->texture) {
290 return;
291 }
9ac06df8
DK
292 egl_fb_setup_for_tex(&vc->gfx.cursor_fb,
293 dmabuf->backing_width, dmabuf->backing_height,
f1bd3132
GH
294 dmabuf->texture, false);
295 } else {
296 egl_fb_destroy(&vc->gfx.cursor_fb);
297 }
298#endif
299}
300
301void gd_egl_cursor_position(DisplayChangeListener *dcl,
302 uint32_t pos_x, uint32_t pos_y)
303{
304 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
305
f1aba960
GH
306 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
307 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
f1bd3132
GH
308}
309
4782aeb7
GH
310void gd_egl_scanout_flush(DisplayChangeListener *dcl,
311 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
312{
313 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
314 GdkWindow *window;
a4f113fd 315 int ww, wh;
4782aeb7
GH
316
317 if (!vc->gfx.scanout_mode) {
318 return;
319 }
a4f113fd 320 if (!vc->gfx.guest_fb.framebuffer) {
4782aeb7
GH
321 return;
322 }
323
324 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
325 vc->gfx.esurface, vc->gfx.ectx);
326
4782aeb7 327 window = gtk_widget_get_window(vc->gfx.drawing_area);
89d85cde
DB
328 ww = gdk_window_get_width(window);
329 wh = gdk_window_get_height(window);
a4f113fd 330 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
f1bd3132
GH
331 if (vc->gfx.cursor_fb.texture) {
332 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
333 vc->gfx.y0_top);
334 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
335 vc->gfx.y0_top,
051a0cde
CZ
336 vc->gfx.cursor_x, vc->gfx.cursor_y,
337 vc->gfx.scale_x, vc->gfx.scale_y);
f1bd3132
GH
338 } else {
339 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
340 }
4782aeb7 341
65b847d2
VK
342#ifdef CONFIG_GBM
343 if (vc->gfx.guest_fb.dmabuf) {
344 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
345 }
346#endif
347
4782aeb7
GH
348 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
349}
350
ab971f8a
VK
351void gd_egl_flush(DisplayChangeListener *dcl,
352 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
353{
354 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
355 GtkWidget *area = vc->gfx.drawing_area;
356
64f1359b 357 if (vc->gfx.guest_fb.dmabuf && !vc->gfx.guest_fb.dmabuf->draw_submitted) {
ab971f8a 358 graphic_hw_gl_block(vc->gfx.dcl.con, true);
55f4b767 359 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
92b58156 360 gtk_egl_set_scanout_mode(vc, true);
ab971f8a
VK
361 gtk_widget_queue_draw_area(area, x, y, w, h);
362 return;
363 }
364
365 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
366}
367
54d208ff 368void gtk_egl_init(DisplayGLMode mode)
97edf3bd
GH
369{
370 GdkDisplay *gdk_display = gdk_display_get_default();
371 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
372
54d208ff 373 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
97edf3bd
GH
374 return;
375 }
376
377 display_opengl = 1;
378}
4782aeb7 379
5e79d516 380int gd_egl_make_current(DisplayGLCtx *dgc,
4782aeb7
GH
381 QEMUGLContext ctx)
382{
5e79d516 383 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
4782aeb7 384
1d48c9fd
MAL
385 if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
386 vc->gfx.esurface, ctx)) {
387 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());
388 return -1;
389 }
390
391 return 0;
4782aeb7 392}