[Glamor] [PATCH] GLX: Enable glx support.

zhigang.gong at linux.intel.com zhigang.gong at linux.intel.com
Fri Feb 10 00:48:57 PST 2012


From: Zhigang Gong <zhigang.gong at linux.intel.com>

If we are using MESA as our GL library, then both xserver's
GLX and glamor are link to the same library. As xserver's
GLX has its own _glapi_get/set_context/dispatch etc, and it
is a simplified version derived from mesa thus is not
sufficient for mesa/egl's dri loader which is used by glamor.

Then if glx module is loaded before glamoregl module, the
initialization of mesa/egl/opengl will not be correct, and
will fail at a very early stage, most likely fail to map
the element buffer.

Two methodis to fix this problem, first is to modify the xserver's
glx's glapi.c to fit mesa's requirement. The second is to put
a glamor.conf as below, to the system's xorg.conf path.

Section "Module"
        Load  "glamoregl"
EndSection

Then glamor will be loaded firstly, and the mesa's libglapi.so
will be used. As current xserver's dispatch table is the same
as mesa's, then the glx's dri loader can work without problem.

We took the second method as it don't need any change to xorg.:)
Although this is not a graceful implementation as it depends
on the xserver's dispatch table and the mesa's dispatch table
is the same and the context set and get is using the same method.
Anyway it works.

As by default, xserver will enable GLX_USE_TLS. But mesa will not
enable it, you may need to enable that when build mesa.

Three pre-requirements to make this glamor version work:

0. Make sure xserver has commit 66e603, if not please pull the latest
   master branch.
1. Rebuild mesa by enable GLX_USE_TLS.
2. Put the glamor.conf to your system's xorg.conf path and make sure
   it loaded prior to glx module.

Preliminary testing shows indirect glxgears works fine.

If user want to use GLES2 for glamor by using MESA, GLX will not
work correctly.

If you are not using normal MESA, for example PVR's private GLES
implementation, then it should be ok to use GLES2 glamor and the
GLX should work as expected. In this commit, I use gbm to check
whether we are using MESA or non-mesa. Maybe not the best way.

Signed-off-by: Zhigang Gong <zhigang.gong at linux.intel.com>
---
 configure.ac              |   54 +++++++++++++++++++++
 src/Makefile.am           |    4 +-
 src/glamor.c              |    8 ++--
 src/glamor.h              |    4 ++
 src/glamor_copyarea.c     |   15 ++++--
 src/glamor_copyplane.c    |    7 +++
 src/glamor_egl.c          |   59 ++++++++++++++++++++--
 src/glamor_fbo.c          |   14 ++++-
 src/glamor_fillspans.c    |   22 +++++++--
 src/glamor_getimage.c     |   14 +++++-
 src/glamor_getspans.c     |   15 ++++--
 src/glamor_glyphblt.c     |   21 ++++++++
 src/glamor_polyfillrect.c |   21 ++++++--
 src/glamor_polylines.c    |    6 ++
 src/glamor_polyops.c      |   21 ++++++++
 src/glamor_priv.h         |    1 +
 src/glamor_putimage.c     |   20 ++++++--
 src/glamor_render.c       |   22 ++++++---
 src/glamor_setspans.c     |   18 +++++--
 src/glamor_utils.h        |   29 +++++++++++
 src/glapi.h               |  116 +++++++++++++++++++++++++++++++++++++++++++++
 21 files changed, 440 insertions(+), 51 deletions(-)
 create mode 100644 src/glapi.h

diff --git a/configure.ac b/configure.ac
index 1d19534..59a1051 100644
--- a/configure.ac
+++ b/configure.ac
@@ -106,9 +106,63 @@ if test "x$EGL = xyes"; then
    PKG_CHECK_MODULES(GBM, $LIBGBM, [GBM=yes], [GBM=no])
    if test "x$GBM" = xyes; then
      AC_DEFINE(GLAMOR_HAS_GBM, 1, [Use GBM.])
+     AC_DEFINE(GLX_USE_SHARED_DISPATCH, 1, [GLX and GLAMOR share the glapi dispatch table.])
    fi
 fi
 
