summaryrefslogtreecommitdiff
path: root/driver/xf86-video-r128/src/r128_exa_render.c
blob: 0d81a09a625bf91a3f7fd707fa881c7f1aa5c0c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*
 * Copyright 2003 Eric Anholt
 * Copyright 2003 Anders Carlsson
 * Copyright 2012 Connor Behan
 * Copyright 2012 Michel Dänzer
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Anders Carlsson <andersca@gnome.org>
 *    Eric Anholt <anholt@FreeBSD.org>
 *    Connor Behan <connor.behan@gmail.com>
 *    Michel Dänzer <michel.daenzer@amd.com>
 *
 */

/* The following is based on the kdrive ATI driver. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if defined(R128DRI) && defined(RENDER)
#include "xf86.h"
#include "exa.h"

#include "r128.h"
#include "r128_reg.h"
#include "r128_rop.h"


static struct {
    Bool dst_alpha;
    Bool src_alpha;
    uint32_t sblend;
    uint32_t dblend;
} R128BlendOp[] = {
    /* Clear */
    {0, 0, R128_ALPHA_BLEND_ZERO        , R128_ALPHA_BLEND_ZERO},
    /* Src */
    {0, 0, R128_ALPHA_BLEND_ONE         , R128_ALPHA_BLEND_ZERO},
    /* Dst */
    {0, 0, R128_ALPHA_BLEND_ZERO        , R128_ALPHA_BLEND_ONE},
    /* Over */
    {0, 1, R128_ALPHA_BLEND_ONE         , R128_ALPHA_BLEND_INVSRCALPHA},
    /* OverReverse */
    {1, 0, R128_ALPHA_BLEND_INVDSTALPHA , R128_ALPHA_BLEND_ONE},
    /* In */
    {1, 0, R128_ALPHA_BLEND_DSTALPHA    , R128_ALPHA_BLEND_ZERO},
    /* InReverse */
    {0, 1, R128_ALPHA_BLEND_ZERO        , R128_ALPHA_BLEND_SRCALPHA},
    /* Out */
    {1, 0, R128_ALPHA_BLEND_INVDSTALPHA , R128_ALPHA_BLEND_ZERO},
    /* OutReverse */
    {0, 1, R128_ALPHA_BLEND_ZERO        , R128_ALPHA_BLEND_INVSRCALPHA},
    /* Atop */
    {1, 1, R128_ALPHA_BLEND_DSTALPHA    , R128_ALPHA_BLEND_INVSRCALPHA},
    /* AtopReverse */
    {1, 1, R128_ALPHA_BLEND_INVDSTALPHA , R128_ALPHA_BLEND_SRCALPHA},
    /* Xor */
    {1, 1, R128_ALPHA_BLEND_INVDSTALPHA , R128_ALPHA_BLEND_INVSRCALPHA},
    /* Add */
    {0, 0, R128_ALPHA_BLEND_ONE         , R128_ALPHA_BLEND_ONE},
};

static Bool
R128TransformAffineOrScaled(PictTransformPtr t)
{
    if (t == NULL) return TRUE;

    /* the shaders don't handle scaling either */
    return t->matrix[2][0] == 0 && t->matrix[2][1] == 0 && t->matrix[2][2] == IntToxFixed(1);
}

static PixmapPtr
R128GetDrawablePixmap(DrawablePtr pDrawable)
{
    if (pDrawable->type == DRAWABLE_WINDOW)
	return pDrawable->pScreen->GetWindowPixmap((WindowPtr)pDrawable);
    else
	return (PixmapPtr)pDrawable;
}

