]> git.proxmox.com Git - mirror_qemu.git/blob - ui/gtk-gl-area.c
Merge tag 'pull-nbd-2021-11-22' of https://repo.or.cz/qemu/ericb into staging
[mirror_qemu.git] / ui / gtk-gl-area.c
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
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
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
21 static 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) {
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);
33 }
34 }
35 }
36
37 /** DisplayState Callbacks (opengl version) **/
38
39 void gd_gl_area_draw(VirtualConsole *vc)
40 {
41 #ifdef CONFIG_GBM
42 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
43 #endif
44 int ww, wh, ws, y1, y2;
45
46 if (!vc->gfx.gls) {
47 return;
48 }
49
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;
54
55 if (vc->gfx.scanout_mode) {
56 if (!vc->gfx.guest_fb.framebuffer) {
57 return;
58 }
59
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
70 glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
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);
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
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 graphic_hw_gl_flushed(vc->gfx.dcl.con);
106 }
107
108 void gd_gl_area_update(DisplayChangeListener *dcl,
109 int x, int y, int w, int h)
110 {
111 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
112
113 if (!vc->gfx.gls || !vc->gfx.ds) {
114 return;
115 }
116
117 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
118 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
119 vc->gfx.glupdates++;
120 }
121
122 void gd_gl_area_refresh(DisplayChangeListener *dcl)
123 {
124 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
125
126 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
127 vc->window ? vc->window : vc->gfx.drawing_area);
128
129 if (!vc->gfx.gls) {
130 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
131 return;
132 }
133 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
134 vc->gfx.gls = qemu_gl_init_shader();
135 if (vc->gfx.ds) {
136 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
137 }
138 }
139
140 graphic_hw_update(dcl->con);
141
142 if (vc->gfx.glupdates) {
143 vc->gfx.glupdates = 0;
144 gtk_gl_area_set_scanout_mode(vc, false);
145 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
146 }
147 }
148
149 void gd_gl_area_switch(DisplayChangeListener *dcl,
150 DisplaySurface *surface)
151 {
152 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
153 bool resized = true;
154
155 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
156
157 if (vc->gfx.ds &&
158 surface_width(vc->gfx.ds) == surface_width(surface) &&
159 surface_height(vc->gfx.ds) == surface_height(surface)) {
160 resized = false;
161 }
162
163 if (vc->gfx.gls) {
164 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
165 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
166 surface_gl_create_texture(vc->gfx.gls, surface);
167 }
168 vc->gfx.ds = surface;
169
170 if (resized) {
171 gd_update_windowsize(vc);
172 }
173 }
174
175 QEMUGLContext gd_gl_area_create_context(DisplayChangeListener *dcl,
176 QEMUGLParams *params)
177 {
178 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
179 GdkWindow *window;
180 GdkGLContext *ctx;
181 GError *err = NULL;
182
183 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
184 window = gtk_widget_get_window(vc->gfx.drawing_area);
185 ctx = gdk_window_create_gl_context(window, &err);
186 if (err) {
187 g_printerr("Create gdk gl context failed: %s\n", err->message);
188 g_error_free(err);
189 return NULL;
190 }
191 gdk_gl_context_set_required_version(ctx,
192 params->major_ver,
193 params->minor_ver);
194 gdk_gl_context_realize(ctx, &err);
195 if (err) {
196 g_printerr("Realize gdk gl context failed: %s\n", err->message);
197 g_error_free(err);
198 g_clear_object(&ctx);
199 return NULL;
200 }
201 return ctx;
202 }
203
204 void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
205 {
206 /* FIXME */
207 }
208
209 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
210 uint32_t backing_id,
211 bool backing_y_0_top,
212 uint32_t backing_width,
213 uint32_t backing_height,
214 uint32_t x, uint32_t y,
215 uint32_t w, uint32_t h)
216 {
217 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
218
219 vc->gfx.x = x;
220 vc->gfx.y = y;
221 vc->gfx.w = w;
222 vc->gfx.h = h;
223 vc->gfx.y0_top = backing_y_0_top;
224
225 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
226
227 if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
228 gtk_gl_area_set_scanout_mode(vc, false);
229 return;
230 }
231
232 gtk_gl_area_set_scanout_mode(vc, true);
233 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
234 backing_id, false);
235 }
236
237 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
238 {
239 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
240
241 gtk_gl_area_set_scanout_mode(vc, false);
242 }
243
244 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
245 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
246 {
247 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
248
249 if (vc->gfx.guest_fb.dmabuf) {
250 graphic_hw_gl_block(vc->gfx.dcl.con, true);
251 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
252 }
253 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
254 }
255
256 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
257 QemuDmaBuf *dmabuf)
258 {
259 #ifdef CONFIG_GBM
260 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
261
262 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
263 egl_dmabuf_import_texture(dmabuf);
264 if (!dmabuf->texture) {
265 return;
266 }
267
268 gd_gl_area_scanout_texture(dcl, dmabuf->texture,
269 false, dmabuf->width, dmabuf->height,
270 0, 0, dmabuf->width, dmabuf->height);
271
272 if (dmabuf->allow_fences) {
273 vc->gfx.guest_fb.dmabuf = dmabuf;
274 }
275 #endif
276 }
277
278 void gtk_gl_area_init(void)
279 {
280 display_opengl = 1;
281 }
282
283 int gd_gl_area_make_current(DisplayChangeListener *dcl,
284 QEMUGLContext ctx)
285 {
286 gdk_gl_context_make_current(ctx);
287 return 0;
288 }