exa_accel.c 41.7 KB
Newer Older
1
/*
Matěj Cepl's avatar
Matěj Cepl committed
2
 * Copyright © 2001 Keith Packard
3
 *
Matěj Cepl's avatar
Matěj Cepl committed
4
 * Partly based on code that is Copyright © The XFree86 Project Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
23
24
25
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
Matěj Cepl's avatar
Matěj Cepl committed
26
 *    Michel Dänzer <michel@tungstengraphics.com>
27
 *
28
29
 */

30
31
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
32
#endif
33
#include "exa_priv.h"
34
#include <X11/fonts/fontstruct.h>
35
36
37
38
39
#include "dixfontstr.h"
#include "exa.h"

static void
exaFillSpans(DrawablePtr pDrawable, GCPtr pGC, int n,
40
             DDXPointPtr ppt, int *pwidth, int fSorted)
41
{
42
43
44
45
46
47
48
49
50
51
52
53
54
    ScreenPtr pScreen = pDrawable->pScreen;

    ExaScreenPriv(pScreen);
    RegionPtr pClip = fbGetCompositeClip(pGC);
    PixmapPtr pPixmap = exaGetDrawablePixmap(pDrawable);

    ExaPixmapPriv(pPixmap);
    BoxPtr pextent, pbox;
    int nbox;
    int extentX1, extentX2, extentY1, extentY2;
    int fullX1, fullX2, fullY1;
    int partX1, partX2;
    int off_x, off_y;
55

56
    if (pExaScr->fallback_counter ||
57
58
59
60
        pExaScr->swappedOut ||
        pGC->fillStyle != FillSolid || pExaPixmap->accel_blocked) {
        ExaCheckFillSpans(pDrawable, pGC, n, ppt, pwidth, fSorted);
        return;
61
62
63
    }

    if (pExaScr->do_migration) {
64
        ExaMigrationRec pixmaps[1];
65

66
67
68
69
        pixmaps[0].as_dst = TRUE;
        pixmaps[0].as_src = FALSE;
        pixmaps[0].pPix = pPixmap;
        pixmaps[0].pReg = NULL;
70

71
        exaDoMigration(pixmaps, 1, TRUE);
72
73
    }

74
75
76
77
78
79
    if (!(pPixmap = exaGetOffscreenPixmap(pDrawable, &off_x, &off_y)) ||
        !(*pExaScr->info->PrepareSolid) (pPixmap,
                                         pGC->alu,
                                         pGC->planemask, pGC->fgPixel)) {
        ExaCheckFillSpans(pDrawable, pGC, n, ppt, pwidth, fSorted);
        return;
80
81
    }

82
    pextent = RegionExtents(pClip);
83
84
85
86
    extentX1 = pextent->x1;
    extentY1 = pextent->y1;
    extentX2 = pextent->x2;
    extentY2 = pextent->y2;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    while (n--) {
        fullX1 = ppt->x;
        fullY1 = ppt->y;
        fullX2 = fullX1 + (int) *pwidth;
        ppt++;
        pwidth++;

        if (fullY1 < extentY1 || extentY2 <= fullY1)
            continue;

        if (fullX1 < extentX1)
            fullX1 = extentX1;

        if (fullX2 > extentX2)
            fullX2 = extentX2;

        if (fullX1 >= fullX2)
            continue;

        nbox = RegionNumRects(pClip);
        if (nbox == 1) {
            (*pExaScr->info->Solid) (pPixmap,
                                     fullX1 + off_x, fullY1 + off_y,
                                     fullX2 + off_x, fullY1 + 1 + off_y);
        }
        else {
            pbox = RegionRects(pClip);
            while (nbox--) {
                if (pbox->y1 <= fullY1 && fullY1 < pbox->y2) {
                    partX1 = pbox->x1;
                    if (partX1 < fullX1)
                        partX1 = fullX1;
                    partX2 = pbox->x2;
                    if (partX2 > fullX2)
                        partX2 = fullX2;
                    if (partX2 > partX1) {
                        (*pExaScr->info->Solid) (pPixmap,
                                                 partX1 + off_x, fullY1 + off_y,
                                                 partX2 + off_x,
                                                 fullY1 + 1 + off_y);
                    }
                }
                pbox++;
            }
        }
132
    }
133
    (*pExaScr->info->DoneSolid) (pPixmap);
134
135
136
    exaMarkSync(pScreen);
}

Michel Dänzer's avatar
Michel Dänzer committed
137
static Bool
138
139
exaDoPutImage(DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
              int w, int h, int format, char *bits, int src_stride)
