]> git.proxmox.com Git - mirror_qemu.git/blob - ui/gtk-gl-area.c
ui/gtk: Update the refresh rate for gl-area too
[mirror_qemu.git] / ui / gtk-gl-area.c
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 #include "qemu/main-loop.h"
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) {
29 egl_fb_destroy(&vc->gfx.guest_fb);
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 {
41 int ww, wh, y1, y2;
42
43 if (!vc->gfx.gls) {
44 return;
45 }
46
47 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
48 ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area);
49 wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area);
50
51 if (vc->gfx.scanout_mode) {
52 if (!vc->gfx.guest_fb.framebuffer) {
53 return;
54 }
55
56 glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
57 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
58
59 glViewport(0, 0, ww, wh);
60 y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
61 y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
62 glBlitFramebuffer(0, y1, vc->gfx.w, y2,
63 0, 0, ww, wh,
64 GL_COLOR_BUFFER_BIT, GL_NEAREST);
65 } else {
66 if (!vc->gfx.ds) {
67 return;
68 }
69 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
70
71 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
72 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
73 }
74
75 #ifdef CONFIG_GBM
76 if (vc->gfx.guest_fb.dmabuf) {
77 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
78 }
79 #endif
80
81 glFlush();
82 #ifdef CONFIG_GBM
83 if (vc->gfx.guest_fb.dmabuf) {
84 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
85
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
94 graphic_hw_gl_flushed(vc->gfx.dcl.con);
95 }
96
97 void gd_gl_area_update(DisplayChangeListener *dcl,
98 int x, int y, int w, int h)
99 {
100 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
101
102 if (!vc->gfx.gls || !vc->gfx.ds) {
103 return;
104 }
105
106 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
107 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
108 vc->gfx.glupdates++;
109 }
110
111 void gd_gl_area_refresh(DisplayChangeListener *dcl)
112 {
113 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
114
115 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
116 vc->window ? vc->window : vc->gfx.drawing_area);
117
118 if (!vc->gfx.gls) {
119 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
120 return;
121 }
122 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
123 vc->gfx.gls = qemu_gl_init_shader();
124 if (vc->gfx.ds) {
125 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
126 }
127 }
128
129 graphic_hw_update(dcl->con);
130
131 if (vc->gfx.glupdates) {
132 vc->gfx.glupdates = 0;
133 gtk_gl_area_set_scanout_mode(vc, false);
134 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
135 }
136 }
137
138 void gd_gl_area_switch(DisplayChangeListener *dcl,
139 DisplaySurface *surface)
140 {
141 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
142 bool resized = true;
143
144 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
145
146 if (vc->gfx.ds &&
147 surface_width(vc->gfx.ds) == surface_width(surface) &&
148 surface_height(vc->gfx.ds) == surface_height(surface)) {
149 resized = false;
150 }
151
152 if (vc->gfx.gls) {
153 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
154 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
155 surface_gl_create_texture(vc->gfx.gls, surface);
156 }
157 vc->gfx.ds = surface;
158
159 if (resized) {
160 gd_update_windowsize(vc);
161 }
162 }
163
164 QEMUGLContext gd_gl_area_create_context(DisplayChangeListener *dcl,
165 QEMUGLParams *params)
166 {
167 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
168 GdkWindow *window;
169 GdkGLContext *ctx;
170 GError *err = NULL;
171
172 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
173 window = gtk_widget_get_window(vc->gfx.drawing_area);
174 ctx = gdk_window_create_gl_context(window, &err);
175 if (err) {
176 g_printerr("Create gdk gl context failed: %s\n", err->message);
177 g_error_free(err);
178 return NULL;
179 }
180 gdk_gl_context_set_required_version(ctx,
181 params->major_ver,
182 params->minor_ver);
183 gdk_gl_context_realize(ctx, &err);
184 if (err) {
185 g_printerr("Realize gdk gl context failed: %s\n", err->message);
186 g_error_free(err);
187 g_clear_object(&ctx);
188 return NULL;
189 }
190 return ctx;
191 }
192
193 void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
194 {
195 /* FIXME */
196 }
197
198 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
199 uint32_t backing_id,
200 bool backing_y_0_top,
201 uint32_t backing_width,
202 uint32_t backing_height,
203 uint32_t x, uint32_t y,
204 uint32_t w, uint32_t h)
205 {
206 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
207
208 vc->gfx.x = x;
209 vc->gfx.y = y;
210 vc->gfx.w = w;
211 vc->gfx.h = h;
212 vc->gfx.y0_top = backing_y_0_top;
213
214 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
215
216 if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
217 gtk_gl_area_set_scanout_mode(vc, false);
218 return;
219 }
220
221 gtk_gl_area_set_scanout_mode(vc, true);
222 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
223 backing_id, false);
224 }
225
226 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
227 {
228 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
229
230 gtk_gl_area_set_scanout_mode(vc, false);
231 }
232
233 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
234 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
235 {
236 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
237
238 if (vc->gfx.guest_fb.dmabuf) {
239 graphic_hw_gl_block(vc->gfx.dcl.con, true);
240 }
241 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
242 }
243
244 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
245 QemuDmaBuf *dmabuf)
246 {
247 #ifdef CONFIG_GBM
248 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
249
250 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
251 egl_dmabuf_import_texture(dmabuf);
252 if (!dmabuf->texture) {
253 return;
254 }
255
256 gd_gl_area_scanout_texture(dcl, dmabuf->texture,
257 false, dmabuf->width, dmabuf->height,
258 0, 0, dmabuf->width, dmabuf->height);
259
260 if (dmabuf->allow_fences) {
261 vc->gfx.guest_fb.dmabuf = dmabuf;
262 }
263 #endif
264 }
265
266 void gtk_gl_area_init(void)
267 {
268 display_opengl = 1;
269 }
270
271 int gd_gl_area_make_current(DisplayChangeListener *dcl,
272 QEMUGLContext ctx)
273 {
274 gdk_gl_context_make_current(ctx);
275 return 0;
276 }