5d4276c
diff --git a/exa/exa.c b/exa/exa.c
5d4276c
index e9f42df..8e22b89 100644
5d4276c
--- a/exa/exa.c
5d4276c
+++ b/exa/exa.c
5d4276c
@@ -126,7 +126,7 @@ exaGetDrawablePixmap(DrawablePtr pDrawable)
5d4276c
  * the backing drawable. These coordinates are nonzero only for redirected
5d4276c
  * windows.
5d4276c
  */
5d4276c
-static void
5d4276c
+void
5d4276c
 exaGetDrawableDeltas (DrawablePtr pDrawable, PixmapPtr pPixmap,
5d4276c
 		      int *xp, int *yp)
5d4276c
 {
5d4276c
@@ -172,29 +172,6 @@ exaPixmapDirty (PixmapPtr pPix, int x1, int y1, int x2, int y2)
5d4276c
     REGION_UNINIT(pScreen, &region);
5d4276c
 }
5d4276c
 
5d4276c
-/**
5d4276c
- * exaDrawableDirty() marks a pixmap backing a drawable as dirty, allowing for
5d4276c
- * optimizations in pixmap migration when no changes have occurred.
5d4276c
- */
5d4276c
-void
5d4276c
-exaDrawableDirty (DrawablePtr pDrawable, int x1, int y1, int x2, int y2)
5d4276c
-{
5d4276c
-    PixmapPtr pPix = exaGetDrawablePixmap(pDrawable);
5d4276c
-    int xoff, yoff;
5d4276c
-
5d4276c
-    x1 = max(x1, pDrawable->x);
5d4276c
-    y1 = max(y1, pDrawable->y);
5d4276c
-    x2 = min(x2, pDrawable->x + pDrawable->width);
5d4276c
-    y2 = min(y2, pDrawable->y + pDrawable->height);
5d4276c
-
5d4276c
-    if (x1 >= x2 || y1 >= y2)
5d4276c
-	return;
5d4276c
-
5d4276c
-    exaGetDrawableDeltas(pDrawable, pPix, &xoff, &yoff);
5d4276c
-
5d4276c
-    exaPixmapDirty(pPix, x1 + xoff, y1 + yoff, x2 + xoff, y2 + yoff);
5d4276c
-}
5d4276c
-
5d4276c
 static Bool
5d4276c
 exaDestroyPixmap (PixmapPtr pPixmap)
5d4276c
 {
5d4276c
@@ -322,6 +299,9 @@ exaPixmapIsOffscreen(PixmapPtr p)
5d4276c
     if (p->devPrivate.ptr == NULL)
5d4276c
 	return TRUE;
5d4276c
 
5d4276c
+    if (pExaScr->info->PixmapIsOffscreen)
5d4276c
+	return pExaScr->info->PixmapIsOffscreen(p);
5d4276c
+
5d4276c
     return ((unsigned long) ((CARD8 *) p->devPrivate.ptr -
5d4276c
 			     (CARD8 *) pExaScr->info->memoryBase) <
5d4276c
 	    pExaScr->info->memorySize);
5d4276c
@@ -589,6 +569,45 @@ exaDriverInit (ScreenPtr		pScreen,
5d4276c
     PictureScreenPtr ps;
5d4276c
 #endif
5d4276c
 
5d4276c
+    if (!pScreenInfo)
5d4276c
+	return FALSE;
5d4276c
+
5d4276c
+    if (!pScreenInfo->memoryBase) {
5d4276c
+	LogMessage(X_ERROR, "EXA(%d): ExaDriverRec::memoryBase must be "
5d4276c
+		   "non-zero\n", pScreen->myNum);
5d4276c
+	return FALSE;
5d4276c
+    }
5d4276c
+
5d4276c
+    if (!pScreenInfo->memorySize) {
5d4276c
+	LogMessage(X_ERROR, "EXA(%d): ExaDriverRec::memorySize must be "
5d4276c
+		   "non-zero\n", pScreen->myNum);
5d4276c
+	return FALSE;
5d4276c
+    }
5d4276c
+
5d4276c
+    if (pScreenInfo->offScreenBase > pScreenInfo->memorySize) {
5d4276c
+	LogMessage(X_ERROR, "EXA(%d): ExaDriverRec::offScreenBase must be <= "
5d4276c
+		   "ExaDriverRec::memorySize\n", pScreen->myNum);
5d4276c
+	return FALSE;
5d4276c
+    }
5d4276c
+
5d4276c
+    if (!pScreenInfo->PrepareSolid) {
5d4276c
+	LogMessage(X_ERROR, "EXA(%d): ExaDriverRec::PrepareSolid must be "
5d4276c
+		   "non-NULL\n", pScreen->myNum);
5d4276c
+	return FALSE;
5d4276c
+    }
5d4276c
+
5d4276c
+    if (!pScreenInfo->PrepareCopy) {
5d4276c
+	LogMessage(X_ERROR, "EXA(%d): ExaDriverRec::PrepareCopy must be "
5d4276c
+		   "non-NULL\n", pScreen->myNum);
5d4276c
+	return FALSE;
5d4276c
+    }
5d4276c
+
5d4276c
+    if (!pScreenInfo->WaitMarker) {
5d4276c
+	LogMessage(X_ERROR, "EXA(%d): ExaDriverRec::WaitMarker must be "
5d4276c
+		   "non-NULL\n", pScreen->myNum);
5d4276c
+	return FALSE;
5d4276c
+    }
5d4276c
+
5d4276c
     if (pScreenInfo->exa_major != EXA_VERSION_MAJOR ||
5d4276c
 	pScreenInfo->exa_minor > EXA_VERSION_MINOR)
5d4276c
     {
5d4276c
@@ -668,10 +687,6 @@ exaDriverInit (ScreenPtr		pScreen,
5d4276c
     }
5d4276c
 #endif
5d4276c
 
5d4276c
-#ifdef COMPOSITE
5d4276c
-    miDisableCompositeWrapper(pScreen);
5d4276c
-#endif
5d4276c
-
5d4276c
 #ifdef MITSHM
5d4276c
     /* Re-register with the MI funcs, which don't allow shared pixmaps.
5d4276c
      * Shared pixmaps are almost always a performance loss for us, but this
5d4276c
diff --git a/exa/exa.h b/exa/exa.h
5d4276c
index bf723f7..9ea5933 100644
5d4276c
--- a/exa/exa.h
5d4276c
+++ b/exa/exa.h
5d4276c
@@ -39,7 +39,7 @@
5d4276c
 #include "fb.h"
5d4276c
 
5d4276c
 #define EXA_VERSION_MAJOR   2
5d4276c
-#define EXA_VERSION_MINOR   1
5d4276c
+#define EXA_VERSION_MINOR   2
5d4276c
 #define EXA_VERSION_RELEASE 0
5d4276c
 
5d4276c
 typedef struct _ExaOffscreenArea ExaOffscreenArea;
5d4276c
@@ -229,7 +229,7 @@ typedef struct _ExaDriver {
5d4276c
      * @{
5d4276c
      */
5d4276c
     /**
5d4276c
-     * PrepareCopy() sets up the driver for doing a copy within offscreen
5d4276c
+     * PrepareCopy() sets up the driver for doing a copy within video 
5d4276c
      * memory.
5d4276c
      *
5d4276c
      * @param pSrcPixmap source pixmap
5d4276c
@@ -636,6 +636,23 @@ typedef struct _ExaDriver {
5d4276c
      */
5d4276c
     void	(*FinishAccess)(PixmapPtr pPix, int index);
5d4276c
 
5d4276c
+    /**
5d4276c
+     * PixmapIsOffscreen() is an optional driver replacement to
5d4276c
+     * exaPixmapIsOffscreen(). Set to NULL if you want the standard behaviour
5d4276c
+     * of exaPixmapIsOffscreen().
5d4276c
+     *
5d4276c
+     * @param pPix the pixmap
5d4276c
+     * @return TRUE if the given drawable is in framebuffer memory.
5d4276c
+     *
5d4276c
+     * exaPixmapIsOffscreen() is used to determine if a pixmap is in offscreen
5d4276c
+     * memory, meaning that acceleration could probably be done to it, and that it
5d4276c
+     * will need to be wrapped by PrepareAccess()/FinishAccess() when accessing it
5d4276c
+     * with the CPU.
5d4276c
+     *
5d4276c
+     *
5d4276c
+     */
5d4276c
+    Bool	(*PixmapIsOffscreen)(PixmapPtr pPix);
5d4276c
+
5d4276c
 	/** @name PrepareAccess() and FinishAccess() indices
5d4276c
 	 * @{
5d4276c
 	 */
5d4276c
@@ -704,6 +721,9 @@ exaOffscreenAlloc(ScreenPtr pScreen, int size, int align,
5d4276c
 ExaOffscreenArea *
5d4276c
 exaOffscreenFree(ScreenPtr pScreen, ExaOffscreenArea *area);
5d4276c
 
5d4276c
+void
5d4276c
+ExaOffscreenMarkUsed (PixmapPtr pPixmap);
5d4276c
+
5d4276c
 unsigned long
5d4276c
 exaGetPixmapOffset(PixmapPtr pPix);
5d4276c
 
5d4276c
diff --git a/exa/exa_accel.c b/exa/exa_accel.c
5d4276c
index e633d80..cc383cc 100644
5d4276c
--- a/exa/exa_accel.c
5d4276c
+++ b/exa/exa_accel.c
5d4276c
@@ -74,6 +74,7 @@ exaFillSpans(DrawablePtr pDrawable, GCPtr pGC, int n,
5d4276c
 					 pGC->planemask,
5d4276c
 					 pGC->fgPixel))
5d4276c
     {
5d4276c
+	exaDoMigration (pixmaps, 1, FALSE);
5d4276c
 	ExaCheckFillSpans (pDrawable, pGC, n, ppt, pwidth, fSorted);
5d4276c
 	return;
5d4276c
     }
5d4276c
@@ -109,8 +110,6 @@ exaFillSpans(DrawablePtr pDrawable, GCPtr pGC, int n,
5d4276c
 	    (*pExaScr->info->Solid) (pPixmap,
5d4276c
 				     fullX1 + off_x, fullY1 + off_y,
5d4276c
 				     fullX2 + off_x, fullY1 + 1 + off_y);
5d4276c
-	    exaPixmapDirty (pPixmap, fullX1 + off_x, fullY1 + off_y,
5d4276c
-			    fullX2 + off_x, fullY1 + 1 + off_y);
5d4276c
 	}
5d4276c
 	else
5d4276c
 	{
5d4276c
@@ -129,8 +128,6 @@ exaFillSpans(DrawablePtr pDrawable, GCPtr pGC, int n,
5d4276c
 			(*pExaScr->info->Solid) (pPixmap,
5d4276c
 						 partX1 + off_x, fullY1 + off_y,
5d4276c
 						 partX2 + off_x, fullY1 + 1 + off_y);
5d4276c
-			exaPixmapDirty (pPixmap, partX1 + off_x, fullY1 + off_y,
5d4276c
-					partX2 + off_x, fullY1 + 1 + off_y);
5d4276c
 		    }
5d4276c
 		}
5d4276c
 		pbox++;
5d4276c
@@ -154,8 +151,9 @@ exaPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
5d4276c
     int xoff, yoff;
5d4276c
     int src_stride, bpp = pDrawable->bitsPerPixel;
5d4276c
 
5d4276c
-    if (pExaScr->swappedOut || pExaScr->info->UploadToScreen == NULL)
5d4276c
-	goto migrate_and_fallback;
5d4276c
+    pixmaps[0].as_dst = TRUE;
5d4276c
+    pixmaps[0].as_src = FALSE;
5d4276c
+    pixmaps[0].pPix = exaGetDrawablePixmap (pDrawable);
5d4276c
 
5d4276c
     /* Don't bother with under 8bpp, XYPixmaps. */
5d4276c
     if (format != ZPixmap || bpp < 8)
5d4276c
@@ -165,10 +163,14 @@ exaPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
5d4276c
     if (!EXA_PM_IS_SOLID(pDrawable, pGC->planemask) || pGC->alu != GXcopy)
5d4276c
 	goto migrate_and_fallback;
5d4276c
 
5d4276c
-    pixmaps[0].as_dst = TRUE;
5d4276c
-    pixmaps[0].as_src = FALSE;
5d4276c
-    pixmaps[0].pPix = exaGetDrawablePixmap (pDrawable);
5d4276c
+    if (pExaScr->swappedOut)
5d4276c
+	goto fallback;
5d4276c
+
5d4276c
     exaDoMigration (pixmaps, 1, TRUE);
5d4276c
+
5d4276c
+    if (pExaScr->info->UploadToScreen == NULL)
5d4276c
+	goto fallback;
5d4276c
+
5d4276c
     pPix = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff);
5d4276c
 
5d4276c
     if (pPix == NULL)
5d4276c
@@ -221,25 +223,23 @@ exaPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
5d4276c
 
5d4276c
 	    fbBltStip((FbStip *)bits + (y1 - y) * (src_stride / sizeof(FbStip)),
5d4276c
 		      src_stride / sizeof(FbStip),
5d4276c
-		      (x1 - x) * bpp,
5d4276c
-		      dst + (y1 + yoff) * dst_stride,
5d4276c
+		      (x1 - x) * dstBpp,
5d4276c
+		      dst + (y1 + dstYoff) * dst_stride,
5d4276c
 		      dst_stride,
5d4276c
-		      (x1 + xoff) * bpp,
5d4276c
-		      (x2 - x1) * bpp,
5d4276c
+		      (x1 + dstXoff) * dstBpp,
5d4276c
+		      (x2 - x1) * dstBpp,
5d4276c
 		      y2 - y1,
5d4276c
-		      GXcopy, FB_ALLONES, bpp);
5d4276c
+		      GXcopy, FB_ALLONES, dstBpp);
5d4276c
 
5d4276c
 	    exaFinishAccess(pDrawable, EXA_PREPARE_DEST);
5d4276c
 	}
