[Glamor] [PATCH] Fixup glx support

Chris Wilson chris at chris-wilson.co.uk
Fri Feb 10 04:54:44 PST 2012


---
 src/glamor.c              |   21 +++++++-------
 src/glamor.h              |   24 +++++++---------
 src/glamor_addtraps.c     |    4 ---
 src/glamor_copyarea.c     |   42 ++++++++++++----------------
 src/glamor_copyplane.c    |    7 -----
 src/glamor_copywindow.c   |    4 ---
 src/glamor_core.c         |   20 +++++++------
 src/glamor_egl.c          |   45 +++++++++++++++++-------------
 src/glamor_eglmodule.c    |    4 +-
 src/glamor_fbo.c          |   25 +++++------------
 src/glamor_fill.c         |   26 ++++++++---------
 src/glamor_fillspans.c    |    7 -----
 src/glamor_getimage.c     |   12 ++------
 src/glamor_getspans.c     |   11 ++-----
 src/glamor_glyphblt.c     |   24 +---------------
 src/glamor_glyphs.c       |   10 ++-----
 src/glamor_picture.c      |    6 +---
 src/glamor_pixmap.c       |   37 +++++++++++++++----------
 src/glamor_polyfillrect.c |    7 -----
 src/glamor_polylines.c    |    7 -----
 src/glamor_polyops.c      |   41 +++------------------------
 src/glamor_priv.h         |    6 +---
 src/glamor_putimage.c     |    9 +++---
 src/glamor_render.c       |   67 ++++++++++++++++++++++++---------------------
 src/glamor_setspans.c     |   16 +++--------
 src/glamor_tile.c         |   14 +++++-----
 src/glamor_triangles.c    |    4 ---
 src/glamor_utils.h        |   47 +++++++++++++++++++++-----------
 src/glamor_window.c       |    4 ---
 29 files changed, 218 insertions(+), 333 deletions(-)

diff --git a/src/glamor.c b/src/glamor.c
index 6b6330f..86de115 100644
--- a/src/glamor.c
+++ b/src/glamor.c
@@ -196,24 +196,25 @@ 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_gl_dispatch *dispatch;
 
-	GLAMOR_SET_CONTEXT(glamor_priv);
+	dispatch = glamor_get_dispatch(glamor_priv);
 	glamor_priv->tick++;
 	dispatch->glFlush();
 	dispatch->glFinish();
 	glamor_fbo_expire(glamor_priv);
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
+	glamor_put_dispatch(glamor_priv);
 }
 
 static void
 _glamor_block_handler(void *data, OSTimePtr timeout,
 		      void *last_select_mask)
 {
-	glamor_gl_dispatch *dispatch = data;
+	glamor_screen_private *glamor_priv = data;
+	glamor_gl_dispatch *dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glFlush();
 	dispatch->glFinish();
+	glamor_put_dispatch(glamor_priv);
 }
 
 static void
@@ -289,8 +290,7 @@ glamor_init(ScreenPtr screen, unsigned int flags)
 	}
 #endif
 
-	glamor_gl_dispatch_init(screen, &glamor_priv->dispatch,
-				gl_version);
+	glamor_gl_dispatch_init(screen, &glamor_priv->_dispatch, gl_version);
 
 #ifdef GLAMOR_GLES2
 	if (!glamor_gl_has_extension("GL_EXT_texture_format_BGRA8888")) {
@@ -303,8 +303,8 @@ glamor_init(ScreenPtr screen, unsigned int flags)
 	    glamor_gl_has_extension("GL_MESA_pack_invert");
 	glamor_priv->has_fbo_blit =
 	    glamor_gl_has_extension("GL_EXT_framebuffer_blit");
-	glamor_priv->dispatch.glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE,
-					    &glamor_priv->max_fbo_size);
+	glamor_priv->_dispatch.glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE,
+					     &glamor_priv->max_fbo_size);
 
 	glamor_set_debug_level(&glamor_debug_level);
 
@@ -324,8 +324,7 @@ glamor_init(ScreenPtr screen, unsigned int flags)
 	if (flags & GLAMOR_USE_SCREEN) {
 		if (!RegisterBlockAndWakeupHandlers(_glamor_block_handler,
 						    _glamor_wakeup_handler,
-						    (void *)
-						    &glamor_priv->dispatch)) {
+						    glamor_priv)) {
 			goto fail;
 		}
 
diff --git a/src/glamor.h b/src/glamor.h
index 712a7a9..d3437d3 100644
--- a/src/glamor.h
+++ b/src/glamor.h
@@ -28,18 +28,16 @@
 #ifndef GLAMOR_H
 #define GLAMOR_H
 
-#include "scrnintstr.h"
+#include <scrnintstr.h>
 #ifdef GLAMOR_FOR_XORG
-#include "xf86str.h"
+#include <xf86str.h>
 #endif
-#include "pixmapstr.h"
-#include "windowstr.h"
-#include "gcstruct.h"
-#include "picturestr.h"
-#include "fb.h"
-#include "fbpict.h"
-
-#endif				/* GLAMOR_H */
+#include <pixmapstr.h>
+#include <windowstr.h>
+#include <gcstruct.h>
+#include <picturestr.h>
+#include <fb.h>
+#include <fbpict.h>
 
 /*
  * glamor_pixmap_type : glamor pixmap's type.
@@ -145,9 +143,8 @@ 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);
+extern _X_EXPORT void glamor_egl_make_current(ScreenPtr screen);
+extern _X_EXPORT void glamor_egl_restore_context(ScreenPtr screen);
 
 #ifdef GLAMOR_FOR_XORG
 
@@ -320,3 +317,4 @@ extern _X_EXPORT Bool glamor_poly_line_nf(DrawablePtr pDrawable, GCPtr pGC, int
 extern _X_EXPORT Bool glamor_poly_lines_nf(DrawablePtr drawable, GCPtr gc, int mode, int n,
 					   DDXPointPtr points);
 
+#endif /* GLAMOR_H */
diff --git a/src/glamor_addtraps.c b/src/glamor_addtraps.c
index 28775e5..ac85296 100644
--- a/src/glamor_addtraps.c
+++ b/src/glamor_addtraps.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 static Bool
diff --git a/src/glamor_copyarea.c b/src/glamor_copyarea.c
index e656934..cad9e25 100644
--- a/src/glamor_copyarea.c
+++ b/src/glamor_copyarea.c
@@ -21,10 +21,6 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_copyarea.c
@@ -43,7 +39,7 @@ glamor_copy_n_to_n_fbo_blit(DrawablePtr src,
 	glamor_pixmap_private *src_pixmap_priv;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	int dst_x_off, dst_y_off, src_x_off, src_y_off, i;
 
 	if (!glamor_priv->has_fbo_blit) {
@@ -78,6 +74,7 @@ glamor_copy_n_to_n_fbo_blit(DrawablePtr src,
 	}
 	glamor_validate_pixmap(dst_pixmap);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT,
 				    src_pixmap_priv->fbo->fb);
 	glamor_get_drawable_deltas(dst, dst_pixmap, &dst_x_off,
@@ -136,6 +133,7 @@ glamor_copy_n_to_n_fbo_blit(DrawablePtr src,
 						    GL_NEAREST);
 		}
 	}
