summaryrefslogtreecommitdiff
path: root/hw/kdrive/igs/igsdraw.c
blob: 677436be52026481f72d79c7dbe4005f5594d448 (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
/*
 * Copyright © 2000 Keith Packard
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif
#include "igs.h"
#include "igsdraw.h"

#include	"Xmd.h"
#include	"gcstruct.h"
#include	"scrnintstr.h"
#include	"pixmapstr.h"
#include	"regionstr.h"
#include	"mistruct.h"
#include	"fontstruct.h"
#include	"dixfontstr.h"
#include	"fb.h"
#include	"migc.h"
#include	"miline.h"

CARD8 igsPatRop[16] = {
    /* GXclear      */      0x00,         /* 0 */
    /* GXand        */      0xa0,         /* src AND dst */
    /* GXandReverse */      0x50,         /* src AND NOT dst */
    /* GXcopy       */      0xf0,         /* src */
    /* GXandInverted*/      0x0a,         /* NOT src AND dst */
    /* GXnoop       */      0xaa,         /* dst */
    /* GXxor        */      0x5a,         /* src XOR dst */
    /* GXor         */      0xfa,         /* src OR dst */
    /* GXnor        */      0x05,         /* NOT src AND NOT dst */
    /* GXequiv      */      0xa5,         /* NOT src XOR dst */
    /* GXinvert     */      0x55,         /* NOT dst */
    /* GXorReverse  */      0xf5,         /* src OR NOT dst */
    /* GXcopyInverted*/     0x0f,         /* NOT src */
    /* GXorInverted */      0xaf,         /* NOT src OR dst */
    /* GXnand       */      0x5f,         /* NOT src OR NOT dst */
    /* GXset        */      0xff,         /* 1 */
};

/*
 * Handle pixel transfers
 */

#define BURST
#ifdef BURST
#define PixTransDeclare	VOL32	*pix_trans_base = igsc->copData,\
				*pix_trans = pix_trans_base
#define PixTransStart(n)	if (pix_trans + (n) > pix_trans_base + 16384) pix_trans = pix_trans_base
#define PixTransStore(t)	*pix_trans++ = (t)
#else
#define PixTransDeclare	VOL32	*pix_trans = igsc->copData
#define PixTransStart(n)	
#define PixTransStore(t)	*pix_trans = (t)
#endif

static IgsPattern *
igsSetPattern (ScreenPtr    pScreen,
	       PixmapPtr    pPixmap,
	       CARD8	    fillStyle,
	       INT32	    xrot,
	       INT32	    yrot)
{
    KdScreenPriv(pScreen);
    igsCardInfo(pScreenPriv);
    igsScreenInfo(pScreenPriv);
    int		i;
    IgsPatternCache *c;
    IgsPattern	*p;

    if (fillStyle == FillTiled)
	c = &igss->tile;
    else
	c = &igss->stipple;
    for (i = 0; i < IGS_NUM_PATTERN; i++)
    {
	p = &c->pattern[i];
	if (p->serial_number == pPixmap->drawable.serialNumber &&
	    p->xrot == xrot &&
	    p->yrot == yrot)
	{
	    return p;
	}
    }
    p = &c->pattern[c->next];
    if (++c->next == IGS_NUM_PATTERN)
	c->next = 0;
    p->serial_number = pPixmap->drawable.serialNumber;
    p->xrot = xrot;
    p->yrot = yrot;

    if (fillStyle != FillTiled)
    {
	FbStip	    *pix;
	FbStride    pixStride;
	int	    pixBpp;
	int	    pixXoff, pixYoff;
	CARD8	    tmp[8];
	CARD32	    *pat;
	int	    stipX, stipY;
	int	    y;
	FbStip	    bits;

	fbGetStipDrawable (&pPixmap->drawable, pix, pixStride, pixBpp, pixXoff, pixYoff);
	
	modulus (-yrot - pixYoff, pPixmap->drawable.height, stipY);
	modulus (-xrot - pixXoff, FB_UNIT, stipX);

	pat = (CARD32 *) p->base;

	for (y = 0; y < 8; y++)
	{
	    bits = pix[stipY * pixStride];
	    FbRotLeft (bits, stipX);
	    tmp[y] = (CARD8) bits;
	    stipY++;
	    if (stipY == pPixmap->drawable.height)
		stipY = 0;
	}
	for (i = 0; i < 2; i++)
	{
	    bits = (tmp[i*4+0] | 
		      (tmp[i*4+1] << 8) | 
		      (tmp[i*4+2] << 16) |
		      (tmp[i*4+3] << 24));
	    IgsAdjustBits32 (bits);
	    *pat++ = bits;
	}
    }
    else
    {
	FbBits	    *pix;
	FbStride    pixStride;
	int	    pixBpp;
	FbBits	    *pat;
	FbStride    patStride;
	int	    patBpp;
	int	    patXoff, patYoff;
	
	fbGetDrawable (&pPixmap->drawable, pix, pixStride, pixBpp, patXoff, patYoff);

	pat = (FbBits *) p->base;
	patBpp = pixBpp;
	patStride = (patBpp * IGS_PATTERN_WIDTH) / (8 * sizeof (FbBits));

	fbTile (pat, patStride, 0,
		patBpp * IGS_PATTERN_WIDTH, IGS_PATTERN_HEIGHT,

		pix, pixStride, 
		pPixmap->drawable.width * pixBpp,
		pPixmap->drawable.height,
		GXcopy, FB_ALLONES, pixBpp,
		(xrot - patXoff) * pixBpp, yrot - patYoff);
    }
    return p;
}

