Merge remote-tracking branch 'remotes/kraxel/tags/pull-seabios-1.8.2-20150617-1'...
[qemu/ar7.git] / ui / gtk-egl.c
blob15b41f2bab0baf438318115565138c432641c8f7
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-common.h"
16 #include "trace.h"
18 #include "ui/console.h"
19 #include "ui/gtk.h"
20 #include "ui/egl-helpers.h"
22 #include "sysemu/sysemu.h"
24 /** DisplayState Callbacks (opengl version) **/
26 void gd_egl_init(VirtualConsole *vc)
28 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
29 if (!gdk_window) {
30 return;
33 #if GTK_CHECK_VERSION(3, 0, 0)
34 Window x11_window = gdk_x11_window_get_xid(gdk_window);
35 #else
36 Window x11_window = gdk_x11_drawable_get_xid(gdk_window);
37 #endif
38 if (!x11_window) {
39 return;
42 vc->gfx.ectx = qemu_egl_init_ctx();
43 vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window);
45 assert(vc->gfx.esurface);
48 void gd_egl_draw(VirtualConsole *vc)
50 GdkWindow *window;
51 int ww, wh;
53 if (!vc->gfx.gls || !vc->gfx.ds) {
54 return;
57 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
58 vc->gfx.esurface, vc->gfx.ectx);
60 window = gtk_widget_get_window(vc->gfx.drawing_area);
61 gdk_drawable_get_size(window, &ww, &wh);
62 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
63 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
65 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
68 void gd_egl_update(DisplayChangeListener *dcl,
69 int x, int y, int w, int h)
71 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
73 if (!vc->gfx.gls || !vc->gfx.ds) {
74 return;
77 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
78 vc->gfx.esurface, vc->gfx.ectx);
79 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
80 vc->gfx.glupdates++;
83 void gd_egl_refresh(DisplayChangeListener *dcl)
85 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
87 if (!vc->gfx.esurface) {
88 gd_egl_init(vc);
89 if (!vc->gfx.esurface) {
90 return;
92 vc->gfx.gls = console_gl_init_context();
93 if (vc->gfx.ds) {
94 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
98 graphic_hw_update(dcl->con);
100 if (vc->gfx.glupdates) {
101 vc->gfx.glupdates = 0;
102 gd_egl_draw(vc);
106 void gd_egl_switch(DisplayChangeListener *dcl,
107 DisplaySurface *surface)
109 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
110 bool resized = true;
112 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
114 if (vc->gfx.ds &&
115 surface_width(vc->gfx.ds) == surface_width(surface) &&
116 surface_height(vc->gfx.ds) == surface_height(surface)) {
117 resized = false;
120 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
121 vc->gfx.ds = surface;
122 if (vc->gfx.gls) {
123 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
126 if (resized) {
127 gd_update_windowsize(vc);
131 void gtk_egl_init(void)
133 GdkDisplay *gdk_display = gdk_display_get_default();
134 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
136 if (qemu_egl_init_dpy(x11_display, false, false) < 0) {
137 return;
140 display_opengl = 1;