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-common.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
) {
30 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
31 GL_COLOR_ATTACHMENT0_EXT
,
33 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
34 glDeleteFramebuffers(1, &vc
->gfx
.fbo_id
);
37 if (vc
->gfx
.surface
) {
38 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
39 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
44 /** DisplayState Callbacks (opengl version) **/
46 void gd_gl_area_draw(VirtualConsole
*vc
)
54 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
55 ww
= gtk_widget_get_allocated_width(vc
->gfx
.drawing_area
);
56 wh
= gtk_widget_get_allocated_height(vc
->gfx
.drawing_area
);
58 if (vc
->gfx
.scanout_mode
) {
59 if (!vc
->gfx
.fbo_id
) {
63 glBindFramebuffer(GL_READ_FRAMEBUFFER
, vc
->gfx
.fbo_id
);
64 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
66 glViewport(0, 0, ww
, wh
);
67 y1
= vc
->gfx
.y0_top
? 0 : vc
->gfx
.h
;
68 y2
= vc
->gfx
.y0_top
? vc
->gfx
.h
: 0;
69 glBlitFramebuffer(0, y1
, vc
->gfx
.w
, y2
,
71 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
76 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
78 surface_gl_setup_viewport(vc
->gfx
.gls
, vc
->gfx
.ds
, ww
, wh
);
79 surface_gl_render_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
83 void gd_gl_area_update(DisplayChangeListener
*dcl
,
84 int x
, int y
, int w
, int h
)
86 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
88 if (!vc
->gfx
.gls
|| !vc
->gfx
.ds
) {
92 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
93 surface_gl_update_texture(vc
->gfx
.gls
, vc
->gfx
.ds
, x
, y
, w
, h
);
97 void gd_gl_area_refresh(DisplayChangeListener
*dcl
)
99 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
102 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
105 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
106 vc
->gfx
.gls
= console_gl_init_context();
108 surface_gl_create_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
112 graphic_hw_update(dcl
->con
);
114 if (vc
->gfx
.glupdates
) {
115 vc
->gfx
.glupdates
= 0;
116 gtk_gl_area_set_scanout_mode(vc
, false);
117 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
121 void gd_gl_area_switch(DisplayChangeListener
*dcl
,
122 DisplaySurface
*surface
)
124 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
127 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
130 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
131 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
136 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
137 surface_gl_destroy_texture(vc
->gfx
.gls
, vc
->gfx
.ds
);
138 surface_gl_create_texture(vc
->gfx
.gls
, surface
);
140 vc
->gfx
.ds
= surface
;
143 gd_update_windowsize(vc
);
147 QEMUGLContext
gd_gl_area_create_context(DisplayChangeListener
*dcl
,
148 QEMUGLParams
*params
)
150 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
155 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
156 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
157 ctx
= gdk_window_create_gl_context(window
, &err
);
158 gdk_gl_context_set_required_version(ctx
,
161 gdk_gl_context_realize(ctx
, &err
);
165 void gd_gl_area_destroy_context(DisplayChangeListener
*dcl
, QEMUGLContext ctx
)
170 void gd_gl_area_scanout(DisplayChangeListener
*dcl
,
171 uint32_t backing_id
, bool backing_y_0_top
,
172 uint32_t backing_width
, uint32_t backing_height
,
173 uint32_t x
, uint32_t y
,
174 uint32_t w
, uint32_t h
)
176 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
182 vc
->gfx
.tex_id
= backing_id
;
183 vc
->gfx
.y0_top
= backing_y_0_top
;
185 gtk_gl_area_make_current(GTK_GL_AREA(vc
->gfx
.drawing_area
));
187 if (vc
->gfx
.tex_id
== 0 || vc
->gfx
.w
== 0 || vc
->gfx
.h
== 0) {
188 gtk_gl_area_set_scanout_mode(vc
, false);
192 gtk_gl_area_set_scanout_mode(vc
, true);
193 if (!vc
->gfx
.fbo_id
) {
194 glGenFramebuffers(1, &vc
->gfx
.fbo_id
);
197 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, vc
->gfx
.fbo_id
);
198 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
199 GL_TEXTURE_2D
, vc
->gfx
.tex_id
, 0);
202 void gd_gl_area_scanout_flush(DisplayChangeListener
*dcl
,
203 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
205 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
207 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
210 void gtk_gl_area_init(void)
215 QEMUGLContext
gd_gl_area_get_current_context(DisplayChangeListener
*dcl
)
217 return gdk_gl_context_get_current();
220 int gd_gl_area_make_current(DisplayChangeListener
*dcl
,
223 gdk_gl_context_make_current(ctx
);