void
igsFillBoxSolid (DrawablePtr pDrawable, int nBox, BoxPtr pBox, 
		 unsigned long pixel, int alu, unsigned long planemask)
{
    SetupIgs(pDrawable->pScreen);
    CARD32	cmd;

    _igsSetSolidRect(cop,alu,planemask,pixel,cmd);
    while (nBox--) 
    {
	_igsRect(cop,pBox->x1,pBox->y1,pBox->x2-pBox->x1,pBox->y2-pBox->y1,cmd);
	pBox++;
    }
    KdMarkSync (pDrawable->pScreen);
}

void
igsFillBoxTiled (DrawablePtr pDrawable, int nBox, BoxPtr pBox,
		 PixmapPtr pPixmap, int xrot, int yrot, int alu)
{
    SetupIgs(pDrawable->pScreen);
    CARD32      cmd;
    IgsPattern	*p = igsSetPattern (pDrawable->pScreen,
				    pPixmap,
				    FillTiled,
				    xrot, yrot);
    
    _igsSetTiledRect(cop,alu,planemask,p->offset,cmd);
    while (nBox--) 
    {
	_igsPatRect(cop,pBox->x1,pBox->y1,pBox->x2-pBox->x1,pBox->y2-pBox->y1,cmd);
	pBox++;
    }
    KdMarkSync (pDrawable->pScreen);
}

void
igsFillBoxStippled (DrawablePtr pDrawable, GCPtr pGC,
		    int nBox, BoxPtr pBox)
{
    SetupIgs(pDrawable->pScreen);
    CARD32      cmd;
    int		xrot = pGC->patOrg.x + pDrawable->x;
    int		yrot = pGC->patOrg.y + pDrawable->y;
    IgsPattern	*p = igsSetPattern (pDrawable->pScreen,
				    pGC->stipple,
				    pGC->fillStyle,
				    xrot, yrot);
    if (pGC->fillStyle == FillStippled)
    {
	_igsSetStippledRect (cop,pGC->alu,planemask,pGC->fgPixel,p->offset,cmd);
    }
    else
    {
	_igsSetOpaqueStippledRect (cop,pGC->alu,planemask,
				   pGC->fgPixel,pGC->bgPixel,p->offset,cmd);
    }
    while (nBox--) 
    {
	_igsPatRect(cop,pBox->x1,pBox->y1,pBox->x2-pBox->x1,pBox->y2-pBox->y1,cmd);
	pBox++;
    }
    KdMarkSync (pDrawable->pScreen);
}


void
igsStipple (ScreenPtr	pScreen,
	    CARD32	cmd,
	    FbStip	*psrcBase,
	    FbStride	widthSrc,
	    int		srcx,
	    int		srcy,
	    int		dstx,
	    int		dsty,
	    int		width,
	    int		height)
{
    SetupIgs(pScreen);
    FbStip	*psrcLine, *psrc;
    FbStride	widthRest;
    FbStip	bits, tmp, lastTmp;
    int		leftShift, rightShift;
    int		nl, nlMiddle;
    int		r;
    PixTransDeclare;
    
    /* Compute blt address and parameters */
    psrc = psrcBase + srcy * widthSrc + (srcx >> 5);
    nlMiddle = (width + 31) >> 5;
    leftShift = srcx & 0x1f;
    rightShift = 32 - leftShift;
    widthRest = widthSrc - nlMiddle;
    
    _igsPlaneBlt(cop,dstx,dsty,width,height,cmd);
    
    if (leftShift == 0)
    {
	while (height--)
	{
	    nl = nlMiddle;
	    PixTransStart(nl);
	    while (nl--)
	    {
		tmp = *psrc++;
		IgsAdjustBits32 (tmp);
		PixTransStore (tmp);
	    }
	    psrc += widthRest;
	}
    }
    else
    {
	widthRest--;
	while (height--)
	{
	    bits = *psrc++;
	    nl = nlMiddle;
	    PixTransStart(nl);
	    while (nl--)
	    {
		tmp = FbStipLeft(bits, leftShift);
		bits = *psrc++;
		tmp |= FbStipRight(bits, rightShift);
		IgsAdjustBits32(tmp);
		PixTransStore (tmp);
	    }
	    psrc += widthRest;
	}
    }
}
	    
void
igsCopyNtoN (DrawablePtr	pSrcDrawable,
	    DrawablePtr	pDstDrawable,
	    GCPtr	pGC,
	    BoxPtr	pbox,
	    int		nbox,
	    int		dx,
	    int		dy,
	    Bool	reverse,
	    Bool	upsidedown,
	    Pixel	bitplane,
	    void	*closure)
{
    SetupIgs(pDstDrawable->pScreen);
    int	    srcX, srcY, dstX, dstY;
    int	    w, h;
    CARD32  flags;
    CARD32  cmd;
    CARD8   alu;
    
    if (pGC)
    {
	alu = pGC->alu;
	if (sourceInvarient (pGC->alu))
	{
	    igsFillBoxSolid (pDstDrawable, nbox, pbox, 0, pGC->alu, pGC->planemask);
	    return;
	}
    }
    else
	alu = GXcopy;
    
    _igsSetBlt(cop,alu,pGC->planemask,reverse,upsidedown,cmd);
    while (nbox--)
    {
	w = pbox->x2 - pbox->x1;
	h = pbox->y2 - pbox->y1;
	if (reverse)
	    dstX = pbox->x2 - 1;
	else
	    dstX = pbox->x1;
	srcX = dstX + dx;
	
	if (upsidedown)
	    dstY = pbox->y2 - 1;
	else
	    dstY = pbox->y1;
	
	srcY = dstY + dy;
	
	_igsBlt (cop, srcX, srcY, dstX, dstY, w, h, cmd);
	pbox++;
    }
    KdMarkSync (pDstDrawable->pScreen);
}