+	glamor_put_dispatch(glamor_priv);
 	return TRUE;
 }
 #endif
@@ -147,7 +145,7 @@ glamor_copy_n_to_n_textured(DrawablePtr src,
 {
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(dst->pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	PixmapPtr src_pixmap = glamor_get_drawable_pixmap(src);
 	PixmapPtr dst_pixmap = glamor_get_drawable_pixmap(dst);
 	int i;
@@ -158,23 +156,24 @@ glamor_copy_n_to_n_textured(DrawablePtr src,
 	enum glamor_pixmap_status src_status = GLAMOR_NONE;
 	GLfloat dst_xscale, dst_yscale, src_xscale, src_yscale;
 	int flush_needed = 0;
+	int alu = GXcopy;
 
 	src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
 	dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);
 
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dst_pixmap_priv)) {
 		glamor_delayed_fallback(dst->pScreen, "dst has no fbo.\n");
-		goto fail;
+		return FALSE;
 	}
 
 	if (!src_pixmap_priv || !src_pixmap_priv->gl_fbo) {
 #ifndef GLAMOR_PIXMAP_DYNAMIC_UPLOAD
 		glamor_delayed_fallback(dst->pScreen, "src has no fbo.\n");
-		goto fail;
+		return FALSE;
 #else
 		src_status = glamor_upload_pixmap_to_texture(src_pixmap);
 		if (src_status != GLAMOR_UPLOAD_DONE)
-			goto fail;
+			return FALSE;
 
 		src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
 #endif
@@ -182,14 +181,9 @@ glamor_copy_n_to_n_textured(DrawablePtr src,
 		flush_needed = 1;
 
 	if (gc) {
-		glamor_set_alu(dispatch, gc->alu);
 		if (!glamor_set_planemask(dst_pixmap, gc->planemask))
-			goto fail;
-		if (gc->alu != GXcopy) {
-			glamor_set_destination_pixmap_priv_nc
-			    (src_pixmap_priv);
-			glamor_validate_pixmap(src_pixmap);
-		}
+			return FALSE;
+		alu = gc->alu;
 	}
 
 	glamor_set_destination_pixmap_priv_nc(dst_pixmap_priv);
@@ -201,6 +195,13 @@ glamor_copy_n_to_n_textured(DrawablePtr src,
 	glamor_get_drawable_deltas(dst, dst_pixmap, &dst_x_off,
 				   &dst_y_off);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
+
+	glamor_set_alu(dispatch, alu);
+	if (alu != GXcopy) {
+		glamor_set_destination_pixmap_priv_nc (src_pixmap_priv);
+		glamor_validate_pixmap(src_pixmap);
+	}
 	dispatch->glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
 					GL_FALSE, 2 * sizeof(float),
 					vertices);
@@ -284,12 +285,8 @@ glamor_copy_n_to_n_textured(DrawablePtr src,
 	/* The source texture is bound to a fbo, we have to flush it here. */
 	if (flush_needed)
 		dispatch->glFlush();
+	glamor_put_dispatch(glamor_priv);
 	return TRUE;
-
-      fail:
-	glamor_set_alu(dispatch, GXcopy);
-	glamor_set_planemask(dst_pixmap, ~0);
-	return FALSE;
 }
 
 static Bool 
@@ -317,7 +314,6 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	int i;
 	int overlaped = 0;
 	Bool ret = FALSE;
-	GLAMOR_DEFINE_CONTEXT;
 
 	dst_pixmap = glamor_get_drawable_pixmap(dst);
 	dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);
@@ -326,7 +322,6 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	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",
@@ -448,7 +443,6 @@ _glamor_copy_n_to_n(DrawablePtr src,
 	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 b6b26a5..6487ff7 100644
--- a/src/glamor_copyplane.c
+++ b/src/glamor_copyplane.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 static Bool
@@ -37,7 +33,6 @@ _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 
@@ -47,14 +42,12 @@ _glamor_copy_plane(DrawablePtr pSrc, DrawablePtr pDst, GCPtr pGC,
 		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_copywindow.c b/src/glamor_copywindow.c
index 11b3036..b181ff5 100644
--- a/src/glamor_copywindow.c
+++ b/src/glamor_copywindow.c
@@ -21,10 +21,6 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_copywindow.c
diff --git a/src/glamor_core.c b/src/glamor_core.c
index 01c9aea..0376388 100644
--- a/src/glamor_core.c
+++ b/src/glamor_core.c
@@ -31,10 +31,6 @@
  * This file covers core X rendering in glamor.
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include <stdlib.h>
 
 #include "glamor_priv.h"
@@ -174,7 +170,7 @@ glamor_init_finish_access_shaders(ScreenPtr screen)
 	GLint sampler_uniform_location;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch =  &glamor_priv->dispatch;
+	dispatch =  glamor_get_dispatch(glamor_priv);
 	glamor_priv->finish_access_prog[0] = dispatch->glCreateProgram();
 	glamor_priv->finish_access_prog[1] = dispatch->glCreateProgram();
 
@@ -248,7 +244,7 @@ glamor_init_finish_access_shaders(ScreenPtr screen)
 	dispatch->glUniform1i(sampler_uniform_location, 0);
 	dispatch->glUniform1i(glamor_priv->finish_access_swap_rb[1], 0);
 	dispatch->glUseProgram(0);
-
+	glamor_put_dispatch(glamor_priv);
 }
 
 void
@@ -258,9 +254,10 @@ glamor_fini_finish_access_shaders(ScreenPtr screen)
 	glamor_gl_dispatch *dispatch;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch =  &glamor_priv->dispatch;
+	dispatch =  glamor_get_dispatch(glamor_priv);
 	dispatch->glDeleteProgram(glamor_priv->finish_access_prog[0]);
 	dispatch->glDeleteProgram(glamor_priv->finish_access_prog[1]);
+	glamor_put_dispatch(glamor_priv);
 }
 
 void
@@ -271,7 +268,6 @@ glamor_finish_access(DrawablePtr drawable, glamor_access_t access_mode)
 	    glamor_get_pixmap_private(pixmap);
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(drawable->pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
 
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO_DOWNLOADED(pixmap_priv))
 		return;
@@ -281,11 +277,17 @@ glamor_finish_access(DrawablePtr drawable, glamor_access_t access_mode)
 	}
 
 	if (pixmap_priv->fbo->pbo != 0 && pixmap_priv->fbo->pbo_valid) {
+		glamor_gl_dispatch *dispatch;
+
 		assert(glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP);
+
+		dispatch = glamor_get_dispatch(glamor_priv);
 		dispatch->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
 		dispatch->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
-		pixmap_priv->fbo->pbo_valid = FALSE;
 		dispatch->glDeleteBuffers(1, &pixmap_priv->fbo->pbo);
+		glamor_put_dispatch(glamor_priv);
+
+		pixmap_priv->fbo->pbo_valid = FALSE;
 		pixmap_priv->fbo->pbo = 0;
 	} else {
 		free(pixmap->devPrivate.ptr);
diff --git a/src/glamor_egl.c b/src/glamor_egl.c
index a339527..a402e88 100644
--- a/src/glamor_egl.c
+++ b/src/glamor_egl.c
@@ -31,11 +31,8 @@
 #include "config.h"
 #endif
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
 #define GLAMOR_FOR_XORG
-#include "xorg-server.h"
+#include <xorg-server.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
@@ -93,7 +90,8 @@ struct glamor_egl_screen_private {
 	struct gbm_device *gbm;
 #endif
 	int has_gem;
-	void *gl_context;
+	void *gl_context, *old_context;
+	int gl_context_depth;
 
 	PFNEGLCREATEIMAGEKHRPROC egl_create_image_khr;
 	PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image_khr;
@@ -112,13 +110,18 @@ glamor_egl_get_screen_private(ScrnInfoPtr scrn)
 	    scrn->privates[xf86GlamorEGLPrivateIndex].ptr;
 }
 #ifdef GLX_USE_SHARED_DISPATCH
-_X_EXPORT void *
+_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);
+
+	if (glamor_egl->gl_context_depth++)
+		return;
+
 	GET_CURRENT_CONTEXT(current);
+	glamor_egl->old_context = current;
 
 	if (glamor_egl->gl_context != current) {
 		eglMakeCurrent(glamor_egl->display, EGL_NO_SURFACE,
@@ -126,24 +129,28 @@ glamor_egl_make_current(ScreenPtr screen)
 		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;
+			FatalError("Failed to make EGL context current\n");
 		}
-		return current;
 	}
-	return NULL;
 }
 
 _X_EXPORT void
