]>
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) { | |
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 | ||
39 | void gd_gl_area_draw(VirtualConsole *vc) | |
40 | { | |
55f4b767 DK |
41 | #ifdef CONFIG_GBM |
42 | QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; | |
43 | #endif | |
925a0400 GH |
44 | int ww, wh, 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 | ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area); | |
52 | wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area); | |
53 | ||
54 | if (vc->gfx.scanout_mode) { | |
a4f113fd | 55 | if (!vc->gfx.guest_fb.framebuffer) { |
925a0400 GH |
56 | return; |
57 | } | |
58 | ||
55f4b767 DK |
59 | #ifdef CONFIG_GBM |
60 | if (dmabuf) { | |
61 | if (!dmabuf->draw_submitted) { | |
62 | return; | |
63 | } else { | |
64 | dmabuf->draw_submitted = false; | |
65 | } | |
66 | } | |
67 | #endif | |
68 | ||
a4f113fd | 69 | glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer); |
925a0400 GH |
70 | /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */ |
71 | ||
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, | |
76 | 0, 0, ww, wh, | |
77 | GL_COLOR_BUFFER_BIT, GL_NEAREST); | |
55f4b767 DK |
78 | #ifdef CONFIG_GBM |
79 | if (dmabuf) { | |
80 | egl_dmabuf_create_sync(dmabuf); | |
81 | } | |
82 | #endif | |
83 | glFlush(); | |
84 | #ifdef CONFIG_GBM | |
85 | if (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); | |
89 | return; | |
90 | } | |
91 | graphic_hw_gl_block(vc->gfx.dcl.con, false); | |
92 | } | |
93 | #endif | |
925a0400 GH |
94 | } else { |
95 | if (!vc->gfx.ds) { | |
96 | return; | |
97 | } | |
98 | gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); | |
99 | ||
100 | surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); | |
101 | surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); | |
102 | } | |
3cddb8b9 | 103 | |
3cddb8b9 | 104 | graphic_hw_gl_flushed(vc->gfx.dcl.con); |
925a0400 GH |
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++; | |
119 | } | |
120 | ||
121 | void gd_gl_area_refresh(DisplayChangeListener *dcl) | |
122 | { | |
123 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
124 | ||
760deab3 NP |
125 | vc->gfx.dcl.update_interval = gd_monitor_update_interval( |
126 | vc->window ? vc->window : vc->gfx.drawing_area); | |
127 | ||
925a0400 GH |
128 | if (!vc->gfx.gls) { |
129 | if (!gtk_widget_get_realized(vc->gfx.drawing_area)) { | |
130 | return; | |
131 | } | |
132 | gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); | |
46e19e14 | 133 | vc->gfx.gls = qemu_gl_init_shader(); |
925a0400 GH |
134 | if (vc->gfx.ds) { |
135 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); | |
136 | } | |
137 | } | |
138 | ||
139 | graphic_hw_update(dcl->con); | |
140 | ||
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)); | |
145 | } | |
146 | } | |
147 | ||
148 | void gd_gl_area_switch(DisplayChangeListener *dcl, | |
149 | DisplaySurface *surface) | |
150 | { | |
151 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
152 | bool resized = true; | |
153 | ||
154 | trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); | |
155 | ||
156 | if (vc->gfx.ds && | |
157 | surface_width(vc->gfx.ds) == surface_width(surface) && | |
158 | surface_height(vc->gfx.ds) == surface_height(surface)) { | |
159 | resized = false; | |
160 | } | |
161 | ||
162 | if (vc->gfx.gls) { | |
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); | |
166 | } | |
167 | vc->gfx.ds = surface; | |
168 | ||
169 | if (resized) { | |
170 | gd_update_windowsize(vc); | |
171 | } | |
172 | } | |
173 | ||
174 | QEMUGLContext gd_gl_area_create_context(DisplayChangeListener *dcl, | |
175 | QEMUGLParams *params) | |
176 | { | |
177 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
178 | GdkWindow *window; | |
179 | GdkGLContext *ctx; | |
180 | GError *err = NULL; | |
181 | ||
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); | |
2cd1e3f9 PN |
185 | if (err) { |
186 | g_printerr("Create gdk gl context failed: %s\n", err->message); | |
187 | g_error_free(err); | |
188 | return NULL; | |
189 | } | |
925a0400 GH |
190 | gdk_gl_context_set_required_version(ctx, |
191 | params->major_ver, | |
192 | params->minor_ver); | |
193 | gdk_gl_context_realize(ctx, &err); | |
2cd1e3f9 PN |
194 | if (err) { |
195 | g_printerr("Realize gdk gl context failed: %s\n", err->message); | |
196 | g_error_free(err); | |
197 | g_clear_object(&ctx); | |
198 | return NULL; | |
199 | } | |
925a0400 GH |
200 | return ctx; |
201 | } | |
202 | ||
203 | void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx) | |
204 | { | |
205 | /* FIXME */ | |
206 | } | |
207 | ||
f4c36bda GH |
208 | void gd_gl_area_scanout_texture(DisplayChangeListener *dcl, |
209 | uint32_t backing_id, | |
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) | |
925a0400 GH |
215 | { |
216 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
217 | ||
218 | vc->gfx.x = x; | |
219 | vc->gfx.y = y; | |
220 | vc->gfx.w = w; | |
221 | vc->gfx.h = h; | |
925a0400 GH |
222 | vc->gfx.y0_top = backing_y_0_top; |
223 | ||
224 | gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); | |
225 | ||
2ff408de | 226 | if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) { |
925a0400 GH |
227 | gtk_gl_area_set_scanout_mode(vc, false); |
228 | return; | |
229 | } | |
230 | ||
231 | gtk_gl_area_set_scanout_mode(vc, true); | |
74083f9c GH |
232 | egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, |
233 | backing_id, false); | |
925a0400 GH |
234 | } |
235 | ||
568b12fc MAL |
236 | void gd_gl_area_scanout_disable(DisplayChangeListener *dcl) |
237 | { | |
238 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
239 | ||
240 | gtk_gl_area_set_scanout_mode(vc, false); | |
241 | } | |
242 | ||
925a0400 GH |
243 | void gd_gl_area_scanout_flush(DisplayChangeListener *dcl, |
244 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) | |
245 | { | |
246 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
247 | ||
65b847d2 VK |
248 | if (vc->gfx.guest_fb.dmabuf) { |
249 | graphic_hw_gl_block(vc->gfx.dcl.con, true); | |
55f4b767 | 250 | vc->gfx.guest_fb.dmabuf->draw_submitted = true; |
65b847d2 | 251 | } |
925a0400 GH |
252 | gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area)); |
253 | } | |
254 | ||
2606519b MAL |
255 | void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl, |
256 | QemuDmaBuf *dmabuf) | |
257 | { | |
bc6a3565 | 258 | #ifdef CONFIG_GBM |
2606519b MAL |
259 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
260 | ||
261 | gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); | |
262 | egl_dmabuf_import_texture(dmabuf); | |
263 | if (!dmabuf->texture) { | |
264 | return; | |
265 | } | |
266 | ||
267 | gd_gl_area_scanout_texture(dcl, dmabuf->texture, | |
268 | false, dmabuf->width, dmabuf->height, | |
269 | 0, 0, dmabuf->width, dmabuf->height); | |
65b847d2 VK |
270 | |
271 | if (dmabuf->allow_fences) { | |
272 | vc->gfx.guest_fb.dmabuf = dmabuf; | |
273 | } | |
2606519b MAL |
274 | #endif |
275 | } | |
276 | ||
925a0400 GH |
277 | void gtk_gl_area_init(void) |
278 | { | |
279 | display_opengl = 1; | |
280 | } | |
281 | ||
925a0400 GH |
282 | int gd_gl_area_make_current(DisplayChangeListener *dcl, |
283 | QEMUGLContext ctx) | |
284 | { | |
285 | gdk_gl_context_make_current(ctx); | |
286 | return 0; | |
287 | } |