RegionPtr
igsCopyArea(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable, GCPtr pGC,
	   int srcx, int srcy, int width, int height, int dstx, int dsty)
{
    KdScreenPriv(pDstDrawable->pScreen);
    FbBits	    depthMask;
    
    depthMask = FbFullMask (pDstDrawable->depth);
    if ((pGC->planemask & depthMask) == depthMask &&
	pSrcDrawable->type == DRAWABLE_WINDOW &&
	pDstDrawable->type == DRAWABLE_WINDOW)
    {
	return fbDoCopy (pSrcDrawable, pDstDrawable, pGC, 
			 srcx, srcy, width, height, 
			 dstx, dsty, igsCopyNtoN, 0, 0);
    }
    return KdCheckCopyArea (pSrcDrawable, pDstDrawable, pGC, 
		       srcx, srcy, width, height, dstx, dsty);
}

typedef struct _igs1toNargs {
    unsigned long	copyPlaneFG, copyPlaneBG;
    Bool		opaque;
} igs1toNargs;

void
igsCopy1toN (DrawablePtr    pSrcDrawable,
	     DrawablePtr    pDstDrawable,
	     GCPtr	    pGC,
	     BoxPtr	    pbox,
	     int	    nbox,
	     int	    dx,
	     int	    dy,
	     Bool	    reverse,
	     Bool	    upsidedown,
	     Pixel	    bitplane,
	     void	    *closure)
{
    SetupIgs(pDstDrawable->pScreen);
    
    igs1toNargs		*args = closure;
    int			dstx, dsty;
    FbStip		*psrcBase;
    FbStride		widthSrc;
    int			srcBpp;
    int			srcXoff, srcYoff;
    CARD32		cmd;

    if (args->opaque && sourceInvarient (pGC->alu))
    {
	igsFillBoxSolid (pDstDrawable, nbox, pbox,
			 pGC->bgPixel, pGC->alu, pGC->planemask);
	return;
    }
    
    fbGetStipDrawable (pSrcDrawable, psrcBase, widthSrc, srcBpp, srcXoff, srcYoff);
    
    if (args->opaque)
    {
	_igsSetOpaquePlaneBlt (cop, pGC->alu, pGC->planemask, args->copyPlaneFG,
			       args->copyPlaneBG, cmd);
    }
    else
    {
	_igsSetTransparentPlaneBlt (cop, pGC->alu, pGC->planemask,
				    args->copyPlaneFG, cmd);
    }
    
    while (nbox--)
    {
	dstx = pbox->x1;
	dsty = pbox->y1;
	
	igsStipple (pDstDrawable->pScreen, cmd,
		    psrcBase, widthSrc, 
		    dstx + dx - srcXoff, dsty + dy - srcYoff,
		    dstx, dsty, 
		    pbox->x2 - dstx, pbox->y2 - dsty);
	pbox++;
    }
    KdMarkSync (pDstDrawable->pScreen);
}

RegionPtr
igsCopyPlane (DrawablePtr   pSrcDrawable,
	      DrawablePtr   pDstDrawable,
	      GCPtr	    pGC,
	      int	    srcx,
	      int	    srcy,
	      int	    width,
	      int	    height, 
	      int	    dstx,
	      int	    dsty,
	      unsigned long bitPlane)
{
    RegionPtr	    ret;
    igs1toNargs	    args;
    FbBits	    depthMask;

    depthMask = FbFullMask (pDstDrawable->depth);
    if ((pGC->planemask & depthMask) == depthMask &&
	pDstDrawable->type == DRAWABLE_WINDOW &&
	pSrcDrawable->depth == 1)
    {
	args.copyPlaneFG = pGC->fgPixel;
	args.copyPlaneBG = pGC->bgPixel;
	args.opaque = TRUE;
	return fbDoCopy (pSrcDrawable, pDstDrawable, pGC, 
			 srcx, srcy, width, height, 
			 dstx, dsty, igsCopy1toN, bitPlane, &args);
    }
    return KdCheckCopyPlane(pSrcDrawable, pDstDrawable, pGC, 
			    srcx, srcy, width, height, 
			    dstx, dsty, bitPlane);
}

#if 0
/* would you believe this is slower than fb? */
void
igsPushPixels (GCPtr	    pGC,
	       PixmapPtr    pBitmap,
	       DrawablePtr  pDrawable,
	       int	    w,
	       int	    h,
	       int	    x,
	       int	    y)
{
    igs1toNargs		args;
    FbBits	    depthMask;

    depthMask = FbFullMask (pDstDrawable->depth);
    if ((pGC->planemask & depthMask) == depthMask &&
	pDrawable->type == DRAWABLE_WINDOW && 
	pGC->fillStyle == FillSolid)
    {
	args.opaque = FALSE;
	args.copyPlaneFG = pGC->fgPixel;
	(void) fbDoCopy ((DrawablePtr) pBitmap, pDrawable, pGC,
			  0, 0, w, h, x, y, igsCopy1toN, 1, &args);
    }
    else
    {
	KdCheckPushPixels (pGC, pBitmap, pDrawable, w, h, x, y);
    }
}
#else
#define igsPushPixels KdCheckPushPixels
#endif

BOOL
igsFillOk (GCPtr pGC)
{
    FbBits  depthMask;

    depthMask = FbFullMask(pGC->depth);
    if ((pGC->planemask & depthMask) != depthMask)
	return FALSE;
    switch (pGC->fillStyle) {
    case FillSolid:
	return TRUE;
    case FillTiled:
	return (igsPatternDimOk (pGC->tile.pixmap->drawable.width) &&
		igsPatternDimOk (pGC->tile.pixmap->drawable.height));
    case FillStippled:
    case FillOpaqueStippled:
	return (igsPatternDimOk (pGC->stipple->drawable.width) &&
		igsPatternDimOk (pGC->stipple->drawable.height));
    }
    return FALSE;
}

