blob: eb86c26a1d131b62722d0a7fe1269ccad3f7785b [file] [log] [blame]
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +01001/*
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
Peter Maydelle16f4c82016-01-29 17:49:51 +000014#include "qemu/osdep.h"
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +010015#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
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +020025static 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) {
Gerd Hoffmanna4f113f2017-06-14 10:41:49 +020033 egl_fb_destroy(&vc->gfx.guest_fb);
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +020034 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
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +010041/** DisplayState Callbacks (opengl version) **/
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
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +020070 if (!vc->gfx.gls) {
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +010071 return;
72 }
73
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +020074 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);
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +010082
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +020083 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);
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +010087
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +020088 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
89 }
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +010090}
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 }
Gerd Hoffmann46e19e12017-10-10 15:54:49 +0200116 vc->gfx.gls = qemu_gl_init_shader();
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +0100117 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;
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200126 gtk_egl_set_scanout_mode(vc, false);
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +0100127 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
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200156QEMUGLContext 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
Gerd Hoffmann543a7a12017-02-21 10:37:21 +0100166void 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;
Gerd Hoffmann543a7a12017-02-21 10:37:21 +0100172 gtk_egl_set_scanout_mode(vc, false);
173}
174
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +0100175void 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)
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200180{
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;
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200187 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
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200192 gtk_egl_set_scanout_mode(vc, true);
Gerd Hoffmann74083f92017-09-27 13:50:31 +0200193 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
194 backing_id, false);
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200195}
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;
Gerd Hoffmanna4f113f2017-06-14 10:41:49 +0200202 int ww, wh;
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200203
204 if (!vc->gfx.scanout_mode) {
205 return;
206 }
Gerd Hoffmanna4f113f2017-06-14 10:41:49 +0200207 if (!vc->gfx.guest_fb.framebuffer) {
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200208 return;
209 }
210
211 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
212 vc->gfx.esurface, vc->gfx.ectx);
213
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200214 window = gtk_widget_get_window(vc->gfx.drawing_area);
215 gdk_drawable_get_size(window, &ww, &wh);
Gerd Hoffmanna4f113f2017-06-14 10:41:49 +0200216 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);
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200218
219 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
220}
221
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +0100222void 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
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200227 if (qemu_egl_init_dpy_x11(x11_display) < 0) {
Gerd Hoffmann97edf3b2015-01-20 12:43:28 +0100228 return;
229 }
230
231 display_opengl = 1;
232}
Gerd Hoffmann4782aeb2015-05-08 11:30:51 +0200233
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}