static PixmapPtr
R128SolidPixmap(ScreenPtr pScreen, uint32_t solid)
{
    ScrnInfoPtr   pScrn     = xf86ScreenToScrn(pScreen);
    R128InfoPtr   info      = R128PTR(pScrn);
    PixmapPtr	  pPix	    = pScreen->CreatePixmap(pScreen, 1, 1, 32, 0);

    exaMoveInPixmap(pPix);
    if (!exaDrawableIsOffscreen(&pPix->drawable)) {
        pScreen->DestroyPixmap(pPix);
	return NULL;
    }
    info->ExaDriver->WaitMarker(pScreen, 0);

#if X_BYTE_ORDER == X_BIG_ENDIAN
    if (pScrn->bitsPerPixel == 32)
	R128CopySwap(info->ExaDriver->memoryBase + exaGetPixmapOffset(pPix), (uint8_t*)&solid, 4,
		     APER_0_BIG_ENDIAN_32BPP_SWAP);
    else if (pScrn->bitsPerPixel == 16)
	R128CopySwap(info->ExaDriver->memoryBase + exaGetPixmapOffset(pPix), (uint8_t*)&solid, 4,
		     APER_0_BIG_ENDIAN_16BPP_SWAP);
    else
	/* Fall through for 8 bpp */
#endif
    memcpy(info->ExaDriver->memoryBase + exaGetPixmapOffset(pPix), &solid, 4);

    return pPix;
}

static Bool
R128GetDatatypePict1(uint32_t format, uint32_t *type)
{
    switch(format) {
    case PICT_r5g6b5:
	*type = R128_DATATYPE_RGB565;
	return TRUE;
    case PICT_x1r5g5b5:
	*type = R128_DATATYPE_ARGB1555;
	return TRUE;
    case PICT_x8r8g8b8:
	*type = R128_DATATYPE_ARGB8888;
	return TRUE;
    default:
        return FALSE;
    }
}

static Bool
R128GetDatatypePict2(uint32_t format, uint32_t *type)
{
    switch(format) {
    case PICT_a8:
        *type = R128_DATATYPE_RGB8;
	return TRUE;
    case PICT_r5g6b5:
	*type = R128_DATATYPE_RGB565;
	return TRUE;
    case PICT_a8r8g8b8:
	*type = R128_DATATYPE_ARGB8888;
	return TRUE;
    default:
        return FALSE;
    }
}

static Bool
R128CheckCompositeTexture(PicturePtr pPict, PicturePtr pDstPict, int op)
{
#if R128_DEBUG
    ScreenPtr     pScreen   = pDstPict->pDrawable->pScreen;
    ScrnInfoPtr   pScrn     = xf86ScreenToScrn(pScreen);
#endif

    unsigned int repeatType = pPict->repeat ? pPict->repeatType : RepeatNone;
    uint32_t tmp1;

    if (!R128GetDatatypePict2(pPict->format, &tmp1)) return FALSE;

    if (pPict->pDrawable) {
        int w = pPict->pDrawable->width;
        int h = pPict->pDrawable->height;

        if (pPict->repeat && ((w & (w - 1)) != 0 || (h & (h - 1)) != 0)) {
            DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                                "NPOT repeat unsupported (%dx%d)\n", w, h));
	    return FALSE;
        }
    }

    if (pPict->filter != PictFilterNearest && pPict->filter != PictFilterBilinear) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Unsupported filter 0x%x\n",
                            pPict->filter));
	return FALSE;
    }

    /* The radeon driver has a long explanation about this part that I don't really understand */
    if (pPict->transform != 0 && repeatType == RepeatNone && PICT_FORMAT_A(pPict->format) == 0) {
	if (!(((op == PictOpSrc) || (op == PictOpClear)) && (PICT_FORMAT_A(pDstPict->format) == 0))) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "REPEAT_NONE unsupported for "
                            "transformed xRGB source\n"));
	    return FALSE;
	}
    }
    if (!R128TransformAffineOrScaled(pPict->transform)) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Non-affine transforms not supported\n"));
	return FALSE;
    }

    return TRUE;
}