void
igsFillSpans (DrawablePtr pDrawable, GCPtr pGC, int n, 
	     DDXPointPtr ppt, int *pwidth, int fSorted)
{
    SetupIgs(pDrawable->pScreen);
    DDXPointPtr	    pptFree;
    FbGCPrivPtr	    fbPriv = fbGetGCPrivate(pGC);
    int		    *pwidthFree;/* copies of the pointers to free */
    CARD32	    cmd;
    int		    nTmp;
    INT16	    x, y;
    int		    width;
    IgsPattern	    *p;
    
    if (!igsFillOk (pGC))
    {
	KdCheckFillSpans (pDrawable, pGC, n, ppt, pwidth, fSorted);
	return;
    }
    nTmp = n * miFindMaxBand(fbGetCompositeClip(pGC));
    pwidthFree = (int *)xalloc(nTmp * sizeof(int));
    pptFree = (DDXPointRec *)xalloc(nTmp * sizeof(DDXPointRec));
    if(!pptFree || !pwidthFree)
    {
	if (pptFree) xfree(pptFree);
	if (pwidthFree) xfree(pwidthFree);
	return;
    }
    n = miClipSpans(fbGetCompositeClip(pGC),
		     ppt, pwidth, n,
		     pptFree, pwidthFree, fSorted);
    pwidth = pwidthFree;
    ppt = pptFree;
    switch (pGC->fillStyle) {
    case FillSolid:
	_igsSetSolidRect(cop,pGC->alu,pGC->planemask,pGC->fgPixel,cmd);
	break;
    case FillTiled:
	p = igsSetPattern (pDrawable->pScreen,
			   pGC->tile.pixmap,
			   FillTiled,
			   pGC->patOrg.x + pDrawable->x,
			   pGC->patOrg.y + pDrawable->y);
	_igsSetTiledRect (cop,pGC->alu,pGC->planemask,p->offset,cmd);
	break;
    default:
	p = igsSetPattern (pDrawable->pScreen,
			   pGC->stipple,
			   pGC->fillStyle,
			   pGC->patOrg.x + pDrawable->x,
			   pGC->patOrg.y + pDrawable->y);
	if (pGC->fillStyle == FillStippled)
	{
	    _igsSetStippledRect (cop,pGC->alu,pGC->planemask,
				 pGC->fgPixel,p->offset,cmd);
	}
	else
	{
	    _igsSetOpaqueStippledRect (cop,pGC->alu,pGC->planemask,
				       pGC->fgPixel,pGC->bgPixel,p->offset,cmd);
	}
	break;
    }
    while (n--)
    {
	x = ppt->x;
	y = ppt->y;
	ppt++;
	width = *pwidth++;
	if (width)
	{
	    _igsPatRect(cop,x,y,width,1,cmd);
	}
    }
    xfree(pptFree);
    xfree(pwidthFree);
    KdMarkSync (pDrawable->pScreen);
}

#define NUM_STACK_RECTS	1024

void
igsPolyFillRect (DrawablePtr pDrawable, GCPtr pGC, 
		int nrectFill, xRectangle *prectInit)
{
    SetupIgs(pDrawable->pScreen);
    xRectangle	    *prect;
    RegionPtr	    prgnClip;
    register BoxPtr pbox;
    register BoxPtr pboxClipped;
    BoxPtr	    pboxClippedBase;
    BoxPtr	    pextent;
    BoxRec	    stackRects[NUM_STACK_RECTS];
    FbGCPrivPtr	    fbPriv = fbGetGCPrivate (pGC);
    int		    numRects;
    int		    n;
    int		    xorg, yorg;
    int		    x, y;
    
    if (!igsFillOk (pGC))
    {
	KdCheckPolyFillRect (pDrawable, pGC, nrectFill, prectInit);
	return;
    }
    prgnClip = fbGetCompositeClip (pGC);
    xorg = pDrawable->x;
    yorg = pDrawable->y;
    
    if (xorg || yorg)
    {
	prect = prectInit;
	n = nrectFill;
	while(n--)
	{
	    prect->x += xorg;
	    prect->y += yorg;
	    prect++;
	}
    }
    
    prect = prectInit;

    numRects = REGION_NUM_RECTS(prgnClip) * nrectFill;
    if (numRects > NUM_STACK_RECTS)
    {
	pboxClippedBase = (BoxPtr)xalloc(numRects * sizeof(BoxRec));
	if (!pboxClippedBase)
	    return;
    }
    else
	pboxClippedBase = stackRects;

    pboxClipped = pboxClippedBase;
	
    if (REGION_NUM_RECTS(prgnClip) == 1)
    {
	int x1, y1, x2, y2, bx2, by2;

	pextent = REGION_RECTS(prgnClip);
	x1 = pextent->x1;
	y1 = pextent->y1;
	x2 = pextent->x2;
	y2 = pextent->y2;
    	while (nrectFill--)
    	{
	    if ((pboxClipped->x1 = prect->x) < x1)
		pboxClipped->x1 = x1;
    
	    if ((pboxClipped->y1 = prect->y) < y1)
		pboxClipped->y1 = y1;
    
	    bx2 = (int) prect->x + (int) prect->width;
	    if (bx2 > x2)
		bx2 = x2;
	    pboxClipped->x2 = bx2;
    
	    by2 = (int) prect->y + (int) prect->height;
	    if (by2 > y2)
		by2 = y2;
	    pboxClipped->y2 = by2;

	    prect++;
	    if ((pboxClipped->x1 < pboxClipped->x2) &&
		(pboxClipped->y1 < pboxClipped->y2))
	    {
		pboxClipped++;
	    }
    	}
    }
    else
    {
	int x1, y1, x2, y2, bx2, by2;

	pextent = REGION_EXTENTS(pGC->pScreen, prgnClip);
	x1 = pextent->x1;
	y1 = pextent->y1;
	x2 = pextent->x2;
	y2 = pextent->y2;
    	while (nrectFill--)
    	{
	    BoxRec box;
    
	    if ((box.x1 = prect->x) < x1)
		box.x1 = x1;
    
	    if ((box.y1 = prect->y) < y1)
		box.y1 = y1;
    
	    bx2 = (int) prect->x + (int) prect->width;
	    if (bx2 > x2)
		bx2 = x2;
	    box.x2 = bx2;
    
	    by2 = (int) prect->y + (int) prect->height;
	    if (by2 > y2)
		by2 = y2;
	    box.y2 = by2;
    
	    prect++;
    
	    if ((box.x1 >= box.x2) || (box.y1 >= box.y2))
	    	continue;
    
	    n = REGION_NUM_RECTS (prgnClip);
	    pbox = REGION_RECTS(prgnClip);
    
	    /* clip the rectangle to each box in the clip region
	       this is logically equivalent to calling Intersect()
	    */
	    while(n--)
	    {
		pboxClipped->x1 = max(box.x1, pbox->x1);
		pboxClipped->y1 = max(box.y1, pbox->y1);
		pboxClipped->x2 = min(box.x2, pbox->x2);
		pboxClipped->y2 = min(box.y2, pbox->y2);
		pbox++;

		/* see if clipping left anything */
		if(pboxClipped->x1 < pboxClipped->x2 && 
		   pboxClipped->y1 < pboxClipped->y2)
		{
		    pboxClipped++;
		}
	    }
    	}
    }
    if (pboxClipped != pboxClippedBase)
    {
	switch (pGC->fillStyle) {
	case FillSolid:
	    igsFillBoxSolid(pDrawable,
			   pboxClipped-pboxClippedBase, pboxClippedBase,
			   pGC->fgPixel, pGC->alu, pGC->planemask);
	    break;
	case FillTiled:
	    igsFillBoxTiled(pDrawable,
			    pboxClipped-pboxClippedBase, pboxClippedBase,
			    pGC->tile.pixmap,
			    pGC->patOrg.x + pDrawable->x,
			    pGC->patOrg.y + pDrawable->y,
			    pGC->alu);
	    break;
	case FillStippled:
	case FillOpaqueStippled:
	    igsFillBoxStippled (pDrawable, pGC,
				pboxClipped-pboxClippedBase, pboxClippedBase);
	    break;
	}
    }
    if (pboxClippedBase != stackRects)
    	xfree(pboxClippedBase);
}

