ff0bb53
From 3a5a88ec354de45434b9e037d21cc56c3079f4b2 Mon Sep 17 00:00:00 2001
ff0bb53
From: Adam Jackson <ajax@redhat.com>
ff0bb53
Date: Wed, 5 Nov 2008 14:59:27 -0500
fad0e67
Subject: [PATCH] Upgrade to master EXA
fad0e67
fad0e67
---
ff0bb53
 exa/Makefile.am            |    1 +
ff0bb53
 exa/exa.c                  |   95 +++--
ff0bb53
 exa/exa.h                  |   19 +-
ff0bb53
 exa/exa_accel.c            |  144 ++++++--
ff0bb53
 exa/exa_glyphs.c           |  897 ++++++++++++++++++++++++++++++++++++++++++++
ff0bb53
 exa/exa_migration.c        |   45 ++-
ff0bb53
 exa/exa_priv.h             |   76 +++-
ff0bb53
 exa/exa_render.c           |  280 +++++++++++++--
ff0bb53
 exa/exa_unaccel.c          |   30 +-
ff0bb53
 hw/xfree86/exa/exa.man.pre |    6 -
ff0bb53
 hw/xfree86/exa/examodule.c |    9 +-
ff0bb53
 11 files changed, 1446 insertions(+), 156 deletions(-)
ff0bb53
 create mode 100644 exa/exa_glyphs.c
fad0e67
fad0e67
diff --git a/exa/Makefile.am b/exa/Makefile.am
fad0e67
index e2f7ed3..2b3f1e4 100644
fad0e67
--- a/exa/Makefile.am
fad0e67
+++ b/exa/Makefile.am
fad0e67
@@ -18,6 +18,7 @@ libexa_la_SOURCES = \
fad0e67
 	exa.c \
fad0e67
 	exa.h \
fad0e67
 	exa_accel.c \
fad0e67
+	exa_glyphs.c \
fad0e67
 	exa_migration.c \
fad0e67
 	exa_offscreen.c \
fad0e67
 	exa_render.c \
fad0e67
diff --git a/exa/exa.c b/exa/exa.c
ff0bb53
index 72539c0..22f3ab9 100644
fad0e67
--- a/exa/exa.c
fad0e67
+++ b/exa/exa.c
fad0e67
@@ -35,8 +35,6 @@
fad0e67
 #include <stdlib.h>
fad0e67
 
fad0e67
 #include "exa_priv.h"
fad0e67
-#include <X11/fonts/fontstruct.h>
fad0e67
-#include "dixfontstr.h"
fad0e67
 #include "exa.h"
fad0e67
 #include "cw.h"
fad0e67
 
ff0bb53
@@ -165,7 +163,7 @@ exaPixmapDirty (PixmapPtr pPix, int x1, int y1, int x2, int y2)
fad0e67
     RegionPtr pDamageReg;
fad0e67
     RegionRec region;
fad0e67
 
fad0e67
-    if (!pExaPixmap)
fad0e67
+    if (!pExaPixmap || !pExaPixmap->pDamage)
fad0e67
 	return;
fad0e67
 	
fad0e67
     box.x1 = max(x1, 0);
ff0bb53
@@ -265,6 +263,21 @@ exaSetFbPitch(ExaScreenPrivPtr pExaScr, ExaPixmapPrivPtr pExaPixmap,
fad0e67
                                      pExaScr->info->pixmapPitchAlign);
fad0e67
 }
fad0e67
 
fad0e67
+
fad0e67
+static void
fad0e67
+ExaDamageReport(DamagePtr pDamage, RegionPtr pReg, void *pClosure)
fad0e67
+{
fad0e67
+    PixmapPtr pPixmap = pClosure;
fad0e67
+    ExaPixmapPriv(pPixmap);
fad0e67
+    RegionPtr pDamageReg = DamageRegion(pDamage);
fad0e67
+
fad0e67
+    if (pExaPixmap->pendingDamage) {
fad0e67
+	REGION_UNION(pScreen, pDamageReg, pDamageReg, pReg);
fad0e67
+	pExaPixmap->pendingDamage = FALSE;
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+
fad0e67
 /**
fad0e67
  * exaCreatePixmap() creates a new pixmap.
fad0e67
  *
ff0bb53
@@ -325,6 +338,7 @@ exaCreatePixmap(ScreenPtr pScreen, int w, int h, int depth,
fad0e67
                                        paddedWidth, NULL);
fad0e67
         pExaPixmap->score = EXA_PIXMAP_SCORE_PINNED;
fad0e67
         pExaPixmap->fb_ptr = NULL;
fad0e67
+        pExaPixmap->pDamage = NULL;
fad0e67
     } else {
fad0e67
         pExaPixmap->driverPriv = NULL;
fad0e67
         /* Scratch pixmaps may have w/h equal to zero, and may not be
ff0bb53
@@ -349,21 +363,22 @@ exaCreatePixmap(ScreenPtr pScreen, int w, int h, int depth,
fad0e67
 	     fbDestroyPixmap(pPixmap);
fad0e67
 	     return NULL;
fad0e67
         }
fad0e67
-    }
fad0e67
- 
fad0e67
-    pExaPixmap->area = NULL;
fad0e67
 
fad0e67
-    /* Set up damage tracking */
fad0e67
-    pExaPixmap->pDamage = DamageCreate (NULL, NULL, DamageReportNone, TRUE,
fad0e67
-					pScreen, pPixmap);
fad0e67
+	/* Set up damage tracking */
fad0e67
+	pExaPixmap->pDamage = DamageCreate (ExaDamageReport, NULL,
fad0e67
+					    DamageReportRawRegion, TRUE,
fad0e67
+					    pScreen, pPixmap);
fad0e67
 
fad0e67
-    if (pExaPixmap->pDamage == NULL) {
fad0e67
-	fbDestroyPixmap (pPixmap);
fad0e67
-	return NULL;
fad0e67
-    }
fad0e67
+	if (pExaPixmap->pDamage == NULL) {
fad0e67
+	    fbDestroyPixmap (pPixmap);
fad0e67
+	    return NULL;
fad0e67
+	}
fad0e67
 
fad0e67
-    DamageRegister (&pPixmap->drawable, pExaPixmap->pDamage);
fad0e67
-    DamageSetReportAfterOp (pExaPixmap->pDamage, TRUE);
fad0e67
+	DamageRegister (&pPixmap->drawable, pExaPixmap->pDamage);
fad0e67
+	DamageSetReportAfterOp (pExaPixmap->pDamage, TRUE);
fad0e67
+    }
fad0e67
+ 
fad0e67
+    pExaPixmap->area = NULL;
fad0e67
 
fad0e67
     /* None of the pixmap bits are valid initially */
fad0e67
     REGION_NULL(pScreen, &pExaPixmap->validSys);
ff0bb53
@@ -660,34 +675,25 @@ exaCreateGC (GCPtr pGC)
fad0e67
     return TRUE;
fad0e67
 }
fad0e67
 
fad0e67
-void
fad0e67
-exaPrepareAccessWindow(WindowPtr pWin)
fad0e67
+static Bool
fad0e67
+exaChangeWindowAttributes(WindowPtr pWin, unsigned long mask)
fad0e67
 {
fad0e67
-    if (pWin->backgroundState == BackgroundPixmap) 
fad0e67
+    Bool ret;
fad0e67
+
fad0e67
+    if ((mask & CWBackPixmap) && pWin->backgroundState == BackgroundPixmap) 
fad0e67
         exaPrepareAccess(&pWin->background.pixmap->drawable, EXA_PREPARE_SRC);
fad0e67
 
fad0e67
-    if (pWin->borderIsPixel == FALSE)
fad0e67
-        exaPrepareAccess(&pWin->border.pixmap->drawable, EXA_PREPARE_SRC);
fad0e67
-}
fad0e67
+    if ((mask & CWBorderPixmap) && pWin->borderIsPixel == FALSE)
fad0e67
+        exaPrepareAccess(&pWin->border.pixmap->drawable, EXA_PREPARE_MASK);
fad0e67
 
fad0e67
-void
fad0e67
-exaFinishAccessWindow(WindowPtr pWin)
fad0e67
-{
fad0e67
-    if (pWin->backgroundState == BackgroundPixmap) 
fad0e67
-        exaFinishAccess(&pWin->background.pixmap->drawable, EXA_PREPARE_SRC);
fad0e67
+    ret = fbChangeWindowAttributes(pWin, mask);
fad0e67
 
fad0e67
-    if (pWin->borderIsPixel == FALSE)
fad0e67
-        exaFinishAccess(&pWin->border.pixmap->drawable, EXA_PREPARE_SRC);
fad0e67
-}
fad0e67
+    if ((mask & CWBorderPixmap) && pWin->borderIsPixel == FALSE)
fad0e67
+        exaFinishAccess(&pWin->border.pixmap->drawable, EXA_PREPARE_MASK);
fad0e67
 
fad0e67
-static Bool
fad0e67
-exaChangeWindowAttributes(WindowPtr pWin, unsigned long mask)
fad0e67
-{
fad0e67
-    Bool ret;
fad0e67
+    if ((mask & CWBackPixmap) && pWin->backgroundState == BackgroundPixmap) 
fad0e67
+        exaFinishAccess(&pWin->background.pixmap->drawable, EXA_PREPARE_SRC);
fad0e67
 
fad0e67
-    exaPrepareAccessWindow(pWin);
fad0e67
-    ret = fbChangeWindowAttributes(pWin, mask);
fad0e67
-    exaFinishAccessWindow(pWin);
fad0e67
     return ret;
fad0e67
 }
fad0e67
 
ff0bb53
@@ -741,6 +747,9 @@ exaCloseScreen(int i, ScreenPtr pScreen)
fad0e67
     PictureScreenPtr	ps = GetPictureScreenIfSet(pScreen);
fad0e67
 #endif
fad0e67
 
fad0e67
+    if (ps->Glyphs == exaGlyphs)
fad0e67
+	exaGlyphsFini(pScreen);
fad0e67
+
fad0e67
     pScreen->CreateGC = pExaScr->SavedCreateGC;
fad0e67
     pScreen->CloseScreen = pExaScr->SavedCloseScreen;
fad0e67
     pScreen->GetImage = pExaScr->SavedGetImage;
ff0bb53
@@ -754,8 +763,9 @@ exaCloseScreen(int i, ScreenPtr pScreen)
fad0e67
 #ifdef RENDER
fad0e67
     if (ps) {
fad0e67
 	ps->Composite = pExaScr->SavedComposite;
fad0e67
+	ps->Glyphs = pExaScr->SavedGlyphs;
fad0e67
 	ps->Trapezoids = pExaScr->SavedTrapezoids;
fad0e67
-	ps->AddTraps = pExaScr->SavedAddTraps;
fad0e67
+	ps->Triangles = pExaScr->SavedTriangles;
fad0e67
     }
fad0e67
 #endif
fad0e67
 
ff0bb53
@@ -917,14 +927,16 @@ exaDriverInit (ScreenPtr		pScreen,
fad0e67
         pExaScr->SavedComposite = ps->Composite;
fad0e67
 	ps->Composite = exaComposite;
fad0e67
 
fad0e67
+	if (pScreenInfo->PrepareComposite) {
fad0e67
+	    pExaScr->SavedGlyphs = ps->Glyphs;
fad0e67
+	    ps->Glyphs = exaGlyphs;
fad0e67
+	}
fad0e67
+	
fad0e67
 	pExaScr->SavedTriangles = ps->Triangles;
fad0e67
 	ps->Triangles = exaTriangles;
fad0e67
 
fad0e67
 	pExaScr->SavedTrapezoids = ps->Trapezoids;
fad0e67
 	ps->Trapezoids = exaTrapezoids;
fad0e67
-
fad0e67
-	pExaScr->SavedAddTraps = ps->AddTraps;
fad0e67
-	ps->AddTraps = ExaCheckAddTraps;
fad0e67
     }
fad0e67
 #endif
fad0e67
 
ff0bb53
@@ -978,6 +990,9 @@ exaDriverInit (ScreenPtr		pScreen,
fad0e67
 	}
fad0e67
     }
fad0e67
 
fad0e67
+    if (ps->Glyphs == exaGlyphs)
fad0e67
+	exaGlyphsInit(pScreen);
fad0e67
+
fad0e67
     LogMessage(X_INFO, "EXA(%d): Driver registered support for the following"
fad0e67
 	       " operations:\n", pScreen->myNum);
fad0e67
     assert(pScreenInfo->PrepareSolid != NULL);
