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