int
igsTextInRegion (GCPtr		pGC,
		 int		x,
		 int		y,
		 unsigned int	nglyph,
		 CharInfoPtr	*ppci)
{
    int		    w;
    FontPtr	    pfont = pGC->font;
    BoxRec	    bbox;
    
    if (FONTCONSTMETRICS(pfont))
	w = FONTMAXBOUNDS(pfont,characterWidth) * nglyph;
    else
    {
	w = 0;
	while (nglyph--)
	    w += (*ppci++)->metrics.characterWidth;
    }
    if (w < 0)
    {
	bbox.x1 = x + w;
	bbox.x2 = x;
    }
    else
    {
	bbox.x1 = x;
	bbox.x2 = x + w;
    }
    w = FONTMINBOUNDS(pfont,leftSideBearing);
    if (w < 0)
	bbox.x1 += w;
    w = FONTMAXBOUNDS(pfont, rightSideBearing) - FONTMINBOUNDS(pfont, characterWidth);
    if (w > 0)
	bbox.x2 += w;
    bbox.y1 = y - FONTMAXBOUNDS(pfont,ascent);
    bbox.y2 = y + FONTMAXBOUNDS(pfont,descent);
    
    return RECT_IN_REGION(pGC->pScreen, fbGetCompositeClip(pGC), &bbox);
}
		 