fad0e67
diff --git a/exa/exa.h b/exa/exa.h
ff0bb53
index a3dad69..6486bd3 100644
fad0e67
--- a/exa/exa.h
fad0e67
+++ b/exa/exa.h
fad0e67
@@ -744,21 +744,36 @@ typedef struct _ExaDriver {
fad0e67
 
fad0e67
 /** @} */
fad0e67
 
fad0e67
+/* in exa.c */
fad0e67
 ExaDriverPtr
fad0e67
 exaDriverAlloc(void);
fad0e67
 
fad0e67
 Bool
fad0e67
-exaDriverInit(ScreenPtr                pScreen,
fad0e67
+exaDriverInit(ScreenPtr      pScreen,
fad0e67
               ExaDriverPtr   pScreenInfo);
fad0e67
 
fad0e67
 void
fad0e67
-exaDriverFini(ScreenPtr                pScreen);
fad0e67
+exaDriverFini(ScreenPtr      pScreen);
fad0e67
 
fad0e67
 void
fad0e67
 exaMarkSync(ScreenPtr pScreen);
fad0e67
 void
fad0e67
 exaWaitSync(ScreenPtr pScreen);
fad0e67
 
fad0e67
+unsigned long
fad0e67
+exaGetPixmapOffset(PixmapPtr pPix);
fad0e67
+
fad0e67
+unsigned long
fad0e67
+exaGetPixmapPitch(PixmapPtr pPix);
fad0e67
+
fad0e67
+unsigned long
fad0e67
+exaGetPixmapSize(PixmapPtr pPix);
fad0e67
+
fad0e67
+void *
fad0e67
+exaGetPixmapDriverPrivate(PixmapPtr p);
fad0e67
+
fad0e67
+
fad0e67
+/* in exa_offscreen.c */
fad0e67
 ExaOffscreenArea *
fad0e67
 exaOffscreenAlloc(ScreenPtr pScreen, int size, int align,
fad0e67
                   Bool locked,
fad0e67
diff --git a/exa/exa_accel.c b/exa/exa_accel.c
fad0e67
index 3ec9625..1c07a0b 100644
fad0e67
--- a/exa/exa_accel.c
fad0e67
+++ b/exa/exa_accel.c
fad0e67
@@ -144,7 +144,6 @@ exaDoPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
fad0e67
     ExaScreenPriv (pDrawable->pScreen);
fad0e67
     PixmapPtr pPix = exaGetDrawablePixmap (pDrawable);
fad0e67
     ExaPixmapPriv(pPix);
fad0e67
-    ExaMigrationRec pixmaps[1];
fad0e67
     RegionPtr pClip;
fad0e67
     BoxPtr pbox;
fad0e67
     int nbox;
fad0e67
@@ -166,11 +165,16 @@ exaDoPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
fad0e67
     if (pExaScr->swappedOut)
fad0e67
 	return FALSE;
fad0e67
 
fad0e67
-    pixmaps[0].as_dst = TRUE;
fad0e67
-    pixmaps[0].as_src = FALSE;
fad0e67
-    pixmaps[0].pPix = pPix;
fad0e67
-    pixmaps[0].pReg = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
-    exaDoMigration (pixmaps, 1, TRUE);
fad0e67
+    if (pExaPixmap->pDamage) {
fad0e67
+	ExaMigrationRec pixmaps[1];
fad0e67
+
fad0e67
+ 	pixmaps[0].as_dst = TRUE;
fad0e67
+	pixmaps[0].as_src = FALSE;
fad0e67
+	pixmaps[0].pPix = pPix;
fad0e67
+	pixmaps[0].pReg = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
+
fad0e67
+	exaDoMigration (pixmaps, 1, TRUE);
fad0e67
+    }
fad0e67
 
fad0e67
     pPix = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff);
fad0e67
 
fad0e67
@@ -441,16 +445,36 @@ exaCopyNtoN (DrawablePtr    pSrcDrawable,
fad0e67
     pDstExaPixmap = ExaGetPixmapPriv (pDstPixmap);
fad0e67
 
fad0e67
     /* Check whether the accelerator can use this pixmap.
fad0e67
-     * FIXME: If it cannot, use temporary pixmaps so that the drawing
fad0e67
-     * happens within limits.
fad0e67
+     * If the pitch of the pixmaps is out of range, there's nothing
fad0e67
+     * we can do but fall back to software rendering.
fad0e67
      */
fad0e67
-    if (pSrcExaPixmap->accel_blocked || pDstExaPixmap->accel_blocked)
fad0e67
-    {
fad0e67
+    if (pSrcExaPixmap->accel_blocked & EXA_RANGE_PITCH ||
fad0e67
+        pDstExaPixmap->accel_blocked & EXA_RANGE_PITCH)
fad0e67
 	goto fallback;
fad0e67
-    } else {
fad0e67
-	exaDoMigration (pixmaps, 2, TRUE);
fad0e67
+
fad0e67
+    /* If the width or the height of either of the pixmaps
fad0e67
+     * is out of range, check whether the boxes are actually out of the
fad0e67
+     * addressable range as well. If they aren't, we can still do
fad0e67
+     * the copying in hardware.
fad0e67
+     */
fad0e67
+    if (pSrcExaPixmap->accel_blocked || pDstExaPixmap->accel_blocked) {
fad0e67
+        int i;
fad0e67
+
fad0e67
+        for (i = 0; i < nbox; i++) {
fad0e67
+            /* src */
fad0e67
+            if ((pbox[i].x2 + dx + src_off_x) >= pExaScr->info->maxX ||
fad0e67
+                (pbox[i].y2 + dy + src_off_y) >= pExaScr->info->maxY)
fad0e67
+                goto fallback;
fad0e67
+
fad0e67
+            /* dst */
fad0e67
+            if ((pbox[i].x2 + dst_off_x) >= pExaScr->info->maxX ||
fad0e67
+                (pbox[i].y2 + dst_off_y) >= pExaScr->info->maxY)
fad0e67
+                goto fallback;
fad0e67
+        }
fad0e67
     }
fad0e67
 
fad0e67
+    exaDoMigration (pixmaps, 2, TRUE);
fad0e67
+
fad0e67
     /* Mixed directions must be handled specially if the card is lame */
fad0e67
     if ((pExaScr->info->flags & EXA_TWO_BITBLT_DIRECTIONS) &&
fad0e67
 	reverse != upsidedown) {
fad0e67
@@ -860,16 +884,23 @@ exaImageGlyphBlt (DrawablePtr	pDrawable,
fad0e67
     FbBits	    depthMask;
fad0e67
     PixmapPtr	    pPixmap = exaGetDrawablePixmap(pDrawable);
fad0e67
     ExaPixmapPriv(pPixmap);
fad0e67
-    RegionPtr	    pending_damage = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
-    BoxRec	    extents = *REGION_EXTENTS(pScreen, pending_damage);
fad0e67
+    RegionPtr	    pending_damage = NULL;
fad0e67
+    BoxRec	    extents;
fad0e67
     int		    xoff, yoff;
fad0e67
 
fad0e67
-    if (extents.x1 >= extents.x2 || extents.y1 >= extents.y2)
fad0e67
-	return;
fad0e67
+    if (pExaPixmap->pDamage)
fad0e67
+	pending_damage = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
+
fad0e67
+    if (pending_damage) {
fad0e67
+	extents = *REGION_EXTENTS(pScreen, pending_damage);
fad0e67
+
fad0e67
+	if (extents.x1 >= extents.x2 || extents.y1 >= extents.y2)
fad0e67
+	    return;
fad0e67
 
fad0e67
-    depthMask = FbFullMask(pDrawable->depth);
fad0e67
+	depthMask = FbFullMask(pDrawable->depth);
fad0e67
+    }
fad0e67
 
fad0e67
-    if ((pGC->planemask & depthMask) != depthMask)
fad0e67
+    if (!pending_damage || (pGC->planemask & depthMask) != depthMask)
fad0e67
     {
fad0e67
 	ExaCheckImageGlyphBlt(pDrawable, pGC, x, y, nglyph, ppciInit, pglyphBase);
fad0e67
 	return;
fad0e67
@@ -1103,6 +1134,7 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
fad0e67
     int nbox = REGION_NUM_RECTS (pRegion);
fad0e67
     BoxPtr pBox = REGION_RECTS (pRegion);
fad0e67
     Bool ret = FALSE;
fad0e67
+    int i;
fad0e67
 
fad0e67
     tileWidth = pTile->drawable.width;
fad0e67
     tileHeight = pTile->drawable.height;
fad0e67
@@ -1125,14 +1157,11 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
fad0e67
     pixmaps[1].pPix = pTile;
fad0e67
     pixmaps[1].pReg = NULL;
fad0e67
 
fad0e67
-    exaGetDrawableDeltas(pDrawable, pPixmap, &xoff, &yoff);
fad0e67
-    REGION_TRANSLATE(pScreen, pRegion, xoff, yoff);
fad0e67
-
fad0e67
     pExaPixmap = ExaGetPixmapPriv (pPixmap);
fad0e67
 
fad0e67
     if (pExaPixmap->accel_blocked || pTileExaPixmap->accel_blocked)
fad0e67
     {
fad0e67
-	goto out;
fad0e67
+	return FALSE;
fad0e67
     } else {
fad0e67
 	exaDoMigration (pixmaps, 2, TRUE);
fad0e67
     }
fad0e67
@@ -1140,24 +1169,33 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
fad0e67
     pPixmap = exaGetOffscreenPixmap (pDrawable, &xoff, &yoff);
fad0e67
 
fad0e67
     if (!pPixmap || !exaPixmapIsOffscreen(pTile))
fad0e67
-	goto out;
fad0e67
+	return FALSE;
fad0e67
 
fad0e67
     if ((*pExaScr->info->PrepareCopy) (pTile, pPixmap, 1, 1, alu, planemask))
fad0e67
     {
fad0e67
-	while (nbox--)
fad0e67
+	if (xoff || yoff)
fad0e67
+	    REGION_TRANSLATE(pScreen, pRegion, xoff, yoff);
fad0e67
+
fad0e67
+	for (i = 0; i < nbox; i++)
fad0e67
 	{
fad0e67
-	    int height = pBox->y2 - pBox->y1;
fad0e67
-	    int dstY = pBox->y1;
fad0e67
+	    int height = pBox[i].y2 - pBox[i].y1;
fad0e67
+	    int dstY = pBox[i].y1;
fad0e67
 	    int tileY;
fad0e67
 
fad0e67
+	    if (alu == GXcopy)
fad0e67
+		height = min(height, tileHeight);
fad0e67
+
fad0e67
 	    modulus(dstY - yoff - pDrawable->y - pPatOrg->y, tileHeight, tileY);
fad0e67
 
fad0e67
 	    while (height > 0) {
fad0e67
-		int width = pBox->x2 - pBox->x1;
fad0e67
-		int dstX = pBox->x1;
fad0e67
+		int width = pBox[i].x2 - pBox[i].x1;
fad0e67
+		int dstX = pBox[i].x1;
fad0e67
 		int tileX;
fad0e67
 		int h = tileHeight - tileY;
fad0e67
 
fad0e67
+		if (alu == GXcopy)
fad0e67
+		    width = min(width, tileWidth);
fad0e67
+
fad0e67
 		if (h > height)
fad0e67
 		    h = height;
fad0e67
 		height -= h;
fad0e67
@@ -1179,17 +1217,57 @@ exaFillRegionTiled (DrawablePtr	pDrawable,
fad0e67
 		dstY += h;
fad0e67
 		tileY = 0;
fad0e67
 	    }
fad0e67
-	    pBox++;
fad0e67
 	}
fad0e67
 	(*pExaScr->info->DoneCopy) (pPixmap);
fad0e67
+
fad0e67
+	/* With GXcopy, we only need to do the basic algorithm up to the tile
fad0e67
+	 * size; then, we can just keep doubling the destination in each
fad0e67
+	 * direction until it fills the box. This way, the number of copy
fad0e67
+	 * operations is O(log(rx)) + O(log(ry)) instead of O(rx * ry), where
fad0e67
+	 * rx/ry is the ratio between box and tile width/height. This can make
fad0e67
+	 * a big difference if each driver copy incurs a significant constant
fad0e67
+	 * overhead.
fad0e67
+	 */
fad0e67
+	if (alu != GXcopy)
fad0e67
+	    ret = TRUE;
fad0e67
+	else if ((*pExaScr->info->PrepareCopy) (pPixmap, pPixmap, 1, 1, alu,
fad0e67
+						planemask)) {
fad0e67
+	    for (i = 0; i < nbox; i++)
fad0e67
+	    {
fad0e67
+		int dstX = pBox[i].x1 + tileWidth;
fad0e67
+		int dstY = pBox[i].y1 + tileHeight;
fad0e67
+		int width = min(pBox[i].x2 - dstX, tileWidth);
fad0e67
+		int height = min(pBox[i].y2 - pBox[i].y1, tileHeight);
fad0e67
+
fad0e67
+		while (dstX < pBox[i].x2) {
fad0e67
+		    (*pExaScr->info->Copy) (pPixmap, pBox[i].x1, pBox[i].y1,
fad0e67
+					    dstX, pBox[i].y1, width, height);
fad0e67
+		    dstX += width;
fad0e67
+		    width = min(pBox[i].x2 - dstX, width * 2);
fad0e67
+		}
fad0e67
+
fad0e67
+		width = pBox[i].x2 - pBox[i].x1;
fad0e67
+		height = min(pBox[i].y2 - dstY, tileHeight);
fad0e67
+
fad0e67
+		while (dstY < pBox[i].y2) {
fad0e67
+		    (*pExaScr->info->Copy) (pPixmap, pBox[i].x1, pBox[i].y1,
fad0e67
+					    pBox[i].x1, dstY, width, height);
fad0e67
+		    dstY += height;
fad0e67
+		    height = min(pBox[i].y2 - dstY, height * 2);
fad0e67
+		}
fad0e67
+	    }
fad0e67
+
fad0e67
+	    (*pExaScr->info->DoneCopy) (pPixmap);
fad0e67
+
fad0e67
+	    ret = TRUE;
fad0e67
+	}
fad0e67
+
fad0e67
 	exaMarkSync(pDrawable->pScreen);
fad0e67
 
fad0e67
-	ret = TRUE;
fad0e67
+	if (xoff || yoff)
fad0e67
+	    REGION_TRANSLATE(pScreen, pRegion, -xoff, -yoff);
fad0e67
     }
fad0e67
 
fad0e67
-out:
fad0e67
-    REGION_TRANSLATE(pScreen, pRegion, -xoff, -yoff);
fad0e67
-
fad0e67
     return ret;
fad0e67
 }
fad0e67
 
fad0e67
diff --git a/exa/exa_glyphs.c b/exa/exa_glyphs.c
fad0e67
new file mode 100644
fad0e67
index 0000000..b23e7f6
fad0e67
--- /dev/null
fad0e67
+++ b/exa/exa_glyphs.c
fad0e67
@@ -0,0 +1,897 @@
fad0e67
+/*
fad0e67
+ * Copyright © 2008 Red Hat, Inc.
fad0e67
+ * Partly based on code Copyright © 2000 SuSE, Inc.
fad0e67
+ *
fad0e67
+ * Permission to use, copy, modify, distribute, and sell this software and its
fad0e67
+ * documentation for any purpose is hereby granted without fee, provided that
fad0e67
+ * the above copyright notice appear in all copies and that both that
fad0e67
+ * copyright notice and this permission notice appear in supporting
fad0e67
+ * documentation, and that the name of Red Hat not be used in advertising or
fad0e67
+ * publicity pertaining to distribution of the software without specific,
fad0e67
+ * written prior permission.  Red Hat makes no representations about the
fad0e67
+ * suitability of this software for any purpose.  It is provided "as is"
fad0e67
+ * without express or implied warranty.
fad0e67
+ *
fad0e67
+ * Red Hat DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
fad0e67
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Red Hat
fad0e67
+ * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
fad0e67
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
fad0e67
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
fad0e67
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
fad0e67
+ *
fad0e67
+ * Permission to use, copy, modify, distribute, and sell this software and its
fad0e67
+ * documentation for any purpose is hereby granted without fee, provided that
fad0e67
+ * the above copyright notice appear in all copies and that both that
fad0e67
+ * copyright notice and this permission notice appear in supporting
fad0e67
+ * documentation, and that the name of SuSE not be used in advertising or
fad0e67
+ * publicity pertaining to distribution of the software without specific,
fad0e67
+ * written prior permission.  SuSE makes no representations about the
fad0e67
+ * suitability of this software for any purpose.  It is provided "as is"
fad0e67
+ * without express or implied warranty.
fad0e67
+ *
fad0e67
+ * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
fad0e67
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
fad0e67
+ * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
fad0e67
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
fad0e67
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
fad0e67
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
fad0e67
+ *
fad0e67
+ * Author: Owen Taylor <otaylor@fishsoup.net>
fad0e67
+ * Based on code by: Keith Packard
fad0e67
+ */
fad0e67
+
fad0e67
+#ifdef HAVE_DIX_CONFIG_H
fad0e67
+#include <dix-config.h>
fad0e67
+#endif
fad0e67
+
fad0e67
+#include <stdlib.h>
fad0e67
+
fad0e67
+#include "exa_priv.h"
fad0e67
+
fad0e67
+#include "mipict.h"
fad0e67
+
fad0e67
+#if DEBUG_GLYPH_CACHE
fad0e67
+#define DBG_GLYPH_CACHE(a) ErrorF a
fad0e67
+#else
fad0e67
+#define DBG_GLYPH_CACHE(a)
fad0e67
+#endif
fad0e67
+
fad0e67
+/* Width of the pixmaps we use for the caches; this should be less than
fad0e67
+ * max texture size of the driver; this may need to actually come from
fad0e67
+ * the driver.
fad0e67
+ */
fad0e67
+#define CACHE_PICTURE_WIDTH 1024
fad0e67
+
fad0e67
+/* Maximum number of glyphs we buffer on the stack before flushing
fad0e67
+ * rendering to the mask or destination surface.
fad0e67
+ */
fad0e67
+#define GLYPH_BUFFER_SIZE 256
fad0e67
+
fad0e67
+typedef struct {
fad0e67
+    PicturePtr source;
fad0e67
+    ExaCompositeRectRec rects[GLYPH_BUFFER_SIZE];
fad0e67
+    int count;
fad0e67
+} ExaGlyphBuffer, *ExaGlyphBufferPtr;
fad0e67
+
fad0e67
+typedef enum {
fad0e67
+    ExaGlyphSuccess,    /* Glyph added to render buffer */
fad0e67
+    ExaGlyphFail,       /* out of memory, etc */
fad0e67
+    ExaGlyphNeedFlush,  /* would evict a glyph already in the buffer */
fad0e67
+} ExaGlyphCacheResult;
fad0e67
+
fad0e67
+void
fad0e67
+exaGlyphsInit(ScreenPtr pScreen)
fad0e67
+{
fad0e67
+    ExaScreenPriv(pScreen);
fad0e67
+    int i = 0;
fad0e67
+
fad0e67
+    memset(pExaScr->glyphCaches, 0, sizeof(pExaScr->glyphCaches));
fad0e67
+
fad0e67
+    pExaScr->glyphCaches[i].format = PICT_a8;
fad0e67
+    pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight = 16;
fad0e67
+    i++;
fad0e67
+    pExaScr->glyphCaches[i].format = PICT_a8;
fad0e67
+    pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight = 32;
fad0e67
+    i++;
fad0e67
+    pExaScr->glyphCaches[i].format = PICT_a8r8g8b8;
fad0e67
+    pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight = 16;
fad0e67
+    i++;
fad0e67
+    pExaScr->glyphCaches[i].format = PICT_a8r8g8b8;
fad0e67
+    pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight = 32;
fad0e67
+    i++;
fad0e67
+
fad0e67
+    assert(i == EXA_NUM_GLYPH_CACHES);
fad0e67
+    
fad0e67
+    for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
fad0e67
+	pExaScr->glyphCaches[i].columns = CACHE_PICTURE_WIDTH / pExaScr->glyphCaches[i].glyphWidth;
fad0e67
+	pExaScr->glyphCaches[i].size = 256;
fad0e67
+	pExaScr->glyphCaches[i].hashSize = 557;
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+static void
fad0e67
+exaUnrealizeGlyphCaches(ScreenPtr    pScreen,
fad0e67
+			unsigned int format)
fad0e67
+{
fad0e67
+    ExaScreenPriv(pScreen);
fad0e67
+    int i;
fad0e67
+
fad0e67
+    for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
fad0e67
+	ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
fad0e67
+	
fad0e67
+	if (cache->format != format)
fad0e67
+	    continue;
fad0e67
+
fad0e67
+	if (cache->picture) {
fad0e67
+	    FreePicture ((pointer) cache->picture, (XID) 0);
fad0e67
+	    cache->picture = NULL;
fad0e67
+	}
fad0e67
+
fad0e67
+	if (cache->hashEntries) {
fad0e67
+	    xfree(cache->hashEntries);
fad0e67
+	    cache->hashEntries = NULL;
fad0e67
+	}
fad0e67
+	
fad0e67
+	if (cache->glyphs) {
fad0e67
+	    xfree(cache->glyphs);
fad0e67
+	    cache->glyphs = NULL;
fad0e67
+	}
fad0e67
+	cache->glyphCount = 0;
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+/* All caches for a single format share a single pixmap for glyph storage,
fad0e67
+ * allowing mixing glyphs of different sizes without paying a penalty
fad0e67
+ * for switching between source pixmaps. (Note that for a size of font
fad0e67
+ * right at the border between two sizes, we might be switching for almost
fad0e67
+ * every glyph.)
fad0e67
+ *
fad0e67
+ * This function allocates the storage pixmap, and then fills in the
fad0e67
+ * rest of the allocated structures for all caches with the given format.
fad0e67
+ */
fad0e67
+static Bool
fad0e67
+exaRealizeGlyphCaches(ScreenPtr    pScreen,
fad0e67
+		      unsigned int format)
fad0e67
+{
fad0e67
+    ExaScreenPriv(pScreen);
fad0e67
+
fad0e67
+    int depth = PIXMAN_FORMAT_DEPTH(format);
fad0e67
+    PictFormatPtr pPictFormat;
fad0e67
+    PixmapPtr pPixmap;
fad0e67
+    PicturePtr pPicture;
fad0e67
+    int height;
fad0e67
+    int i;
fad0e67
+    int	error;
fad0e67
+
fad0e67
+    pPictFormat = PictureMatchFormat(pScreen, depth, format);
fad0e67
+    if (!pPictFormat)
fad0e67
+	return FALSE;
fad0e67
+    
fad0e67
+    /* Compute the total vertical size needed for the format */
fad0e67
+
fad0e67
+    height = 0;
fad0e67
+    for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
fad0e67
+	ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
fad0e67
+	int rows;
fad0e67
+
fad0e67
+	if (cache->format != format)
fad0e67
+	    continue;
fad0e67
+
fad0e67
+	cache->yOffset = height;
fad0e67
+
fad0e67
+	rows = (cache->size + cache->columns - 1) / cache->columns;
fad0e67
+	height += rows * cache->glyphHeight;
fad0e67
+    }
fad0e67
+
fad0e67
+    /* Now allocate the pixmap and picture */
fad0e67
+       
fad0e67
+    pPixmap = (*pScreen->CreatePixmap) (pScreen,
fad0e67
+					CACHE_PICTURE_WIDTH,
fad0e67
+					height, depth, 0);
fad0e67
+    if (!pPixmap)
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    pPicture = CreatePicture(0, &pPixmap->drawable, pPictFormat,
fad0e67
+			     0, 0, serverClient, &error);
fad0e67
+
fad0e67
+    (*pScreen->DestroyPixmap) (pPixmap); /* picture holds a refcount */
fad0e67
+
fad0e67
+    if (!pPicture)
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    /* And store the picture in all the caches for the format */
fad0e67
+    
fad0e67
+    for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
fad0e67
+	ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
fad0e67
+	int j;
fad0e67
+
fad0e67
+	if (cache->format != format)
fad0e67
+	    continue;
fad0e67
+
fad0e67
+	cache->picture = pPicture;
fad0e67
+	cache->picture->refcnt++;
fad0e67
+	cache->hashEntries = xalloc(sizeof(int) * cache->hashSize);
fad0e67
+	cache->glyphs = xalloc(sizeof(ExaCachedGlyphRec) * cache->size);
fad0e67
+	cache->glyphCount = 0;
fad0e67
+
fad0e67
+	if (!cache->hashEntries || !cache->glyphs)
fad0e67
+	    goto bail;
fad0e67
+
fad0e67
+	for (j = 0; j < cache->hashSize; j++)
fad0e67
+	    cache->hashEntries[j] = -1;
fad0e67
+	
fad0e67
+	cache->evictionPosition = rand() % cache->size;
fad0e67
+    }
fad0e67
+
fad0e67
+    /* Each cache references the picture individually */
fad0e67
+    FreePicture ((pointer) pPicture, (XID) 0);
fad0e67
+    return TRUE;
fad0e67
+
fad0e67
+bail:
fad0e67
+    exaUnrealizeGlyphCaches(pScreen, format);
fad0e67
+    return FALSE;
fad0e67
+}
fad0e67
+
fad0e67
+void
fad0e67
+exaGlyphsFini (ScreenPtr pScreen)
fad0e67
+{
fad0e67
+    ExaScreenPriv(pScreen);
fad0e67
+    int i;
fad0e67
+
fad0e67
+    for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
fad0e67
+	ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
fad0e67
+
fad0e67
+	if (cache->picture)
fad0e67
+	    exaUnrealizeGlyphCaches(pScreen, cache->format);
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+static int
fad0e67
+exaGlyphCacheHashLookup(ExaGlyphCachePtr cache,
fad0e67
+			GlyphPtr         pGlyph)
fad0e67
+{
fad0e67
+    int slot;
fad0e67
+
fad0e67
+    slot = (*(CARD32 *) pGlyph->sha1) % cache->hashSize;
fad0e67
+    
fad0e67
+    while (TRUE) { /* hash table can never be full */
fad0e67
+	int entryPos = cache->hashEntries[slot];
fad0e67
+	if (entryPos == -1)
fad0e67
+	    return -1;
fad0e67
+
fad0e67
+	if (memcmp(pGlyph->sha1, cache->glyphs[entryPos].sha1, sizeof(pGlyph->sha1)) == 0){
fad0e67
+	    return entryPos;
fad0e67
+	}
fad0e67
+	    
fad0e67
+	slot--;
fad0e67
+	if (slot < 0)
fad0e67
+	    slot = cache->hashSize - 1;
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+static void
fad0e67
+exaGlyphCacheHashInsert(ExaGlyphCachePtr cache,
fad0e67
+			GlyphPtr         pGlyph,
fad0e67
+			int              pos)
fad0e67
+{
fad0e67
+    int slot;
fad0e67
+
fad0e67
+    memcpy(cache->glyphs[pos].sha1, pGlyph->sha1, sizeof(pGlyph->sha1));
fad0e67
+    
fad0e67
+    slot = (*(CARD32 *) pGlyph->sha1) % cache->hashSize;
fad0e67
+    
fad0e67
+    while (TRUE) { /* hash table can never be full */
fad0e67
+	if (cache->hashEntries[slot] == -1) {
fad0e67
+	    cache->hashEntries[slot] = pos;
fad0e67
+	    return;
fad0e67
+	}
fad0e67
+	    
fad0e67
+	slot--;
fad0e67
+	if (slot < 0)
fad0e67
+	    slot = cache->hashSize - 1;
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+static void
fad0e67
+exaGlyphCacheHashRemove(ExaGlyphCachePtr cache,
fad0e67
+			int              pos)
fad0e67
+{
fad0e67
+    int slot;
fad0e67
+    int emptiedSlot = -1;
fad0e67
+
fad0e67
+    slot = (*(CARD32 *) cache->glyphs[pos].sha1) % cache->hashSize;
fad0e67
+
fad0e67
+    while (TRUE) { /* hash table can never be full */
fad0e67
+	int entryPos = cache->hashEntries[slot];
fad0e67
+	
fad0e67
+	if (entryPos == -1)
fad0e67
+	    return;
fad0e67
+
fad0e67
+	if (entryPos == pos) {
fad0e67
+	    cache->hashEntries[slot] = -1;
fad0e67
+	    emptiedSlot = slot;
fad0e67
+	} else if (emptiedSlot != -1) {
fad0e67
+	    /* See if we can move this entry into the emptied slot, we can't
fad0e67
+	     * do that if if entry would have hashed between the current position
fad0e67
+	     * and the emptied slot. (taking wrapping into account). Bad positions
fad0e67
+	     * are:
fad0e67
+	     *
fad0e67
+	     * |   XXXXXXXXXX             |
fad0e67
+	     *     i         j            
fad0e67
+	     *                            
fad0e67
+	     * |XXX                   XXXX|
fad0e67
+	     *     j                  i
fad0e67
+	     *
fad0e67
+	     * i - slot, j - emptiedSlot
fad0e67
+	     *
fad0e67
+	     * (Knuth 6.4R)
fad0e67
+	     */
fad0e67
+	    
fad0e67
+	    int entrySlot = (*(CARD32 *) cache->glyphs[entryPos].sha1) % cache->hashSize;
fad0e67
+
fad0e67
+	    if (!((entrySlot >= slot && entrySlot < emptiedSlot) ||
fad0e67
+		  (emptiedSlot < slot && (entrySlot < emptiedSlot || entrySlot >= slot)))) 
fad0e67
+	    {
fad0e67
+		cache->hashEntries[emptiedSlot] = entryPos;
fad0e67
+		cache->hashEntries[slot] = -1;
fad0e67
+		emptiedSlot = slot;
fad0e67
+	    }
fad0e67
+	}
fad0e67
+	
fad0e67
+	slot--;
fad0e67
+	if (slot < 0)
fad0e67
+	    slot = cache->hashSize - 1;
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+#define CACHE_X(pos) (((pos) % cache->columns) * cache->glyphWidth)
fad0e67
+#define CACHE_Y(pos) (cache->yOffset + ((pos) / cache->columns) * cache->glyphHeight)
fad0e67
+
fad0e67
+/* The most efficient thing to way to upload the glyph to the screen
fad0e67
+ * is to use the UploadToScreen() driver hook; this allows us to
fad0e67
+ * pipeline glyph uploads and to avoid creating offscreen pixmaps for
fad0e67
+ * glyphs that we'll never use again.
fad0e67
+ */
fad0e67
+static Bool
fad0e67
+exaGlyphCacheUploadGlyph(ScreenPtr         pScreen,
fad0e67
+			 ExaGlyphCachePtr  cache,
fad0e67
+			 int               pos,
fad0e67
+			 GlyphPtr          pGlyph)
fad0e67
+{
fad0e67
+    ExaScreenPriv(pScreen);
fad0e67
+    PicturePtr pGlyphPicture = GlyphPicture(pGlyph)[pScreen->myNum];
fad0e67
+    PixmapPtr pGlyphPixmap = (PixmapPtr)pGlyphPicture->pDrawable;
fad0e67
+    ExaPixmapPriv(pGlyphPixmap);
fad0e67
+    PixmapPtr pCachePixmap = (PixmapPtr)cache->picture->pDrawable;
fad0e67
+    ExaMigrationRec pixmaps[1];
fad0e67
+
fad0e67
+    if (!pExaScr->info->UploadToScreen || pExaScr->swappedOut || pExaPixmap->accel_blocked)
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    /* If the glyph pixmap is already uploaded, no point in doing
fad0e67
+     * things this way */
fad0e67
+    if (exaPixmapIsOffscreen(pGlyphPixmap))
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    /* UploadToScreen only works if bpp match */
fad0e67
+    if (pGlyphPixmap->drawable.bitsPerPixel != pCachePixmap->drawable.bitsPerPixel)
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    /* cache pixmap must be offscreen. */
fad0e67
+    pixmaps[0].as_dst = TRUE;
fad0e67
+    pixmaps[0].as_src = FALSE;
fad0e67
+    pixmaps[0].pPix = pCachePixmap;
fad0e67
+    pixmaps[0].pReg = NULL;
fad0e67
+    exaDoMigration (pixmaps, 1, TRUE);
fad0e67
+
fad0e67
+    if (!exaPixmapIsOffscreen(pCachePixmap))
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    /* CACHE_{X,Y} are in pixmap coordinates, no need for cache{X,Y}off */
fad0e67
+    if (!pExaScr->info->UploadToScreen(pCachePixmap,
fad0e67
+				       CACHE_X(pos),
fad0e67
+				       CACHE_Y(pos),
fad0e67
+				       pGlyph->info.width,
fad0e67
+				       pGlyph->info.height,
fad0e67
+				       (char *)pExaPixmap->sys_ptr,
fad0e67
+				       pExaPixmap->sys_pitch))
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    /* This pixmap should never be bound to a window, so no need to offset coordinates. */
fad0e67
+    exaPixmapDirty (pCachePixmap,
fad0e67
+		    CACHE_X(pos),
fad0e67
+		    CACHE_Y(pos),
fad0e67
+		    CACHE_X(pos) + pGlyph->info.width,
fad0e67
+		    CACHE_Y(pos) + pGlyph->info.height);
fad0e67
+
fad0e67
+    return TRUE;
fad0e67
+}
fad0e67
+
fad0e67
+static ExaGlyphCacheResult
fad0e67
+exaGlyphCacheBufferGlyph(ScreenPtr         pScreen,
fad0e67
+			 ExaGlyphCachePtr  cache,
fad0e67
+			 ExaGlyphBufferPtr buffer,
fad0e67
+			 GlyphPtr          pGlyph,
fad0e67
+			 int               xGlyph,
fad0e67
+			 int               yGlyph)
fad0e67
+{
fad0e67
+    ExaCompositeRectPtr rect;
fad0e67
+    int pos;
fad0e67
+    
fad0e67
+    if (buffer->source && buffer->source != cache->picture)
fad0e67
+	return ExaGlyphNeedFlush;
fad0e67
+
fad0e67
+    if (!cache->picture) {
fad0e67
+	if (!exaRealizeGlyphCaches(pScreen, cache->format))
fad0e67
+	    return ExaGlyphFail;
fad0e67
+    }
fad0e67
+
fad0e67
+    DBG_GLYPH_CACHE(("(%d,%d,%s): buffering glyph %lx\n",
fad0e67
+		     cache->glyphWidth, cache->glyphHeight, cache->format == PICT_a8 ? "A" : "ARGB",
fad0e67
+		     (long)*(CARD32 *) pGlyph->sha1));
fad0e67
+   
fad0e67
+    pos = exaGlyphCacheHashLookup(cache, pGlyph);
fad0e67
+    if (pos != -1) {
fad0e67
+	DBG_GLYPH_CACHE(("  found existing glyph at %d\n", pos));
fad0e67
+    } else {
fad0e67
+	if (cache->glyphCount < cache->size) {
fad0e67
+	    /* Space remaining; we fill from the start */
fad0e67
+	    pos = cache->glyphCount;
fad0e67
+	    cache->glyphCount++;
fad0e67
+	    DBG_GLYPH_CACHE(("  storing glyph in free space at %d\n", pos));
fad0e67
+
fad0e67
+	    exaGlyphCacheHashInsert(cache, pGlyph, pos);
fad0e67
+
fad0e67
+	} else {
fad0e67
+	    /* Need to evict an entry. We have to see if any glyphs
fad0e67
+	     * already in the output buffer were at this position in
fad0e67
+	     * the cache
fad0e67
+	     */
fad0e67
+	    
fad0e67
+	    pos = cache->evictionPosition;
fad0e67
+	    DBG_GLYPH_CACHE(("  evicting glyph at %d\n", pos));
fad0e67
+	    if (buffer->count) {
fad0e67
+		int x, y;
fad0e67
+		int i;
fad0e67
+		
fad0e67
+		x = CACHE_X(pos);
fad0e67
+		y = CACHE_Y(pos);
fad0e67
+
fad0e67
+		for (i = 0; i < buffer->count; i++) {
fad0e67
+		    if (buffer->rects[i].xSrc == x && buffer->rects[i].ySrc == y) {
fad0e67
+			DBG_GLYPH_CACHE(("  must flush buffer\n"));
fad0e67
+			return ExaGlyphNeedFlush;
fad0e67
+		    }
fad0e67
+		}
fad0e67
+	    }
fad0e67
+
fad0e67
+	    /* OK, we're all set, swap in the new glyph */
fad0e67
+	    exaGlyphCacheHashRemove(cache, pos);
fad0e67
+	    exaGlyphCacheHashInsert(cache, pGlyph, pos);
fad0e67
+
fad0e67
+	    /* And pick a new eviction position */
fad0e67
+	    cache->evictionPosition = rand() % cache->size;
fad0e67
+	}
fad0e67
+
fad0e67
+	/* Now actually upload the glyph into the cache picture; if
fad0e67
+	 * we can't do it with UploadToScreen (because the glyph is
fad0e67
+	 * offscreen, etc), we fall back to CompositePicture.
fad0e67
+	 */
fad0e67
+	if (!exaGlyphCacheUploadGlyph(pScreen, cache, pos, pGlyph)) {
fad0e67
+	    CompositePicture (PictOpSrc,
fad0e67
+			      GlyphPicture(pGlyph)[pScreen->myNum],
fad0e67
+			      None,
fad0e67
+			      cache->picture,
fad0e67
+			      0, 0,
fad0e67
+			      0, 0,
fad0e67
+			      CACHE_X(pos),
fad0e67
+			      CACHE_Y(pos),
fad0e67
+			      pGlyph->info.width,
fad0e67
+			      pGlyph->info.height);
fad0e67
+	}
fad0e67
+
fad0e67
+    }
fad0e67
+    
fad0e67
+
fad0e67
+    buffer->source = cache->picture;
fad0e67
+	    
fad0e67
+    rect = &buffer->rects[buffer->count];
fad0e67
+    rect->xSrc = CACHE_X(pos);
fad0e67
+    rect->ySrc = CACHE_Y(pos);
fad0e67
+    rect->xDst = xGlyph - pGlyph->info.x;
fad0e67
+    rect->yDst = yGlyph - pGlyph->info.y;
fad0e67
+    rect->width = pGlyph->info.width;
fad0e67
+    rect->height = pGlyph->info.height;
fad0e67
+	    
fad0e67
+    buffer->count++;
fad0e67
+
fad0e67
+    return ExaGlyphSuccess;
fad0e67
+}
fad0e67
+
fad0e67
+#undef CACHE_X
fad0e67
+#undef CACHE_Y
fad0e67
+
fad0e67
+static ExaGlyphCacheResult
fad0e67
+exaBufferGlyph(ScreenPtr         pScreen,
fad0e67
+	       ExaGlyphBufferPtr buffer,
fad0e67
+	       GlyphPtr          pGlyph,
fad0e67
+	       int               xGlyph,
fad0e67
+	       int               yGlyph)
fad0e67
+{
fad0e67
+    ExaScreenPriv(pScreen);
fad0e67
+    unsigned int format = (GlyphPicture(pGlyph)[pScreen->myNum])->format;
fad0e67
+    int width = pGlyph->info.width;
fad0e67
+    int height = pGlyph->info.height;
fad0e67
+    ExaCompositeRectPtr rect;
fad0e67
+    PicturePtr source;
fad0e67
+    int i;
fad0e67
+
fad0e67
+    if (buffer->count == GLYPH_BUFFER_SIZE)
fad0e67
+	return ExaGlyphNeedFlush;
fad0e67
+
fad0e67
+    if (PICT_FORMAT_BPP(format) == 1)
fad0e67
+	format = PICT_a8;
fad0e67
+    
fad0e67
+    for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
fad0e67
+	ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
fad0e67
+
fad0e67
+	if (format == cache->format &&
fad0e67
+	    width <= cache->glyphWidth &&
fad0e67
+	    height <= cache->glyphHeight) {
fad0e67
+	    ExaGlyphCacheResult result = exaGlyphCacheBufferGlyph(pScreen, &pExaScr->glyphCaches[i],
fad0e67
+								  buffer,
fad0e67
+								  pGlyph, xGlyph, yGlyph);
fad0e67
+	    switch (result) {
fad0e67
+	    case ExaGlyphFail:
fad0e67
+		break;
fad0e67
+	    case ExaGlyphSuccess:
fad0e67
+	    case ExaGlyphNeedFlush:
fad0e67
+		return result;
fad0e67
+	    }
fad0e67
+	}
fad0e67
+    }
fad0e67
+
fad0e67
+    /* Couldn't find the glyph in the cache, use the glyph picture directly */
fad0e67
+
fad0e67
+    source = GlyphPicture(pGlyph)[pScreen->myNum];
fad0e67
+    if (buffer->source && buffer->source != source)
fad0e67
+	return ExaGlyphNeedFlush;
fad0e67
+
fad0e67
+    buffer->source = source;
fad0e67
+    
fad0e67
+    rect = &buffer->rects[buffer->count];
fad0e67
+    rect->xSrc = 0;
fad0e67
+    rect->ySrc = 0;
fad0e67
+    rect->xDst = xGlyph - pGlyph->info.x;
fad0e67
+    rect->yDst = yGlyph - pGlyph->info.y;
fad0e67
+    rect->width = pGlyph->info.width;
fad0e67
+    rect->height = pGlyph->info.height;
fad0e67
+
fad0e67
+    buffer->count++;
fad0e67
+
fad0e67
+    return ExaGlyphSuccess;
fad0e67
+}
fad0e67
+
fad0e67
+static void
fad0e67
+exaGlyphsToMask(PicturePtr        pMask,
fad0e67
+		ExaGlyphBufferPtr buffer)
fad0e67
+{
fad0e67
+    exaCompositeRects(PictOpAdd, buffer->source, pMask,
fad0e67
+		      buffer->count, buffer->rects);
fad0e67
+    
fad0e67
+    buffer->count = 0;
fad0e67
+    buffer->source = NULL;
fad0e67
+}
fad0e67
+
fad0e67
+static void
fad0e67
+exaGlyphsToDst(CARD8		 op,
fad0e67
+	       PicturePtr	 pSrc,
fad0e67
+	       PicturePtr	 pDst,
fad0e67
+	       ExaGlyphBufferPtr buffer,
fad0e67
+	       INT16		 xSrc,
fad0e67
+	       INT16		 ySrc,
fad0e67
+	       INT16		 xDst,
fad0e67
+	       INT16		 yDst)
fad0e67
+{
fad0e67
+    int i;
fad0e67
+
fad0e67
+    for (i = 0; i < buffer->count; i++) {
fad0e67
+	ExaCompositeRectPtr rect = &buffer->rects[i];
fad0e67
+	
fad0e67
+	CompositePicture (op,
fad0e67
+			  pSrc,
fad0e67
+			  buffer->source,
fad0e67
+			  pDst,
fad0e67
+			  xSrc + rect->xDst - xDst,
fad0e67
+			  ySrc + rect->yDst - yDst,
fad0e67
+			  rect->xSrc,
fad0e67
+			  rect->ySrc,
fad0e67
+			  rect->xDst,
fad0e67
+			  rect->yDst,
fad0e67
+			  rect->width,
fad0e67
+			  rect->height);
fad0e67
+    }
fad0e67
+    
fad0e67
+    buffer->count = 0;
fad0e67
+    buffer->source = NULL;
fad0e67
+}
fad0e67
+
fad0e67
+/* Cut and paste from render/glyph.c - probably should export it instead */
fad0e67
+static void
fad0e67
+GlyphExtents (int		nlist,
fad0e67
+	      GlyphListPtr	list,
fad0e67
+	      GlyphPtr	       *glyphs,
fad0e67
+	      BoxPtr		extents)
fad0e67
+{
fad0e67
+    int		x1, x2, y1, y2;
fad0e67
+    int		n;
fad0e67
+    GlyphPtr	glyph;
fad0e67
+    int		x, y;
fad0e67
+    
fad0e67
+    x = 0;
fad0e67
+    y = 0;
fad0e67
+    extents->x1 = MAXSHORT;
fad0e67
+    extents->x2 = MINSHORT;
fad0e67
+    extents->y1 = MAXSHORT;
fad0e67
+    extents->y2 = MINSHORT;
fad0e67
+    while (nlist--)
fad0e67
+    {
fad0e67
+	x += list->xOff;
fad0e67
+	y += list->yOff;
fad0e67
+	n = list->len;
fad0e67
+	list++;
fad0e67
+	while (n--)
fad0e67
+	{
fad0e67
+	    glyph = *glyphs++;
fad0e67
+	    x1 = x - glyph->info.x;
fad0e67
+	    if (x1 < MINSHORT)
fad0e67
+		x1 = MINSHORT;
fad0e67
+	    y1 = y - glyph->info.y;
fad0e67
+	    if (y1 < MINSHORT)
fad0e67
+		y1 = MINSHORT;
fad0e67
+	    x2 = x1 + glyph->info.width;
fad0e67
+	    if (x2 > MAXSHORT)
fad0e67
+		x2 = MAXSHORT;
fad0e67
+	    y2 = y1 + glyph->info.height;
fad0e67
+	    if (y2 > MAXSHORT)
fad0e67
+		y2 = MAXSHORT;
fad0e67
+	    if (x1 < extents->x1)
fad0e67
+		extents->x1 = x1;
fad0e67
+	    if (x2 > extents->x2)
fad0e67
+		extents->x2 = x2;
fad0e67
+	    if (y1 < extents->y1)
fad0e67
+		extents->y1 = y1;
fad0e67
+	    if (y2 > extents->y2)
fad0e67
+		extents->y2 = y2;
fad0e67
+	    x += glyph->info.xOff;
fad0e67
+	    y += glyph->info.yOff;
fad0e67
+	}
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+/**
fad0e67
+ * Returns TRUE if the glyphs in the lists intersect.  Only checks based on
fad0e67
+ * bounding box, which appears to be good enough to catch most cases at least.
fad0e67
+ */
fad0e67
+static Bool
fad0e67
+exaGlyphsIntersect(int nlist, GlyphListPtr list, GlyphPtr *glyphs)
fad0e67
+{
fad0e67
+    int x1, x2, y1, y2;
fad0e67
+    int n;
fad0e67
+    GlyphPtr glyph;
fad0e67
+    int x, y;
fad0e67
+    BoxRec extents;
fad0e67
+    Bool first = TRUE;
fad0e67
+
fad0e67
+    x = 0;
fad0e67
+    y = 0;
fad0e67
+    while (nlist--) {
fad0e67
+       x += list->xOff;
fad0e67
+       y += list->yOff;
fad0e67
+       n = list->len;
fad0e67
+       list++;
fad0e67
+       while (n--) {
fad0e67
+           glyph = *glyphs++;
fad0e67
+
fad0e67
+           if (glyph->info.width == 0 || glyph->info.height == 0) {
fad0e67
+               x += glyph->info.xOff;
fad0e67
+               y += glyph->info.yOff;
fad0e67
+               continue;
fad0e67
+           }
fad0e67
+
fad0e67
+           x1 = x - glyph->info.x;
fad0e67
+           if (x1 < MINSHORT)
fad0e67
+               x1 = MINSHORT;
fad0e67
+           y1 = y - glyph->info.y;
fad0e67
+           if (y1 < MINSHORT)
fad0e67
+               y1 = MINSHORT;
fad0e67
+           x2 = x1 + glyph->info.width;
fad0e67
+           if (x2 > MAXSHORT)
fad0e67
+               x2 = MAXSHORT;
fad0e67
+           y2 = y1 + glyph->info.height;
fad0e67
+           if (y2 > MAXSHORT)
fad0e67
+               y2 = MAXSHORT;
fad0e67
+
fad0e67
+           if (first) {
fad0e67
+               extents.x1 = x1;
fad0e67
+               extents.y1 = y1;
fad0e67
+               extents.x2 = x2;
fad0e67
+               extents.y2 = y2;
fad0e67
+               first = FALSE;
fad0e67
+           } else {
fad0e67
+               if (x1 < extents.x2 && x2 > extents.x1 &&
fad0e67
+                   y1 < extents.y2 && y2 > extents.y1)
fad0e67
+               {
fad0e67
+                   return TRUE;
fad0e67
+               }
fad0e67
+
fad0e67
+               if (x1 < extents.x1)
fad0e67
+                  extents.x1 = x1;
fad0e67
+               if (x2 > extents.x2)
fad0e67
+                   extents.x2 = x2;
fad0e67
+               if (y1 < extents.y1)
fad0e67
+                   extents.y1 = y1;
fad0e67
+               if (y2 > extents.y2)
fad0e67
+                   extents.y2 = y2;
fad0e67
+           }
fad0e67
+           x += glyph->info.xOff;
fad0e67
+           y += glyph->info.yOff;
fad0e67
+       }
fad0e67
+    }
fad0e67
+
fad0e67
+    return FALSE;
fad0e67
+}
fad0e67
+
fad0e67
+#define NeedsComponent(f) (PICT_FORMAT_A(f) != 0 && PICT_FORMAT_RGB(f) != 0)
fad0e67
+
fad0e67
+void
fad0e67
+exaGlyphs (CARD8 	 op,
fad0e67
+	   PicturePtr	 pSrc,
fad0e67
+	   PicturePtr	 pDst,
fad0e67
+	   PictFormatPtr maskFormat,
fad0e67
+	   INT16	 xSrc,
fad0e67
+	   INT16	 ySrc,
fad0e67
+	   int		 nlist,
fad0e67
+	   GlyphListPtr	 list,
fad0e67
+	   GlyphPtr	*glyphs)
fad0e67
+{
fad0e67
+    PicturePtr	pPicture;
fad0e67
+    PixmapPtr   pMaskPixmap = 0;
fad0e67
+    PicturePtr  pMask;
fad0e67
+    ScreenPtr   pScreen = pDst->pDrawable->pScreen;
fad0e67
+    int		width = 0, height = 0;
fad0e67
+    int		x, y;
fad0e67
+    int		xDst = list->xOff, yDst = list->yOff;
fad0e67
+    int		n;
fad0e67
+    GlyphPtr	glyph;
fad0e67
+    int		error;
fad0e67
+    BoxRec	extents = {0, 0, 0, 0};
fad0e67
+    CARD32	component_alpha;
fad0e67
+    ExaGlyphBuffer buffer;
fad0e67
+
fad0e67
+    /* If we don't have a mask format but all the glyphs have the same format
fad0e67
+     * and don't intersect, use the glyph format as mask format for the full
fad0e67
+     * benefits of the glyph cache.
fad0e67
+     */
fad0e67
+    if (!maskFormat) {
fad0e67
+       Bool sameFormat = TRUE;
fad0e67
+       int i;
fad0e67
+
fad0e67
+       maskFormat = list[0].format;
fad0e67
+
fad0e67
+       for (i = 0; i < nlist; i++) {
fad0e67
+           if (maskFormat->format != list[i].format->format) {
fad0e67
+               sameFormat = FALSE;
fad0e67
+               break;
fad0e67
+           }
fad0e67
+       }
fad0e67
+
fad0e67
+       if (!sameFormat || (maskFormat->depth != 1 &&
fad0e67
+			   exaGlyphsIntersect(nlist, list, glyphs))) {
fad0e67
+	   maskFormat = NULL;
fad0e67
+       }
fad0e67
+    }
fad0e67
+
fad0e67
+    if (maskFormat)
fad0e67
+    {
fad0e67
+	GCPtr	    pGC;
fad0e67
+	xRectangle  rect;
fad0e67
+
fad0e67
+	GlyphExtents (nlist, list, glyphs, &extents);
fad0e67
+
fad0e67
+	if (extents.x2 <= extents.x1 || extents.y2 <= extents.y1)
fad0e67
+	    return;
fad0e67
+	width = extents.x2 - extents.x1;
fad0e67
+	height = extents.y2 - extents.y1;
fad0e67
+
fad0e67
+	if (maskFormat->depth == 1) {
fad0e67
+	    PictFormatPtr a8Format = PictureMatchFormat (pScreen, 8, PICT_a8);
fad0e67
+
fad0e67
+	    if (a8Format)
fad0e67
+		maskFormat = a8Format;
fad0e67
+	}
fad0e67
+
fad0e67
+	pMaskPixmap = (*pScreen->CreatePixmap) (pScreen, width, height,
fad0e67
+						maskFormat->depth,
fad0e67
+						CREATE_PIXMAP_USAGE_SCRATCH);
fad0e67
+	if (!pMaskPixmap)
fad0e67
+	    return;
fad0e67
+	component_alpha = NeedsComponent(maskFormat->format);
fad0e67
+	pMask = CreatePicture (0, &pMaskPixmap->drawable,
fad0e67
+			       maskFormat, CPComponentAlpha, &component_alpha,
fad0e67
+			       serverClient, &error);
fad0e67
+	if (!pMask)
fad0e67
+	{
fad0e67
+	    (*pScreen->DestroyPixmap) (pMaskPixmap);
fad0e67
+	    return;
fad0e67
+	}
fad0e67
+	pGC = GetScratchGC (pMaskPixmap->drawable.depth, pScreen);
fad0e67
+	ValidateGC (&pMaskPixmap->drawable, pGC);
fad0e67
+	rect.x = 0;
fad0e67
+	rect.y = 0;
fad0e67
+	rect.width = width;
fad0e67
+	rect.height = height;
fad0e67
+	(*pGC->ops->PolyFillRect) (&pMaskPixmap->drawable, pGC, 1, &rect);
fad0e67
+	FreeScratchGC (pGC);
fad0e67
+	x = -extents.x1;
fad0e67
+	y = -extents.y1;
fad0e67
+    }
fad0e67
+    else
fad0e67
+    {
fad0e67
+	pMask = pDst;
fad0e67
+	x = 0;
fad0e67
+	y = 0;
fad0e67
+    }
fad0e67
+    buffer.count = 0;
fad0e67
+    buffer.source = NULL;
fad0e67
+    while (nlist--)
fad0e67
+    {
fad0e67
+	x += list->xOff;
fad0e67
+	y += list->yOff;
fad0e67
+	n = list->len;
fad0e67
+	while (n--)
fad0e67
+	{
fad0e67
+	    glyph = *glyphs++;
fad0e67
+	    pPicture = GlyphPicture (glyph)[pScreen->myNum];
fad0e67
+
fad0e67
+	    if (glyph->info.width > 0 && glyph->info.height > 0 &&
fad0e67
+		exaBufferGlyph(pScreen, &buffer, glyph, x, y) == ExaGlyphNeedFlush)
fad0e67
+	    {
fad0e67
+		if (maskFormat)
fad0e67
+		    exaGlyphsToMask(pMask, &buffer);
fad0e67
+		else
fad0e67
+		    exaGlyphsToDst(op, pSrc, pDst, &buffer,
fad0e67
+				   xSrc, ySrc, xDst, yDst);
fad0e67
+
fad0e67
+		exaBufferGlyph(pScreen, &buffer, glyph, x, y);
fad0e67
+	    }
fad0e67
+
fad0e67
+	    x += glyph->info.xOff;
fad0e67
+	    y += glyph->info.yOff;
fad0e67
+	}
fad0e67
+	list++;
fad0e67
+    }
fad0e67
+    
fad0e67
+    if (maskFormat)
fad0e67
+	exaGlyphsToMask(pMask, &buffer);
fad0e67
+    else
fad0e67
+	exaGlyphsToDst(op, pSrc, pDst, &buffer,
fad0e67
+		       xSrc, ySrc, xDst, yDst);
fad0e67
+    
fad0e67
+    if (maskFormat)
fad0e67
+    {
fad0e67
+	x = extents.x1;
fad0e67
+	y = extents.y1;
fad0e67
+	CompositePicture (op,
fad0e67
+			  pSrc,
fad0e67
+			  pMask,
fad0e67
+			  pDst,
fad0e67
+			  xSrc + x - xDst,
fad0e67
+			  ySrc + y - yDst,
fad0e67
+			  0, 0,
fad0e67
+			  x, y,
fad0e67
+			  width, height);
fad0e67
+	FreePicture ((pointer) pMask, (XID) 0);
fad0e67
+	(*pScreen->DestroyPixmap) (pMaskPixmap);
fad0e67
+    }
fad0e67
+}
fad0e67
diff --git a/exa/exa_migration.c b/exa/exa_migration.c
ff0bb53
index b7cc062..8f000e9 100644
fad0e67
--- a/exa/exa_migration.c
fad0e67
+++ b/exa/exa_migration.c
fad0e67
@@ -33,8 +33,6 @@
fad0e67
 #include <string.h>
fad0e67
 
fad0e67
 #include "exa_priv.h"
fad0e67
-#include <X11/fonts/fontstruct.h>
fad0e67
-#include "dixfontstr.h"
fad0e67
 #include "exa.h"
fad0e67
 #include "cw.h"
fad0e67
 
fad0e67
@@ -45,6 +43,39 @@
fad0e67
 #endif
fad0e67
 
fad0e67
 /**
fad0e67
+ * Returns TRUE if the pixmap has damage.
fad0e67
+ * EXA only migrates the parts of a destination 
fad0e67
+ * that are affected by rendering.
fad0e67
+ * It uses the current damage as indication.
fad0e67
+ * So anything that does not need to be updated won't be.
fad0e67
+ * For clarity this seperate function was made.
fad0e67
+ * Note that some situations don't use this, 
fad0e67
+ * because their calls are wrapped by the damage layer.
fad0e67
+ */
fad0e67
+Bool
fad0e67
+exaDamageDestForMigration(DrawablePtr pDrawable, PixmapPtr pPix, RegionPtr region)
fad0e67
+{
fad0e67
+    ScreenPtr pScreen = pDrawable->pScreen;
fad0e67
+    (void) pScreen; /* the macros don't use pScreen currently */
fad0e67
+    ExaPixmapPriv (pPix);
fad0e67
+    int x_offset, y_offset;
fad0e67
+    RegionPtr pending_damage;
fad0e67
+
fad0e67
+    if (!pExaPixmap->pDamage)
fad0e67
+	return FALSE;
fad0e67
+
fad0e67
+    exaGetDrawableDeltas(pDrawable, pPix, &x_offset, &y_offset);
fad0e67
+
fad0e67
+    REGION_TRANSLATE(pScreen, region, x_offset, y_offset);
fad0e67
+    pending_damage = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
+    REGION_UNION(pScreen, pending_damage, pending_damage, region);
fad0e67
+    /* Restore region as we got it. */
fad0e67
+    REGION_TRANSLATE(pScreen, region, -x_offset, -y_offset);
fad0e67
+
fad0e67
+    return TRUE;
fad0e67
+}
fad0e67
+
fad0e67
+/**
fad0e67
  * Returns TRUE if the pixmap is not movable.  This is the case where it's a
fad0e67
  * fake pixmap for the frontbuffer (no pixmap private) or it's a scratch
fad0e67
  * pixmap created by some other X Server internals (the score says it's
ff0bb53
@@ -212,9 +243,9 @@ exaCopyDirty(ExaMigrationPtr migrate, RegionPtr pValidDst, RegionPtr pValidSrc,
fad0e67
 				    pBox->x1, pBox->y1,
fad0e67
 				    pBox->x2 - pBox->x1,
fad0e67
 				    pBox->y2 - pBox->y1,
fad0e67
-				    pExaPixmap->sys_ptr
fad0e67
+				    (char *) (pExaPixmap->sys_ptr
fad0e67
 				    + pBox->y1 * pExaPixmap->sys_pitch
fad0e67
-				    + pBox->x1 * pPixmap->drawable.bitsPerPixel / 8,
fad0e67
+				    + pBox->x1 * pPixmap->drawable.bitsPerPixel / 8),
fad0e67
 				    pExaPixmap->sys_pitch))
fad0e67
 	{
fad0e67
 	    if (!access_prepared) {
ff0bb53
@@ -303,6 +334,9 @@ exaDoMoveInPixmap (ExaMigrationPtr migrate)
fad0e67
     ExaScreenPriv (pScreen);
fad0e67
     ExaPixmapPriv (pPixmap);
fad0e67
 
fad0e67
+    if (migrate->as_dst)
fad0e67
+	pExaPixmap->pendingDamage = TRUE;
fad0e67
+
fad0e67
     /* If we're VT-switched away, no touching card memory allowed. */
fad0e67
     if (pExaScr->swappedOut)
fad0e67
 	return;
ff0bb53
@@ -371,6 +405,9 @@ exaDoMoveOutPixmap (ExaMigrationPtr migrate)
fad0e67
     PixmapPtr pPixmap = migrate->pPix;
fad0e67
     ExaPixmapPriv (pPixmap);
fad0e67
 
fad0e67
+    if (migrate->as_dst)
fad0e67
+	pExaPixmap->pendingDamage = TRUE;
fad0e67
+
fad0e67
     if (!pExaPixmap->area || exaPixmapIsPinned(pPixmap))
fad0e67
 	return;
fad0e67
 
fad0e67
diff --git a/exa/exa_priv.h b/exa/exa_priv.h
ff0bb53
index 387e751..f0696b0 100644
fad0e67
--- a/exa/exa_priv.h
fad0e67
+++ b/exa/exa_priv.h
fad0e67
@@ -61,6 +61,7 @@
fad0e67
 #define DEBUG_MIGRATE		0
fad0e67
 #define DEBUG_PIXMAP		0
fad0e67
 #define DEBUG_OFFSCREEN		0
fad0e67
+#define DEBUG_GLYPH_CACHE	0
fad0e67
 
fad0e67
 #if DEBUG_TRACE_FALL
fad0e67
 #define EXA_FALLBACK(x)     					\
fad0e67
@@ -95,6 +96,38 @@ enum ExaMigrationHeuristic {
fad0e67
     ExaMigrationSmart
fad0e67
 };
fad0e67
 
fad0e67
+typedef struct {
fad0e67
+    unsigned char sha1[20];
fad0e67
+} ExaCachedGlyphRec, *ExaCachedGlyphPtr;
fad0e67
+
fad0e67
+typedef struct {
fad0e67
+    /* The identity of the cache, statically configured at initialization */
fad0e67
+    unsigned int format;
fad0e67
+    int glyphWidth;
fad0e67
+    int glyphHeight;
fad0e67
+
fad0e67
+    int size; /* Size of cache; eventually this should be dynamically determined */
fad0e67
+
fad0e67
+    /* Hash table mapping from glyph sha1 to position in the glyph; we use
fad0e67
+     * open addressing with a hash table size determined based on size and large
fad0e67
+     * enough so that we always have a good amount of free space, so we can
fad0e67
+     * use linear probing. (Linear probing is preferrable to double hashing
fad0e67
+     * here because it allows us to easily remove entries.)
fad0e67
+     */
fad0e67
+    int *hashEntries;
fad0e67
+    int hashSize;
fad0e67
+    
fad0e67
+    ExaCachedGlyphPtr glyphs;
fad0e67
+    int glyphCount; /* Current number of glyphs */
fad0e67
+    
fad0e67
+    PicturePtr picture;   /* Where the glyphs of the cache are stored */
fad0e67
+    int yOffset;          /* y location within the picture where the cache starts */
fad0e67
+    int columns;          /* Number of columns the glyphs are layed out in */
fad0e67
+    int evictionPosition; /* Next random position to evict a glyph */
fad0e67
+} ExaGlyphCacheRec, *ExaGlyphCachePtr;
fad0e67
+
fad0e67
+#define EXA_NUM_GLYPH_CACHES 4
fad0e67
+
fad0e67
 typedef void (*EnableDisableFBAccessProcPtr)(int, Bool);
fad0e67
 typedef struct {
fad0e67
     ExaDriverPtr info;
fad0e67
@@ -114,7 +147,6 @@ typedef struct {
fad0e67
     TrianglesProcPtr		 SavedTriangles;
fad0e67
     GlyphsProcPtr                SavedGlyphs;
fad0e67
     TrapezoidsProcPtr            SavedTrapezoids;
fad0e67
-    AddTrapsProcPtr		 SavedAddTraps;
fad0e67
 #endif
fad0e67
   
fad0e67
     Bool			 swappedOut;
fad0e67
@@ -123,6 +155,8 @@ typedef struct {
fad0e67
     unsigned			 disableFbCount;
fad0e67
     Bool			 optimize_migration;
fad0e67
     unsigned			 offScreenCounter;
fad0e67
+
fad0e67
+    ExaGlyphCacheRec             glyphCaches[EXA_NUM_GLYPH_CACHES];
fad0e67
 } ExaScreenPrivRec, *ExaScreenPrivPtr;
fad0e67
 
fad0e67
 /*
fad0e67
@@ -192,6 +226,7 @@ typedef struct {
fad0e67
      * location.
fad0e67
      */
fad0e67
     DamagePtr	    pDamage;
fad0e67
+    Bool	    pendingDamage;
fad0e67
     /**
fad0e67
      * The valid regions mark the valid bits (at least, as they're derived from
fad0e67
      * damage, which may be overreported) of a pixmap's system and FB copies.
fad0e67
@@ -210,18 +245,21 @@ typedef struct _ExaMigrationRec {
fad0e67
     RegionPtr pReg;
fad0e67
 } ExaMigrationRec, *ExaMigrationPtr;
fad0e67
 
fad0e67
+typedef struct {
fad0e67
+    INT16 xSrc;
fad0e67
+    INT16 ySrc;
fad0e67
+    INT16 xDst;
fad0e67
+    INT16 yDst;
fad0e67
+    INT16 width;
fad0e67
+    INT16 height;
fad0e67
+} ExaCompositeRectRec, *ExaCompositeRectPtr;
fad0e67
+
fad0e67
 /**
fad0e67
  * exaDDXDriverInit must be implemented by the DDX using EXA, and is the place
fad0e67
  * to set EXA options or hook in screen functions to handle using EXA as the AA.
fad0e67
   */
fad0e67
 void exaDDXDriverInit (ScreenPtr pScreen);
fad0e67
 
fad0e67
-void
fad0e67
-exaPrepareAccessWindow(WindowPtr pWin);
fad0e67
-
fad0e67
-void
fad0e67
-exaFinishAccessWindow(WindowPtr pWin);
fad0e67
-
fad0e67
 /* exa_unaccel.c */
fad0e67
 void
fad0e67
 exaPrepareAccessGC(GCPtr pGC);
fad0e67
@@ -294,13 +332,6 @@ ExaCheckGetSpans (DrawablePtr pDrawable,
fad0e67
 		 int nspans,
fad0e67
 		 char *pdstStart);
fad0e67
 
fad0e67
-void
fad0e67
-ExaCheckAddTraps (PicturePtr	pPicture,
fad0e67
-		  INT16		x_off,
fad0e67
-		  INT16		y_off,
fad0e67
-		  int		ntrap,
fad0e67
-		  xTrap		*traps);
fad0e67
-
fad0e67
 /* exa_accel.c */
fad0e67
 
fad0e67
 static _X_INLINE Bool
ff0bb53
@@ -419,6 +450,13 @@ exaComposite(CARD8	op,
fad0e67
 	     CARD16	height);
fad0e67
 
fad0e67
 void
fad0e67
+exaCompositeRects(CARD8	              op,
fad0e67
+		  PicturePtr	      Src,
fad0e67
+		  PicturePtr	      pDst,
fad0e67
+		  int                 nrect,
fad0e67
+		  ExaCompositeRectPtr rects);
fad0e67
+
fad0e67
+void
fad0e67
 exaTrapezoids (CARD8 op, PicturePtr pSrc, PicturePtr pDst,
fad0e67
                PictFormatPtr maskFormat, INT16 xSrc, INT16 ySrc,
fad0e67
                int ntrap, xTrapezoid *traps);
ff0bb53
@@ -428,6 +466,13 @@ exaTriangles (CARD8 op, PicturePtr pSrc, PicturePtr pDst,
fad0e67
 	      PictFormatPtr maskFormat, INT16 xSrc, INT16 ySrc,
fad0e67
 	      int ntri, xTriangle *tris);
fad0e67
 
fad0e67
+/* exa_glyph.c */
fad0e67
+void
fad0e67
+exaGlyphsInit(ScreenPtr pScreen);
fad0e67
+
fad0e67
+void
fad0e67
+exaGlyphsFini (ScreenPtr pScreen);
fad0e67
+
fad0e67
 void
fad0e67
 exaGlyphs (CARD8	op,
fad0e67
 	  PicturePtr	pSrc,
ff0bb53
@@ -446,4 +491,7 @@ exaDoMigration (ExaMigrationPtr pixmaps, int npixmaps, Bool can_accel);
fad0e67
 void
fad0e67
 exaPixmapSave (ScreenPtr pScreen, ExaOffscreenArea *area);
fad0e67
 
fad0e67
+Bool
fad0e67
+exaDamageDestForMigration(DrawablePtr pDrawable, PixmapPtr pPix, RegionPtr region);
fad0e67
+
fad0e67
 #endif /* EXAPRIV_H */
fad0e67
diff --git a/exa/exa_render.c b/exa/exa_render.c
fad0e67
index 1d7b897..bafa309 100644
fad0e67
--- a/exa/exa_render.c
fad0e67
+++ b/exa/exa_render.c
fad0e67
@@ -332,6 +332,228 @@ exaTryDriverSolidFill(PicturePtr	pSrc,
fad0e67
 }
fad0e67
 
fad0e67
 static int
fad0e67
+exaTryDriverCompositeRects(CARD8	       op,
fad0e67
+			   PicturePtr	       pSrc,
fad0e67
+			   PicturePtr	       pDst,
fad0e67
+			   int                 nrect,
fad0e67
+			   ExaCompositeRectPtr rects)
fad0e67
+{
fad0e67
+    ExaScreenPriv (pDst->pDrawable->pScreen);
fad0e67
+    int src_off_x, src_off_y, dst_off_x, dst_off_y;
fad0e67
+    PixmapPtr pSrcPix, pDstPix;
fad0e67
+    ExaPixmapPrivPtr pSrcExaPix, pDstExaPix;
fad0e67
+    struct _Pixmap scratch;
fad0e67
+    ExaMigrationRec pixmaps[2];
fad0e67
+
fad0e67
+    if (!pExaScr->info->PrepareComposite)
fad0e67
+	return -1;
fad0e67
+
fad0e67
+    pSrcPix = exaGetDrawablePixmap(pSrc->pDrawable);
fad0e67
+    pSrcExaPix = ExaGetPixmapPriv(pSrcPix);
fad0e67
+
fad0e67
+    pDstPix = exaGetDrawablePixmap(pDst->pDrawable);
fad0e67
+    pDstExaPix = ExaGetPixmapPriv(pDstPix);
fad0e67
+
fad0e67
+    /* Check whether the accelerator can use these pixmaps.
fad0e67
+     * FIXME: If it cannot, use temporary pixmaps so that the drawing
fad0e67
+     * happens within limits.
fad0e67
+     */
fad0e67
+    if (pSrcExaPix->accel_blocked ||
fad0e67
+	pDstExaPix->accel_blocked)
fad0e67
+    {
fad0e67
+	return -1;
fad0e67
+    }
fad0e67
+
fad0e67
+    if (pExaScr->info->CheckComposite &&
fad0e67
+	!(*pExaScr->info->CheckComposite) (op, pSrc, NULL, pDst))
fad0e67
+    {
fad0e67
+	return -1;
fad0e67
+    }
fad0e67
+    
fad0e67
+    exaGetDrawableDeltas (pDst->pDrawable, pDstPix, &dst_off_x, &dst_off_y);
fad0e67
+	
fad0e67
+    pixmaps[0].as_dst = TRUE;
fad0e67
+    pixmaps[0].as_src = exaOpReadsDestination(op);
fad0e67
+    pixmaps[0].pPix = pDstPix;
fad0e67
+    pixmaps[0].pReg = NULL;
fad0e67
+    pixmaps[1].as_dst = FALSE;
fad0e67
+    pixmaps[1].as_src = TRUE;
fad0e67
+    pixmaps[1].pPix = pSrcPix;
fad0e67
+    pixmaps[1].pReg = NULL;
fad0e67
+    exaDoMigration(pixmaps, 2, TRUE);
fad0e67
+
fad0e67
+    pSrcPix = exaGetOffscreenPixmap (pSrc->pDrawable, &src_off_x, &src_off_y);
fad0e67
+    if (!exaPixmapIsOffscreen(pDstPix))
fad0e67
+	return 0;
fad0e67
+    
fad0e67
+    if (!pSrcPix && pExaScr->info->UploadToScratch)
fad0e67
+    {
fad0e67
+	pSrcPix = exaGetDrawablePixmap (pSrc->pDrawable);
fad0e67
+	if ((*pExaScr->info->UploadToScratch) (pSrcPix, &scratch))
fad0e67
+	    pSrcPix = &scratch;
fad0e67
+    }
fad0e67
+
fad0e67
+    if (!pSrcPix)
fad0e67
+	return 0;
fad0e67
+
fad0e67
+    if (!(*pExaScr->info->PrepareComposite) (op, pSrc, NULL, pDst, pSrcPix,
fad0e67
+					     NULL, pDstPix))
fad0e67
+	return -1;
fad0e67
+
fad0e67
+    while (nrect--)
fad0e67
+    {
fad0e67
+	INT16 xDst = rects->xDst + pDst->pDrawable->x;
fad0e67
+	INT16 yDst = rects->yDst + pDst->pDrawable->y;
fad0e67
+	INT16 xSrc = rects->xSrc + pSrc->pDrawable->x;
fad0e67
+	INT16 ySrc = rects->ySrc + pSrc->pDrawable->y;
fad0e67
+	
fad0e67
+	RegionRec region;
fad0e67
+	BoxPtr pbox;
fad0e67
+	int nbox;
fad0e67
+	
fad0e67
+	if (!miComputeCompositeRegion (&region, pSrc, NULL, pDst,
fad0e67
+				       xSrc, ySrc, 0, 0, xDst, yDst,
fad0e67
+				       rects->width, rects->height))
fad0e67
+	    goto next_rect;
fad0e67
+	
fad0e67
+	REGION_TRANSLATE(pScreen, &region, dst_off_x, dst_off_y);
fad0e67
+	
fad0e67
+	nbox = REGION_NUM_RECTS(&region);
fad0e67
+	pbox = REGION_RECTS(&region);
fad0e67
+
fad0e67
+	xSrc = xSrc + src_off_x - xDst - dst_off_x;
fad0e67
+	ySrc = ySrc + src_off_y - yDst - dst_off_y;
fad0e67
+	
fad0e67
+	while (nbox--)
fad0e67
+	{
fad0e67
+	    (*pExaScr->info->Composite) (pDstPix,
fad0e67
+					 pbox->x1 + xSrc,
fad0e67
+					 pbox->y1 + ySrc,
fad0e67
+					 0, 0,
fad0e67
+					 pbox->x1,
fad0e67
+					 pbox->y1,
fad0e67
+					 pbox->x2 - pbox->x1,
fad0e67
+					 pbox->y2 - pbox->y1);
fad0e67
+	    pbox++;
fad0e67
+	}
fad0e67
+
fad0e67
+    next_rect:
fad0e67
+	REGION_UNINIT(pDst->pDrawable->pScreen, &region);
fad0e67
+
fad0e67
+	rects++;
fad0e67
+    }
fad0e67
+    
fad0e67
+    (*pExaScr->info->DoneComposite) (pDstPix);
fad0e67
+    exaMarkSync(pDst->pDrawable->pScreen);
fad0e67
+	
fad0e67
+    return 1;
fad0e67
+}
fad0e67
+
fad0e67
+/**
fad0e67
+ * Copy a number of rectangles from source to destination in a single
fad0e67
+ * operation. This is specialized for building a glyph mask: we don'y
fad0e67
+ * have a mask argument because we don't need it for that, and we
fad0e67
+ * don't have he special-case fallbacks found in exaComposite() - if the
fad0e67
+ * driver can support it, we use the driver functionality, otherwise we
fad0e67
+ * fallback straight to software.
fad0e67
+ */
fad0e67
+void
fad0e67
+exaCompositeRects(CARD8	              op,
fad0e67
+		  PicturePtr	      pSrc,
fad0e67
+		  PicturePtr	      pDst,
fad0e67
+		  int                 nrect,
fad0e67
+		  ExaCompositeRectPtr rects)
fad0e67
+{
fad0e67
+    PixmapPtr pPixmap = exaGetDrawablePixmap(pDst->pDrawable);
fad0e67
+    ExaPixmapPriv(pPixmap);
fad0e67
+    RegionRec region;
fad0e67
+    int n;
fad0e67
+    ExaCompositeRectPtr r;
fad0e67
+    
fad0e67
+    if (pExaPixmap->pDamage) {
fad0e67
+	int x1 = MAXSHORT;
fad0e67
+	int y1 = MAXSHORT;
fad0e67
+	int x2 = MINSHORT;
fad0e67
+	int y2 = MINSHORT;
fad0e67
+	BoxRec box;
fad0e67
+    
fad0e67
+	/* We have to manage the damage ourselves, since CompositeRects isn't
fad0e67
+	 * something in the screen that can be managed by the damage extension,
fad0e67
+	 * and EXA depends on damage to track what needs to be migrated between
fad0e67
+	 * offscreen and onscreen.
fad0e67
+	 */
fad0e67
+
fad0e67
+	/* Compute the overall extents of the composited region - we're making
fad0e67
+	 * the assumption here that we are compositing a bunch of glyphs that
fad0e67
+	 * cluster closely together and damaging each glyph individually would
fad0e67
+	 * be a loss compared to damaging the bounding box.
fad0e67
+	 */
fad0e67
+	n = nrect;
fad0e67
+	r = rects;
fad0e67
+	while (n--) {
fad0e67
+	    int rect_x2 = r->xDst + r->width;
fad0e67
+	    int rect_y2 = r->yDst + r->width;
fad0e67
+
fad0e67
+	    if (r->xDst < x1) x1 = r->xDst;
fad0e67
+	    if (r->xDst < y1) y1 = r->xDst;
fad0e67
+	    if (rect_x2 > x2) x2 = rect_x2;
fad0e67
+	    if (rect_y2 > y2) y2 = rect_y2;
fad0e67
+
fad0e67
+	    r++;
fad0e67
+	}
fad0e67
+
fad0e67
+	if (x2 <= x1 && y2 <= y1)
fad0e67
+	    return;
fad0e67
+
fad0e67
+	box.x1 = x1;
fad0e67
+	box.x2 = x2 < MAXSHORT ? x2 : MAXSHORT;
fad0e67
+	box.y1 = y1;
fad0e67
+	box.y2 = y2 < MAXSHORT ? y2 : MAXSHORT;
fad0e67
+
fad0e67
+ 	/* The pixmap migration code relies on pendingDamage indicating
fad0e67
+	 * the bounds of the current rendering, so we need to force 
fad0e67
+	 * the actual damage into that region before we do anything, and
fad0e67
+	 * (see use of DamagePendingRegion in exaCopyDirty)
fad0e67
+	 */
fad0e67
+
fad0e67
+	REGION_INIT(pScreen, &region, &box, 1);
fad0e67
+    
fad0e67
+	exaDamageDestForMigration(pDst->pDrawable, pPixmap, &region);
fad0e67
+    }
fad0e67
+    
fad0e67
+    /************************************************************/
fad0e67
+    
fad0e67
+    ValidatePicture (pSrc);
fad0e67
+    ValidatePicture (pDst);
fad0e67
+    
fad0e67
+    if (exaTryDriverCompositeRects(op, pSrc, pDst, nrect, rects) != 1) {
fad0e67
+	n = nrect;
fad0e67
+	r = rects;
fad0e67
+	while (n--) {
fad0e67
+	    ExaCheckComposite (op, pSrc, NULL, pDst,
fad0e67
+			       r->xSrc, r->ySrc,
fad0e67
+			       0, 0,
fad0e67
+			       r->xDst, r->yDst,
fad0e67
+			       r->width, r->height);
fad0e67
+	    r++;
fad0e67
+	}
fad0e67
+    }
fad0e67
+    
fad0e67
+    /************************************************************/
fad0e67
+
fad0e67
+    if (pExaPixmap->pDamage) {
fad0e67
+	/* Now we have to flush the damage out from pendingDamage => damage 
fad0e67
+	 * Calling DamageDamageRegion has that effect. (We could pass
fad0e67
+	 * in an empty region here, but we pass in the same region we
fad0e67
+	 * use above; the effect is the same.)
fad0e67
+	 */
fad0e67
+
fad0e67
+	DamageDamageRegion(pDst->pDrawable, &region);
fad0e67
+	REGION_UNINIT(pScreen, &region);
fad0e67
+    }
fad0e67
+}
fad0e67
+
fad0e67
+static int
fad0e67
 exaTryDriverComposite(CARD8		op,
fad0e67
 		      PicturePtr	pSrc,
fad0e67
 		      PicturePtr	pMask,
fad0e67
@@ -843,22 +1065,16 @@ exaTrapezoids (CARD8 op, PicturePtr pSrc, PicturePtr pDst,
fad0e67
 	PixmapPtr pixmap = exaGetDrawablePixmap (pDraw);
fad0e67
 	ExaPixmapPriv (pixmap);
fad0e67
 	RegionRec migration;
fad0e67
-	RegionPtr pending_damage = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
-	int xoff, yoff;
fad0e67
-
fad0e67
-	exaGetDrawableDeltas(pDraw, pixmap, &xoff, &yoff);
fad0e67
 
fad0e67
-	xoff += pDraw->x;
fad0e67
-	yoff += pDraw->y;
fad0e67
+	if (pExaPixmap->pDamage) {
fad0e67
+	    bounds.x1 += pDraw->x;
fad0e67
+	    bounds.y1 += pDraw->y;
fad0e67
+	    bounds.x2 += pDraw->x;
fad0e67
+	    bounds.y2 += pDraw->y;
fad0e67
 
fad0e67
-	bounds.x1 += xoff;
fad0e67
-	bounds.y1 += yoff;
fad0e67
-	bounds.x2 += xoff;
fad0e67
-	bounds.y2 += yoff;
fad0e67
-
fad0e67
-	REGION_INIT(pScreen, &migration, &bounds, 1);
fad0e67
-	REGION_UNION(pScreen, pending_damage, pending_damage, &migration);
fad0e67
-	REGION_UNINIT(pScreen, &migration);
fad0e67
+	    REGION_INIT(pScreen, &migration, &bounds, 1);
fad0e67
+	    exaDamageDestForMigration(pDraw, pixmap, &migration);
fad0e67
+	}
fad0e67
 
fad0e67
 	exaPrepareAccess(pDraw, EXA_PREPARE_DEST);
fad0e67
 
fad0e67
@@ -866,6 +1082,13 @@ exaTrapezoids (CARD8 op, PicturePtr pSrc, PicturePtr pDst,
fad0e67
 	    (*ps->RasterizeTrapezoid) (pDst, traps, 0, 0);
fad0e67
 
fad0e67
 	exaFinishAccess(pDraw, EXA_PREPARE_DEST);
fad0e67
+
fad0e67
+	/* Damage manually, because Trapezoids expects to hit Composite normally. */
fad0e67
+	/* Composite is wrapped by damage, but Trapezoids isn't. */
fad0e67
+	if (pExaPixmap->pDamage) {
fad0e67
+	    DamageDamageRegion(pDraw, &migration);
fad0e67
+	    REGION_UNINIT(pScreen, &migration);
fad0e67
+	}
fad0e67
     }
fad0e67
     else if (maskFormat)
fad0e67
     {
fad0e67
@@ -946,26 +1169,27 @@ exaTriangles (CARD8 op, PicturePtr pSrc, PicturePtr pDst,
fad0e67
 	PixmapPtr pixmap = exaGetDrawablePixmap (pDraw);
fad0e67
 	ExaPixmapPriv (pixmap);
fad0e67
 	RegionRec migration;
fad0e67
-	RegionPtr pending_damage = DamagePendingRegion(pExaPixmap->pDamage);
fad0e67
-	int xoff, yoff;
fad0e67
-
fad0e67
-	exaGetDrawableDeltas(pDraw, pixmap, &xoff, &yoff);
fad0e67
-
fad0e67
-	xoff += pDraw->x;
fad0e67
-	yoff += pDraw->y;
fad0e67
 
fad0e67
-	bounds.x1 += xoff;
fad0e67
-	bounds.y1 += yoff;
fad0e67
-	bounds.x2 += xoff;
fad0e67
-	bounds.y2 += yoff;
fad0e67
+	if (pExaPixmap->pDamage) {
fad0e67
+	    bounds.x1 += pDraw->x;
fad0e67
+	    bounds.y1 += pDraw->y;
fad0e67
+	    bounds.x2 += pDraw->x;
fad0e67
+	    bounds.y2 += pDraw->y;
fad0e67
 
fad0e67
-	REGION_INIT(pScreen, &migration, &bounds, 1);
fad0e67
-	REGION_UNION(pScreen, pending_damage, pending_damage, &migration);
fad0e67
-	REGION_UNINIT(pScreen, &migration);
fad0e67
+	    REGION_INIT(pScreen, &migration, &bounds, 1);
fad0e67
+	    exaDamageDestForMigration(pDraw, pixmap, &migration);
fad0e67
+	}
fad0e67
 
fad0e67
 	exaPrepareAccess(pDraw, EXA_PREPARE_DEST);
fad0e67
 	(*ps->AddTriangles) (pDst, 0, 0, ntri, tris);
fad0e67
 	exaFinishAccess(pDraw, EXA_PREPARE_DEST);
fad0e67
+
fad0e67
+	/* Damage manually, because Triangles expects to hit Composite normally. */
fad0e67
+	/* Composite is wrapped by damage, but Triangles isn't. */
fad0e67
+	if (pExaPixmap->pDamage) {
fad0e67
+	    DamageDamageRegion(pDraw, &migration);
fad0e67
+	    REGION_UNINIT(pScreen, &migration);
fad0e67
+	}
fad0e67
     }
fad0e67
     else if (maskFormat)
fad0e67
     {
fad0e67
diff --git a/exa/exa_unaccel.c b/exa/exa_unaccel.c
fad0e67
index d7bd06c..d5d6a30 100644
fad0e67
--- a/exa/exa_unaccel.c
fad0e67
+++ b/exa/exa_unaccel.c
fad0e67
@@ -97,12 +97,15 @@ ExaCheckPutImage (DrawablePtr pDrawable, GCPtr pGC, int depth,
fad0e67
 		 int x, int y, int w, int h, int leftPad, int format,
fad0e67
 		 char *bits)
fad0e67
 {
fad0e67
+    ExaPixmapPriv(exaGetDrawablePixmap(pDrawable));
fad0e67
+
fad0e67
     EXA_FALLBACK(("to %p (%c)\n", pDrawable, exaDrawableLocation(pDrawable)));
fad0e67
     if (exaGCReadsDestination(pDrawable, pGC->planemask, pGC->fillStyle,
fad0e67
 			      pGC->alu))
fad0e67
 	exaPrepareAccess (pDrawable, EXA_PREPARE_DEST);
fad0e67
     else
fad0e67
-	ExaDoPrepareAccess (pDrawable, EXA_PREPARE_DEST);
fad0e67
+	exaPrepareAccessReg (pDrawable, EXA_PREPARE_DEST, pExaPixmap->pDamage ?
fad0e67
+			     DamagePendingRegion(pExaPixmap->pDamage) : NULL);
fad0e67
     fbPutImage (pDrawable, pGC, depth, x, y, w, h, leftPad, format, bits);
fad0e67
     exaFinishAccess (pDrawable, EXA_PREPARE_DEST);
fad0e67
 }
fad0e67
@@ -350,20 +353,6 @@ ExaCheckComposite (CARD8      op,
fad0e67
     REGION_UNINIT(pScreen, &region);
fad0e67
 }
fad0e67
 
fad0e67
-void
fad0e67
-ExaCheckAddTraps (PicturePtr	pPicture,
fad0e67
-		  INT16		x_off,
fad0e67
-		  INT16		y_off,
fad0e67
-		  int		ntrap,
fad0e67
-		  xTrap		*traps)
fad0e67
-{
fad0e67
-    EXA_FALLBACK(("to pict %p (%c)\n",
fad0e67
-		  exaDrawableLocation(pPicture->pDrawable)));
fad0e67
-    exaPrepareAccess(pPicture->pDrawable, EXA_PREPARE_DEST);
fad0e67
-    fbAddTraps (pPicture, x_off, y_off, ntrap, traps);
fad0e67
-    exaFinishAccess(pPicture->pDrawable, EXA_PREPARE_DEST);
fad0e67
-}
fad0e67
-
fad0e67
 /**
fad0e67
  * Gets the 0,0 pixel of a pixmap.  Used for doing solid fills of tiled pixmaps
fad0e67
  * that happen to be 1x1.  Pixmap must be at least 8bpp.
fad0e67
@@ -373,23 +362,22 @@ ExaCheckAddTraps (PicturePtr	pPicture,
fad0e67
 CARD32
fad0e67
 exaGetPixmapFirstPixel (PixmapPtr pPixmap)
fad0e67
 {
fad0e67
-    ExaScreenPriv(pPixmap->drawable.pScreen);
fad0e67
     CARD32 pixel;
fad0e67
     void *fb;
fad0e67
     Bool need_finish = FALSE;
fad0e67
     BoxRec box;
fad0e67
     RegionRec migration;
fad0e67
     ExaPixmapPriv (pPixmap);
fad0e67
-    Bool sys_valid = !miPointInRegion(&pExaPixmap->validSys, 0, 0,  &box);
fad0e67
-    Bool damaged = miPointInRegion(DamageRegion(pExaPixmap->pDamage), 0, 0,
fad0e67
-				   &box);
fad0e67
+    Bool sys_valid = pExaPixmap->pDamage &&
fad0e67
+	!miPointInRegion(&pExaPixmap->validSys, 0, 0,  &box);
fad0e67
+    Bool damaged = pExaPixmap->pDamage &&
fad0e67
+ 	miPointInRegion(DamageRegion(pExaPixmap->pDamage), 0, 0, &box);
fad0e67
     Bool offscreen = exaPixmapIsOffscreen(pPixmap);
fad0e67
 
fad0e67
     fb = pExaPixmap->sys_ptr;
fad0e67
 
fad0e67
     /* Try to avoid framebuffer readbacks */
fad0e67
-    if (pExaScr->info->CreatePixmap ||
fad0e67
-	(!offscreen && !sys_valid && !damaged) ||
fad0e67
+    if ((!offscreen && !sys_valid && !damaged) ||
fad0e67
 	(offscreen && (!sys_valid || damaged)))
fad0e67
     {
fad0e67
 	box.x1 = 0;
fad0e67
diff --git a/hw/xfree86/exa/exa.man.pre b/hw/xfree86/exa/exa.man.pre
fad0e67
index 14859bc..31e1cfe 100644
fad0e67
--- a/hw/xfree86/exa/exa.man.pre
fad0e67
+++ b/hw/xfree86/exa/exa.man.pre
fad0e67
@@ -31,12 +31,6 @@ Disables acceleration of downloading of pixmap data from the framebuffer.
fad0e67
 Not usable with drivers which rely on DownloadFromScreen succeeding.
fad0e67
 Default: No.
fad0e67
 .TP
fad0e67
-.BI "Option \*qEXAOptimizeMigration\*q \*q" boolean \*q
fad0e67
-Enables an additional optimization for migration of destination pixmaps. This
fad0e67
-may improve performance in some cases (e.g. when switching virtual desktops with
fad0e67
-no compositing manager) but causes corruption in others (e.g. when starting
fad0e67
-compiz). Default: No.
fad0e67
-.TP
fad0e67
 .BI "Option \*qMigrationHeuristic\*q \*q" anystr \*q
fad0e67
 Chooses an alternate pixmap migration heuristic, for debugging purposes.  The
fad0e67
 default is intended to be the best performing one for general use, though others
fad0e67
diff --git a/hw/xfree86/exa/examodule.c b/hw/xfree86/exa/examodule.c
fad0e67
index e18da0a..4a8d8f2 100644
fad0e67
--- a/hw/xfree86/exa/examodule.c
fad0e67
+++ b/hw/xfree86/exa/examodule.c
fad0e67
@@ -145,7 +145,7 @@ exaDDXDriverInit(ScreenPtr pScreen)
fad0e67
 	pExaScr->optimize_migration =
fad0e67
 	    xf86ReturnOptValBool(pScreenPriv->options,
fad0e67
 				 EXAOPT_OPTIMIZE_MIGRATION,
fad0e67
-				 FALSE);
fad0e67
+				 TRUE);
fad0e67
     }
fad0e67
 
fad0e67
     if (xf86ReturnOptValBool(pScreenPriv->options,
fad0e67
@@ -179,13 +179,6 @@ exaDDXDriverInit(ScreenPtr pScreen)
fad0e67
     
fad0e67
 }
fad0e67
 
fad0e67
-/*ARGSUSED*/
fad0e67
-static const OptionInfoRec *
fad0e67
-EXAAvailableOptions(void *unused)
fad0e67
-{
fad0e67
-    return (EXAOptions);
fad0e67
-}
fad0e67
-
fad0e67
 static XF86ModuleVersionInfo exaVersRec =
fad0e67
 {
fad0e67
 	"exa",
ff0bb53
-- 
ff0bb53
1.6.0.3
ff0bb53