ui: introduce enum to track VNC client framebuffer update request state
[qemu/ar7.git] / ui / gtk-egl.c
blobeb86c26a1d131b62722d0a7fe1269ccad3f7785b
1 /*
2 * GTK UI -- egl opengl code.
4 * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget,
5 * which is GtkDrawingArea like widget with opengl rendering support.
7 * This code handles opengl support on older gtk versions, using egl
8 * to get a opengl context for the X11 window.
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.
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
17 #include "trace.h"
19 #include "ui/console.h"
20 #include "ui/gtk.h"
21 #include "ui/egl-helpers.h"
23 #include "sysemu/sysemu.h"
25 static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
27 if (vc->gfx.scanout_mode == scanout) {
28 return;
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);
41 /** DisplayState Callbacks (opengl version) **/
43 void gd_egl_init(VirtualConsole *vc)
45 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
46 if (!gdk_window) {
47 return;
50 #if GTK_CHECK_VERSION(3, 0, 0)
51 Window x11_window = gdk_x11_window_get_xid(gdk_window);
52 #else
53 Window x11_window = gdk_x11_drawable_get_xid(gdk_window);
54 #endif
55 if (!x11_window) {
56 return;
59 vc->gfx.ectx = qemu_egl_init_ctx();
60 vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window);
62 assert(vc->gfx.esurface);
65 void gd_egl_draw(VirtualConsole *vc)
67 GdkWindow *window;
68 int ww, wh;
70 if (!vc->gfx.gls) {
71 return;
74 if (vc->gfx.scanout_mode) {
75 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
76 } else {
77 if (!vc->gfx.ds) {
78 return;
80 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
81 vc->gfx.esurface, vc->gfx.ectx);
83 window = gtk_widget_get_window(vc->gfx.drawing_area);
84 gdk_drawable_get_size(window, &ww, &wh);
85 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
86 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
88 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
92 void gd_egl_update(DisplayChangeListener *dcl,
93 int x, int y, int w, int h)
95 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
97 if (!vc->gfx.gls || !vc->gfx.ds) {
98 return;
101 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
102 vc->gfx.esurface, vc->gfx.ectx);
103 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
104 vc->gfx.glupdates++;
107 void gd_egl_refresh(DisplayChangeListener *dcl)
109 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
111 if (!vc->gfx.esurface) {
112 gd_egl_init(vc);
113 if (!vc->gfx.esurface) {
114 return;
116 vc->gfx.gls = qemu_gl_init_shader();
117 if (vc->gfx.ds) {
118 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
122 graphic_hw_update(dcl->con);
124 if (vc->gfx.glupdates) {
125 vc->gfx.glupdates = 0;
126 gtk_egl_set_scanout_mode(vc, false);
127 gd_egl_draw(vc);
131 void gd_egl_switch(DisplayChangeListener *dcl,
132 DisplaySurface *surface)
134 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
135 bool resized = true;
137 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
139 if (vc->gfx.ds &&
140 surface_width(vc->gfx.ds) == surface_width(surface) &&
141 surface_height(vc->gfx.ds) == surface_height(surface)) {
142 resized = false;
145 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
146 vc->gfx.ds = surface;
147 if (vc->gfx.gls) {
148 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
151 if (resized) {
152 gd_update_windowsize(vc);
156 QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
157 QEMUGLParams *params)
159 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
161 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
162 vc->gfx.esurface, vc->gfx.ectx);
163 return qemu_egl_create_context(dcl, params);
166 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
168 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
170 vc->gfx.w = 0;
171 vc->gfx.h = 0;
172 gtk_egl_set_scanout_mode(vc, false);
175 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
176 uint32_t backing_id, bool backing_y_0_top,
177 uint32_t backing_width, uint32_t backing_height,
178 uint32_t x, uint32_t y,
179 uint32_t w, uint32_t h)
181 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
183 vc->gfx.x = x;
184 vc->gfx.y = y;
185 vc->gfx.w = w;
186 vc->gfx.h = h;
187 vc->gfx.y0_top = backing_y_0_top;
189 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
190 vc->gfx.esurface, vc->gfx.ectx);
192 gtk_egl_set_scanout_mode(vc, true);
193 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
194 backing_id, false);
197 void gd_egl_scanout_flush(DisplayChangeListener *dcl,
198 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
200 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
201 GdkWindow *window;
202 int ww, wh;
204 if (!vc->gfx.scanout_mode) {
205 return;
207 if (!vc->gfx.guest_fb.framebuffer) {
208 return;
211 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
212 vc->gfx.esurface, vc->gfx.ectx);
214 window = gtk_widget_get_window(vc->gfx.drawing_area);
215 gdk_drawable_get_size(window, &ww, &wh);
216 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
217 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
219 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
222 void gtk_egl_init(void)
224 GdkDisplay *gdk_display = gdk_display_get_default();
225 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
227 if (qemu_egl_init_dpy_x11(x11_display) < 0) {
228 return;
231 display_opengl = 1;
234 int gd_egl_make_current(DisplayChangeListener *dcl,
235 QEMUGLContext ctx)
237 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
239 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
240 vc->gfx.esurface, ctx);