void
igsGlyphBltClipped (DrawablePtr	    pDrawable,
		    GCPtr	    pGC,
		    int		    x,
		    int		    y,
		    unsigned int    nglyph,
		    CharInfoPtr	    *ppciInit,
		    Bool	    image)
{
    SetupIgs(pDrawable->pScreen);
    CARD32	    cmd;
    int		    h;
    int		    w;
    int		    xBack, yBack;
    int		    hBack, wBack;
    int		    lw;
    FontPtr	    pfont = pGC->font;
    CharInfoPtr	    pci;
    unsigned long   *bits;
    BoxPtr	    extents;
    BoxRec	    bbox;
    CARD32	    b;
    CharInfoPtr	    *ppci;
    FbGCPrivPtr	    fbPriv = fbGetGCPrivate(pGC);
    RegionPtr	    pClip = fbGetCompositeClip(pGC);
    BoxPtr	    pBox;
    int		    nbox;
    int		    x1, y1, x2, y2;
    unsigned char   alu;
    Bool	    set;
    PixTransDeclare;

    if (image)
    {
	xBack = x;
	yBack = y - FONTASCENT(pGC->font);
	wBack = 0;
	hBack = FONTASCENT(pGC->font) + FONTDESCENT(pGC->font);
	if (hBack)
	{
	    h = nglyph;
	    ppci = ppciInit;
	    while (h--)
		wBack += (*ppci++)->metrics.characterWidth;
	}
	if (wBack < 0)
	{
	    xBack = xBack + wBack;
	    wBack = -wBack;
	}
	if (hBack < 0)
	{
	    yBack = yBack + hBack;
	    hBack = -hBack;
	}
	alu = GXcopy;
	if (wBack)
	{
	    _igsSetSolidRect (cop, GXcopy, pGC->planemask, pGC->bgPixel, cmd);
	    for (nbox = REGION_NUM_RECTS (pClip),
		 pBox = REGION_RECTS (pClip);
		 nbox--;
		 pBox++)
	    {
		x1 = xBack;
		x2 = xBack + wBack;
		y1 = yBack;
		y2 = yBack + hBack;
		if (x1 < pBox->x1) x1 = pBox->x1;
		if (x2 > pBox->x2) x2 = pBox->x2;
		if (y1 < pBox->y1) y1 = pBox->y1;
		if (y2 > pBox->y2) y2 = pBox->y2;
		if (x1 < x2 && y1 < y2)
		{
		    _igsRect (cop, x1, y1, x2 - x1, y2 - y1, cmd);
		}
	    }
	    KdMarkSync (pDrawable->pScreen);
	}
    }
    else
    {
	wBack = 0;
	alu = pGC->alu;
    }
    
    ppci = ppciInit;
    set = FALSE;
    while (nglyph--)
    {
	pci = *ppci++;
	h = pci->metrics.ascent + pci->metrics.descent;
	w = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
	x1 = x + pci->metrics.leftSideBearing;
	y1 = y - pci->metrics.ascent;
	bbox.x1 = x1;
	bbox.y1 = y1;
	bbox.x2 = x1 + w;
	bbox.y2 = y1 + h;
	switch (RECT_IN_REGION(pGC->pScreen, pClip, &bbox))
	{
	case rgnIN:
	    lw = h * ((w + 31) >> 5);
	    if (lw)
	    {
		if (!set)
		{
		    _igsSetTransparentPlaneBlt (cop, alu, pGC->planemask, pGC->fgPixel, cmd);
		    set = TRUE;
		}
		_igsPlaneBlt(cop,
			     x + pci->metrics.leftSideBearing,
			     y - pci->metrics.ascent,
			     w, h, cmd);
		bits = (unsigned long *) pci->bits;
		PixTransStart (lw);
		while (lw--) 
		{
		    b = *bits++;
		    IgsAdjustBits32 (b);
		    PixTransStore(b);
		}
		KdMarkSync (pDrawable->pScreen);
	    }
	    break;
	case rgnPART:
	    set = FALSE;
	    KdCheckSync (pDrawable->pScreen);
	    fbPutXYImage (pDrawable,
			  pClip,
			  fbPriv->fg,
			  fbPriv->bg,
			  fbPriv->pm,
			  alu,
			  FALSE,
			  x1, y1,
			  w, h,
			  (FbStip *) pci->bits,
			  (w + 31) >> 5,
			  0);
	    break;
	case rgnOUT:
	    break;
	}
	x += pci->metrics.characterWidth;
    }
}
		       
void
igsGlyphBlt (DrawablePtr    pDrawable, 
	     GCPtr	    pGC, 
	     int	    x,
	     int	    y, 
	     unsigned int   nglyph,
	     CharInfoPtr    *ppciInit,
	     Bool	    image)
{
    SetupIgs(pDrawable->pScreen);
    CARD32	    cmd;
    int		    h;
    int		    w;
    int		    xBack, yBack;
    int		    hBack, wBack;
    int		    lw;
    FontPtr	    pfont = pGC->font;
    CharInfoPtr	    pci;
    unsigned long   *bits;
    BoxPtr	    extents;
    BoxRec	    bbox;
    CARD32	    b;
    CharInfoPtr	    *ppci;
    unsigned char   alu;
    PixTransDeclare;

    /*
     * Paint background for image text
     */
    if (image)
    {
	xBack = x;
	yBack = y - FONTASCENT(pGC->font);
	wBack = 0;
	hBack = FONTASCENT(pGC->font) + FONTDESCENT(pGC->font);
	if (hBack)
	{
	    h = nglyph;
	    ppci = ppciInit;
	    while (h--)
		wBack += (*ppci++)->metrics.characterWidth;
	}
	if (wBack < 0)
	{
	    xBack = xBack + wBack;
	    wBack = -wBack;
	}
	if (hBack < 0)
	{
	    yBack = yBack + hBack;
	    hBack = -hBack;
	}
	alu = GXcopy;
	if (wBack)
	{
	    _igsSetSolidRect (cop, GXcopy, pGC->planemask, pGC->bgPixel, cmd);
	    _igsRect (cop, xBack, yBack, wBack, hBack, cmd);
	}
    }
    else
    {
	wBack = 0;
	alu = pGC->alu;
    }
    
    _igsSetTransparentPlaneBlt (cop, alu, pGC->planemask, pGC->fgPixel, cmd);
    ppci = ppciInit;
    while (nglyph--)
    {
	pci = *ppci++;
	h = pci->metrics.ascent + pci->metrics.descent;
	w = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
	lw = h * ((w + 31) >> 5);
	if (lw)
	{
	    _igsPlaneBlt(cop,
			 x + pci->metrics.leftSideBearing,
			 y - pci->metrics.ascent,
			 w, h, cmd);
	    bits = (unsigned long *) pci->bits;
	    PixTransStart(lw);
	    while (lw--) 
	    {
		b = *bits++;
		IgsAdjustBits32 (b);
		PixTransStore(b);
	    }
	}
	x += pci->metrics.characterWidth;
    }
    KdMarkSync (pDrawable->pScreen);
}