140
{
141
142
143
    ExaScreenPriv(pDrawable->pScreen);
    PixmapPtr pPix = exaGetDrawablePixmap(pDrawable);

144
    ExaPixmapPriv(pPix);
145
146
147
148
    RegionPtr pClip;
    BoxPtr pbox;
    int nbox;
    int xoff, yoff;
Michel Dänzer's avatar
Michel Dänzer committed
149
    int bpp = pDrawable->bitsPerPixel;
150
    Bool ret = TRUE;
151

152
153
154
    if (pExaScr->fallback_counter || pExaPixmap->accel_blocked ||
        !pExaScr->info->UploadToScreen)
        return FALSE;
155

156
157
    /* If there's a system copy, we want to save the result there */
    if (pExaPixmap->pDamage)
158
        return FALSE;
159

160
161
    /* Don't bother with under 8bpp, XYPixmaps. */
    if (format != ZPixmap || bpp < 8)
162
        return FALSE;
163
164
165

    /* Only accelerate copies: no rop or planemask. */
    if (!EXA_PM_IS_SOLID(pDrawable, pGC->planemask) || pGC->alu != GXcopy)
166
        return FALSE;
167

Michel Dänzer's avatar
Michel Dänzer committed
168
    if (pExaScr->swappedOut)
169
        return FALSE;
Michel Dänzer's avatar
Michel Dänzer committed
170

171
    if (pExaScr->do_migration) {
172
        ExaMigrationRec pixmaps[1];
173

174
175
176
177
        pixmaps[0].as_dst = TRUE;
        pixmaps[0].as_src = FALSE;
        pixmaps[0].pPix = pPix;
        pixmaps[0].pReg = DamagePendingRegion(pExaPixmap->pDamage);
178

179
        exaDoMigration(pixmaps, 1, TRUE);
180
181
    }

182
    pPix = exaGetOffscreenPixmap(pDrawable, &xoff, &yoff);
183

184
    if (!pPix)
185
        return FALSE;
186

187
188
189
    x += pDrawable->x;
    y += pDrawable->y;

190
    pClip = fbGetCompositeClip(pGC);
191
    for (nbox = RegionNumRects(pClip),
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
         pbox = RegionRects(pClip); nbox--; pbox++) {
        int x1 = x;
        int y1 = y;
        int x2 = x + w;
        int y2 = y + h;
        char *src;
        Bool ok;

        if (x1 < pbox->x1)
            x1 = pbox->x1;
        if (y1 < pbox->y1)
            y1 = pbox->y1;
        if (x2 > pbox->x2)
            x2 = pbox->x2;
        if (y2 > pbox->y2)
            y2 = pbox->y2;
        if (x1 >= x2 || y1 >= y2)
            continue;

        src = bits + (y1 - y) * src_stride + (x1 - x) * (bpp / 8);
        ok = pExaScr->info->UploadToScreen(pPix, x1 + xoff, y1 + yoff,
                                           x2 - x1, y2 - y1, src, src_stride);
        /* We have to fall back completely, and ignore what has already been completed.
         * Messing with the fb layer directly like we used to is completely unacceptable.
         */
        if (!ok) {
            ret = FALSE;
            break;
        }
221
222
    }

223
    if (ret)
224
        exaMarkSync(pDrawable->pScreen);
225

226
    return ret;
Michel Dänzer's avatar
Michel Dänzer committed
227
228
}

229
static void
230
231
exaPutImage(DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
            int w, int h, int leftPad, int format, char *bits)
232
233
{
    if (!exaDoPutImage(pDrawable, pGC, depth, x, y, w, h, format, bits,
234
235
236
                       PixmapBytePad(w, pDrawable->depth)))
        ExaCheckPutImage(pDrawable, pGC, depth, x, y, w, h, leftPad, format,
                         bits);
237
238
}

239
static Bool inline
240
241
exaCopyNtoNTwoDir(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable,
                  GCPtr pGC, BoxPtr pbox, int nbox, int dx, int dy)
242
{
243
    ExaScreenPriv(pDstDrawable->pScreen);
244
245
246
247
248
    PixmapPtr pSrcPixmap, pDstPixmap;
    int src_off_x, src_off_y, dst_off_x, dst_off_y;
    int dirsetup;

    /* Need to get both pixmaps to call the driver routines */
249
250
    pSrcPixmap = exaGetOffscreenPixmap(pSrcDrawable, &src_off_x, &src_off_y);
    pDstPixmap = exaGetOffscreenPixmap(pDstDrawable, &dst_off_x, &dst_off_y);
251
    if (!pSrcPixmap || !pDstPixmap)
252
        return FALSE;
253
254
255
256
257

    /*
     * Now the case of a chip that only supports xdir = ydir = 1 or
     * xdir = ydir = -1, but we have xdir != ydir.
     */
258
    dirsetup = 0;               /* No direction set up yet. */
259
    for (; nbox; pbox++, nbox--) {
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        if (dx >= 0 && (src_off_y + pbox->y1 + dy) != pbox->y1) {
            /* Do a xdir = ydir = -1 blit instead. */
            if (dirsetup != -1) {
                if (dirsetup != 0)
                    pExaScr->info->DoneCopy(pDstPixmap);
                dirsetup = -1;
                if (!(*pExaScr->info->PrepareCopy) (pSrcPixmap,
                                                    pDstPixmap,
                                                    -1, -1,
                                                    pGC ? pGC->alu : GXcopy,
                                                    pGC ? pGC->planemask :
                                                    FB_ALLONES))
                    return FALSE;
            }
            (*pExaScr->info->Copy) (pDstPixmap,
                                    src_off_x + pbox->x1 + dx,
                                    src_off_y + pbox->y1 + dy,
                                    dst_off_x + pbox->x1,
                                    dst_off_y + pbox->y1,
                                    pbox->x2 - pbox->x1, pbox->y2 - pbox->y1);
        }
        else if (dx < 0 && (src_off_y + pbox->y1 + dy) != pbox->y1) {
            /* Do a xdir = ydir = 1 blit instead. */
            if (dirsetup != 1) {
                if (dirsetup != 0)
                    pExaScr->info->DoneCopy(pDstPixmap);
                dirsetup = 1;
                if (!(*pExaScr->info->PrepareCopy) (pSrcPixmap,
                                                    pDstPixmap,
                                                    1, 1,
                                                    pGC ? pGC->alu : GXcopy,
                                                    pGC ? pGC->planemask :
                                                    FB_ALLONES))
                    return FALSE;
            }
            (*pExaScr->info->Copy) (pDstPixmap,
                                    src_off_x + pbox->x1 + dx,
                                    src_off_y + pbox->y1 + dy,
                                    dst_off_x + pbox->x1,
                                    dst_off_y + pbox->y1,
                                    pbox->x2 - pbox->x1, pbox->y2 - pbox->y1);
        }
        else if (dx >= 0) {
            /*
             * xdir = 1, ydir = -1.
             * Perform line-by-line xdir = ydir = 1 blits, going up.
             */
            int i;

            if (dirsetup != 1) {
                if (dirsetup != 0)
                    pExaScr->info->DoneCopy(pDstPixmap);
                dirsetup = 1;
                if (!(*pExaScr->info->PrepareCopy) (pSrcPixmap,
                                                    pDstPixmap,
                                                    1, 1,
                                                    pGC ? pGC->alu : GXcopy,
                                                    pGC ? pGC->planemask :
                                                    FB_ALLONES))
                    return FALSE;
            }
            for (i = pbox->y2 - pbox->y1 - 1; i >= 0; i--)
                (*pExaScr->info->Copy) (pDstPixmap,
                                        src_off_x + pbox->x1 + dx,
                                        src_off_y + pbox->y1 + dy + i,
                                        dst_off_x + pbox->x1,
                                        dst_off_y + pbox->y1 + i,
                                        pbox->x2 - pbox->x1, 1);
        }
        else {
            /*
             * xdir = -1, ydir = 1.
             * Perform line-by-line xdir = ydir = -1 blits, going down.
             */
            int i;

            if (dirsetup != -1) {
                if (dirsetup != 0)
                    pExaScr->info->DoneCopy(pDstPixmap);
                dirsetup = -1;
                if (!(*pExaScr->info->PrepareCopy) (pSrcPixmap,
                                                    pDstPixmap,
                                                    -1, -1,
                                                    pGC ? pGC->alu : GXcopy,
                                                    pGC ? pGC->planemask :
                                                    FB_ALLONES))
                    return FALSE;
            }
            for (i = 0; i < pbox->y2 - pbox->y1; i++)
                (*pExaScr->info->Copy) (pDstPixmap,
                                        src_off_x + pbox->x1 + dx,
                                        src_off_y + pbox->y1 + dy + i,
                                        dst_off_x + pbox->x1,
                                        dst_off_y + pbox->y1 + i,
                                        pbox->x2 - pbox->x1, 1);
        }
356
    }
357
    if (dirsetup != 0)
358
        pExaScr->info->DoneCopy(pDstPixmap);
359
360
361
362
    exaMarkSync(pDstDrawable->pScreen);
    return TRUE;
}