-glamor_egl_restore_context(ScreenPtr screen, void *context)
+glamor_egl_restore_context(ScreenPtr screen)
 {
-	if (context)
-		SET_CURRENT_CONTEXT(context);
+	ScrnInfoPtr scrn = xf86Screens[screen->myNum];
+	struct glamor_egl_screen_private *glamor_egl =
+	    glamor_egl_get_screen_private(scrn);
+
+	if (--glamor_egl->gl_context_depth)
+		return;
+
+	if (glamor_egl->old_context &&
+	    glamor_egl->gl_context != glamor_egl->old_context)
+		SET_CURRENT_CONTEXT(glamor_egl->old_context);
 }
 #else
-#define glamor_egl_make_current(x)  NULL
-#define glamor_egl_restore_context(s, c)
+#define glamor_egl_make_current(x)
+#define glamor_egl_restore_context(s)
 #endif
 
 static EGLImageKHR
@@ -252,12 +259,11 @@ 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);
+	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,
@@ -285,8 +291,7 @@ glamor_egl_create_textured_pixmap(PixmapPtr pixmap, int handle, int stride)
 	ret = TRUE;
 
 done:
-	if (prev_context)
-		glamor_egl_restore_context(screen, prev_context);
+	glamor_egl_restore_context(screen);
 	return ret;
 }
 
diff --git a/src/glamor_eglmodule.c b/src/glamor_eglmodule.c
index e1f6672..0961651 100644
--- a/src/glamor_eglmodule.c
+++ b/src/glamor_eglmodule.c
@@ -28,10 +28,10 @@
 #include "config.h"
 #endif
 
-#include <xorg-server.h>
 #define GLAMOR_FOR_XORG
 #include "glamor.h"
-#include "xf86Module.h"
+#include <xf86Module.h>
+#include <xorg-server.h>
 
 static XF86ModuleVersionInfo VersRec = {
 	GLAMOR_EGL_MODULE_NAME,
diff --git a/src/glamor_fbo.c b/src/glamor_fbo.c
index 929caba..e5675d8 100644
--- a/src/glamor_fbo.c
+++ b/src/glamor_fbo.c
@@ -1,7 +1,3 @@
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include <stdlib.h>
 
 #include "glamor_priv.h"
@@ -126,17 +122,14 @@ 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;
+	glamor_gl_dispatch *dispatch = glamor_get_dispatch(fbo->glamor_priv);
 	if (fbo->fb)
 		dispatch->glDeleteFramebuffers(1, &fbo->fb);
 	if (fbo->tex)
 		dispatch->glDeleteTextures(1, &fbo->tex);
 	if (fbo->pbo)
 		dispatch->glDeleteBuffers(1, &fbo->pbo);
-	GLAMOR_RESTORE_CONTEXT(fbo->glamor_priv);
+	glamor_put_dispatch(fbo->glamor_priv);
 
 	free(fbo);
 }
@@ -292,7 +285,6 @@ 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;
@@ -305,10 +297,9 @@ 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;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glGenTextures(1, &tex);
 	dispatch->glBindTexture(GL_TEXTURE_2D, tex);
 	dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
@@ -317,12 +308,13 @@ glamor_create_tex_obj(glamor_screen_private *glamor_priv,
 				  GL_NEAREST);
 	dispatch->glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format,
 			       GL_UNSIGNED_BYTE, NULL);
+	glamor_put_dispatch(glamor_priv);
+
 	fbo->tex = tex;
 	fbo->width = w;
 	fbo->height = h;
 	fbo->format = format;
 	fbo->glamor_priv = glamor_priv;
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 
 	return fbo;
 }
@@ -344,7 +336,6 @@ 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))
@@ -364,9 +355,7 @@ glamor_create_fbo(glamor_screen_private *glamor_priv,
 	if (fbo)
 		return fbo;
 new_fbo:
-
-	GLAMOR_SET_CONTEXT(glamor_priv);
-	dispatch = &glamor_priv->dispatch;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glGenTextures(1, &tex);
 	dispatch->glBindTexture(GL_TEXTURE_2D, tex);
 	dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
@@ -377,7 +366,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);
+	glamor_put_dispatch(glamor_priv);
 
 	return fbo;
 }
diff --git a/src/glamor_fill.c b/src/glamor_fill.c
index b462f59..8573309 100644
--- a/src/glamor_fill.c
+++ b/src/glamor_fill.c
@@ -21,10 +21,6 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_fillspans.c
@@ -112,7 +108,7 @@ glamor_init_solid_shader(ScreenPtr screen)
 	GLint fs_prog, vs_prog;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch =  &glamor_priv->dispatch;
+	dispatch =  glamor_get_dispatch(glamor_priv);
 	glamor_priv->solid_prog = dispatch->glCreateProgram();
 	vs_prog = glamor_compile_glsl_prog(dispatch, GL_VERTEX_SHADER, solid_vs);
 	fs_prog = glamor_compile_glsl_prog(dispatch, GL_FRAGMENT_SHADER,
@@ -127,6 +123,7 @@ glamor_init_solid_shader(ScreenPtr screen)
 	glamor_priv->solid_color_uniform_location =
 	    dispatch->glGetUniformLocation(glamor_priv->solid_prog,
 					   "color");
+	glamor_put_dispatch(glamor_priv);
 }
 
 void
