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