void
igsTEGlyphBlt (DrawablePtr  pDrawable,
	       GCPtr	    pGC,
	       int	    xInit,
	       int	    yInit,
	       unsigned int nglyph,
	       CharInfoPtr  *ppci,
	       Bool	    image)
{
    SetupIgs(pDrawable->pScreen);
    CARD32	    cmd;
    int		    x, y;
    int		    h, lw, lwTmp;
    int		    w;
    FontPtr	    pfont = pGC->font;
    unsigned long   *char1, *char2, *char3, *char4;
    int		    widthGlyphs, widthGlyph;
    BoxRec	    bbox;
    CARD32	    tmp;
    PixTransDeclare;

    widthGlyph = FONTMAXBOUNDS(pfont,characterWidth);
    if (!widthGlyph)
	return;
    
    h = FONTASCENT(pfont) + FONTDESCENT(pfont);
    if (!h)
	return;
    
    x = xInit + FONTMAXBOUNDS(pfont,leftSideBearing);
    y = yInit - FONTASCENT(pfont);
    
    if (image)
    {
	_igsSetOpaquePlaneBlt (cop, GXcopy, pGC->planemask, pGC->fgPixel, pGC->bgPixel, cmd);
    }
    else
    {
	_igsSetTransparentPlaneBlt (cop, pGC->alu, pGC->planemask, pGC->fgPixel, cmd);
    }

#if BITMAP_BIT_ORDER == LSBFirst
#define SHIFT	<<
#else
#define SHIFT	>>
#endif
    
#define LoopIt(count, w, loadup, fetch) \
    while (nglyph >= count) \
    { \
	nglyph -= count; \
	_igsPlaneBlt (cop, x, y, w, h, cmd); \
	x += w; \
	loadup \
	lwTmp = h; \
	PixTransStart(h); \
	while (lwTmp--) { \
	    tmp = fetch; \
	    IgsAdjustBits32(tmp); \
	    PixTransStore(tmp); \
	} \
    }

    if (widthGlyph <= 8)
    {
	widthGlyphs = widthGlyph << 2;
	LoopIt(4, widthGlyphs,
	       char1 = (unsigned long *) (*ppci++)->bits;
	       char2 = (unsigned long *) (*ppci++)->bits;
	       char3 = (unsigned long *) (*ppci++)->bits;
	       char4 = (unsigned long *) (*ppci++)->bits;,
	       (*char1++ | ((*char2++ | ((*char3++ | (*char4++
						      SHIFT widthGlyph))
					 SHIFT widthGlyph))
			    SHIFT widthGlyph)))
    }
    else if (widthGlyph <= 10)
    {
	widthGlyphs = (widthGlyph << 1) + widthGlyph;
	LoopIt(3, widthGlyphs,
	       char1 = (unsigned long *) (*ppci++)->bits;
	       char2 = (unsigned long *) (*ppci++)->bits;
	       char3 = (unsigned long *) (*ppci++)->bits;,
	       (*char1++ | ((*char2++ | (*char3++ SHIFT widthGlyph)) SHIFT widthGlyph)))
    }
    else if (widthGlyph <= 16)
    {
	widthGlyphs = widthGlyph << 1;
	LoopIt(2, widthGlyphs,
	       char1 = (unsigned long *) (*ppci++)->bits;
	       char2 = (unsigned long *) (*ppci++)->bits;,
	       (*char1++ | (*char2++ SHIFT widthGlyph)))
    }
    lw = h * ((widthGlyph + 31) >> 5);
    while (nglyph--) 
    {
	_igsPlaneBlt (cop, x, y, widthGlyph, h, cmd);
	x += widthGlyph;
	char1 = (unsigned long *) (*ppci++)->bits;
	lwTmp = lw;
	PixTransStart(lw);
	while (lwTmp--)
	{
	    tmp = *char1++;
	    IgsAdjustBits32(tmp);
	    PixTransStore(tmp);
	}
    }
    KdMarkSync (pDrawable->pScreen);
}

/*
 * Blt glyphs using image transfer window
 */

void
igsPolyGlyphBlt (DrawablePtr	pDrawable, 
		 GCPtr		pGC, 
		 int		x,
		 int		y, 
		 unsigned int	nglyph,
		 CharInfoPtr	*ppci, 
		 pointer	pglyphBase)
{
    if (pGC->fillStyle != FillSolid ||
	fbGetGCPrivate(pGC)->pm != FB_ALLONES)
    {
	KdCheckPolyGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, pglyphBase);
	return;
    }
    x += pDrawable->x;
    y += pDrawable->y;

    switch (igsTextInRegion (pGC, x, y, nglyph, ppci)) {
    case rgnIN:
	if (TERMINALFONT(pGC->font))
	    igsTEGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, FALSE);
	else
	    igsGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, FALSE);
	break;
    case rgnPART:
	igsGlyphBltClipped (pDrawable, pGC, x, y, nglyph, ppci, FALSE);
	break;
    case rgnOUT:
	break;
    }
}

void
igsImageGlyphBlt (DrawablePtr pDrawable, 
		GCPtr pGC, 
		int x, int y, 
		unsigned int nglyph, 
		CharInfoPtr *ppci, 
		pointer pglyphBase)
{
    if (fbGetGCPrivate(pGC)->pm != FB_ALLONES)
    {
	KdCheckImageGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, pglyphBase);
	return;
    }
    x += pDrawable->x;
    y += pDrawable->y;

    switch (igsTextInRegion (pGC, x, y, nglyph, ppci)) {
    case rgnIN:
	if (TERMINALFONT(pGC->font))
	    igsTEGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, TRUE);
	else
	    igsGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, TRUE);
	break;
    case rgnPART:
	igsGlyphBltClipped (pDrawable, pGC, x, y, nglyph, ppci, TRUE);
	break;
    case rgnOUT:
	break;
    }
}

static void
igsInvalidatePattern (IgsPatternCache	*c,
		      PixmapPtr		pPixmap)
{
    int i;

    if (c->base)
    {
	for (i = 0; i < IGS_NUM_PATTERN; i++)
	{
	    if (c->pattern[i].serial_number == pPixmap->drawable.serialNumber)
		c->pattern[i].serial_number = ~0;
	}
    }    
}