@@ -136,8 +133,9 @@ glamor_fini_solid_shader(ScreenPtr screen)
 	glamor_gl_dispatch *dispatch;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch =  &glamor_priv->dispatch;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glDeleteProgram(glamor_priv->solid_prog);
+	glamor_put_dispatch(glamor_priv);
 }
 
 Bool
@@ -150,7 +148,7 @@ glamor_solid(PixmapPtr pixmap, int x, int y, int width, int height,
 	    glamor_get_screen_private(screen);
 	glamor_pixmap_private *pixmap_priv =
 	    glamor_get_pixmap_private(pixmap);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	int x1 = x;
 	int x2 = x + width;
 	int y1 = y;
@@ -158,15 +156,16 @@ glamor_solid(PixmapPtr pixmap, int x, int y, int width, int height,
 	GLfloat color[4];
 	float vertices[8];
 	GLfloat xscale, yscale;
+
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) {
 		glamor_fallback("dest %p has no fbo.\n", pixmap);
-		goto fail;
+		return FALSE;
 	}
-	glamor_set_alu(dispatch, alu);
+
 	if (!glamor_set_planemask(pixmap, planemask)) {
 		glamor_fallback
 		    ("Failedto set planemask  in glamor_solid.\n");
-		goto fail;
+		return FALSE;
 	}
 
 	glamor_get_rgba_from_pixel(fg_pixel,
@@ -189,6 +188,8 @@ glamor_solid(PixmapPtr pixmap, int x, int y, int width, int height,
 	glamor_set_destination_pixmap_priv_nc(pixmap_priv);
 	glamor_validate_pixmap(pixmap);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
+	glamor_set_alu(dispatch, alu);
 	dispatch->glUseProgram(glamor_priv->solid_prog);
 
 	dispatch->glUniform4fv(glamor_priv->solid_color_uniform_location,
@@ -205,9 +206,6 @@ glamor_solid(PixmapPtr pixmap, int x, int y, int width, int height,
 	dispatch->glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 	dispatch->glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
 	dispatch->glUseProgram(0);
+	glamor_put_dispatch(glamor_priv);
 	return TRUE;
-      fail:
-	glamor_set_alu(dispatch, GXcopy);
-	glamor_set_planemask(pixmap, ~0);
-	return FALSE;
 }
diff --git a/src/glamor_fillspans.c b/src/glamor_fillspans.c
index 97e4497..6598249 100644
--- a/src/glamor_fillspans.c
+++ b/src/glamor_fillspans.c
@@ -20,10 +20,6 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 /** @file glamor_fillspans.c
  *
  * FillSpans implementation, taken from fb_fillsp.c
@@ -41,11 +37,9 @@ _glamor_fill_spans(DrawablePtr drawable,
 	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;
@@ -99,7 +93,6 @@ fail:
 	ret = TRUE;
 
 done:
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_getimage.c b/src/glamor_getimage.c
index 7caf2a3..efbd1ba 100644
--- a/src/glamor_getimage.c
+++ b/src/glamor_getimage.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 
@@ -46,13 +42,11 @@ _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;
@@ -65,7 +59,6 @@ _glamor_get_image(DrawablePtr drawable, int x, int y, int w, int h,
 	}
 	glamor_priv = glamor_get_screen_private(drawable->pScreen);
 	pixmap_priv = glamor_get_pixmap_private(pixmap);
-	dispatch = &glamor_priv->dispatch;
 
 
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
@@ -97,6 +90,8 @@ _glamor_get_image(DrawablePtr drawable, int x, int y, int w, int h,
 
 	int row_length = PixmapBytePad(w, drawable->depth);
 	row_length = (row_length * 8) / drawable->bitsPerPixel;
+
+	dispatch = glamor_get_dispatch(glamor_priv);
 	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
 		dispatch->glPixelStorei(GL_PACK_ALIGNMENT, 1);
 		dispatch->glPixelStorei(GL_PACK_ROW_LENGTH, row_length);
@@ -117,14 +112,13 @@ _glamor_get_image(DrawablePtr drawable, int x, int y, int w, int h,
 				       h,
 				       tex_format,
 				       tex_type, d);
+	glamor_put_dispatch(glamor_priv);
 	if (temp_pixmap)
 		glamor_destroy_pixmap(temp_pixmap);
 
 	ret = TRUE;
 
 fall_back:
-
-	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 2d30f37..91030a3 100644
--- a/src/glamor_getspans.c
+++ b/src/glamor_getspans.c
@@ -25,10 +25,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 static Bool 
@@ -44,15 +40,13 @@ _glamor_get_spans(DrawablePtr drawable,
 	    glamor_get_screen_private(drawable->pScreen);
 	glamor_pixmap_private *pixmap_priv =
 	    glamor_get_pixmap_private(pixmap);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	PixmapPtr temp_pixmap = NULL;
 	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;
@@ -82,6 +76,7 @@ _glamor_get_spans(DrawablePtr drawable,
 	}
 
 	glamor_get_drawable_deltas(drawable, pixmap, &x_off, &y_off);
+	dispatch = glamor_get_dispatch(glamor_priv);
 	for (i = 0; i < count; i++) {
 		if (glamor_priv->yInverted) {
 			dispatch->glReadPixels(points[i].x + x_off,
@@ -98,6 +93,7 @@ _glamor_get_spans(DrawablePtr drawable,
 		readpixels_dst +=
 		    PixmapBytePad(widths[i], drawable->depth);
 	}
+	glamor_put_dispatch(glamor_priv);
 	if (temp_pixmap)
 		glamor_destroy_pixmap(temp_pixmap);
 
@@ -116,7 +112,6 @@ fail:
 		glamor_finish_access(drawable, GLAMOR_ACCESS_RO);
 	}
 done:
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_glyphblt.c b/src/glamor_glyphblt.c
index 8c853e9..1630998 100644
--- a/src/glamor_glyphblt.c
+++ b/src/glamor_glyphblt.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 static Bool
@@ -37,7 +33,6 @@ _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 
@@ -46,13 +41,11 @@ _glamor_image_glyph_blt(DrawablePtr pDrawable, GCPtr 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;
@@ -79,25 +72,20 @@ _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;
+		return FALSE;
 
 	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;
 }
 
 void
@@ -120,17 +108,12 @@ 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;
+		return FALSE;
 
-	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);
@@ -138,10 +121,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;
 }
 
 void
diff --git a/src/glamor_glyphs.c b/src/glamor_glyphs.c
index 33e06f1..fe7d9de 100644
--- a/src/glamor_glyphs.c
+++ b/src/glamor_glyphs.c
@@ -40,15 +40,11 @@
  * Based on code by: Keith Packard
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include <stdlib.h>
 
 #include "glamor_priv.h"
 
-#include "mipict.h"
+#include <mipict.h>
 
 #if DEBUG_GLYPH_CACHE
 #define DBG_GLYPH_CACHE(a) ErrorF a
@@ -163,7 +159,7 @@ glamor_realize_glyph_caches(ScreenPtr pScreen)
 		glamor_glyph_cache_t *cache = &glamor->glyphCaches[i];
 		PixmapPtr pixmap;
 		PicturePtr picture;
-		CARD32 component_alpha;
+		XID component_alpha;
 		int depth = PIXMAN_FORMAT_DEPTH(formats[i]);
 		int error;
 		PictFormatPtr pPictFormat =
@@ -652,7 +648,7 @@ glamor_glyphs_via_mask(CARD8 op,
 	GlyphPtr glyph;
 	int error;
 	BoxRec extents = { 0, 0, 0, 0 };
-	CARD32 component_alpha;
+	XID component_alpha;
 	glamor_glyph_buffer_t buffer;
 	xRectangle fill_rect;
 
diff --git a/src/glamor_picture.c b/src/glamor_picture.c
index 6904dab..815377d 100644
--- a/src/glamor_picture.c
+++ b/src/glamor_picture.c
@@ -1,11 +1,7 @@
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include <stdlib.h>
 
-#include "mipict.h"
 #include "glamor_priv.h"
+#include <mipict.h>
 
 /* Upload picture to texture.  We may need to flip the y axis or
  * wire alpha to 1. So we may conditional create fbo for the picture.
diff --git a/src/glamor_pixmap.c b/src/glamor_pixmap.c
index 1a63ffd..7f4a90b 100644
--- a/src/glamor_pixmap.c
+++ b/src/glamor_pixmap.c
@@ -1,7 +1,3 @@
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include <stdlib.h>
 
 #include "glamor_priv.h"
@@ -31,7 +27,7 @@ static void
 _glamor_pixmap_validate_filling(glamor_screen_private * glamor_priv,
 				glamor_pixmap_private * pixmap_priv)
 {
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch = glamor_get_dispatch(glamor_priv);
 	GLfloat vertices[8];
 	dispatch->glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
 					GL_FALSE, 2 * sizeof(float),
@@ -52,6 +48,7 @@ _glamor_pixmap_validate_filling(glamor_screen_private * glamor_priv,
 	dispatch->glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
 	dispatch->glUseProgram(0);
 	pixmap_priv->pending_op.type = GLAMOR_PENDING_NONE;
+	glamor_put_dispatch(glamor_priv);
 }
 
 
@@ -94,7 +91,7 @@ glamor_validate_pixmap(PixmapPtr pixmap)
 void
 glamor_set_destination_pixmap_priv_nc(glamor_pixmap_private * pixmap_priv)
 {
-	glamor_gl_dispatch *dispatch = &pixmap_priv->glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch = glamor_get_dispatch(pixmap_priv->glamor_priv);
 	dispatch->glBindFramebuffer(GL_FRAMEBUFFER, pixmap_priv->fbo->fb);
 #ifndef GLAMOR_GLES2
 	dispatch->glMatrixMode(GL_PROJECTION);
@@ -106,6 +103,7 @@ glamor_set_destination_pixmap_priv_nc(glamor_pixmap_private * pixmap_priv)
 			     pixmap_priv->fbo->width,
 			     pixmap_priv->fbo->height);
 
+	glamor_put_dispatch(pixmap_priv->glamor_priv);
 }
 
 int
@@ -222,7 +220,7 @@ __glamor_upload_pixmap_to_texture(PixmapPtr pixmap, GLenum format,
 	    glamor_get_pixmap_private(pixmap);
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(pixmap->drawable.pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	unsigned int stride, row_length;
 	void *texels;
 	GLenum iformat;
@@ -235,6 +233,7 @@ __glamor_upload_pixmap_to_texture(PixmapPtr pixmap, GLenum format,
 	stride = pixmap->devKind;
 	row_length = (stride * 8) / pixmap->drawable.bitsPerPixel;
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glBindTexture(GL_TEXTURE_2D, tex);
 	dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 				  GL_NEAREST);
@@ -271,6 +270,7 @@ __glamor_upload_pixmap_to_texture(PixmapPtr pixmap, GLenum format,
 
 	if (pixmap_priv->fbo->pbo && pixmap_priv->fbo->pbo_valid)
 		dispatch->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+	glamor_put_dispatch(glamor_priv);
 }
 
 
@@ -289,7 +289,7 @@ _glamor_upload_pixmap_to_texture(PixmapPtr pixmap, GLenum format,
 	    glamor_get_pixmap_private(pixmap);
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(pixmap->drawable.pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	static float vertices[8];
 	static float texcoords[8] = { 0, 1,
 		1, 1,
@@ -339,6 +339,7 @@ _glamor_upload_pixmap_to_texture(PixmapPtr pixmap, GLenum format,
 				     vertices);
 
 	/* Slow path, we need to flip y or wire alpha to 1. */
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
 					GL_FALSE, 2 * sizeof(float),
 					vertices);
@@ -379,13 +380,17 @@ _glamor_upload_pixmap_to_texture(PixmapPtr pixmap, GLenum format,
 	dispatch->glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
 	dispatch->glDeleteTextures(1, &tex);
 	dispatch->glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+	glamor_put_dispatch(glamor_priv);
 }
 
 void
 glamor_pixmap_ensure_fb(glamor_pixmap_fbo *fbo)
 {
+	glamor_gl_dispatch *dispatch;
 	int status;
-	glamor_gl_dispatch *dispatch = &fbo->glamor_priv->dispatch;
+
+	dispatch = glamor_get_dispatch(fbo->glamor_priv);
 
 	if (fbo->fb == 0)
 		dispatch->glGenFramebuffers(1, &fbo->fb);
@@ -422,11 +427,10 @@ glamor_pixmap_ensure_fb(glamor_pixmap_fbo *fbo)
 			break;
 		}
 
-		LogMessageVerb(X_INFO, 0,
-			       "destination is framebuffer incomplete: %s [%#x]\n",
-			       str, status);
-		assert(0);
+		FatalError("destination is framebuffer incomplete: %s [%#x]\n",
+			   str, status);
 	}
+	glamor_put_dispatch(fbo->glamor_priv);
 }
 
 /*  
@@ -571,7 +575,6 @@ glamor_es2_pixmap_read_prepare(PixmapPtr source, GLenum * format,
 
 	glamor_priv = glamor_get_screen_private(screen);
 	source_priv = glamor_get_pixmap_private(source);
-	dispatch = &glamor_priv->dispatch;
 	if (*format == GL_BGRA) {
 		*format = GL_RGBA;
 		swap_rb = 1;
@@ -585,6 +588,7 @@ glamor_es2_pixmap_read_prepare(PixmapPtr source, GLenum * format,
 
 	temp_pixmap_priv = glamor_get_pixmap_private(temp_pixmap);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glBindTexture(GL_TEXTURE_2D, temp_pixmap_priv->fbo->tex);
 	dispatch->glTexParameteri(GL_TEXTURE_2D,
 				  GL_TEXTURE_MIN_FILTER,
@@ -631,6 +635,7 @@ glamor_es2_pixmap_read_prepare(PixmapPtr source, GLenum * format,
 	dispatch->glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
 	dispatch->glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
 	dispatch->glUseProgram(0);
+	glamor_put_dispatch(glamor_priv);
 	return temp_pixmap;
 }
 
@@ -657,7 +662,7 @@ glamor_download_pixmap_to_cpu(PixmapPtr pixmap, glamor_access_t access)
 	ScreenPtr screen;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(pixmap->drawable.pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 
 	screen = pixmap->drawable.pScreen;
 	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
@@ -715,6 +720,7 @@ glamor_download_pixmap_to_cpu(PixmapPtr pixmap, glamor_access_t access)
 	}
 	row_length = (stride * 8) / pixmap->drawable.bitsPerPixel;
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
 		dispatch->glPixelStorei(GL_PACK_ALIGNMENT, 1);
 		dispatch->glPixelStorei(GL_PACK_ROW_LENGTH, row_length);
@@ -795,6 +801,7 @@ glamor_download_pixmap_to_cpu(PixmapPtr pixmap, glamor_access_t access)
 	}
 
 	dispatch->glBindFramebuffer(GL_FRAMEBUFFER, 0);
+	glamor_put_dispatch(glamor_priv);
       done:
 
 	pixmap_priv->gl_fbo = GLAMOR_FBO_DOWNLOADED;
diff --git a/src/glamor_polyfillrect.c b/src/glamor_polyfillrect.c
index 8b36241..61e707f 100644
--- a/src/glamor_polyfillrect.c
+++ b/src/glamor_polyfillrect.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_fillspans.c
@@ -48,10 +44,8 @@ _glamor_poly_fill_rect(DrawablePtr drawable,
 	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;
@@ -119,7 +113,6 @@ fail:
 	ret = TRUE;
 
 done:
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_polylines.c b/src/glamor_polylines.c
index 0d37a1d..175e958 100644
--- a/src/glamor_polylines.c
+++ b/src/glamor_polylines.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_polylines.c
@@ -50,7 +46,6 @@ _glamor_poly_lines(DrawablePtr drawable, GCPtr gc, int mode, int n,
 	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) {
@@ -112,7 +107,6 @@ _glamor_poly_lines(DrawablePtr drawable, GCPtr gc, int mode, int n,
 		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)) {
@@ -126,7 +120,6 @@ 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 ca21b8b..7320c17 100644
--- a/src/glamor_polyops.c
+++ b/src/glamor_polyops.c
@@ -26,36 +26,23 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 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;
+		return FALSE;
 
-	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:
-	return FALSE;
 }
 
 void
@@ -76,16 +63,11 @@ 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;
+		return FALSE;
 
-	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);
@@ -94,13 +76,9 @@ _glamor_poly_segment(DrawablePtr pDrawable, GCPtr pGC, int nseg,
 		glamor_finish_access(pDrawable, GLAMOR_ACCESS_RW);
 		glamor_finish_access_gc(pGC);
 	} else
-	fbPolySegment(pDrawable, pGC, nseg, pSeg);
+		fbPolySegment(pDrawable, pGC, nseg, pSeg);
 
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
-
-fail:
-	return FALSE;
 }
 
 void
@@ -121,17 +99,12 @@ 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;
+		return FALSE;
 	/* 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);
@@ -139,13 +112,9 @@ _glamor_poly_line(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
 		glamor_finish_access(pDrawable, GLAMOR_ACCESS_RW);
 		glamor_finish_access_gc(pGC);
 	} else
-	fbPolyLine(pDrawable, pGC, mode, npt, ppt);
+		fbPolyLine(pDrawable, pGC, mode, npt, ppt);
 
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return TRUE;
-
-fail:
-	return FALSE;
 }
 
 void
diff --git a/src/glamor_priv.h b/src/glamor_priv.h
index d04421b..05bb18a 100644
--- a/src/glamor_priv.h
+++ b/src/glamor_priv.h
@@ -31,10 +31,6 @@
 #include "config.h"
 #endif
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#include <xorg-config.h>
-#endif
 #include <xorg-server.h>
 
 #include "glamor.h"
@@ -171,7 +167,7 @@ struct glamor_saved_procs {
 	(((int)(t1) - (int)(t0)) < 0)
 
 typedef struct glamor_screen_private {
-	struct glamor_gl_dispatch dispatch;
+	struct glamor_gl_dispatch _dispatch;
 	int yInverted;
 	unsigned int tick;
 	enum glamor_gl_flavor gl_flavor;
diff --git a/src/glamor_putimage.c b/src/glamor_putimage.c
index 5813822..75e5b4c 100644
--- a/src/glamor_putimage.c
+++ b/src/glamor_putimage.c
@@ -251,7 +251,7 @@ _glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
 {
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(drawable->pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
 	glamor_pixmap_private *pixmap_priv =
 	    glamor_get_pixmap_private(pixmap);
@@ -265,10 +265,8 @@ _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;
@@ -287,7 +285,6 @@ _glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
 	if (!glamor_set_planemask(pixmap, gc->planemask)) {
 		goto fail;
 	}
-	glamor_set_alu(dispatch, gc->alu);
 
 	if (glamor_get_tex_format_type_from_pixmap(pixmap,
 						   &format,
@@ -298,6 +295,8 @@ _glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
 	}
 
 	/* XXX consider to reuse a function to do the following work. */
+	dispatch = glamor_get_dispatch(glamor_priv);
+	glamor_set_alu(dispatch, gc->alu);
 	glamor_set_destination_pixmap_priv_nc(pixmap_priv);
 	glamor_validate_pixmap(pixmap);
 	dispatch->glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
@@ -400,6 +399,7 @@ _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);
+	glamor_put_dispatch(glamor_priv);
 
 	ret = TRUE;
 	goto done;
