Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / ui / gtk-egl.c
blob9831c10e1bd58f76d8f803ad0a72d983adc3f490
1 /*
2 * GTK UI -- egl opengl code.
4 * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget,
5 * which is GtkDrawingArea like widget with opengl rendering support.
7 * This code handles opengl support on older gtk versions, using egl
8 * to get a opengl context for the X11 window.
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.
14 #include "qemu/osdep.h"
15 #include "qemu/main-loop.h"
16 #include "qemu/error-report.h"
18 #include "trace.h"
20 #include "ui/console.h"
21 #include "ui/gtk.h"
22 #include "ui/egl-helpers.h"
23 #include "ui/shader.h"
25 #include "sysemu/sysemu.h"
27 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
29 if (vc->gfx.scanout_mode == scanout) {
30 return;
33 vc->gfx.scanout_mode = scanout;
34 if (!vc->gfx.scanout_mode) {
35 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
36 vc->gfx.esurface, vc->gfx.ectx);
37 egl_fb_destroy(&vc->gfx.guest_fb);
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);
45 /** DisplayState Callbacks (opengl version) **/
47 void gd_egl_init(VirtualConsole *vc)
49 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
50 if (!gdk_window) {
51 return;
54 Window x11_window = gdk_x11_window_get_xid(gdk_window);
55 if (!x11_window) {
56 return;
59 vc->gfx.ectx = qemu_egl_init_ctx();
60 vc->gfx.esurface = qemu_egl_init_surface_x11
61 (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
63 assert(vc->gfx.esurface);
66 void gd_egl_draw(VirtualConsole *vc)
68 GdkWindow *window;
69 #ifdef CONFIG_GBM
70 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
71 int fence_fd;
72 #endif
73 int ww, wh, ws;
75 if (!vc->gfx.gls) {
76 return;
79 window = gtk_widget_get_window(vc->gfx.drawing_area);
80 ws = gdk_window_get_scale_factor(window);
81 ww = gdk_window_get_width(window) * ws;
82 wh = gdk_window_get_height(window) * ws;
84 if (vc->gfx.scanout_mode) {
85 #ifdef CONFIG_GBM
86 if (dmabuf) {
87 if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
88 return;
89 } else {
90 qemu_dmabuf_set_draw_submitted(dmabuf, false);
93 #endif
94 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
96 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
97 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
99 glFlush();
100 #ifdef CONFIG_GBM
101 if (dmabuf) {
102 egl_dmabuf_create_fence(dmabuf);
103 fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
104 if (fence_fd >= 0) {
105 qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
106 return;
108 graphic_hw_gl_block(vc->gfx.dcl.con, false);
110 #endif
111 } else {
112 if (!vc->gfx.ds) {
113 return;
115 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
116 vc->gfx.esurface, vc->gfx.ectx);
118 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
119 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
121 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
123 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
124 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
126 glFlush();
130 void gd_egl_update(DisplayChangeListener *dcl,
131 int x, int y, int w, int h)
133 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
135 if (!vc->gfx.gls || !vc->gfx.ds) {
136 return;
139 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
140 vc->gfx.esurface, vc->gfx.ectx);
141 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
142 vc->gfx.glupdates++;
143 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
144 EGL_NO_SURFACE, EGL_NO_CONTEXT);
147 void gd_egl_refresh(DisplayChangeListener *dcl)
149 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
151 gd_update_monitor_refresh_rate(
152 vc, vc->window ? vc->window : vc->gfx.drawing_area);
154 if (vc->gfx.guest_fb.dmabuf &&
155 qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
156 gd_egl_draw(vc);
157 return;
160 if (!vc->gfx.esurface) {
161 gd_egl_init(vc);
162 if (!vc->gfx.esurface) {
163 return;
165 vc->gfx.gls = qemu_gl_init_shader();
166 if (vc->gfx.ds) {
167 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
168 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
170 #ifdef CONFIG_GBM
171 if (vc->gfx.guest_fb.dmabuf) {
172 egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
173 gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
175 #endif
178 graphic_hw_update(dcl->con);
180 if (vc->gfx.glupdates) {
181 vc->gfx.glupdates = 0;
182 gtk_egl_set_scanout_mode(vc, false);
183 gd_egl_draw(vc);
187 void gd_egl_switch(DisplayChangeListener *dcl,
188 DisplaySurface *surface)
190 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
191 bool resized = true;
193 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
195 if (vc->gfx.ds &&
196 surface_width(vc->gfx.ds) == surface_width(surface) &&
197 surface_height(vc->gfx.ds) == surface_height(surface)) {
198 resized = false;
200 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
201 vc->gfx.esurface, vc->gfx.ectx);
203 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
204 vc->gfx.ds = surface;
205 if (vc->gfx.gls) {
206 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
209 if (resized) {
210 gd_update_windowsize(vc);
213 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
214 EGL_NO_CONTEXT);
217 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
218 QEMUGLParams *params)
220 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
222 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
223 vc->gfx.esurface, vc->gfx.ectx);
224 return qemu_egl_create_context(dgc, params);
227 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
229 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
231 vc->gfx.w = 0;
232 vc->gfx.h = 0;
233 gtk_egl_set_scanout_mode(vc, false);
236 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
237 uint32_t backing_id, bool backing_y_0_top,
238 uint32_t backing_width, uint32_t backing_height,
239 uint32_t x, uint32_t y,
240 uint32_t w, uint32_t h,
241 void *d3d_tex2d)
243 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
245 vc->gfx.x = x;
246 vc->gfx.y = y;
247 vc->gfx.w = w;
248 vc->gfx.h = h;
249 vc->gfx.y0_top = backing_y_0_top;
251 if (!vc->gfx.esurface) {
252 gd_egl_init(vc);
253 if (!vc->gfx.esurface) {
254 return;
258 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
259 vc->gfx.esurface, vc->gfx.ectx);
261 gtk_egl_set_scanout_mode(vc, true);
262 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
263 backing_id, false);
266 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
267 QemuDmaBuf *dmabuf)
269 #ifdef CONFIG_GBM
270 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
271 uint32_t x, y, width, height, backing_width, backing_height, texture;
272 bool y0_top;
274 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
275 vc->gfx.esurface, vc->gfx.ectx);
277 egl_dmabuf_import_texture(dmabuf);
278 texture = qemu_dmabuf_get_texture(dmabuf);
279 if (!texture) {
280 return;
283 x = qemu_dmabuf_get_x(dmabuf);
284 y = qemu_dmabuf_get_y(dmabuf);
285 width = qemu_dmabuf_get_width(dmabuf);
286 height = qemu_dmabuf_get_height(dmabuf);
287 backing_width = qemu_dmabuf_get_backing_width(dmabuf);
288 backing_height = qemu_dmabuf_get_backing_height(dmabuf);
289 y0_top = qemu_dmabuf_get_y0_top(dmabuf);
291 gd_egl_scanout_texture(dcl, texture, y0_top, backing_width, backing_height,
292 x, y, width, height, NULL);
294 if (qemu_dmabuf_get_allow_fences(dmabuf)) {
295 vc->gfx.guest_fb.dmabuf = dmabuf;
297 #endif
300 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
301 QemuDmaBuf *dmabuf, bool have_hot,
302 uint32_t hot_x, uint32_t hot_y)
304 #ifdef CONFIG_GBM
305 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
306 uint32_t backing_width, backing_height, texture;
308 if (dmabuf) {
309 egl_dmabuf_import_texture(dmabuf);
310 texture = qemu_dmabuf_get_texture(dmabuf);
311 if (!texture) {
312 return;
315 backing_width = qemu_dmabuf_get_backing_width(dmabuf);
316 backing_height = qemu_dmabuf_get_backing_height(dmabuf);
317 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, backing_width, backing_height,
318 texture, false);
319 } else {
320 egl_fb_destroy(&vc->gfx.cursor_fb);
322 #endif
325 void gd_egl_cursor_position(DisplayChangeListener *dcl,
326 uint32_t pos_x, uint32_t pos_y)
328 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
330 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
331 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
334 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
335 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
337 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
338 GdkWindow *window;
339 int ww, wh, ws;
341 if (!vc->gfx.scanout_mode) {
342 return;
344 if (!vc->gfx.guest_fb.framebuffer) {
345 return;
348 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
349 vc->gfx.esurface, vc->gfx.ectx);
351 window = gtk_widget_get_window(vc->gfx.drawing_area);
352 ws = gdk_window_get_scale_factor(window);
353 ww = gdk_window_get_width(window) * ws;
354 wh = gdk_window_get_height(window) * ws;
355 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
356 if (vc->gfx.cursor_fb.texture) {
357 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
358 vc->gfx.y0_top);
359 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
360 vc->gfx.y0_top,
361 vc->gfx.cursor_x, vc->gfx.cursor_y,
362 vc->gfx.scale_x, vc->gfx.scale_y);
363 } else {
364 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
367 #ifdef CONFIG_GBM
368 if (vc->gfx.guest_fb.dmabuf) {
369 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
371 #endif
373 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
376 void gd_egl_flush(DisplayChangeListener *dcl,
377 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
379 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
380 GtkWidget *area = vc->gfx.drawing_area;
382 if (vc->gfx.guest_fb.dmabuf &&
383 !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
384 graphic_hw_gl_block(vc->gfx.dcl.con, true);
385 qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
386 gtk_egl_set_scanout_mode(vc, true);
387 gtk_widget_queue_draw_area(area, x, y, w, h);
388 return;
391 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
394 void gtk_egl_init(DisplayGLMode mode)
396 GdkDisplay *gdk_display = gdk_display_get_default();
397 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
399 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
400 return;
403 display_opengl = 1;
406 int gd_egl_make_current(DisplayGLCtx *dgc,
407 QEMUGLContext ctx)
409 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
411 if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
412 vc->gfx.esurface, ctx)) {
413 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string());
414 return -1;
417 return 0;