xref: /openbmc/qemu/ui/gtk-gl-area.c (revision 0694dabe)
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 
12 #include "trace.h"
13 
14 #include "ui/console.h"
15 #include "ui/gtk.h"
16 #include "ui/egl-helpers.h"
17 
18 #include "sysemu/sysemu.h"
19 
20 static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout)
21 {
22     if (vc->gfx.scanout_mode == scanout) {
23         return;
24     }
25 
26     vc->gfx.scanout_mode = scanout;
27     if (!vc->gfx.scanout_mode) {
28         egl_fb_destroy(&vc->gfx.guest_fb);
29         if (vc->gfx.surface) {
30             surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
31             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
32         }
33     }
34 }
35 
36 /** DisplayState Callbacks (opengl version) **/
37 
38 void gd_gl_area_draw(VirtualConsole *vc)
39 {
40     int ww, wh, y1, y2;
41 
42     if (!vc->gfx.gls) {
43         return;
44     }
45 
46     gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
47     ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area);
48     wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area);
49 
50     if (vc->gfx.scanout_mode) {
51         if (!vc->gfx.guest_fb.framebuffer) {
52             return;
53         }
54 
55         glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
56         /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
57 
58         glViewport(0, 0, ww, wh);
59         y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
60         y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
61         glBlitFramebuffer(0, y1, vc->gfx.w, y2,
62                           0, 0, ww, wh,
63                           GL_COLOR_BUFFER_BIT, GL_NEAREST);
64     } else {
65         if (!vc->gfx.ds) {
66             return;
67         }
68         gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
69 
70         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
71         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
72     }
73 
74     glFlush();
75     graphic_hw_gl_flushed(vc->gfx.dcl.con);
76 }
77 
78 void gd_gl_area_update(DisplayChangeListener *dcl,
79                    int x, int y, int w, int h)
80 {
81     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
82 
83     if (!vc->gfx.gls || !vc->gfx.ds) {
84         return;
85     }
86 
87     gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
88     surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
89     vc->gfx.glupdates++;
90 }
91 
92 void gd_gl_area_refresh(DisplayChangeListener *dcl)
93 {
94     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
95 
96     if (!vc->gfx.gls) {
97         if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
98             return;
99         }
100         gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
101         vc->gfx.gls = qemu_gl_init_shader();
102         if (vc->gfx.ds) {
103             surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
104         }
105     }
106 
107     graphic_hw_update(dcl->con);
108 
109     if (vc->gfx.glupdates) {
110         vc->gfx.glupdates = 0;
111         gtk_gl_area_set_scanout_mode(vc, false);
112         gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
113     }
114 }
115 
116 void gd_gl_area_switch(DisplayChangeListener *dcl,
117                        DisplaySurface *surface)
118 {
119     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
120     bool resized = true;
121 
122     trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
123 
124     if (vc->gfx.ds &&
125         surface_width(vc->gfx.ds) == surface_width(surface) &&
126         surface_height(vc->gfx.ds) == surface_height(surface)) {
127         resized = false;
128     }
129 
130     if (vc->gfx.gls) {
131         gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
132         surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
133         surface_gl_create_texture(vc->gfx.gls, surface);
134     }
135     vc->gfx.ds = surface;
136 
137     if (resized) {
138         gd_update_windowsize(vc);
139     }
140 }
141 
142 QEMUGLContext gd_gl_area_create_context(DisplayChangeListener *dcl,
143                                         QEMUGLParams *params)
144 {
145     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
146     GdkWindow *window;
147     GdkGLContext *ctx;
148     GError *err = NULL;
149 
150     gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
151     window = gtk_widget_get_window(vc->gfx.drawing_area);
152     ctx = gdk_window_create_gl_context(window, &err);
153     if (err) {
154         g_printerr("Create gdk gl context failed: %s\n", err->message);
155         g_error_free(err);
156         return NULL;
157     }
158     gdk_gl_context_set_required_version(ctx,
159                                         params->major_ver,
160                                         params->minor_ver);
161     gdk_gl_context_realize(ctx, &err);
162     if (err) {
163         g_printerr("Realize gdk gl context failed: %s\n", err->message);
164         g_error_free(err);
165         g_clear_object(&ctx);
166         return NULL;
167     }
168     return ctx;
169 }
170 
171 void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
172 {
173     /* FIXME */
174 }
175 
176 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
177                                 uint32_t backing_id,
178                                 bool backing_y_0_top,
179                                 uint32_t backing_width,
180                                 uint32_t backing_height,
181                                 uint32_t x, uint32_t y,
182                                 uint32_t w, uint32_t h)
183 {
184     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
185 
186     vc->gfx.x = x;
187     vc->gfx.y = y;
188     vc->gfx.w = w;
189     vc->gfx.h = h;
190     vc->gfx.y0_top = backing_y_0_top;
191 
192     gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
193 
194     if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
195         gtk_gl_area_set_scanout_mode(vc, false);
196         return;
197     }
198 
199     gtk_gl_area_set_scanout_mode(vc, true);
200     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
201                          backing_id, false);
202 }
203 
204 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
205 {
206     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
207 
208     gtk_gl_area_set_scanout_mode(vc, false);
209 }
210 
211 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
212                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
213 {
214     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
215 
216     gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
217 }
218 
219 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
220                                QemuDmaBuf *dmabuf)
221 {
222 #ifdef CONFIG_OPENGL_DMABUF
223     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
224 
225     gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
226     egl_dmabuf_import_texture(dmabuf);
227     if (!dmabuf->texture) {
228         return;
229     }
230 
231     gd_gl_area_scanout_texture(dcl, dmabuf->texture,
232                                false, dmabuf->width, dmabuf->height,
233                                0, 0, dmabuf->width, dmabuf->height);
234 #endif
235 }
236 
237 void gtk_gl_area_init(void)
238 {
239     display_opengl = 1;
240 }
241 
242 int gd_gl_area_make_current(DisplayChangeListener *dcl,
243                             QEMUGLContext ctx)
244 {
245     gdk_gl_context_make_current(ctx);
246     return 0;
247 }
248