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