]>
Commit | Line | Data |
---|---|---|
97edf3bd GH |
1 | /* |
2 | * GTK UI -- egl opengl code. | |
3 | * | |
4 | * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget, | |
5 | * which is GtkDrawingArea like widget with opengl rendering support. | |
6 | * | |
7 | * This code handles opengl support on older gtk versions, using egl | |
8 | * to get a opengl context for the X11 window. | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
13 | ||
e16f4c87 | 14 | #include "qemu/osdep.h" |
97edf3bd GH |
15 | #include "qemu-common.h" |
16 | ||
17 | #include "trace.h" | |
18 | ||
19 | #include "ui/console.h" | |
20 | #include "ui/gtk.h" | |
21 | #include "ui/egl-helpers.h" | |
f1bd3132 | 22 | #include "ui/shader.h" |
97edf3bd GH |
23 | |
24 | #include "sysemu/sysemu.h" | |
25 | ||
4782aeb7 GH |
26 | static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) |
27 | { | |
28 | if (vc->gfx.scanout_mode == scanout) { | |
29 | return; | |
30 | } | |
31 | ||
32 | vc->gfx.scanout_mode = scanout; | |
33 | if (!vc->gfx.scanout_mode) { | |
a4f113fd | 34 | egl_fb_destroy(&vc->gfx.guest_fb); |
4782aeb7 GH |
35 | if (vc->gfx.surface) { |
36 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); | |
37 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); | |
38 | } | |
39 | } | |
40 | } | |
41 | ||
97edf3bd GH |
42 | /** DisplayState Callbacks (opengl version) **/ |
43 | ||
44 | void gd_egl_init(VirtualConsole *vc) | |
45 | { | |
46 | GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area); | |
47 | if (!gdk_window) { | |
48 | return; | |
49 | } | |
50 | ||
51 | #if GTK_CHECK_VERSION(3, 0, 0) | |
52 | Window x11_window = gdk_x11_window_get_xid(gdk_window); | |
53 | #else | |
54 | Window x11_window = gdk_x11_drawable_get_xid(gdk_window); | |
55 | #endif | |
56 | if (!x11_window) { | |
57 | return; | |
58 | } | |
59 | ||
60 | vc->gfx.ectx = qemu_egl_init_ctx(); | |
61 | vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window); | |
62 | ||
63 | assert(vc->gfx.esurface); | |
64 | } | |
65 | ||
66 | void gd_egl_draw(VirtualConsole *vc) | |
67 | { | |
68 | GdkWindow *window; | |
69 | int ww, wh; | |
70 | ||
4782aeb7 | 71 | if (!vc->gfx.gls) { |
97edf3bd GH |
72 | return; |
73 | } | |
74 | ||
4782aeb7 GH |
75 | if (vc->gfx.scanout_mode) { |
76 | gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h); | |
77 | } else { | |
78 | if (!vc->gfx.ds) { | |
79 | return; | |
80 | } | |
81 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, | |
82 | vc->gfx.esurface, vc->gfx.ectx); | |
97edf3bd | 83 | |
4782aeb7 GH |
84 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
85 | gdk_drawable_get_size(window, &ww, &wh); | |
86 | surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); | |
87 | surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); | |
97edf3bd | 88 | |
4782aeb7 GH |
89 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
90 | } | |
97edf3bd GH |
91 | } |
92 | ||
93 | void gd_egl_update(DisplayChangeListener *dcl, | |
94 | int x, int y, int w, int h) | |
95 | { | |
96 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
97 | ||
98 | if (!vc->gfx.gls || !vc->gfx.ds) { | |
99 | return; | |
100 | } | |
101 | ||
102 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, | |
103 | vc->gfx.esurface, vc->gfx.ectx); | |
104 | surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h); | |
105 | vc->gfx.glupdates++; | |
106 | } | |
107 | ||
108 | void gd_egl_refresh(DisplayChangeListener *dcl) | |
109 | { | |
110 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
111 | ||
112 | if (!vc->gfx.esurface) { | |
113 | gd_egl_init(vc); | |
114 | if (!vc->gfx.esurface) { | |
115 | return; | |
116 | } | |
46e19e14 | 117 | vc->gfx.gls = qemu_gl_init_shader(); |
97edf3bd GH |
118 | if (vc->gfx.ds) { |
119 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); | |
120 | } | |
121 | } | |
122 | ||
123 | graphic_hw_update(dcl->con); | |
124 | ||
125 | if (vc->gfx.glupdates) { | |
126 | vc->gfx.glupdates = 0; | |
4782aeb7 | 127 | gtk_egl_set_scanout_mode(vc, false); |
97edf3bd GH |
128 | gd_egl_draw(vc); |
129 | } | |
130 | } | |
131 | ||
132 | void gd_egl_switch(DisplayChangeListener *dcl, | |
133 | DisplaySurface *surface) | |
134 | { | |
135 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
136 | bool resized = true; | |
137 | ||
138 | trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); | |
139 | ||
140 | if (vc->gfx.ds && | |
141 | surface_width(vc->gfx.ds) == surface_width(surface) && | |
142 | surface_height(vc->gfx.ds) == surface_height(surface)) { | |
143 | resized = false; | |
144 | } | |
145 | ||
146 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); | |
147 | vc->gfx.ds = surface; | |
148 | if (vc->gfx.gls) { | |
149 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); | |
150 | } | |
151 | ||
152 | if (resized) { | |
153 | gd_update_windowsize(vc); | |
154 | } | |
155 | } | |
156 | ||
4782aeb7 GH |
157 | QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl, |
158 | QEMUGLParams *params) | |
159 | { | |
160 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
161 | ||
162 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, | |
163 | vc->gfx.esurface, vc->gfx.ectx); | |
164 | return qemu_egl_create_context(dcl, params); | |
165 | } | |
166 | ||
543a7a16 GH |
167 | void gd_egl_scanout_disable(DisplayChangeListener *dcl) |
168 | { | |
169 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
170 | ||
171 | vc->gfx.w = 0; | |
172 | vc->gfx.h = 0; | |
543a7a16 GH |
173 | gtk_egl_set_scanout_mode(vc, false); |
174 | } | |
175 | ||
f4c36bda GH |
176 | void gd_egl_scanout_texture(DisplayChangeListener *dcl, |
177 | uint32_t backing_id, bool backing_y_0_top, | |
178 | uint32_t backing_width, uint32_t backing_height, | |
179 | uint32_t x, uint32_t y, | |
180 | uint32_t w, uint32_t h) | |
4782aeb7 GH |
181 | { |
182 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
183 | ||
184 | vc->gfx.x = x; | |
185 | vc->gfx.y = y; | |
186 | vc->gfx.w = w; | |
187 | vc->gfx.h = h; | |
4782aeb7 GH |
188 | vc->gfx.y0_top = backing_y_0_top; |
189 | ||
190 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, | |
191 | vc->gfx.esurface, vc->gfx.ectx); | |
192 | ||
4782aeb7 | 193 | gtk_egl_set_scanout_mode(vc, true); |
74083f9c GH |
194 | egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, |
195 | backing_id, false); | |
4782aeb7 GH |
196 | } |
197 | ||
70763fea GH |
198 | void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl, |
199 | QemuDmaBuf *dmabuf) | |
200 | { | |
201 | #ifdef CONFIG_OPENGL_DMABUF | |
202 | egl_dmabuf_import_texture(dmabuf); | |
203 | if (!dmabuf->texture) { | |
204 | return; | |
205 | } | |
206 | ||
207 | gd_egl_scanout_texture(dcl, dmabuf->texture, | |
208 | false, dmabuf->width, dmabuf->height, | |
209 | 0, 0, dmabuf->width, dmabuf->height); | |
210 | #endif | |
211 | } | |
212 | ||
f1bd3132 GH |
213 | void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl, |
214 | QemuDmaBuf *dmabuf, bool have_hot, | |
215 | uint32_t hot_x, uint32_t hot_y) | |
216 | { | |
217 | #ifdef CONFIG_OPENGL_DMABUF | |
218 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
219 | ||
220 | if (dmabuf) { | |
221 | egl_dmabuf_import_texture(dmabuf); | |
222 | if (!dmabuf->texture) { | |
223 | return; | |
224 | } | |
225 | egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height, | |
226 | dmabuf->texture, false); | |
227 | } else { | |
228 | egl_fb_destroy(&vc->gfx.cursor_fb); | |
229 | } | |
230 | #endif | |
231 | } | |
232 | ||
233 | void gd_egl_cursor_position(DisplayChangeListener *dcl, | |
234 | uint32_t pos_x, uint32_t pos_y) | |
235 | { | |
236 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
237 | ||
238 | vc->gfx.cursor_x = pos_x; | |
239 | vc->gfx.cursor_y = pos_y; | |
240 | } | |
241 | ||
70763fea GH |
242 | void gd_egl_release_dmabuf(DisplayChangeListener *dcl, |
243 | QemuDmaBuf *dmabuf) | |
244 | { | |
245 | #ifdef CONFIG_OPENGL_DMABUF | |
246 | egl_dmabuf_release_texture(dmabuf); | |
247 | #endif | |
248 | } | |
249 | ||
4782aeb7 GH |
250 | void gd_egl_scanout_flush(DisplayChangeListener *dcl, |
251 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) | |
252 | { | |
253 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
254 | GdkWindow *window; | |
a4f113fd | 255 | int ww, wh; |
4782aeb7 GH |
256 | |
257 | if (!vc->gfx.scanout_mode) { | |
258 | return; | |
259 | } | |
a4f113fd | 260 | if (!vc->gfx.guest_fb.framebuffer) { |
4782aeb7 GH |
261 | return; |
262 | } | |
263 | ||
264 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, | |
265 | vc->gfx.esurface, vc->gfx.ectx); | |
266 | ||
4782aeb7 GH |
267 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
268 | gdk_drawable_get_size(window, &ww, &wh); | |
a4f113fd | 269 | egl_fb_setup_default(&vc->gfx.win_fb, ww, wh); |
f1bd3132 GH |
270 | if (vc->gfx.cursor_fb.texture) { |
271 | egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb, | |
272 | vc->gfx.y0_top); | |
273 | egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb, | |
274 | vc->gfx.y0_top, | |
275 | vc->gfx.cursor_x, vc->gfx.cursor_y); | |
276 | } else { | |
277 | egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top); | |
278 | } | |
4782aeb7 GH |
279 | |
280 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); | |
281 | } | |
282 | ||
54d208ff | 283 | void gtk_egl_init(DisplayGLMode mode) |
97edf3bd GH |
284 | { |
285 | GdkDisplay *gdk_display = gdk_display_get_default(); | |
286 | Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display); | |
287 | ||
54d208ff | 288 | if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) { |
97edf3bd GH |
289 | return; |
290 | } | |
291 | ||
292 | display_opengl = 1; | |
293 | } | |
4782aeb7 GH |
294 | |
295 | int gd_egl_make_current(DisplayChangeListener *dcl, | |
296 | QEMUGLContext ctx) | |
297 | { | |
298 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); | |
299 | ||
300 | return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, | |
301 | vc->gfx.esurface, ctx); | |
302 | } |