@@ -421,7 +421,6 @@ fail:
 	ret = TRUE;
 
 done:
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_render.c b/src/glamor_render.c
index b240eac..89e8265 100644
--- a/src/glamor_render.c
+++ b/src/glamor_render.c
@@ -220,15 +220,16 @@ glamor_create_composite_shader(ScreenPtr screen, struct shader_key *key,
 	GLuint vs, fs, prog;
 	GLint source_sampler_uniform_location,
 	    mask_sampler_uniform_location;
-	glamor_screen_private *glamor = glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor->dispatch;
+	glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
+	glamor_gl_dispatch *dispatch;
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	vs = glamor_create_composite_vs(dispatch, key);
 	if (vs == 0)
-		return;
+		goto out;
 	fs = glamor_create_composite_fs(dispatch, key);
 	if (fs == 0)
-		return;
+		goto out;
 
 	prog = dispatch->glCreateProgram();
 	dispatch->glAttachShader(prog, vs);
@@ -268,6 +269,9 @@ glamor_create_composite_shader(ScreenPtr screen, struct shader_key *key,
 			    (mask_sampler_uniform_location, 1);
 		}
 	}
+
+out:
+	glamor_put_dispatch(glamor_priv);
 }
 
 static glamor_composite_shader *
@@ -316,7 +320,7 @@ glamor_init_composite_shaders(ScreenPtr screen)
 	int vb_size;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch = &glamor_priv->dispatch;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glGenBuffers(1, &glamor_priv->vbo);
 	dispatch->glGenBuffers(1, &glamor_priv->ebo);
 	dispatch->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glamor_priv->ebo);
