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
;
50 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
51 ww
= gtk_widget_get_allocated_width(vc
->gfx
.drawing_area
);
52 wh
= gtk_widget_get_allocated_height(vc
->gfx
.drawing_area
);
54 if (vc
->gfx
.scanout_mode
) {
55 if (!vc
->gfx
.guest_fb
.framebuffer
) {
61 if (!dmabuf
->draw_submitted
) {
64 dmabuf
->draw_submitted
= false;
69 glBindFramebuffer(GL_READ_FRAMEBUFFER
, vc
->gfx
.guest_fb
.framebuffer
);
70 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
72 glViewport(0, 0, ww
, wh
);
73 y1
= vc
->gfx
.y0_top
? 0 : vc
->gfx
.h
;
74 y2
= vc
->gfx
.y0_top
? vc
->gfx
.h
: 0;
75 glBlitFramebuffer(0, y1
, vc
->gfx
.w
, y2
,
77 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
80 egl_dmabuf_create_sync(dmabuf
);
86 egl_dmabuf_create_fence(dmabuf
);
87 if (dmabuf
->fence_fd
> 0) {
88 qemu_set_fd_handler(dmabuf
->fence_fd
, gd_hw_gl_flushed
, NULL
, vc
);
91 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
98 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
100 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
101 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
104 graphic_hw_gl_flushed(vc
->gfx
.dcl
.con
);
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
);
121 void gd_gl_area_refresh(DisplayChangeListener
*dcl
)
123 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
125 vc
->gfx
.dcl
.update_interval
= gd_monitor_update_interval(
126 vc
->window
? vc
->window
: vc
->gfx
.drawing_area
);
129 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
132 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
133 vc
->gfx
.gls
= qemu_gl_init_shader();
135 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
139 graphic_hw_update(dcl
->con
);
141 if (vc
->gfx
.glupdates
) {
142 vc
->gfx
.glupdates
= 0;
143 gtk_gl_area_set_scanout_mode(vc
, false);
144 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
148 void gd_gl_area_switch(DisplayChangeListener
*dcl
,
149 DisplaySurface
*surface
)
151 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
154 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
157 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
158 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
163 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
164 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
165 surface_gl_create_texture(vc
->gfx
.gls
, surface
);
167 vc
->gfx
.ds
= surface
;
170 gd_update_windowsize(vc
);
174 QEMUGLContext
gd_gl_area_create_context(DisplayChangeListener
*dcl
,
175 QEMUGLParams
*params
)
177 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
182 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
183 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
184 ctx
= gdk_window_create_gl_context(window
, &err
);
186 g_printerr("Create gdk gl context failed: %s\n", err
->message
);
190 gdk_gl_context_set_required_version(ctx
,
193 gdk_gl_context_realize(ctx
, &err
);
195 g_printerr("Realize gdk gl context failed: %s\n", err
->message
);
197 g_clear_object(&ctx
);
203 void gd_gl_area_destroy_context(DisplayChangeListener
*dcl
, QEMUGLContext ctx
)
208 void gd_gl_area_scanout_texture(DisplayChangeListener
*dcl
,
210 bool backing_y_0_top
,
211 uint32_t backing_width
,
212 uint32_t backing_height
,
213 uint32_t x
, uint32_t y
,
214 uint32_t w
, uint32_t h
)
216 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
222 vc
->gfx
.y0_top
= backing_y_0_top
;
224 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
226 if (backing_id
== 0 || vc
->gfx
.w
== 0 || vc
->gfx
.h
== 0) {
227 gtk_gl_area_set_scanout_mode(vc
, false);
231 gtk_gl_area_set_scanout_mode(vc
, true);
232 egl_fb_setup_for_tex(&vc
->gfx
.guest_fb
, backing_width
, backing_height
,
236 void gd_gl_area_scanout_disable(DisplayChangeListener
*dcl
)
238 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
240 gtk_gl_area_set_scanout_mode(vc
, false);
243 void gd_gl_area_scanout_flush(DisplayChangeListener
*dcl
,
244 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
246 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
248 if (vc
->gfx
.guest_fb
.dmabuf
) {
249 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, true);
250 vc
->gfx
.guest_fb
.dmabuf
->draw_submitted
= true;
252 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
255 void gd_gl_area_scanout_dmabuf(DisplayChangeListener
*dcl
,
259 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
261 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
262 egl_dmabuf_import_texture(dmabuf
);
263 if (!dmabuf
->texture
) {
267 gd_gl_area_scanout_texture(dcl
, dmabuf
->texture
,
268 false, dmabuf
->width
, dmabuf
->height
,
269 0, 0, dmabuf
->width
, dmabuf
->height
);
271 if (dmabuf
->allow_fences
) {
272 vc
->gfx
.guest_fb
.dmabuf
= dmabuf
;
277 void gtk_gl_area_init(void)
282 int gd_gl_area_make_current(DisplayChangeListener
*dcl
,
285 gdk_gl_context_make_current(ctx
);