5d4276c
+
5d4276c
 	exaPixmapDirty(pPix, x1 + xoff, y1 + yoff, x2 + xoff, y2 + yoff);
5d4276c
     }
5d4276c
 
5d4276c
     return;
5d4276c
 
5d4276c
 migrate_and_fallback:
5d4276c
-    pixmaps[0].as_dst = TRUE;
5d4276c
-    pixmaps[0].as_src = FALSE;
5d4276c
-    pixmaps[0].pPix = exaGetDrawablePixmap (pDrawable);
5d4276c
     exaDoMigration (pixmaps, 1, FALSE);
5d4276c
 
5d4276c
 fallback:
5d4276c
@@ -387,6 +387,7 @@ exaCopyNtoN (DrawablePtr    pSrcDrawable,
5d4276c
     int	    src_off_x, src_off_y;
5d4276c
     int	    dst_off_x, dst_off_y;
5d4276c
     ExaMigrationRec pixmaps[2];
5d4276c
+    Bool fallback = FALSE;
5d4276c
 
5d4276c
     pixmaps[0].as_dst = TRUE;
5d4276c
     pixmaps[0].as_src = FALSE;
5d4276c
@@ -404,62 +405,64 @@ exaCopyNtoN (DrawablePtr    pSrcDrawable,
5d4276c
 	pDstPixmap->drawable.width > pExaScr->info->maxX ||
5d4276c
 	pDstPixmap->drawable.height > pExaScr->info->maxY)
5d4276c
     {
5d4276c
-	exaDoMigration (pixmaps, 2, FALSE);
5d4276c
-	goto fallback;
5d4276c
+	fallback = TRUE;
5d4276c
     } else {
5d4276c
 	exaDoMigration (pixmaps, 2, TRUE);
5d4276c
     }
5d4276c
 
5d4276c
     /* Mixed directions must be handled specially if the card is lame */
5d4276c
-    if (pExaScr->info->flags & EXA_TWO_BITBLT_DIRECTIONS &&
5d4276c
+    if (!fallback && (pExaScr->info->flags & EXA_TWO_BITBLT_DIRECTIONS) &&
5d4276c
 	reverse != upsidedown) {
5d4276c
-	if (!exaCopyNtoNTwoDir(pSrcDrawable, pDstDrawable, pGC, pbox, nbox,
5d4276c
+	if (exaCopyNtoNTwoDir(pSrcDrawable, pDstDrawable, pGC, pbox, nbox,
5d4276c
 			       dx, dy))
5d4276c
-	    goto fallback;
5d4276c
-	return;
5d4276c
+	    return;
5d4276c
+	fallback = TRUE;
5d4276c
+    }
5d4276c
+
5d4276c
+    pSrcPixmap = exaGetDrawablePixmap (pSrcDrawable);
5d4276c
+    pDstPixmap = exaGetDrawablePixmap (pDstDrawable);
5d4276c
+
5d4276c
+    exaGetDrawableDeltas (pSrcDrawable, pSrcPixmap, &src_off_x, &src_off_y);
5d4276c
+    exaGetDrawableDeltas (pDstDrawable, pDstPixmap, &dst_off_x, &dst_off_y);
5d4276c
+
5d4276c
+    if (fallback || !exaPixmapIsOffscreen(pSrcPixmap) ||
5d4276c
+	!exaPixmapIsOffscreen(pDstPixmap) ||
5d4276c
+	!(*pExaScr->info->PrepareCopy) (pSrcPixmap, pDstPixmap, reverse ? -1 : 1,
5d4276c
+					upsidedown ? -1 : 1,
5d4276c
+					pGC ? pGC->alu : GXcopy,
5d4276c
+					pGC ? pGC->planemask : FB_ALLONES)) {
5d4276c
+	fallback = TRUE;
5d4276c
+	EXA_FALLBACK(("from %p to %p (%c,%c)\n", pSrcDrawable, pDstDrawable,
5d4276c
+		      exaDrawableLocation(pSrcDrawable),
5d4276c
+		      exaDrawableLocation(pDstDrawable)));
5d4276c
+	exaDoMigration (pixmaps, 2, FALSE);
5d4276c
+	exaPrepareAccess (pDstDrawable, EXA_PREPARE_DEST);
5d4276c
+	exaPrepareAccess (pSrcDrawable, EXA_PREPARE_SRC);
5d4276c
+	fbCopyNtoN (pSrcDrawable, pDstDrawable, pGC,
5d4276c
+		    pbox, nbox, dx, dy, reverse, upsidedown,
5d4276c
+		    bitplane, closure);
5d4276c
+	exaFinishAccess (pSrcDrawable, EXA_PREPARE_SRC);
5d4276c
+	exaFinishAccess (pDstDrawable, EXA_PREPARE_DEST);
5d4276c
     }
5d4276c
 
5d4276c
-    if ((pSrcPixmap = exaGetOffscreenPixmap (pSrcDrawable, &src_off_x, &src_off_y)) &&
5d4276c
-	(pDstPixmap = exaGetOffscreenPixmap (pDstDrawable, &dst_off_x, &dst_off_y)) &&
5d4276c
-	(*pExaScr->info->PrepareCopy) (pSrcPixmap, pDstPixmap,
5d4276c
-				       reverse ? -1 : 1, upsidedown ? -1 : 1,
5d4276c
-				       pGC ? pGC->alu : GXcopy,
5d4276c
-				       pGC ? pGC->planemask : FB_ALLONES))
5d4276c
+    while (nbox--)
5d4276c
     {
5d4276c
-	while (nbox--)
5d4276c
-	{
5d4276c
+	if (!fallback)
5d4276c
 	    (*pExaScr->info->Copy) (pDstPixmap,
5d4276c
 				    pbox->x1 + dx + src_off_x,
5d4276c
 				    pbox->y1 + dy + src_off_y,
5d4276c
 				    pbox->x1 + dst_off_x, pbox->y1 + dst_off_y,
5d4276c
-				    pbox->x2 - pbox->x1,
5d4276c
-				    pbox->y2 - pbox->y1);
5d4276c
-	    exaPixmapDirty (pDstPixmap,
5d4276c
-			    pbox->x1 + dst_off_x, pbox->y1 + dst_off_y,
5d4276c
-			    pbox->x2 + dst_off_x, pbox->y2 + dst_off_y);
5d4276c
-	    pbox++;
5d4276c
-	}
5d4276c
-	(*pExaScr->info->DoneCopy) (pDstPixmap);
5d4276c
-	exaMarkSync(pDstDrawable->pScreen);
5d4276c
-	return;
5d4276c
-    }
5d4276c
-
5d4276c
-fallback:
5d4276c
-    EXA_FALLBACK(("from %p to %p (%c,%c)\n", pSrcDrawable, pDstDrawable,
5d4276c
-		  exaDrawableLocation(pSrcDrawable),
5d4276c
-		  exaDrawableLocation(pDstDrawable)));
5d4276c
-    exaPrepareAccess (pDstDrawable, EXA_PREPARE_DEST);
5d4276c
-    exaPrepareAccess (pSrcDrawable, EXA_PREPARE_SRC);
5d4276c
-    fbCopyNtoN (pSrcDrawable, pDstDrawable, pGC,
5d4276c
-		pbox, nbox, dx, dy, reverse, upsidedown,
5d4276c
-		bitplane, closure);
5d4276c
-    exaFinishAccess (pSrcDrawable, EXA_PREPARE_SRC);
5d4276c
-    exaFinishAccess (pDstDrawable, EXA_PREPARE_DEST);
5d4276c
-    while (nbox--)
5d4276c
-    {
5d4276c
-	exaDrawableDirty (pDstDrawable, pbox->x1, pbox->y1, pbox->x2, pbox->y2);
5d4276c
+				    pbox->x2 - pbox->x1, pbox->y2 - pbox->y1);
5d4276c
+	exaPixmapDirty (pDstPixmap, pbox->x1 + dst_off_x, pbox->y1 + dst_off_y,
5d4276c
+			pbox->x2  + dst_off_x, pbox->y2 + dst_off_y);
5d4276c
 	pbox++;
5d4276c
     }
5d4276c
+
5d4276c
+    if (fallback)
5d4276c
+	return;
5d4276c
+
5d4276c
+    (*pExaScr->info->DoneCopy) (pDstPixmap);
5d4276c
+    exaMarkSync (pDstDrawable->pScreen);
5d4276c
 }
5d4276c
 
5d4276c
 RegionPtr
5d4276c
@@ -618,6 +621,9 @@ exaPolySegment (DrawablePtr pDrawable, GCPtr pGC, int nseg,
5d4276c
     DEALLOCATE_LOCAL(prect);
5d4276c
 }
5d4276c
 
5d4276c
+static Bool exaFillRegionSolid (DrawablePtr pDrawable, RegionPtr pRegion,
5d4276c
+				Pixel pixel, CARD32 planemask, CARD32 alu);
5d4276c
+
5d4276c
 static void
5d4276c
 exaPolyFillRect(DrawablePtr pDrawable,
5d4276c
 		GCPtr	    pGC,
5d4276c
@@ -626,7 +632,7 @@ exaPolyFillRect(DrawablePtr pDrawable,
5d4276c
 {
5d4276c
     ExaScreenPriv (pDrawable->pScreen);
5d4276c
     RegionPtr	    pClip = fbGetCompositeClip(pGC);
5d4276c
-    PixmapPtr	    pPixmap;
5d4276c
+    PixmapPtr	    pPixmap = exaGetDrawablePixmap(pDrawable);
5d4276c
     register BoxPtr pbox;
5d4276c
     BoxPtr	    pextent;
5d4276c
     int		    extentX1, extentX2, extentY1, extentY2;
5d4276c
@@ -635,40 +641,73 @@ exaPolyFillRect(DrawablePtr pDrawable,
5d4276c
     int		    xoff, yoff;
5d4276c
     int		    xorg, yorg;
5d4276c
     int		    n;
5d4276c
-    ExaMigrationRec pixmaps[1];
5d4276c
+    ExaMigrationRec pixmaps[2];
5d4276c
+    RegionPtr pReg = RECTS_TO_REGION(pScreen, nrect, prect, CT_UNSORTED);
5d4276c
+
5d4276c
+    /* Compute intersection of rects and clip region */
5d4276c
+    REGION_TRANSLATE(pScreen, pReg, pDrawable->x, pDrawable->y);
5d4276c
+    REGION_INTERSECT(pScreen, pReg, pClip, pReg);
5d4276c
+
5d4276c
+    if (!REGION_NUM_RECTS(pReg)) {
5d4276c
+	goto out;
5d4276c
+    }
5d4276c
 
5d4276c
     pixmaps[0].as_dst = TRUE;
5d4276c
     pixmaps[0].as_src = FALSE;
5d4276c
-    pixmaps[0].pPix = pPixmap = exaGetDrawablePixmap (pDrawable);
5d4276c
+    pixmaps[0].pPix = pPixmap;
5d4276c
  
5d4276c
+    exaGetDrawableDeltas(pDrawable, pPixmap, &xoff, &yoff);
5d4276c
+
5d4276c
     if (pExaScr->swappedOut ||
5d4276c
-	pGC->fillStyle != FillSolid ||
5d4276c
 	pPixmap->drawable.width > pExaScr->info->maxX ||
5d4276c
 	pPixmap->drawable.height > pExaScr->info->maxY)
5d4276c
     {
5d4276c
-	exaDoMigration (pixmaps, 1, FALSE);
5d4276c
-	ExaCheckPolyFillRect (pDrawable, pGC, nrect, prect);
5d4276c
-	while (nrect-- >= 0) {
5d4276c
-	    exaDrawableDirty(pDrawable,
5d4276c
-			     pDrawable->x + prect->x,
5d4276c
-			     pDrawable->y + prect->y,
5d4276c
-			     pDrawable->x + prect->x + prect->width,
5d4276c
-			     pDrawable->y + prect->y + prect->height);
5d4276c
-	    prect++;
5d4276c
+	goto fallback;
5d4276c
+    }
5d4276c
+
5d4276c
+    /* For ROPs where overlaps don't matter, convert rectangles to region and
5d4276c
+     * call exaFillRegion{Solid,Tiled}.
5d4276c
+     */
5d4276c
+    if ((pGC->fillStyle == FillSolid || pGC->fillStyle == FillTiled) &&
5d4276c
+	(pGC->alu == GXcopy || pGC->alu == GXclear || pGC->alu == GXnoop ||
5d4276c
+	 pGC->alu == GXcopyInverted || pGC->alu == GXset)) {
5d4276c
+	if (((pGC->fillStyle == FillSolid || pGC->tileIsPixel) &&
5d4276c
+	     exaFillRegionSolid(pDrawable, pReg, pGC->fillStyle == FillSolid ?
5d4276c
+				pGC->fgPixel : pGC->tile.pixel,	pGC->planemask,
5d4276c
+				pGC->alu)) ||
5d4276c
+	    (pGC->fillStyle == FillTiled && !pGC->tileIsPixel &&
5d4276c
+	     exaFillRegionTiled(pDrawable, pReg, pGC->tile.pixmap, &pGC->patOrg,
5d4276c
+				pGC->planemask, pGC->alu))) {
5d4276c
+	    goto out;
5d4276c
 	}
5d4276c
-	return;
5d4276c
-    } else {
5d4276c
-	exaDoMigration (pixmaps, 1, TRUE);
5d4276c
     }
5d4276c
 
5d4276c
-    if (!(pPixmap = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff)) ||
5d4276c
+    if (pGC->fillStyle != FillSolid &&
5d4276c
+	!(pGC->tileIsPixel && pGC->fillStyle == FillTiled))
5d4276c
+    {
5d4276c
+	goto fallback;
5d4276c
+    }
5d4276c
+
5d4276c
+    exaDoMigration (pixmaps, 1, TRUE);
5d4276c
+
5d4276c
+    if (!exaPixmapIsOffscreen (pPixmap) ||
5d4276c
 	!(*pExaScr->info->PrepareSolid) (pPixmap,
5d4276c
 					 pGC->alu,
5d4276c
 					 pGC->planemask,
5d4276c
 					 pGC->fgPixel))
5d4276c
     {
5d4276c
+fallback:
5d4276c
+	if (pGC->fillStyle == FillTiled && !pGC->tileIsPixel) {
5d4276c
+	    pixmaps[1].as_dst = FALSE;
5d4276c
+	    pixmaps[1].as_src = TRUE;
5d4276c
+	    pixmaps[1].pPix = pGC->tile.pixmap;
5d4276c
+	    exaDoMigration (pixmaps, 2, FALSE);
5d4276c
+	} else {
5d4276c
+	    exaDoMigration (pixmaps, 1, FALSE);
5d4276c
+	}
5d4276c
+
5d4276c
 	ExaCheckPolyFillRect (pDrawable, pGC, nrect, prect);
5d4276c
-	return;
5d4276c
+	goto out;
5d4276c
     }
5d4276c
 
5d4276c
     xorg = pDrawable->x;
5d4276c
@@ -707,15 +746,14 @@ exaPolyFillRect(DrawablePtr pDrawable,
5d4276c
 	    (*pExaScr->info->Solid) (pPixmap,
5d4276c
 				     fullX1 + xoff, fullY1 + yoff,
5d4276c
 				     fullX2 + xoff, fullY2 + yoff);
5d4276c
-	    exaPixmapDirty (pPixmap, fullX1 + xoff, fullY1 + yoff,
5d4276c
-			    fullX2 + xoff, fullY2 + yoff);
5d4276c
 	}
5d4276c
 	else
5d4276c
 	{
5d4276c
 	    pbox = REGION_RECTS(pClip);
5d4276c
 	    /*
5d4276c
 	     * clip the rectangle to each box in the clip region
5d4276c
-	     * this is logically equivalent to calling Intersect()
5d4276c
+	     * this is logically equivalent to calling Intersect(),
5d4276c
+	     * but rectangles may overlap each other here.
5d4276c
 	     */
5d4276c
 	    while(n--)
5d4276c
 	    {
5d4276c
@@ -738,14 +776,15 @@ exaPolyFillRect(DrawablePtr pDrawable,
5d4276c
 		    (*pExaScr->info->Solid) (pPixmap,
5d4276c
 					     partX1 + xoff, partY1 + yoff,
5d4276c
 					     partX2 + xoff, partY2 + yoff);
5d4276c
-		    exaPixmapDirty (pPixmap, partX1 + xoff, partY1 + yoff,
5d4276c
-				    partX2 + xoff, partY2 + yoff);
5d4276c
 		}
5d4276c
 	    }
5d4276c
 	}
5d4276c
     }
5d4276c
     (*pExaScr->info->DoneSolid) (pPixmap);
5d4276c
     exaMarkSync(pDrawable->pScreen);
5d4276c
+
5d4276c
+out:
5d4276c
+    REGION_DESTROY(pScreen, pReg);
5d4276c
 }
5d4276c
 
5d4276c
 static void
5d4276c
@@ -775,20 +814,19 @@ exaSolidBoxClipped (DrawablePtr	pDrawable,
5d4276c
 	pPixmap->drawable.width > pExaScr->info->maxX ||
5d4276c
 	pPixmap->drawable.height > pExaScr->info->maxY)
5d4276c
     {
5d4276c
-	exaDoMigration (pixmaps, 1, FALSE);
5d4276c
-	goto fallback;
5d4276c
+	fallback = TRUE;
5d4276c
     } else {
5d4276c
 	exaDoMigration (pixmaps, 1, TRUE);
5d4276c
     }
5d4276c
 
5d4276c
-    pPixmap = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff);
5d4276c
+    exaGetDrawableDeltas (pDrawable, pPixmap, &xoff, &yoff);
5d4276c
 
5d4276c
-    if (!pPixmap ||
5d4276c
+    if (fallback || !exaPixmapIsOffscreen(pPixmap) ||
5d4276c
 	!(*pExaScr->info->PrepareSolid) (pPixmap, GXcopy, pm, fg))
5d4276c
     {
5d4276c
-fallback:
5d4276c
 	EXA_FALLBACK(("to %p (%c)\n", pDrawable,
5d4276c
 		      exaDrawableLocation(pDrawable)));
5d4276c
+	exaDoMigration (pixmaps, 1, FALSE);
5d4276c
 	fallback = TRUE;
5d4276c
 	exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
 	fg = fbReplicatePixel (fg, pDrawable->bitsPerPixel);
5d4276c
@@ -827,10 +865,10 @@ fallback:
5d4276c
 	    (*pExaScr->info->Solid) (pPixmap,
5d4276c
 				     partX1 + xoff, partY1 + yoff,
5d4276c
 				     partX2 + xoff, partY2 + yoff);
5d4276c
-	    exaPixmapDirty (pPixmap, partX1 + xoff, partY1 + yoff,
5d4276c
-			    partX2 + xoff, partY2 + yoff);
5d4276c
-	} else
5d4276c
-	    exaDrawableDirty (pDrawable, partX1, partY1, partX2, partY2);
5d4276c
+	}
5d4276c
+
5d4276c
+	exaPixmapDirty (pPixmap, partX1 + xoff, partY1 + yoff, partX2 + xoff,
5d4276c
+			partY2 + yoff);
5d4276c
     }
5d4276c
 
5d4276c
     if (fallback)
5d4276c
@@ -870,12 +908,36 @@ exaImageGlyphBlt (DrawablePtr	pDrawable,
5d4276c
     int		    dstBpp;
5d4276c
     int		    dstXoff, dstYoff;
5d4276c
     FbBits	    depthMask;
5d4276c
+    PixmapPtr	    pPixmap = exaGetDrawablePixmap(pDrawable);
5d4276c
+    ExaMigrationRec pixmaps[1];
5d4276c
+    int		    xBack, widthBack, yBack, heightBack;
5d4276c
+
5d4276c
+    for (ppci = ppciInit, n = nglyph, widthBack = 0; n; n--)
5d4276c
+	widthBack += (*ppci++)->metrics.characterWidth;
5d4276c
+
5d4276c
+    xBack = x;
5d4276c
+    if (widthBack < 0)
5d4276c
+    {
5d4276c
+	xBack += widthBack;
5d4276c
+	widthBack = -widthBack;
5d4276c
+    }
5d4276c
+    yBack = y - FONTASCENT(pGC->font);
5d4276c
+    heightBack = FONTASCENT(pGC->font) + FONTDESCENT(pGC->font);
5d4276c
+
5d4276c
+    if (xBack >= pDrawable->width || yBack >= pDrawable->height ||
5d4276c
+	(xBack + widthBack) <= 0 || (yBack + heightBack) <= 0)
5d4276c
+	return;
5d4276c
+
5d4276c
+    pixmaps[0].as_dst = TRUE;
5d4276c
+    pixmaps[0].as_src = TRUE;
5d4276c
+    pixmaps[0].pPix = pPixmap;
5d4276c
 
5d4276c
     depthMask = FbFullMask(pDrawable->depth);
5d4276c
     if ((pGC->planemask & depthMask) != depthMask)
5d4276c
     {
5d4276c
+	exaDoMigration(pixmaps, 1, FALSE);
5d4276c
 	ExaCheckImageGlyphBlt(pDrawable, pGC, x, y, nglyph, ppciInit, pglyphBase);
5d4276c
-	return;
5d4276c
+	goto damage;
5d4276c
     }
5d4276c
     glyph = NULL;
5d4276c
     switch (pDrawable->bitsPerPixel) {
5d4276c
@@ -887,6 +949,8 @@ exaImageGlyphBlt (DrawablePtr	pDrawable,
5d4276c
 
5d4276c
     x += pDrawable->x;
5d4276c
     y += pDrawable->y;
5d4276c
+    xBack += pDrawable->x;
5d4276c
+    yBack += pDrawable->y;
5d4276c
 
5d4276c
     if (TERMINALFONT (pGC->font) && !glyph)
5d4276c
     {
5d4276c
@@ -894,23 +958,6 @@ exaImageGlyphBlt (DrawablePtr	pDrawable,
5d4276c
     }
5d4276c
     else
5d4276c
     {
5d4276c
-	int		xBack, widthBack;
5d4276c
-	int		yBack, heightBack;
5d4276c
-
5d4276c
-	ppci = ppciInit;
5d4276c
-	n = nglyph;
5d4276c
-	widthBack = 0;
5d4276c
-	while (n--)
5d4276c
-	    widthBack += (*ppci++)->metrics.characterWidth;
5d4276c
-
5d4276c
-        xBack = x;
5d4276c
-	if (widthBack < 0)
5d4276c
-	{
5d4276c
-	    xBack += widthBack;
5d4276c
-	    widthBack = -widthBack;
5d4276c
-	}
5d4276c
-	yBack = y - FONTASCENT(pGC->font);
5d4276c
-	heightBack = FONTASCENT(pGC->font) + FONTDESCENT(pGC->font);
5d4276c
         exaSolidBoxClipped (pDrawable,
5d4276c
 			    fbGetCompositeClip(pGC),
5d4276c
 			    pGC->planemask,
5d4276c
@@ -923,74 +970,50 @@ exaImageGlyphBlt (DrawablePtr	pDrawable,
5d4276c
     }
5d4276c
 
5d4276c
     EXA_FALLBACK(("to %p (%c)\n", pDrawable, exaDrawableLocation(pDrawable)));
5d4276c
+    exaDoMigration(pixmaps, 1, FALSE);
5d4276c
     exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
     exaPrepareAccessGC (pGC);
5d4276c
 
5d4276c
     fbGetDrawable (pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
5d4276c
 
5d4276c
-    ppci = ppciInit;
5d4276c
-    while (nglyph--)
5d4276c
+    for (ppci = ppciInit; nglyph; nglyph--, x += pci->metrics.characterWidth)
5d4276c
     {
5d4276c
 	pci = *ppci++;
5d4276c
-	pglyph = FONTGLYPHBITS(pglyphBase, pci);
5d4276c
 	gWidth = GLYPHWIDTHPIXELS(pci);
5d4276c
 	gHeight = GLYPHHEIGHTPIXELS(pci);
5d4276c
-	if (gWidth && gHeight)
5d4276c
+	gx = x + pci->metrics.leftSideBearing;
5d4276c
+	gy = y - pci->metrics.ascent;
5d4276c
+
5d4276c
+	if (!gWidth || !gHeight || (gx + gWidth) <= xBack ||
5d4276c
+	    (gy + gHeight) <= yBack || gx >= (xBack + widthBack) ||
5d4276c
+	    gy >= (yBack + heightBack))
5d4276c
+	    continue;
5d4276c
+
5d4276c
+	pglyph = FONTGLYPHBITS(pglyphBase, pci);
5d4276c
+
5d4276c
+	if (glyph && gWidth <= sizeof (FbStip) * 8 &&
5d4276c
+	    fbGlyphIn (fbGetCompositeClip(pGC), gx, gy, gWidth, gHeight))
5d4276c
 	{
5d4276c
-	    gx = x + pci->metrics.leftSideBearing;
5d4276c
-	    gy = y - pci->metrics.ascent;
5d4276c
-	    if (glyph && gWidth <= sizeof (FbStip) * 8 &&
5d4276c
-		fbGlyphIn (fbGetCompositeClip(pGC), gx, gy, gWidth, gHeight))
5d4276c
-	    {
5d4276c
-		(*glyph) (dst + (gy + dstYoff) * dstStride,
5d4276c
-			  dstStride,
5d4276c
-			  dstBpp,
5d4276c
-			  (FbStip *) pglyph,
5d4276c
-			  pPriv->fg,
5d4276c
-			  gx + dstXoff,
5d4276c
-			  gHeight);
5d4276c
-		exaDrawableDirty (pDrawable, gx, gy, gx + gWidth, gy + gHeight);
5d4276c
-	    }
5d4276c
-	    else
5d4276c
-	    {
5d4276c
-		RegionPtr pClip = fbGetCompositeClip(pGC);
5d4276c
-		int nbox;
5d4276c
-		BoxPtr pbox;
5d4276c
-
5d4276c
-		gStride = GLYPHWIDTHBYTESPADDED(pci) / sizeof (FbStip);
5d4276c
-		fbPutXYImage (pDrawable,
5d4276c
-			      pClip,
5d4276c
-			      pPriv->fg,
5d4276c
-			      pPriv->bg,
5d4276c
-			      pPriv->pm,
5d4276c
-			      GXcopy,
5d4276c
-			      opaque,
5d4276c
-
5d4276c
-			      gx,
5d4276c
-			      gy,
5d4276c
-			      gWidth, gHeight,
5d4276c
-
5d4276c
-			      (FbStip *) pglyph,
5d4276c
-			      gStride,
5d4276c
-			      0);
5d4276c
-
5d4276c
-		for (nbox = REGION_NUM_RECTS(pClip), pbox = REGION_RECTS(pClip);
5d4276c
-		     nbox--; pbox++) {
5d4276c
-		    int x1 = max(gx, pbox->x1), x2 = min(gx + gWidth, pbox->x2);
5d4276c
-		    int y1 = max(gy, pbox->y1), y2 = min(gy + gHeight, pbox->y2);
5d4276c
-
5d4276c
-		    if (x1 >= x2 || y1 >= y2)
5d4276c
-			continue;
5d4276c
-
5d4276c
-		    exaDrawableDirty (pDrawable, gx, gy, gx + gWidth,
5d4276c
-				      gy + gHeight);
5d4276c
-		}
5d4276c
-	    }
5d4276c
+	    (*glyph) (dst + (gy + dstYoff) * dstStride, dstStride, dstBpp,
5d4276c
+		      (FbStip *) pglyph, pPriv->fg, gx + dstXoff, gHeight);
5d4276c
+	}
5d4276c
+	else
5d4276c
+	{
5d4276c
+	    RegionPtr pClip = fbGetCompositeClip(pGC);
5d4276c
+
5d4276c
+	    gStride = GLYPHWIDTHBYTESPADDED(pci) / sizeof (FbStip);
5d4276c
+	    fbPutXYImage (pDrawable, pClip, pPriv->fg, pPriv->bg, pPriv->pm,
5d4276c
+			  GXcopy, opaque, gx, gy, gWidth, gHeight,
5d4276c
+			  (FbStip *) pglyph, gStride, 0);
5d4276c
 	}
5d4276c
-	x += pci->metrics.characterWidth;
5d4276c
     }
5d4276c
     exaFinishAccessGC (pGC);
5d4276c
     exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
+
5d4276c
+damage:
5d4276c
+    exaGetDrawableDeltas(pDrawable, pPixmap, &dstXoff, &dstYoff);
5d4276c
+    exaPixmapDirty(pPixmap, xBack + dstXoff, yBack + dstYoff,
5d4276c
+		   xBack + dstXoff + widthBack, yBack + dstYoff + heightBack);
5d4276c
 }
5d4276c
 
5d4276c
 const GCOps exaOps = {
5d4276c
@@ -1043,10 +1066,12 @@ exaCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
5d4276c
     REGION_UNINIT(pWin->drawable.pScreen, &rgnDst);
5d4276c
 }
5d4276c
 
5d4276c
-static void
5d4276c
+static Bool
5d4276c
 exaFillRegionSolid (DrawablePtr	pDrawable,
5d4276c
 		    RegionPtr	pRegion,
5d4276c
-		    Pixel	pixel)
5d4276c
+		    Pixel	pixel,
5d4276c
+		    CARD32	planemask,
5d4276c
+		    CARD32	alu)
5d4276c
 {
5d4276c
     ExaScreenPriv(pDrawable->pScreen);
5d4276c
     PixmapPtr pPixmap;
5d4276c
@@ -1062,22 +1087,19 @@ exaFillRegionSolid (DrawablePtr	pDrawable,
5d4276c
     if (pPixmap->drawable.width > pExaScr->info->maxX ||
5d4276c
 	pPixmap->drawable.height > pExaScr->info->maxY)
5d4276c
     {
5d4276c
-	exaDoMigration (pixmaps, 1, FALSE);
5d4276c
 	goto fallback;
5d4276c
     } else {
5d4276c
 	exaDoMigration (pixmaps, 1, TRUE);
5d4276c
     }
5d4276c
 
5d4276c
     if ((pPixmap = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff)) &&
5d4276c
-	(*pExaScr->info->PrepareSolid) (pPixmap, GXcopy, FB_ALLONES, pixel))
5d4276c
+	(*pExaScr->info->PrepareSolid) (pPixmap, alu, planemask, pixel))
5d4276c
     {
5d4276c
 	while (nbox--)
5d4276c
 	{
5d4276c
 	    (*pExaScr->info->Solid) (pPixmap,
5d4276c
 				     pBox->x1 + xoff, pBox->y1 + yoff,
5d4276c
 				     pBox->x2 + xoff, pBox->y2 + yoff);
5d4276c
-	    exaPixmapDirty (pPixmap, pBox->x1 + xoff, pBox->y1 + yoff,
5d4276c
-			    pBox->x2 + xoff, pBox->y2 + yoff);
5d4276c
 	    pBox++;
5d4276c
 	}
5d4276c
 	(*pExaScr->info->DoneSolid) (pPixmap);
5d4276c
@@ -1086,27 +1108,30 @@ exaFillRegionSolid (DrawablePtr	pDrawable,
5d4276c
     else
5d4276c
     {
5d4276c
 fallback:
5d4276c
+	if (alu != GXcopy || planemask != FB_ALLONES)
5d4276c
+	    return FALSE;
5d4276c
 	EXA_FALLBACK(("to %p (%c)\n", pDrawable,
5d4276c
 		      exaDrawableLocation(pDrawable)));
5d4276c
+	exaDoMigration (pixmaps, 1, FALSE);
5d4276c
 	exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
 	fbFillRegionSolid (pDrawable, pRegion, 0,
5d4276c
 			   fbReplicatePixel (pixel, pDrawable->bitsPerPixel));
5d4276c
 	exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
-	while (nbox--)
5d4276c
-	{
5d4276c
-	    exaDrawableDirty (pDrawable, pBox->x1, pBox->y1, pBox->x2, pBox->y2);
5d4276c
-	    pBox++;
5d4276c
-	}
5d4276c
     }
5d4276c
+
5d4276c
+    return TRUE;
5d4276c
 }
5d4276c
 
5d4276c
 /* Try to do an accelerated tile of the pTile into pRegion of pDrawable.
5d4276c
  * Based on fbFillRegionTiled(), fbTile().
5d4276c
  */
5d4276c
-static void
5d4276c
+Bool
5d4276c
 exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
 		    RegionPtr	pRegion,
5d4276c
-		    PixmapPtr	pTile)
5d4276c
+		    PixmapPtr	pTile,
5d4276c
+		    DDXPointPtr pPatOrg,
5d4276c
+		    CARD32	planemask,
5d4276c
+		    CARD32	alu)
5d4276c
 {
5d4276c
     ExaScreenPriv(pDrawable->pScreen);
5d4276c
     PixmapPtr pPixmap;
5d4276c
@@ -1122,10 +1147,10 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
     /* If we're filling with a solid color, grab it out and go to
5d4276c
      * FillRegionSolid, saving numerous copies.
5d4276c
      */
5d4276c
-    if (tileWidth == 1 && tileHeight == 1) {
5d4276c
-	exaFillRegionSolid(pDrawable, pRegion, exaGetPixmapFirstPixel (pTile));
5d4276c
-	return;
5d4276c
-    }
5d4276c
+    if (tileWidth == 1 && tileHeight == 1)
5d4276c
+	return exaFillRegionSolid(pDrawable, pRegion,
5d4276c
+				  exaGetPixmapFirstPixel (pTile), planemask,
5d4276c
+				  alu);
5d4276c
 
5d4276c
     pixmaps[0].as_dst = TRUE;
5d4276c
     pixmaps[0].as_src = FALSE;
5d4276c
@@ -1139,7 +1164,6 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
 	tileWidth > pExaScr->info->maxX ||
5d4276c
 	tileHeight > pExaScr->info->maxY)
5d4276c
     {
5d4276c
-	exaDoMigration (pixmaps, 2, FALSE);
5d4276c
 	goto fallback;
5d4276c
     } else {
5d4276c
 	exaDoMigration (pixmaps, 2, TRUE);
5d4276c
@@ -1153,8 +1177,9 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
     if (!exaPixmapIsOffscreen(pTile))
5d4276c
 	goto fallback;
5d4276c
 
5d4276c
-    if ((*pExaScr->info->PrepareCopy) (exaGetOffscreenPixmap((DrawablePtr)pTile, &tileXoff, &tileYoff), pPixmap, 0, 0, GXcopy,
5d4276c
-				       FB_ALLONES))
5d4276c
+    if ((*pExaScr->info->PrepareCopy) (exaGetOffscreenPixmap((DrawablePtr)pTile,
5d4276c
+							     &tileXoff, &tileYoff),
5d4276c
+				       pPixmap, 0, 0, alu, planemask))
5d4276c
     {
5d4276c
 	while (nbox--)
5d4276c
 	{
5d4276c
@@ -1162,7 +1187,7 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
 	    int dstY = pBox->y1;
5d4276c
 	    int tileY;
5d4276c
 
5d4276c
-	    tileY = (dstY - pDrawable->y) % tileHeight;
5d4276c
+	    tileY = (dstY - pDrawable->y - pPatOrg->y) % tileHeight;
5d4276c
 	    while (height > 0) {
5d4276c
 		int width = pBox->x2 - pBox->x1;
5d4276c
 		int dstX = pBox->x1;
5d4276c
@@ -1173,7 +1198,7 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
 		    h = height;
5d4276c
 		height -= h;
5d4276c
 
5d4276c
-		tileX = (dstX - pDrawable->x) % tileWidth;
5d4276c
+		tileX = (dstX - pDrawable->x - pPatOrg->x) % tileWidth;
5d4276c
 		while (width > 0) {
5d4276c
 		    int w = tileWidth - tileX;
5d4276c
 		    if (w > width)
5d4276c
@@ -1190,38 +1215,44 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
5d4276c
 		dstY += h;
5d4276c
 		tileY = 0;
5d4276c
 	    }
5d4276c
-	    exaPixmapDirty (pPixmap, pBox->x1 + xoff, pBox->y1 + yoff,
5d4276c
-			    pBox->x2 + xoff, pBox->y2 + yoff);
5d4276c
 	    pBox++;
5d4276c
 	}
5d4276c
 	(*pExaScr->info->DoneCopy) (pPixmap);
5d4276c
 	exaMarkSync(pDrawable->pScreen);
5d4276c
-	return;
5d4276c
+	return TRUE;
5d4276c
     }
5d4276c
 
5d4276c
 fallback:
5d4276c
+    if (alu != GXcopy || planemask != FB_ALLONES)
5d4276c
+	return FALSE;
5d4276c
     EXA_FALLBACK(("from %p to %p (%c,%c)\n", pTile, pDrawable,
5d4276c
 		  exaDrawableLocation(&pTile->drawable),
5d4276c
 		  exaDrawableLocation(pDrawable)));
5d4276c
+    exaDoMigration (pixmaps, 2, FALSE);
5d4276c
     exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
     exaPrepareAccess ((DrawablePtr)pTile, EXA_PREPARE_SRC);
5d4276c
     fbFillRegionTiled (pDrawable, pRegion, pTile);
5d4276c
     exaFinishAccess ((DrawablePtr)pTile, EXA_PREPARE_SRC);
5d4276c
     exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
-    while (nbox--)
5d4276c
-    {
5d4276c
-	exaDrawableDirty (pDrawable, pBox->x1, pBox->y1, pBox->x2, pBox->y2);
5d4276c
-	pBox++;
5d4276c
-    }
5d4276c
+
5d4276c
+    return TRUE;
5d4276c
 }
5d4276c
 
5d4276c
 void
5d4276c
 exaPaintWindow(WindowPtr pWin, RegionPtr pRegion, int what)
5d4276c
 {
5d4276c
     ExaScreenPriv (pWin->drawable.pScreen);
5d4276c
-    if (!REGION_NUM_RECTS(pRegion))
5d4276c
+    PixmapPtr pPixmap = exaGetDrawablePixmap((DrawablePtr)pWin);
5d4276c
+    int xoff, yoff;
5d4276c
+    BoxPtr pBox;
5d4276c
+    int nbox = REGION_NUM_RECTS(pRegion);
5d4276c
+
5d4276c
+    if (!nbox)
5d4276c
 	return;
5d4276c
+
5d4276c
     if (!pExaScr->swappedOut) {
5d4276c
+	DDXPointRec zeros = { 0, 0 };
5d4276c
+
5d4276c
         switch (what) {
5d4276c
         case PW_BACKGROUND:
5d4276c
             switch (pWin->backgroundState) {
5d4276c
@@ -1235,25 +1266,41 @@ exaPaintWindow(WindowPtr pWin, RegionPtr pRegion, int what)
5d4276c
                                                                  what);
5d4276c
                 return;
5d4276c
             case BackgroundPixel:
5d4276c
-                exaFillRegionSolid((DrawablePtr)pWin, pRegion, pWin->background.pixel);
5d4276c
-                return;
5d4276c
+		exaFillRegionSolid((DrawablePtr)pWin, pRegion, pWin->background.pixel,
5d4276c
+				   FB_ALLONES, GXcopy);
5d4276c
+                goto damage;
5d4276c
             case BackgroundPixmap:
5d4276c
-                exaFillRegionTiled((DrawablePtr)pWin, pRegion, pWin->background.pixmap);
5d4276c
-                return;
5d4276c
+                exaFillRegionTiled((DrawablePtr)pWin, pRegion, pWin->background.pixmap,
5d4276c
+				   &zeros, FB_ALLONES, GXcopy);
5d4276c
+                goto damage;
5d4276c
             }
5d4276c
             break;
5d4276c
         case PW_BORDER:
5d4276c
             if (pWin->borderIsPixel) {
5d4276c
-                exaFillRegionSolid((DrawablePtr)pWin, pRegion, pWin->border.pixel);
5d4276c
-                return;
5d4276c
+                exaFillRegionSolid((DrawablePtr)pWin, pRegion, pWin->border.pixel,
5d4276c
+				   FB_ALLONES, GXcopy);
5d4276c
+                goto damage;
5d4276c
             } else {
5d4276c
-                exaFillRegionTiled((DrawablePtr)pWin, pRegion, pWin->border.pixmap);
5d4276c
-                return;
5d4276c
+                exaFillRegionTiled((DrawablePtr)pWin, pRegion, pWin->border.pixmap,
5d4276c
+				   &zeros, FB_ALLONES, GXcopy);
5d4276c
+                goto damage;
5d4276c
             }
5d4276c
             break;
5d4276c
         }
5d4276c
     }
5d4276c
     ExaCheckPaintWindow (pWin, pRegion, what);
5d4276c
+
5d4276c
+damage:
5d4276c
+    exaGetDrawableDeltas((DrawablePtr)pWin, pPixmap, &xoff, &yoff);
5d4276c
+
5d4276c
+    pBox = REGION_RECTS(pRegion);
5d4276c
+
5d4276c
+    while (nbox--)
5d4276c
+    {
5d4276c
+	exaPixmapDirty (pPixmap, pBox->x1 + xoff, pBox->y1 + yoff,
5d4276c
+			pBox->x2 + xoff, pBox->y2 + yoff);
5d4276c
+	pBox++;
5d4276c
+    }
5d4276c
 }
5d4276c
 
5d4276c
 /**
5d4276c
@@ -1273,27 +1320,22 @@ exaGetImage (DrawablePtr pDrawable, int x, int y, int w, int h,
5d4276c
     int xoff, yoff;
5d4276c
     Bool ok;
5d4276c
 
5d4276c
-    if (pExaScr->swappedOut || pExaScr->info->DownloadFromScreen == NULL)
5d4276c
+    if (pExaScr->swappedOut || (w == 1 && h == 1))
5d4276c
 	goto fallback;
5d4276c
 
5d4276c
+    if (pExaScr->info->DownloadFromScreen == NULL)
5d4276c
+	goto migrate_and_fallback;
5d4276c
+
5d4276c
     /* Only cover the ZPixmap, solid copy case. */
5d4276c
     if (format != ZPixmap || !EXA_PM_IS_SOLID(pDrawable, planeMask))
5d4276c
-	goto fallback;
5d4276c
+	goto migrate_and_fallback;
5d4276c
 
5d4276c
     /* Only try to handle the 8bpp and up cases, since we don't want to think
5d4276c
      * about <8bpp.
5d4276c
      */
5d4276c
     if (pDrawable->bitsPerPixel < 8)
5d4276c
-	goto fallback;
5d4276c
+	goto migrate_and_fallback;
5d4276c
 
5d4276c
-    /* Migrate, but assume that we could accelerate the download. It is up to
5d4276c
-     * the migration scheme to ensure that this case doesn't result in bad
5d4276c
-     * moving of pixmaps.
5d4276c
-     */
5d4276c
-    pixmaps[0].as_dst = FALSE;
5d4276c
-    pixmaps[0].as_src = TRUE;
5d4276c
-    pixmaps[0].pPix = exaGetDrawablePixmap (pDrawable);
5d4276c
-    exaDoMigration (pixmaps, 1, TRUE);
5d4276c
     pPix = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff);
5d4276c
     if (pPix == NULL)
5d4276c
 	goto fallback;
5d4276c
@@ -1308,12 +1350,12 @@ exaGetImage (DrawablePtr pDrawable, int x, int y, int w, int h,
5d4276c
 	return;
5d4276c
     }
5d4276c
 
5d4276c
-fallback:
5d4276c
+migrate_and_fallback:
5d4276c
     pixmaps[0].as_dst = FALSE;
5d4276c
     pixmaps[0].as_src = TRUE;
5d4276c
     pixmaps[0].pPix = exaGetDrawablePixmap (pDrawable);
5d4276c
     exaDoMigration (pixmaps, 1, FALSE);
5d4276c
-
5d4276c
+fallback:
5d4276c
     ExaCheckGetImage (pDrawable, x, y, w, h, format, planeMask, d);
5d4276c
 }
5d4276c
 
5d4276c
diff --git a/exa/exa_migration.c b/exa/exa_migration.c
5d4276c
index eedc5fd..70d8e12 100644
5d4276c
--- a/exa/exa_migration.c
5d4276c
+++ b/exa/exa_migration.c
5d4276c
@@ -464,12 +464,10 @@ exaAssertNotDirty (PixmapPtr pPixmap)
5d4276c
     BoxPtr pBox = REGION_RECTS(pValidReg);
5d4276c
     Bool ret = TRUE;
5d4276c
 
5d4276c
-    if (pExaPixmap == NULL || pExaPixmap->fb_ptr == NULL)
5d4276c
+    if (!nbox || exaPixmapIsPinned(pPixmap) || pExaPixmap->fb_ptr == NULL)
5d4276c
 	return ret;
5d4276c
 
5d4276c
-    dst = pExaPixmap->sys_ptr;
5d4276c
     dst_pitch = pExaPixmap->sys_pitch;
5d4276c
-    src = pExaPixmap->fb_ptr;
5d4276c
     src_pitch = pExaPixmap->fb_pitch;
5d4276c
     cpp = pPixmap->drawable.bitsPerPixel / 8;
5d4276c
 
5d4276c
@@ -486,21 +484,18 @@ exaAssertNotDirty (PixmapPtr pPixmap)
5d4276c
 		continue;
5d4276c
 
5d4276c
 	    rowbytes = (pBox->x2 - pBox->x1) * cpp;
5d4276c
-	    src += pBox->y1 * src_pitch + pBox->x1 * cpp;
5d4276c
-	    dst += pBox->y1 * dst_pitch + pBox->x1 * cpp;
5d4276c
+	    src = pExaPixmap->fb_ptr + pBox->y1 * src_pitch + pBox->x1 * cpp;
5d4276c
+	    dst = pExaPixmap->sys_ptr + pBox->y1 * dst_pitch + pBox->x1 * cpp;
5d4276c
 
5d4276c
-	    for (y = pBox->y2 - pBox->y1; y; y--) {
5d4276c
-		if (memcmp(dst + pBox->y1 * dst_pitch + pBox->x1 * cpp,
5d4276c
-			   src + pBox->y1 * src_pitch + pBox->x1 * cpp,
5d4276c
-			   (pBox->x2 - pBox->x1) * cpp) != 0) {
5d4276c
+	    for (y = pBox->y1; y < pBox->y2;
5d4276c
+		 y++, src += src_pitch, dst += dst_pitch) {
5d4276c
+		if (memcmp(dst, src, rowbytes) != 0) {
5d4276c
 		    ret = FALSE;
5d4276c
+		    exaPixmapDirty(pPixmap, pBox->x1, pBox->y1, pBox->x2,
5d4276c
+				   pBox->y2);
5d4276c
 		    break;
5d4276c
 		}
5d4276c
-		src += src_pitch;
5d4276c
-		dst += dst_pitch;
5d4276c
 	    }
5d4276c
-	    src -= pBox->y1 * src_pitch + pBox->x1 * cpp;
5d4276c
-	    dst -= pBox->y1 * dst_pitch + pBox->x1 * cpp;
5d4276c
     }
5d4276c
     exaFinishAccess(&pPixmap->drawable, EXA_PREPARE_SRC);
5d4276c
 
5d4276c
diff --git a/exa/exa_offscreen.c b/exa/exa_offscreen.c
5d4276c
index 7708dd7..c666b00 100644
5d4276c
--- a/exa/exa_offscreen.c
5d4276c
+++ b/exa/exa_offscreen.c
5d4276c
@@ -54,7 +54,7 @@ ExaOffscreenValidate (ScreenPtr pScreen)
5d4276c
 	assert (area->offset >= area->base_offset &&
5d4276c
 		area->offset < (area->base_offset + area->size));
5d4276c
 	if (prev)
5d4276c
-	    assert (prev->base_offset + prev->area.size == area->base_offset);
5d4276c
+	    assert (prev->base_offset + prev->size == area->base_offset);
5d4276c
 	prev = area;
5d4276c
     }
5d4276c
     assert (prev->base_offset + prev->size == pExaScr->info->memorySize);
5d4276c
@@ -341,13 +341,15 @@ exaEnableDisableFBAccess (int index, Bool enable)
5d4276c
     ScreenPtr pScreen = screenInfo.screens[index];
5d4276c
     ExaScreenPriv (pScreen);
5d4276c
 
5d4276c
-    if (!enable) {
5d4276c
+    if (!enable && pExaScr->disableFbCount++ == 0) {
5d4276c
 	if (pExaScr->info->exa_minor < 1)
5d4276c
 	    ExaOffscreenSwapOut (pScreen);
5d4276c
 	else
5d4276c
 	    ExaOffscreenEjectPixmaps (pScreen);
5d4276c
 	pExaScr->swappedOut = TRUE;
5d4276c
-    } else {
5d4276c
+    }
5d4276c
+    
5d4276c
+    if (enable && --pExaScr->disableFbCount == 0) {
5d4276c
 	if (pExaScr->info->exa_minor < 1)
5d4276c
 	    ExaOffscreenSwapIn (pScreen);
5d4276c
 	pExaScr->swappedOut = FALSE;
5d4276c
@@ -427,7 +429,7 @@ ExaOffscreenMarkUsed (PixmapPtr pPixmap)
5d4276c
     ExaScreenPriv (pPixmap->drawable.pScreen);
5d4276c
     static int iter = 0;
5d4276c
 
5d4276c
-    if (!pExaPixmap->area)
5d4276c
+    if (!pExaPixmap || !pExaPixmap->area)
5d4276c
 	return;
5d4276c
 
5d4276c
     /* The numbers here are arbitrary.  We may want to tune these. */
5d4276c
diff --git a/exa/exa_priv.h b/exa/exa_priv.h
5d4276c
index 984cb66..a6d98cd 100644
5d4276c
--- a/exa/exa_priv.h
5d4276c
+++ b/exa/exa_priv.h
5d4276c
@@ -113,6 +113,7 @@ typedef struct {
5d4276c
     enum ExaMigrationHeuristic	 migration;
5d4276c
     Bool			 hideOffscreenPixmapData;
5d4276c
     Bool			 checkDirtyCorrectness;
5d4276c
+    unsigned			 disableFbCount;
5d4276c
 } ExaScreenPrivRec, *ExaScreenPrivPtr;
5d4276c
 
5d4276c
 /*
5d4276c
@@ -287,6 +288,10 @@ exaGetPixmapFirstPixel (PixmapPtr pPixmap);
5d4276c
 void
5d4276c
 exaCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc);
5d4276c
 
5d4276c
+Bool
5d4276c
+exaFillRegionTiled (DrawablePtr	pDrawable, RegionPtr pRegion, PixmapPtr pTile,
5d4276c
+		    DDXPointPtr pPatOrg, CARD32 planemask, CARD32 alu);
5d4276c
+
5d4276c
 void
5d4276c
 exaPaintWindow(WindowPtr pWin, RegionPtr pRegion, int what);
5d4276c
 
5d4276c
@@ -318,9 +323,6 @@ ExaCheckComposite (CARD8      op,
5d4276c
 
5d4276c
 /* exa_offscreen.c */
5d4276c
 void
5d4276c
-ExaOffscreenMarkUsed (PixmapPtr pPixmap);
5d4276c
-
5d4276c
-void
5d4276c
 ExaOffscreenSwapOut (ScreenPtr pScreen);
5d4276c
 
5d4276c
 void
5d4276c
@@ -343,7 +345,8 @@ void
5d4276c
 exaPixmapDirty(PixmapPtr pPix, int x1, int y1, int x2, int y2);
5d4276c
 
5d4276c
 void
5d4276c
-exaDrawableDirty(DrawablePtr pDrawable, int x1, int y1, int x2, int y2);
5d4276c
+exaGetDrawableDeltas (DrawablePtr pDrawable, PixmapPtr pPixmap,
5d4276c
+		      int *xp, int *yp);
5d4276c
 
5d4276c
 Bool
5d4276c
 exaDrawableIsOffscreen (DrawablePtr pDrawable);
5d4276c
diff --git a/exa/exa_render.c b/exa/exa_render.c
5d4276c
index b78d728..5e7c67f 100644
5d4276c
--- a/exa/exa_render.c
5d4276c
+++ b/exa/exa_render.c
5d4276c
@@ -297,15 +297,15 @@ exaTryDriverSolidFill(PicturePtr	pSrc,
5d4276c
 
5d4276c
     nbox = REGION_NUM_RECTS(&region);
5d4276c
     pbox = REGION_RECTS(&region);
5d4276c
+
5d4276c
     while (nbox--)
5d4276c
     {
5d4276c
 	(*pExaScr->info->Solid) (pDstPix,
5d4276c
 				 pbox->x1 + dst_off_x, pbox->y1 + dst_off_y,
5d4276c
 				 pbox->x2 + dst_off_x, pbox->y2 + dst_off_y);
5d4276c
-	exaPixmapDirty (pDstPix, pbox->x1 + dst_off_x, pbox->y1 + dst_off_y,
5d4276c
-			pbox->x2 + dst_off_x, pbox->y2 + dst_off_y);
5d4276c
 	pbox++;
5d4276c
     }
5d4276c
+
5d4276c
     (*pExaScr->info->DoneSolid) (pDstPix);
5d4276c
     exaMarkSync(pDst->pDrawable->pScreen);
5d4276c
 
5d4276c
@@ -446,8 +446,6 @@ exaTryDriverComposite(CARD8		op,
5d4276c
 				     pbox->y1 + dst_off_y,
5d4276c
 				     pbox->x2 - pbox->x1,
5d4276c
 				     pbox->y2 - pbox->y1);
5d4276c
-	exaPixmapDirty (pDstPix, pbox->x1 + dst_off_x, pbox->y1 + dst_off_y,
5d4276c
-			pbox->x2 + dst_off_x, pbox->y2 + dst_off_y);
5d4276c
 	pbox++;
5d4276c
     }
5d4276c
     (*pExaScr->info->DoneComposite) (pDstPix);
5d4276c
@@ -521,6 +519,9 @@ exaTryMagicTwoPassCompositeHelper(CARD8 op,
5d4276c
 				  CARD16 height)
5d4276c
 {
5d4276c
     ExaScreenPriv (pDst->pDrawable->pScreen);
5d4276c
+    DrawablePtr pDstDraw = pDst->pDrawable;
5d4276c
+    PixmapPtr pDstPixmap = exaGetDrawablePixmap(pDstDraw);
5d4276c
+    int xoff, yoff;
5d4276c
 
5d4276c
     assert(op == PictOpOver);
5d4276c
 
5d4276c
@@ -539,6 +540,12 @@ exaTryMagicTwoPassCompositeHelper(CARD8 op,
5d4276c
     exaComposite(PictOpOutReverse, pSrc, pMask, pDst, xSrc, ySrc, xMask, yMask,
5d4276c
 		 xDst, yDst, width, height);
5d4276c
 
5d4276c
+    exaGetDrawableDeltas(pDstDraw, pDstPixmap, &xoff, &yoff);
5d4276c
+    xoff += pDstDraw->x;
5d4276c
+    yoff += pDstDraw->y;
5d4276c
+    exaPixmapDirty(pDstPixmap, xDst + xoff, yDst + yoff, xDst + xoff + width,
5d4276c
+		   yDst + yoff + height);
5d4276c
+
5d4276c
     /* Then, add in the source value times the destination alpha factors (1.0).
5d4276c
      */
5d4276c
     exaComposite(PictOpAdd, pSrc, pMask, pDst, xSrc, ySrc, xMask, yMask,
5d4276c
@@ -565,6 +572,28 @@ exaComposite(CARD8	op,
5d4276c
     int ret = -1;
5d4276c
     Bool saveSrcRepeat = pSrc->repeat;
5d4276c
     Bool saveMaskRepeat = pMask ? pMask->repeat : 0;
5d4276c
+    ExaMigrationRec pixmaps[3];
5d4276c
+    int npixmaps = 1;
5d4276c
+    PixmapPtr pSrcPixmap = NULL;
5d4276c
+
5d4276c
+    pixmaps[0].as_dst = TRUE;
5d4276c
+    pixmaps[0].as_src = exaOpReadsDestination(op);
5d4276c
+    pixmaps[0].pPix = exaGetDrawablePixmap (pDst->pDrawable);
5d4276c
+
5d4276c
+    if (pSrc->pDrawable) {
5d4276c
+	pSrcPixmap = exaGetDrawablePixmap (pSrc->pDrawable);
5d4276c
+	pixmaps[npixmaps].as_dst = FALSE;
5d4276c
+	pixmaps[npixmaps].as_src = TRUE;
5d4276c
+	pixmaps[npixmaps].pPix = pSrcPixmap;
5d4276c
+	npixmaps++;
5d4276c
+    }
5d4276c
+
5d4276c
+    if (pMask && pMask->pDrawable) {
5d4276c
+	pixmaps[npixmaps].as_dst = FALSE;
5d4276c
+	pixmaps[npixmaps].as_src = TRUE;
5d4276c
+	pixmaps[npixmaps].pPix = exaGetDrawablePixmap (pMask->pDrawable);
5d4276c
+	npixmaps++;
5d4276c
+    }
5d4276c
 
5d4276c
     /* We currently don't support acceleration of gradients, or other pictures
5d4276c
      * with a NULL pDrawable.
5d4276c
@@ -583,19 +612,24 @@ exaComposite(CARD8	op,
5d4276c
 
5d4276c
     if (!pMask)
5d4276c
     {
5d4276c
-	if (op == PictOpSrc)
5d4276c
+      if ((op == PictOpSrc &&
5d4276c
+	   ((pSrc->format == pDst->format) ||
5d4276c
+	    (pSrc->format==PICT_a8r8g8b8 && pDst->format==PICT_x8r8g8b8) ||
5d4276c
+	    (pSrc->format==PICT_a8b8g8r8 && pDst->format==PICT_x8b8g8r8))) ||
5d4276c
+	  (op == PictOpOver && !pSrc->alphaMap && !pDst->alphaMap &&
5d4276c
+	   pSrc->format == pDst->format &&
5d4276c
+	   (pSrc->format==PICT_x8r8g8b8 || pSrc->format==PICT_x8b8g8r8)))
5d4276c
 	{
5d4276c
 	    if (pSrc->pDrawable->width == 1 &&
5d4276c
-		pSrc->pDrawable->height == 1 && pSrc->repeat &&
5d4276c
-		pSrc->repeatType == RepeatNormal)
5d4276c
+		pSrc->pDrawable->height == 1 &&
5d4276c
+		pSrc->repeat)
5d4276c
 	    {
5d4276c
 		ret = exaTryDriverSolidFill(pSrc, pDst, xSrc, ySrc, xDst, yDst,
5d4276c
 					    width, height);
5d4276c
 		if (ret == 1)
5d4276c
 		    goto done;
5d4276c
 	    }
5d4276c
-	    else if (!pSrc->repeat && !pSrc->transform &&
5d4276c
-		     pSrc->format == pDst->format)
5d4276c
+	    else if (pSrcPixmap && !pSrc->repeat && !pSrc->transform)
5d4276c
 	    {
5d4276c
 		RegionRec	region;
5d4276c
 
5d4276c
@@ -617,6 +651,45 @@ exaComposite(CARD8	op,
5d4276c
 		REGION_UNINIT(pDst->pDrawable->pScreen, &region);
5d4276c
 		goto done;
5d4276c
 	    }
5d4276c
+	    else if (pSrcPixmap && !pSrc->transform &&
5d4276c
+		     pSrc->repeatType == RepeatNormal)
5d4276c
+	    {
5d4276c
+		RegionRec region;
5d4276c
+		DDXPointRec srcOrg;
5d4276c
+
5d4276c
+		/* Let's see if the driver can do the repeat in one go */
5d4276c
+		if (pExaScr->info->PrepareComposite && !pSrc->alphaMap &&
5d4276c
+		    !pDst->alphaMap)
5d4276c
+		{
5d4276c
+		    ret = exaTryDriverComposite(op, pSrc, pMask, pDst, xSrc,
5d4276c
+						ySrc, xMask, yMask, xDst, yDst,
5d4276c
+						width, height);
5d4276c
+		    if (ret == 1)
5d4276c
+			goto done;
5d4276c
+		}
5d4276c
+
5d4276c
+		/* Now see if we can use exaFillRegionTiled() */
5d4276c
+		xDst += pDst->pDrawable->x;
5d4276c
+		yDst += pDst->pDrawable->y;
5d4276c
+		xSrc += pSrc->pDrawable->x;
5d4276c
+		ySrc += pSrc->pDrawable->y;
5d4276c
+
5d4276c
+		if (!miComputeCompositeRegion (&region, pSrc, pMask, pDst, xSrc,
5d4276c
+					       ySrc, xMask, yMask, xDst, yDst,
5d4276c
+					       width, height))
5d4276c
+		    goto done;
5d4276c
+
5d4276c
+		srcOrg.x = (xSrc - xDst) % pSrcPixmap->drawable.width;
5d4276c
+		srcOrg.y = (ySrc - yDst) % pSrcPixmap->drawable.height;
5d4276c
+
5d4276c
+		ret = exaFillRegionTiled(pDst->pDrawable, &region, pSrcPixmap,
5d4276c
+					 &srcOrg, FB_ALLONES, GXcopy);
5d4276c
+
5d4276c
+		REGION_UNINIT(pDst->pDrawable->pScreen, &region);
5d4276c
+
5d4276c
+		if (ret)
5d4276c
+		    goto done;
5d4276c
+	    }
5d4276c
 	}
5d4276c
     }
5d4276c
 
5d4276c
@@ -627,8 +700,8 @@ exaComposite(CARD8	op,
5d4276c
 	    pMask->repeat = 0;
5d4276c
 
5d4276c
     if (pExaScr->info->PrepareComposite &&
5d4276c
-	(!pSrc->repeat || pSrc->repeat == RepeatNormal) &&
5d4276c
-	(!pMask || !pMask->repeat || pMask->repeat == RepeatNormal) &&
5d4276c
+	(!pSrc->repeat || pSrc->repeatType == RepeatNormal) &&
5d4276c
+	(!pMask || !pMask->repeat || pMask->repeatType == RepeatNormal) &&
5d4276c
 	!pSrc->alphaMap && (!pMask || !pMask->alphaMap) && !pDst->alphaMap)
5d4276c
     {
5d4276c
 	Bool isSrcSolid;
5d4276c
@@ -660,39 +733,14 @@ exaComposite(CARD8	op,
5d4276c
 	}
5d4276c
     }
5d4276c
 
5d4276c
-    if (ret != 0) {
5d4276c
-	ExaMigrationRec pixmaps[3];
5d4276c
-	/* failure to accelerate was not due to pixmaps being in the wrong
5d4276c
-	 * locations.
5d4276c
-	 */
5d4276c
-	pixmaps[0].as_dst = TRUE;
5d4276c
-	pixmaps[0].as_src = exaOpReadsDestination(op);
5d4276c
-	pixmaps[0].pPix = exaGetDrawablePixmap (pDst->pDrawable);
5d4276c
-	pixmaps[1].as_dst = FALSE;
5d4276c
-	pixmaps[1].as_src = TRUE;
5d4276c
-	pixmaps[1].pPix = exaGetDrawablePixmap (pSrc->pDrawable);
5d4276c
-	if (pMask) {
5d4276c
-	    pixmaps[2].as_dst = FALSE;
5d4276c
-	    pixmaps[2].as_src = TRUE;
5d4276c
-	    pixmaps[2].pPix = exaGetDrawablePixmap (pMask->pDrawable);
5d4276c
-	    exaDoMigration(pixmaps, 3, FALSE);
5d4276c
-	} else {
5d4276c
-	    exaDoMigration(pixmaps, 2, FALSE);
5d4276c
-	}
5d4276c
-    }
5d4276c
-
5d4276c
 fallback:
5d4276c
 #if DEBUG_TRACE_FALL
5d4276c
     exaPrintCompositeFallback (op, pSrc, pMask, pDst);
5d4276c
 #endif
5d4276c
 
5d4276c
+    exaDoMigration(pixmaps, npixmaps, FALSE);
5d4276c
     ExaCheckComposite (op, pSrc, pMask, pDst, xSrc, ySrc,
5d4276c
 		      xMask, yMask, xDst, yDst, width, height);
5d4276c
-    exaDrawableDirty(pDst->pDrawable,
5d4276c
-		     pDst->pDrawable->x + xDst,
5d4276c
-		     pDst->pDrawable->y + yDst,
5d4276c
-		     pDst->pDrawable->x + xDst + width,
5d4276c
-		     pDst->pDrawable->y + yDst + height);
5d4276c
 
5d4276c
 done:
5d4276c
     pSrc->repeat = saveSrcRepeat;
5d4276c
@@ -716,6 +764,7 @@ exaRasterizeTrapezoid (PicturePtr pPicture, xTrapezoid  *trap,
5d4276c
 {
5d4276c
     DrawablePtr pDraw = pPicture->pDrawable;
5d4276c
     ExaMigrationRec pixmaps[1];
5d4276c
+    int xoff, yoff;
5d4276c
 
5d4276c
     pixmaps[0].as_dst = TRUE;
5d4276c
     pixmaps[0].as_src = TRUE;
5d4276c
@@ -724,8 +773,10 @@ exaRasterizeTrapezoid (PicturePtr pPicture, xTrapezoid  *trap,
5d4276c
 
5d4276c
     exaPrepareAccess(pDraw, EXA_PREPARE_DEST);
5d4276c
     fbRasterizeTrapezoid(pPicture, trap, x_off, y_off);
5d4276c
-    exaDrawableDirty(pDraw, pDraw->x, pDraw->y,
5d4276c
-		     pDraw->x + pDraw->width, pDraw->y + pDraw->height);
5d4276c
+    exaGetDrawableDeltas(pDraw, pixmaps[0].pPix, &xoff, &yoff);
5d4276c
+    exaPixmapDirty(pixmaps[0].pPix, pDraw->x + xoff, pDraw->y + yoff,
5d4276c
+		   pDraw->x + xoff + pDraw->width,
5d4276c
+		   pDraw->y + yoff + pDraw->height);
5d4276c
     exaFinishAccess(pDraw, EXA_PREPARE_DEST);
5d4276c
 }
5d4276c
 
5d4276c
@@ -739,6 +790,7 @@ exaAddTriangles (PicturePtr pPicture, INT16 x_off, INT16 y_off, int ntri,
5d4276c
 {
5d4276c
     DrawablePtr pDraw = pPicture->pDrawable;
5d4276c
     ExaMigrationRec pixmaps[1];
5d4276c
+    int xoff, yoff;
5d4276c
 
5d4276c
     pixmaps[0].as_dst = TRUE;
5d4276c
     pixmaps[0].as_src = TRUE;
5d4276c
@@ -747,8 +799,10 @@ exaAddTriangles (PicturePtr pPicture, INT16 x_off, INT16 y_off, int ntri,
5d4276c
 
5d4276c
     exaPrepareAccess(pDraw, EXA_PREPARE_DEST);
5d4276c
     fbAddTriangles(pPicture, x_off, y_off, ntri, tris);
5d4276c
-    exaDrawableDirty(pDraw, pDraw->x, pDraw->y,
5d4276c
-		     pDraw->x + pDraw->width, pDraw->y + pDraw->height);
5d4276c
+    exaGetDrawableDeltas(pDraw, pixmaps[0].pPix, &xoff, &yoff);
5d4276c
+    exaPixmapDirty(pixmaps[0].pPix, pDraw->x + xoff, pDraw->y + yoff,
5d4276c
+		   pDraw->x + xoff + pDraw->width,
5d4276c
+		   pDraw->y + yoff + pDraw->height);
5d4276c
     exaFinishAccess(pDraw, EXA_PREPARE_DEST);
5d4276c
 }
5d4276c
 
5d4276c
@@ -845,10 +899,11 @@ exaGlyphs (CARD8	op,
5d4276c
     PixmapPtr	pPixmap = NULL;
5d4276c
     PicturePtr	pPicture;
5d4276c
     PixmapPtr   pMaskPixmap = NULL;
5d4276c
+    PixmapPtr   pDstPixmap = exaGetDrawablePixmap(pDst->pDrawable);
5d4276c
     PicturePtr  pMask;
5d4276c
     ScreenPtr   pScreen = pDst->pDrawable->pScreen;
5d4276c
     int		width = 0, height = 0;
5d4276c
-    int		x, y;
5d4276c
+    int		x, y, x1, y1, xoff, yoff;
5d4276c
     int		xDst = list->xOff, yDst = list->yOff;
5d4276c
     int		n;
5d4276c
     int		error;
5d4276c
@@ -892,7 +947,12 @@ exaGlyphs (CARD8	op,
5d4276c
 	xRectangle  rect;
5d4276c
 	
5d4276c
 	miGlyphExtents (nlist, list, glyphs, &extents);
5d4276c
-	
5d4276c
+
5d4276c
+	extents.x1 = max(extents.x1, 0);
5d4276c
+	extents.y1 = max(extents.y1, 0);
5d4276c
+	extents.x2 = min(extents.x2, pDst->pDrawable->width);
5d4276c
+	extents.y2 = min(extents.y2, pDst->pDrawable->height);
5d4276c
+
5d4276c
 	if (extents.x2 <= extents.x1 || extents.y2 <= extents.y1)
5d4276c
 	    return;
5d4276c
 	width = extents.x2 - extents.x1;
5d4276c
@@ -918,6 +978,7 @@ exaGlyphs (CARD8	op,
5d4276c
 	rect.width = width;
5d4276c
 	rect.height = height;
5d4276c
 	(*pGC->ops->PolyFillRect) (&pMaskPixmap->drawable, pGC, 1, &rect);
5d4276c
+	exaPixmapDirty(pMaskPixmap, 0, 0, width, height);
5d4276c
 	FreeScratchGC (pGC);
5d4276c
 	x = -extents.x1;
5d4276c
 	y = -extents.y1;
5d4276c
@@ -929,6 +990,8 @@ exaGlyphs (CARD8	op,
5d4276c
 	y = 0;
5d4276c
     }
5d4276c
 
5d4276c
+    exaGetDrawableDeltas(pDst->pDrawable, pDstPixmap, &xoff, &yoff);
5d4276c
+
5d4276c
     while (nlist--)
5d4276c
     {
5d4276c
 	GCPtr pGC = NULL;
5d4276c
@@ -983,13 +1046,21 @@ exaGlyphs (CARD8	op,
5d4276c
 	pixmaps[0].as_dst = TRUE;
5d4276c
 	pixmaps[0].as_src = TRUE;
5d4276c
 	pixmaps[0].pPix = pPixmap;
5d4276c
-	exaDoMigration (pixmaps, 1, TRUE);
5d4276c
+	exaDoMigration (pixmaps, 1, pExaScr->info->PrepareComposite != NULL);
5d4276c
 
5d4276c
 	while (n--)
5d4276c
 	{
5d4276c
 	    GlyphPtr glyph = *glyphs++;
5d4276c
 	    pointer glyphdata = (pointer) (glyph + 1);
5d4276c
-	    
5d4276c
+	    DrawablePtr pCmpDrw = (maskFormat ? pMask : pDst)->pDrawable;
5d4276c
+
5d4276c
+	    x1 = x - glyph->info.x;
5d4276c
+	    y1 = y - glyph->info.y;
5d4276c
+
5d4276c
+	    if (x1 >= pCmpDrw->width || y1 >= pCmpDrw->height ||
5d4276c
+		(x1 + glyph->info.width) <= 0 || (y1 + glyph->info.height) <= 0)
5d4276c
+		goto nextglyph;
5d4276c
+
5d4276c
 	    (*pScreen->ModifyPixmapHeader) (pScratchPixmap, 
5d4276c
 					    glyph->info.width,
5d4276c
 					    glyph->info.height,
5d4276c
@@ -1048,17 +1119,22 @@ exaGlyphs (CARD8	op,
5d4276c
 	    if (maskFormat)
5d4276c
 	    {
5d4276c
 		exaComposite (PictOpAdd, pPicture, NULL, pMask, 0, 0, 0, 0,
5d4276c
-			      x - glyph->info.x, y - glyph->info.y,
5d4276c
-			      glyph->info.width, glyph->info.height);
5d4276c
+			      x1, y1, glyph->info.width, glyph->info.height);
5d4276c
+		exaPixmapDirty(pMaskPixmap, x1, y1, x1 + glyph->info.width,
5d4276c
+			       y1 + glyph->info.height);
5d4276c
 	    }
5d4276c
 	    else
5d4276c
 	    {
5d4276c
 		exaComposite (op, pSrc, pPicture, pDst,
5d4276c
-			      xSrc + (x - glyph->info.x) - xDst,
5d4276c
-			      ySrc + (y - glyph->info.y) - yDst,
5d4276c
-			      0, 0, x - glyph->info.x, y - glyph->info.y,
5d4276c
-			      glyph->info.width, glyph->info.height);
5d4276c
+			      xSrc + x1 - xDst, ySrc + y1 - yDst,
5d4276c
+			      0, 0, x1, y1, glyph->info.width,
5d4276c
+			      glyph->info.height);
5d4276c
+		x1 += pDst->pDrawable->x + xoff;
5d4276c
+		y1 += pDst->pDrawable->y + yoff;
5d4276c
+		exaPixmapDirty(pDstPixmap, x1, y1, x1 + glyph->info.width,
5d4276c
+			       y1 + glyph->info.height);
5d4276c
 	    }
5d4276c
+nextglyph:
5d4276c
 	    x += glyph->info.xOff;
5d4276c
 	    y += glyph->info.yOff;
5d4276c
 	}
5d4276c
diff --git a/exa/exa_unaccel.c b/exa/exa_unaccel.c
5d4276c
index 7713a08..b67ea63 100644
5d4276c
--- a/exa/exa_unaccel.c
5d4276c
+++ b/exa/exa_unaccel.c
5d4276c
@@ -88,10 +88,15 @@ ExaCheckPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth,
5d4276c
 		 int x, int y, int w, int h, int leftPad, int format,
5d4276c
 		 char *bits)
5d4276c
 {
5d4276c
+    PixmapPtr pPixmap = exaGetDrawablePixmap(pDrawable);
5d4276c
+    int xoff, yoff;
5d4276c
+
5d4276c
     EXA_FALLBACK(("to %p (%c)\n", pDrawable, exaDrawableLocation(pDrawable)));
5d4276c
     exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
     fbPutImage (pDrawable, pGC, depth, x, y, w, h, leftPad, format, bits);
5d4276c
     exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
+    exaGetDrawableDeltas(pDrawable, pPixmap, &xoff, &yoff);
5d4276c
+    exaPixmapDirty(pPixmap, x + xoff, y + yoff, x + xoff + w, y + yoff + h);
5d4276c
 }
5d4276c
 
5d4276c
 RegionPtr
5d4276c
@@ -201,32 +206,11 @@ ExaCheckPolyFillRect (DrawablePtr pDrawable, GCPtr pGC,
5d4276c
 {
5d4276c
     EXA_FALLBACK(("to %p (%c)\n", pDrawable, exaDrawableLocation(pDrawable)));
5d4276c
 
5d4276c
-    if (nrect) {
5d4276c
-	int x1 = max(prect->x, 0), y1 = max(prect->y, 0);
5d4276c
-	int x2 = min(prect->x + prect->width, pDrawable->width);
5d4276c
-	int y2 = min(prect->y + prect->height, pDrawable->height);
5d4276c
-
5d4276c
-	exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
-	exaPrepareAccessGC (pGC);
5d4276c
-	fbPolyFillRect (pDrawable, pGC, nrect, prect);
5d4276c
-	exaFinishAccessGC (pGC);
5d4276c
-	exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
-
5d4276c
-	/* Only track bounding box of damage, as this path can degenerate to
5d4276c
-	 * zillions of damage boxes
5d4276c
-	 */
5d4276c
-	while (--nrect)
5d4276c
-	{
5d4276c
-	    prect++;
5d4276c
-	    x1 = min(x1, prect->x);
5d4276c
-	    x2 = max(x2, prect->x + prect->width);
5d4276c
-	    y1 = min(y1, prect->y);
5d4276c
-	    y2 = max(y2, prect->y + prect->height);
5d4276c
-	}
5d4276c
-
5d4276c
-	exaDrawableDirty (pDrawable, pDrawable->x + x1, pDrawable->y + y1,
5d4276c
-			  pDrawable->x + x2, pDrawable->y + y2);
5d4276c
-    }
5d4276c
+    exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
+    exaPrepareAccessGC (pGC);
5d4276c
+    fbPolyFillRect (pDrawable, pGC, nrect, prect);
5d4276c
+    exaFinishAccessGC (pGC);
5d4276c
+    exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
5d4276c
 }
5d4276c
 
5d4276c
 void
5d4276c
@@ -385,31 +369,48 @@ ExaCheckComposite (CARD8      op,
5d4276c
 /**
5d4276c
  * Gets the 0,0 pixel of a pixmap.  Used for doing solid fills of tiled pixmaps
5d4276c
  * that happen to be 1x1.  Pixmap must be at least 8bpp.
5d4276c
+ *
5d4276c
+ * XXX This really belongs in fb, so it can be aware of tiling and etc.
5d4276c
  */
5d4276c
 CARD32
5d4276c
 exaGetPixmapFirstPixel (PixmapPtr pPixmap)
5d4276c
 {
5d4276c
     CARD32 pixel;
5d4276c
+    void *fb;
5d4276c
+    Bool need_finish = FALSE;
5d4276c
+    BoxRec box;
5d4276c
     ExaMigrationRec pixmaps[1];
5d4276c
+    ExaPixmapPriv (pPixmap);
5d4276c
+
5d4276c
+    /* Try to avoid framebuffer readbacks */
5d4276c
+    if (exaPixmapIsOffscreen(pPixmap)) {
5d4276c
+	if (!miPointInRegion(DamageRegion(pExaPixmap->pDamage), 0, 0,  &box)) {
5d4276c
+	    fb = pExaPixmap->sys_ptr;
5d4276c
+	} else {
5d4276c
+	    need_finish = TRUE;
5d4276c
+	    fb = pPixmap->devPrivate.ptr;
5d4276c
+	    pixmaps[0].as_dst = FALSE;
5d4276c
+	    pixmaps[0].as_src = TRUE;
5d4276c
+	    pixmaps[0].pPix = pPixmap;
5d4276c
+	    exaDoMigration (pixmaps, 1, FALSE);
5d4276c
+	    exaPrepareAccess(&pPixmap->drawable, EXA_PREPARE_SRC);
5d4276c
+	}
5d4276c
+    }
5d4276c
 
5d4276c
-    pixmaps[0].as_dst = FALSE;
5d4276c
-    pixmaps[0].as_src = TRUE;
5d4276c
-    pixmaps[0].pPix = pPixmap;
5d4276c
-    exaDoMigration (pixmaps, 1, FALSE);
5d4276c
-
5d4276c
-    exaPrepareAccess(&pPixmap->drawable, EXA_PREPARE_SRC);
5d4276c
     switch (pPixmap->drawable.bitsPerPixel) {
5d4276c
     case 32:
5d4276c
-	pixel = *(CARD32 *)(pPixmap->devPrivate.ptr);
5d4276c
+	pixel = *(CARD32 *)fb;
5d4276c
 	break;
5d4276c
     case 16:
5d4276c
-	pixel = *(CARD16 *)(pPixmap->devPrivate.ptr);
5d4276c
+	pixel = *(CARD16 *)fb;
5d4276c
 	break;
5d4276c
     default:
5d4276c
-	pixel = *(CARD8 *)(pPixmap->devPrivate.ptr);
5d4276c
+	pixel = *(CARD8 *)fb;
5d4276c
 	break;
5d4276c
     }
5d4276c
-    exaFinishAccess(&pPixmap->drawable, EXA_PREPARE_SRC);
5d4276c
+
5d4276c
+    if (need_finish)
5d4276c
+	exaFinishAccess(&pPixmap->drawable, EXA_PREPARE_SRC);
5d4276c
 
5d4276c
     return pixel;
5d4276c
 }