2 * GTK UI -- glarea opengl code.
4 * Requires 3.16+ (GtkGLArea widget).
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
15 #include "ui/console.h"
17 #include "ui/egl-helpers.h"
19 #include "sysemu/sysemu.h"
21 static void gtk_gl_area_set_scanout_mode(VirtualConsole
*vc
, bool scanout
)
23 if (vc
->gfx
.scanout_mode
== scanout
) {
27 vc
->gfx
.scanout_mode
= scanout
;
28 if (!vc
->gfx
.scanout_mode
) {
29 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
30 egl_fb_destroy(&vc
->gfx
.guest_fb
);
31 if (vc
->gfx
.surface
) {
32 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
33 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
38 /** DisplayState Callbacks (opengl version) **/
40 void gd_gl_area_draw(VirtualConsole
*vc
)
43 QemuDmaBuf
*dmabuf
= vc
->gfx
.guest_fb
.dmabuf
;
45 int ww
, wh
, ws
, y1
, y2
;
51 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
52 ws
= gdk_window_get_scale_factor(gtk_widget_get_window(vc
->gfx
.drawing_area
));
53 ww
= gtk_widget_get_allocated_width(vc
->gfx
.drawing_area
) * ws
;
54 wh
= gtk_widget_get_allocated_height(vc
->gfx
.drawing_area
) * ws
;
56 if (vc
->gfx
.scanout_mode
) {
57 if (!vc
->gfx
.guest_fb
.framebuffer
) {
63 if (!qemu_dmabuf_get_draw_submitted(dmabuf
)) {
66 qemu_dmabuf_set_draw_submitted(dmabuf
, false);
71 glBindFramebuffer(GL_READ_FRAMEBUFFER
, vc
->gfx
.guest_fb
.framebuffer
);
72 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
74 glViewport(0, 0, ww
, wh
);
75 y1
= vc
->gfx
.y0_top
? 0 : vc
->gfx
.h
;
76 y2
= vc
->gfx
.y0_top
? vc
->gfx
.h
: 0;
77 glBlitFramebuffer(0, y1
, vc
->gfx
.w
, y2
,
79 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
82 egl_dmabuf_create_sync(dmabuf
);
89 egl_dmabuf_create_fence(dmabuf
);
90 fence_fd
= qemu_dmabuf_get_fence_fd(dmabuf
);
92 qemu_set_fd_handler(fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
95 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
102 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
104 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
105 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
109 void gd_gl_area_update(DisplayChangeListener
*dcl
,
110 int x
, int y
, int w
, int h
)
112 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
114 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
118 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
119 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
121 gdk_gl_context_clear_current();
124 void gd_gl_area_refresh(DisplayChangeListener
*dcl
)
126 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
128 gd_update_monitor_refresh_rate(vc
, vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
130 if (vc
->gfx
.guest_fb
.dmabuf
&&
131 qemu_dmabuf_get_draw_submitted(vc
->gfx
.guest_fb
.dmabuf
)) {
137 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
140 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
141 vc
->gfx
.gls
= qemu_gl_init_shader();
143 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
147 graphic_hw_update(dcl
->con
);
149 if (vc
->gfx
.glupdates
) {
150 vc
->gfx
.glupdates
= 0;
151 gtk_gl_area_set_scanout_mode(vc
, false);
152 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
156 void gd_gl_area_switch(DisplayChangeListener
*dcl
,
157 DisplaySurface
*surface
)
159 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
162 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
165 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
166 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
171 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
172 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
173 surface_gl_create_texture(vc
->gfx
.gls
, surface
);
175 vc
->gfx
.ds
= surface
;
178 gd_update_windowsize(vc
);
182 static int gd_cmp_gl_context_version(int major
, int minor
, QEMUGLParams
*params
)
184 if (major
> params
->major_ver
) {
187 if (major
< params
->major_ver
) {
190 if (minor
> params
->minor_ver
) {
193 if (minor
< params
->minor_ver
) {
199 QEMUGLContext
gd_gl_area_create_context(DisplayGLCtx
*dgc
,
200 QEMUGLParams
*params
)
202 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
208 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
209 ctx
= gdk_window_create_gl_context(window
, &err
);
211 g_printerr("Create gdk gl context failed: %s\n", err
->message
);
215 gdk_gl_context_set_required_version(ctx
,
218 gdk_gl_context_realize(ctx
, &err
);
220 g_printerr("Realize gdk gl context failed: %s\n", err
->message
);
222 g_clear_object(&ctx
);
226 gdk_gl_context_make_current(ctx
);
227 gdk_gl_context_get_version(ctx
, &major
, &minor
);
228 gdk_gl_context_clear_current();
229 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
231 if (gd_cmp_gl_context_version(major
, minor
, params
) == -1) {
232 /* created ctx version < requested version */
233 g_clear_object(&ctx
);
236 trace_gd_gl_area_create_context(ctx
, params
->major_ver
, params
->minor_ver
);
240 void gd_gl_area_destroy_context(DisplayGLCtx
*dgc
, QEMUGLContext ctx
)
242 GdkGLContext
*current_ctx
= gdk_gl_context_get_current();
244 trace_gd_gl_area_destroy_context(ctx
, current_ctx
);
245 if (ctx
== current_ctx
) {
246 gdk_gl_context_clear_current();
248 g_clear_object(&ctx
);
251 void gd_gl_area_scanout_texture(DisplayChangeListener
*dcl
,
253 bool backing_y_0_top
,
254 uint32_t backing_width
,
255 uint32_t backing_height
,
256 uint32_t x
, uint32_t y
,
257 uint32_t w
, uint32_t h
,
260 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
266 vc
->gfx
.y0_top
= backing_y_0_top
;
268 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
270 if (backing_id
== 0 || vc
->gfx
.w
== 0 || vc
->gfx
.h
== 0) {
271 gtk_gl_area_set_scanout_mode(vc
, false);
275 gtk_gl_area_set_scanout_mode(vc
, true);
276 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
280 void gd_gl_area_scanout_disable(DisplayChangeListener
*dcl
)
282 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
284 gtk_gl_area_set_scanout_mode(vc
, false);
287 void gd_gl_area_scanout_flush(DisplayChangeListener
*dcl
,
288 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
290 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
292 if (vc
->gfx
.guest_fb
.dmabuf
&&
293 !qemu_dmabuf_get_draw_submitted(vc
->gfx
.guest_fb
.dmabuf
)) {
294 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
295 qemu_dmabuf_set_draw_submitted(vc
->gfx
.guest_fb
.dmabuf
, true);
296 gtk_gl_area_set_scanout_mode(vc
, true);
298 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
301 void gd_gl_area_scanout_dmabuf(DisplayChangeListener
*dcl
,
305 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
306 uint32_t x
, y
, width
, height
, backing_width
, backing_height
, texture
;
309 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
310 egl_dmabuf_import_texture(dmabuf
);
311 texture
= qemu_dmabuf_get_texture(dmabuf
);
316 x
= qemu_dmabuf_get_x(dmabuf
);
317 y
= qemu_dmabuf_get_y(dmabuf
);
318 width
= qemu_dmabuf_get_width(dmabuf
);
319 height
= qemu_dmabuf_get_height(dmabuf
);
320 backing_width
= qemu_dmabuf_get_backing_width(dmabuf
);
321 backing_height
= qemu_dmabuf_get_backing_height(dmabuf
);
322 y0_top
= qemu_dmabuf_get_y0_top(dmabuf
);
324 gd_gl_area_scanout_texture(dcl
, texture
, y0_top
,
325 backing_width
, backing_height
,
326 x
, y
, width
, height
, NULL
);
328 if (qemu_dmabuf_get_allow_fences(dmabuf
)) {
329 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
334 void gtk_gl_area_init(void)
339 int gd_gl_area_make_current(DisplayGLCtx
*dgc
,
342 gdk_gl_context_make_current(ctx
);