From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35666) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf2U5-00015d-2i for qemu-devel@nongnu.org; Thu, 24 Sep 2015 05:06:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zf2U3-0008Ty-Le for qemu-devel@nongnu.org; Thu, 24 Sep 2015 05:06:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42874) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf2U3-0008SR-Cd for qemu-devel@nongnu.org; Thu, 24 Sep 2015 05:06:31 -0400 From: Gerd Hoffmann Date: Thu, 24 Sep 2015 11:05:01 +0200 Message-Id: <1443085502-596-11-git-send-email-kraxel@redhat.com> In-Reply-To: <1443085502-596-1-git-send-email-kraxel@redhat.com> References: <1443085502-596-1-git-send-email-kraxel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 10/11] gtk/opengl: add opengl context and scanout support (egl) List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: David Airlie , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Gerd Hoffmann , Max Reitz This allows virtio-gpu to render in 3d mode. Uses egl, for gtk versions 3.14 and older. Signed-off-by: Gerd Hoffmann Reviewed-by: Marc-Andr=C3=A9 Lureau --- include/ui/gtk.h | 16 +++++++ ui/gtk-egl.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++= ++---- ui/gtk.c | 7 +++ 3 files changed, 146 insertions(+), 8 deletions(-) diff --git a/include/ui/gtk.h b/include/ui/gtk.h index ee6dffd..6d152e5 100644 --- a/include/ui/gtk.h +++ b/include/ui/gtk.h @@ -24,6 +24,7 @@ =20 #if defined(CONFIG_OPENGL) #include "ui/egl-helpers.h" +#include "ui/egl-context.h" #endif =20 /* Compatibility define to let us build on both Gtk2 and Gtk3 */ @@ -50,6 +51,11 @@ typedef struct VirtualGfxConsole { EGLContext ectx; EGLSurface esurface; int glupdates; + int x, y, w, h; + GLuint tex_id; + GLuint fbo_id; + bool y0_top; + bool scanout_mode; #endif } VirtualGfxConsole; =20 @@ -94,6 +100,16 @@ void gd_egl_update(DisplayChangeListener *dcl, void gd_egl_refresh(DisplayChangeListener *dcl); void gd_egl_switch(DisplayChangeListener *dcl, DisplaySurface *surface); +QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl, + QEMUGLParams *params); +void gd_egl_scanout(DisplayChangeListener *dcl, + uint32_t backing_id, bool backing_y_0_top, + uint32_t x, uint32_t y, + uint32_t w, uint32_t h); +void gd_egl_scanout_flush(DisplayChangeListener *dcl, + uint32_t x, uint32_t y, uint32_t w, uint32_t h= ); void gtk_egl_init(void); +int gd_egl_make_current(DisplayChangeListener *dcl, + QEMUGLContext ctx); =20 #endif /* UI_GTK_H */ diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c index 15b41f2..500c42c 100644 --- a/ui/gtk-egl.c +++ b/ui/gtk-egl.c @@ -21,6 +21,29 @@ =20 #include "sysemu/sysemu.h" =20 +static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) +{ + if (vc->gfx.scanout_mode =3D=3D scanout) { + return; + } + + vc->gfx.scanout_mode =3D scanout; + if (!vc->gfx.scanout_mode) { + if (vc->gfx.fbo_id) { + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, + GL_COLOR_ATTACHMENT0_EXT, + GL_TEXTURE_2D, 0, 0); + glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); + glDeleteFramebuffers(1, &vc->gfx.fbo_id); + vc->gfx.fbo_id =3D 0; + } + if (vc->gfx.surface) { + surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); + surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); + } + } +} + /** DisplayState Callbacks (opengl version) **/ =20 void gd_egl_init(VirtualConsole *vc) @@ -50,19 +73,26 @@ void gd_egl_draw(VirtualConsole *vc) GdkWindow *window; int ww, wh; =20 - if (!vc->gfx.gls || !vc->gfx.ds) { + if (!vc->gfx.gls) { return; } =20 - eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, - vc->gfx.esurface, vc->gfx.ectx); + if (vc->gfx.scanout_mode) { + gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h); + } else { + if (!vc->gfx.ds) { + return; + } + eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, + vc->gfx.esurface, vc->gfx.ectx); =20 - window =3D gtk_widget_get_window(vc->gfx.drawing_area); - gdk_drawable_get_size(window, &ww, &wh); - surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); - surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); + window =3D gtk_widget_get_window(vc->gfx.drawing_area); + gdk_drawable_get_size(window, &ww, &wh); + surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); + surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); =20 - eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); + eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); + } } =20 void gd_egl_update(DisplayChangeListener *dcl, @@ -99,6 +129,7 @@ void gd_egl_refresh(DisplayChangeListener *dcl) =20 if (vc->gfx.glupdates) { vc->gfx.glupdates =3D 0; + gtk_egl_set_scanout_mode(vc, false); gd_egl_draw(vc); } } @@ -128,6 +159,81 @@ void gd_egl_switch(DisplayChangeListener *dcl, } } =20 +QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl, + QEMUGLParams *params) +{ + VirtualConsole *vc =3D container_of(dcl, VirtualConsole, gfx.dcl); + + eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, + vc->gfx.esurface, vc->gfx.ectx); + return qemu_egl_create_context(dcl, params); +} + +void gd_egl_scanout(DisplayChangeListener *dcl, + uint32_t backing_id, bool backing_y_0_top, + uint32_t x, uint32_t y, + uint32_t w, uint32_t h) +{ + VirtualConsole *vc =3D container_of(dcl, VirtualConsole, gfx.dcl); + + vc->gfx.x =3D x; + vc->gfx.y =3D y; + vc->gfx.w =3D w; + vc->gfx.h =3D h; + vc->gfx.tex_id =3D backing_id; + vc->gfx.y0_top =3D backing_y_0_top; + + eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, + vc->gfx.esurface, vc->gfx.ectx); + + if (vc->gfx.tex_id =3D=3D 0 || vc->gfx.w =3D=3D 0 || vc->gfx.h =3D=3D= 0) { + gtk_egl_set_scanout_mode(vc, false); + return; + } + + gtk_egl_set_scanout_mode(vc, true); + if (!vc->gfx.fbo_id) { + glGenFramebuffers(1, &vc->gfx.fbo_id); + } + + glBindFramebuffer(GL_FRAMEBUFFER_EXT, vc->gfx.fbo_id); + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_E= XT, + GL_TEXTURE_2D, vc->gfx.tex_id, 0); +} + +void gd_egl_scanout_flush(DisplayChangeListener *dcl, + uint32_t x, uint32_t y, uint32_t w, uint32_t h= ) +{ + VirtualConsole *vc =3D container_of(dcl, VirtualConsole, gfx.dcl); + GdkWindow *window; + int ww, wh, y1, y2; + + if (!vc->gfx.scanout_mode) { + return; + } + if (!vc->gfx.fbo_id) { + return; + } + + eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, + vc->gfx.esurface, vc->gfx.ectx); + + glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.fbo_id); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + + window =3D gtk_widget_get_window(vc->gfx.drawing_area); + gdk_drawable_get_size(window, &ww, &wh); + glViewport(0, 0, ww, wh); + y1 =3D vc->gfx.y0_top ? 0 : vc->gfx.h; + y2 =3D vc->gfx.y0_top ? vc->gfx.h : 0; + glBlitFramebuffer(0, y1, vc->gfx.w, y2, + 0, 0, ww, wh, + GL_COLOR_BUFFER_BIT, GL_NEAREST); + glBindFramebuffer(GL_FRAMEBUFFER_EXT, vc->gfx.fbo_id); + + eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); +} + void gtk_egl_init(void) { GdkDisplay *gdk_display =3D gdk_display_get_default(); @@ -139,3 +245,12 @@ void gtk_egl_init(void) =20 display_opengl =3D 1; } + +int gd_egl_make_current(DisplayChangeListener *dcl, + QEMUGLContext ctx) +{ + VirtualConsole *vc =3D container_of(dcl, VirtualConsole, gfx.dcl); + + return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, + vc->gfx.esurface, ctx); +} diff --git a/ui/gtk.c b/ui/gtk.c index 187de74..ce68def 100644 --- a/ui/gtk.c +++ b/ui/gtk.c @@ -606,6 +606,13 @@ static const DisplayChangeListenerOps dcl_egl_ops =3D= { .dpy_refresh =3D gd_egl_refresh, .dpy_mouse_set =3D gd_mouse_set, .dpy_cursor_define =3D gd_cursor_define, + + .dpy_gl_ctx_create =3D gd_egl_create_context, + .dpy_gl_ctx_destroy =3D qemu_egl_destroy_context, + .dpy_gl_ctx_make_current =3D gd_egl_make_current, + .dpy_gl_ctx_get_current =3D qemu_egl_get_current_context, + .dpy_gl_scanout =3D gd_egl_scanout, + .dpy_gl_update =3D gd_egl_scanout_flush, }; =20 #endif --=20 1.8.3.1