summaryrefslogtreecommitdiff
path: root/src/lx_exa.c
blob: 1f017176c2dcb34beb1eccd4691cc3b357b694a3 (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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
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
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/*
 * Copyright (c) 2007-2008 Advanced Micro Devices, Inc.
 *
 * 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 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.
 *
 * Neither the name of the Advanced Micro Devices, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 */

/* TODO:
   Support a8 as a source or destination?
   convert !a8 or !a4 masks?
   support multiple pass operations?
*/

/* To support PictOptAdd with a mask */

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

#include "xf86.h"
#include "exa.h"

#include "geode.h"
#include "cim_defs.h"
#include "cim_regs.h"

#include "geode_blend.h"

#define F(x)    IntToxFixed(x)
#define I(x)    xFixedToInt(x)

#define GEODE_TRACE_FALL 0

#if GEODE_TRACE_FALL
#define GEODE_FALLBACK(x)               \
do {                                    \
	ErrorF("%s: ", __FUNCTION__);   \
	ErrorF x;                       \
	return FALSE;                   \
} while (0)
#else
#define GEODE_FALLBACK(x) return FALSE
#endif

static const struct exa_format_t {
    int exa;
    int bpp;
    int fmt;
    int alphabits;
} lx_exa_formats[] = {
    {
    PICT_a8r8g8b8, 32, CIMGP_SOURCE_FMT_8_8_8_8, 8}, {
    PICT_x8r8g8b8, 32, CIMGP_SOURCE_FMT_8_8_8_8, 0}, {
    PICT_x8b8g8r8, 32, CIMGP_SOURCE_FMT_32BPP_BGR, 0}, {
    PICT_a4r4g4b4, 16, CIMGP_SOURCE_FMT_4_4_4_4, 4}, {
    PICT_a1r5g5b5, 16, CIMGP_SOURCE_FMT_1_5_5_5, 1}, {
    PICT_r5g6b5, 16, CIMGP_SOURCE_FMT_0_5_6_5, 0}, {
    PICT_b5g6r5, 16, CIMGP_SOURCE_FMT_16BPP_BGR, 0}, {
    PICT_x1r5g5b5, 16, CIMGP_SOURCE_FMT_1_5_5_5, 0}, {
    PICT_x1b5g5r5, 16, CIMGP_SOURCE_FMT_15BPP_BGR, 0}, {
    PICT_r3g3b2, 8, CIMGP_SOURCE_FMT_3_3_2, 0}, {
    PICT_a8, 32, CIMGP_SOURCE_FMT_8_8_8_8, 8}
};

/* This is a chunk of memory we use for scratch space */

#define COMP_TYPE_MASK 0
#define COMP_TYPE_ONEPASS 1
#define COMP_TYPE_TWOPASS 3
#define COMP_TYPE_ROTATE  5

static struct {
    int type;

    unsigned int srcOffset;
    unsigned int srcPitch;
    unsigned int srcBpp;
    unsigned int srcWidth, srcHeight;
    PixmapPtr srcPixmap;

    unsigned int srcColor;
    int op;
    int repeat;
    int maskrepeat;
    unsigned int fourBpp;
    unsigned int bufferOffset;
    struct exa_format_t *srcFormat;
    struct exa_format_t *dstFormat;

    int rotate;
    PictTransform *transform;

} exaScratch;

static const int SDfn[16] = {
    0x00, 0x88, 0x44, 0xCC, 0x22, 0xAA, 0x66, 0xEE,
    0x11, 0x99, 0x55, 0xDD, 0x33, 0xBB, 0x77, 0xFF
};

static const int SDfn_PM[16] = {
    0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
    0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA
};

/* These functions check to see if we can safely prefetch the memory
 * for the blt, or if we have to wait the previous blt to complete.
 * One function is for the fill, and the other is for the copy because
 * they have different requirements based on ROP
 */

static int lx0 = -1, ly0 = -1, lx1 = -1, ly1 = -1;

static int
lx_fill_flags(int x0, int y0, int w, int h, int rop)
{
    int x1 = x0 + w, y1 = y0 + h;
    int n = ((rop ^ (rop >> 1)) & 0x55) == 0 || /* no dst */
        x0 >= lx1 || y0 >= ly1 ||       /* rght/below */
        x1 <= lx0 || y1 <= ly0 ?        /* left/above */
        0 : CIMGP_BLTFLAGS_HAZARD;

    lx0 = x0;
    ly0 = y0;
    lx1 = x1;
    ly1 = y1;

    return n;
}

static int
lx_copy_flags(int x0, int y0, int x1, int y1, int w, int h, int rop)
{
    int x2 = x1 + w, y2 = y1 + h;

    /* dst not hazzard and src not hazzard */
    int n = (((rop ^ (rop >> 1)) & 0x55) == 0 ||
             x1 >= lx1 || y1 >= ly1 ||
             x2 <= lx0 || y2 <= ly0) &&
        (((rop ^ (rop >> 2)) & 0x33) == 0 ||
         x0 >= lx1 || y0 >= ly1 ||
         x0 + w <= lx0 || y0 + h <= ly0) ? 0 : CIMGP_BLTFLAGS_HAZARD;

    lx0 = x1;
    ly0 = y1;
    lx1 = x2;
    ly1 = y2;

    return n;
}

/* These are borrowed from the exa engine - they should be made global
   and available to drivers, but until then....
*/

/* exaGetPixelFromRGBA (exa_render.c) */

static Bool
_GetPixelFromRGBA(CARD32 *pixel,
                  CARD16 red, CARD16 green, CARD16 blue, CARD16 alpha,
                  CARD32 format)
{
    int rbits, bbits, gbits, abits;
    int rshift, bshift, gshift, ashift;

    *pixel = 0;

    if (!PICT_FORMAT_COLOR(format))
        return FALSE;

    rbits = PICT_FORMAT_R(format);
    gbits = PICT_FORMAT_G(format);
    bbits = PICT_FORMAT_B(format);
    abits = PICT_FORMAT_A(format);

    if (PICT_FORMAT_TYPE(format) == PICT_TYPE_ARGB) {
        bshift = 0;
        gshift = bbits;
        rshift = gshift + gbits;
        ashift = rshift + rbits;
    }
    else {                      /* PICT_TYPE_ABGR */
        rshift = 0;
        gshift = rbits;
        bshift = gshift + gbits;
        ashift = bshift + bbits;
    }

    *pixel |= (blue >> (16 - bbits)) << bshift;
    *pixel |= (red >> (16 - rbits)) << rshift;
    *pixel |= (green >> (16 - gbits)) << gshift;
    *pixel |= (alpha >> (16 - abits)) << ashift;

    return TRUE;
}

/* exaGetRGBAFromPixel (exa_render.c) */

static Bool
_GetRGBAFromPixel(CARD32 pixel,
                  CARD16 *red,
                  CARD16 *green, CARD16 *blue, CARD16 *alpha, CARD32 format)
{
    int rbits, bbits, gbits, abits;
    int rshift, bshift, gshift, ashift;

    if (!PICT_FORMAT_COLOR(format))
        return FALSE;

    rbits = PICT_FORMAT_R(format);
    gbits = PICT_FORMAT_G(format);
    bbits = PICT_FORMAT_B(format);
    abits = PICT_FORMAT_A(format);

    if (PICT_FORMAT_TYPE(format) == PICT_TYPE_ARGB) {
        bshift = 0;
        gshift = bbits;
        rshift = gshift + gbits;
        ashift = rshift + rbits;
    }
    else {                      /* PICT_TYPE_ABGR */
        rshift = 0;
        gshift = rbits;
        bshift = gshift + gbits;
        ashift = bshift + bbits;
    }

    *red = ((pixel >> rshift) & ((1 << rbits) - 1)) << (16 - rbits);
    while (rbits < 16) {
        *red |= *red >> rbits;
        rbits <<= 1;
    }

    *green = ((pixel >> gshift) & ((1 << gbits) - 1)) << (16 - gbits);
    while (gbits < 16) {
        *green |= *green >> gbits;
        gbits <<= 1;
    }

    *blue = ((pixel >> bshift) & ((1 << bbits) - 1)) << (16 - bbits);
    while (bbits < 16) {
        *blue |= *blue >> bbits;
        bbits <<= 1;
    }

    if (abits) {
        *alpha = ((pixel >> ashift) & ((1 << abits) - 1)) << (16 - abits);
        while (abits < 16) {
            *alpha |= *alpha >> abits;
            abits <<= 1;
        }
    }
    else
        *alpha = 0xffff;

    return TRUE;
}

static unsigned int
lx_get_source_color(PixmapPtr pSrc, int srcFormat, int dstFormat)
{
    CARD32 in, out;
    CARD16 red = 0, green = 0, blue = 0, alpha = 0;

    /* Stall to avoid a race with the upload function */
    /* for 1.4 and newer, the problem will be resolved within
     * exaGetPixmapFirstPixel, so this should be adjusted so
     * the stall isn't run needlessly
     */

    gp_wait_until_idle();
    in = exaGetPixmapFirstPixel(pSrc);

    _GetRGBAFromPixel(in, &red, &blue, &green, &alpha, srcFormat);
    _GetPixelFromRGBA(&out, red, blue, green, alpha, dstFormat);

    return out;
}

static Bool
lx_prepare_solid(PixmapPtr pxMap, int alu, Pixel planemask, Pixel fg)
{
    int pitch = exaGetPixmapPitch(pxMap);
    int op = (planemask == ~0U) ? SDfn[alu] : SDfn_PM[alu];

    gp_declare_blt(0);
    gp_set_bpp(pxMap->drawable.bitsPerPixel);

    gp_set_raster_operation(op);

    if (planemask != ~0U)
        gp_set_solid_pattern(planemask);

    exaScratch.op = op;

    gp_set_solid_source(fg);

    gp_set_strides(pitch, pitch);
    gp_write_parameters();
    return TRUE;
}

static void
lx_do_solid(PixmapPtr pxMap, int x1, int y1, int x2, int y2)
{
    int bpp = (pxMap->drawable.bitsPerPixel + 7) / 8;
    int pitch = exaGetPixmapPitch(pxMap);
    unsigned int offset = exaGetPixmapOffset(pxMap) + (pitch * y1) + (bpp * x1);

    gp_declare_blt(lx_fill_flags(x1, y1, x2 - x1, y2 - y1, exaScratch.op));
    gp_pattern_fill(offset, x2 - x1, y2 - y1);
}

static Bool
lx_prepare_copy(PixmapPtr pxSrc, PixmapPtr pxDst, int dx, int dy,
                int alu, Pixel planemask)
{
    int dpitch = exaGetPixmapPitch(pxDst);
    int op = (planemask == ~0U) ? SDfn[alu] : SDfn_PM[alu];

    gp_declare_blt(0);
    gp_set_bpp(pxDst->drawable.bitsPerPixel);

    gp_set_raster_operation(op);

    if (planemask != ~0U)
        gp_set_solid_pattern(planemask);

    exaScratch.srcOffset = exaGetPixmapOffset(pxSrc);
    exaScratch.srcPitch = exaGetPixmapPitch(pxSrc);
    exaScratch.srcBpp = (pxSrc->drawable.bitsPerPixel + 7) / 8;

    exaScratch.op = op;

    gp_set_strides(dpitch, exaScratch.srcPitch);
    gp_write_parameters();
    return TRUE;
}

static void
lx_do_copy(PixmapPtr pxDst, int srcX, int srcY,
           int dstX, int dstY, int w, int h)
{
    int dstBpp = (pxDst->drawable.bitsPerPixel + 7) / 8;
    int dstPitch = exaGetPixmapPitch(pxDst);
    unsigned int srcOffset, dstOffset;
    int flags = 0;

    gp_declare_blt(lx_copy_flags(srcX, srcY, dstX, dstY, w, h, exaScratch.op));

    srcOffset = exaScratch.srcOffset + (exaScratch.srcPitch * srcY) +
        (exaScratch.srcBpp) * srcX;

    dstOffset = exaGetPixmapOffset(pxDst) + (dstPitch * dstY) + (dstBpp * dstX);

    if (dstX > srcX)
        flags |= CIMGP_NEGXDIR;

    if (dstY > srcY)
        flags |= CIMGP_NEGYDIR;

    gp_screen_to_screen_blt(dstOffset, srcOffset, w, h, flags);
}

/* Composite operations

These are the simplest - one pass operations - if there is no format or
mask, the we can make these happen pretty fast

                       Operation  Type  Channel   Alpha
PictOpClear            0          2     0         3
PictOpSrc              0          3     0         3
PictOpDst              0          3     1         3
PictOpOver             2          0     0         3
PictOpOverReverse      2          0     1         3
PictOpIn               0          1     0         3
PictOpInReverse        0          1     1         3
PictOpOut              1          0     0         3
PictOpOutReverse       1          0     1         3
PictOpAdd              2          2     0         3

The following require multiple passes
PictOpAtop
PictOpXor
*/

struct blend_ops_t {
    int operation;
    int type;
    int channel;
} lx_alpha_ops[] = {
    /* PictOpClear */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONSTANT_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    },
        /* PictOpSrc */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_ALPHA_EQUALS_ONE, CIMGP_CHANNEL_A_SOURCE}, {
    },
        /* PictOpDst */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_ALPHA_EQUALS_ONE, CIMGP_CHANNEL_A_DEST}, {
    },
        /* PictOpOver */
    {
    CIMGP_A_PLUS_BETA_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONVERTED_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* PictOpOverReverse */
    {
    CIMGP_A_PLUS_BETA_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_DEST}, {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONVERTED_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* PictOpIn */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_CHANNEL_B_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONVERTED_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* PictOpInReverse */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_CHANNEL_B_ALPHA, CIMGP_CHANNEL_A_DEST}, {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONVERTED_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* PictOpOut */
    {
    CIMGP_BETA_TIMES_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_DEST}, {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONVERTED_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* PictOpOutReverse */
    {
    CIMGP_BETA_TIMES_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    CIMGP_ALPHA_TIMES_A, CIMGP_CONVERTED_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* SrcAtop */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_CHANNEL_B_ALPHA, CIMGP_CHANNEL_A_DEST}, {
    CIMGP_BETA_TIMES_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* SrcAtopReverse */
    {
    CIMGP_ALPHA_TIMES_A, CIMGP_CHANNEL_B_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    CIMGP_BETA_TIMES_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_DEST},
        /* Xor */
    {
    CIMGP_BETA_TIMES_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    CIMGP_BETA_TIMES_B, CIMGP_CHANNEL_A_ALPHA, CIMGP_CHANNEL_A_SOURCE},
        /* PictOpAdd */
    {
    CIMGP_A_PLUS_BETA_B, CIMGP_CONSTANT_ALPHA, CIMGP_CHANNEL_A_SOURCE}, {
    }
};

#define ARRAY_SIZE(a) (sizeof((a)) / (sizeof(*(a))))

static const struct exa_format_t *
lx_get_format(PicturePtr p)
{
    int i;
    unsigned int format = p->format;

    for (i = 0; i < ARRAY_SIZE(lx_exa_formats); i++)
        if (lx_exa_formats[i].exa == format)
            return (&lx_exa_formats[i]);

    return NULL;
}

static Bool
lx_process_transform(PicturePtr pSrc)
{
    PictTransformPtr t = pSrc->transform;
    xFixed c0 = t->matrix[0][0];
    xFixed s0 = t->matrix[0][1];
    xFixed s1 = t->matrix[1][0];
    xFixed c1 = t->matrix[1][1];

    /* If the transform doesn't have any rotation
     * or scaling components, then just grab the
     * translate coordinates */

    if (t->matrix[0][0] == 0 &&
        t->matrix[0][1] == 0 && t->matrix[1][0] == 0 && t->matrix[1][1] == 0) {
        exaScratch.transform = pSrc->transform;
        return TRUE;
    }

    /* Otherwise, see if this is a simple
     * rotate transform - if it isn't, then
     * we have to punt back to software */

    if (t->matrix[2][2] != F(1))
        return FALSE;

    /* The rotate matrix looks like this:
     * [ cos X   -sin x 
     * sin X   cos X ]
     * 
     * Where X is the angle.  We do a simple
     * check first - if [0,0] != [1,1], then 
     * scaling was specified too, and we can 
     * bail, and if [0,1] != -[1,1] then this
     * isn't scaling that we can handle.
     */

    if ((c0 != c1) || (s0 != -s1))
        return FALSE;

    /* Now, figure out what angle we want - we
     * can only accelerate right angle rotations,
     * so this turns into an easy set of if statements */

    if (c0 == F(1) && s1 == F(0))
        exaScratch.rotate = RR_Rotate_0;
    else if (c0 == F(0) && s1 == F(1))
        exaScratch.rotate = RR_Rotate_90;
    else if (c0 == F(-1) && s1 == F(0))
        exaScratch.rotate = RR_Rotate_180;
    else if (c0 == F(0) && s1 == F(-1))
        exaScratch.rotate = RR_Rotate_270;
    else
        return FALSE;

    exaScratch.transform = pSrc->transform;

    return TRUE;
}

static Bool
lx_check_composite(int op, PicturePtr pSrc, PicturePtr pMsk, PicturePtr pDst)
{
    GeodeRec *pGeode = GEODEPTR_FROM_PICTURE(pDst);
    const struct exa_format_t *srcFmt, *dstFmt;

    if (op > PictOpAdd)
        GEODE_FALLBACK(("Operation %d is not supported\n", op));

    /* FIXME: Meet this conditions from the debug for PictOpAdd.
     * Any Other possibilities? Add a judge for the future supplement */
    if (op == PictOpAdd && pSrc->format == PICT_a8r8g8b8 &&
        pDst->format == PICT_a8 && !pMsk)
        return TRUE;

    if (op == PictOpAdd && pSrc->format == PICT_x8r8g8b8 &&
        pDst->format == PICT_a8 && !pMsk)
        return TRUE;

    if (op == PictOpAdd && pSrc->format == PICT_r5g6b5 &&
        pDst->format == PICT_a8 && !pMsk)
        return TRUE;

    if (usesPasses(op)) {
        if (pGeode->exaBfrOffset == 0 || !pMsk)
            GEODE_FALLBACK(("Multipass operation requires off-screen buffer\n"));
    }

    if (pMsk && op == PictOpAdd)
        GEODE_FALLBACK(("PictOpAdd with mask is not supported\n"));

    /* Check that the filter matches what we support */

    switch (pSrc->filter) {
    case PictFilterNearest:
    case PictFilterFast:
    case PictFilterGood:
    case PictFilterBest:
        break;

    default:
        GEODE_FALLBACK(("Bilinear or convolution filters are not supported\n"));
    }

    if (pMsk && pMsk->transform)
        GEODE_FALLBACK(("Mask transforms are not supported\n"));

    /* XXX - don't know if we can do any hwaccel on solid fills or gradient types */
    if (pSrc->pSourcePict || (pMsk && pMsk->pSourcePict))
        GEODE_FALLBACK(("Solid fills or gradient types are not supported\n"));

    /* Keep an eye out for source rotation transforms - those we can
     * do something about */

    exaScratch.rotate = RR_Rotate_0;
    exaScratch.transform = NULL;

    if (pSrc->transform && !lx_process_transform(pSrc))
        GEODE_FALLBACK(("Transform operation is non-trivial\n"));

    /* XXX - I don't understand PICT_a8 enough - so I'm punting */
    if ((op != PictOpAdd) && (pSrc->format == PICT_a8 ||
                              pDst->format == PICT_a8))
        GEODE_FALLBACK(("PICT_a8 as src or dst format is unsupported\n"));

    if (pMsk && op != PictOpClear) {
        struct blend_ops_t *opPtr = &lx_alpha_ops[op * 2];
        int direction = (opPtr->channel == CIMGP_CHANNEL_A_SOURCE) ? 0 : 1;

        /* Direction 0 indicates src->dst, 1 indiates dst->src */
        if (((direction == 0) && (pSrc->pDrawable->bitsPerPixel < 16)) ||
            ((direction == 1) && (pDst->pDrawable->bitsPerPixel < 16))) {
            ErrorF("Mask blending unsupported with <16bpp\n");
            return FALSE;
        }
        if (pMsk->format != PICT_a8 && pMsk->format != PICT_a4)
            GEODE_FALLBACK(("Masks can be only done with a 8bpp or 4bpp depth\n"));

        /* The pSrc should be 1x1 pixel if the pMsk is not zero */
        if (pSrc->pDrawable->width != 1 || pSrc->pDrawable->height != 1)
            GEODE_FALLBACK(("pSrc should be 1x1 pixel if pMsk is not zero\n"));
        /* FIXME: In lx_prepare_composite, there are no variables to record the
         * one pixel source's width and height when the mask is not zero.
         * That will lead to bigger region to render instead of one pixel in lx
         * _do_composite, so we should fallback currently to avoid this */
        if (!pSrc->repeat)
            GEODE_FALLBACK(("FIXME: unzero mask might lead to bigger rendering region than 1x1 pixels\n"));
    }

    /* Get the formats for the source and destination */

    if ((srcFmt = lx_get_format(pSrc)) == NULL) {
        ErrorF("EXA: Invalid source format %x\n", pSrc->format);
        return FALSE;
    }

    if ((dstFmt = lx_get_format(pDst)) == NULL) {
        ErrorF("EXA: Invalid destination format %x\n", pDst->format);
        return FALSE;
    }

    /* Make sure operations that need alpha bits have them */
    /* If a mask is enabled, the alpha will come from there */

    if (!pMsk && (!srcFmt->alphabits && usesSrcAlpha(op)))
        GEODE_FALLBACK(("Operation requires src alpha, but alphabits is unset\n"));

    if (!pMsk && (!dstFmt->alphabits && usesDstAlpha(op)))
        GEODE_FALLBACK(("Operation requires dst alpha, but alphabits is unset\n"));

    /* FIXME: See a way around this! */
    if (srcFmt->alphabits == 0 && dstFmt->alphabits != 0)
        GEODE_FALLBACK(("src_alphabits=0, dst_alphabits!=0\n"));

    /* If this is a rotate operation, then make sure the src and dst
     * formats are the same */
    if (exaScratch.rotate != RR_Rotate_0 && srcFmt != dstFmt) {
        ErrorF("EXA: Unable to rotate and convert formats at the same time\n");
        return FALSE;
    }
    return TRUE;
}

static Bool
lx_prepare_composite(int op, PicturePtr pSrc, PicturePtr pMsk,
                     PicturePtr pDst, PixmapPtr pxSrc, PixmapPtr pxMsk,
                     PixmapPtr pxDst)
{
    GeodeRec *pGeode = GEODEPTR_FROM_PIXMAP(pxDst);
    const struct exa_format_t *srcFmt, *dstFmt;

    /* Get the formats for the source and destination */

    srcFmt = lx_get_format(pSrc);
    dstFmt = lx_get_format(pDst);

    /* Set up the scratch buffer with the information we need */

    exaScratch.srcFormat = (struct exa_format_t *) srcFmt;
    exaScratch.dstFormat = (struct exa_format_t *) dstFmt;
    exaScratch.op = op;
    exaScratch.repeat = pSrc->repeat;
    exaScratch.bufferOffset = pGeode->exaBfrOffset;

    if (pMsk && op != PictOpClear) {
        /* Get the source color */
        /* If the op is PictOpOver(or PictOpOutReverse, PictOpInReverse,
         * PictOpIn, PictOpOut, PictOpOverReverse), we should get the
         * ARGB32 source format */

        if ((op == PictOpOver || op == PictOpOutReverse || op ==
             PictOpInReverse || op == PictOpIn || op == PictOpOut ||
             op == PictOpOverReverse) && (srcFmt->alphabits != 0))
            exaScratch.srcColor = exaGetPixmapFirstPixel(pxSrc);
        else if ((op == PictOpOver || op == PictOpOutReverse || op ==
                  PictOpInReverse || op == PictOpIn || op == PictOpOut ||
                  op == PictOpOverReverse) && (srcFmt->alphabits == 0))
            exaScratch.srcColor = lx_get_source_color(pxSrc, pSrc->format,
                                                      PICT_a8r8g8b8);
        else
            exaScratch.srcColor = lx_get_source_color(pxSrc, pSrc->format,
                                                      pDst->format);

        /* Save off the info we need (reuse the source values to save space) */

        exaScratch.type = COMP_TYPE_MASK;
        exaScratch.maskrepeat = pMsk->repeat;

        exaScratch.srcOffset = exaGetPixmapOffset(pxMsk);
        exaScratch.srcPitch = exaGetPixmapPitch(pxMsk);
        exaScratch.srcBpp = (pxMsk->drawable.bitsPerPixel + 7) / 8;

        exaScratch.srcWidth = pMsk->pDrawable->width;
        exaScratch.srcHeight = pMsk->pDrawable->height;

        /* Flag to indicate if this a 8BPP or a 4BPP mask */
        exaScratch.fourBpp = (pxMsk->drawable.bitsPerPixel == 4) ? 1 : 0;
    }
    else {
        if (usesPasses(op))
            exaScratch.type = COMP_TYPE_TWOPASS;
        else if (exaScratch.rotate != RR_Rotate_0)
            exaScratch.type = COMP_TYPE_ROTATE;
        else
            exaScratch.type = COMP_TYPE_ONEPASS;

        exaScratch.srcOffset = exaGetPixmapOffset(pxSrc);
        exaScratch.srcPitch = exaGetPixmapPitch(pxSrc);
        exaScratch.srcBpp = (pxSrc->drawable.bitsPerPixel + 7) / 8;

        exaScratch.srcWidth = pSrc->pDrawable->width;
        exaScratch.srcHeight = pSrc->pDrawable->height;
    }

    return TRUE;
}

static int
lx_get_bpp_from_format(int format)
{

    switch (format) {
    case CIMGP_SOURCE_FMT_8_8_8_8:
    case CIMGP_SOURCE_FMT_32BPP_BGR:
        return 32;

    case CIMGP_SOURCE_FMT_4_4_4_4:
        return 12;

    case CIMGP_SOURCE_FMT_0_5_6_5:
    case CIMGP_SOURCE_FMT_16BPP_BGR:
        return 16;

    case CIMGP_SOURCE_FMT_1_5_5_5:
    case CIMGP_SOURCE_FMT_15BPP_BGR:
        return 15;

    case CIMGP_SOURCE_FMT_3_3_2:
        return 8;
    }

    return 0;
}

/* BGR needs to be set in the source for it to take - so adjust the source
 * to enable BGR if the two formats are different, and disable it if they
 * are the same
 */

static void
lx_set_source_format(int srcFormat, int dstFormat)
{
    if (!(srcFormat & 0x10) && (dstFormat & 0x10))
        gp_set_source_format(srcFormat | 0x10);
    else if ((srcFormat & 0x10) && (dstFormat & 0x10))
        gp_set_source_format(srcFormat & ~0x10);
    else
        gp_set_source_format(srcFormat);
}

/* If we are converting colors and we need the channel A alpha,
 * then use a special alpha type that preserves the alpha before
 * converting the format
 */

static inline int
get_op_type(struct exa_format_t *src, struct exa_format_t *dst, int type)
{
    return (type == CIMGP_CHANNEL_A_ALPHA &&
            src->alphabits != dst->alphabits) ? CIMGP_CONVERTED_ALPHA : type;
}

/* Note - this is the preferred onepass method.  The other will remain
 * ifdefed out until such time that we are sure its not needed
 */

#define GetPixmapOffset(px, x, y) ( exaGetPixmapOffset((px)) + \
  (exaGetPixmapPitch((px)) * (y)) + \
  ((((px)->drawable.bitsPerPixel + 7) / 8) * (x)) )

#define GetSrcOffset(_x, _y) (exaScratch.srcOffset + ((_y) * exaScratch.srcPitch) + \
			      ((_x) * exaScratch.srcBpp))

static void
lx_composite_onepass_add_a8(PixmapPtr pxDst, unsigned long dstOffset,
                            unsigned long srcOffset, int width, int height,
                            int opX, int opY, int srcX, int srcY)
{
    struct blend_ops_t *opPtr;
    int apply, type;
    int optempX, optempY;
    int i, j;
    unsigned long pixmapOffset, pixmapPitch, calBitsPixel;

    pixmapOffset = exaGetPixmapOffset(pxDst);
    pixmapPitch = exaGetPixmapPitch(pxDst);
    calBitsPixel = (pxDst->drawable.bitsPerPixel + 7) / 8;

    /* Keep this GP idle judge here. Otherwise the SW method has chance to
     * conflict with the HW rendering method */
    gp_wait_until_idle();

    if (opX % 4 == 0 && srcX % 4 == 0) {
        /* HW acceleration */
        opPtr = &lx_alpha_ops[exaScratch.op * 2];
        apply = CIMGP_APPLY_BLEND_TO_ALL;
        gp_declare_blt(0);
        gp_set_bpp(32);
        gp_set_strides(exaGetPixmapPitch(pxDst), exaScratch.srcPitch);
        gp_set_source_format(8);
        type = opPtr->type;
        gp_set_alpha_operation(opPtr->operation, type, opPtr->channel, apply,
                               0);
        gp_screen_to_screen_convert(dstOffset, srcOffset, width / 4, height, 0);
        /* Calculate the pixels in the tail of each line */
        for (j = srcY; j < srcY + height; j++)
            for (i = srcX + (width / 4) * 4; i < srcX + width; i++) {
                srcOffset = GetSrcOffset(i, j);
                optempX = opX + i - srcX;
                optempY = opY + j - srcY;
                dstOffset = pixmapOffset + pixmapPitch * optempY +
                    calBitsPixel * optempX;
                *(cim_fb_ptr + dstOffset) = (*(cim_fb_ptr + srcOffset)
                                             + *(cim_fb_ptr + dstOffset) <=
                                             0xff) ? *(cim_fb_ptr + srcOffset) +
                    *(cim_fb_ptr + dstOffset) : 0xff;
            }
    }
    else {
        for (j = srcY; j < srcY + height; j++)
            for (i = srcX; i < srcX + width; i++) {
                srcOffset = GetSrcOffset(i, j);
                optempX = opX + i - srcX;
                optempY = opY + j - srcY;
                dstOffset = pixmapOffset + pixmapPitch * optempY +
                    calBitsPixel * optempX;
                *(cim_fb_ptr + dstOffset) = (*(cim_fb_ptr + srcOffset) +
                                             *(cim_fb_ptr + dstOffset) <=
                                             0xff) ? *(cim_fb_ptr + srcOffset) +
                    *(cim_fb_ptr + dstOffset) : 0xff;
            }
    }
}

static void
lx_composite_onepass(PixmapPtr pxDst, unsigned long dstOffset,
                     unsigned long srcOffset, int width, int height)
{
    struct blend_ops_t *opPtr;
    int apply, type;

    opPtr = &lx_alpha_ops[exaScratch.op * 2];

    apply = (exaScratch.dstFormat->alphabits != 0 &&
             exaScratch.srcFormat->alphabits != 0) ?
        CIMGP_APPLY_BLEND_TO_ALL : CIMGP_APPLY_BLEND_TO_RGB;

    gp_declare_blt(0);
    gp_set_bpp(lx_get_bpp_from_format(exaScratch.dstFormat->fmt));
    gp_set_strides(exaGetPixmapPitch(pxDst), exaScratch.srcPitch);

    lx_set_source_format(exaScratch.srcFormat->fmt, exaScratch.dstFormat->fmt);

    type = get_op_type(exaScratch.srcFormat, exaScratch.dstFormat, opPtr->type);

    gp_set_alpha_operation(opPtr->operation, type, opPtr->channel, apply, 0);

    gp_screen_to_screen_convert(dstOffset, srcOffset, width, height, 0);
}

static void
lx_composite_all_black(unsigned long srcOffset, int width, int height)
{
    struct blend_ops_t *opPtr;
    int apply, type;

    opPtr = &lx_alpha_ops[0];
    apply = (exaScratch.srcFormat->alphabits != 0) ?
        CIMGP_APPLY_BLEND_TO_ALL : CIMGP_APPLY_BLEND_TO_RGB;
    gp_declare_blt(0);
    gp_set_bpp(lx_get_bpp_from_format(exaScratch.srcFormat->fmt));
    gp_set_strides(exaScratch.srcPitch, exaScratch.srcPitch);
    lx_set_source_format(exaScratch.srcFormat->fmt, exaScratch.srcFormat->fmt);
    type = get_op_type(exaScratch.srcFormat, exaScratch.srcFormat, opPtr->type);
    gp_set_alpha_operation(opPtr->operation, type, opPtr->channel, apply, 0);
    gp_screen_to_screen_convert(srcOffset, srcOffset, width, height, 0);

}

static void
lx_composite_onepass_special(PixmapPtr pxDst, int width, int height, int opX,
                             int opY, int srcX, int srcY)
{
    struct blend_ops_t *opPtr;
    int apply, type;
    int opWidth, opHeight;
    int optempX, optempY;
    unsigned int dstOffset, srcOffset = 0;

    optempX = opX;
    optempY = opY;

    /* Make sure srcX and srcY are in source region */
    srcX = ((srcX % (int) exaScratch.srcWidth) + (int) exaScratch.srcWidth)
        % (int) exaScratch.srcWidth;
    srcY = ((srcY % (int) exaScratch.srcHeight) + (int) exaScratch.srcHeight)
        % (int) exaScratch.srcHeight;

    opWidth = exaScratch.srcWidth - srcX;
    opHeight = exaScratch.srcHeight - srcY;

    srcOffset = GetSrcOffset(srcX, srcY);

    if (width < opWidth)
        opWidth = width;
    if (height < opHeight)
        opHeight = height;

    while (1) {
        gp_wait_until_idle();
        dstOffset = GetPixmapOffset(pxDst, optempX, optempY);
        opPtr = &lx_alpha_ops[exaScratch.op * 2];
        apply = (exaScratch.dstFormat->alphabits != 0 &&
                 exaScratch.srcFormat->alphabits != 0) ?
            CIMGP_APPLY_BLEND_TO_ALL : CIMGP_APPLY_BLEND_TO_RGB;
        gp_declare_blt(0);
        gp_set_bpp(lx_get_bpp_from_format(exaScratch.dstFormat->fmt));
        gp_set_strides(exaGetPixmapPitch(pxDst), exaScratch.srcPitch);
        lx_set_source_format(exaScratch.srcFormat->fmt,
                             exaScratch.dstFormat->fmt);
        type = get_op_type(exaScratch.srcFormat, exaScratch.dstFormat,
                           opPtr->type);
        gp_set_alpha_operation(opPtr->operation, type, opPtr->channel,
                               apply, 0);
        gp_screen_to_screen_convert(dstOffset, srcOffset, opWidth, opHeight, 0);

        optempX += opWidth;
        if (optempX >= opX + width) {
            optempX = opX;
            optempY += opHeight;
            if (optempY >= opY + height)
                break;
        }
        if (optempX == opX) {
            srcOffset = GetSrcOffset(srcX, 0);
            opWidth = ((opX + width) - optempX) > (exaScratch.srcWidth - srcX)
                ? (exaScratch.srcWidth - srcX) : ((opX + width) - optempX);
            opHeight = ((opY + height) - optempY) > exaScratch.srcHeight
                ? exaScratch.srcHeight : ((opY + height) - optempY);
        }
        else if (optempY == opY) {
            srcOffset = GetSrcOffset(0, srcY);
            opWidth = ((opX + width) - optempX) > exaScratch.srcWidth
                ? exaScratch.srcWidth : ((opX + width) - optempX);
            opHeight = ((opY + height) - optempY) > (exaScratch.srcHeight -
                                                     srcY) ? (exaScratch.
                                                              srcHeight -
                                                              srcY) : ((opY +
                                                                        height)
                                                                       -
                                                                       optempY);
        }
        else {
            srcOffset = GetSrcOffset(0, 0);
            opWidth = ((opX + width) - optempX) > exaScratch.srcWidth
                ? exaScratch.srcWidth : ((opX + width) - optempX);
            opHeight = ((opY + height) - optempY) > exaScratch.srcHeight
                ? exaScratch.srcHeight : ((opY + height) - optempY);
        }
    }
}

/* This function handles the multipass blend functions */

static void
lx_composite_multipass(PixmapPtr pxDst, unsigned long dstOffset,
                       unsigned long srcOffset, int width, int height)
{
    struct blend_ops_t *opPtr;
    int sbpp = lx_get_bpp_from_format(exaScratch.srcFormat->fmt);
    int apply, type;

    /* Wait until the GP is idle - this will ensure that the scratch buffer
     * isn't occupied */

    gp_wait_until_idle();

    /* Copy the destination to the scratch buffer, and convert it to the
     * source format */

    gp_declare_blt(0);

    gp_set_bpp(sbpp);
    gp_set_source_format(exaScratch.dstFormat->fmt);
    gp_set_raster_operation(0xCC);
    gp_set_strides(exaScratch.srcPitch, exaGetPixmapPitch(pxDst));
    gp_screen_to_screen_convert(exaScratch.bufferOffset, dstOffset,
                                width, height, 0);

    /* Do the first blend from the source to the scratch buffer */

    gp_declare_blt(CIMGP_BLTFLAGS_HAZARD);
    gp_set_bpp(sbpp);
    gp_set_source_format(exaScratch.srcFormat->fmt);
    gp_set_strides(exaScratch.srcPitch, exaScratch.srcPitch);

    opPtr = &lx_alpha_ops[exaScratch.op * 2];

    apply = (exaScratch.srcFormat->alphabits == 0) ?
        CIMGP_APPLY_BLEND_TO_RGB : CIMGP_APPLY_BLEND_TO_ALL;

    /* If we're destroying the source alpha bits, then make sure we
     * use the alpha before the color conversion
     */

    gp_screen_to_screen_blt(exaScratch.bufferOffset, srcOffset, width, height,
                            0);

    /* Finally, do the second blend back to the destination */

    opPtr = &lx_alpha_ops[(exaScratch.op * 2) + 1];

    apply = (exaScratch.dstFormat->alphabits == 0) ?
        CIMGP_APPLY_BLEND_TO_RGB : CIMGP_APPLY_BLEND_TO_ALL;

    gp_declare_blt(CIMGP_BLTFLAGS_HAZARD);
    gp_set_bpp(lx_get_bpp_from_format(exaScratch.dstFormat->fmt));

    lx_set_source_format(exaScratch.srcFormat->fmt, exaScratch.dstFormat->fmt);

    type = get_op_type(exaScratch.srcFormat, exaScratch.dstFormat, opPtr->type);

    gp_set_alpha_operation(opPtr->operation, type, opPtr->channel, apply, 0);

    gp_screen_to_screen_convert(dstOffset, exaScratch.bufferOffset,
                                width, height, 0);
}

static void
lx_composite_rotate(PixmapPtr pxDst, unsigned long dstOffset,
                    unsigned int srcOffset, int width, int height)
{
    int degrees = 0;

    gp_declare_blt(0);
    gp_set_bpp(lx_get_bpp_from_format(exaScratch.dstFormat->fmt));
    gp_set_strides(exaGetPixmapPitch(pxDst), exaScratch.srcPitch);

    lx_set_source_format(exaScratch.srcFormat->fmt, exaScratch.dstFormat->fmt);

    gp_set_raster_operation(0xCC);

    /* RandR rotation is counter-clockwise, our rotation
     * is clockwise, so adjust the numbers accordingly */

    switch (exaScratch.rotate) {
    case RR_Rotate_90:
        degrees = 270;
        break;
    case RR_Rotate_180:
        degrees = 180;
        break;
    case RR_Rotate_270:
        degrees = 90;
        break;
    }

    gp_rotate_blt(dstOffset, srcOffset, width, height, degrees);
}

static void
lx_do_composite_mask(PixmapPtr pxDst, unsigned long dstOffset,
                     unsigned int maskOffset, int width, int height)
{
    struct blend_ops_t *opPtr = &lx_alpha_ops[exaScratch.op * 2];

    gp_declare_blt(0);

    gp_set_source_format(exaScratch.srcFormat->fmt);
    gp_set_strides(exaGetPixmapPitch(pxDst), exaScratch.srcPitch);
    gp_set_bpp(lx_get_bpp_from_format(exaScratch.dstFormat->fmt));
    gp_set_solid_source(exaScratch.srcColor);

    gp_blend_mask_blt(dstOffset, 0, width, height, maskOffset,
                      exaScratch.srcPitch, opPtr->operation,
                      exaScratch.fourBpp);
}

static void
lx_do_composite_mask_two_pass(PixmapPtr pxDst, unsigned long dstOffset,
                              unsigned int maskOffset, int width, int height,
                              int opX, int opY, xPointFixed srcPoint)
{
    int apply, type;
    struct blend_ops_t *opPtr;
    int opWidth, opHeight;
    int opoverX, opoverY;

    opoverX = opX;
    opoverY = opY;

    /* The rendering region should not be bigger than off-screen memory size
     * which equals to DEFAULT_EXA_SCRATCH_BFRSZ. If that happens, we split
     * the PictOpOver rendering region into several 256KB chunks. And because
     * of the Pitch(stride) parameter, so we use maximun width of mask picture.
     * that is to say it is a scanline rendering process */
    if (width * height * 4 > DEFAULT_EXA_SCRATCH_BFRSZ) {
        opWidth = width;
        opHeight = DEFAULT_EXA_SCRATCH_BFRSZ / (width * 4);
    }
    else {
        opWidth = width;
        opHeight = height;
    }

    while (1) {

        /* Wait until the GP is idle - this will ensure that the scratch buffer
         * isn't occupied */

        gp_wait_until_idle();

        /* Copy the source to the scratch buffer, and do a src * mask raster
         * operation */

        gp_declare_blt(0);
        opPtr = &lx_alpha_ops[(exaScratch.op * 2) + 1];
        gp_set_source_format(CIMGP_SOURCE_FMT_8_8_8_8);
        gp_set_strides(opWidth * 4, exaScratch.srcPitch);
        gp_set_bpp(lx_get_bpp_from_format(CIMGP_SOURCE_FMT_8_8_8_8));
        gp_set_solid_source(exaScratch.srcColor);
        gp_blend_mask_blt(exaScratch.bufferOffset, 0, opWidth, opHeight,
                          maskOffset, exaScratch.srcPitch, opPtr->operation,
                          exaScratch.fourBpp);

        /* Do a relative operation(refer rendercheck ops.c), and copy the
         * operation result to destination */

        gp_declare_blt(CIMGP_BLTFLAGS_HAZARD);
        opPtr = &lx_alpha_ops[exaScratch.op * 2];
        apply = (exaScratch.dstFormat->alphabits == 0) ?
            CIMGP_APPLY_BLEND_TO_RGB : CIMGP_APPLY_BLEND_TO_ALL;
        gp_set_source_format(CIMGP_SOURCE_FMT_8_8_8_8);
        gp_set_strides(exaGetPixmapPitch(pxDst), opWidth * 4);
        gp_set_bpp(lx_get_bpp_from_format(exaScratch.dstFormat->fmt));
        type = CIMGP_CONVERTED_ALPHA;
        gp_set_alpha_operation(opPtr->operation, type, opPtr->channel,
                               apply, 0);
        gp_screen_to_screen_convert(dstOffset, exaScratch.bufferOffset,
                                    opWidth, opHeight, 0);

        if (width * height * 4 > DEFAULT_EXA_SCRATCH_BFRSZ) {
            /* Finish the rendering */
            if (opoverY + opHeight == opY + height)
                break;
            /* Recalculate the Dest and Mask rendering start point */
            srcPoint.y = srcPoint.y + F(opHeight);
            opoverY = opoverY + opHeight;
            if (opoverY + opHeight > opY + height)
                opHeight = opY + height - opoverY;
            dstOffset = GetPixmapOffset(pxDst, opoverX, opoverY);
            maskOffset = GetSrcOffset(I(srcPoint.x), I(srcPoint.y));
        }
        else
            break;
    }
}

static void
transformPoint(PictTransform * t, xPointFixed * point)
{
    PictVector v;

    v.vector[0] = point->x;
    v.vector[1] = point->y;
    v.vector[2] = xFixed1;

    if (t != NULL)
        PictureTransformPoint(t, &v);

    point->x = v.vector[0];
    point->y = v.vector[1];
}

static void
lx_do_composite(PixmapPtr pxDst, int srcX, int srcY, int maskX,
                int maskY, int dstX, int dstY, int width, int height)
{
    unsigned int dstOffset, srcOffset = 0;

    xPointFixed srcPoint;

    int opX = dstX;
    int opY = dstY;
    int opWidth = width;
    int opHeight = height;

    /* Transform the source coordinates */

    if (exaScratch.type == COMP_TYPE_MASK) {
        srcPoint.x = F(maskX);
        srcPoint.y = F(maskY);
    }
    else {
        srcPoint.x = F(srcX);
        srcPoint.y = F(srcY);
    }

    /* srcX, srcY point to the upper right side of the bounding box
     * in the unrotated coordinate space.  Depending on the orientation,
     * we have to translate the coordinates to point to the origin of
     * the rectangle in the source pixmap */

    switch (exaScratch.rotate) {
    case RR_Rotate_270:
        srcPoint.x += F(width);

        opWidth = height;
        opHeight = width;
        break;

    case RR_Rotate_180:
        srcPoint.x += F(width);
        srcPoint.y += F(height);

        srcX += width;
        srcY += height;
        break;

    case RR_Rotate_90:
        srcPoint.y += F(height);

        opWidth = height;
        opHeight = width;
        break;
    }

    transformPoint(exaScratch.transform, &srcPoint);

    /* Adjust the point to fit into the pixmap */

    if (I(srcPoint.x) < 0) {
        opWidth += I(srcPoint.x);
        srcPoint.x = F(0);
    }

    if (I(srcPoint.y) < 0) {
        opHeight += I(srcPoint.y);
        srcPoint.y = F(0);
    }

    /* Get the source point offset position */

    srcOffset = GetSrcOffset(I(srcPoint.x), I(srcPoint.y));

    /* When mask exists, exaScratch.srcWidth and exaScratch.srcHeight are
     * the source width and source height; Otherwise, they are mask width
     * and mask height */
    /* exaScratch.repeat is the source repeat attribute
     * exaScratch.maskrepeat is the mask repeat attribute */
    /* If type is COMP_TYPE_MASK, maskX and maskY are not zero, we should
     * subtract them to do the operation in the correct region */

    /* FIXME:  Please add the code to handle the condition when the maskX
     * and maskY coordinate are negative or greater than
     * exaScratch.srcWidth and exaScratch.srcHeight */

    if (exaScratch.type == COMP_TYPE_MASK) {
        if ((exaScratch.srcWidth - maskX) < opWidth)
            opWidth = exaScratch.srcWidth - maskX;
        if ((exaScratch.srcHeight - maskY) < opHeight)
            opHeight = exaScratch.srcHeight - maskY;
    }
    else {
        if (exaScratch.type == COMP_TYPE_ONEPASS) {
            /* This is the condition srcX or/and srcY is/are out of source
             * region */
            if (((srcX >= 0 && srcY >= exaScratch.srcHeight)
                 || (srcX >= exaScratch.srcWidth && srcY >= 0)) &&
                (exaScratch.op == PictOpOver || exaScratch.op == PictOpSrc)) {
                if (exaScratch.repeat == 1) {
                    opWidth = width;
                    opHeight = height;
                }
                else {
                    if (exaScratch.op == PictOpOver)
                        return;
                    else {
                        exaScratch.op = PictOpClear;
                        opWidth = width;
                        opHeight = height;
                    }
                }
                /* This is the condition srcX or/and srcY is/are in the source
                 * region */
            }
            else if (srcX >= 0 && srcY >= 0 &&
                     (exaScratch.op == PictOpOver ||
                      exaScratch.op == PictOpSrc)) {
                if (exaScratch.repeat == 1) {
                    opWidth = width;
                    opHeight = height;
                }
                else {
                    if ((exaScratch.srcWidth - srcX) < opWidth)
                        opWidth = exaScratch.srcWidth - srcX;
                    if ((exaScratch.srcHeight - srcY) < opHeight)
                        opHeight = exaScratch.srcHeight - srcY;
                }
                /* This is the condition srcX or/and srcY is/are negative */
            }
            else if ((srcX < 0 || srcY < 0) &&
                     (exaScratch.op == PictOpOver ||
                      exaScratch.op == PictOpSrc)) {
                if (exaScratch.repeat == 1) {
                    opWidth = width;
                    opHeight = height;
                }
                else {
                    /* Have not met this condition till now */
                    return;
                }
            }
            else {
                if (exaScratch.srcWidth < opWidth)
                    opWidth = exaScratch.srcWidth;
                if (exaScratch.srcHeight < opHeight)
                    opHeight = exaScratch.srcHeight;
            }
        }
        else {
            if (exaScratch.rotate == RR_Rotate_180) {
            }
            else {
                if ((exaScratch.srcWidth - srcY) < opWidth)
                    opWidth = exaScratch.srcWidth - srcY;
                if ((exaScratch.srcHeight - srcX) < opHeight)
                    opHeight = exaScratch.srcHeight - srcX;
            }
        }
    }

    while (1) {

        dstOffset = GetPixmapOffset(pxDst, opX, opY);

        switch (exaScratch.type) {

        case COMP_TYPE_MASK:{
            if (exaScratch.op == PictOpOver || exaScratch.op ==
                PictOpOutReverse || exaScratch.op == PictOpInReverse ||
                exaScratch.op == PictOpIn || exaScratch.op == PictOpOut ||
                exaScratch.op == PictOpOverReverse)
                lx_do_composite_mask_two_pass(pxDst, dstOffset,
                                              srcOffset, opWidth, opHeight, opX,
                                              opY, srcPoint);
            else
                lx_do_composite_mask(pxDst, dstOffset, srcOffset,
                                     opWidth, opHeight);
        }
            break;

        case COMP_TYPE_ONEPASS:
            if ((exaScratch.op == PictOpOver || exaScratch.op == PictOpSrc)
                && (exaScratch.repeat == 1)) {
                lx_composite_onepass_special(pxDst, opWidth, opHeight, opX, opY,
                                             srcX, srcY);
                return;
            }
            else if ((exaScratch.op == PictOpAdd) && (exaScratch.srcFormat->exa
                                                      == PICT_a8) &&
                     (exaScratch.dstFormat->exa == PICT_a8))
                lx_composite_onepass_add_a8(pxDst, dstOffset, srcOffset,
                                            opWidth, opHeight, opX, opY, srcX,
                                            srcY);
            else
                lx_composite_onepass(pxDst, dstOffset, srcOffset, opWidth,
                                     opHeight);
            break;

        case COMP_TYPE_TWOPASS:
            lx_composite_multipass(pxDst, dstOffset, srcOffset, opWidth,
                                   opHeight);

        case COMP_TYPE_ROTATE:
            lx_composite_rotate(pxDst, dstOffset, srcOffset, opWidth, opHeight);
            break;
        }

        opX += opWidth;

        if (opX >= dstX + width) {
            opX = dstX;
            opY += opHeight;

            if (opY >= dstY + height)
                break;
        }

        /* FIXME:  Please add the code to handle the condition when the maskX
         * and maskY coordinate are negative or greater than
         * exaScratch.srcWidth and exaScratch.srcHeight */

        if (exaScratch.type == COMP_TYPE_MASK) {
            opWidth = ((dstX + width) - opX) > (exaScratch.srcWidth - maskX)
                ? (exaScratch.srcWidth - maskX) : (dstX + width) - opX;
            opHeight = ((dstY + height) - opY) > (exaScratch.srcHeight - maskY)
                ? (exaScratch.srcHeight - maskY) : (dstY + height) - opY;
            /* All black out of the mask */
            if (!exaScratch.maskrepeat)
                exaScratch.srcColor = 0x0;
        }
        else {
            if (exaScratch.type == COMP_TYPE_ONEPASS) {
                if (srcX >= 0 && srcY >= 0 && (exaScratch.op == PictOpOver ||
                                               exaScratch.op == PictOpSrc ||
                                               exaScratch.op == PictOpClear)) {
                    opWidth =
                        ((dstX + width) - opX) >
                        (exaScratch.srcWidth - srcX) ? (exaScratch.srcWidth -
                                                        srcX) : (dstX + width)
                        - opX;
                    opHeight = ((dstY + height) - opY) >
                        (exaScratch.srcHeight - srcY) ?
                        (exaScratch.srcHeight - srcY) : (dstY + height) - opY;
                }
                else {
                    opWidth = ((dstX + width) - opX) > exaScratch.srcWidth ?
                        exaScratch.srcWidth : (dstX + width) - opX;
                    opHeight = ((dstY + height) - opY) > exaScratch.srcHeight ?
                        exaScratch.srcHeight : (dstY + height) - opY;
                }
            }
            else {
                opWidth = ((dstX + width) - opX) > (exaScratch.srcWidth - srcY)
                    ? (exaScratch.srcWidth - srcY) : (dstX + width) - opX;
                opHeight =
                    ((dstY + height) - opY) >
                    (exaScratch.srcHeight - srcX) ? (exaScratch.srcHeight -
                                                     srcX) : (dstY + height) -
                    opY;
            }
            /* All black out of the source */
            if (!exaScratch.repeat && (exaScratch.type == COMP_TYPE_ONEPASS)) {
                lx_composite_all_black(srcOffset, exaScratch.srcWidth,
                                       exaScratch.srcHeight);
            }
            if (!exaScratch.repeat && (exaScratch.type == COMP_TYPE_ROTATE))
                break;
        }
    }
}

static void
lx_wait_marker(ScreenPtr PScreen, int marker)
{
    gp_wait_until_idle();
}

static void
lx_done(PixmapPtr ptr)
{
}

#if 0
static void
lx_upload_to_screen(PixmapPtr pxDst, int x, int y, int w, int h,
                    char *src, int src_pitch)
{
    GeodeRec *pGeode = GEODEPTR_FROM_PIXMAP(pxDst);
    int dst_pitch = exaGetPixmapPitch(pxDst);
    int cpp = (pxDst->drawable.bitsPerPixel + 7) / 8;

    char *dst;
    int offset = exaGetPixmapOffset(pxDst);

    dst = (char *) (pGeode->FBBase + offset + (y * dst_pitch) + (x * cpp));
    int i;

    for (i = 0; i < h; i++) {
        memcpy(dst, src, w * cpp);
        dst += dst_pitch;
        src += src_pitch;
    }
}
#endif

#if EXA_VERSION_MAJOR > 2 || (EXA_VERSION_MAJOR == 2 && EXA_VERSION_MINOR >= 2)

static Bool
lx_exa_pixmap_is_offscreen(PixmapPtr pPixmap)
{
    ScrnInfoPtr pScrni = xf86Screens[pPixmap->drawable.pScreen->myNum];
    GeodeRec *pGeode = GEODEPTR(pScrni);
    void *start = (void *) (pGeode->FBBase);
    void *end =
        (void *) (pGeode->FBBase + pGeode->offscreenStart +
                  pGeode->offscreenSize);

    if ((void *) pPixmap->devPrivate.ptr >= start &&
        (void *) pPixmap->devPrivate.ptr < end)
        return TRUE;

    return FALSE;
}

#endif

Bool
LXExaInit(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrni = xf86Screens[pScreen->myNum];
    GeodeRec *pGeode = GEODEPTR(pScrni);
    ExaDriverPtr pExa = pGeode->pExa;

    pExa->exa_major = EXA_VERSION_MAJOR;
    pExa->exa_minor = EXA_VERSION_MINOR;

    pExa->WaitMarker = lx_wait_marker;

    pExa->PrepareSolid = lx_prepare_solid;
    pExa->Solid = lx_do_solid;
    pExa->DoneSolid = lx_done;

    pExa->PrepareCopy = lx_prepare_copy;
    pExa->Copy = lx_do_copy;
    pExa->DoneCopy = lx_done;

    /* Composite */
    pExa->CheckComposite = lx_check_composite;
    pExa->PrepareComposite = lx_prepare_composite;
    pExa->Composite = lx_do_composite;
    pExa->DoneComposite = lx_done;
    //pExa->UploadToScreen =  lx_upload_to_screen;

#if EXA_VERSION_MAJOR > 2 || (EXA_VERSION_MAJOR == 2 && EXA_VERSION_MINOR >= 2)
    pExa->PixmapIsOffscreen = lx_exa_pixmap_is_offscreen;
#endif

    //pExa->flags = EXA_OFFSCREEN_PIXMAPS;

    return exaDriverInit(pScreen, pGeode->pExa);
}