Bool
R128CCECheckComposite(int op, PicturePtr pSrcPicture, PicturePtr pMaskPicture, PicturePtr pDstPicture)
{
#if R128_DEBUG
    ScreenPtr     pScreen   = pDstPicture->pDrawable->pScreen;
    ScrnInfoPtr   pScrn     = xf86ScreenToScrn(pScreen);
#endif

    PixmapPtr pSrcPixmap, pDstPixmap;
    uint32_t tmp1;

    /* Check for unsupported compositing operations. */
    if (op >= sizeof(R128BlendOp) / sizeof(R128BlendOp[0])) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Unsupported Composite op 0x%x\n", op));
	return FALSE;
    }

    pDstPixmap = R128GetDrawablePixmap(pDstPicture->pDrawable);
    if (pDstPixmap->drawable.width > 1024 || pDstPixmap->drawable.height > 1024) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Dest w/h too large (%d,%d).\n",
                            pDstPixmap->drawable.width,
                            pDstPixmap->drawable.height));
	return FALSE;
    }

    if (pSrcPicture->pDrawable) {
        pSrcPixmap = R128GetDrawablePixmap(pSrcPicture->pDrawable);
        if (pSrcPixmap->drawable.width > 1024 || pSrcPixmap->drawable.height > 1024) {
            DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                                "Source w/h too large (%d,%d).\n",
                                pSrcPixmap->drawable.width,
                                pSrcPixmap->drawable.height));
	    return FALSE;
        }
    } else if (pSrcPicture->pSourcePict->type != SourcePictTypeSolidFill) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Gradient pictures not supported yet\n"));
	return FALSE;
    }

    if (pDstPicture->format == PICT_a8) {
        if (R128BlendOp[op].src_alpha || R128BlendOp[op].dst_alpha || pMaskPicture != NULL) {
            DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                                "Alpha blending unsupported with "
                                "A8 dst?\n"));
	    return FALSE;
	}
    } else {
        if (!R128GetDatatypePict1(pDstPicture->format, &tmp1)) return FALSE;
    }

    if (pMaskPicture) {
        PixmapPtr pMaskPixmap;

        if (pMaskPicture->pDrawable) {
	    pMaskPixmap = R128GetDrawablePixmap(pMaskPicture->pDrawable);
            if (pMaskPixmap->drawable.width > 1024 || pMaskPixmap->drawable.height > 1024) {
                DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                                    "Mask w/h too large (%d,%d).\n",
                                    pMaskPixmap->drawable.width,
                                    pMaskPixmap->drawable.height));
	        return FALSE;
            }
	} else if (pMaskPicture->pSourcePict->type != SourcePictTypeSolidFill) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Gradient pictures not supported yet\n"));
	    return FALSE;
	}

	if (pMaskPicture->componentAlpha && R128BlendOp[op].src_alpha) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Component alpha not supported with "
                            "source alpha blending\n"));
	    return FALSE;
	}

	if (!R128CheckCompositeTexture(pMaskPicture, pDstPicture, op)) return FALSE;
    }

    if (!R128CheckCompositeTexture(pSrcPicture, pDstPicture, op)) return FALSE;
    return TRUE;
}