static void
igsInitPattern (IgsPatternCache *c, int bsize, int psize)
{
    int	    i;
    int	    boffset;
    int	    poffset;

    for (i = 0; i < IGS_NUM_PATTERN; i++)
    {
	boffset = i * bsize;
	poffset = i * psize;
	c->pattern[i].xrot = -1;
	c->pattern[i].yrot = -1;
	c->pattern[i].serial_number = ~0;
	c->pattern[i].offset = c->offset + poffset;
	c->pattern[i].base = c->base + boffset;
    }
    c->next = 0;
}
	
static const GCOps	igsOps = {
    igsFillSpans,
    KdCheckSetSpans,
    KdCheckPutImage,
    igsCopyArea,
    igsCopyPlane,
    KdCheckPolyPoint,
    KdCheckPolylines,
    KdCheckPolySegment,
    miPolyRectangle,
    KdCheckPolyArc,
    miFillPolygon,
    igsPolyFillRect,
    KdCheckPolyFillArc,
    miPolyText8,
    miPolyText16,
    miImageText8,
    miImageText16,
    igsImageGlyphBlt,
    igsPolyGlyphBlt,
    igsPushPixels,
};

void
igsValidateGC (GCPtr pGC, Mask changes, DrawablePtr pDrawable)
{
    FbGCPrivPtr fbPriv = fbGetGCPrivate(pGC);
    
    fbValidateGC (pGC, changes, pDrawable);
    
    if (pDrawable->type == DRAWABLE_WINDOW)
	pGC->ops = (GCOps *) &igsOps;
    else
	pGC->ops = (GCOps *) &fbGCOps;
}

GCFuncs	igsGCFuncs = {
    igsValidateGC,
    miChangeGC,
    miCopyGC,
    miDestroyGC,
    miChangeClip,
    miDestroyClip,
    miCopyClip
};

int
igsCreateGC (GCPtr pGC)
{
    if (!fbCreateGC (pGC))
	return FALSE;

    if (pGC->depth != 1)
	pGC->funcs = &igsGCFuncs;
    
    return TRUE;
}

void
igsCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
    ScreenPtr	pScreen = pWin->drawable.pScreen;
    KdScreenPriv(pScreen);
    RegionRec	rgnDst;
    int		dx, dy;
    WindowPtr	pwinRoot;

    pwinRoot = WindowTable[pWin->drawable.pScreen->myNum];

    dx = ptOldOrg.x - pWin->drawable.x;
    dy = ptOldOrg.y - pWin->drawable.y;
    REGION_TRANSLATE(pWin->drawable.pScreen, prgnSrc, -dx, -dy);

    REGION_INIT (pWin->drawable.pScreen, &rgnDst, NullBox, 0);
    
    REGION_INTERSECT(pWin->drawable.pScreen, &rgnDst, &pWin->borderClip, prgnSrc);

    fbCopyRegion ((DrawablePtr)pwinRoot, (DrawablePtr)pwinRoot,
		  0,
		  &rgnDst, dx, dy, igsCopyNtoN, 0, 0);
    
    REGION_UNINIT(pWin->drawable.pScreen, &rgnDst);
}


Bool
igsDrawInit (ScreenPtr pScreen)
{
    KdScreenPriv(pScreen);
    igsCardInfo(pScreenPriv);
    igsScreenInfo(pScreenPriv);
    int i;
    int pattern_size;
    int boffset, poffset;
    
    KdScreenInitAsync (pScreen);
    
    /*
     * Replace various fb screen functions
     */
    pScreen->CreateGC = igsCreateGC;
    pScreen->CopyWindow = igsCopyWindow;

    /*
     * Initialize patterns
     */
    if (igss->tile.base)
    {
	pattern_size = IgsTileSize(pScreenPriv->screen->fb[0].bitsPerPixel);
	igsInitPattern (&igss->tile,
			pattern_size,
			pattern_size * 8 / pScreenPriv->screen->fb[0].bitsPerPixel);
	pattern_size = IgsStippleSize(pScreenPriv->screen->fb[0].bitsPerPixel);
	igsInitPattern (&igss->stipple,
			pattern_size,
			pattern_size * 8 / pScreenPriv->screen->fb[0].bitsPerPixel);
    }
    return TRUE;
}

void
igsDrawEnable (ScreenPtr pScreen)
{
    SetupIgs(pScreen);
    CARD32  cmd;
    CARD32  base;
    CARD16  stride;
    CARD32  format;
    
    stride = pScreenPriv->screen->fb[0].pixelStride;
    _igsWaitIdleEmpty(cop);
    _igsReset(cop);
    switch (pScreenPriv->screen->fb[0].bitsPerPixel) {
    case 8:
	format = IGS_FORMAT_8BPP;
	break;
    case 16:
	format = IGS_FORMAT_16BPP;
	break;
    case 24:
	format = IGS_FORMAT_24BPP;
	break;
    case 32:
	format = IGS_FORMAT_32BPP;
	break;
    }
    cop->format = format;
    cop->dst_stride = stride - 1;
    cop->src1_stride = stride - 1;
    cop->src2_stride = stride - 1;
    cop->src1_start = 0;
    cop->src2_start = 0;
    cop->extension |= IGS_BLOCK_COP_REG | IGS_BURST_ENABLE;
    
    _igsSetSolidRect(cop, GXcopy, ~0, pScreen->blackPixel, cmd);
    _igsRect (cop, 0, 0, 
	      pScreenPriv->screen->width, pScreenPriv->screen->height,
	      cmd);
    _igsWaitIdleEmpty (cop);
}

void
igsDrawDisable (ScreenPtr pScreen)
{
}

void
igsDrawFini (ScreenPtr pScreen)
{
}

void
igsDrawSync (ScreenPtr pScreen)
{
    SetupIgs(pScreen);

    _igsWaitIdleEmpty(cop);
}