+dnl
+dnl TLS detection
+dnl
+AC_MSG_CHECKING(for thread local storage (TLS) support)
+AC_CACHE_VAL(ac_cv_tls, [
+    ac_cv_tls=none
+    keywords="__thread __declspec(thread)"
+    for kw in $keywords ; do
+        AC_TRY_COMPILE([int $kw test;], [], ac_cv_tls=$kw)
+    done
+])
+AC_MSG_RESULT($ac_cv_tls)
+
+if test "$ac_cv_tls" != "none"; then
+    AC_MSG_CHECKING(for tls_model attribute support)
+    AC_CACHE_VAL(ac_cv_tls_model, [
+        save_CFLAGS="$CFLAGS"
+        CFLAGS="$CFLAGS $STRICT_CFLAGS"
+        AC_TRY_COMPILE([int $ac_cv_tls __attribute__((tls_model("initial-exec"))) test;], [],
+                       ac_cv_tls_model=yes, ac_cv_tls_model=no)
+        CFLAGS="$save_CFLAGS"
+    ])
+    AC_MSG_RESULT($ac_cv_tls_model)
+
+    if test "x$ac_cv_tls_model" = "xyes" ; then
+        mesa_tls=$ac_cv_tls' __attribute__((tls_model("initial-exec")))'
+    else
+        mesa_tls=$ac_cv_tls
+    fi
+
+    AC_DEFINE_UNQUOTED([TLS], $mesa_tls, [The compiler supported TLS storage class, prefering initial-exec if tls_model is supported])
+fi
+
+AC_ARG_ENABLE([glx-tls],
+    [AS_HELP_STRING([--enable-glx-tls],
+        [enable TLS support in GLX @<:@default=disabled@:>@])],
+    [GLX_USE_TLS=$enableval
+     if test "x$GLX_USE_TLS" = "xyes" && test "${ac_cv_tls}" = "none" ; then
+        AC_MSG_ERROR([GLX with TLS support requested, but the compiler does not support it.])
+     fi],
+    [GLX_USE_TLS=no
+     if test "${ac_cv_tls}" != "none" ; then
+        GLX_USE_TLS=yes
+     fi])
+AC_SUBST(GLX_TLS, ${GLX_USE_TLS})
+
+if test "x$GLX_USE_TLS" = xyes ; then
+        GLX_DEFINES="-DGLX_USE_TLS -DPTHREADS"
+        GLX_SYS_LIBS="$GLX_SYS_LIBS -lpthread"
+fi
+AC_SUBST([GLX_DEFINES])
+AC_SUBST([GLX_SYS_LIBS])
+
 AC_OUTPUT([Makefile
            glamor-egl.pc
            glamor.pc
diff --git a/src/Makefile.am b/src/Makefile.am
index ca8fc3f..c7e3000 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -53,9 +53,9 @@ if EGL
 LIBGLAMOREGL = libglamoregl.la
 module_LTLIBRARIES = $(LIBGLAMOREGL)
 libglamoregl_la_DEPENDENCIES = libglamor.la
-libglamoregl_la_LDFLAGS = -avoid-version -module $(EGL_LIBS) -lglamor
+libglamoregl_la_LDFLAGS = -avoid-version -module $(EGL_LIBS) -lglamor $(GLX_SYS_LIBS)
 libglamoregl_la_SOURCES = glamor_eglmodule.c $(top_srcdir)/src/glamor_egl.c
-libglamoregl_la_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/src $(LIBDRM_CFLAGS) $(EGL_CFLAGS)
+libglamoregl_la_CFLAGS = $(AM_CFLAGS) $(GLX_DEFINES) -I$(top_srcdir)/src $(LIBDRM_CFLAGS) $(EGL_CFLAGS)
 endif
 
 
diff --git a/src/glamor.c b/src/glamor.c
index 0245fda..6b6330f 100644
--- a/src/glamor.c
+++ b/src/glamor.c
@@ -83,11 +83,9 @@ glamor_set_pixmap_texture(PixmapPtr pixmap, unsigned int tex)
 	ScreenPtr screen = pixmap->drawable.pScreen;
 	glamor_pixmap_private *pixmap_priv;
 	glamor_screen_private *glamor_priv;
-	glamor_gl_dispatch *dispatch;
 	glamor_pixmap_fbo *fbo;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch  = &glamor_priv->dispatch;
 	pixmap_priv = glamor_get_pixmap_private(pixmap);
 
 	if (pixmap_priv->fbo) {
@@ -130,7 +128,6 @@ glamor_create_pixmap(ScreenPtr screen, int w, int h, int depth,
 	glamor_pixmap_private *pixmap_priv;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
 	glamor_pixmap_fbo *fbo;
 	int pitch;
 	int flag;
@@ -149,7 +146,6 @@ glamor_create_pixmap(ScreenPtr screen, int w, int h, int depth,
 		fbDestroyPixmap(pixmap);
 		return fbCreatePixmap(screen, w, h, depth, usage);
 	}
-
 	glamor_set_pixmap_private(pixmap, pixmap_priv);
 
 	pixmap_priv->container = pixmap;
@@ -201,11 +197,14 @@ glamor_block_handler(ScreenPtr screen)
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
 	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	GLAMOR_DEFINE_CONTEXT;
 
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	glamor_priv->tick++;
 	dispatch->glFlush();
 	dispatch->glFinish();
 	glamor_fbo_expire(glamor_priv);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 }
 
 static void
@@ -392,6 +391,7 @@ glamor_init(ScreenPtr screen, unsigned int flags)
 	glamor_pixmap_init(screen);
 
 	glamor_priv->flags = flags;
+	glamor_priv->screen = screen;
 
 	return TRUE;
 
diff --git a/src/glamor.h b/src/glamor.h
index 3d6e5fe..712a7a9 100644
--- a/src/glamor.h
+++ b/src/glamor.h
@@ -145,6 +145,10 @@ extern _X_EXPORT PixmapPtr glamor_create_pixmap(ScreenPtr screen, int w, int h,
 
 extern _X_EXPORT void glamor_egl_screen_init(ScreenPtr screen);
 
+extern _X_EXPORT void * glamor_egl_make_current(ScreenPtr screen);
+
+extern _X_EXPORT void glamor_egl_restore_context(ScreenPtr screen, void *context);
+
 #ifdef GLAMOR_FOR_XORG
 
 #define GLAMOR_EGL_MODULE_NAME  "glamoregl"
diff --git a/src/glamor_copyarea.c b/src/glamor_copyarea.c
index f2d710a..e656934 100644
--- a/src/glamor_copyarea.c
+++ b/src/glamor_copyarea.c
@@ -308,6 +308,7 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	PixmapPtr dst_pixmap, src_pixmap, temp_pixmap = NULL;
 	DrawablePtr temp_src = src;
 	glamor_pixmap_private *dst_pixmap_priv, *src_pixmap_priv;
+	glamor_screen_private *glamor_priv;
 	BoxRec bound;
 	ScreenPtr screen;
 	int temp_dx = dx;
@@ -315,7 +316,8 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	int src_x_off, src_y_off, dst_x_off, dst_y_off;
 	int i;
 	int overlaped = 0;
-	Bool ret = TRUE;
+	Bool ret = FALSE;
+	GLAMOR_DEFINE_CONTEXT;
 
 	dst_pixmap = glamor_get_drawable_pixmap(dst);
 	dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);
@@ -323,6 +325,9 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
 	screen = dst_pixmap->drawable.pScreen;
 
+	glamor_priv = glamor_get_screen_private(dst->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
+
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dst_pixmap_priv)) {
 		glamor_fallback("dest pixmap %p has no fbo. \n",
 				dst_pixmap);
@@ -356,6 +361,7 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	     || !src_pixmap_priv->gl_tex || !dst_pixmap_priv->gl_tex)
 	    && glamor_copy_n_to_n_fbo_blit(src, dst, gc, box, nbox, dx,
 					   dy)) {
+		ret = TRUE;
 		goto done;
 	}
 #endif
@@ -400,6 +406,7 @@ _glamor_copy_n_to_n(DrawablePtr src,
 
 	if (glamor_copy_n_to_n_textured
 	    (temp_src, dst, gc, box, nbox, temp_dx, temp_dy)) {
+		ret = TRUE;
 		goto done;
 	}
 
@@ -408,10 +415,8 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	
 	if (!fallback 
 	    && glamor_ddx_fallback_check_pixmap(src)
-	    && glamor_ddx_fallback_check_pixmap(dst)) {
-		ret = FALSE;
+	    && glamor_ddx_fallback_check_pixmap(dst))
 		goto done;
-	} 
 
 	glamor_report_delayed_fallbacks(src->pScreen);
 	glamor_report_delayed_fallbacks(dst->pScreen);
@@ -436,12 +441,14 @@ _glamor_copy_n_to_n(DrawablePtr src,
 		}
 		glamor_finish_access(dst, GLAMOR_ACCESS_RW);
 	}
