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 (!dmabuf
->draw_submitted
) {
66 dmabuf
->draw_submitted
= 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
);
88 egl_dmabuf_create_fence(dmabuf
);
89 if (dmabuf
->fence_fd
> 0) {
90 qemu_set_fd_handler(dmabuf
->fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
93 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
100 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
102 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
103 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
107 void gd_gl_area_update(DisplayChangeListener
*dcl
,
108 int x
, int y
, int w
, int h
)
110 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
112 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
116 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
117 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
119 gdk_gl_context_clear_current();
122 void gd_gl_area_refresh(DisplayChangeListener
*dcl
)
124 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
126 gd_update_monitor_refresh_rate(vc
, vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
128 if (vc
->gfx
.guest_fb
.dmabuf
&& vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
) {
133 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
136 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
137 vc
->gfx
.gls
= qemu_gl_init_shader();
139 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
143 graphic_hw_update(dcl
->con
);
145 if (vc
->gfx
.glupdates
) {
146 vc
->gfx
.glupdates
= 0;
147 gtk_gl_area_set_scanout_mode(vc
, false);
148 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
152 void gd_gl_area_switch(DisplayChangeListener
*dcl
,
153 DisplaySurface
*surface
)
155 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
158 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
161 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
162 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
167 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
168 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
169 surface_gl_create_texture(vc
->gfx
.gls
, surface
);
171 vc
->gfx
.ds
= surface
;
174 gd_update_windowsize(vc
);
178 static int gd_cmp_gl_context_version(int major
, int minor
, QEMUGLParams
*params
)
180 if (major
> params
->major_ver
) {
183 if (major
< params
->major_ver
) {
186 if (minor
> params
->minor_ver
) {
189 if (minor
< params
->minor_ver
) {
195 QEMUGLContext
gd_gl_area_create_context(DisplayGLCtx
*dgc
,
196 QEMUGLParams
*params
)
198 VirtualConsole
*vc
= container_of(dgc
, VirtualConsole
, gfx
.dgc
);
204 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
205 ctx
= gdk_window_create_gl_context(window
, &err
);
207 g_printerr("Create gdk gl context failed: %s\n", err
->message
);
211 gdk_gl_context_set_required_version(ctx
,
214 gdk_gl_context_realize(ctx
, &err
);
216 g_printerr("Realize gdk gl context failed: %s\n", err
->message
);
218 g_clear_object(&ctx
);
222 gdk_gl_context_make_current(ctx
);
223 gdk_gl_context_get_version(ctx
, &major
, &minor
);
224 gdk_gl_context_clear_current();
225 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
227 if (gd_cmp_gl_context_version(major
, minor
, params
) == -1) {
228 /* created ctx version < requested version */
229 g_clear_object(&ctx
);
232 trace_gd_gl_area_create_context(ctx
, params
->major_ver
, params
->minor_ver
);
236 void gd_gl_area_destroy_context(DisplayGLCtx
*dgc
, QEMUGLContext ctx
)
238 GdkGLContext
*current_ctx
= gdk_gl_context_get_current();
240 trace_gd_gl_area_destroy_context(ctx
, current_ctx
);
241 if (ctx
== current_ctx
) {
242 gdk_gl_context_clear_current();
244 g_clear_object(&ctx
);
247 void gd_gl_area_scanout_texture(DisplayChangeListener
*dcl
,
249 bool backing_y_0_top
,
250 uint32_t backing_width
,
251 uint32_t backing_height
,
252 uint32_t x
, uint32_t y
,
253 uint32_t w
, uint32_t h
,
256 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
262 vc
->gfx
.y0_top
= backing_y_0_top
;
264 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
266 if (backing_id
== 0 || vc
->gfx
.w
== 0 || vc
->gfx
.h
== 0) {
267 gtk_gl_area_set_scanout_mode(vc
, false);
271 gtk_gl_area_set_scanout_mode(vc
, true);
272 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
276 void gd_gl_area_scanout_disable(DisplayChangeListener
*dcl
)
278 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
280 gtk_gl_area_set_scanout_mode(vc
, false);
283 void gd_gl_area_scanout_flush(DisplayChangeListener
*dcl
,
284 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
286 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
288 if (vc
->gfx
.guest_fb
.dmabuf
&& !vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
) {
289 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
290 vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
= true;
291 gtk_gl_area_set_scanout_mode(vc
, true);
293 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
296 void gd_gl_area_scanout_dmabuf(DisplayChangeListener
*dcl
,
300 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
302 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
303 egl_dmabuf_import_texture(dmabuf
);
304 if (!dmabuf
->texture
) {
308 gd_gl_area_scanout_texture(dcl
, dmabuf
->texture
,
310 dmabuf
->backing_width
, dmabuf
->backing_height
,
311 dmabuf
->x
, dmabuf
->y
, dmabuf
->width
,
312 dmabuf
->height
, NULL
);
314 if (dmabuf
->allow_fences
) {
315 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
320 void gtk_gl_area_init(void)
325 int gd_gl_area_make_current(DisplayGLCtx
*dgc
,
328 gdk_gl_context_make_current(ctx
);