static Bool
R128TextureSetup(PicturePtr pPict, PixmapPtr pPix, int unit, uint32_t *txsize, uint32_t *tex_cntl_c, Bool trying_solid)
{
    ScreenPtr     pScreen   = pPix->drawable.pScreen;
    ScrnInfoPtr   pScrn     = xf86ScreenToScrn(pScreen);
    R128InfoPtr   info      = R128PTR(pScrn);

    int w, h, bytepp, shift, l2w, l2h, l2p, pitch;

    if (pPict->pDrawable) {
	w = pPict->pDrawable->width;
	h = pPict->pDrawable->height;
    } else {
	w = h = 1;
    }

    pitch = exaGetPixmapPitch(pPix);
    if ((pitch & (pitch - 1)) != 0) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "NPOT pitch 0x%x unsupported\n", pitch));
	return FALSE;
    }

    if (!R128GetDatatypePict2(pPict->format, tex_cntl_c)) return FALSE;

    bytepp = PICT_FORMAT_BPP(pPict->format) / 8;
    *tex_cntl_c |= R128_MIP_MAP_DISABLE;

    if (pPict->filter == PictFilterBilinear) {
        *tex_cntl_c |= R128_MIN_BLEND_LINEAR | R128_MAG_BLEND_LINEAR;
    } else if (pPict->filter == PictFilterNearest) {
	*tex_cntl_c |= R128_MIN_BLEND_NEAREST | R128_MAG_BLEND_NEAREST;
    } else {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Bad filter 0x%x\n", pPict->filter));
	return FALSE;
    }

    if (unit == 0) {
        shift = 0;
    } else {
        shift = 16;
        *tex_cntl_c |= R128_SEC_SELECT_SEC_ST;
    }

    l2w = R128MinBits(w) - 1;
    l2h = R128MinBits(h) - 1;
    l2p = R128MinBits(pitch / bytepp) - 1;

    if (pPict->repeat && w == 1 && h == 1) {
        l2p = 0;
    } else if (pPict->repeat && l2p != l2w) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Repeat not supported for pitch != "
                            "width\n"));
	return FALSE;
    }

    l2w = l2p;
    /* This is required to handle NPOT height */
    if ((unit == 1) || (unit == 0 && !pPict->repeat && !trying_solid)) l2h++;

    info->state_2d.widths[unit] = 1 << l2w;
    info->state_2d.heights[unit] = 1 << l2h;
    *txsize |= l2p << (R128_TEX_PITCH_SHIFT + shift);
    *txsize |= ((w > h) ? l2w : l2h) << (R128_TEX_SIZE_SHIFT + shift);
    *txsize |= l2h << (R128_TEX_HEIGHT_SHIFT + shift);

    if (pPict->transform != 0) {
        info->state_2d.is_transform[unit] = TRUE;
        info->state_2d.transform[unit] = pPict->transform;
    } else {
        info->state_2d.is_transform[unit] = FALSE;
    }

    return TRUE;
}

/* The composite preparation commands that are the same every time can
 * just be written once.
 */
#define COMPOSITE_SETUP()				\
do {							\
    BEGIN_RING( 10 );					\
							\
    OUT_RING_REG(R128_SCALE_3D_CNTL,			\
		    R128_SCALE_3D_TEXMAP_SHADE |	\
		    R128_SCALE_PIX_REPLICATE |		\
		    R128_TEX_CACHE_SPLIT |		\
		    R128_TEX_MAP_ALPHA_IN_TEXTURE |	\
		    R128_TEX_CACHE_LINE_SIZE_4QW);	\
    OUT_RING_REG(R128_SETUP_CNTL,			\
		    R128_COLOR_GOURAUD |		\
		    R128_PRIM_TYPE_TRI |		\
		    R128_TEXTURE_ST_MULT_W |		\
		    R128_STARTING_VERTEX_1 |		\
		    R128_ENDING_VERTEX_3 |		\
		    R128_SUB_PIX_4BITS);		\
    OUT_RING_REG(R128_PM4_VC_FPU_SETUP,			\
		    R128_FRONT_DIR_CCW |		\
		    R128_BACKFACE_SOLID |		\
		    R128_FRONTFACE_SOLID |		\
		    R128_FPU_COLOR_GOURAUD |		\
		    R128_FPU_SUB_PIX_4BITS |		\
		    R128_FPU_MODE_3D |			\
		    R128_TRAP_BITS_DISABLE |		\
		    R128_XFACTOR_2 |			\
		    R128_YFACTOR_2 |			\
		    R128_FLAT_SHADE_VERTEX_OGL |	\
		    R128_FPU_ROUND_TRUNCATE |		\
		    R128_WM_SEL_8DW);			\
    OUT_RING_REG(R128_PLANE_3D_MASK_C, 0xffffffff);	\
    OUT_RING_REG(R128_CONSTANT_COLOR_C, 0xff000000);	\
							\
    ADVANCE_RING();					\
} while(0)