+	ret = TRUE;
 
       done:
 	glamor_clear_delayed_fallbacks(src->pScreen);
 	glamor_clear_delayed_fallbacks(dst->pScreen);
 	if (temp_src != src)
 		glamor_destroy_pixmap(temp_pixmap);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_copyplane.c b/src/glamor_copyplane.c
index 288d7ed..b6b26a5 100644
--- a/src/glamor_copyplane.c
+++ b/src/glamor_copyplane.c
@@ -37,17 +37,24 @@ _glamor_copy_plane(DrawablePtr pSrc, DrawablePtr pDst, GCPtr pGC,
 		   int srcx, int srcy, int w, int h, int dstx, int dsty,
 		   unsigned long bitPlane, RegionPtr *pRegion, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_gc(pGC)
 	    && glamor_ddx_fallback_check_pixmap(pSrc)
 	    && glamor_ddx_fallback_check_pixmap(pDst))
 		goto fail;
+
+	glamor_priv = glamor_get_screen_private(pDst->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	glamor_prepare_access(pDst, GLAMOR_ACCESS_RW);
 	glamor_prepare_access(pSrc, GLAMOR_ACCESS_RO);
 	*pRegion = fbCopyPlane(pSrc, pDst, pGC, srcx, srcy, w, h,
 			  dstx, dsty, bitPlane);
 	glamor_finish_access(pSrc, GLAMOR_ACCESS_RO);
 	glamor_finish_access(pDst, GLAMOR_ACCESS_RW);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
 
  fail:
diff --git a/src/glamor_egl.c b/src/glamor_egl.c
index 027722e..a339527 100644
--- a/src/glamor_egl.c
+++ b/src/glamor_egl.c
@@ -63,6 +63,9 @@
 
 #include "glamor.h"
 #include "glamor_gl_dispatch.h"
+#ifdef GLX_USE_SHARED_DISPATCH
+#include "glapi.h"
+#endif
 
 static const char glamor_name[] = "glamor";
 
@@ -90,6 +93,7 @@ struct glamor_egl_screen_private {
 	struct gbm_device *gbm;
 #endif
 	int has_gem;
+	void *gl_context;
 
 	PFNEGLCREATEIMAGEKHRPROC egl_create_image_khr;
 	PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image_khr;
@@ -101,13 +105,46 @@ struct glamor_egl_screen_private {
 
 int xf86GlamorEGLPrivateIndex = -1;
 
-static struct glamor_egl_screen_private
-*
+static struct glamor_egl_screen_private *
 glamor_egl_get_screen_private(ScrnInfoPtr scrn)
 {
 	return (struct glamor_egl_screen_private *)
 	    scrn->privates[xf86GlamorEGLPrivateIndex].ptr;
 }
+#ifdef GLX_USE_SHARED_DISPATCH
+_X_EXPORT void *
+glamor_egl_make_current(ScreenPtr screen)
+{
+	ScrnInfoPtr scrn = xf86Screens[screen->myNum];
+	struct glamor_egl_screen_private *glamor_egl =
+	    glamor_egl_get_screen_private(scrn);
+	GET_CURRENT_CONTEXT(current);
+
+	if (glamor_egl->gl_context != current) {
+		eglMakeCurrent(glamor_egl->display, EGL_NO_SURFACE,
+			       EGL_NO_SURFACE, EGL_NO_CONTEXT);
+		if (!eglMakeCurrent(glamor_egl->display,
+				    EGL_NO_SURFACE, EGL_NO_SURFACE,
+				    glamor_egl->context)) {
+			xf86DrvMsg(scrn->scrnIndex, X_ERROR,
+				   "Failed to make EGL context current\n");
+			return NULL;
+		}
+		return current;
+	}
+	return NULL;
+}
+
+_X_EXPORT void
+glamor_egl_restore_context(ScreenPtr screen, void *context)
+{
+	if (context)
+		SET_CURRENT_CONTEXT(context);
+}
+#else
+#define glamor_egl_make_current(x)  NULL
+#define glamor_egl_restore_context(s, c)
+#endif
 
 static EGLImageKHR
 _glamor_egl_create_image(struct glamor_egl_screen_private *glamor_egl,
@@ -215,9 +252,12 @@ glamor_egl_create_textured_pixmap(PixmapPtr pixmap, int handle, int stride)
 	EGLImageKHR image;
 	GLuint texture;
 	int name;
+	void *prev_context;
+	Bool ret = FALSE;
 
 	glamor_egl = glamor_egl_get_screen_private(scrn);
 
+	prev_context = glamor_egl_make_current(screen);
 	if (glamor_egl->has_gem) {
 		if (!glamor_get_flink_name(glamor_egl->fd, handle, &name)) {
 			xf86DrvMsg(scrn->scrnIndex, X_ERROR,
@@ -234,16 +274,20 @@ glamor_egl_create_textured_pixmap(PixmapPtr pixmap, int handle, int stride)
 					 ((stride * 8 + 7) / pixmap->drawable.bitsPerPixel),
 					 name,
 					 pixmap->drawable.depth);
-	if (image == EGL_NO_IMAGE_KHR) 
-		return FALSE;
+	if (image == EGL_NO_IMAGE_KHR)
+		goto done;
 
 	glamor_create_texture_from_image(glamor_egl, image, &texture);
 	glamor_set_pixmap_type(pixmap, GLAMOR_TEXTURE_DRM);
 	glamor_set_pixmap_texture(pixmap, texture);
 	dixSetPrivate(&pixmap->devPrivates, glamor_egl_pixmap_private_key,
 		      image);
+	ret = TRUE;
 
-	return TRUE;
+done:
+	if (prev_context)
+		glamor_egl_restore_context(screen, prev_context);
+	return ret;
 }
 
 static void
@@ -447,7 +491,10 @@ glamor_egl_init(ScrnInfoPtr scrn, int fd)
 			   "Failed to make EGL context current\n");
 		return FALSE;
 	}
-
+#ifdef GLX_USE_SHARED_DISPATCH
+	GET_CURRENT_CONTEXT(current);
+	glamor_egl->gl_context = current;
+#endif
 	glamor_egl->saved_free_screen = scrn->FreeScreen;
 	scrn->FreeScreen = glamor_egl_free_screen;
 	return TRUE;
diff --git a/src/glamor_fbo.c b/src/glamor_fbo.c
index 2243564..929caba 100644
--- a/src/glamor_fbo.c
+++ b/src/glamor_fbo.c
@@ -126,6 +126,9 @@ glamor_pixmap_fbo_cache_get(glamor_screen_private *glamor_priv,
 void
 glamor_purge_fbo(glamor_pixmap_fbo *fbo)
 {
+	GLAMOR_DEFINE_CONTEXT;
+
+	GLAMOR_SET_CONTEXT(fbo->glamor_priv);
 	glamor_gl_dispatch *dispatch = &fbo->glamor_priv->dispatch;
 	if (fbo->fb)
 		dispatch->glDeleteFramebuffers(1, &fbo->fb);
@@ -133,6 +136,7 @@ glamor_purge_fbo(glamor_pixmap_fbo *fbo)
 		dispatch->glDeleteTextures(1, &fbo->tex);
 	if (fbo->pbo)
 		dispatch->glDeleteBuffers(1, &fbo->pbo);
+	GLAMOR_RESTORE_CONTEXT(fbo->glamor_priv);
 
 	free(fbo);
 }
@@ -166,7 +170,6 @@ glamor_pixmap_fbo *
 glamor_create_fbo_from_tex(glamor_screen_private *glamor_priv,
 		  int w, int h, int depth, GLint tex, int flag)
 {
-	glamor_gl_dispatch *dispatch;
 	glamor_pixmap_fbo *fbo;
 	GLenum format;
 
@@ -276,8 +279,6 @@ glamor_fini_pixmap_fbo(ScreenPtr screen)
 void
 glamor_destroy_fbo(glamor_pixmap_fbo *fbo)
 {
-	glamor_gl_dispatch *dispatch = &fbo->glamor_priv->dispatch;
-
 	list_del(&fbo->list);
 	glamor_pixmap_fbo_cache_put(fbo);
 
@@ -291,6 +292,7 @@ glamor_create_tex_obj(glamor_screen_private *glamor_priv,
 	glamor_pixmap_fbo *fbo;
 	int cache_flag = GLAMOR_CACHE_TEXTURE;
 	GLuint tex;
+	GLAMOR_DEFINE_CONTEXT;
 
 	if (flag == GLAMOR_CREATE_TEXTURE_EXACT_SIZE)
 		cache_flag |= GLAMOR_CACHE_EXACT_SIZE;
@@ -303,6 +305,7 @@ glamor_create_tex_obj(glamor_screen_private *glamor_priv,
 	if (fbo == NULL)
 		return NULL;
 
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	list_init(&fbo->list);
 
 	dispatch = &glamor_priv->dispatch;
@@ -319,6 +322,7 @@ glamor_create_tex_obj(glamor_screen_private *glamor_priv,
 	fbo->height = h;
 	fbo->format = format;
 	fbo->glamor_priv = glamor_priv;
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 
 	return fbo;
 }
@@ -340,6 +344,7 @@ glamor_create_fbo(glamor_screen_private *glamor_priv,
 	GLenum format;
 	GLint tex;
 	int cache_flag;
+	GLAMOR_DEFINE_CONTEXT;
 
 	if (!glamor_check_fbo_size(glamor_priv, w, h)
 	    || !glamor_check_fbo_depth(depth))
@@ -359,6 +364,8 @@ glamor_create_fbo(glamor_screen_private *glamor_priv,
 	if (fbo)
 		return fbo;
 new_fbo:
+
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	dispatch = &glamor_priv->dispatch;
 	dispatch->glGenTextures(1, &tex);
 	dispatch->glBindTexture(GL_TEXTURE_2D, tex);
@@ -370,6 +377,7 @@ new_fbo:
 			       GL_UNSIGNED_BYTE, NULL);
 
 	fbo = glamor_create_fbo_from_tex(glamor_priv, w, h, depth, tex, flag);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 
 	return fbo;
 }
diff --git a/src/glamor_fillspans.c b/src/glamor_fillspans.c
index 97c4403..97e4497 100644
--- a/src/glamor_fillspans.c
+++ b/src/glamor_fillspans.c
@@ -40,6 +40,12 @@ _glamor_fill_spans(DrawablePtr drawable,
 	BoxPtr pbox;
 	int x1, x2, y;
 	RegionPtr pClip = fbGetCompositeClip(gc);
+	glamor_screen_private *glamor_priv;
+	GLAMOR_DEFINE_CONTEXT;
+	Bool ret = FALSE;
+
+	glamor_priv = glamor_get_screen_private(drawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 
 	if (gc->fillStyle != FillSolid && gc->fillStyle != FillTiled)
 		goto fail;
@@ -71,13 +77,15 @@ _glamor_fill_spans(DrawablePtr drawable,
 			pbox++;
 		}
 	}
-	return TRUE;
+	ret = TRUE;
+	goto done;
 
-      fail:
+fail:
 	if (!fallback 
 	    && glamor_ddx_fallback_check_pixmap(drawable)
-	    && glamor_ddx_fallback_check_gc(gc))
-		return FALSE;
+	    && glamor_ddx_fallback_check_gc(gc)) {
+		goto done;
+	}
 	glamor_fallback("to %p (%c)\n", drawable,
 			glamor_get_drawable_location(drawable));
 	if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
@@ -88,7 +96,11 @@ _glamor_fill_spans(DrawablePtr drawable,
 		}
 		glamor_finish_access(drawable, GLAMOR_ACCESS_RW);
 	}
-	return TRUE;
+	ret = TRUE;
+
+done:
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	return ret;
 }
 
 
diff --git a/src/glamor_getimage.c b/src/glamor_getimage.c
index 377783c..7caf2a3 100644
--- a/src/glamor_getimage.c
+++ b/src/glamor_getimage.c
@@ -46,8 +46,14 @@ _glamor_get_image(DrawablePtr drawable, int x, int y, int w, int h,
 	int no_alpha, no_revert;
 	PixmapPtr temp_pixmap = NULL;
 	glamor_gl_dispatch * dispatch;
+	GLAMOR_DEFINE_CONTEXT;
+	Bool ret = FALSE;
 
 	goto fall_back;
+
+	glamor_priv = glamor_get_screen_private(drawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
+
 	if (format != ZPixmap)
 		goto fall_back;
 
@@ -113,10 +119,14 @@ _glamor_get_image(DrawablePtr drawable, int x, int y, int w, int h,
 				       tex_type, d);
 	if (temp_pixmap)
 		glamor_destroy_pixmap(temp_pixmap);
-	return TRUE;
+
+	ret = TRUE;
 
 fall_back:
-	miGetImage(drawable, x, y, w, h, format, planeMask, d);
+
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	if (ret == FALSE)
+		miGetImage(drawable, x, y, w, h, format, planeMask, d);
 	return TRUE;
 }
 
diff --git a/src/glamor_getspans.c b/src/glamor_getspans.c
index 6c50d1b..2d30f37 100644
--- a/src/glamor_getspans.c
+++ b/src/glamor_getspans.c
@@ -49,7 +49,10 @@ _glamor_get_spans(DrawablePtr drawable,
 	int i;
 	uint8_t *readpixels_dst = (uint8_t *) dst;
 	int x_off, y_off;
+	GLAMOR_DEFINE_CONTEXT;
+	Bool ret = FALSE;
 
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) {
 		glamor_fallback("pixmap has no fbo.\n");
 		goto fail;
@@ -97,20 +100,24 @@ _glamor_get_spans(DrawablePtr drawable,
 	}
 	if (temp_pixmap)
 		glamor_destroy_pixmap(temp_pixmap);
-	return TRUE;
 
-      fail:
+	ret = TRUE;
+	goto done;
+fail:
 
 	if (!fallback
 	    && glamor_ddx_fallback_check_pixmap(drawable))
-		return FALSE; 
+		goto done;
+
 	glamor_fallback("from %p (%c)\n", drawable,
 			glamor_get_drawable_location(drawable));
 	if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RO)) {
 		fbGetSpans(drawable, wmax, points, widths, count, dst);
 		glamor_finish_access(drawable, GLAMOR_ACCESS_RO);
 	}
-	return TRUE;
+done:
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	return ret;
 }
 
 void
diff --git a/src/glamor_glyphblt.c b/src/glamor_glyphblt.c
index c9a35a0..8c853e9 100644
--- a/src/glamor_glyphblt.c
+++ b/src/glamor_glyphblt.c
@@ -37,15 +37,22 @@ _glamor_image_glyph_blt(DrawablePtr pDrawable, GCPtr pGC,
                     int x, int y, unsigned int nglyph,
                     CharInfoPtr * ppci, pointer pglyphBase, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_pixmap(pDrawable)
 	    && glamor_ddx_fallback_check_gc(pGC))
 		goto fail;
+
+	glamor_priv = glamor_get_screen_private(pDrawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	glamor_prepare_access(pDrawable, GLAMOR_ACCESS_RW);
 	glamor_prepare_access_gc(pGC);
 	fbImageGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase);
 	glamor_finish_access_gc(pGC);
 	glamor_finish_access(pDrawable, GLAMOR_ACCESS_RW);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
  fail:
 	return FALSE;
@@ -72,15 +79,22 @@ _glamor_poly_glyph_blt(DrawablePtr pDrawable, GCPtr pGC,
                     int x, int y, unsigned int nglyph,
                     CharInfoPtr * ppci, pointer pglyphBase, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_pixmap(pDrawable)
 	    && glamor_ddx_fallback_check_gc(pGC))
 		goto fail;
+
+	glamor_priv = glamor_get_screen_private(pDrawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	glamor_prepare_access(pDrawable, GLAMOR_ACCESS_RW);
 	glamor_prepare_access_gc(pGC);
 	fbPolyGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase);
 	glamor_finish_access_gc(pGC);
 	glamor_finish_access(pDrawable, GLAMOR_ACCESS_RW);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
  fail:
 	return FALSE;
@@ -106,11 +120,17 @@ static Bool
 _glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
 		    DrawablePtr pDrawable, int w, int h, int x, int y, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_pixmap(pDrawable)
 	    && glamor_ddx_fallback_check_pixmap(&pBitmap->drawable)
 	    && glamor_ddx_fallback_check_gc(pGC))
 		goto fail;
+
+	glamor_priv = glamor_get_screen_private(pDrawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	glamor_prepare_access(pDrawable, GLAMOR_ACCESS_RW);
 	glamor_prepare_access(&pBitmap->drawable, GLAMOR_ACCESS_RO);
 	glamor_prepare_access_gc(pGC);
@@ -118,6 +138,7 @@ _glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
 	glamor_finish_access_gc(pGC);
 	glamor_finish_access(&pBitmap->drawable, GLAMOR_ACCESS_RO);
 	glamor_finish_access(pDrawable, GLAMOR_ACCESS_RW);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
  fail:
 	return FALSE;
diff --git a/src/glamor_polyfillrect.c b/src/glamor_polyfillrect.c
index 32afd09..8b36241 100644
--- a/src/glamor_polyfillrect.c
+++ b/src/glamor_polyfillrect.c
@@ -45,8 +45,14 @@ _glamor_poly_fill_rect(DrawablePtr drawable,
 	int xorg, yorg;
 	int n;
 	register BoxPtr pbox;
-
 	RegionPtr pClip = fbGetCompositeClip(gc);
+	Bool ret = FALSE;
+	glamor_screen_private *glamor_priv;
+	GLAMOR_DEFINE_CONTEXT;
+
+	glamor_priv = glamor_get_screen_private(drawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
+
 	if (gc->fillStyle != FillSolid && gc->fillStyle != FillTiled) {
 		goto fail;
 	}
@@ -91,14 +97,15 @@ _glamor_poly_fill_rect(DrawablePtr drawable,
 				goto fail;
 		}
 	}
-	return TRUE;
+	ret = TRUE;
+	goto done;
 
-      fail:
+fail:
 
 	if (!fallback
 	    && glamor_ddx_fallback_check_pixmap(drawable)
 	    && glamor_ddx_fallback_check_gc(gc))
-		return FALSE;
+		goto done;
 
 	glamor_fallback(" to %p (%c)\n",
 			drawable, glamor_get_drawable_location(drawable));
@@ -109,7 +116,11 @@ _glamor_poly_fill_rect(DrawablePtr drawable,
 		}
 		glamor_finish_access(drawable, GLAMOR_ACCESS_RW);
 	}
-	return TRUE;
+	ret = TRUE;
+
+done:
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	return ret;
 }
 
 
diff --git a/src/glamor_polylines.c b/src/glamor_polylines.c
index 0217e8f..0d37a1d 100644
--- a/src/glamor_polylines.c
+++ b/src/glamor_polylines.c
@@ -49,6 +49,9 @@ _glamor_poly_lines(DrawablePtr drawable, GCPtr gc, int mode, int n,
 	xRectangle *rects;
 	int x1, x2, y1, y2;
 	int i;
+	glamor_screen_private *glamor_priv;
+	GLAMOR_DEFINE_CONTEXT;
+
 	/* Don't try to do wide lines or non-solid fill style. */
 	if (gc->lineWidth != 0) {
 		/* This ends up in miSetSpans, which is accelerated as well as we
@@ -108,6 +111,8 @@ _glamor_poly_lines(DrawablePtr drawable, GCPtr gc, int mode, int n,
 	    && glamor_ddx_fallback_check_gc(gc))
 		return FALSE;
 
+	glamor_priv = glamor_get_screen_private(drawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	if (gc->lineWidth == 0) {
 		if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
 			if (glamor_prepare_access_gc(gc)) {
@@ -121,6 +126,7 @@ wide_line:
 		/* fb calls mi functions in the lineWidth != 0 case. */
 		fbPolyLine(drawable, gc, mode, n, points);
 	}
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
 }
 
diff --git a/src/glamor_polyops.c b/src/glamor_polyops.c
index 6188bb2..ca21b8b 100644
--- a/src/glamor_polyops.c
+++ b/src/glamor_polyops.c
@@ -36,15 +36,22 @@ static Bool
 _glamor_poly_point(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
 		   DDXPointPtr ppt, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_gc(pGC)
 	    && glamor_ddx_fallback_check_pixmap(pDrawable))
 		goto fail;
+
+	glamor_priv = glamor_get_screen_private(pDrawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	glamor_prepare_access_gc(pGC);
 	glamor_prepare_access(pDrawable, GLAMOR_ACCESS_RW);
 	fbPolyPoint(pDrawable, pGC, mode, npt, ppt);
 	glamor_finish_access(pDrawable, GLAMOR_ACCESS_RW);
 	glamor_finish_access_gc(pGC);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
 
 fail:
@@ -69,10 +76,16 @@ static Bool
 _glamor_poly_segment(DrawablePtr pDrawable, GCPtr pGC, int nseg,
 		     xSegment *pSeg, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_gc(pGC)
 	    && glamor_ddx_fallback_check_pixmap(pDrawable))
 		goto fail;
+
+	glamor_priv = glamor_get_screen_private(pDrawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	/* For lineWidth is not zero, fb calls to mi functions. */
 	if (pGC->lineWidth == 0) {
 		glamor_prepare_access_gc(pGC);
@@ -83,6 +96,7 @@ _glamor_poly_segment(DrawablePtr pDrawable, GCPtr pGC, int nseg,
 	} else
 	fbPolySegment(pDrawable, pGC, nseg, pSeg);
 
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
 
 fail:
@@ -107,11 +121,17 @@ static Bool
 _glamor_poly_line(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
 		  DDXPointPtr ppt, Bool fallback)
 {
+	GLAMOR_DEFINE_CONTEXT;
+	glamor_screen_private *glamor_priv;
+
 	if (!fallback 
 	    && glamor_ddx_fallback_check_gc(pGC)
 	    && glamor_ddx_fallback_check_pixmap(pDrawable))
 		goto fail;
 	/* For lineWidth is not zero, fb calls to mi functions. */
+
+	glamor_priv = glamor_get_screen_private(pDrawable->pScreen);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	if (pGC->lineWidth == 0) {
 		glamor_prepare_access_gc(pGC);
 		glamor_prepare_access(pDrawable, GLAMOR_ACCESS_RW);
@@ -121,6 +141,7 @@ _glamor_poly_line(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
 	} else
 	fbPolyLine(pDrawable, pGC, mode, npt, ppt);
 
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
 
 fail:
diff --git a/src/glamor_priv.h b/src/glamor_priv.h
index b19a304..d04421b 100644
--- a/src/glamor_priv.h
+++ b/src/glamor_priv.h
@@ -219,6 +219,7 @@ typedef struct glamor_screen_private {
 	int delayed_fallback_pending;
 	glamor_pixmap_validate_function_t *pixmap_validate_funcs;
 	int flags;
+	ScreenPtr screen;
 } glamor_screen_private;
 
 typedef enum glamor_access {
diff --git a/src/glamor_putimage.c b/src/glamor_putimage.c
index 26d5cf7..5813822 100644
--- a/src/glamor_putimage.c
+++ b/src/glamor_putimage.c
@@ -230,7 +230,7 @@ glamor_put_image_xybitmap(DrawablePtr drawable, GCPtr gc,
 	glamor_set_planemask(pixmap, ~0);
 	glamor_fallback(": to %p (%c)\n",
 			drawable, glamor_get_drawable_location(drawable));
-      fail:
+fail:
 	if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
 		fbPutImage(drawable, gc, 1, x, y, w, h, left_pad, XYBitmap,
 			   bits);
@@ -265,6 +265,10 @@ _glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
 	GLfloat xscale, yscale, txscale, tyscale;
 	GLuint tex;
 	int no_alpha, no_revert;
+	GLAMOR_DEFINE_CONTEXT;
+	Bool ret = FALSE;
+
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	if (image_format == XYBitmap) {
 		assert(depth == 1);
 		goto fail;
@@ -396,14 +400,16 @@ _glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
 		dispatch->glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 	glamor_set_alu(dispatch, GXcopy);
 	glamor_set_planemask(pixmap, ~0);
-	return TRUE;
 
-      fail:
+	ret = TRUE;
+	goto done;
+
+fail:
 	glamor_set_planemask(pixmap, ~0);
 
 	if (!fallback
 	    && glamor_ddx_fallback_check_pixmap(&pixmap->drawable))
-		return FALSE;
+		goto done;
 
 	glamor_fallback("to %p (%c)\n",
 			drawable, glamor_get_drawable_location(drawable));
@@ -412,7 +418,11 @@ _glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
 			   left_pad, image_format, bits);
 		glamor_finish_access(&pixmap->drawable, GLAMOR_ACCESS_RW);
 	}
-	return TRUE;
+	ret = TRUE;
+
+done:
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	return ret;
 }
 
 void
diff --git a/src/glamor_render.c b/src/glamor_render.c
index 789c684..b240eac 100644
--- a/src/glamor_render.c
+++ b/src/glamor_render.c
@@ -870,7 +870,10 @@ glamor_composite_with_shader(CARD8 op,
 	dest_pixmap_priv = glamor_get_pixmap_private(dest_pixmap);
 	int vert_stride = 4;
 	int nrect_max;
+	GLAMOR_DEFINE_CONTEXT;
+	Bool ret = FALSE;
 
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dest_pixmap_priv)) {
 		glamor_fallback("dest has no fbo.\n");
 		goto fail;
@@ -1230,15 +1233,20 @@ glamor_composite_with_shader(CARD8 op,
 	dispatch->glUseProgram(0);
 	if (saved_source_format)
 		source->format = saved_source_format;
-	return TRUE;
 
-      fail:
+	ret = TRUE;
+	goto done;
+
+fail:
 	if (saved_source_format)
 		source->format = saved_source_format;
 
 	dispatch->glDisable(GL_BLEND);
 	dispatch->glUseProgram(0);
-	return FALSE;
+
+done:
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	return ret;
 }
 
 static PicturePtr
@@ -1308,11 +1316,11 @@ _glamor_composite(CARD8 op,
 	int prect_size = ARRAY_SIZE(rect);
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
 	Bool ret = TRUE;
 	RegionRec region;
 	BoxPtr box;
 	int nbox, i, ok;
+	GLAMOR_DEFINE_CONTEXT;
 
 	x_temp_src = x_source;
 	y_temp_src = y_source;
@@ -1320,6 +1328,7 @@ _glamor_composite(CARD8 op,
 	y_temp_mask = y_mask;
 
 	dest_pixmap_priv = glamor_get_pixmap_private(dest_pixmap);
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	/* Currently. Always fallback to cpu if destination is in CPU memory. */
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dest_pixmap_priv)) {
 		goto fail;
@@ -1473,10 +1482,8 @@ _glamor_composite(CARD8 op,
 	if (ok)
 		goto done;
 
-      fail:
+fail:
 
-	dispatch->glUseProgram(0);
-	dispatch->glDisable(GL_BLEND);
 	if (!fallback
 	    && glamor_ddx_fallback_check_pixmap(&dest_pixmap->drawable)
 	    && (!source_pixmap 
@@ -1528,6 +1535,7 @@ _glamor_composite(CARD8 op,
 		FreePicture(temp_mask, 0);
 	if (prect != rect)
 		free(prect);
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_setspans.c b/src/glamor_setspans.c
index 26e774b..07c94ab 100644
--- a/src/glamor_setspans.c
+++ b/src/glamor_setspans.c
@@ -47,7 +47,10 @@ _glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
 	RegionPtr clip = fbGetCompositeClip(gc);
 	BoxRec *pbox;
 	int x_off, y_off;
+	GLAMOR_DEFINE_CONTEXT;
+	Bool ret = FALSE;
 
+	GLAMOR_SET_CONTEXT(glamor_priv);
 	dest_pixmap_priv = glamor_get_pixmap_private(dest_pixmap);
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dest_pixmap_priv)) {
 		glamor_fallback("pixmap has no fbo.\n");
@@ -100,18 +103,25 @@ _glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
 	glamor_set_planemask(dest_pixmap, ~0);
 	glamor_set_alu(dispatch, GXcopy);
 	dispatch->glDisable(GL_SCISSOR_TEST);
-	return TRUE;
-      fail:
+	ret = TRUE;
+	goto done;
+
+fail:
 	if (!fallback
 	    && glamor_ddx_fallback_check_pixmap(drawable))
-		return FALSE;
+		goto done;
+
 	glamor_fallback("to %p (%c)\n",
 			drawable, glamor_get_drawable_location(drawable));
 	if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
 		fbSetSpans(drawable, gc, src, points, widths, n, sorted);
 		glamor_finish_access(drawable, GLAMOR_ACCESS_RW);
 	}
-	return TRUE;
+	ret = TRUE;
+
+done:
+	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	return ret;
 }
 
 void
diff --git a/src/glamor_utils.h b/src/glamor_utils.h
index 74ce6cd..a66e248 100644
--- a/src/glamor_utils.h
+++ b/src/glamor_utils.h
@@ -663,4 +663,33 @@ static inline void glamor_dump_pixmap(PixmapPtr pixmap, int x, int y, int w, int
 	}
 	glamor_finish_access(&pixmap->drawable, GLAMOR_ACCESS_RO);
 }
+
+static inline void *glamor_make_current(ScreenPtr screen)
+{
+	return glamor_egl_make_current(screen);
+}
+
+static inline void glamor_restore_current(ScreenPtr screen, void *previous_context)
+{
+	glamor_egl_restore_context(screen, previous_context);
+}
+
+#ifdef GLX_USE_SHARED_DISPATCH
+#define GLAMOR_DEFINE_CONTEXT		void *_previous_context_ = NULL
+#define GLAMOR_SET_CONTEXT(glamor_priv)			\
+	if (glamor_priv->flags & GLAMOR_USE_EGL_SCREEN) \
+		_previous_context_ = glamor_make_current(glamor_priv->screen)
+
+#define GLAMOR_RESTORE_CONTEXT(glamor_priv)			\
+	if ((glamor_priv->flags & GLAMOR_USE_EGL_SCREEN)	\
+	     && _previous_context_ != NULL) 			\
+		glamor_restore_current(glamor_priv->screen, _previous_context_)
+#else
+
+#define GLAMOR_DEFINE_CONTEXT
+#define GLAMOR_SET_CONTEXT(glamor_priv)
+#define GLAMOR_RESTORE_CONTEXT(glamor_priv)
+
+#endif
+
 #endif
diff --git a/src/glapi.h b/src/glapi.h
new file mode 100644
index 0000000..a7d4e0a
--- /dev/null
+++ b/src/glapi.h
@@ -0,0 +1,116 @@
+/*
+ * Mesa 3-D graphics library
+ * Version:  7.1
+ *
+ * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+/**
+ * \mainpage Mesa GL API Module
+ *
+ * \section GLAPIIntroduction Introduction
+ *
+ * The Mesa GL API module is responsible for dispatching all the
+ * gl*() functions.  All GL functions are dispatched by jumping through
+ * the current dispatch table (basically a struct full of function
+ * pointers.)
+ *
+ * A per-thread current dispatch table and per-thread current context
+ * pointer are managed by this module too.
+ *
+ * This module is intended to be non-Mesa-specific so it can be used
+ * with the X/DRI libGL also.
+ */
+
+#ifndef _GLAPI_H
+#define _GLAPI_H
+
+#define GL_GLEXT_PROTOTYPES
+
+#include "GL/gl.h"
+#include "GL/glext.h"
+
+/* Is this needed?  It is incomplete anyway. */
+#ifdef USE_MGL_NAMESPACE
+#define _glapi_set_dispatch _mglapi_set_dispatch
+#define _glapi_get_dispatch _mglapi_get_dispatch
+#define _glapi_set_context _mglapi_set_context
+#define _glapi_get_context _mglapi_get_context
+#define _glapi_Dispatch _mglapi_Dispatch
+#define _glapi_Context _mglapi_Context
+#endif
+
+typedef void (*_glapi_proc)(void);
+struct _glapi_table;
+
+
+#if defined (GLX_USE_TLS)
+
+extern __thread struct _glapi_table * _glapi_tls_Dispatch
+    __attribute__((tls_model("initial-exec")));
+
+extern __thread void * _glapi_tls_Context
+    __attribute__((tls_model("initial-exec")));
+
+extern const struct _glapi_table *_glapi_Dispatch;
+extern const void *_glapi_Context;
+
+# define GET_DISPATCH() _glapi_tls_Dispatch
+# define GET_CURRENT_CONTEXT(C)  struct gl_context *C = (struct gl_context *) _glapi_tls_Context
+# define SET_CURRENT_CONTEXT(C)  _glapi_tls_Context = (void*)C
+
+#else
+
+extern struct _glapi_table *_glapi_Dispatch;
+extern void *_glapi_Context;
+
+# ifdef THREADS
+
+#  define GET_DISPATCH() \
+     (likely(_glapi_Dispatch) ? _glapi_Dispatch : _glapi_get_dispatch())
+
+#  define GET_CURRENT_CONTEXT(C)  struct gl_context *C = (struct gl_context *) \
+     (likely(_glapi_Context) ? _glapi_Context : _glapi_get_context())
+
+
+# define SET_CURRENT_CONTEXT(C) do { if (likely(_glapi_Context))   \
+					_glapi_Context = (void*)C; \
+				     else \
+					_glapi_set_context(C); } while(0)
+
+# else
+
+#  define GET_DISPATCH() _glapi_Dispatch
+#  define GET_CURRENT_CONTEXT(C)  struct gl_context *C = (struct gl_context *) _glapi_Context
+# define SET_CURRENT_CONTEXT(C)  _glapi_Context = (void*)C
+
+# endif
+
+#endif /* defined (GLX_USE_TLS) */
+
+
+extern void
+_glapi_set_context(void *context);
+
+extern void *
+_glapi_get_context(void);
+
+#endif
-- 
1.7.4.4



More information about the Glamor mailing list