xref: /openbmc/qemu/ui/gtk-egl.c (revision ad1a706f)
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 
14 #include "qemu/osdep.h"
15 
16 #include "trace.h"
17 
18 #include "ui/console.h"
19 #include "ui/gtk.h"
20 #include "ui/egl-helpers.h"
21 #include "ui/shader.h"
22 
23 #include "sysemu/sysemu.h"
24 
25 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
26 {
27     if (vc->gfx.scanout_mode == scanout) {
28         return;
29     }
30 
31     vc->gfx.scanout_mode = scanout;
32     if (!vc->gfx.scanout_mode) {
33         egl_fb_destroy(&vc->gfx.guest_fb);
34         if (vc->gfx.surface) {
35             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
36             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
37         }
38     }
39 }
40 
41 /** DisplayState Callbacks (opengl version) **/
42 
43 void gd_egl_init(VirtualConsole *vc)
44 {
45     GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
46     if (!gdk_window) {
47         return;
48     }
49 
50     Window x11_window = gdk_x11_window_get_xid(gdk_window);
51     if (!x11_window) {
52         return;
53     }
54 
55     vc->gfx.ectx = qemu_egl_init_ctx();
56     vc->gfx.esurface = qemu_egl_init_surface_x11
57         (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
58 
59     assert(vc->gfx.esurface);
60 }
61 
62 void gd_egl_draw(VirtualConsole *vc)
63 {
64     GdkWindow *window;
65     int ww, wh;
66 
67     if (!vc->gfx.gls) {
68         return;
69     }
70 
71     window = gtk_widget_get_window(vc->gfx.drawing_area);
72     ww = gdk_window_get_width(window);
73     wh = gdk_window_get_height(window);
74 
75     if (vc->gfx.scanout_mode) {
76         gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
77 
78         vc->gfx.scale_x = (double)ww / vc->gfx.w;
79         vc->gfx.scale_y = (double)wh / vc->gfx.h;
80     } else {
81         if (!vc->gfx.ds) {
82             return;
83         }
84         eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
85                        vc->gfx.esurface, vc->gfx.ectx);
86 
87         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
88         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
89 
90         eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
91 
92         vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
93         vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
94     }
95 
96     glFlush();
97     graphic_hw_gl_flushed(vc->gfx.dcl.con);
98 }
99 
100 void gd_egl_update(DisplayChangeListener *dcl,
101                    int x, int y, int w, int h)
102 {
103     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
104 
105     if (!vc->gfx.gls || !vc->gfx.ds) {
106         return;
107     }
108 
109     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
110                    vc->gfx.esurface, vc->gfx.ectx);
111     surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
112     vc->gfx.glupdates++;
113 }
114 
115 void gd_egl_refresh(DisplayChangeListener *dcl)
116 {
117     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
118 
119     vc->gfx.dcl.update_interval = gd_monitor_update_interval(
120             vc->window ? vc->window : vc->gfx.drawing_area);
121 
122     if (!vc->gfx.esurface) {
123         gd_egl_init(vc);
124         if (!vc->gfx.esurface) {
125             return;
126         }
127         vc->gfx.gls = qemu_gl_init_shader();
128         if (vc->gfx.ds) {
129             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
130         }
131     }
132 
133     graphic_hw_update(dcl->con);
134 
135     if (vc->gfx.glupdates) {
136         vc->gfx.glupdates = 0;
137         gtk_egl_set_scanout_mode(vc, false);
138         gd_egl_draw(vc);
139     }
140 }
141 
142 void gd_egl_switch(DisplayChangeListener *dcl,
143                    DisplaySurface *surface)
144 {
145     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
146     bool resized = true;
147 
148     trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
149 
150     if (vc->gfx.ds &&
151         surface_width(vc->gfx.ds) == surface_width(surface) &&
152         surface_height(vc->gfx.ds) == surface_height(surface)) {
153         resized = false;
154     }
155 
156     surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
157     vc->gfx.ds = surface;
158     if (vc->gfx.gls) {
159         surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
160     }
161 
162     if (resized) {
163         gd_update_windowsize(vc);
164     }
165 }
166 
167 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
168                                     QEMUGLParams *params)
169 {
170     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
171 
172     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
173                    vc->gfx.esurface, vc->gfx.ectx);
174     return qemu_egl_create_context(dcl, params);
175 }
176 
177 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
178 {
179     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
180 
181     vc->gfx.w = 0;
182     vc->gfx.h = 0;
183     gtk_egl_set_scanout_mode(vc, false);
184 }
185 
186 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
187                             uint32_t backing_id, bool backing_y_0_top,
188                             uint32_t backing_width, uint32_t backing_height,
189                             uint32_t x, uint32_t y,
190                             uint32_t w, uint32_t h)
191 {
192     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
193 
194     vc->gfx.x = x;
195     vc->gfx.y = y;
196     vc->gfx.w = w;
197     vc->gfx.h = h;
198     vc->gfx.y0_top = backing_y_0_top;
199 
200     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
201                    vc->gfx.esurface, vc->gfx.ectx);
202 
203     gtk_egl_set_scanout_mode(vc, true);
204     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
205                          backing_id, false);
206 }
207 
208 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
209                            QemuDmaBuf *dmabuf)
210 {
211 #ifdef CONFIG_GBM
212     egl_dmabuf_import_texture(dmabuf);
213     if (!dmabuf->texture) {
214         return;
215     }
216 
217     gd_egl_scanout_texture(dcl, dmabuf->texture,
218                            false, dmabuf->width, dmabuf->height,
219                            0, 0, dmabuf->width, dmabuf->height);
220 #endif
221 }
222 
223 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
224                           QemuDmaBuf *dmabuf, bool have_hot,
225                           uint32_t hot_x, uint32_t hot_y)
226 {
227 #ifdef CONFIG_GBM
228     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
229 
230     if (dmabuf) {
231         egl_dmabuf_import_texture(dmabuf);
232         if (!dmabuf->texture) {
233             return;
234         }
235         egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
236                              dmabuf->texture, false);
237     } else {
238         egl_fb_destroy(&vc->gfx.cursor_fb);
239     }
240 #endif
241 }
242 
243 void gd_egl_cursor_position(DisplayChangeListener *dcl,
244                             uint32_t pos_x, uint32_t pos_y)
245 {
246     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
247 
248     vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
249     vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
250 }
251 
252 void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
253                            QemuDmaBuf *dmabuf)
254 {
255 #ifdef CONFIG_GBM
256     egl_dmabuf_release_texture(dmabuf);
257 #endif
258 }
259 
260 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
261                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
262 {
263     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
264     GdkWindow *window;
265     int ww, wh;
266 
267     if (!vc->gfx.scanout_mode) {
268         return;
269     }
270     if (!vc->gfx.guest_fb.framebuffer) {
271         return;
272     }
273 
274     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
275                    vc->gfx.esurface, vc->gfx.ectx);
276 
277     window = gtk_widget_get_window(vc->gfx.drawing_area);
278     ww = gdk_window_get_width(window);
279     wh = gdk_window_get_height(window);
280     egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
281     if (vc->gfx.cursor_fb.texture) {
282         egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
283                          vc->gfx.y0_top);
284         egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
285                           vc->gfx.y0_top,
286                           vc->gfx.cursor_x, vc->gfx.cursor_y,
287                           vc->gfx.scale_x, vc->gfx.scale_y);
288     } else {
289         egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
290     }
291 
292     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
293 }
294 
295 void gtk_egl_init(DisplayGLMode mode)
296 {
297     GdkDisplay *gdk_display = gdk_display_get_default();
298     Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
299 
300     if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
301         return;
302     }
303 
304     display_opengl = 1;
305 }
306 
307 int gd_egl_make_current(DisplayChangeListener *dcl,
308                         QEMUGLContext ctx)
309 {
310     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
311 
312     return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
313                           vc->gfx.esurface, ctx);
314 }
315