@@ -331,17 +335,13 @@ glamor_init_composite_shaders(ScreenPtr screen)
 	}
 	else {
 		vb = malloc(GLAMOR_COMPOSITE_VBO_VERT_CNT * sizeof(float) * 2);
-		if (vb == NULL) {
-			ErrorF("Failed to allocate vb memory.\n");
-			exit(1);
-		}
+		if (vb == NULL)
+			FatalError("Failed to allocate vb memory.\n");
 		eb = malloc(eb_size);
 	}
 
-	if (eb == NULL) {
-		ErrorF("fatal error, fail to get eb.\n");
-		exit(1);
-	}
+	if (eb == NULL)
+		FatalError("fatal error, fail to get eb.\n");
 	glamor_init_eb(eb, GLAMOR_COMPOSITE_VBO_VERT_CNT);
 
 	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
@@ -362,6 +362,8 @@ glamor_init_composite_shaders(ScreenPtr screen)
 		free(eb);
 		glamor_priv->vb = (char*)vb;
 	}
+
+	glamor_put_dispatch(glamor_priv);
 }
 
 void
@@ -373,7 +375,7 @@ glamor_fini_composite_shaders(ScreenPtr screen)
 	int i,j,k;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch = &glamor_priv->dispatch;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glDeleteBuffers(1, &glamor_priv->vbo);
 	dispatch->glDeleteBuffers(1, &glamor_priv->ebo);
 
