aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/glamor/glamor_largepixmap.c
blob: 91ee8f2dfe3a5c7eea8f61a269d10af36b55a5ce (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
#include <stdlib.h>

#include "glamor_priv.h"

/**
 * Clip the boxes regards to each pixmap's block array.
 *
 * Should translate the region to relative coords to the pixmap,
 * start at (0,0).
 */
#if 0
//#define DEBUGF(str, ...)  do {} while(0)
#define DEBUGF(str, ...) ErrorF(str, ##__VA_ARGS__)
//#define DEBUGRegionPrint(x) do {} while (0)
#define DEBUGRegionPrint RegionPrint
#endif

static glamor_pixmap_clipped_regions *
__glamor_compute_clipped_regions(int block_w,
			       int block_h,
			       int block_stride,
			       int x, int y,
			       int w, int h,
                               RegionPtr region,
                               int *n_region,
			       int reverse,
			       int upsidedown)
{
	glamor_pixmap_clipped_regions * clipped_regions;
	BoxPtr extent;
	int start_x, start_y, end_x, end_y;
	int start_block_x, start_block_y;
	int end_block_x, end_block_y;
	int loop_start_block_x, loop_start_block_y;
	int loop_end_block_x, loop_end_block_y;
	int loop_block_stride;
	int i, j, delta_i, delta_j;
	RegionRec temp_region;
	RegionPtr current_region;
	int block_idx;
	int k = 0;
	int temp_block_idx;

	extent = RegionExtents(region);
	start_x = MAX(x, extent->x1);
	start_y = MAX(y, extent->y1);
	end_x = MIN(x + w, extent->x2);
	end_y = MIN(y + h, extent->y2);

	DEBUGF("start compute clipped regions:\n");
	DEBUGF("block w %d h %d  x %d y %d w %d h %d, block_stride %d \n",
		block_w, block_h, x, y, w, h, block_stride);
	DEBUGRegionPrint(region);

	DEBUGF("start_x %d start_y %d end_x %d end_y %d \n", start_x, start_y, end_x, end_y);

	if (start_x >= end_x || start_y >= end_y) {
		*n_region = 0;
		return NULL;
	}

	start_block_x = (start_x  - x)/ block_w;
	start_block_y = (start_y - y)/ block_h;
	end_block_x = (end_x - x)/ block_w;
	end_block_y = (end_y - y)/ block_h;

	clipped_regions = calloc((end_block_x - start_block_x + 1)
				 * (end_block_y - start_block_y + 1),
				 sizeof(*clipped_regions));


	DEBUGF("startx %d starty %d endx %d endy %d \n",
		start_x, start_y, end_x, end_y);
	DEBUGF("start_block_x %d end_block_x %d \n", start_block_x, end_block_x);
	DEBUGF("start_block_y %d end_block_y %d \n", start_block_y, end_block_y);

	if (!reverse) {
		loop_start_block_x = start_block_x;
		loop_end_block_x = end_block_x + 1;
		delta_i = 1;
	} else {
		loop_start_block_x = end_block_x;
		loop_end_block_x = start_block_x - 1;
		delta_i = -1;
	}

	if (!upsidedown) {
		loop_start_block_y = start_block_y;
		loop_end_block_y = end_block_y + 1;
		delta_j = 1;
	} else {
		loop_start_block_y = end_block_y;
		loop_end_block_y = start_block_y - 1;
		delta_j = -1;
	}

	loop_block_stride = delta_j * block_stride;
	block_idx = (loop_start_block_y - delta_j) * block_stride;

	for(j = loop_start_block_y; j != loop_end_block_y; j += delta_j)
	{
		block_idx += loop_block_stride;
		temp_block_idx = block_idx + loop_start_block_x;
		for(i = loop_start_block_x;
		    i != loop_end_block_x; i += delta_i, temp_block_idx += delta_i)
		{
			BoxRec temp_box;
			temp_box.x1 = x + i * block_w;
			temp_box.y1 = y + j * block_h;
			temp_box.x2 = MIN(temp_box.x1 + block_w, end_x);
			temp_box.y2 = MIN(temp_box.y1 + block_h, end_y);
			RegionInitBoxes(&temp_region, &temp_box, 1);
			DEBUGF("block idx %d \n",temp_block_idx);
			DEBUGRegionPrint(&temp_region);
			current_region = RegionCreate(NULL, 4);
			RegionIntersect(current_region, &temp_region, region);
			DEBUGF("i %d j %d  region: \n",i ,j);
			DEBUGRegionPrint(current_region);
			if (RegionNumRects(current_region)) {
				clipped_regions[k].region = current_region;
				clipped_regions[k].block_idx = temp_block_idx;
				k++;
			} else
				RegionDestroy(current_region);
			RegionUninit(&temp_region);
		}
	}

	*n_region = k;
	return clipped_regions;
}

/**
 * Do a two round clipping,
 * first is to clip the region regard to current pixmap's
 * block array. Then for each clipped region, do a inner
 * block clipping. This is to make sure the final result
 * will be shapped by inner_block_w and inner_block_h, and
 * the final region also will not cross the pixmap's block
 * boundary.
 *
 * This is mainly used by transformation support when do
 * compositing.
 */

glamor_pixmap_clipped_regions *
glamor_compute_clipped_regions_ext(glamor_pixmap_private *pixmap_priv,
				   RegionPtr region,
				   int *n_region,
				   int inner_block_w, int inner_block_h,
				   int reverse, int upsidedown)
{
	glamor_pixmap_clipped_regions * clipped_regions, *inner_regions, *result_regions;
	int i, j, x, y, k, inner_n_regions;
	int width, height;
	glamor_pixmap_private_large_t *priv;
	priv = &pixmap_priv->large;

	DEBUGF("ext called \n");

	if (pixmap_priv->type != GLAMOR_TEXTURE_LARGE) {
		clipped_regions = calloc(1, sizeof(*clipped_regions));
		if (clipped_regions == NULL) {
			*n_region = 0;
			return NULL;
		}
		clipped_regions[0].region = RegionCreate(NULL, 1);
		clipped_regions[0].block_idx = 0;
		RegionCopy(clipped_regions[0].region, region);
		*n_region = 1;
		priv->block_w = priv->base.pixmap->drawable.width;
		priv->block_h = priv->base.pixmap->drawable.height;
		priv->box_array = &priv->box;
		priv->box.x1 = priv->box.y1 = 0;
		priv->box.x2 = priv->block_w;
		priv->box.y2 = priv->block_h;
	} else {
		clipped_regions =  __glamor_compute_clipped_regions(priv->block_w,
					priv->block_h,
					priv->block_wcnt,
					0, 0,
					priv->base.pixmap->drawable.width,
					priv->base.pixmap->drawable.height,
					region, n_region, reverse, upsidedown
					);

		if (clipped_regions == NULL) {
			*n_region = 0;
			return NULL;
		}
	}
	if (inner_block_w >= priv->block_w
	    && inner_block_h >= priv->block_h)
		return clipped_regions;
	result_regions = calloc(*n_region
				* ((priv->block_w + inner_block_w - 1)/inner_block_w)
				* ((priv->block_h + inner_block_h - 1)/ inner_block_h),
				sizeof(*result_regions));
	k = 0;
	for(i = 0; i < *n_region; i++)
	{
		x = priv->box_array[clipped_regions[i].block_idx].x1;
		y = priv->box_array[clipped_regions[i].block_idx].y1;
		width = priv->box_array[clipped_regions[i].block_idx].x2 - x;
		height = priv->box_array[clipped_regions[i].block_idx].y2 - y;
		inner_regions = __glamor_compute_clipped_regions(inner_block_w,
					inner_block_h,
					0, x, y,
					width,
					height,
					clipped_regions[i].region,
					&inner_n_regions, reverse, upsidedown);
		for(j = 0; j < inner_n_regions; j++)
		{
			result_regions[k].region = inner_regions[j].region;
			result_regions[k].block_idx = clipped_regions[i].block_idx;
			k++;
		}
		free(inner_regions);
	}
	*n_region = k;
	free(clipped_regions);
	return result_regions;
}

/*
 *
 * For the repeat pad mode, we can simply convert the region and
 * let the out-of-box region can cover the needed edge of the source/mask
 * Then apply a normal clip we can get what we want.
 */
static RegionPtr
_glamor_convert_pad_region(RegionPtr region, int w, int h)
{
	RegionPtr pad_region;
	int nrect;
	BoxPtr box;
	int overlap;

	nrect = RegionNumRects(region);
	box = RegionRects(region);
	pad_region = RegionCreate(NULL, 4);
	if (pad_region == NULL)
		return NULL;
	while(nrect--) {
		BoxRec pad_box;
		RegionRec temp_region;
		pad_box = *box;
		if (pad_box.x1 < 0 && pad_box.x2 <= 0)
			pad_box.x2 = 1;
		else if (pad_box.x1 >= w && pad_box.x2 > w)
			pad_box.x1 = w - 1;
		if (pad_box.y1 < 0 && pad_box.y2 <=0)
			pad_box.y2 = 1;
		else if (pad_box.y1 >= h && pad_box.y2 > h)
			pad_box.y1 = h - 1;
		RegionInitBoxes(&temp_region, &pad_box, 1);
		RegionAppend(pad_region, &temp_region);
		RegionUninit(&temp_region);
		box++;
	}
	RegionValidate(pad_region, &overlap);
	return pad_region;
}

/*
 * For one type of large pixmap, its one direction is not exceed the
 * size limitation, and in another word, on one direction it has only
 * one block.
 *
 * This case of reflect repeating, we can optimize it and avoid repeat
 * clip on that direction. We can just enlarge the repeat box and can
 * cover all the dest region on that direction. But latter, we need to
 * fixup the clipped result to get a correct coords for the subsequent
 * processing. This function is to do the coords correction.
 *
 * */
static void
_glamor_largepixmap_reflect_fixup(short *xy1, short *xy2, int wh)
{
	int odd1, odd2;
	int c1, c2;

	if (*xy2 - *xy1 > wh) {
		*xy1 = 0;
		*xy2 = wh;
		return;
	}
	modulus(*xy1, wh, c1);
	odd1 = ((*xy1 - c1) / wh) & 0x1;
	modulus(*xy2, wh, c2);
	odd2 = ((*xy2 - c2) / wh) & 0x1;

	if (odd1 && odd2) {
		*xy1 = wh - c2;
		*xy2 = wh - c1;
	} else if (odd1 && !odd2) {
		*xy1 = 0;
		*xy2 = MAX(c2, wh - c1);
	} else if (!odd1 && odd2) {
		*xy2 = wh;
		*xy1 = MIN(c1, wh - c2);
	} else {
		*xy1 = c1;
		*xy2 = c2;
	}
}

/**
 * Clip the boxes regards to each pixmap's block array.
 *
 * Should translate the region to relative coords to the pixmap,
 * start at (0,0).
 *
 * @is_transform: if it is set, it has a transform matrix.
 *
 */
static glamor_pixmap_clipped_regions *
_glamor_compute_clipped_regions(glamor_pixmap_private *pixmap_priv,
				RegionPtr region, int *n_region,
				int repeat_type, int is_transform,
				int reverse, int upsidedown)
{
	glamor_pixmap_clipped_regions * clipped_regions;
	BoxPtr extent;
	int i, j;
	RegionPtr current_region;
	int pixmap_width, pixmap_height;
	int m;
	BoxRec repeat_box;
	RegionRec repeat_region;
	int right_shift = 0;
	int down_shift = 0;
	int x_center_shift = 0, y_center_shift = 0;
	glamor_pixmap_private_large_t *priv;
	priv = &pixmap_priv->large;

	DEBUGRegionPrint(region);
	if (pixmap_priv->type != GLAMOR_TEXTURE_LARGE) {
		clipped_regions = calloc(1, sizeof(*clipped_regions));
		clipped_regions[0].region = RegionCreate(NULL, 1);
		clipped_regions[0].block_idx = 0;
		RegionCopy(clipped_regions[0].region, region);
		*n_region = 1;
		return clipped_regions;
	}

	pixmap_width = priv->base.pixmap->drawable.width;
	pixmap_height = priv->base.pixmap->drawable.height;
	if (repeat_type == 0 || repeat_type == RepeatPad) {
		RegionPtr saved_region = NULL;
		if (repeat_type == RepeatPad) {
			saved_region = region;
			region = _glamor_convert_pad_region(saved_region, pixmap_width, pixmap_height);
			if (region == NULL) {
				*n_region = 0;
				return NULL;
			}
		}
		clipped_regions = __glamor_compute_clipped_regions(priv->block_w,
							priv->block_h,
							priv->block_wcnt,
							0, 0,
							priv->base.pixmap->drawable.width,
							priv->base.pixmap->drawable.height,
							region, n_region, reverse, upsidedown
							);
		if (saved_region)
			RegionDestroy(region);
		return clipped_regions;
	}
	extent = RegionExtents(region);

	x_center_shift = extent->x1 / pixmap_width;
	if (x_center_shift < 0)
		x_center_shift--;
	if (abs(x_center_shift) & 1)
		x_center_shift++;
	y_center_shift = extent->y1 / pixmap_height;
	if (y_center_shift < 0)
		y_center_shift--;
	if (abs(y_center_shift) & 1)
		y_center_shift++;

	if (extent->x1 < 0)
		right_shift = ((-extent->x1 + pixmap_width - 1) / pixmap_width );
	if (extent->y1 < 0)
		down_shift = ((-extent->y1 + pixmap_height - 1) / pixmap_height );

	if (right_shift != 0 || down_shift != 0) {
		if (repeat_type == RepeatReflect) {
			right_shift = (right_shift + 1)&~1;
			down_shift = (down_shift + 1)&~1;
		}
		RegionTranslate(region, right_shift * pixmap_width, down_shift * pixmap_height);
	}

	extent = RegionExtents(region);
	/* Tile a large pixmap to another large pixmap.
	 * We can't use the target large pixmap as the
	 * loop variable, instead we need to loop for all
	 * the blocks in the tile pixmap.
	 *
	 * simulate repeat each single block to cover the
	 * target's blocks. Two special case:
	 * a block_wcnt == 1 or block_hcnt ==1, then we
	 * only need to loop one direction as the other
	 * direction is fully included in the first block.
	 *
	 * For the other cases, just need to start
	 * from a proper shiftx/shifty, and then increase
	 * y by tile_height each time to walk trhough the
	 * target block and then walk trhough the target
	 * at x direction by increate tile_width each time.
	 *
	 * This way, we can consolidate all the sub blocks
	 * of the target boxes into one tile source's block.
	 *
	 * */
	m = 0;
	clipped_regions = calloc(priv->block_wcnt * priv->block_hcnt,
				 sizeof(*clipped_regions));
	if (clipped_regions == NULL) {
		*n_region = 0;
		return NULL;
	}
	if (right_shift != 0 || down_shift != 0) {
		DEBUGF("region to be repeated shifted \n");
		DEBUGRegionPrint(region);
	}
	DEBUGF("repeat pixmap width %d height %d \n", pixmap_width, pixmap_height);
	DEBUGF("extent x1 %d y1 %d x2 %d y2 %d \n", extent->x1, extent->y1, extent->x2, extent->y2);
	for(j = 0; j < priv->block_hcnt; j++)
	{
		for(i = 0; i < priv->block_wcnt; i++)
		{
			int dx = pixmap_width;
			int dy = pixmap_height;
			int idx;
			int shift_x;
			int shift_y;
			int saved_y1, saved_y2;
			int x_idx = 0, y_idx = 0, saved_y_idx = 0;
			RegionRec temp_region;
			BoxRec reflect_repeat_box;
			BoxPtr valid_repeat_box;

			shift_x = (extent->x1 / pixmap_width) * pixmap_width;
			shift_y = (extent->y1 / pixmap_height) * pixmap_height;
			idx = j * priv->block_wcnt + i;
			if (repeat_type == RepeatReflect) {
				x_idx = (extent->x1 / pixmap_width);
				y_idx = (extent->y1 / pixmap_height);
			}

			/* Construct a rect to clip the target region. */
			repeat_box.x1 = shift_x + priv->box_array[idx].x1;
			repeat_box.y1 = shift_y + priv->box_array[idx].y1;
			if (priv->block_wcnt == 1) {
				repeat_box.x2 = extent->x2;
				dx = extent->x2 - repeat_box.x1;
			} else
				repeat_box.x2 = shift_x + priv->box_array[idx].x2;
			if (priv->block_hcnt == 1) {
				repeat_box.y2 = extent->y2;
				dy = extent->y2 - repeat_box.y1;
			} else
				repeat_box.y2 = shift_y + priv->box_array[idx].y2;

			current_region = RegionCreate(NULL, 4);
			RegionInit(&temp_region, NULL, 4);
			DEBUGF("init repeat box %d %d %d %d \n",
				repeat_box.x1, repeat_box.y1, repeat_box.x2, repeat_box.y2);

			if (repeat_type == RepeatNormal) {
				saved_y1 = repeat_box.y1;
				saved_y2 = repeat_box.y2;
				for(; repeat_box.x1 < extent->x2;
				      repeat_box.x1 += dx, repeat_box.x2 += dx)
				{
					repeat_box.y1 = saved_y1;
					repeat_box.y2 = saved_y2;
					for( repeat_box.y1 = saved_y1, repeat_box.y2 = saved_y2;
					     repeat_box.y1 < extent->y2;
					     repeat_box.y1 += dy, repeat_box.y2 += dy)
					{

						RegionInitBoxes(&repeat_region, &repeat_box, 1);
						DEBUGF("Start to clip repeat region: \n");
						DEBUGRegionPrint(&repeat_region);
						RegionIntersect(&temp_region, &repeat_region, region);
						DEBUGF("clip result:\n");
						DEBUGRegionPrint(&temp_region);
						RegionAppend(current_region, &temp_region);
						RegionUninit(&repeat_region);
					}
				}
			} else if (repeat_type == RepeatReflect) {
				saved_y1 = repeat_box.y1;
				saved_y2 = repeat_box.y2;
				saved_y_idx = y_idx;
				for(; ; repeat_box.x1 += dx, repeat_box.x2 += dx)
				{
					repeat_box.y1 = saved_y1;
					repeat_box.y2 = saved_y2;
					y_idx = saved_y_idx;
					reflect_repeat_box.x1 = (x_idx & 1) ?
								((2 * x_idx + 1) * dx - repeat_box.x2) : repeat_box.x1;
					reflect_repeat_box.x2 = (x_idx & 1) ?
								((2 * x_idx + 1) * dx - repeat_box.x1) : repeat_box.x2;
					valid_repeat_box = &reflect_repeat_box;

					if (valid_repeat_box->x1 >= extent->x2)
						break;
					for( repeat_box.y1 = saved_y1, repeat_box.y2 = saved_y2;
					     ;
					     repeat_box.y1 += dy, repeat_box.y2 += dy)
					{

						DEBUGF("x_idx %d y_idx %d dx %d dy %d\n", x_idx, y_idx, dx, dy);
						DEBUGF("repeat box %d %d %d %d \n",
							repeat_box.x1, repeat_box.y1, repeat_box.x2, repeat_box.y2);

						if (priv->block_hcnt > 1) {
						reflect_repeat_box.y1 = (y_idx & 1) ?
							((2 * y_idx + 1) * dy - repeat_box.y2) : repeat_box.y1;
						reflect_repeat_box.y2 = (y_idx & 1) ?
							((2 * y_idx + 1) * dy - repeat_box.y1) : repeat_box.y2;
						} else {
						reflect_repeat_box.y1 = repeat_box.y1;
						reflect_repeat_box.y2 = repeat_box.y2;
						}

						DEBUGF("valid_repeat_box x1 %d y1 %d \n",
							valid_repeat_box->x1, valid_repeat_box->y1);
						if (valid_repeat_box->y1 >= extent->y2)
							break;
						RegionInitBoxes(&repeat_region, valid_repeat_box, 1);
						DEBUGF("start to clip repeat[reflect] region: \n");
						DEBUGRegionPrint(&repeat_region);
						RegionIntersect(&temp_region, &repeat_region, region);
						DEBUGF("result:\n");
						DEBUGRegionPrint(&temp_region);
						if (is_transform && RegionNumRects(&temp_region)) {
							BoxRec temp_box;
							BoxPtr temp_extent;
							temp_extent = RegionExtents(&temp_region);
							if (priv->block_wcnt > 1) {
								if (x_idx & 1) {
									temp_box.x1 = ((2 * x_idx + 1)*dx - temp_extent->x2);
									temp_box.x2 = ((2 * x_idx + 1)*dx - temp_extent->x1);
								} else {
									temp_box.x1 = temp_extent->x1;
									temp_box.x2 = temp_extent->x2;
								}
								modulus(temp_box.x1, pixmap_width, temp_box.x1);
								modulus(temp_box.x2, pixmap_width, temp_box.x2);
								if (temp_box.x2 == 0) temp_box.x2 = pixmap_width;
							} else {
								temp_box.x1 = temp_extent->x1;
								temp_box.x2 = temp_extent->x2;
								_glamor_largepixmap_reflect_fixup(&temp_box.x1, &temp_box.x2, pixmap_width);
							}

							if (priv->block_hcnt > 1) {
								if (y_idx & 1) {
									temp_box.y1 = ((2 * y_idx + 1)*dy - temp_extent->y2);
									temp_box.y2 = ((2 * y_idx + 1)*dy - temp_extent->y1);
								} else {
									temp_box.y1 = temp_extent->y1;
									temp_box.y2 = temp_extent->y2;
								}

								modulus(temp_box.y1, pixmap_height, temp_box.y1);
								modulus(temp_box.y2, pixmap_height, temp_box.y2);
								if (temp_box.y2 == 0) temp_box.y2 = pixmap_height;
							} else {
								temp_box.y1 = temp_extent->y1;
								temp_box.y2 = temp_extent->y2;
								_glamor_largepixmap_reflect_fixup(&temp_box.y1, &temp_box.y2, pixmap_height);
							}

							RegionInitBoxes(&temp_region, &temp_box, 1);
							RegionTranslate(&temp_region, x_center_shift * pixmap_width, y_center_shift * pixmap_height);
							DEBUGF("for transform result:\n");
							DEBUGRegionPrint(&temp_region);
						}
						RegionAppend(current_region, &temp_region);
						RegionUninit(&repeat_region);
						y_idx++;
					}
					x_idx++;
				}
			}
			DEBUGF("dx %d dy %d \n", dx, dy);

			if (RegionNumRects(current_region)) {

				if ((right_shift != 0 || down_shift != 0) && !(is_transform && repeat_type == RepeatReflect))
					RegionTranslate(current_region,
							-right_shift * pixmap_width,
							-down_shift * pixmap_height);
				clipped_regions[m].region = current_region;
				clipped_regions[m].block_idx = idx;
				m++;
			} else
				RegionDestroy(current_region);
			RegionUninit(&temp_region);
		}
	}

	if (right_shift != 0 || down_shift != 0)
		RegionTranslate(region, -right_shift * pixmap_width, -down_shift * pixmap_height);
	*n_region = m;

	return clipped_regions;
}

glamor_pixmap_clipped_regions *
glamor_compute_clipped_regions(glamor_pixmap_private *priv, RegionPtr region,
			       int *n_region, int repeat_type,
			       int reverse, int upsidedown)
{
	return _glamor_compute_clipped_regions(priv, region, n_region, repeat_type, 0, reverse, upsidedown);
}

/* XXX overflow still exist. maybe we need to change to use region32.
 * by default. Or just use region32 for repeat cases?
 **/
glamor_pixmap_clipped_regions *
glamor_compute_transform_clipped_regions(glamor_pixmap_private *priv, struct pixman_transform *transform,
					 RegionPtr region, int *n_region, int dx, int dy, int repeat_type,
					 int reverse, int upsidedown)
{
	BoxPtr temp_extent;
	struct pixman_box32 temp_box;
	struct pixman_box16 short_box;
	RegionPtr temp_region;
	glamor_pixmap_clipped_regions *ret;

	temp_region = RegionCreate(NULL, 4);
	temp_extent = RegionExtents(region);
	DEBUGF("dest region \n");
	DEBUGRegionPrint(region);
	/* dx/dy may exceed MAX SHORT. we have to use
	 * a box32 to represent it.*/
	temp_box.x1 = temp_extent->x1 + dx;
	temp_box.x2 = temp_extent->x2 + dx;
	temp_box.y1 = temp_extent->y1 + dy;
	temp_box.y2 = temp_extent->y2 + dy;

	DEBUGF("source box %d %d %d %d \n", temp_box.x1, temp_box.y1, temp_box.x2, temp_box.y2);
	if (transform)
		glamor_get_transform_extent_from_box(&temp_box, transform);
	if (repeat_type == RepeatNone) {
		if (temp_box.x1 < 0) temp_box.x1 = 0;
		if (temp_box.y1 < 0) temp_box.y1 = 0;
		temp_box.x2 = MIN(temp_box.x2, priv->base.pixmap->drawable.width);
		temp_box.y2 = MIN(temp_box.y2, priv->base.pixmap->drawable.height);
	}
	/* Now copy back the box32 to a box16 box. */
	short_box.x1 = temp_box.x1;
	short_box.y1 = temp_box.y1;
	short_box.x2 = temp_box.x2;
	short_box.y2 = temp_box.y2;
	RegionInitBoxes(temp_region, &short_box, 1);
	DEBUGF("copy to temp source region \n");
	DEBUGRegionPrint(temp_region);
	ret = _glamor_compute_clipped_regions(priv,
					      temp_region,
					      n_region,
					      repeat_type,
					      1, reverse,
					      upsidedown);
	DEBUGF("n_regions = %d \n", *n_region);
	RegionDestroy(temp_region);

	return ret;
}
/*
 * As transform and repeatpad mode.
 * We may get a clipped result which in multipe regions.
 * It's not easy to do a 2nd round clipping just as we do
 * without transform/repeatPad. As it's not easy to reverse
 * the 2nd round clipping result with a transform/repeatPad mode,
 * or even impossible for some transformation.
 *
 * So we have to merge the fragmental region into one region
 * if the clipped result cross the region boundary.
 */
static void
glamor_merge_clipped_regions(glamor_pixmap_private *pixmap_priv, int repeat_type,
			     glamor_pixmap_clipped_regions *clipped_regions,
			     int *n_regions, int *need_clean_fbo)
{
	BoxPtr temp_extent;
	BoxRec temp_box, copy_box;
	RegionPtr temp_region;
	glamor_pixmap_private *temp_priv;
	PixmapPtr temp_pixmap;
	int overlap;
	int i;
	int pixmap_width, pixmap_height;
	glamor_pixmap_private_large_t *priv;

	priv = &pixmap_priv->large;
	pixmap_width = priv->base.pixmap->drawable.width;
	pixmap_height = priv->base.pixmap->drawable.height;

	temp_region = RegionCreate(NULL, 4);
	for(i = 0; i < *n_regions; i++)
	{
		DEBUGF("Region %d:\n", i);
		DEBUGRegionPrint(clipped_regions[i].region);
		RegionAppend(temp_region, clipped_regions[i].region);
	}

	RegionValidate(temp_region, &overlap);
	DEBUGF("temp region: \n");
	DEBUGRegionPrint(temp_region);
	temp_extent = RegionExtents(temp_region);

	temp_box = *temp_extent;

	DEBUGF("need copy region: \n");
	DEBUGF("%d %d %d %d \n", temp_box.x1, temp_box.y1, temp_box.x2, temp_box.y2);
	temp_pixmap = glamor_create_pixmap(priv->base.pixmap->drawable.pScreen,
					  temp_box.x2 - temp_box.x1,
					  temp_box.y2 - temp_box.y1,
					  priv->base.pixmap->drawable.depth,
					  GLAMOR_CREATE_PIXMAP_FIXUP);
	if (temp_pixmap == NULL) {
		assert(0);
		return;
	}

	temp_priv = glamor_get_pixmap_private(temp_pixmap);
	assert(temp_priv->type != GLAMOR_TEXTURE_LARGE);

	priv->box = temp_box;
	if (temp_extent->x1 >= 0 && temp_extent->x2 <= pixmap_width
	    && temp_extent->y1 >= 0 && temp_extent->y2 <= pixmap_height) {
		int dx, dy;
		copy_box.x1 = 0;
		copy_box.y1 = 0;
		copy_box.x2 = temp_extent->x2 - temp_extent->x1;
		copy_box.y2 = temp_extent->y2 - temp_extent->y1;
		dx = temp_extent->x1;
		dy = temp_extent->y1;
		glamor_copy_n_to_n(&priv->base.pixmap->drawable,
				   &temp_pixmap->drawable,
				   NULL, &copy_box, 1, dx,
				   dy, 0, 0, 0, NULL);
//		glamor_solid(temp_pixmap, 0, 0, temp_pixmap->drawable.width,
//			       temp_pixmap->drawable.height, GXcopy, 0xffffffff, 0xff00);
	} else {
		for (i = 0; i < *n_regions; i++)
		{
			BoxPtr box;
			int nbox;
			box = REGION_RECTS(clipped_regions[i].region);
			nbox = REGION_NUM_RECTS(clipped_regions[i].region);
			while(nbox--) {
				int dx, dy, c, d;
				DEBUGF("box x1 %d y1 %d x2 %d y2 %d \n",
					box->x1, box->y1, box->x2, box->y2);
				modulus(box->x1, pixmap_width, c);
				dx = c - (box->x1 - temp_box.x1);
				copy_box.x1 = box->x1 - temp_box.x1;
				copy_box.x2 = box->x2 - temp_box.x1;

				modulus(box->y1, pixmap_height, d);
				dy = d - (box->y1 - temp_box.y1);
				copy_box.y1 = box->y1 - temp_box.y1;
				copy_box.y2 = box->y2 - temp_box.y1;

				DEBUGF("copying box %d %d %d %d, dx %d dy %d\n",
					copy_box.x1, copy_box.y1, copy_box.x2,
					copy_box.y2, dx, dy);

				glamor_copy_n_to_n(&priv->base.pixmap->drawable,
						   &temp_pixmap->drawable,
						   NULL, &copy_box, 1, dx,
						   dy, 0, 0, 0, NULL);
				box++;
			}
		}
		//glamor_solid(temp_pixmap, 0, 0, temp_pixmap->drawable.width,
		//	       temp_pixmap->drawable.height, GXcopy, 0xffffffff, 0xff);
	}
	/* The first region will be released at caller side. */
	for(i = 1; i < *n_regions; i++)
		RegionDestroy(clipped_regions[i].region);
	RegionDestroy(temp_region);
	priv->box = temp_box;
	priv->base.fbo = glamor_pixmap_detach_fbo(temp_priv);
	DEBUGF("priv box x1 %d y1 %d x2 %d y2 %d \n",
		priv->box.x1, priv->box.y1, priv->box.x2, priv->box.y2);
	glamor_destroy_pixmap(temp_pixmap);
	*need_clean_fbo = 1;
	*n_regions = 1;
}



/**
 * Given an expected transformed block width and block height,
 *
 * This function calculate a new block width and height which
 * guarantee the transform result will not exceed the given
 * block width and height.
 *
 * For large block width and height (> 2048), we choose a
 * smaller new width and height and to reduce the cross region
 * boundary and can avoid some overhead.
 *
 **/
Bool
glamor_get_transform_block_size(struct pixman_transform *transform,
			   int block_w, int block_h,
			   int *transformed_block_w,
			   int *transformed_block_h)
{
	double a,b,c,d,e,f,g,h;
	double scale;
	int width, height;
	a = pixman_fixed_to_double(transform->matrix[0][0]);
	b = pixman_fixed_to_double(transform->matrix[0][1]);
	c = pixman_fixed_to_double(transform->matrix[1][0]);
	d = pixman_fixed_to_double(transform->matrix[1][1]);
	scale = pixman_fixed_to_double(transform->matrix[2][2]);
	if (block_w > 2048) {
	/* For large block size, we shrink it to smaller box,
	 * thus latter we may get less cross boundary regions and
	 * thus can avoid some extra copy.
	 *
	 **/
		width = block_w  / 4;
		height = block_h / 4;
	} else {
		width = block_w - 2;
		height = block_h - 2;
	}
	e = a + b;
	f = c + d;

	g = a - b;
	h = c - d;

	e = MIN(block_w, floor(width * scale) / MAX(fabs(e), fabs(g)));
	f = MIN(block_h, floor(height * scale) / MAX(fabs(f), fabs(h)));
	*transformed_block_w = MIN(e, f) - 1;
	*transformed_block_h = *transformed_block_w;
	if (*transformed_block_w <= 0 || *transformed_block_h <= 0)
		return FALSE;
	DEBUGF("original block_w/h %d %d, fixed %d %d \n", block_w, block_h,
		*transformed_block_w, *transformed_block_h);
	return TRUE;
}

#define VECTOR_FROM_POINT(p, x, y)	\
	p.v[0] = x;  \
	p.v[1] = y;  \
	p.v[2] = 1.0;
void
glamor_get_transform_extent_from_box(struct pixman_box32 *box,
		struct pixman_transform *transform)
{
	struct pixman_f_vector p0, p1, p2, p3;
	float min_x, min_y, max_x, max_y;

	struct pixman_f_transform ftransform;

	VECTOR_FROM_POINT(p0, box->x1, box->y1)
	VECTOR_FROM_POINT(p1, box->x2, box->y1)
	VECTOR_FROM_POINT(p2, box->x2, box->y2)
	VECTOR_FROM_POINT(p3, box->x1, box->y2)

	pixman_f_transform_from_pixman_transform(&ftransform, transform);
	pixman_f_transform_point(&ftransform, &p0);
	pixman_f_transform_point(&ftransform, &p1);
	pixman_f_transform_point(&ftransform, &p2);
	pixman_f_transform_point(&ftransform, &p3);

	min_x = MIN(p0.v[0], p1.v[0]);
	min_x = MIN(min_x, p2.v[0]);
	min_x = MIN(min_x, p3.v[0]);

	min_y = MIN(p0.v[1], p1.v[1]);
	min_y = MIN(min_y, p2.v[1]);
	min_y = MIN(min_y, p3.v[1]);

	max_x = MAX(p0.v[0], p1.v[0]);
	max_x = MAX(max_x, p2.v[0]);
	max_x = MAX(max_x, p3.v[0]);

	max_y = MAX(p0.v[1], p1.v[1]);
	max_y = MAX(max_y, p2.v[1]);
	max_y = MAX(max_y, p3.v[1]);
	box->x1 = floor(min_x) - 1;
	box->y1 = floor(min_y) - 1;
	box->x2 = ceil(max_x) + 1;
	box->y2 = ceil(max_y) + 1;
}

static void
_glamor_process_transformed_clipped_region(glamor_pixmap_private *priv,
					int repeat_type,
					glamor_pixmap_clipped_regions *clipped_regions,
					int *n_regions, int *need_clean_fbo)
{
	int shift_x, shift_y;
	if (*n_regions != 1) {
	/* Merge all source regions into one region. */
		glamor_merge_clipped_regions(priv, repeat_type,
					     clipped_regions, n_regions,
					     need_clean_fbo);
	} else {
		SET_PIXMAP_FBO_CURRENT(priv,
				       clipped_regions[0].block_idx);
		if (repeat_type == RepeatReflect || repeat_type == RepeatNormal) {
			/* The required source areas are in one region,
			 * we need to shift the corresponding box's coords to proper position,
			 * thus we can calculate the relative coords correctly.*/
			BoxPtr temp_box;
			int rem;
			temp_box = RegionExtents(clipped_regions[0].region);
			modulus(temp_box->x1, priv->base.pixmap->drawable.width, rem);
			shift_x = (temp_box->x1 - rem) / priv->base.pixmap->drawable.width;
			modulus(temp_box->y1, priv->base.pixmap->drawable.height, rem);
			shift_y = (temp_box->y1 - rem) / priv->base.pixmap->drawable.height;

			if (shift_x != 0) {
				priv->large.box.x1 += shift_x * priv->base.pixmap->drawable.width;
				priv->large.box.x2 += shift_x * priv->base.pixmap->drawable.width;
			}
			if (shift_y != 0) {
				priv->large.box.y1 += shift_y * priv->base.pixmap->drawable.height;
				priv->large.box.y2 += shift_y * priv->base.pixmap->drawable.height;
			}
		}
	}
}


Bool
glamor_composite_largepixmap_region(CARD8 op,
			  PicturePtr source,
			  PicturePtr mask,
			  PicturePtr dest,
			  glamor_pixmap_private * source_pixmap_priv,
			  glamor_pixmap_private * mask_pixmap_priv,
			  glamor_pixmap_private * dest_pixmap_priv,
			  RegionPtr region, Bool force_clip,
			  INT16 x_source,
			  INT16 y_source,
			  INT16 x_mask,
			  INT16 y_mask,
			  INT16 x_dest, INT16 y_dest,
			  CARD16 width, CARD16 height)
{
	glamor_screen_private *glamor_priv;
	glamor_pixmap_clipped_regions *clipped_dest_regions;
	glamor_pixmap_clipped_regions *clipped_source_regions;
	glamor_pixmap_clipped_regions *clipped_mask_regions;
	int n_dest_regions;
	int n_mask_regions;
	int n_source_regions;
	int i,j,k;
	int need_clean_source_fbo = 0;
	int need_clean_mask_fbo = 0;
	int is_normal_source_fbo = 0;
	int is_normal_mask_fbo = 0;
	int fixed_block_width, fixed_block_height;
	int null_source, null_mask;
	glamor_pixmap_private * need_free_source_pixmap_priv = NULL;
	glamor_pixmap_private * need_free_mask_pixmap_priv = NULL;
	int source_repeat_type = 0, mask_repeat_type = 0;
	int ok = TRUE;

	if (source->repeat)
		source_repeat_type = source->repeatType;
	else
		source_repeat_type = RepeatNone;

	if (mask && mask->repeat)
		mask_repeat_type = mask->repeatType;
	else
		mask_repeat_type = RepeatNone;

	glamor_priv = dest_pixmap_priv->base.glamor_priv;
	fixed_block_width = glamor_priv->max_fbo_size;
	fixed_block_height = glamor_priv->max_fbo_size;
	/* If we got an totally out-of-box region for a source or mask
	 * region without repeat, we need to set it as null_source and
	 * give it a solid color (0,0,0,0). */
	null_source = 0;
	null_mask = 0;
	RegionTranslate(region, -dest->pDrawable->x,
			-dest->pDrawable->y);

	/* need to transform the dest region to the correct sourcei/mask region.
	 * it's a little complex, as one single edge of the
	 * target region may be transformed to cross a block boundary of the
	 * source or mask. Then it's impossible to handle it as usual way.
	 * We may have to split the original dest region to smaller region, and
	 * make sure each region's transformed region can fit into one texture,
	 * and then continue this loop again, and each time when a transformed region
	 * cross the bound, we need to copy it to a single pixmap and do the composition
	 * with the new pixmap. If the transformed region doesn't cross a source/mask's
	 * boundary then we don't need to copy.
	 *
	 */
	if (source_pixmap_priv
	    && source->transform
	    && source_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
		int source_transformed_block_width, source_transformed_block_height;
		if (!glamor_get_transform_block_size(source->transform,
					  source_pixmap_priv->large.block_w,
					  source_pixmap_priv->large.block_h,
					  &source_transformed_block_width,
					  &source_transformed_block_height)) {
			DEBUGF("source block size less than 1, fallback.\n");
			RegionTranslate(region, dest->pDrawable->x,
					dest->pDrawable->y);
			return FALSE;
		}
		fixed_block_width = min(fixed_block_width , source_transformed_block_width);
		fixed_block_height = min(fixed_block_height , source_transformed_block_height);
		DEBUGF("new source block size %d x %d \n", fixed_block_width, fixed_block_height);
	}

	if (mask_pixmap_priv
	    && mask->transform
	    && mask_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
		int mask_transformed_block_width, mask_transformed_block_height;
		if (!glamor_get_transform_block_size(mask->transform,
					  mask_pixmap_priv->large.block_w,
					  mask_pixmap_priv->large.block_h,
					  &mask_transformed_block_width,
					  &mask_transformed_block_height)) {
			DEBUGF("mask block size less than 1, fallback.\n");
			RegionTranslate(region, dest->pDrawable->x,
					dest->pDrawable->y);
			return FALSE;
		}
		fixed_block_width = min(fixed_block_width , mask_transformed_block_width);
		fixed_block_height = min(fixed_block_height , mask_transformed_block_height);
		DEBUGF("new mask block size %d x %d \n", fixed_block_width, fixed_block_height);
	}

	/*compute the correct block width and height whose transformed source/mask
	 *region can fit into one texture.*/
	if (force_clip || fixed_block_width < glamor_priv->max_fbo_size
	    || fixed_block_height < glamor_priv->max_fbo_size)
		clipped_dest_regions = glamor_compute_clipped_regions_ext(dest_pixmap_priv,
									  region,
									  &n_dest_regions,
									  fixed_block_width,
									  fixed_block_height,
									  0, 0);
	else
		clipped_dest_regions = glamor_compute_clipped_regions(dest_pixmap_priv,
								      region,
								      &n_dest_regions,
								      0, 0, 0);
	DEBUGF("dest clipped result %d region: \n", n_dest_regions);
	if (source_pixmap_priv
	    && (source_pixmap_priv == dest_pixmap_priv || source_pixmap_priv == mask_pixmap_priv)
		&& source_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
		/* XXX self-copy...*/
		need_free_source_pixmap_priv = source_pixmap_priv;
		source_pixmap_priv = malloc(sizeof(*source_pixmap_priv));
		*source_pixmap_priv = *need_free_source_pixmap_priv;
		need_free_source_pixmap_priv = source_pixmap_priv;
	}
	assert(mask_pixmap_priv != dest_pixmap_priv);

	for(i = 0; i < n_dest_regions; i++)
	{
		DEBUGF("dest region %d  idx %d\n", i, clipped_dest_regions[i].block_idx);
		DEBUGRegionPrint(clipped_dest_regions[i].region);
		SET_PIXMAP_FBO_CURRENT(dest_pixmap_priv, clipped_dest_regions[i].block_idx);
		if ( source_pixmap_priv && source_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
			if (!source->transform && source_repeat_type != RepeatPad) {
				RegionTranslate(clipped_dest_regions[i].region,
						x_source - x_dest,
						y_source - y_dest);
				clipped_source_regions = glamor_compute_clipped_regions(source_pixmap_priv,
										        clipped_dest_regions[i].region,
										        &n_source_regions, source_repeat_type,
											0, 0);
				is_normal_source_fbo = 1;
			}
			else {
				clipped_source_regions = glamor_compute_transform_clipped_regions(source_pixmap_priv,
									source->transform,
									clipped_dest_regions[i].region,
									&n_source_regions,
									x_source - x_dest, y_source - y_dest,
									source_repeat_type, 0, 0);
				is_normal_source_fbo = 0;
				if (n_source_regions == 0) {
					/* Pad the out-of-box region to (0,0,0,0). */
					null_source = 1;
					n_source_regions = 1;
				} else
					_glamor_process_transformed_clipped_region(source_pixmap_priv,
						source_repeat_type, clipped_source_regions, &n_source_regions,
						&need_clean_source_fbo);
			}
			DEBUGF("source clipped result %d region: \n", n_source_regions);
			for(j = 0; j < n_source_regions; j++)
			{
				if (is_normal_source_fbo)
					SET_PIXMAP_FBO_CURRENT(source_pixmap_priv,
							       clipped_source_regions[j].block_idx);

				if (mask_pixmap_priv && mask_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
					if (is_normal_mask_fbo && is_normal_source_fbo) {
						/* both mask and source are normal fbo box without transform or repeatpad.
						 * The region is clipped against source and then we clip it against mask here.*/
						DEBUGF("source region %d  idx %d\n", j, clipped_source_regions[j].block_idx);
						DEBUGRegionPrint(clipped_source_regions[j].region);
						RegionTranslate(clipped_source_regions[j].region,
								- x_source + x_mask,
							- y_source + y_mask);
						clipped_mask_regions = glamor_compute_clipped_regions(mask_pixmap_priv,
											     clipped_source_regions[j].region,
											     &n_mask_regions, mask_repeat_type,
											     0, 0);
						is_normal_mask_fbo = 1;
					} else if (is_normal_mask_fbo && !is_normal_source_fbo) {
						assert(n_source_regions == 1);
						/* The source fbo is not a normal fbo box, it has transform or repeatpad.
						 * the valid clip region should be the clip dest region rather than the
						 * clip source region.*/
						RegionTranslate(clipped_dest_regions[i].region,
								- x_dest + x_mask,
								- y_dest + y_mask);
						clipped_mask_regions = glamor_compute_clipped_regions(mask_pixmap_priv,
											     clipped_dest_regions[i].region,
											     &n_mask_regions, mask_repeat_type,
											     0, 0);
						is_normal_mask_fbo = 1;
					} else {
						/* This mask region has transform or repeatpad, we need clip it agains the previous
						 * valid region rather than the mask region. */
						if (!is_normal_source_fbo)
							clipped_mask_regions = glamor_compute_transform_clipped_regions(mask_pixmap_priv,
												mask->transform,
												clipped_dest_regions[i].region,
												&n_mask_regions,
												x_mask - x_dest,
												y_mask - y_dest,
												mask_repeat_type, 0, 0);
						else
							clipped_mask_regions = glamor_compute_transform_clipped_regions(mask_pixmap_priv,
												mask->transform,
												clipped_source_regions[j].region,
												&n_mask_regions,
												x_mask - x_source, y_mask - y_source,
												mask_repeat_type, 0, 0);
						is_normal_mask_fbo = 0;
						if (n_mask_regions == 0) {
						/* Pad the out-of-box region to (0,0,0,0). */
							null_mask = 1;
							n_mask_regions = 1;
						} else
							_glamor_process_transformed_clipped_region(mask_pixmap_priv,
								mask_repeat_type, clipped_mask_regions, &n_mask_regions,
								&need_clean_mask_fbo);
					}
					DEBUGF("mask clipped result %d region: \n", n_mask_regions);

#define COMPOSITE_REGION(region) do {				\
	if (!glamor_composite_clipped_region(op,		\
			 null_source ? NULL : source,		\
			 null_mask ? NULL : mask, dest,		\
			 null_source ? NULL : source_pixmap_priv, \
			 null_mask ? NULL : mask_pixmap_priv, 	\
			 dest_pixmap_priv, region,		\
			 x_source, y_source, x_mask, y_mask,	\
			 x_dest, y_dest)) {			\
		assert(0);					\
	}							\
   } while(0)

					for(k = 0; k < n_mask_regions; k++)
					{
						DEBUGF("mask region %d  idx %d\n", k, clipped_mask_regions[k].block_idx);
						DEBUGRegionPrint(clipped_mask_regions[k].region);
						if (is_normal_mask_fbo) {
							SET_PIXMAP_FBO_CURRENT(mask_pixmap_priv,
								       clipped_mask_regions[k].block_idx);
							DEBUGF("mask fbo off %d %d \n",
								mask_pixmap_priv->large.box.x1,
								mask_pixmap_priv->large.box.y1);
							DEBUGF("start composite mask hasn't transform.\n");
							RegionTranslate(clipped_mask_regions[k].region,
									x_dest - x_mask + dest->pDrawable->x,
									y_dest - y_mask + dest->pDrawable->y);
							COMPOSITE_REGION(clipped_mask_regions[k].region);
						} else if (!is_normal_mask_fbo && !is_normal_source_fbo) {
							DEBUGF("start composite both mask and source have transform.\n");
							RegionTranslate(clipped_dest_regions[i].region,
									dest->pDrawable->x,
									dest->pDrawable->y);
							COMPOSITE_REGION(clipped_dest_regions[i].region);
						} else {
							DEBUGF("start composite only mask has transform.\n");
							RegionTranslate(clipped_source_regions[j].region,
									x_dest - x_source + dest->pDrawable->x,
									y_dest - y_source + dest->pDrawable->y);
							COMPOSITE_REGION(clipped_source_regions[j].region);
						}
						RegionDestroy(clipped_mask_regions[k].region);
					}
					free(clipped_mask_regions);
					if (null_mask) null_mask = 0;
					if (need_clean_mask_fbo) {
						assert(is_normal_mask_fbo == 0);
						glamor_destroy_fbo(mask_pixmap_priv->base.fbo);
						mask_pixmap_priv->base.fbo = NULL;
						need_clean_mask_fbo = 0;
					}
				} else {
					if (is_normal_source_fbo) {
						RegionTranslate(clipped_source_regions[j].region,
								-x_source + x_dest + dest->pDrawable->x,
								-y_source + y_dest + dest->pDrawable->y);
						COMPOSITE_REGION(clipped_source_regions[j].region);
					} else {
						/* Source has transform or repeatPad. dest regions is the right
						 * region to do the composite. */
						RegionTranslate(clipped_dest_regions[i].region,
								dest->pDrawable->x,
								dest->pDrawable->y);
						COMPOSITE_REGION(clipped_dest_regions[i].region);
					}
				}
				if (clipped_source_regions && clipped_source_regions[j].region)
					RegionDestroy(clipped_source_regions[j].region);
			}
			free(clipped_source_regions);
			if (null_source) null_source = 0;
			if (need_clean_source_fbo) {
				assert(is_normal_source_fbo == 0);
				glamor_destroy_fbo(source_pixmap_priv->base.fbo);
				source_pixmap_priv->base.fbo = NULL;
				need_clean_source_fbo = 0;
			}
		}
		else {
			if (mask_pixmap_priv && mask_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
				if (!mask->transform && mask_repeat_type != RepeatPad) {
					RegionTranslate(clipped_dest_regions[i].region,
							x_mask - x_dest,
							y_mask - y_dest);
					clipped_mask_regions = glamor_compute_clipped_regions(mask_pixmap_priv,
								        clipped_dest_regions[i].region,
								        &n_mask_regions, mask_repeat_type, 0, 0);
					is_normal_mask_fbo = 1;
				}
				else {
					clipped_mask_regions = glamor_compute_transform_clipped_regions(mask_pixmap_priv,
										mask->transform,
										clipped_dest_regions[i].region,
										&n_mask_regions,
										x_mask - x_dest, y_mask - y_dest,
										mask_repeat_type, 0, 0);
					is_normal_mask_fbo = 0;
					if (n_mask_regions == 0) {
						/* Pad the out-of-box region to (0,0,0,0). */
						null_mask = 1;
						n_mask_regions = 1;
					} else
						_glamor_process_transformed_clipped_region(mask_pixmap_priv,
							mask_repeat_type, clipped_mask_regions, &n_mask_regions,
							&need_clean_mask_fbo);
				}

				for(k = 0; k < n_mask_regions; k++)
				{
					DEBUGF("mask region %d  idx %d\n", k, clipped_mask_regions[k].block_idx);
					DEBUGRegionPrint(clipped_mask_regions[k].region);
					if (is_normal_mask_fbo) {
						SET_PIXMAP_FBO_CURRENT(mask_pixmap_priv,
								       clipped_mask_regions[k].block_idx);
						RegionTranslate(clipped_mask_regions[k].region,
								x_dest - x_mask + dest->pDrawable->x,
								y_dest - y_mask + dest->pDrawable->y);
						COMPOSITE_REGION(clipped_mask_regions[k].region);
					} else {
						RegionTranslate(clipped_dest_regions[i].region,
								dest->pDrawable->x,
								dest->pDrawable->y);
						COMPOSITE_REGION(clipped_dest_regions[i].region);
					}
					RegionDestroy(clipped_mask_regions[k].region);
				}
				free(clipped_mask_regions);
				if (null_mask) null_mask = 0;
				if (need_clean_mask_fbo) {
					glamor_destroy_fbo(mask_pixmap_priv->base.fbo);
					mask_pixmap_priv->base.fbo = NULL;
					need_clean_mask_fbo = 0;
				}
			}
			else {
				RegionTranslate(clipped_dest_regions[i].region,
						dest->pDrawable->x,
						dest->pDrawable->y);
				COMPOSITE_REGION(clipped_dest_regions[i].region);
			}
		}
		RegionDestroy(clipped_dest_regions[i].region);
	}
	free(clipped_dest_regions);
	free(need_free_source_pixmap_priv);
	free(need_free_mask_pixmap_priv);
	ok = TRUE;
	return ok;
}