]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk-egl.c
spapr/target-ppc/kvm: Only add hcall-instructions if KVM supports it
[mirror_qemu.git] / ui / gtk-egl.c
CommitLineData
97edf3bd
GH
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
e16f4c87 14#include "qemu/osdep.h"
97edf3bd
GH
15#include "qemu-common.h"
16
17#include "trace.h"
18
19#include "ui/console.h"
20#include "ui/gtk.h"
21#include "ui/egl-helpers.h"
22
23#include "sysemu/sysemu.h"
24
4782aeb7
GH
25static 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 if (vc->gfx.fbo_id) {
34 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
35 GL_COLOR_ATTACHMENT0_EXT,
36 GL_TEXTURE_2D, 0, 0);
37 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
38 glDeleteFramebuffers(1, &vc->gfx.fbo_id);
39 vc->gfx.fbo_id = 0;
40 }
41 if (vc->gfx.surface) {
42 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
43 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
44 }
45 }
46}
47
97edf3bd
GH
48/** DisplayState Callbacks (opengl version) **/
49
50void gd_egl_init(VirtualConsole *vc)
51{
52 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
53 if (!gdk_window) {
54 return;
55 }
56
57#if GTK_CHECK_VERSION(3, 0, 0)
58 Window x11_window = gdk_x11_window_get_xid(gdk_window);
59#else
60 Window x11_window = gdk_x11_drawable_get_xid(gdk_window);
61#endif
62 if (!x11_window) {
63 return;
64 }
65
66 vc->gfx.ectx = qemu_egl_init_ctx();
67 vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window);
68
69 assert(vc->gfx.esurface);
70}
71
72void gd_egl_draw(VirtualConsole *vc)
73{
74 GdkWindow *window;
75 int ww, wh;
76
4782aeb7 77 if (!vc->gfx.gls) {
97edf3bd
GH
78 return;
79 }
80
4782aeb7
GH
81 if (vc->gfx.scanout_mode) {
82 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
83 } else {
84 if (!vc->gfx.ds) {
85 return;
86 }
87 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
88 vc->gfx.esurface, vc->gfx.ectx);
97edf3bd 89
4782aeb7
GH
90 window = gtk_widget_get_window(vc->gfx.drawing_area);
91 gdk_drawable_get_size(window, &ww, &wh);
92 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
93 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
97edf3bd 94
4782aeb7
GH
95 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
96 }
97edf3bd
GH
97}
98
99void gd_egl_update(DisplayChangeListener *dcl,
100 int x, int y, int w, int h)
101{
102 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
103
104 if (!vc->gfx.gls || !vc->gfx.ds) {
105 return;
106 }
107
108 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
109 vc->gfx.esurface, vc->gfx.ectx);
110 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
111 vc->gfx.glupdates++;
112}
113
114void gd_egl_refresh(DisplayChangeListener *dcl)
115{
116 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
117
118 if (!vc->gfx.esurface) {
119 gd_egl_init(vc);
120 if (!vc->gfx.esurface) {
121 return;
122 }
123 vc->gfx.gls = console_gl_init_context();
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;
4782aeb7 133 gtk_egl_set_scanout_mode(vc, false);
97edf3bd
GH
134 gd_egl_draw(vc);
135 }
136}
137
138void gd_egl_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 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
153 vc->gfx.ds = surface;
154 if (vc->gfx.gls) {
155 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
156 }
157
158 if (resized) {
159 gd_update_windowsize(vc);
160 }
161}
162
4782aeb7
GH
163QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
164 QEMUGLParams *params)
165{
166 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
167
168 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
169 vc->gfx.esurface, vc->gfx.ectx);
170 return qemu_egl_create_context(dcl, params);
171}
172
173void gd_egl_scanout(DisplayChangeListener *dcl,
174 uint32_t backing_id, bool backing_y_0_top,
175 uint32_t x, uint32_t y,
176 uint32_t w, uint32_t h)
177{
178 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
179
180 vc->gfx.x = x;
181 vc->gfx.y = y;
182 vc->gfx.w = w;
183 vc->gfx.h = h;
184 vc->gfx.tex_id = backing_id;
185 vc->gfx.y0_top = backing_y_0_top;
186
187 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
188 vc->gfx.esurface, vc->gfx.ectx);
189
190 if (vc->gfx.tex_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
191 gtk_egl_set_scanout_mode(vc, false);
192 return;
193 }
194
195 gtk_egl_set_scanout_mode(vc, true);
196 if (!vc->gfx.fbo_id) {
197 glGenFramebuffers(1, &vc->gfx.fbo_id);
198 }
199
200 glBindFramebuffer(GL_FRAMEBUFFER_EXT, vc->gfx.fbo_id);
201 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
202 GL_TEXTURE_2D, vc->gfx.tex_id, 0);
203}
204
205void gd_egl_scanout_flush(DisplayChangeListener *dcl,
206 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
207{
208 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
209 GdkWindow *window;
210 int ww, wh, y1, y2;
211
212 if (!vc->gfx.scanout_mode) {
213 return;
214 }
215 if (!vc->gfx.fbo_id) {
216 return;
217 }
218
219 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
220 vc->gfx.esurface, vc->gfx.ectx);
221
222 glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.fbo_id);
223 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
224
225 window = gtk_widget_get_window(vc->gfx.drawing_area);
226 gdk_drawable_get_size(window, &ww, &wh);
227 glViewport(0, 0, ww, wh);
228 y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
229 y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
230 glBlitFramebuffer(0, y1, vc->gfx.w, y2,
231 0, 0, ww, wh,
232 GL_COLOR_BUFFER_BIT, GL_NEAREST);
233 glBindFramebuffer(GL_FRAMEBUFFER_EXT, vc->gfx.fbo_id);
234
235 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
236}
237
97edf3bd
GH
238void gtk_egl_init(void)
239{
240 GdkDisplay *gdk_display = gdk_display_get_default();
241 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
242
243 if (qemu_egl_init_dpy(x11_display, false, false) < 0) {
244 return;
245 }
246
247 display_opengl = 1;
248}
4782aeb7
GH
249
250int gd_egl_make_current(DisplayChangeListener *dcl,
251 QEMUGLContext ctx)
252{
253 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
254
255 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
256 vc->gfx.esurface, ctx);
257}