@@ -388,8 +390,9 @@ glamor_fini_composite_shaders(ScreenPtr screen)
 	if (glamor_priv->gl_flavor != GLAMOR_GL_DESKTOP
 	    && glamor_priv->vb)
 		free(glamor_priv->vb);
-}
 
+	glamor_put_dispatch(glamor_priv);
+}
 
 static Bool
 glamor_set_composite_op(ScreenPtr screen,
@@ -399,7 +402,7 @@ glamor_set_composite_op(ScreenPtr screen,
 	struct blendinfo *op_info;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 
 	if (op >= ARRAY_SIZE(composite_op_info)) {
 		glamor_fallback("unsupported render op %d \n", op);
@@ -435,12 +438,14 @@ glamor_set_composite_op(ScreenPtr screen,
 			dest_blend = GL_ONE_MINUS_SRC_COLOR;
 	}
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	if (source_blend == GL_ONE && dest_blend == GL_ZERO) {
 		dispatch->glDisable(GL_BLEND);
 	} else {
 		dispatch->glEnable(GL_BLEND);
 		dispatch->glBlendFunc(source_blend, dest_blend);
 	}
+	glamor_put_dispatch(glamor_priv);
 	return TRUE;
 }
 
@@ -451,7 +456,6 @@ glamor_composite_texture_fixup(ScreenPtr screen,
 {
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
 	Bool has_repeat;
 	int width, height;
 
@@ -478,7 +482,9 @@ glamor_set_composite_texture(ScreenPtr screen, int unit,
 {
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
+
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glActiveTexture(GL_TEXTURE0 + unit);
 	dispatch->glBindTexture(GL_TEXTURE_2D, pixmap_priv->fbo->tex);
 	switch (picture->repeatType) {
@@ -533,6 +539,7 @@ glamor_set_composite_texture(ScreenPtr screen, int unit,
 #ifndef GLAMOR_GLES2
 	dispatch->glEnable(GL_TEXTURE_2D);
 #endif
+	glamor_put_dispatch(glamor_priv);
 }
 
 static void
@@ -654,7 +661,7 @@ glamor_setup_composite_vbo(ScreenPtr screen, int n_verts)
 {
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 
 	glamor_priv->vbo_offset = 0;
 	glamor_priv->vbo_offset = 0;
@@ -667,6 +674,7 @@ glamor_setup_composite_vbo(ScreenPtr screen, int n_verts)
 	if (glamor_priv->has_mask_coords)
 		glamor_priv->vb_stride += 2 * sizeof(float);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glBindBuffer(GL_ARRAY_BUFFER, glamor_priv->vbo);
 	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
 		dispatch->glBufferData(GL_ARRAY_BUFFER,
@@ -706,6 +714,7 @@ glamor_setup_composite_vbo(ScreenPtr screen, int n_verts)
 							  sizeof(float)));
 		dispatch->glEnableVertexAttribArray(GLAMOR_VERTEX_MASK);
 	}
+	glamor_put_dispatch(glamor_priv);
 }
 
 static void
@@ -739,9 +748,12 @@ glamor_flush_composite_rects(ScreenPtr screen)
 {
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
+
 	if (!glamor_priv->render_nr_verts)
 		return;
+
+	dispatch = glamor_get_dispatch(glamor_priv);
 	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP)
 		dispatch->glUnmapBuffer(GL_ARRAY_BUFFER);
 	else {
@@ -754,6 +766,7 @@ glamor_flush_composite_rects(ScreenPtr screen)
 
 	dispatch->glDrawElements(GL_TRIANGLES, (glamor_priv->render_nr_verts * 3) / 2,
 				 GL_UNSIGNED_SHORT, NULL);
+	glamor_put_dispatch(glamor_priv);
 }
 
 static void
@@ -846,7 +859,7 @@ glamor_composite_with_shader(CARD8 op,
 	ScreenPtr screen = dest->pDrawable->pScreen;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	PixmapPtr dest_pixmap =
 	    glamor_get_drawable_pixmap(dest->pDrawable);
 	PixmapPtr source_pixmap = NULL, mask_pixmap = NULL;
@@ -870,10 +883,8 @@ 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;
@@ -1090,6 +1101,7 @@ glamor_composite_with_shader(CARD8 op,
 		goto fail;
 	}
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glUseProgram(shader->prog);
 
 	if (key.source == SHADER_SOURCE_SOLID) {
@@ -1233,6 +1245,7 @@ glamor_composite_with_shader(CARD8 op,
 	dispatch->glUseProgram(0);
 	if (saved_source_format)
 		source->format = saved_source_format;
+	glamor_put_dispatch(glamor_priv);
 
 	ret = TRUE;
 	goto done;
@@ -1240,12 +1253,7 @@ glamor_composite_with_shader(CARD8 op,
 fail:
 	if (saved_source_format)
 		source->format = saved_source_format;
-
-	dispatch->glDisable(GL_BLEND);
-	dispatch->glUseProgram(0);
-
 done:
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
@@ -1320,7 +1328,6 @@ _glamor_composite(CARD8 op,
 	RegionRec region;
 	BoxPtr box;
 	int nbox, i, ok;
-	GLAMOR_DEFINE_CONTEXT;
 
 	x_temp_src = x_source;
 	y_temp_src = y_source;
@@ -1328,7 +1335,6 @@ _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;
@@ -1535,7 +1541,6 @@ fail:
 		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 07c94ab..a947169 100644
--- a/src/glamor_setspans.c
+++ b/src/glamor_setspans.c
@@ -25,10 +25,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 static Bool
@@ -40,17 +36,15 @@ _glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
 	glamor_pixmap_private *dest_pixmap_priv;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(drawable->pScreen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	GLenum format, type;
 	int no_alpha, no_revert, i;
 	uint8_t *drawpixels_src = (uint8_t *) 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");
@@ -70,17 +64,15 @@ _glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
 		goto fail;
 	}
 
-
 	glamor_set_destination_pixmap_priv_nc(dest_pixmap_priv);
 	glamor_validate_pixmap(dest_pixmap);
 	if (!glamor_set_planemask(dest_pixmap, gc->planemask))
 		goto fail;
-	glamor_set_alu(dispatch, gc->alu);
-	if (!glamor_set_planemask(dest_pixmap, gc->planemask))
-		goto fail;
 
 	glamor_get_drawable_deltas(drawable, dest_pixmap, &x_off, &y_off);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
+	glamor_set_alu(dispatch, gc->alu);
 	for (i = 0; i < n; i++) {
 
 		n = REGION_NUM_RECTS(clip);
@@ -103,6 +95,7 @@ _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);
+	glamor_put_dispatch(glamor_priv);
 	ret = TRUE;
 	goto done;
 
@@ -120,7 +113,6 @@ fail:
 	ret = TRUE;
 
 done:
-	GLAMOR_RESTORE_CONTEXT(glamor_priv);
 	return ret;
 }
 
diff --git a/src/glamor_tile.c b/src/glamor_tile.c
index 3087bea..917078f 100644
--- a/src/glamor_tile.c
+++ b/src/glamor_tile.c
@@ -25,10 +25,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_tile.c
@@ -60,7 +56,7 @@ glamor_init_tile_shader(ScreenPtr screen)
 	GLint sampler_uniform_location;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch =  &glamor_priv->dispatch;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	glamor_priv->tile_prog = dispatch->glCreateProgram();
 	vs_prog = glamor_compile_glsl_prog(dispatch, GL_VERTEX_SHADER, tile_vs);
 	fs_prog = glamor_compile_glsl_prog(dispatch, GL_FRAGMENT_SHADER,
@@ -81,6 +77,7 @@ glamor_init_tile_shader(ScreenPtr screen)
 	dispatch->glUseProgram(glamor_priv->tile_prog);
 	dispatch->glUniform1i(sampler_uniform_location, 0);
 	dispatch->glUseProgram(0);
+	glamor_put_dispatch(glamor_priv);
 }
 
 void
@@ -90,8 +87,9 @@ glamor_fini_tile_shader(ScreenPtr screen)
 	glamor_gl_dispatch *dispatch;
 
 	glamor_priv = glamor_get_screen_private(screen);
-	dispatch =  &glamor_priv->dispatch;
+	dispatch = glamor_get_dispatch(glamor_priv);
 	dispatch->glDeleteProgram(glamor_priv->tile_prog);
+	glamor_put_dispatch(glamor_priv);
 }
 
 Bool
@@ -103,7 +101,7 @@ glamor_tile(PixmapPtr pixmap, PixmapPtr tile,
 	ScreenPtr screen = pixmap->drawable.pScreen;
 	glamor_screen_private *glamor_priv =
 	    glamor_get_screen_private(screen);
-	glamor_gl_dispatch *dispatch = &glamor_priv->dispatch;
+	glamor_gl_dispatch *dispatch;
 	int x1 = x;
 	int x2 = x + width;
 	int y1 = y;
@@ -161,6 +159,7 @@ glamor_tile(PixmapPtr pixmap, PixmapPtr tile,
 	glamor_validate_pixmap(pixmap);
 	pixmap_priv_get_scale(dst_pixmap_priv, &dst_xscale, &dst_yscale);
 
+	dispatch = glamor_get_dispatch(glamor_priv);
 	glamor_set_alu(dispatch, alu);
 
 	if (GLAMOR_PIXMAP_PRIV_NO_PENDING(src_pixmap_priv)) {
@@ -219,6 +218,7 @@ glamor_tile(PixmapPtr pixmap, PixmapPtr tile,
 	dispatch->glUseProgram(0);
 	glamor_set_alu(dispatch, GXcopy);
 	glamor_set_planemask(pixmap, ~0);
+	glamor_put_dispatch(glamor_priv);
 	return TRUE;
 
       fail:
diff --git a/src/glamor_triangles.c b/src/glamor_triangles.c
index 208ac77..e0f4a97 100644
--- a/src/glamor_triangles.c
+++ b/src/glamor_triangles.c
@@ -26,10 +26,6 @@
  *
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 static Bool
diff --git a/src/glamor_utils.h b/src/glamor_utils.h
index a66e248..236ffd9 100644
--- a/src/glamor_utils.h
+++ b/src/glamor_utils.h
@@ -664,31 +664,46 @@ 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)
+static inline void glamor_make_current(ScreenPtr screen)
 {
-	return glamor_egl_make_current(screen);
+	glamor_egl_make_current(screen);
 }
 
-static inline void glamor_restore_current(ScreenPtr screen, void *previous_context)
+static inline void glamor_restore_current(ScreenPtr screen)
 {
-	glamor_egl_restore_context(screen, previous_context);
+	glamor_egl_restore_context(screen);
 }
 
 #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_)
+static inline glamor_gl_dispatch *
+glamor_get_dispatch(glamor_screen_private *glamor_priv)
+{
+	if (glamor_priv->flags & GLAMOR_USE_EGL_SCREEN)
+		glamor_make_current(glamor_priv->screen);
+
+	return &glamor_priv->_dispatch;
+}
+
+static inline void
+glamor_put_dispatch(glamor_screen_private *glamor_priv)
+{
+	if (glamor_priv->flags & GLAMOR_USE_EGL_SCREEN)
+		glamor_restore_current(glamor_priv->screen);
+}
+
 #else
 
-#define GLAMOR_DEFINE_CONTEXT
-#define GLAMOR_SET_CONTEXT(glamor_priv)
-#define GLAMOR_RESTORE_CONTEXT(glamor_priv)
+#error No!
+static inline glamor_gl_dispatch *
+glamor_get_dispatch(glamor_screen_private *glamor_priv)
+{
+	return &glamor_priv->_dispatch;
+}
+
+static inline void
+glamor_put_dispatch(glamor_screen_private *glamor_priv)
+{
+}
 
 #endif
 
diff --git a/src/glamor_window.c b/src/glamor_window.c
index f6e4cd1..3da11e4 100644
--- a/src/glamor_window.c
+++ b/src/glamor_window.c
@@ -21,10 +21,6 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
 #include "glamor_priv.h"
 
 /** @file glamor_window.c
-- 
1.7.9



More information about the Glamor mailing list