Maarten Maathuis's avatar
Maarten Maathuis committed
363
Bool
364
365
366
367
368
exaHWCopyNtoN(DrawablePtr pSrcDrawable,
              DrawablePtr pDstDrawable,
              GCPtr pGC,
              BoxPtr pbox,
              int nbox, int dx, int dy, Bool reverse, Bool upsidedown)
369
{
370
    ExaScreenPriv(pDstDrawable->pScreen);
371
    PixmapPtr pSrcPixmap, pDstPixmap;
372
    ExaPixmapPrivPtr pSrcExaPixmap, pDstExaPixmap;
373
374
    int src_off_x, src_off_y;
    int dst_off_x, dst_off_y;
375
376
    RegionPtr srcregion = NULL, dstregion = NULL;
    xRectangle *rects;
Maarten Maathuis's avatar
Maarten Maathuis committed
377
    Bool ret = TRUE;
378

379
380
    /* avoid doing copy operations if no boxes */
    if (nbox == 0)
381
        return TRUE;
382

383
384
    pSrcPixmap = exaGetDrawablePixmap(pSrcDrawable);
    pDstPixmap = exaGetDrawablePixmap(pDstDrawable);
385

386
387
    exaGetDrawableDeltas(pSrcDrawable, pSrcPixmap, &src_off_x, &src_off_y);
    exaGetDrawableDeltas(pDstDrawable, pDstPixmap, &dst_off_x, &dst_off_y);
388

389
    rects = xallocarray(nbox, sizeof(xRectangle));
390

391
    if (rects) {
392
393
394
395
396
397
398
399
400
        int i;
        int ordering;

        for (i = 0; i < nbox; i++) {
            rects[i].x = pbox[i].x1 + dx + src_off_x;
            rects[i].y = pbox[i].y1 + dy + src_off_y;
            rects[i].width = pbox[i].x2 - pbox[i].x1;
            rects[i].height = pbox[i].y2 - pbox[i].y1;
        }
401

402
403
404
405
406
407
408
409
410
411
412
413
414
415
        /* This must match the RegionCopy() logic for reversing rect order */
        if (nbox == 1 || (dx > 0 && dy > 0) ||
            (pDstDrawable != pSrcDrawable &&
             (pDstDrawable->type != DRAWABLE_WINDOW ||
              pSrcDrawable->type != DRAWABLE_WINDOW)))
            ordering = CT_YXBANDED;
        else
            ordering = CT_UNSORTED;

        srcregion = RegionFromRects(nbox, rects, ordering);
        free(rects);

        if (!pGC || !exaGCReadsDestination(pDstDrawable, pGC->planemask,
                                           pGC->fillStyle, pGC->alu,
416
                                           pGC->clientClip != NULL)) {
417
418
419
420
421
422
            dstregion = RegionCreate(NullBox, 0);
            RegionCopy(dstregion, srcregion);
            RegionTranslate(dstregion, dst_off_x - dx - src_off_x,
                            dst_off_y - dy - src_off_y);
        }
    }
423

424
425
    pSrcExaPixmap = ExaGetPixmapPriv(pSrcPixmap);
    pDstExaPixmap = ExaGetPixmapPriv(pDstPixmap);
426
427

    /* Check whether the accelerator can use this pixmap.
428
429
     * If the pitch of the pixmaps is out of range, there's nothing
     * we can do but fall back to software rendering.
430
     */
431
432
    if (pSrcExaPixmap->accel_blocked & EXA_RANGE_PITCH ||
        pDstExaPixmap->accel_blocked & EXA_RANGE_PITCH)
433
        goto fallback;
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

    /* If the width or the height of either of the pixmaps
     * is out of range, check whether the boxes are actually out of the
     * addressable range as well. If they aren't, we can still do
     * the copying in hardware.
     */
    if (pSrcExaPixmap->accel_blocked || pDstExaPixmap->accel_blocked) {
        int i;

        for (i = 0; i < nbox; i++) {
            /* src */
            if ((pbox[i].x2 + dx + src_off_x) >= pExaScr->info->maxX ||
                (pbox[i].y2 + dy + src_off_y) >= pExaScr->info->maxY)
                goto fallback;

            /* dst */
            if ((pbox[i].x2 + dst_off_x) >= pExaScr->info->maxX ||
                (pbox[i].y2 + dst_off_y) >= pExaScr->info->maxY)
                goto fallback;
        }
454
455
    }

456
    if (pExaScr->do_migration) {
457
458
459
460
461
462
463
464
465
466
467
468
        ExaMigrationRec pixmaps[2];

        pixmaps[0].as_dst = TRUE;
        pixmaps[0].as_src = FALSE;
        pixmaps[0].pPix = pDstPixmap;
        pixmaps[0].pReg = dstregion;
        pixmaps[1].as_dst = FALSE;
        pixmaps[1].as_src = TRUE;
        pixmaps[1].pPix = pSrcPixmap;
        pixmaps[1].pReg = srcregion;

        exaDoMigration(pixmaps, 2, TRUE);
469
    }
470

471
    /* Mixed directions must be handled specially if the card is lame */
472
    if ((pExaScr->info->flags & EXA_TWO_BITBLT_DIRECTIONS) &&
473
474
475
476
477
        reverse != upsidedown) {
        if (exaCopyNtoNTwoDir(pSrcDrawable, pDstDrawable, pGC, pbox, nbox,
                              dx, dy))
            goto out;
        goto fallback;
478
479
    }

480
    if (exaPixmapHasGpuCopy(pDstPixmap)) {
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
        /* Normal blitting. */
        if (exaPixmapHasGpuCopy(pSrcPixmap)) {
            if (!(*pExaScr->info->PrepareCopy)
                (pSrcPixmap, pDstPixmap, reverse ? -1 : 1, upsidedown ? -1 : 1,
                 pGC ? pGC->alu : GXcopy, pGC ? pGC->planemask : FB_ALLONES)) {
                goto fallback;
            }

            while (nbox--) {
                (*pExaScr->info->Copy) (pDstPixmap,
                                        pbox->x1 + dx + src_off_x,
                                        pbox->y1 + dy + src_off_y,
                                        pbox->x1 + dst_off_x,
                                        pbox->y1 + dst_off_y,
                                        pbox->x2 - pbox->x1,
                                        pbox->y2 - pbox->y1);
                pbox++;
            }

            (*pExaScr->info->DoneCopy) (pDstPixmap);
            exaMarkSync(pDstDrawable->pScreen);
            /* UTS: mainly for SHM PutImage's secondary path.
             *
             * Only taking this path for directly accessible pixmaps.
             */
        }
        else if (!pDstExaPixmap->pDamage && pSrcExaPixmap->sys_ptr) {
            int bpp = pSrcDrawable->bitsPerPixel;
            int src_stride = exaGetPixmapPitch(pSrcPixmap);
            CARD8 *src = NULL;

            if (!pExaScr->info->UploadToScreen)
                goto fallback;

            if (pSrcDrawable->bitsPerPixel != pDstDrawable->bitsPerPixel)
                goto fallback;

            if (pSrcDrawable->bitsPerPixel < 8)
                goto fallback;

            if (pGC &&
                !(pGC->alu == GXcopy &&
                  EXA_PM_IS_SOLID(pSrcDrawable, pGC->planemask)))
                goto fallback;

            while (nbox--) {
                src =
                    pSrcExaPixmap->sys_ptr + (pbox->y1 + dy +
                                              src_off_y) * src_stride +
                    (pbox->x1 + dx + src_off_x) * (bpp / 8);
                if (!pExaScr->info->
                    UploadToScreen(pDstPixmap, pbox->x1 + dst_off_x,
                                   pbox->y1 + dst_off_y, pbox->x2 - pbox->x1,
                                   pbox->y2 - pbox->y1, (char *) src,
                                   src_stride))
                    goto fallback;

                pbox++;
            }
        }
        else
            goto fallback;
    }
    else
        goto fallback;
546

547
    goto out;
548

549
 fallback:
Maarten Maathuis's avatar
Maarten Maathuis committed
550
    ret = FALSE;
551

552
 out:
553
    if (dstregion) {
554
555
        RegionUninit(dstregion);
        RegionDestroy(dstregion);
556
557
    }
    if (srcregion) {
558
559
        RegionUninit(srcregion);
        RegionDestroy(srcregion);
560
    }
Maarten Maathuis's avatar
Maarten Maathuis committed
561
562
563
564
565

    return ret;
}