Bool
R128CCEPrepareComposite(int op, PicturePtr pSrcPicture, PicturePtr pMaskPicture,
    PicturePtr pDstPicture, PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
{
    ScreenPtr     pScreen   = pDst->drawable.pScreen;
    ScrnInfoPtr   pScrn     = xf86ScreenToScrn(pScreen);
    R128InfoPtr   info      = R128PTR(pScrn);
    unsigned char *R128MMIO = info->MMIO;
    RING_LOCALS;

    Bool add_src = FALSE;
    Bool add_msk = FALSE;
    uint32_t txsize = 0, prim_tex_cntl_c, sec_tex_cntl_c = 0, dstDatatype;
    uint32_t src_pitch_offset, dst_pitch_offset, color_factor, in_color_factor, alpha_comb;
    uint32_t sblend, dblend, blend_cntl, window_offset;
    int i;

    if (pDstPicture->format == PICT_a8) {
        if (R128BlendOp[op].dst_alpha) {
            DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                                "Can't dst alpha blend A8\n"));
	    return FALSE;
        }
        dstDatatype = R128_DATATYPE_Y8;
    } else {
        if (!R128GetDatatypePict1(pDstPicture->format, &dstDatatype)) return FALSE;
    }

    if (!pSrc) {
	pSrc = R128SolidPixmap(pScreen, cpu_to_le32(pSrcPicture->pSourcePict->solidFill.color));
	if (!pSrc) {
        DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                            "Failed to create solid scratch "
                            "pixmap\n"));
	    return FALSE;
	}
	add_src = TRUE;
    }
    if (pMaskPicture) {
        info->state_2d.has_mask = TRUE;
        if (!pMask) {
	    pMask = R128SolidPixmap(pScreen, cpu_to_le32(pMaskPicture->pSourcePict->solidFill.color));
	    if (!pMask) {
	        if (!pSrcPicture->pDrawable)
		    pScreen->DestroyPixmap(pSrc);
		info->state_2d.has_mask = FALSE;
            DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                                "Failed to create "
                                "solid scratch pixmap\n"));
	        return FALSE;
	    }
	    add_msk = TRUE;
	}
    } else {
        info->state_2d.has_mask = FALSE;
    }

    if (!R128TextureSetup(pSrcPicture, pSrc, 0, &txsize, &prim_tex_cntl_c, (add_src || add_msk))) return FALSE;

    if (pMask != NULL) {
	info->state_2d.has_mask = TRUE;
        if (!R128TextureSetup(pMaskPicture, pMask, 1, &txsize, &sec_tex_cntl_c, (add_src || add_msk))) return FALSE;
    } else {
	info->state_2d.has_mask = FALSE;
	info->state_2d.is_transform[1] = FALSE;
    }

    if (!R128GetPixmapOffsetPitch(pDst, &dst_pitch_offset)) return FALSE;
    if (!R128GetPixmapOffsetPitch(pSrc, &src_pitch_offset)) return FALSE;

    info->state_2d.in_use = TRUE;
    if (add_src) info->state_2d.src_pix = pSrc;
    if (add_msk) info->state_2d.msk_pix = pMask;
    sblend = R128BlendOp[op].sblend;
    dblend = R128BlendOp[op].dblend;
    if (PICT_FORMAT_A(pDstPicture->format) == 0 && R128BlendOp[op].dst_alpha) {
        if (sblend == R128_ALPHA_BLEND_DSTALPHA)
	    sblend = R128_ALPHA_BLEND_ONE;
	else if (sblend == R128_ALPHA_BLEND_INVDSTALPHA)
	    sblend = R128_ALPHA_BLEND_ZERO;
    }
    blend_cntl = (sblend << R128_ALPHA_BLEND_SRC_SHIFT) |
		 (dblend << R128_ALPHA_BLEND_DST_SHIFT);

    R128CCE_REFRESH( pScrn, info );

    if (!info->state_2d.composite_setup) {
        COMPOSITE_SETUP();
        info->state_2d.composite_setup = TRUE;
    }

    /* We cannot guarantee that this register will stay zero - DRI needs it too. */
    if (info->have3DWindows)
        info->ExaDriver->WaitMarker(pScreen, 0);
    window_offset = INREG(R128_WINDOW_XY_OFFSET);
    info->state_2d.x_offset = (window_offset & 0xfff00000) >> R128_WINDOW_X_SHIFT;
    info->state_2d.y_offset = (window_offset & 0x000fffff) >> R128_WINDOW_Y_SHIFT;

    info->state_2d.dp_gui_master_cntl = (R128_GMC_DST_PITCH_OFFSET_CNTL |
        R128_GMC_BRUSH_SOLID_COLOR |
        (dstDatatype >> 8) |
        R128_GMC_SRC_DATATYPE_COLOR |
        R128_ROP[3].rop |
        R128_DP_SRC_SOURCE_MEMORY |
        R128_GMC_3D_FCN_EN |
        R128_GMC_CLR_CMP_CNTL_DIS |
        R128_GMC_AUX_CLIP_DIS |
        R128_GMC_WR_MSK_DIS);
    info->state_2d.dp_cntl = (R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
    info->state_2d.dp_brush_frgd_clr = 0xffffffff;
    info->state_2d.dp_brush_bkgd_clr = 0x00000000;
    info->state_2d.dp_src_frgd_clr = 0xffffffff;
    info->state_2d.dp_src_bkgd_clr = 0x00000000;
    info->state_2d.dp_write_mask = 0xffffffff;
    info->state_2d.dst_pitch_offset = dst_pitch_offset;
    info->state_2d.src_pitch_offset = src_pitch_offset;
    info->state_2d.default_sc_bottom_right = (R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    EmitCCE2DState(pScrn);

    BEGIN_RING( 6 );
    OUT_RING_REG(R128_MISC_3D_STATE_CNTL_REG,
        R128_MISC_SCALE_3D_TEXMAP_SHADE |
        R128_MISC_SCALE_PIX_REPLICATE |
        R128_ALPHA_COMB_ADD_CLAMP |
        blend_cntl);
    OUT_RING_REG(R128_TEX_CNTL_C,
        R128_TEXMAP_ENABLE |
        ((pMask != NULL) ? R128_SEC_TEXMAP_ENABLE : 0) |
        R128_ALPHA_ENABLE |
        R128_TEX_CACHE_FLUSH);
    OUT_RING_REG(R128_PC_GUI_CTLSTAT, R128_PC_FLUSH_GUI);
    ADVANCE_RING();

    /* IN operator: Without a mask, only the first texture unit is enabled.
     * With a mask, we put the source in the first unit and have it pass
     * through as input to the 2nd.  The 2nd unit takes the incoming source
     * pixel and modulates it with either the alpha or each of the channels
     * in the mask, depending on componentAlpha.
     */
    BEGIN_RING( 15 );
    /* R128_PRIM_TEX_CNTL_C,
     * R128_PRIM_TEXTURE_COMBINE_CNTL_C,
     * R128_TEX_SIZE_PITCH_C,
     * R128_PRIM_TEX_0_OFFSET_C - R128_PRIM_TEX_10_OFFSET_C
     */
    OUT_RING(CCE_PACKET0(R128_PRIM_TEX_CNTL_C, 13));
    OUT_RING(prim_tex_cntl_c);

    /* If this is the only stage and the dest is a8, route the alpha result
     * to the color (red channel, in particular), too.  Otherwise, be sure
     * to zero out color channels of an a8 source.
     */
    if (pMaskPicture == NULL && pDstPicture->format == PICT_a8)
        color_factor = R128_COLOR_FACTOR_ALPHA;
    else if (pSrcPicture->format == PICT_a8)
        color_factor = R128_COLOR_FACTOR_CONST_COLOR;
    else
        color_factor = R128_COLOR_FACTOR_TEX;

    if (PICT_FORMAT_A(pSrcPicture->format) == 0)
        alpha_comb = R128_COMB_ALPHA_COPY_INP;
    else
        alpha_comb = R128_COMB_ALPHA_DIS;

    OUT_RING(R128_COMB_COPY |
        color_factor |
        R128_INPUT_FACTOR_INT_COLOR |
        alpha_comb |
        R128_ALPHA_FACTOR_TEX_ALPHA |
        R128_INP_FACTOR_A_CONST_ALPHA);
    OUT_RING(txsize);
    /* We could save some output by only writing the offset register that
     * will actually be used.  On the other hand, this is easy.
     */
    for (i = 0; i <= 10; i++)
        OUT_RING(exaGetPixmapOffset(pSrc));
    ADVANCE_RING();

    if (pMask != NULL) {
        BEGIN_RING( 14 );
	/* R128_SEC_TEX_CNTL_C,
	 * R128_SEC_TEXTURE_COMBINE_CNTL_C,
	 * R128_SEC_TEX_0_OFFSET_C - R128_SEC_TEX_10_OFFSET_C
	 */
        OUT_RING(CCE_PACKET0(R128_SEC_TEX_CNTL_C, 12));
        OUT_RING(sec_tex_cntl_c);

        if (pDstPicture->format == PICT_a8) {
            color_factor = R128_COLOR_FACTOR_ALPHA;
            in_color_factor = R128_INPUT_FACTOR_PREV_ALPHA;
        } else if (pMaskPicture->componentAlpha) {
            color_factor = R128_COLOR_FACTOR_TEX;
            in_color_factor = R128_INPUT_FACTOR_PREV_COLOR;
        } else {
            color_factor = R128_COLOR_FACTOR_ALPHA;
            in_color_factor = R128_INPUT_FACTOR_PREV_COLOR;
        }

        OUT_RING(R128_COMB_MODULATE |
            color_factor |
            in_color_factor |
            R128_COMB_ALPHA_MODULATE |
            R128_ALPHA_FACTOR_TEX_ALPHA |
            R128_INP_FACTOR_A_PREV_ALPHA);
        for (i = 0; i <= 10; i++)
            OUT_RING(exaGetPixmapOffset(pMask));
	ADVANCE_RING();
    }

    return TRUE;
}

typedef union { float f; uint32_t i; } fi_type;

static inline uint32_t
R128FloatAsInt(float val)
{
	fi_type fi;

	fi.f = val;
	return fi.i;
}

#define VTX_OUT_MASK(_dstX, _dstY, _srcX, _srcY, _maskX, _maskY)			\
do {											\
    OUT_RING(R128FloatAsInt((_dstX)));							\
    OUT_RING(R128FloatAsInt(((float)(_dstY)) + 0.125));					\
    OUT_RING(R128FloatAsInt(0.0));							\
    OUT_RING(R128FloatAsInt(1.0));							\
    OUT_RING(R128FloatAsInt((((float)(_srcX)) + 0.5) / (info->state_2d.widths[0])));	\
    OUT_RING(R128FloatAsInt((((float)(_srcY)) + 0.5) / (info->state_2d.heights[0])));	\
    OUT_RING(R128FloatAsInt((((float)(_maskX)) + 0.5) / (info->state_2d.widths[1])));	\
    OUT_RING(R128FloatAsInt((((float)(_maskY)) + 0.5) / (info->state_2d.heights[1])));	\
} while (0)

#define VTX_OUT(_dstX, _dstY, _srcX, _srcY)						\
do {								       			\
    OUT_RING(R128FloatAsInt((_dstX)));							\
    OUT_RING(R128FloatAsInt(((float)(_dstY)) + 0.125));					\
    OUT_RING(R128FloatAsInt(0.0));							\
    OUT_RING(R128FloatAsInt(1.0));							\
    OUT_RING(R128FloatAsInt((((float)(_srcX)) + 0.5) / (info->state_2d.widths[0])));	\
    OUT_RING(R128FloatAsInt((((float)(_srcY)) + 0.5) / (info->state_2d.heights[0])));	\
} while (0)

void
R128CCEComposite(PixmapPtr pDst, int srcX, int srcY, int maskX, int maskY, int dstX, int dstY, int w, int h)
{
    ScreenPtr     pScreen   = pDst->drawable.pScreen;
    ScrnInfoPtr   pScrn     = xf86ScreenToScrn(pScreen);
    R128InfoPtr   info      = R128PTR(pScrn);
    RING_LOCALS;

    int srcXend, srcYend, maskXend, maskYend;
    PictVector v;

    srcXend = srcX + w;
    srcYend = srcY + h;
    maskXend = maskX + w;
    maskYend = maskY + h;
    if (info->state_2d.is_transform[0]) {
        v.vector[0] = IntToxFixed(srcX);
        v.vector[1] = IntToxFixed(srcY);
        v.vector[2] = xFixed1;
        PictureTransformPoint(info->state_2d.transform[0], &v);
        srcX = xFixedToInt(v.vector[0]);
        srcY = xFixedToInt(v.vector[1]);
        v.vector[0] = IntToxFixed(srcXend);
        v.vector[1] = IntToxFixed(srcYend);
        v.vector[2] = xFixed1;
        PictureTransformPoint(info->state_2d.transform[0], &v);
        srcXend = xFixedToInt(v.vector[0]);
        srcYend = xFixedToInt(v.vector[1]);
    }
    if (info->state_2d.is_transform[1]) {
        v.vector[0] = IntToxFixed(maskX);
        v.vector[1] = IntToxFixed(maskY);
        v.vector[2] = xFixed1;
        PictureTransformPoint(info->state_2d.transform[1], &v);
        maskX = xFixedToInt(v.vector[0]);
        maskY = xFixedToInt(v.vector[1]);
        v.vector[0] = IntToxFixed(maskXend);
        v.vector[1] = IntToxFixed(maskYend);
        v.vector[2] = xFixed1;
        PictureTransformPoint(info->state_2d.transform[1], &v);
        maskXend = xFixedToInt(v.vector[0]);
        maskYend = xFixedToInt(v.vector[1]);
    }

    dstX -= info->state_2d.x_offset;
    dstY -= info->state_2d.y_offset;

    R128CCE_REFRESH( pScrn, info );

    if (info->state_2d.has_mask) {
        BEGIN_RING( 3 + 4 * 8 );
        OUT_RING(CCE_PACKET3(R128_CCE_PACKET3_3D_RNDR_GEN_PRIM, 1 + 4 * 8));

	OUT_RING(R128_CCE_VC_FRMT_RHW |
            R128_CCE_VC_FRMT_S_T |
            R128_CCE_VC_FRMT_S2_T2);
    } else {
        BEGIN_RING( 3 + 4 * 6 );
        OUT_RING(CCE_PACKET3(R128_CCE_PACKET3_3D_RNDR_GEN_PRIM, 1 + 4 * 6));

	OUT_RING(R128_CCE_VC_FRMT_RHW |
            R128_CCE_VC_FRMT_S_T);
    }

    OUT_RING(R128_CCE_VC_CNTL_PRIM_TYPE_TRI_FAN |
        R128_CCE_VC_CNTL_PRIM_WALK_RING |
        (4 << R128_CCE_VC_CNTL_NUM_SHIFT));

    if (info->state_2d.has_mask) {
	VTX_OUT_MASK(dstX,     dstY,     srcX,    srcY,    maskX,    maskY);
	VTX_OUT_MASK(dstX,     dstY + h, srcX,    srcYend, maskX,    maskYend);
	VTX_OUT_MASK(dstX + w, dstY + h, srcXend, srcYend, maskXend, maskYend);
	VTX_OUT_MASK(dstX + w, dstY,     srcXend, srcY,    maskXend, maskY);
    } else {
	VTX_OUT(dstX,     dstY,     srcX,    srcY);
	VTX_OUT(dstX,     dstY + h, srcX,    srcYend);
	VTX_OUT(dstX + w, dstY + h, srcXend, srcYend);
	VTX_OUT(dstX + w, dstY,     srcXend, srcY);
    }

    ADVANCE_RING();
}

#endif