1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "qemu/osdep.h"
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
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 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);
37 }
38 }
39}
40
41
42
43void gd_egl_init(VirtualConsole *vc)
44{
45 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
46 if (!gdk_window) {
47 return;
48 }
49
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;
57 }
58
59 vc->gfx.ectx = qemu_egl_init_ctx();
60 vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window);
61
62 assert(vc->gfx.esurface);
63}
64
65void gd_egl_draw(VirtualConsole *vc)
66{
67 GdkWindow *window;
68 int ww, wh;
69
70 if (!vc->gfx.gls) {
71 return;
72 }
73
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;
79 }
80 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
81 vc->gfx.esurface, vc->gfx.ectx);
82
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);
87
88 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
89 }
90}
91
92void gd_egl_update(DisplayChangeListener *dcl,
93 int x, int y, int w, int h)
94{
95 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
96
97 if (!vc->gfx.gls || !vc->gfx.ds) {
98 return;
99 }
100
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++;
105}
106
107void gd_egl_refresh(DisplayChangeListener *dcl)
108{
109 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
110
111 if (!vc->gfx.esurface) {
112 gd_egl_init(vc);
113 if (!vc->gfx.esurface) {
114 return;
115 }
116 vc->gfx.gls = console_gl_init_context();
117 if (vc->gfx.ds) {
118 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
119 }
120 }
121
122 graphic_hw_update(dcl->con);
123
124 if (vc->gfx.glupdates) {
125 vc->gfx.glupdates = 0;
126 gtk_egl_set_scanout_mode(vc, false);
127 gd_egl_draw(vc);
128 }
129}
130
131void gd_egl_switch(DisplayChangeListener *dcl,
132 DisplaySurface *surface)
133{
134 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
135 bool resized = true;
136
137 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
138
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;
143 }
144
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);
149 }
150
151 if (resized) {
152 gd_update_windowsize(vc);
153 }
154}
155
156QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl,
157 QEMUGLParams *params)
158{
159 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
160
161 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
162 vc->gfx.esurface, vc->gfx.ectx);
163 return qemu_egl_create_context(dcl, params);
164}
165
166void gd_egl_scanout_disable(DisplayChangeListener *dcl)
167{
168 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
169
170 vc->gfx.w = 0;
171 vc->gfx.h = 0;
172 gtk_egl_set_scanout_mode(vc, false);
173}
174
175void 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)
180{
181 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
182
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;
188
189 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
190 vc->gfx.esurface, vc->gfx.ectx);
191
192 gtk_egl_set_scanout_mode(vc, true);
193 egl_fb_create_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
194 backing_id);
195}
196
197void gd_egl_scanout_flush(DisplayChangeListener *dcl,
198 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
199{
200 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
201 GdkWindow *window;
202 int ww, wh;
203
204 if (!vc->gfx.scanout_mode) {
205 return;
206 }
207 if (!vc->gfx.guest_fb.framebuffer) {
208 return;
209 }
210
211 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
212 vc->gfx.esurface, vc->gfx.ectx);
213
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);
218
219 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
220}
221
222void gtk_egl_init(void)
223{
224 GdkDisplay *gdk_display = gdk_display_get_default();
225 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
226
227 if (qemu_egl_init_dpy_x11(x11_display) < 0) {
228 return;
229 }
230
231 display_opengl = 1;
232}
233
234int gd_egl_make_current(DisplayChangeListener *dcl,
235 QEMUGLContext ctx)
236{
237 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
238
239 return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
240 vc->gfx.esurface, ctx);
241}
242