]> git.proxmox.com Git - mirror_qemu.git/blob - ui/gtk-egl.c
Merge remote-tracking branch 'remotes/vivier2/tags/trivial-branch-for-6.2-pull-reques...
[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 int ww, wh;
67
68 if (!vc->gfx.gls) {
69 return;
70 }
71
72 window = gtk_widget_get_window(vc->gfx.drawing_area);
73 ww = gdk_window_get_width(window);
74 wh = gdk_window_get_height(window);
75
76 if (vc->gfx.scanout_mode) {
77 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
78
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;
84 }
85 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
86 vc->gfx.esurface, vc->gfx.ectx);
87
88 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
89 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
90
91 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
92
93 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
94 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
95 }
96
97 glFlush();
98 #ifdef CONFIG_GBM
99 if (vc->gfx.guest_fb.dmabuf) {
100 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
101
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;
106 }
107 graphic_hw_gl_block(vc->gfx.dcl.con, false);
108 }
109 #endif
110 graphic_hw_gl_flushed(vc->gfx.dcl.con);
111 }
112
113 void gd_egl_update(DisplayChangeListener *dcl,
114 int x, int y, int w, int h)
115 {
116 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
117
118 if (!vc->gfx.gls || !vc->gfx.ds) {
119 return;
120 }
121
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++;
126 }
127
128 void gd_egl_refresh(DisplayChangeListener *dcl)
129 {
130 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
131
132 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
133 vc->window ? vc->window : vc->gfx.drawing_area);
134
135 if (!vc->gfx.esurface) {
136 gd_egl_init(vc);
137 if (!vc->gfx.esurface) {
138 return;
139 }
140 vc->gfx.gls = qemu_gl_init_shader();
141 if (vc->gfx.ds) {
142 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
143 }
144 }
145
146 graphic_hw_update(dcl->con);
147
148 if (vc->gfx.glupdates) {
149 vc->gfx.glupdates = 0;
150 gtk_egl_set_scanout_mode(vc, false);
151 gd_egl_draw(vc);
152 }
153 }
154
155 void gd_egl_switch(DisplayChangeListener *dcl,
156 DisplaySurface *surface)
157 {
158 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
159 bool resized = true;
160
161 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
162
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;
167 }
168
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);
173 }
174
175 if (resized) {
176 gd_update_windowsize(vc);
177 }
178 }
179
180 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
181 QEMUGLParams *params)
182 {
183 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
184
185 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
186 vc->gfx.esurface, vc->gfx.ectx);
187 return qemu_egl_create_context(dcl, params);
188 }
189
190 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
191 {
192 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
193
194 vc->gfx.w = 0;
195 vc->gfx.h = 0;
196 gtk_egl_set_scanout_mode(vc, false);
197 }
198
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)
204 {
205 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
206
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;
212
213 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
214 vc->gfx.esurface, vc->gfx.ectx);
215
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);
219 }
220
221 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
222 QemuDmaBuf *dmabuf)
223 {
224 #ifdef CONFIG_GBM
225 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
226
227 egl_dmabuf_import_texture(dmabuf);
228 if (!dmabuf->texture) {
229 return;
230 }
231
232 gd_egl_scanout_texture(dcl, dmabuf->texture,
233 false, dmabuf->width, dmabuf->height,
234 0, 0, dmabuf->width, dmabuf->height);
235
236 if (dmabuf->allow_fences) {
237 vc->gfx.guest_fb.dmabuf = dmabuf;
238 }
239 #endif
240 }
241
242 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
243 QemuDmaBuf *dmabuf, bool have_hot,
244 uint32_t hot_x, uint32_t hot_y)
245 {
246 #ifdef CONFIG_GBM
247 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
248
249 if (dmabuf) {
250 egl_dmabuf_import_texture(dmabuf);
251 if (!dmabuf->texture) {
252 return;
253 }
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);
258 }
259 #endif
260 }
261
262 void gd_egl_cursor_position(DisplayChangeListener *dcl,
263 uint32_t pos_x, uint32_t pos_y)
264 {
265 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
266
267 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
268 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
269 }
270
271 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
272 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
273 {
274 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
275 GdkWindow *window;
276 int ww, wh;
277
278 if (!vc->gfx.scanout_mode) {
279 return;
280 }
281 if (!vc->gfx.guest_fb.framebuffer) {
282 return;
283 }
284
285 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
286 vc->gfx.esurface, vc->gfx.ectx);
287
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);
301 }
302
303 #ifdef CONFIG_GBM
304 if (vc->gfx.guest_fb.dmabuf) {
305 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
306 }
307 #endif
308
309 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
310 }
311
312 void gd_egl_flush(DisplayChangeListener *dcl,
313 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
314 {
315 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
316 GtkWidget *area = vc->gfx.drawing_area;
317
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;
322 }
323
324 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
325 }
326
327 void gtk_egl_init(DisplayGLMode mode)
328 {
329 GdkDisplay *gdk_display = gdk_display_get_default();
330 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
331
332 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
333 return;
334 }
335
336 display_opengl = 1;
337 }
338
339 int gd_egl_make_current(DisplayChangeListener *dcl,
340 QEMUGLContext ctx)
341 {
342 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
343
344 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
345 vc->gfx.esurface, ctx);
346 }