void
566
567
568
569
570
571
572
573
exaCopyNtoN(DrawablePtr pSrcDrawable,
            DrawablePtr pDstDrawable,
            GCPtr pGC,
            BoxPtr pbox,
            int nbox,
            int dx,
            int dy,
            Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
Maarten Maathuis's avatar
Maarten Maathuis committed
574
575
576
{
    ExaScreenPriv(pDstDrawable->pScreen);

577
    if (pExaScr->fallback_counter ||
578
579
        (pExaScr->fallback_flags & EXA_FALLBACK_COPYWINDOW))
        return;
Maarten Maathuis's avatar
Maarten Maathuis committed
580

581
582
583
584
    if (exaHWCopyNtoN
        (pSrcDrawable, pDstDrawable, pGC, pbox, nbox, dx, dy, reverse,
         upsidedown))
        return;
Maarten Maathuis's avatar
Maarten Maathuis committed
585
586
587

    /* This is a CopyWindow, it's cleaner to fallback at the original call. */
    if (pExaScr->fallback_flags & EXA_ACCEL_COPYWINDOW) {
588
589
        pExaScr->fallback_flags |= EXA_FALLBACK_COPYWINDOW;
        return;
Maarten Maathuis's avatar
Maarten Maathuis committed
590
591
592
    }

    /* fallback */
593
594
    ExaCheckCopyNtoN(pSrcDrawable, pDstDrawable, pGC, pbox, nbox, dx, dy,
                     reverse, upsidedown, bitplane, closure);
595
596
}

597
RegionPtr
598
exaCopyArea(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable, GCPtr pGC,
599
            int srcx, int srcy, int width, int height, int dstx, int dsty)
600
{
601
    ExaScreenPriv(pDstDrawable->pScreen);
602

603
    if (pExaScr->fallback_counter || pExaScr->swappedOut) {
604
605
        return ExaCheckCopyArea(pSrcDrawable, pDstDrawable, pGC,
                                srcx, srcy, width, height, dstx, dsty);
606
607
    }

608
609
610
    return miDoCopy(pSrcDrawable, pDstDrawable, pGC,
                    srcx, srcy, width, height,
                    dstx, dsty, exaCopyNtoN, 0, NULL);
611
612
}

613
614
static void
exaPolyPoint(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
615
             DDXPointPtr ppt)
616
{
617
    ExaScreenPriv(pDrawable->pScreen);
618
619
620
621
622
623
    int i;
    xRectangle *prect;

    /* If we can't reuse the current GC as is, don't bother accelerating the
     * points.
     */
624
    if (pExaScr->fallback_counter || pGC->fillStyle != FillSolid) {
625
626
        ExaCheckPolyPoint(pDrawable, pGC, mode, npt, ppt);
        return;
627
628
    }

629
    prect = xallocarray(npt, sizeof(xRectangle));
630
    for (i = 0; i < npt; i++) {
631
632
633
634
635
636
637
638
        prect[i].x = ppt[i].x;
        prect[i].y = ppt[i].y;
        if (i > 0 && mode == CoordModePrevious) {
            prect[i].x += prect[i - 1].x;
            prect[i].y += prect[i - 1].y;
        }
        prect[i].width = 1;
        prect[i].height = 1;
639
640
    }
    pGC->ops->PolyFillRect(pDrawable, pGC, npt, prect);
641
    free(prect);
642
643
644
645
646
647
648
649
650
}

/**
 * exaPolylines() checks if it can accelerate the lines as a group of
 * horizontal or vertical lines (rectangles), and uses existing rectangle fill
 * acceleration if so.
 */
static void
exaPolylines(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
651
             DDXPointPtr ppt)
652
{
653
    ExaScreenPriv(pDrawable->pScreen);
654
655
656
657
    xRectangle *prect;
    int x1, x2, y1, y2;
    int i;

658
    if (pExaScr->fallback_counter) {
659
660
        ExaCheckPolylines(pDrawable, pGC, mode, npt, ppt);
        return;
661
662
    }

663
664
    /* Don't try to do wide lines or non-solid fill style. */
    if (pGC->lineWidth != 0 || pGC->lineStyle != LineSolid ||
665
666
667
        pGC->fillStyle != FillSolid) {
        ExaCheckPolylines(pDrawable, pGC, mode, npt, ppt);
        return;
668
669
    }

670
    prect = xallocarray(npt - 1, sizeof(xRectangle));
671
672
673
    x1 = ppt[0].x;
    y1 = ppt[0].y;
    /* If we have any non-horizontal/vertical, fall back. */
674
    for (i = 0; i < npt - 1; i++) {
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
        if (mode == CoordModePrevious) {
            x2 = x1 + ppt[i + 1].x;
            y2 = y1 + ppt[i + 1].y;
        }
        else {
            x2 = ppt[i + 1].x;
            y2 = ppt[i + 1].y;
        }

        if (x1 != x2 && y1 != y2) {
            free(prect);
            ExaCheckPolylines(pDrawable, pGC, mode, npt, ppt);
            return;
        }

        if (x1 < x2) {
            prect[i].x = x1;
            prect[i].width = x2 - x1 + 1;
        }
        else {
            prect[i].x = x2;
            prect[i].width = x1 - x2 + 1;
        }
        if (y1 < y2) {
            prect[i].y = y1;
            prect[i].height = y2 - y1 + 1;
        }
        else {
            prect[i].y = y2;
            prect[i].height = y1 - y2 + 1;
        }

        x1 = x2;
        y1 = y2;
709
710
    }
    pGC->ops->PolyFillRect(pDrawable, pGC, npt - 1, prect);
711
    free(prect);
712
713
}

714
715
716
717
718
719
/**
 * exaPolySegment() checks if it can accelerate the lines as a group of
 * horizontal or vertical lines (rectangles), and uses existing rectangle fill
 * acceleration if so.
 */
static void
720
exaPolySegment(DrawablePtr pDrawable, GCPtr pGC, int nseg, xSegment * pSeg)
721
{
722
    ExaScreenPriv(pDrawable->pScreen);
723
724
725
726
    xRectangle *prect;
    int i;

    /* Don't try to do wide lines or non-solid fill style. */
727
    if (pExaScr->fallback_counter || pGC->lineWidth != 0 ||
728
729
730
        pGC->lineStyle != LineSolid || pGC->fillStyle != FillSolid) {
        ExaCheckPolySegment(pDrawable, pGC, nseg, pSeg);
        return;
731
732
733
734
    }

    /* If we have any non-horizontal/vertical, fall back. */
    for (i = 0; i < nseg; i++) {
735
736
737
738
        if (pSeg[i].x1 != pSeg[i].x2 && pSeg[i].y1 != pSeg[i].y2) {
            ExaCheckPolySegment(pDrawable, pGC, nseg, pSeg);
            return;
        }
739
740
    }

741
    prect = xallocarray(nseg, sizeof(xRectangle));
742
    for (i = 0; i < nseg; i++) {
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
        if (pSeg[i].x1 < pSeg[i].x2) {
            prect[i].x = pSeg[i].x1;
            prect[i].width = pSeg[i].x2 - pSeg[i].x1 + 1;
        }
        else {
            prect[i].x = pSeg[i].x2;
            prect[i].width = pSeg[i].x1 - pSeg[i].x2 + 1;
        }
        if (pSeg[i].y1 < pSeg[i].y2) {
            prect[i].y = pSeg[i].y1;
            prect[i].height = pSeg[i].y2 - pSeg[i].y1 + 1;
        }
        else {
            prect[i].y = pSeg[i].y2;
            prect[i].height = pSeg[i].y1 - pSeg[i].y2 + 1;
        }

        /* don't paint last pixel */
        if (pGC->capStyle == CapNotLast) {
            if (prect[i].width == 1)
                prect[i].height--;
            else
                prect[i].width--;
        }
767
768
    }
    pGC->ops->PolyFillRect(pDrawable, pGC, nseg, prect);
769
    free(prect);
770
771
}

772
773
static Bool exaFillRegionSolid(DrawablePtr pDrawable, RegionPtr pRegion,
                               Pixel pixel, CARD32 planemask, CARD32 alu,
774
                               Bool hasClientClip);
775

776
static void
777
exaPolyFillRect(DrawablePtr pDrawable, GCPtr pGC, int nrect, xRectangle *prect)
778
{
779
780
781
782
783
    ExaScreenPriv(pDrawable->pScreen);
    RegionPtr pClip = fbGetCompositeClip(pGC);
    PixmapPtr pPixmap = exaGetDrawablePixmap(pDrawable);

    ExaPixmapPriv(pPixmap);
784
    register BoxPtr pbox;
785
786
787
788
789
790
791
    BoxPtr pextent;
    int extentX1, extentX2, extentY1, extentY2;
    int fullX1, fullX2, fullY1, fullY2;
    int partX1, partX2, partY1, partY2;
    int xoff, yoff;
    int xorg, yorg;
    int n;
792
    RegionPtr pReg = RegionFromRects(nrect, prect, CT_UNSORTED);
793
794

    /* Compute intersection of rects and clip region */
795
796
    RegionTranslate(pReg, pDrawable->x, pDrawable->y);
    RegionIntersect(pReg, pClip, pReg);
797

798
    if (!RegionNumRects(pReg)) {
799
        goto out;
800
    }
801

802
803
    exaGetDrawableDeltas(pDrawable, pPixmap, &xoff, &yoff);

804
    if (pExaScr->fallback_counter || pExaScr->swappedOut ||
805
806
        pExaPixmap->accel_blocked) {
        goto fallback;
807
808
809
810
811
812
    }

    /* For ROPs where overlaps don't matter, convert rectangles to region and
     * call exaFillRegion{Solid,Tiled}.
     */
    if ((pGC->fillStyle == FillSolid || pGC->fillStyle == FillTiled) &&
813
814
815
816
817
818
        (nrect == 1 || pGC->alu == GXcopy || pGC->alu == GXclear ||
         pGC->alu == GXnoop || pGC->alu == GXcopyInverted ||
         pGC->alu == GXset)) {
        if (((pGC->fillStyle == FillSolid || pGC->tileIsPixel) &&
             exaFillRegionSolid(pDrawable, pReg, pGC->fillStyle == FillSolid ?
                                pGC->fgPixel : pGC->tile.pixel, pGC->planemask,
819
                                pGC->alu, pGC->clientClip != NULL)) ||
820
821
822
            (pGC->fillStyle == FillTiled && !pGC->tileIsPixel &&
             exaFillRegionTiled(pDrawable, pReg, pGC->tile.pixmap, &pGC->patOrg,
                                pGC->planemask, pGC->alu,
823
                                pGC->clientClip != NULL))) {
824
825
            goto out;
        }
826
827
    }

828
    if (pGC->fillStyle != FillSolid &&
829
830
        !(pGC->tileIsPixel && pGC->fillStyle == FillTiled)) {
        goto fallback;
831
832
    }

833
    if (pExaScr->do_migration) {
834
        ExaMigrationRec pixmaps[1];
835

836
837
838
839
        pixmaps[0].as_dst = TRUE;
        pixmaps[0].as_src = FALSE;
        pixmaps[0].pPix = pPixmap;
        pixmaps[0].pReg = NULL;
840

841
        exaDoMigration(pixmaps, 1, TRUE);
842
    }
843

844
845
846
847
848
849
850
    if (!exaPixmapHasGpuCopy(pPixmap) ||
        !(*pExaScr->info->PrepareSolid) (pPixmap,
                                         pGC->alu,
                                         pGC->planemask, pGC->fgPixel)) {
 fallback:
        ExaCheckPolyFillRect(pDrawable, pGC, nrect, prect);
        goto out;
851
852
853
854
855
    }

    xorg = pDrawable->x;
    yorg = pDrawable->y;

856
    pextent = RegionExtents(pClip);
857
858
859
860
    extentX1 = pextent->x1;
    extentY1 = pextent->y1;
    extentX2 = pextent->x2;
    extentY2 = pextent->y2;
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
    while (nrect--) {
        fullX1 = prect->x + xorg;
        fullY1 = prect->y + yorg;
        fullX2 = fullX1 + (int) prect->width;
        fullY2 = fullY1 + (int) prect->height;
        prect++;

        if (fullX1 < extentX1)
            fullX1 = extentX1;

        if (fullY1 < extentY1)
            fullY1 = extentY1;

        if (fullX2 > extentX2)
            fullX2 = extentX2;

        if (fullY2 > extentY2)
            fullY2 = extentY2;

        if ((fullX1 >= fullX2) || (fullY1 >= fullY2))
            continue;
        n = RegionNumRects(pClip);
        if (n == 1) {
            (*pExaScr->info->Solid) (pPixmap,
                                     fullX1 + xoff, fullY1 + yoff,
                                     fullX2 + xoff, fullY2 + yoff);
        }
        else {
            pbox = RegionRects(pClip);
            /*
             * clip the rectangle to each box in the clip region
             * this is logically equivalent to calling Intersect(),
             * but rectangles may overlap each other here.
             */
            while (n--) {
                partX1 = pbox->x1;
                if (partX1 < fullX1)
                    partX1 = fullX1;
                partY1 = pbox->y1;
                if (partY1 < fullY1)
                    partY1 = fullY1;
                partX2 = pbox->x2;
                if (partX2 > fullX2)
                    partX2 = fullX2;
                partY2 = pbox->y2;
                if (partY2 > fullY2)
                    partY2 = fullY2;

                pbox++;

                if (partX1 < partX2 && partY1 < partY2) {
                    (*pExaScr->info->Solid) (pPixmap,
                                             partX1 + xoff, partY1 + yoff,
                                             partX2 + xoff, partY2 + yoff);
                }
            }
        }
918
    }
919
    (*pExaScr->info->DoneSolid) (pPixmap);
920
    exaMarkSync(pDrawable->pScreen);
921

922
 out:
923
924
    RegionUninit(pReg);
    RegionDestroy(pReg);
925
926
}

927
const GCOps exaOps = {
928
929
    exaFillSpans,
    ExaCheckSetSpans,
930
    exaPutImage,
931
932
    exaCopyArea,
    ExaCheckCopyPlane,
933
934
    exaPolyPoint,
    exaPolylines,
935
    exaPolySegment,
936
937
938
939
940
941
942
943
944
    miPolyRectangle,
    ExaCheckPolyArc,
    miFillPolygon,
    exaPolyFillRect,
    miPolyFillArc,
    miPolyText8,
    miPolyText16,
    miImageText8,
    miImageText16,
945
    ExaCheckImageGlyphBlt,
946
947
948
949
    ExaCheckPolyGlyphBlt,
    ExaCheckPushPixels,
};

950
void
951
952
exaCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
953
954
955
956
    RegionRec rgnDst;
    int dx, dy;
    PixmapPtr pPixmap = (*pWin->drawable.pScreen->GetWindowPixmap) (pWin);

Maarten Maathuis's avatar
Maarten Maathuis committed
957
    ExaScreenPriv(pWin->drawable.pScreen);
958

959
960
    dx = ptOldOrg.x - pWin->drawable.x;
    dy = ptOldOrg.y - pWin->drawable.y;
961
    RegionTranslate(prgnSrc, -dx, -dy);
962

963
    RegionInit(&rgnDst, NullBox, 0);
964

965
    RegionIntersect(&rgnDst, &pWin->borderClip, prgnSrc);
966
967
#ifdef COMPOSITE
    if (pPixmap->screen_x || pPixmap->screen_y)
968
        RegionTranslate(&rgnDst, -pPixmap->screen_x, -pPixmap->screen_y);
969
970
#endif

971
    if (pExaScr->fallback_counter) {
972
973
        pExaScr->fallback_flags |= EXA_FALLBACK_COPYWINDOW;
        goto fallback;
974
975
    }

Maarten Maathuis's avatar
Maarten Maathuis committed
976
    pExaScr->fallback_flags |= EXA_ACCEL_COPYWINDOW;
977
978
    miCopyRegion(&pPixmap->drawable, &pPixmap->drawable,
                 NULL, &rgnDst, dx, dy, exaCopyNtoN, 0, NULL);
Maarten Maathuis's avatar
Maarten Maathuis committed
979
    pExaScr->fallback_flags &= ~EXA_ACCEL_COPYWINDOW;
980

981
 fallback:
982
    RegionUninit(&rgnDst);
Maarten Maathuis's avatar
Maarten Maathuis committed
983
984

    if (pExaScr->fallback_flags & EXA_FALLBACK_COPYWINDOW) {
985
986
987
        pExaScr->fallback_flags &= ~EXA_FALLBACK_COPYWINDOW;
        RegionTranslate(prgnSrc, dx, dy);
        ExaCheckCopyWindow(pWin, ptOldOrg, prgnSrc);
Maarten Maathuis's avatar
Maarten Maathuis committed
988
    }
989
990
}

