tests: acpi: add applesmc testcase
[qemu.git] / ui / gtk-gl-area.c
blobfc5a082eb846391b3a22b7f34afbcef1a5dbda40
1 /*
2 * GTK UI -- glarea opengl code.
4 * Requires 3.16+ (GtkGLArea widget).
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 */
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
13 #include "trace.h"
15 #include "ui/console.h"
16 #include "ui/gtk.h"
17 #include "ui/egl-helpers.h"
19 #include "sysemu/sysemu.h"
21 static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout)
23 if (vc->gfx.scanout_mode == scanout) {
24 return;
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);
37 /** DisplayState Callbacks (opengl version) **/
39 void gd_gl_area_draw(VirtualConsole *vc)
41 #ifdef CONFIG_GBM
42 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
43 #endif
44 int ww, wh, ws, y1, y2;
46 if (!vc->gfx.gls) {
47 return;
50 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
51 ws = gdk_window_get_scale_factor(gtk_widget_get_window(vc->gfx.drawing_area));
52 ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area) * ws;
53 wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area) * ws;
55 if (vc->gfx.scanout_mode) {
56 if (!vc->gfx.guest_fb.framebuffer) {
57 return;
60 #ifdef CONFIG_GBM
61 if (dmabuf) {
62 if (!dmabuf->draw_submitted) {
63 return;
64 } else {
65 dmabuf->draw_submitted = false;
68 #endif
70 glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
71 /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
73 glViewport(0, 0, ww, wh);
74 y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
75 y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
76 glBlitFramebuffer(0, y1, vc->gfx.w, y2,
77 0, 0, ww, wh,
78 GL_COLOR_BUFFER_BIT, GL_NEAREST);
79 #ifdef CONFIG_GBM
80 if (dmabuf) {
81 egl_dmabuf_create_sync(dmabuf);
83 #endif
84 glFlush();
85 #ifdef CONFIG_GBM
86 if (dmabuf) {
87 egl_dmabuf_create_fence(dmabuf);
88 if (dmabuf->fence_fd > 0) {
89 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
90 return;
92 graphic_hw_gl_block(vc->gfx.dcl.con, false);
94 #endif
95 } else {
96 if (!vc->gfx.ds) {
97 return;
99 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
101 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
102 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
106 void gd_gl_area_update(DisplayChangeListener *dcl,
107 int x, int y, int w, int h)
109 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
111 if (!vc->gfx.gls || !vc->gfx.ds) {
112 return;
115 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
116 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
117 vc->gfx.glupdates++;
120 void gd_gl_area_refresh(DisplayChangeListener *dcl)
122 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
124 vc->gfx.dcl.update_interval = gd_monitor_update_interval(
125 vc->window ? vc->window : vc->gfx.drawing_area);
127 if (!vc->gfx.gls) {
128 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
129 return;
131 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
132 vc->gfx.gls = qemu_gl_init_shader();
133 if (vc->gfx.ds) {
134 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
138 graphic_hw_update(dcl->con);
140 if (vc->gfx.glupdates) {
141 vc->gfx.glupdates = 0;
142 gtk_gl_area_set_scanout_mode(vc, false);
143 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
147 void gd_gl_area_switch(DisplayChangeListener *dcl,
148 DisplaySurface *surface)
150 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
151 bool resized = true;
153 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
155 if (vc->gfx.ds &&
156 surface_width(vc->gfx.ds) == surface_width(surface) &&
157 surface_height(vc->gfx.ds) == surface_height(surface)) {
158 resized = false;
161 if (vc->gfx.gls) {
162 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
163 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
164 surface_gl_create_texture(vc->gfx.gls, surface);
166 vc->gfx.ds = surface;
168 if (resized) {
169 gd_update_windowsize(vc);
173 QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
174 QEMUGLParams *params)
176 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
177 GdkWindow *window;
178 GdkGLContext *ctx;
179 GError *err = NULL;
181 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
182 window = gtk_widget_get_window(vc->gfx.drawing_area);
183 ctx = gdk_window_create_gl_context(window, &err);
184 if (err) {
185 g_printerr("Create gdk gl context failed: %s\n", err->message);
186 g_error_free(err);
187 return NULL;
189 gdk_gl_context_set_required_version(ctx,
190 params->major_ver,
191 params->minor_ver);
192 gdk_gl_context_realize(ctx, &err);
193 if (err) {
194 g_printerr("Realize gdk gl context failed: %s\n", err->message);
195 g_error_free(err);
196 g_clear_object(&ctx);
197 return NULL;
199 return ctx;
202 void gd_gl_area_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
204 /* FIXME */
207 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
208 uint32_t backing_id,
209 bool backing_y_0_top,
210 uint32_t backing_width,
211 uint32_t backing_height,
212 uint32_t x, uint32_t y,
213 uint32_t w, uint32_t h)
215 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
217 vc->gfx.x = x;
218 vc->gfx.y = y;
219 vc->gfx.w = w;
220 vc->gfx.h = h;
221 vc->gfx.y0_top = backing_y_0_top;
223 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
225 if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
226 gtk_gl_area_set_scanout_mode(vc, false);
227 return;
230 gtk_gl_area_set_scanout_mode(vc, true);
231 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
232 backing_id, false);
235 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
237 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
239 gtk_gl_area_set_scanout_mode(vc, false);
242 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
243 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
245 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
247 if (vc->gfx.guest_fb.dmabuf) {
248 graphic_hw_gl_block(vc->gfx.dcl.con, true);
249 vc->gfx.guest_fb.dmabuf->draw_submitted = true;
251 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
254 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
255 QemuDmaBuf *dmabuf)
257 #ifdef CONFIG_GBM
258 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
260 gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
261 egl_dmabuf_import_texture(dmabuf);
262 if (!dmabuf->texture) {
263 return;
266 gd_gl_area_scanout_texture(dcl, dmabuf->texture,
267 false, dmabuf->width, dmabuf->height,
268 0, 0, dmabuf->width, dmabuf->height);
270 if (dmabuf->allow_fences) {
271 vc->gfx.guest_fb.dmabuf = dmabuf;
273 #endif
276 void gtk_gl_area_init(void)
278 display_opengl = 1;
281 int gd_gl_area_make_current(DisplayGLCtx *dgc,
282 QEMUGLContext ctx)
284 gdk_gl_context_make_current(ctx);
285 return 0;