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 egl_fb_destroy(&vc
->gfx
.guest_fb
);
30 if (vc
->gfx
.surface
) {
31 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
32 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
37 /** DisplayState Callbacks (opengl version) **/
39 void gd_gl_area_draw(VirtualConsole
*vc
)
42 QemuDmaBuf
*dmabuf
= vc
->gfx
.guest_fb
.dmabuf
;
44 int ww
, wh
, ws
, y1
, y2
;
50 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
51 ws
= gdk_window_get_scale_factor(gtk_widget_get_window(vc
->gfx
.drawing_area
));
52 ww
= gtk_widget_get_allocated_width(vc
->gfx
.drawing_area
) * ws
;
53 wh
= gtk_widget_get_allocated_height(vc
->gfx
.drawing_area
) * ws
;
55 if (vc
->gfx
.scanout_mode
) {
56 if (!vc
->gfx
.guest_fb
.framebuffer
) {
62 if (!dmabuf
->draw_submitted
) {
65 dmabuf
->draw_submitted
= false;
70 glBindFramebuffer(GL_READ_FRAMEBUFFER
, vc
->gfx
.guest_fb
.framebuffer
);
71 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
73 glViewport(0, 0, ww
, wh
);
74 y1
= vc
->gfx
.y0_top
? 0 : vc
->gfx
.h
;
75 y2
= vc
->gfx
.y0_top
? vc
->gfx
.h
: 0;
76 glBlitFramebuffer(0, y1
, vc
->gfx
.w
, y2
,
78 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
81 egl_dmabuf_create_sync(dmabuf
);
87 egl_dmabuf_create_fence(dmabuf
);
88 if (dmabuf
->fence_fd
> 0) {
89 qemu_set_fd_handler(dmabuf
->fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
92 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
99 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
101 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
102 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
106 void gd_gl_area_update(DisplayChangeListener
*dcl
,
107 int x
, int y
, int w
, int h
)
109 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
111 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
115 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
116 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
120 void gd_gl_area_refresh(DisplayChangeListener
*dcl
)
122 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
124 gd_update_monitor_refresh_rate(vc
, vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
127 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
130 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
131 vc
->gfx
.gls
= qemu_gl_init_shader();
133 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
137 graphic_hw_update(dcl
->con
);
139 if (vc
->gfx
.glupdates
) {
140 vc
->gfx
.glupdates
= 0;
141 gtk_gl_area_set_scanout_mode(vc
, false);
142 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
146 void gd_gl_area_switch(DisplayChangeListener
*dcl
,
147 DisplaySurface
*surface
)
149 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
152 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
155 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
156 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
161 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
162 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
163 surface_gl_create_texture(vc
->gfx
.gls
, surface
);
165 vc
->gfx
.ds
= surface
;
168 gd_update_windowsize(vc
);
172 static int gd_cmp_gl_context_version(int major
, int minor
, QEMUGLParams
*params
)
174 if (major
> params
->major_ver
) {
177 if (major
< params
->major_ver
) {
180 if (minor
> params
->minor_ver
) {
183 if (minor
< params
->minor_ver
) {
189 QEMUGLContext
gd_gl_area_create_context(DisplayGLCtx
*dgc
,
190 QEMUGLParams
*params
)
192 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
198 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
199 ctx
= gdk_window_create_gl_context(window
, &err
);
201 g_printerr("Create gdk gl context failed: %s\n", err
->message
);
205 gdk_gl_context_set_required_version(ctx
,
208 gdk_gl_context_realize(ctx
, &err
);
210 g_printerr("Realize gdk gl context failed: %s\n", err
->message
);
212 g_clear_object(&ctx
);
216 gdk_gl_context_make_current(ctx
);
217 gdk_gl_context_get_version(ctx
, &major
, &minor
);
218 gdk_gl_context_clear_current();
219 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
221 if (gd_cmp_gl_context_version(major
, minor
, params
) == -1) {
222 /* created ctx version < requested version */
223 g_clear_object(&ctx
);
226 trace_gd_gl_area_create_context(ctx
, params
->major_ver
, params
->minor_ver
);
230 void gd_gl_area_destroy_context(DisplayGLCtx
*dgc
, QEMUGLContext ctx
)
232 GdkGLContext
*current_ctx
= gdk_gl_context_get_current();
234 trace_gd_gl_area_destroy_context(ctx
, current_ctx
);
235 if (ctx
== current_ctx
) {
236 gdk_gl_context_clear_current();
238 g_clear_object(&ctx
);
241 void gd_gl_area_scanout_texture(DisplayChangeListener
*dcl
,
243 bool backing_y_0_top
,
244 uint32_t backing_width
,
245 uint32_t backing_height
,
246 uint32_t x
, uint32_t y
,
247 uint32_t w
, uint32_t h
)
249 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
255 vc
->gfx
.y0_top
= backing_y_0_top
;
257 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
259 if (backing_id
== 0 || vc
->gfx
.w
== 0 || vc
->gfx
.h
== 0) {
260 gtk_gl_area_set_scanout_mode(vc
, false);
264 gtk_gl_area_set_scanout_mode(vc
, true);
265 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
269 void gd_gl_area_scanout_disable(DisplayChangeListener
*dcl
)
271 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
273 gtk_gl_area_set_scanout_mode(vc
, false);
276 void gd_gl_area_scanout_flush(DisplayChangeListener
*dcl
,
277 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
279 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
281 if (vc
->gfx
.guest_fb
.dmabuf
) {
282 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
283 vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
= true;
285 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
288 void gd_gl_area_scanout_dmabuf(DisplayChangeListener
*dcl
,
292 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
294 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
295 egl_dmabuf_import_texture(dmabuf
);
296 if (!dmabuf
->texture
) {
300 gd_gl_area_scanout_texture(dcl
, dmabuf
->texture
,
301 false, dmabuf
->width
, dmabuf
->height
,
302 0, 0, dmabuf
->width
, dmabuf
->height
);
304 if (dmabuf
->allow_fences
) {
305 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
310 void gtk_gl_area_init(void)
315 int gd_gl_area_make_current(DisplayGLCtx
*dgc
,
318 gdk_gl_context_make_current(ctx
);