991
static Bool
992
exaFillRegionSolid(DrawablePtr pDrawable, RegionPtr pRegion, Pixel pixel,
993
                   CARD32 planemask, CARD32 alu, Bool hasClientClip)
994
995
{
    ExaScreenPriv(pDrawable->pScreen);
996
997
998
    PixmapPtr pPixmap = exaGetDrawablePixmap(pDrawable);

    ExaPixmapPriv(pPixmap);
999
    int xoff, yoff;
1000
    Bool ret = FALSE;
1001

1002
    exaGetDrawableDeltas(pDrawable, pPixmap, &xoff, &yoff);
1003
    RegionTranslate(pRegion, xoff, yoff);
1004

1005
    if (pExaScr->fallback_counter || pExaPixmap->accel_blocked)
1006
        goto out;
1007
1008

    if (pExaScr->do_migration) {
1009
        ExaMigrationRec pixmaps[1];
1010

1011
1012
1013
1014
1015
        pixmaps[0].as_dst = TRUE;
        pixmaps[0].as_src = FALSE;
        pixmaps[0].pPix = pPixmap;
        pixmaps[0].pReg = exaGCReadsDestination(pDrawable, planemask, FillSolid,
                                                alu,
1016
                                                hasClientClip) ? NULL : pRegion;
1017

1018
        exaDoMigration(pixmaps, 1, TRUE);
1019
1020
    }

1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
    if (exaPixmapHasGpuCopy(pPixmap) &&
        (*pExaScr->info->PrepareSolid) (pPixmap, alu, planemask, pixel)) {
        int nbox;
        BoxPtr pBox;

        nbox = RegionNumRects(pRegion);
        pBox = RegionRects(pRegion);

        while (nbox--) {
            (*pExaScr->info->Solid) (pPixmap, pBox->x1, pBox->y1, pBox->x2,
                                     pBox->y2);
            pBox++;
        }
        (*pExaScr->info->DoneSolid) (pPixmap);
        exaMarkSync(pDrawable->pScreen);

        if (pExaPixmap->pDamage &&
            pExaPixmap->sys_ptr && pDrawable->type == DRAWABLE_PIXMAP &&
            pDrawable->width == 1 && pDrawable->height == 1 &&
1040
            pDrawable->bitsPerPixel != 24 && alu == GXcopy) {
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
            RegionPtr pending_damage = DamagePendingRegion(pExaPixmap->pDamage);

            switch (pDrawable->bitsPerPixel) {
            case 32:
                *(CARD32 *) pExaPixmap->sys_ptr = pixel;
                break;
            case 16:
                *(CARD16 *) pExaPixmap->sys_ptr = pixel;
                break;
            case 8:
            case 4:
            case 1:
                *(CARD8 *) pExaPixmap->sys_ptr = pixel;
            }

            RegionUnion(&pExaPixmap->validSys, &pExaPixmap->validSys, pRegion);
            RegionUnion(&pExaPixmap->validFB, &pExaPixmap->validFB, pRegion);
            RegionSubtract(pending_damage, pending_damage, pRegion);
        }

        ret = TRUE;
1062
    }
1063

1064
 out:
1065
    RegionTranslate(pRegion, -xoff, -yoff);
1066

1067
    return ret;
1068
1069
}

