]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk-gl-area.c
ui/console: Do not return a value with ui_info
[mirror_qemu.git] / ui / gtk-gl-area.c
CommitLineData
925a0400
GH
1/*
2 * GTK UI -- glarea opengl code.
3 *
4 * Requires 3.16+ (GtkGLArea widget).
5 *
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.
8 */
9
e16f4c87 10#include "qemu/osdep.h"
65b847d2 11#include "qemu/main-loop.h"
925a0400
GH
12
13#include "trace.h"
14
15#include "ui/console.h"
16#include "ui/gtk.h"
17#include "ui/egl-helpers.h"
18
19#include "sysemu/sysemu.h"
20
21static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout)
22{
23 if (vc->gfx.scanout_mode == scanout) {
24 return;
25 }
26
27 vc->gfx.scanout_mode = scanout;
28 if (!vc->gfx.scanout_mode) {
a4f113fd 29 egl_fb_destroy(&vc->gfx.guest_fb);
925a0400
GH
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);
33 }
34 }
35}
36
37/** DisplayState Callbacks (opengl version) **/
38
39void gd_gl_area_draw(VirtualConsole *vc)
40{
55f4b767
DK
41#ifdef CONFIG_GBM
42 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
43#endif
4323118c 44 int ww, wh, ws, y1, y2;
925a0400
GH
45
46 if (!vc->gfx.gls) {
47 return;
48 }
49
50 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
4323118c
AO
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;
925a0400
GH
54
55 if (vc->gfx.scanout_mode) {
a4f113fd 56 if (!vc->gfx.guest_fb.framebuffer) {
925a0400
GH
57 return;
58 }
59
55f4b767
DK
60#ifdef CONFIG_GBM
61 if (dmabuf) {
62 if (!dmabuf->draw_submitted) {
63 return;
64 } else {
65 dmabuf->draw_submitted = false;
66 }
67 }
68#endif
69
a4f113fd 70 glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
925a0400
GH
71 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
72
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,
77 0, 0, ww, wh,
78 GL_COLOR_BUFFER_BIT, GL_NEAREST);
55f4b767
DK
79#ifdef CONFIG_GBM
80 if (dmabuf) {
81 egl_dmabuf_create_sync(dmabuf);
82 }
83#endif
84 glFlush();
85#ifdef CONFIG_GBM
86 if (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);
90 return;
91 }
92 graphic_hw_gl_block(vc->gfx.dcl.con, false);
93 }
94#endif
925a0400
GH
95 } else {
96 if (!vc->gfx.ds) {
97 return;
98 }
99 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
100
101 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
102 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
103 }
104}
105
106void gd_gl_area_update(DisplayChangeListener *dcl,
107 int x, int y, int w, int h)
108{
109 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
110
111 if (!vc->gfx.gls || !vc->gfx.ds) {
112 return;
113 }
114
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);
117 vc->gfx.glupdates++;
118}
119
120void gd_gl_area_refresh(DisplayChangeListener *dcl)
121{
122 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
123
760deab3
NP
124 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
125 vc->window ? vc->window : vc->gfx.drawing_area);
126
925a0400
GH
127 if (!vc->gfx.gls) {
128 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
129 return;
130 }
131 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
46e19e14 132 vc->gfx.gls = qemu_gl_init_shader();
925a0400
GH
133 if (vc->gfx.ds) {
134 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
135 }
136 }
137
138 graphic_hw_update(dcl->con);
139
140 if (vc->gfx.glupdates) {
141 vc->gfx.glupdates = 0;
142 gtk_gl_area_set_scanout_mode(vc, false);
143 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
144 }
145}
146
147void gd_gl_area_switch(DisplayChangeListener *dcl,
148 DisplaySurface *surface)
149{
150 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
151 bool resized = true;
152
153 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
154
155 if (vc->gfx.ds &&
156 surface_width(vc->gfx.ds) == surface_width(surface) &&
157 surface_height(vc->gfx.ds) == surface_height(surface)) {
158 resized = false;
159 }
160
161 if (vc->gfx.gls) {
162 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
163 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
164 surface_gl_create_texture(vc->gfx.gls, surface);
165 }
166 vc->gfx.ds = surface;
167
168 if (resized) {
169 gd_update_windowsize(vc);
170 }
171}
172
09053670
VR
173static int gd_cmp_gl_context_version(int major, int minor, QEMUGLParams *params)
174{
175 if (major > params->major_ver) {
176 return 1;
177 }
178 if (major < params->major_ver) {
179 return -1;
180 }
181 if (minor > params->minor_ver) {
182 return 1;
183 }
184 if (minor < params->minor_ver) {
185 return -1;
186 }
187 return 0;
188}
189
5e79d516 190QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
925a0400
GH
191 QEMUGLParams *params)
192{
5e79d516 193 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
925a0400
GH
194 GdkWindow *window;
195 GdkGLContext *ctx;
196 GError *err = NULL;
09053670 197 int major, minor;
925a0400 198
925a0400
GH
199 window = gtk_widget_get_window(vc->gfx.drawing_area);
200 ctx = gdk_window_create_gl_context(window, &err);
2cd1e3f9
PN
201 if (err) {
202 g_printerr("Create gdk gl context failed: %s\n", err->message);
203 g_error_free(err);
204 return NULL;
205 }
925a0400
GH
206 gdk_gl_context_set_required_version(ctx,
207 params->major_ver,
208 params->minor_ver);
209 gdk_gl_context_realize(ctx, &err);
2cd1e3f9
PN
210 if (err) {
211 g_printerr("Realize gdk gl context failed: %s\n", err->message);
212 g_error_free(err);
213 g_clear_object(&ctx);
214 return NULL;
215 }
09053670
VR
216
217 gdk_gl_context_make_current(ctx);
218 gdk_gl_context_get_version(ctx, &major, &minor);
219 gdk_gl_context_clear_current();
220 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
221
222 if (gd_cmp_gl_context_version(major, minor, params) == -1) {
223 /* created ctx version < requested version */
224 g_clear_object(&ctx);
225 }
226
227 trace_gd_gl_area_create_context(ctx, params->major_ver, params->minor_ver);
925a0400
GH
228 return ctx;
229}
230
5e79d516 231void gd_gl_area_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
925a0400 232{
e561b3b7
VR
233 GdkGLContext *current_ctx = gdk_gl_context_get_current();
234
235 trace_gd_gl_area_destroy_context(ctx, current_ctx);
236 if (ctx == current_ctx) {
237 gdk_gl_context_clear_current();
238 }
239 g_clear_object(&ctx);
925a0400
GH
240}
241
f4c36bda
GH
242void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
243 uint32_t backing_id,
244 bool backing_y_0_top,
245 uint32_t backing_width,
246 uint32_t backing_height,
247 uint32_t x, uint32_t y,
248 uint32_t w, uint32_t h)
925a0400
GH
249{
250 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
251
252 vc->gfx.x = x;
253 vc->gfx.y = y;
254 vc->gfx.w = w;
255 vc->gfx.h = h;
925a0400
GH
256 vc->gfx.y0_top = backing_y_0_top;
257
258 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
259
2ff408de 260 if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
925a0400
GH
261 gtk_gl_area_set_scanout_mode(vc, false);
262 return;
263 }
264
265 gtk_gl_area_set_scanout_mode(vc, true);
74083f9c
GH
266 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
267 backing_id, false);
925a0400
GH
268}
269
568b12fc
MAL
270void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
271{
272 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
273
274 gtk_gl_area_set_scanout_mode(vc, false);
275}
276
925a0400
GH
277void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
278 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
279{
280 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
281
65b847d2
VK
282 if (vc->gfx.guest_fb.dmabuf) {
283 graphic_hw_gl_block(vc->gfx.dcl.con, true);
55f4b767 284 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
65b847d2 285 }
925a0400
GH
286 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
287}
288
2606519b
MAL
289void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
290 QemuDmaBuf *dmabuf)
291{
bc6a3565 292#ifdef CONFIG_GBM
2606519b
MAL
293 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
294
295 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
296 egl_dmabuf_import_texture(dmabuf);
297 if (!dmabuf->texture) {
298 return;
299 }
300
301 gd_gl_area_scanout_texture(dcl, dmabuf->texture,
302 false, dmabuf->width, dmabuf->height,
303 0, 0, dmabuf->width, dmabuf->height);
65b847d2
VK
304
305 if (dmabuf->allow_fences) {
306 vc->gfx.guest_fb.dmabuf = dmabuf;
307 }
2606519b
MAL
308#endif
309}
310
925a0400
GH
311void gtk_gl_area_init(void)
312{
313 display_opengl = 1;
314}
315
5e79d516 316int gd_gl_area_make_current(DisplayGLCtx *dgc,
925a0400
GH
317 QEMUGLContext ctx)
318{
319 gdk_gl_context_make_current(ctx);
320 return 0;
321}