1070
1071
1072
/* Try to do an accelerated tile of the pTile into pRegion of pDrawable.
 * Based on fbFillRegionTiled(), fbTile().
 */
1073
Bool
1074
1075
exaFillRegionTiled(DrawablePtr pDrawable, RegionPtr pRegion, PixmapPtr pTile,
                   DDXPointPtr pPatOrg, CARD32 planemask, CARD32 alu,
1076
                   Bool hasClientClip)
1077
1078
1079
{
    ExaScreenPriv(pDrawable->pScreen);
    PixmapPtr pPixmap;
1080
1081
    ExaPixmapPrivPtr pExaPixmap;
    ExaPixmapPrivPtr pTileExaPixmap = ExaGetPixmapPriv(pTile);
1082
    int xoff, yoff;
1083
    int tileWidth, tileHeight;
1084
1085
    int nbox = RegionNumRects(pRegion);
    BoxPtr pBox = RegionRects(pRegion);
1086
    Bool ret = FALSE;
1087
    int i;
1088
1089
1090

    tileWidth = pTile->drawable.width;
    tileHeight = pTile->drawable.height;
1091

1092
1093
1094
    /* If we're filling with a solid color, grab it out and go to
     * FillRegionSolid, saving numerous copies.
     */
1095
    if (tileWidth == 1 && tileHeight == 1)
1096
1097
        return exaFillRegionSolid(pDrawable, pRegion,
                                  exaGetPixmapFirstPixel(pTile), planemask,
1098
                                  alu, hasClientClip);
1099

1100
1101
    pPixmap = exaGetDrawablePixmap(pDrawable);
    pExaPixmap = ExaGetPixmapPriv(pPixmap);
1102

1103
    if (pExaScr->fallback_counter || pExaPixmap->accel_blocked ||
1104
1105
        pTileExaPixmap->accel_blocked)
        return FALSE;
1106
1107

    if (pExaScr->do_migration) {
1108
1109
1110
1111
1112
1113
1114
        ExaMigrationRec pixmaps[2];

        pixmaps[0].as_dst = TRUE;
        pixmaps[0].as_src = FALSE;
        pixmaps[0].pPix = pPixmap;
        pixmaps[0].pReg = exaGCReadsDestination(pDrawable, planemask, FillTiled,
                                                alu,
1115
                                                hasClientClip) ? NULL : pRegion;
1116
1117
1118
1119
1120
1121
        pixmaps[1].as_dst = FALSE;
        pixmaps[1].as_src = TRUE;
        pixmaps[1].pPix = pTile;
        pixmaps[1].pReg = NULL;

        exaDoMigration(pixmaps, 2, TRUE);
1122
1123
    }

1124
    pPixmap = exaGetOffscreenPixmap(pDrawable, &xoff, &yoff);
1125

1126
    if (!pPixmap || !exaPixmapHasGpuCopy(pTile))
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
        return FALSE;

    if ((*pExaScr->info->PrepareCopy) (pTile, pPixmap, 1, 1, alu, planemask)) {
        if (xoff || yoff)
            RegionTranslate(pRegion, xoff, yoff);

        for (i = 0; i < nbox; i++) {
            int height = pBox[i].y2 - pBox[i].y1;
            int dstY = pBox[i].y1;
            int tileY;

            if (alu == GXcopy)
                height = min(height, tileHeight);

            modulus(dstY - yoff - pDrawable->y - pPatOrg->y, tileHeight, tileY);

            while (height > 0) {
                int width = pBox[i].x2 - pBox[i].x1;
                int dstX = pBox[i].x1;
                int tileX;
                int h = tileHeight - tileY;

                if (alu == GXcopy)
                    width = min(width, tileWidth);

                if (h > height)
                    h = height;
                height -= h;

                modulus(dstX - xoff - pDrawable->x - pPatOrg->x, tileWidth,
                        tileX);

                while (width > 0) {
                    int w = tileWidth - tileX;

                    if (w > width)
                        w = width;
                    width -= w;

                    (*pExaScr->info->Copy) (pPixmap, tileX, tileY, dstX, dstY,
                                            w, h);
                    dstX += w;
                    tileX = 0;
                }
                dstY += h;
                tileY = 0;
            }
        }
        (*pExaScr->info->DoneCopy) (pPixmap);

        /* With GXcopy, we only need to do the basic algorithm up to the tile
         * size; then, we can just keep doubling the destination in each
         * direction until it fills the box. This way, the number of copy
         * operations is O(log(rx)) + O(log(ry)) instead of O(rx * ry), where
         * rx/ry is the ratio between box and tile width/height. This can make
         * a big difference if each driver copy incurs a significant constant
         * overhead.
         */
        if (alu != GXcopy)
            ret = TRUE;
        else {
            Bool more_copy = FALSE;

            for (i = 0; i < nbox; i++) {
                int dstX = pBox[i].x1 + tileWidth;
                int dstY = pBox[i].y1 + tileHeight;

                if ((dstX < pBox[i].x2) || (dstY < pBox[i].y2)) {
                    more_copy = TRUE;
                    break;
                }
            }

            if (more_copy == FALSE)
                ret = TRUE;

            if (more_copy && (*pExaScr->info->PrepareCopy) (pPixmap, pPixmap,
                                                            1, 1, alu,
                                                            planemask)) {
                for (i = 0; i < nbox; i++) {
                    int dstX = pBox[i].x1 + tileWidth;
                    int dstY = pBox[i].y1 + tileHeight;
                    int width = min(pBox[i].x2 - dstX, tileWidth);
                    int height = min(pBox[i].y2 - pBox[i].y1, tileHeight);

                    while (dstX < pBox[i].x2) {
                        (*pExaScr->info->Copy) (pPixmap, pBox[i].x1, pBox[i].y1,
                                                dstX, pBox[i].y1, width,
                                                height);
                        dstX += width;
                        width = min(pBox[i].x2 - dstX, width * 2);
                    }

                    width = pBox[i].x2 - pBox[i].x1;
                    height = min(pBox[i].y2 - dstY, tileHeight);

                    while (dstY < pBox[i].y2) {
                        (*pExaScr->info->Copy) (pPixmap, pBox[i].x1, pBox[i].y1,
                                                pBox[i].x1, dstY, width,
                                                height);
                        dstY += height;
                        height = min(pBox[i].y2 - dstY, height * 2);
                    }
                }

                (*pExaScr->info->DoneCopy) (pPixmap);

                ret = TRUE;
            }
        }

        exaMarkSync(pDrawable->pScreen);

        if (xoff || yoff)
            RegionTranslate(pRegion, -xoff, -yoff);