summaryrefslogtreecommitdiff
path: root/src/cairo-glitz-surface.c
blob: 018b064f07e66ee1cfc3677f09d3ee15542dfac6 (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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2004 David Reveman
 *
 * 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 David
 * Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. David Reveman makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL DAVID REVEMAN 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.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#include "cairoint.h"
#include "cairo-glitz.h"

typedef struct _cairo_glitz_surface {
    cairo_surface_t   base;

    glitz_surface_t   *surface;
    glitz_format_t    *format;
    pixman_region16_t *clip;
} cairo_glitz_surface_t;

static cairo_status_t
_cairo_glitz_surface_finish (void *abstract_surface)
{
    cairo_glitz_surface_t *surface = abstract_surface;

    if (surface->clip)
    {
	glitz_surface_set_clip_region (surface->surface, 0, 0, NULL, 0);
	pixman_region_destroy (surface->clip);
    }
    
    glitz_surface_destroy (surface->surface);

    return CAIRO_STATUS_SUCCESS;
}

static glitz_format_name_t
_glitz_format_from_content (cairo_content_t content)
{
    switch (content) {
    case CAIRO_CONTENT_COLOR:
	return GLITZ_STANDARD_RGB24;
    case CAIRO_CONTENT_ALPHA:
	return GLITZ_STANDARD_A8;
    case CAIRO_CONTENT_COLOR_ALPHA:
	return GLITZ_STANDARD_ARGB32;
    }

    ASSERT_NOT_REACHED;
    return GLITZ_STANDARD_ARGB32;
}

static cairo_surface_t *
_cairo_glitz_surface_create_similar (void	    *abstract_src,
				     cairo_content_t content,
				     int	     width,
				     int	     height)
{
    cairo_glitz_surface_t *src = abstract_src;
    cairo_surface_t	  *crsurface;
    glitz_drawable_t	  *drawable;
    glitz_surface_t	  *surface;
    glitz_format_t	  *gformat;

    drawable = glitz_surface_get_drawable (src->surface);
    
    gformat = glitz_find_standard_format (drawable,
					  _glitz_format_from_content (content));
    if (!gformat) {
	_cairo_error (CAIRO_STATUS_NO_MEMORY);
	return (cairo_surface_t*) &_cairo_surface_nil;
    }
    
    surface = glitz_surface_create (drawable, gformat, width, height, 0, NULL);
    if (surface == NULL) {
	_cairo_error (CAIRO_STATUS_NO_MEMORY);
	return (cairo_surface_t*) &_cairo_surface_nil;
    }

    crsurface = cairo_glitz_surface_create (surface);
    
    glitz_surface_destroy (surface);

    return crsurface;
}

static cairo_status_t
_cairo_glitz_surface_get_image (cairo_glitz_surface_t *surface,
				cairo_rectangle_t     *interest,
				cairo_image_surface_t **image_out,
				cairo_rectangle_t     *rect_out)
{
    cairo_image_surface_t *image;
    int			  x1, y1, x2, y2;
    int			  width, height;
    unsigned char	  *pixels;
    cairo_format_masks_t  format;
    glitz_buffer_t	  *buffer;
    glitz_pixel_format_t  pf;

    x1 = 0;
    y1 = 0;
    x2 = glitz_surface_get_width (surface->surface);
    y2 = glitz_surface_get_height (surface->surface);

    if (interest)
    {
	if (interest->x > x1)
	    x1 = interest->x;
	if (interest->y > y1)
	    y1 = interest->y;
	if (interest->x + interest->width < x2)
	    x2 = interest->x + interest->width;
	if (interest->y + interest->height < y2)
	    y2 = interest->y + interest->height;

	if (x1 >= x2 || y1 >= y2)
	{
	    *image_out = NULL;
	    return CAIRO_STATUS_SUCCESS;
	}
    }

    width  = x2 - x1;
    height = y2 - y1;

    if (rect_out)
    {
	rect_out->x = x1;
	rect_out->y = y1;
	rect_out->width = width;
	rect_out->height = height;
    }
    
    if (surface->format->type == GLITZ_FORMAT_TYPE_COLOR) {
	if (surface->format->color.red_size > 0) {
	    format.bpp = 32;
	    
	    if (surface->format->color.alpha_size > 0)
		format.alpha_mask = 0xff000000;
	    else
		format.alpha_mask = 0x0;
	    
	    format.red_mask = 0xff0000;
	    format.green_mask = 0xff00;
	    format.blue_mask = 0xff;
	} else {
	    format.bpp = 8;
	    format.blue_mask = format.green_mask = format.red_mask = 0x0;
	    format.alpha_mask = 0xff;
	}
    } else {
	format.bpp = 32;
	format.alpha_mask = 0xff000000;
	format.red_mask = 0xff0000;
	format.green_mask = 0xff00;
	format.blue_mask = 0xff;
    }

    pf.masks.bpp = format.bpp;
    pf.masks.alpha_mask = format.alpha_mask;
    pf.masks.red_mask = format.red_mask;
    pf.masks.green_mask = format.green_mask;
    pf.masks.blue_mask = format.blue_mask;
    pf.xoffset = 0;
    pf.skip_lines = 0;

    /* XXX: we should eventually return images with negative stride,
       need to verify that libpixman have no problem with this first. */
    pf.bytes_per_line = (((width * format.bpp) / 8) + 3) & -4;
    pf.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN;

    pixels = malloc (height * pf.bytes_per_line);
    if (!pixels)
	return CAIRO_STATUS_NO_MEMORY;

    buffer = glitz_buffer_create_for_data (pixels);
    if (!buffer) {
	free (pixels);
	return CAIRO_STATUS_NO_MEMORY;
    }
    
    glitz_get_pixels (surface->surface,
		      x1, y1,
		      width, height,
		      &pf,
		      buffer);

    glitz_buffer_destroy (buffer);
    
    image = (cairo_image_surface_t *)
        _cairo_image_surface_create_with_masks (pixels,
						&format,
						width, height,
						pf.bytes_per_line);
    if (image->base.status)
    {
	free (pixels);
	return CAIRO_STATUS_NO_MEMORY;
    }

    _cairo_image_surface_assume_ownership_of_data (image);

    *image_out = image;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_glitz_surface_set_image (void		      *abstract_surface,
				cairo_image_surface_t *image,
				int		      x_dst,
				int		      y_dst)
{
    cairo_glitz_surface_t *surface = abstract_surface;
    glitz_buffer_t	  *buffer;
    glitz_pixel_format_t  pf;
    pixman_format_t	  *format;
    int			  am, rm, gm, bm;
    char		  *data;
    
    format = pixman_image_get_format (image->pixman_image);
    if (!format)
	return CAIRO_STATUS_NO_MEMORY;

    pixman_format_get_masks (format, &pf.masks.bpp, &am, &rm, &gm, &bm);

    pf.masks.alpha_mask = am;
    pf.masks.red_mask = rm;
    pf.masks.green_mask = gm;
    pf.masks.blue_mask = bm;
    pf.xoffset = 0;
    pf.skip_lines = 0;

    /* check for negative stride */
    if (image->stride < 0)
    {
	pf.bytes_per_line = -image->stride;
	pf.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_BOTTOM_UP;
	data = (char *) image->data + image->stride * (image->height - 1);
    }
    else
    {
	pf.bytes_per_line = image->stride;
	pf.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN;
	data = (char *) image->data;
    }

    buffer = glitz_buffer_create_for_data (data);
    if (!buffer)
	return CAIRO_STATUS_NO_MEMORY;
    
    glitz_set_pixels (surface->surface,
		      x_dst, y_dst,
		      image->width, image->height,
		      &pf,
		      buffer);
    
    glitz_buffer_destroy (buffer);
    
    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_glitz_surface_acquire_source_image (void              *abstract_surface,
					   cairo_image_surface_t **image_out,
					   void                  **image_extra)
{
    cairo_glitz_surface_t *surface = abstract_surface;

    *image_extra = NULL;
    
    return _cairo_glitz_surface_get_image (surface, NULL, image_out, NULL);
}

static void
_cairo_glitz_surface_release_source_image (void              *abstract_surface,
					   cairo_image_surface_t *image,
					   void                  *image_extra)
{
    cairo_surface_destroy (&image->base);
}

static cairo_status_t
_cairo_glitz_surface_acquire_dest_image (void                *abstract_surface,
					 cairo_rectangle_t     *interest_rect,
					 cairo_image_surface_t **image_out,
					 cairo_rectangle_t     *image_rect_out,
					 void                  **image_extra)
{
    cairo_glitz_surface_t *surface = abstract_surface;
    cairo_image_surface_t *image;
    cairo_status_t	  status;

    status = _cairo_glitz_surface_get_image (surface, interest_rect, &image,
					     image_rect_out);
    if (status)
	return status;

    *image_out   = image;
    *image_extra = NULL;

    return status;
}

static void
_cairo_glitz_surface_release_dest_image (void                *abstract_surface,
					 cairo_rectangle_t     *interest_rect,
					 cairo_image_surface_t *image,
					 cairo_rectangle_t     *image_rect,
					 void                  *image_extra)
{
    cairo_glitz_surface_t *surface = abstract_surface;

    _cairo_glitz_surface_set_image (surface, image,
				    image_rect->x, image_rect->y);

    cairo_surface_destroy (&image->base);
}


static cairo_status_t
_cairo_glitz_surface_clone_similar (void	    *abstract_surface,
				    cairo_surface_t *src,
				    cairo_surface_t **clone_out)
{
    cairo_glitz_surface_t *surface = abstract_surface;
    cairo_glitz_surface_t *clone;

    if (surface->base.status)
	return surface->base.status;

    if (src->backend == surface->base.backend)
    {
	*clone_out = cairo_surface_reference (src);	
	
	return CAIRO_STATUS_SUCCESS;
    }
    else if (_cairo_surface_is_image (src))
    {
	cairo_image_surface_t *image_src = (cairo_image_surface_t *) src;
	cairo_content_t content = _cairo_content_from_format (image_src->format);
    
	clone = (cairo_glitz_surface_t *)
	    _cairo_glitz_surface_create_similar (surface, content,
						 image_src->width,
						 image_src->height);
	if (clone->base.status)
	    return CAIRO_STATUS_NO_MEMORY;

	_cairo_glitz_surface_set_image (clone, image_src, 0, 0);
	
	*clone_out = &clone->base;

	return CAIRO_STATUS_SUCCESS;
    }
    
    return CAIRO_INT_STATUS_UNSUPPORTED;
}

static void
_cairo_glitz_surface_set_matrix (cairo_glitz_surface_t *surface,
				 cairo_matrix_t	       *matrix)
{
    glitz_transform_t transform;

    transform.matrix[0][0] = _cairo_fixed_from_double (matrix->xx);
    transform.matrix[0][1] = _cairo_fixed_from_double (matrix->xy);
    transform.matrix[0][2] = _cairo_fixed_from_double (matrix->x0);

    transform.matrix[1][0] = _cairo_fixed_from_double (matrix->yx);
    transform.matrix[1][1] = _cairo_fixed_from_double (matrix->yy);
    transform.matrix[1][2] = _cairo_fixed_from_double (matrix->y0);

    transform.matrix[2][0] = 0;
    transform.matrix[2][1] = 0;
    transform.matrix[2][2] = _cairo_fixed_from_double (1);

    glitz_surface_set_transform (surface->surface, &transform);
}

static glitz_operator_t
_glitz_operator (cairo_operator_t op)
{
    switch (op) {
    case CAIRO_OPERATOR_CLEAR:
	return GLITZ_OPERATOR_CLEAR;

    case CAIRO_OPERATOR_SOURCE:
	return GLITZ_OPERATOR_SRC;
    case CAIRO_OPERATOR_OVER:
	return GLITZ_OPERATOR_OVER;
    case CAIRO_OPERATOR_IN:
	return GLITZ_OPERATOR_IN;
    case CAIRO_OPERATOR_OUT:
	return GLITZ_OPERATOR_OUT;
    case CAIRO_OPERATOR_ATOP:
	return GLITZ_OPERATOR_ATOP;

    case CAIRO_OPERATOR_DEST:
	return GLITZ_OPERATOR_DST;
    case CAIRO_OPERATOR_DEST_OVER:
	return GLITZ_OPERATOR_OVER_REVERSE;
    case CAIRO_OPERATOR_DEST_IN:
	return GLITZ_OPERATOR_IN_REVERSE;
    case CAIRO_OPERATOR_DEST_OUT:
	return GLITZ_OPERATOR_OUT_REVERSE;
    case CAIRO_OPERATOR_DEST_ATOP:
	return GLITZ_OPERATOR_ATOP_REVERSE;

    case CAIRO_OPERATOR_XOR:
	return GLITZ_OPERATOR_XOR;
    case CAIRO_OPERATOR_ADD:
	return GLITZ_OPERATOR_ADD;
    case CAIRO_OPERATOR_SATURATE:
	/* XXX: OVER is definitely not the right thing here, (but it
	 * is what the original glitz backend code has always
	 * done). Cairo's SATURATE operator is the native GL
	 * compositing mode, (from my understanding). So why isn't
	 * there a GLITZ_OPERATOR_SATURATE for us to use here? */
	return GLITZ_OPERATOR_OVER;
    }

    ASSERT_NOT_REACHED;

    /* Something's very broken if this line of code can be reached, so
       we want to return something that would give a noticeably
       incorrect result. The XOR operator seems so rearely desired
       that it should fit the bill here. */
    return CAIRO_OPERATOR_XOR;
}

#define CAIRO_GLITZ_FEATURE_OK(surface, name)				  \
    (glitz_drawable_get_features (glitz_surface_get_drawable (surface)) & \
     (GLITZ_FEATURE_ ## name ## _MASK))

static glitz_status_t
_glitz_ensure_target (glitz_surface_t *surface)
{
    if (glitz_surface_get_attached_drawable (surface) ||
	CAIRO_GLITZ_FEATURE_OK (surface, FRAMEBUFFER_OBJECT))
	return CAIRO_STATUS_SUCCESS;

    return CAIRO_INT_STATUS_UNSUPPORTED;
}

typedef struct _cairo_glitz_surface_attributes {
    cairo_surface_attributes_t	base;
    
    glitz_fill_t		fill;
    glitz_filter_t		filter;
    glitz_fixed16_16_t		*params;
    int				n_params;
    cairo_bool_t		acquired;
} cairo_glitz_surface_attributes_t;

static cairo_int_status_t
_cairo_glitz_pattern_acquire_surface (cairo_pattern_t	              *pattern,
				      cairo_glitz_surface_t	       *dst,
				      int			       x,
				      int			       y,
				      unsigned int		       width,
				      unsigned int		       height,
				      cairo_glitz_surface_t	 **surface_out,
				      cairo_glitz_surface_attributes_t *attr)
{
    cairo_glitz_surface_t *src = NULL;

    attr->acquired = FALSE;

    switch (pattern->type) {
    case CAIRO_PATTERN_LINEAR:
    case CAIRO_PATTERN_RADIAL: {
	cairo_gradient_pattern_t    *gradient =
	    (cairo_gradient_pattern_t *) pattern;
	char			    *data;
	glitz_fixed16_16_t	    *params;
	int			    n_params;
	unsigned int		    *pixels;
	int			    i;
	unsigned char		    alpha;
	glitz_buffer_t		    *buffer;
	static glitz_pixel_format_t format = {
            {
                32,
                0xff000000,
                0x00ff0000,
                0x0000ff00,
                0x000000ff
            },
            0, 0, 0,
            GLITZ_PIXEL_SCANLINE_ORDER_BOTTOM_UP
        };
	
	/* XXX: the current color gradient acceleration provided by glitz is
	 * experimental, it's been proven inappropriate in a number of ways,
	 * most importantly, it's currently implemented as filters and
	 * gradients are not filters. eventually, it will be replaced with
	 * something more appropriate.
	 */

	if (gradient->n_stops < 2)
	    break;

	/* glitz doesn't support inner and outer circle with different
	   center points. */
	if (pattern->type == CAIRO_PATTERN_RADIAL)
	{
	    cairo_radial_pattern_t *grad = (cairo_radial_pattern_t *) pattern;
	    
	    if (grad->center0.x != grad->center1.x ||
		grad->center0.y != grad->center1.y)
		break;
	}

	if (!CAIRO_GLITZ_FEATURE_OK (dst->surface, FRAGMENT_PROGRAM))
	    break;
	
	if (pattern->filter != CAIRO_FILTER_BILINEAR &&
	    pattern->filter != CAIRO_FILTER_GOOD &&
	    pattern->filter != CAIRO_FILTER_BEST)
	    break;

	alpha = gradient->stops[0].color.alpha * 0xff;
	for (i = 1; i < gradient->n_stops; i++)
	{
	    unsigned char a;
	    
	    a = gradient->stops[i].color.alpha * 0xff;
	    if (a != alpha)
		break;
	}

	/* we can't have color stops with different alpha as gradient color
	   interpolation should be done to unpremultiplied colors. */
	if (i < gradient->n_stops)
	    break;

	n_params = gradient->n_stops * 3 + 4;

	data = malloc (sizeof (glitz_fixed16_16_t) * n_params +
		       sizeof (unsigned int) * gradient->n_stops);
	if (!data)
	    return CAIRO_STATUS_NO_MEMORY;

	params = (glitz_fixed16_16_t *) data;
	pixels = (unsigned int *)
	    (data + sizeof (glitz_fixed16_16_t) * n_params);

	buffer = glitz_buffer_create_for_data (pixels);
	if (!buffer)
	{
	    free (data);
	    return CAIRO_STATUS_NO_MEMORY;
	}

	src = (cairo_glitz_surface_t *)
	    _cairo_surface_create_similar_scratch (&dst->base,
						   CAIRO_CONTENT_COLOR_ALPHA,
						   gradient->n_stops, 1);
	if (src->base.status)
	{
	    glitz_buffer_destroy (buffer);
	    free (data);
	    return CAIRO_STATUS_NO_MEMORY;
	}

	for (i = 0; i < gradient->n_stops; i++)
	{
	    pixels[i] =
                (((int) alpha) << 24)                                  |
                (((int) gradient->stops[i].color.red   * alpha) << 16) |
                (((int) gradient->stops[i].color.green * alpha) << 8)  |
                (((int) gradient->stops[i].color.blue  * alpha));
	    
	    params[4 + 3 * i] = gradient->stops[i].offset;
	    params[5 + 3 * i] = i << 16;
	    params[6 + 3 * i] = 0;
	}

	glitz_set_pixels (src->surface, 0, 0, gradient->n_stops, 1,
			  &format, buffer);

	glitz_buffer_destroy (buffer);

	if (pattern->type == CAIRO_PATTERN_LINEAR)
	{
	    cairo_linear_pattern_t *grad = (cairo_linear_pattern_t *) pattern;
	    
	    params[0] = _cairo_fixed_from_double (grad->point0.x);
	    params[1] = _cairo_fixed_from_double (grad->point0.y);
	    params[2] = _cairo_fixed_from_double (grad->point1.x);
	    params[3] = _cairo_fixed_from_double (grad->point1.y);
	    attr->filter = GLITZ_FILTER_LINEAR_GRADIENT;
	}
	else
	{
	    cairo_radial_pattern_t *grad = (cairo_radial_pattern_t *) pattern;
	    
	    params[0] = _cairo_fixed_from_double (grad->center0.x);
	    params[1] = _cairo_fixed_from_double (grad->center0.y);
	    params[2] = _cairo_fixed_from_double (grad->radius0);
	    params[3] = _cairo_fixed_from_double (grad->radius1);
	    attr->filter = GLITZ_FILTER_RADIAL_GRADIENT;
	}

	switch (pattern->extend) {
	case CAIRO_EXTEND_NONE:
	    attr->fill = GLITZ_FILL_NEAREST;
	    break;
	case CAIRO_EXTEND_REPEAT:
	    attr->fill = GLITZ_FILL_REPEAT;
	    break;
	case CAIRO_EXTEND_REFLECT:
	    attr->fill = GLITZ_FILL_REFLECT;
	    break;
	}

	attr->params	    = params;
	attr->n_params	    = n_params;
	attr->base.matrix   = pattern->matrix;
	attr->base.x_offset = 0;
	attr->base.y_offset = 0;
    } break;
    default:
	break;
    }

    if (!src)
    {
	cairo_int_status_t status;

	status = _cairo_pattern_acquire_surface (pattern, &dst->base,
						 x, y, width, height,
						 (cairo_surface_t **) &src,
						 &attr->base);
	if (status)
	    return status;
	
	if (src)
	{
	    switch (attr->base.extend) {
	    case CAIRO_EXTEND_NONE:
		attr->fill = GLITZ_FILL_TRANSPARENT;
		break;
	    case CAIRO_EXTEND_REPEAT:
		attr->fill = GLITZ_FILL_REPEAT;
		break;
	    case CAIRO_EXTEND_REFLECT:
		attr->fill = GLITZ_FILL_REFLECT;
		break;
	    }

	    switch (attr->base.filter) {
	    case CAIRO_FILTER_FAST:
	    case CAIRO_FILTER_NEAREST:
		attr->filter = GLITZ_FILTER_NEAREST;
		break;
	    case CAIRO_FILTER_GOOD:
	    case CAIRO_FILTER_BEST:
	    case CAIRO_FILTER_BILINEAR:
	    default:
		attr->filter = GLITZ_FILTER_BILINEAR;
		break;
	    }
	    
	    attr->params   = NULL;
	    attr->n_params = 0;
	    attr->acquired = TRUE;
	}
    }

    *surface_out = src;
    
    return CAIRO_STATUS_SUCCESS;
}

static void
_cairo_glitz_pattern_release_surface (cairo_pattern_t		      *pattern,
				      cairo_glitz_surface_t	      *surface,
				      cairo_glitz_surface_attributes_t *attr)
{
    if (attr->acquired)
	_cairo_pattern_release_surface (pattern, &surface->base, &attr->base);
    else
	cairo_surface_destroy (&surface->base);
}

static cairo_int_status_t
_cairo_glitz_pattern_acquire_surfaces (cairo_pattern_t	                *src,
				       cairo_pattern_t	                *mask,
				       cairo_glitz_surface_t	        *dst,
				       int			        src_x,
				       int			        src_y,
				       int			        mask_x,
				       int			        mask_y,
				       unsigned int		        width,
				       unsigned int		        height,
				       cairo_glitz_surface_t	    **src_out,
				       cairo_glitz_surface_t	    **mask_out,
				       cairo_glitz_surface_attributes_t *sattr,
				       cairo_glitz_surface_attributes_t *mattr)
{
    cairo_int_status_t	  status;
    cairo_pattern_union_t tmp;

    /* If src and mask are both solid, then the mask alpha can be
     * combined into src and mask can be ignored. */

    /* XXX: This optimization assumes that there is no color
     * information in mask, so this will need to change when we
     * support RENDER-style 4-channel masks. */

    if (src->type == CAIRO_PATTERN_SOLID &&
	mask->type == CAIRO_PATTERN_SOLID)
    {
	cairo_color_t combined;
	cairo_solid_pattern_t *src_solid = (cairo_solid_pattern_t *) src;
	cairo_solid_pattern_t *mask_solid = (cairo_solid_pattern_t *) mask;

	combined = src_solid->color;
	_cairo_color_multiply_alpha (&combined, mask_solid->color.alpha);

	_cairo_pattern_init_solid (&tmp.solid, &combined);

	mask = NULL;
    } else {
	_cairo_pattern_init_copy (&tmp.base, src);
    }
	
    status = _cairo_glitz_pattern_acquire_surface (&tmp.base, dst,
						   src_x, src_y,
						   width, height,
						   src_out, sattr);
    
    _cairo_pattern_fini (&tmp.base);

    if (status)
	return status;

    if (mask)
    {
	_cairo_pattern_init_copy (&tmp.base, mask);
	
	status = _cairo_glitz_pattern_acquire_surface (&tmp.base, dst,
						       mask_x, mask_y,
						       width, height,
						       mask_out, mattr);
    
	if (status)
	    _cairo_glitz_pattern_release_surface (&tmp.base, *src_out, sattr);

	_cairo_pattern_fini (&tmp.base);

	return status;
    }
    else
    {
	*mask_out = NULL;
    }

    return CAIRO_STATUS_SUCCESS;
}

static void
_cairo_glitz_surface_set_attributes (cairo_glitz_surface_t	      *surface,
				     cairo_glitz_surface_attributes_t *a)
{
    _cairo_glitz_surface_set_matrix (surface, &a->base.matrix);
    glitz_surface_set_fill (surface->surface, a->fill);
    glitz_surface_set_filter (surface->surface, a->filter,
			      a->params, a->n_params);
}

static cairo_int_status_t
_cairo_glitz_surface_composite (cairo_operator_t op,
				cairo_pattern_t  *src_pattern,
				cairo_pattern_t  *mask_pattern,
				void		 *abstract_dst,
				int		 src_x,
				int		 src_y,
				int		 mask_x,
				int		 mask_y,
				int		 dst_x,
				int		 dst_y,
				unsigned int	 width,
				unsigned int	 height)
{
    cairo_glitz_surface_attributes_t	src_attr, mask_attr;
    cairo_glitz_surface_t		*dst = abstract_dst;
    cairo_glitz_surface_t		*src;
    cairo_glitz_surface_t		*mask;
    cairo_int_status_t			status;

    if (op == CAIRO_OPERATOR_SATURATE)
	return CAIRO_INT_STATUS_UNSUPPORTED;

    if (_glitz_ensure_target (dst->surface))
	return CAIRO_INT_STATUS_UNSUPPORTED;

    status = _cairo_glitz_pattern_acquire_surfaces (src_pattern, mask_pattern,
						    dst,
						    src_x, src_y,
						    mask_x, mask_y,
						    width, height,
						    &src, &mask,
						    &src_attr, &mask_attr);
    if (status)
	return status;

    _cairo_glitz_surface_set_attributes (src, &src_attr);
    if (mask)
    {
	_cairo_glitz_surface_set_attributes (mask, &mask_attr);
	glitz_composite (_glitz_operator (op),
			 src->surface,
			 mask->surface,
			 dst->surface,
			 src_x + src_attr.base.x_offset,
			 src_y + src_attr.base.y_offset,
			 mask_x + mask_attr.base.x_offset,
			 mask_y + mask_attr.base.y_offset,
			 dst_x, dst_y,
			 width, height);
	
	if (mask_attr.n_params)
	    free (mask_attr.params);
	
	_cairo_glitz_pattern_release_surface (mask_pattern, mask, &mask_attr);
    }
    else
    {    
	glitz_composite (_glitz_operator (op),
			 src->surface,
			 NULL,
			 dst->surface,
			 src_x + src_attr.base.x_offset,
			 src_y + src_attr.base.y_offset,
			 0, 0,
			 dst_x, dst_y,
			 width, height);
    }

    if (src_attr.n_params)
	free (src_attr.params);

    _cairo_glitz_pattern_release_surface (src_pattern, src, &src_attr);

    if (glitz_surface_get_status (dst->surface) == GLITZ_STATUS_NOT_SUPPORTED)
	return CAIRO_INT_STATUS_UNSUPPORTED;
    
    return CAIRO_STATUS_SUCCESS;
}

static cairo_int_status_t
_cairo_glitz_surface_fill_rectangles (void		  *abstract_dst,
				      cairo_operator_t	  op,
				      const cairo_color_t *color,
				      cairo_rectangle_t	  *rects,
				      int		  n_rects)
{
    cairo_glitz_surface_t *dst = abstract_dst;

    if (op == CAIRO_OPERATOR_SOURCE)
    {
	glitz_color_t glitz_color;

	glitz_color.red = color->red_short;
	glitz_color.green = color->green_short;
	glitz_color.blue = color->blue_short;
	glitz_color.alpha = color->alpha_short;

	glitz_set_rectangles (dst->surface, &glitz_color,
			      (glitz_rectangle_t *) rects, n_rects);
    }
    else
    {
	cairo_glitz_surface_t *src;
	
	if (op == CAIRO_OPERATOR_SATURATE)
	    return CAIRO_INT_STATUS_UNSUPPORTED;

	if (_glitz_ensure_target (dst->surface))
	    return CAIRO_INT_STATUS_UNSUPPORTED;

	src = (cairo_glitz_surface_t *)
	    _cairo_surface_create_similar_solid (&dst->base,
						 CAIRO_CONTENT_COLOR_ALPHA,
						 1, 1,
						 (cairo_color_t *) color);
	if (src->base.status)
	    return CAIRO_STATUS_NO_MEMORY;

	glitz_surface_set_fill (src->surface, GLITZ_FILL_REPEAT);
	
	while (n_rects--)
	{
	    glitz_composite (_glitz_operator (op),
			     src->surface,
			     NULL,
			     dst->surface,
			     0, 0,
			     0, 0,
			     rects->x, rects->y,
			     rects->width, rects->height);
	    rects++;
	}
	
	cairo_surface_destroy (&src->base);
    }

    if (glitz_surface_get_status (dst->surface) == GLITZ_STATUS_NOT_SUPPORTED)
	return CAIRO_INT_STATUS_UNSUPPORTED;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_int_status_t
_cairo_glitz_surface_composite_trapezoids (cairo_operator_t  op,
					   cairo_pattern_t   *pattern,
					   void		     *abstract_dst,
					   cairo_antialias_t antialias,
					   int		     src_x,
					   int		     src_y,
					   int		     dst_x,
					   int		     dst_y,
					   unsigned int	     width,
					   unsigned int	     height,
					   cairo_trapezoid_t *traps,
					   int		     n_traps)
{
    cairo_pattern_union_t tmp_src_pattern;
    cairo_pattern_t *src_pattern;
    cairo_glitz_surface_attributes_t attributes;
    cairo_glitz_surface_t	     *dst = abstract_dst;
    cairo_glitz_surface_t	     *src;
    cairo_glitz_surface_t	     *mask = NULL;
    glitz_buffer_t		     *buffer = NULL;
    void			     *data = NULL;
    cairo_int_status_t		     status;
    unsigned short		     alpha;

    if (dst->base.status)
	return dst->base.status;

    if (op == CAIRO_OPERATOR_SATURATE)
	return CAIRO_INT_STATUS_UNSUPPORTED;

    if (_glitz_ensure_target (dst->surface))
	return CAIRO_INT_STATUS_UNSUPPORTED;

    if (pattern->type == CAIRO_PATTERN_SURFACE)
    {
	_cairo_pattern_init_copy (&tmp_src_pattern.base, pattern);
	
	status = _cairo_glitz_pattern_acquire_surface (&tmp_src_pattern.base, dst,
						       src_x, src_y,
						       width, height,
						       &src, &attributes);
	src_pattern = &tmp_src_pattern.base;
    }
    else
    {
	status = _cairo_glitz_pattern_acquire_surface (pattern, dst,
						       src_x, src_y,
						       width, height,
						       &src, &attributes);
	src_pattern = pattern;
    }
    alpha = 0xffff;

    if (status)
	return status;

    if (op == CAIRO_OPERATOR_ADD || n_traps <= 1)
    {
	static glitz_color_t	clear_black = { 0, 0, 0, 0 };
	glitz_color_t		color;
	glitz_geometry_format_t	format;
	int			n_trap_added;
	int			offset = 0;
	int			data_size = 0;
	int			size = 30 * n_traps; /* just a guess */

	format.vertex.primitive = GLITZ_PRIMITIVE_QUADS;
	format.vertex.type = GLITZ_DATA_TYPE_FLOAT;
	format.vertex.bytes_per_vertex = 3 * sizeof (glitz_float_t);
	format.vertex.attributes = GLITZ_VERTEX_ATTRIBUTE_MASK_COORD_MASK;
	format.vertex.mask.type = GLITZ_DATA_TYPE_FLOAT;
	format.vertex.mask.size = GLITZ_COORDINATE_SIZE_X;
	format.vertex.mask.offset = 2 * sizeof (glitz_float_t);

	mask = (cairo_glitz_surface_t *)
	    _cairo_glitz_surface_create_similar (&dst->base,
						 CAIRO_CONTENT_ALPHA,
						 2, 1);
	if (mask->base.status)
	{
	    _cairo_glitz_pattern_release_surface (src_pattern, src, &attributes);
	    if (src_pattern == &tmp_src_pattern.base)
		_cairo_pattern_fini (&tmp_src_pattern.base);

	    return CAIRO_STATUS_NO_MEMORY;
	}

	color.red = color.green = color.blue = color.alpha = 0xffff;

	glitz_set_rectangle (mask->surface, &clear_black, 0, 0, 1, 1);
	glitz_set_rectangle (mask->surface, &color, 1, 0, 1, 1);

	glitz_surface_set_fill (mask->surface, GLITZ_FILL_NEAREST);
	glitz_surface_set_filter (mask->surface,
				  GLITZ_FILTER_BILINEAR,
				  NULL, 0);
	
	size *= format.vertex.bytes_per_vertex;
	
	while (n_traps)
	{
	    if (data_size < size)
	    {
		data_size = size;
		data = realloc (data, data_size);
		if (!data)
		{
		    _cairo_glitz_pattern_release_surface (src_pattern, src,
							  &attributes);
		    if (src_pattern == &tmp_src_pattern.base)
			_cairo_pattern_fini (&tmp_src_pattern.base);
		    return CAIRO_STATUS_NO_MEMORY;
		}

		if (buffer)
		    glitz_buffer_destroy (buffer);
		
		buffer = glitz_buffer_create_for_data (data);
		if (!buffer) {
		    free (data);
		    _cairo_glitz_pattern_release_surface (src_pattern, src,
							  &attributes);
		    if (src_pattern == &tmp_src_pattern.base)
			_cairo_pattern_fini (&tmp_src_pattern.base);
		    return CAIRO_STATUS_NO_MEMORY;
		}
	    }
    
	    offset +=
		glitz_add_trapezoids (buffer,
				      offset, size - offset,
				      format.vertex.type, mask->surface,
				      (glitz_trapezoid_t *) traps, n_traps,
				      &n_trap_added);
		
	    n_traps -= n_trap_added;
	    traps   += n_trap_added;
	    size    *= 2;
	}
    
	glitz_set_geometry (dst->surface,
			    GLITZ_GEOMETRY_TYPE_VERTEX,
			    &format, buffer);
	glitz_set_array (dst->surface, 0, 3,
			 offset / format.vertex.bytes_per_vertex,
			 0, 0);
    }
    else
    {
	cairo_image_surface_t *image;
	unsigned char	      *ptr;
	int		      stride;

	stride = (width + 3) & -4;
	data = malloc (stride * height);
	if (!data)
	{
	    _cairo_glitz_pattern_release_surface (src_pattern, src, &attributes);
	    if (src_pattern == &tmp_src_pattern.base)
		_cairo_pattern_fini (&tmp_src_pattern.base);
	    return CAIRO_STATUS_NO_MEMORY;
	}

	memset (data, 0, stride * height);

	/* using negative stride */
	ptr = (unsigned char *) data + stride * (height - 1);
	
	image = (cairo_image_surface_t *)
	    cairo_image_surface_create_for_data (ptr,
						 CAIRO_FORMAT_A8,
						 width, height,
						 -stride);
	if (image->base.status)
	{
	    cairo_surface_destroy (&src->base);
	    free (data);
	    return CAIRO_STATUS_NO_MEMORY;
	}

	pixman_add_trapezoids (image->pixman_image, -dst_x, -dst_y,
			       (pixman_trapezoid_t *) traps, n_traps);

	mask = (cairo_glitz_surface_t *)
	    _cairo_surface_create_similar_scratch (&dst->base,
						   CAIRO_CONTENT_ALPHA,
						   width, height);
	if (mask->base.status)
	{
	    _cairo_glitz_pattern_release_surface (src_pattern, src, &attributes);
	    free (data);
	    cairo_surface_destroy (&image->base);
	    return CAIRO_STATUS_NO_MEMORY;
	}

	_cairo_glitz_surface_set_image (mask, image, 0, 0);
    }

    _cairo_glitz_surface_set_attributes (src, &attributes);
    
    glitz_composite (_glitz_operator (op),
		     src->surface,
		     mask->surface,
		     dst->surface,
		     src_x + attributes.base.x_offset,
		     src_y + attributes.base.y_offset,
		     0, 0,
		     dst_x, dst_y,
		     width, height);

    if (attributes.n_params)
	free (attributes.params);

    glitz_set_geometry (dst->surface,
			GLITZ_GEOMETRY_TYPE_NONE,
			NULL, NULL);

    if (buffer)
	glitz_buffer_destroy (buffer);
    
    free (data);

    _cairo_glitz_pattern_release_surface (src_pattern, src, &attributes);
    if (src_pattern == &tmp_src_pattern.base)
	_cairo_pattern_fini (&tmp_src_pattern.base);

    if (mask)
	cairo_surface_destroy (&mask->base);

    if (glitz_surface_get_status (dst->surface) == GLITZ_STATUS_NOT_SUPPORTED)
	return CAIRO_INT_STATUS_UNSUPPORTED;
    
    return CAIRO_STATUS_SUCCESS;
}

static cairo_int_status_t
_cairo_glitz_surface_set_clip_region (void		*abstract_surface,
				      pixman_region16_t *region)
{
    cairo_glitz_surface_t *surface = abstract_surface;

    if (region)
    {
	glitz_box_t *box;
	int	    n;
	
	if (!surface->clip)
	{
	    surface->clip = pixman_region_create ();
	    if (!surface->clip)
		return CAIRO_STATUS_NO_MEMORY;
	}
	pixman_region_copy (surface->clip, region);

	box = (glitz_box_t *) pixman_region_rects (surface->clip);
	n = pixman_region_num_rects (surface->clip);
	glitz_surface_set_clip_region (surface->surface, 0, 0, box, n);
    }
    else
    {
	glitz_surface_set_clip_region (surface->surface, 0, 0, NULL, 0);
	
	if (surface->clip)
	    pixman_region_destroy (surface->clip);

	surface->clip = NULL;
    }
    
    return CAIRO_STATUS_SUCCESS;
}

static cairo_int_status_t
_cairo_glitz_surface_get_extents (void		    *abstract_surface,
				  cairo_rectangle_t *rectangle)
{
    cairo_glitz_surface_t *surface = abstract_surface;

    rectangle->x = 0;
    rectangle->y = 0;
    rectangle->width = glitz_surface_get_width  (surface->surface);
    rectangle->height = glitz_surface_get_height (surface->surface);

    return CAIRO_STATUS_SUCCESS;
}

#define CAIRO_GLITZ_GLYPH_CACHE_MEMORY_DEFAULT 0x100000

#define CAIRO_GLITZ_AREA_AVAILABLE 0
#define CAIRO_GLITZ_AREA_DIVIDED   1
#define CAIRO_GLITZ_AREA_OCCUPIED  2

typedef struct _cairo_glitz_root_area cairo_glitz_root_area_t;

typedef struct _cairo_glitz_area {
    int			     state;
    int			     level;
    int			     x, y;
    int			     width, height;
    struct _cairo_glitz_area *area[4];
    cairo_glitz_root_area_t  *root;
    void		     *closure;
} cairo_glitz_area_t;

static cairo_glitz_area_t _empty_area = {
    0, 0, 0, 0, 0, 0,
    { NULL, NULL, NULL, NULL },
    NULL,
    NULL
};

typedef struct _cairo_glitz_area_funcs {
    cairo_status_t (*move_in)	    (cairo_glitz_area_t *area,
				     void		*closure);
    
    void	   (*move_out)	    (cairo_glitz_area_t *area,
				     void		*closure);
    
    int		   (*compare_score) (cairo_glitz_area_t *area,
				     void		*closure1,
				     void		*closure2);
} cairo_glitz_area_funcs_t;

struct _cairo_glitz_root_area {
    int				   max_level;
    int				   width, height;
    cairo_glitz_area_t		   *area;
    const cairo_glitz_area_funcs_t *funcs;
};

static cairo_status_t
_cairo_glitz_area_move_in (cairo_glitz_area_t *area,
			   void		      *closure)
{
    area->closure = closure;
    area->state   = CAIRO_GLITZ_AREA_OCCUPIED;
    
    return (*area->root->funcs->move_in) (area, area->closure);
}

static void
_cairo_glitz_area_move_out (cairo_glitz_area_t *area)
{
    if (area->root)
    {
	(*area->root->funcs->move_out) (area, area->closure);

	area->closure = NULL;
	area->state   = CAIRO_GLITZ_AREA_AVAILABLE;
    }
}

static cairo_glitz_area_t *
_cairo_glitz_area_create (cairo_glitz_root_area_t *root,
			  int			  level,
			  int			  x,
			  int			  y,
			  int			  width,
			  int			  height)
{
    cairo_glitz_area_t *area;
    int		       n = 4;
    
    area = malloc (sizeof (cairo_glitz_area_t));
    if (!area)
	return NULL;

    area->level   = level;
    area->x	  = x;
    area->y	  = y;
    area->width   = width;
    area->height  = height;
    area->root    = root;
    area->closure = NULL;
    area->state   = CAIRO_GLITZ_AREA_AVAILABLE;
    
    while (n--)
	area->area[n] = NULL;

    return area;
}

static void
_cairo_glitz_area_destroy (cairo_glitz_area_t *area)
{   
    if (area == NULL)
	return;

    if (area->state == CAIRO_GLITZ_AREA_OCCUPIED)
    {
	_cairo_glitz_area_move_out (area);
    }
    else
    {
	int n = 4;
	
	while (n--)
	    _cairo_glitz_area_destroy (area->area[n]);
    }
    
    free (area);
}

static cairo_glitz_area_t *
_cairo_glitz_area_get_top_scored_sub_area (cairo_glitz_area_t *area)
{
    if (!area)
	return NULL;
		
    switch (area->state) {
    case CAIRO_GLITZ_AREA_OCCUPIED:
	return area;
    case CAIRO_GLITZ_AREA_AVAILABLE:
	break;
    case CAIRO_GLITZ_AREA_DIVIDED: {
	cairo_glitz_area_t *tmp, *top = NULL;
	int		   i;
	
	for (i = 0; i < 4; i++)
	{
	    tmp = _cairo_glitz_area_get_top_scored_sub_area (area->area[i]);
	    if (tmp && top)
	    {
		if ((*area->root->funcs->compare_score) (tmp,
							 tmp->closure,
							 top->closure) > 0)
		    top = tmp;
	    }
	    else if (tmp)
	    {
		top = tmp;
	    }
	}
	return top;
    }
    }
    
    return NULL;
}

static cairo_int_status_t
_cairo_glitz_area_find (cairo_glitz_area_t *area,
			int		   width,
			int		   height,
			cairo_bool_t	   kick_out,
			void		   *closure)
{
    cairo_status_t status;

    if (area->width < width || area->height < height)
	return CAIRO_INT_STATUS_UNSUPPORTED;

    switch (area->state) {
    case CAIRO_GLITZ_AREA_OCCUPIED:
	if (kick_out)
	{
	    if ((*area->root->funcs->compare_score) (area,
						     area->closure,
						     closure) >= 0)
		return CAIRO_INT_STATUS_UNSUPPORTED;
	
	    _cairo_glitz_area_move_out (area);
	} else {
	    return CAIRO_INT_STATUS_UNSUPPORTED;
	}
		
    /* fall-through */
    case CAIRO_GLITZ_AREA_AVAILABLE: {
	if (area->level == area->root->max_level ||
	    (area->width == width && area->height == height))
	{
	    return _cairo_glitz_area_move_in (area, closure);
	}
	else
	{
	    int dx[4], dy[4], w[4], h[4], i;
	    
	    dx[0] = dx[2] = dy[0] = dy[1] = 0;
	    
	    w[0] = w[2] = dx[1] = dx[3] = width;
	    h[0] = h[1] = dy[2] = dy[3] = height;

	    w[1] = w[3] = area->width - width;
	    h[2] = h[3] = area->height - height;
	    
	    for (i = 0; i < 2; i++)
	    {
		if (w[i])
		    area->area[i] =
			_cairo_glitz_area_create (area->root,
						  area->level + 1,
						  area->x + dx[i],
						  area->y + dy[i],
						  w[i], h[i]);
	    }

	    for (; i < 4; i++)
	    {
		if (w[i] && h[i])
		    area->area[i] =
			_cairo_glitz_area_create (area->root,
						  area->level + 1,
						  area->x + dx[i],
						  area->y + dy[i],
						  w[i], h[i]);
	    }

	    area->state = CAIRO_GLITZ_AREA_DIVIDED;
	    
	    status = _cairo_glitz_area_find (area->area[0],
					     width, height,
					     kick_out, closure);
	    if (status == CAIRO_STATUS_SUCCESS)
                return CAIRO_STATUS_SUCCESS;
	}
    } break;
    case CAIRO_GLITZ_AREA_DIVIDED: {
	cairo_glitz_area_t *to_area;
	int		   i, rejected = FALSE;

	for (i = 0; i < 4; i++)
	{
	    if (area->area[i])
	    {
		if (area->area[i]->width >= width &&
		    area->area[i]->height >= height)
		{
		    status = _cairo_glitz_area_find (area->area[i],
						     width, height,
						     kick_out, closure);
		    if (status == CAIRO_STATUS_SUCCESS)
			return CAIRO_STATUS_SUCCESS;
		    
		    rejected = TRUE;
		}
	    }
	}

	if (rejected)
	    return CAIRO_INT_STATUS_UNSUPPORTED;

	to_area = _cairo_glitz_area_get_top_scored_sub_area (area);
	if (to_area)
	{
	    if (kick_out)
	    {
		if ((*area->root->funcs->compare_score) (to_area,
							 to_area->closure,
							 closure) >= 0)
		    return CAIRO_INT_STATUS_UNSUPPORTED;
	    } else {
		return CAIRO_INT_STATUS_UNSUPPORTED;
	    }
	}

	for (i = 0; i < 4; i++)
	{
	    _cairo_glitz_area_destroy (area->area[i]);
	    area->area[i] = NULL;
	}
	
	area->closure = NULL;
	area->state   = CAIRO_GLITZ_AREA_AVAILABLE;

	status = _cairo_glitz_area_find (area, width, height,
					 TRUE, closure);
	if (status == CAIRO_STATUS_SUCCESS)
	    return CAIRO_STATUS_SUCCESS;
	
    } break;
    }

    return CAIRO_INT_STATUS_UNSUPPORTED;
}

static cairo_status_t
_cairo_glitz_root_area_init (cairo_glitz_root_area_t	    *root,
			     int			    max_level,
			     int			    width,
			     int			    height,
			     const cairo_glitz_area_funcs_t *funcs)
{
    root->max_level = max_level;
    root->funcs     = funcs;

    root->area = _cairo_glitz_area_create (root, 0, 0, 0, width, height);
    if (!root->area)
	return CAIRO_STATUS_NO_MEMORY;
    
    return CAIRO_STATUS_SUCCESS;
}

static void
_cairo_glitz_root_area_fini (cairo_glitz_root_area_t *root)
{
    _cairo_glitz_area_destroy (root->area);
}

#define GLYPH_CACHE_TEXTURE_SIZE 512
#define GLYPH_CACHE_MAX_LEVEL     64
#define GLYPH_CACHE_MAX_HEIGHT    72
#define GLYPH_CACHE_MAX_WIDTH     72

#define WRITE_VEC2(ptr, _x, _y) \
    *(ptr)++ = (_x);		\
    *(ptr)++ = (_y)

#define WRITE_BOX(ptr, _vx1, _vy1, _vx2, _vy2, p1, p2) \
    WRITE_VEC2 (ptr, _vx1, _vy1);		       \
    WRITE_VEC2 (ptr, (p1)->x, (p2)->y);		       \
    WRITE_VEC2 (ptr, _vx2, _vy1);		       \
    WRITE_VEC2 (ptr, (p2)->x, (p2)->y);		       \
    WRITE_VEC2 (ptr, _vx2, _vy2);		       \
    WRITE_VEC2 (ptr, (p2)->x, (p1)->y);		       \
    WRITE_VEC2 (ptr, _vx1, _vy2);		       \
    WRITE_VEC2 (ptr, (p1)->x, (p1)->y)

typedef struct _cairo_glitz_glyph_cache {
    cairo_cache_t	    base;
    cairo_glitz_root_area_t root;
    glitz_surface_t	    *surface;
} cairo_glitz_glyph_cache_t;

typedef struct {
    cairo_glyph_cache_key_t key;
    int			    ref_count;
    cairo_glyph_size_t	    size;
    cairo_glitz_area_t	    *area;
    cairo_bool_t	    locked;
    cairo_point_double_t    p1, p2;
} cairo_glitz_glyph_cache_entry_t;

static cairo_status_t
_cairo_glitz_glyph_move_in (cairo_glitz_area_t *area,
			    void	       *closure)
{
    cairo_glitz_glyph_cache_entry_t *entry = closure;
    
    entry->area = area;

    return CAIRO_STATUS_SUCCESS;
}

static void
_cairo_glitz_glyph_move_out (cairo_glitz_area_t	*area,
			     void	        *closure)
{
    cairo_glitz_glyph_cache_entry_t *entry = closure;
    
    entry->area = NULL;
}

static int
_cairo_glitz_glyph_compare (cairo_glitz_area_t *area,
			    void	       *closure1,
			    void	       *closure2)
{
    cairo_glitz_glyph_cache_entry_t *entry = closure1;
    
    if (entry->locked)
	return 1;

    return -1;
}

static const cairo_glitz_area_funcs_t _cairo_glitz_area_funcs = {
    _cairo_glitz_glyph_move_in,
    _cairo_glitz_glyph_move_out,
    _cairo_glitz_glyph_compare
};

static cairo_status_t 
_cairo_glitz_glyph_cache_create_entry (void *abstract_cache,
				       void *abstract_key,
				       void **return_entry)
{
    cairo_glitz_glyph_cache_entry_t *entry;
    cairo_glyph_cache_key_t	    *key = abstract_key;
   
    cairo_status_t status;
    cairo_cache_t *im_cache;
    cairo_image_glyph_cache_entry_t *im;

    unsigned long entry_memory = 0;

    entry = malloc (sizeof (cairo_glitz_glyph_cache_entry_t));
    if (!entry)
	return CAIRO_STATUS_NO_MEMORY;

    _cairo_lock_global_image_glyph_cache ();

    im_cache = _cairo_get_global_image_glyph_cache ();
    if (im_cache == NULL) {
	_cairo_unlock_global_image_glyph_cache ();
	free (entry);
	return CAIRO_STATUS_NO_MEMORY;
    }

    status = _cairo_cache_lookup (im_cache, key, (void **) (&im), NULL);
    if (status != CAIRO_STATUS_SUCCESS || im == NULL) {
	_cairo_unlock_global_image_glyph_cache ();
	free (entry);
	return CAIRO_STATUS_NO_MEMORY;
    }

    if (im->image)
	entry_memory = im->image->width * im->image->stride;

    _cairo_unlock_global_image_glyph_cache ();

    entry->ref_count = 1;
    entry->key	    = *key;
    entry->key.base.memory = entry_memory;
    entry->area	    = NULL;
    entry->locked   = FALSE;

    _cairo_unscaled_font_reference (entry->key.unscaled);
    
    *return_entry = entry;

    return CAIRO_STATUS_SUCCESS;
}

static void 
_cairo_glitz_glyph_cache_destroy_entry (void *abstract_cache,
					void *abstract_entry)
{
    cairo_glitz_glyph_cache_entry_t *entry = abstract_entry;

    entry->ref_count--;
    if (entry->ref_count)
	return;

    if (entry->area)
	_cairo_glitz_area_move_out (entry->area);
    
    _cairo_unscaled_font_destroy (entry->key.unscaled);

    free (entry);	
}

static void 
_cairo_glitz_glyph_cache_entry_reference (void *abstract_entry)
{
    cairo_glitz_glyph_cache_entry_t *entry = abstract_entry;

    entry->ref_count++;	
}

static void 
_cairo_glitz_glyph_cache_destroy_cache (void *abstract_cache)
{
    cairo_glitz_glyph_cache_t *cache = abstract_cache;

    _cairo_glitz_root_area_fini (&cache->root);
	
    glitz_surface_destroy (cache->surface);
}

static const cairo_cache_backend_t _cairo_glitz_glyph_cache_backend = {
    _cairo_glyph_cache_hash,
    _cairo_glyph_cache_keys_equal,
    _cairo_glitz_glyph_cache_create_entry,
    _cairo_glitz_glyph_cache_destroy_entry,
    _cairo_glitz_glyph_cache_destroy_cache
};

static cairo_glitz_glyph_cache_t *_cairo_glitz_glyph_caches = NULL;

static cairo_glitz_glyph_cache_t *
_cairo_glitz_get_glyph_cache (cairo_glitz_surface_t *surface)
{
    cairo_glitz_glyph_cache_t *cache;
    glitz_drawable_t	      *drawable;
    glitz_format_t	      *format;

    /* 
     * FIXME: caches should be thread specific, display specific and screen
     * specific. 
     */
    
    if (_cairo_glitz_glyph_caches)
	return _cairo_glitz_glyph_caches;

    drawable = glitz_surface_get_drawable (surface->surface);
    
    format = glitz_find_standard_format (drawable, GLITZ_STANDARD_A8);
    if (!format)
	return NULL;
    
    cache = malloc (sizeof (cairo_glitz_glyph_cache_t));
    if (!cache)
	return NULL;

    cache->surface =
	glitz_surface_create (drawable, format,
			      GLYPH_CACHE_TEXTURE_SIZE,
			      GLYPH_CACHE_TEXTURE_SIZE,
			      0, NULL);
    if (cache->surface == NULL)
    {
	free (cache);
	return NULL;
    }

    if (_cairo_glitz_root_area_init (&cache->root,
				     GLYPH_CACHE_MAX_LEVEL,
				     GLYPH_CACHE_TEXTURE_SIZE,
				     GLYPH_CACHE_TEXTURE_SIZE,
				     &_cairo_glitz_area_funcs))
    {
	glitz_surface_destroy (cache->surface);
	free (cache);
	return NULL;
    }

    if (_cairo_cache_init (&cache->base,
			   &_cairo_glitz_glyph_cache_backend,
			   CAIRO_GLITZ_GLYPH_CACHE_MEMORY_DEFAULT))
    {
	_cairo_glitz_root_area_fini (&cache->root);
	glitz_surface_destroy (cache->surface);
	free (cache);
	return NULL;
    }
    
    _cairo_glitz_glyph_caches = cache;
    
    return cache;
}

#define FIXED_TO_FLOAT(f) (((glitz_float_t) (f)) / 65536)

static cairo_status_t
_cairo_glitz_cache_glyph (cairo_glitz_glyph_cache_t	  *cache,
			  cairo_glitz_glyph_cache_entry_t *entry,
			  cairo_image_glyph_cache_entry_t *image_entry)
{
    glitz_point_fixed_t  p1, p2;
    glitz_pixel_format_t pf;
    glitz_buffer_t	 *buffer;
    pixman_format_t	 *format;
    int			 am, rm, gm, bm;

    entry->size = image_entry->size;
    
    if (entry->size.width  > GLYPH_CACHE_MAX_WIDTH ||
	entry->size.height > GLYPH_CACHE_MAX_HEIGHT)
	return CAIRO_STATUS_SUCCESS;

    if ((entry->size.width  == 0 && entry->size.height == 0) ||
        !image_entry->image)
    {
	entry->area = &_empty_area;
	return CAIRO_STATUS_SUCCESS;
    }
    
    format = pixman_image_get_format (image_entry->image->pixman_image);
    if (!format)
	return CAIRO_STATUS_NO_MEMORY;
	
    if (_cairo_glitz_area_find (cache->root.area,
				entry->size.width,
				entry->size.height,
				FALSE, entry))
    {
	if (_cairo_glitz_area_find (cache->root.area,
				    entry->size.width,
				    entry->size.height,
				    TRUE, entry))
	    return CAIRO_STATUS_SUCCESS;
    }
    
    buffer = glitz_buffer_create_for_data (image_entry->image->data);
    if (!buffer)
    {
	_cairo_glitz_area_move_out (entry->area);
	return CAIRO_STATUS_NO_MEMORY;
    }

    pixman_format_get_masks (format, &pf.masks.bpp, &am, &rm, &gm, &bm);

    pf.masks.alpha_mask = am;
    pf.masks.red_mask   = rm;
    pf.masks.green_mask = gm;
    pf.masks.blue_mask  = bm;
    
    pf.bytes_per_line = image_entry->image->stride;
    pf.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_BOTTOM_UP;
    pf.xoffset	      = 0;
    pf.skip_lines     = 0;
    
    glitz_set_pixels (cache->surface,
		      entry->area->x,
		      entry->area->y,
		      entry->size.width,
		      entry->size.height,
		      &pf, buffer);

    glitz_buffer_destroy (buffer);
	
    p1.x = entry->area->x << 16;
    p1.y = entry->area->y << 16;
    p2.x = (entry->area->x + entry->size.width)  << 16;
    p2.y = (entry->area->y + entry->size.height) << 16;
    
    glitz_surface_translate_point (cache->surface, &p1, &p1);
    glitz_surface_translate_point (cache->surface, &p2, &p2);
    
    entry->p1.x = FIXED_TO_FLOAT (p1.x);
    entry->p1.y = FIXED_TO_FLOAT (p1.y);
    entry->p2.x = FIXED_TO_FLOAT (p2.x);
    entry->p2.y = FIXED_TO_FLOAT (p2.y);
    
    return CAIRO_STATUS_SUCCESS;
}

#define N_STACK_BUF 256

static cairo_int_status_t
_cairo_glitz_surface_show_glyphs (cairo_scaled_font_t *scaled_font,
				  cairo_operator_t    op,
				  cairo_pattern_t     *pattern,
				  void		      *abstract_surface,
				  int		      src_x,
				  int		      src_y,
				  int		      dst_x,
				  int		      dst_y,
				  unsigned int	      width,
				  unsigned int	      height,
				  const cairo_glyph_t *glyphs,
				  int		      num_glyphs)
{
    cairo_glitz_surface_attributes_t attributes;
    cairo_glitz_surface_t	     *dst = abstract_surface;
    cairo_glitz_surface_t	     *src;
    cairo_glitz_glyph_cache_t	     *cache;
    cairo_glitz_glyph_cache_entry_t  *stack_entries[N_STACK_BUF];
    cairo_glitz_glyph_cache_entry_t  **entries;
    cairo_glyph_cache_key_t	     key;
    glitz_float_t		     stack_vertices[N_STACK_BUF * 16];
    glitz_float_t		     *vertices;
    glitz_buffer_t		     *buffer;
    cairo_int_status_t		     status;
    int				     i, cached_glyphs = 0;
    int				     remaining_glyps = num_glyphs;
    glitz_float_t		     x1, y1, x2, y2;
    static glitz_vertex_format_t     format = {
	GLITZ_PRIMITIVE_QUADS,
	GLITZ_DATA_TYPE_FLOAT,
	sizeof (glitz_float_t) * 4,
	GLITZ_VERTEX_ATTRIBUTE_MASK_COORD_MASK,
	{ 0 },
	{
	    GLITZ_DATA_TYPE_FLOAT,
	    GLITZ_COORDINATE_SIZE_XY,
	    sizeof (glitz_float_t) * 2,
	}
    };

    if (op == CAIRO_OPERATOR_SATURATE)
	return CAIRO_INT_STATUS_UNSUPPORTED;

    if (_glitz_ensure_target (dst->surface))
	return CAIRO_INT_STATUS_UNSUPPORTED;

    status = _cairo_glitz_pattern_acquire_surface (pattern, dst,
						   src_x, src_y,
						   width, height,
						   &src, &attributes);
    if (status)
	return status;

    _cairo_glitz_surface_set_attributes (src, &attributes);

    if (num_glyphs > N_STACK_BUF)
    {
	char *data;
	
	data = malloc (num_glyphs * sizeof (void *) +
		       num_glyphs * sizeof (glitz_float_t) * 16);
	if (!data)
	    goto FAIL1;
	
	entries  = (cairo_glitz_glyph_cache_entry_t **) data;
	vertices = (glitz_float_t *) (data + num_glyphs * sizeof (void *));
    }
    else
    {
	entries  = stack_entries;
	vertices = stack_vertices;
    }

    buffer = glitz_buffer_create_for_data (vertices);
    if (!buffer)
	goto FAIL2;
    
    cache = _cairo_glitz_get_glyph_cache (dst);
    if (!cache)
    {
	num_glyphs = 0;
	goto UNLOCK;
    }

    status = _cairo_scaled_font_get_glyph_cache_key (scaled_font, &key);
    if (status)
	goto UNLOCK;

    for (i = 0; i < num_glyphs; i++)
    {
	key.index = glyphs[i].index;
	
	status = _cairo_cache_lookup (&cache->base,
				      &key,
				      (void **) &entries[i],
				      NULL);
	if (status)
	{
	    num_glyphs = i;
	    goto UNLOCK;
	}

	_cairo_glitz_glyph_cache_entry_reference (entries[i]);

	if (entries[i]->area)
	{
	    remaining_glyps--;

	    if (entries[i]->area->width)
	    {
		x1 = floor (glyphs[i].x + 0.5) + entries[i]->size.x;
		y1 = floor (glyphs[i].y + 0.5) + entries[i]->size.y;
		x2 = x1 + entries[i]->size.width;
		y2 = y1 + entries[i]->size.height;
	    
		WRITE_BOX (vertices, x1, y1, x2, y2,
			   &entries[i]->p1, &entries[i]->p2);
	    
		entries[i]->locked = TRUE;
		cached_glyphs++;
	    }
	}
    }

    if (remaining_glyps)
    {
	cairo_cache_t			*image_cache;
	cairo_image_glyph_cache_entry_t *image_entry;
	cairo_surface_t			*image;
	cairo_glitz_surface_t		*clone;
	
	_cairo_lock_global_image_glyph_cache ();

	image_cache = _cairo_get_global_image_glyph_cache ();
	if (!image_cache)
	{
	    _cairo_unlock_global_image_glyph_cache ();
	    status = CAIRO_STATUS_NO_MEMORY;
	    goto UNLOCK;
	}
	
	for (i = 0; i < num_glyphs; i++)
	{
	    if (!entries[i]->area)
	    {
		key.index = glyphs[i].index;
		
		status = _cairo_cache_lookup (image_cache,
					      &key,
					      (void **) &image_entry,
					      NULL);
		if (status)
		{
		    _cairo_unlock_global_image_glyph_cache ();
		    goto UNLOCK;
		}

		status = _cairo_glitz_cache_glyph (cache,
						   entries[i],
						   image_entry);
		if (status)
		{
		    _cairo_unlock_global_image_glyph_cache ();
		    goto UNLOCK;
		}
	    }
	    
	    x1 = floor (glyphs[i].x + 0.5);
	    y1 = floor (glyphs[i].y + 0.5);

	    if (entries[i]->area)
	    {
		if (entries[i]->area->width)
		{
		    x1 += entries[i]->size.x;
		    y1 += entries[i]->size.y;
		    x2 = x1 + entries[i]->size.width;
		    y2 = y1 + entries[i]->size.height;
		    
		    WRITE_BOX (vertices, x1, y1, x2, y2,
			       &entries[i]->p1, &entries[i]->p2);
		    
		    entries[i]->locked = TRUE;
		    cached_glyphs++;
		}
	    }
	    else
	    {
		x1 += image_entry->size.x;
		y1 += image_entry->size.y;

		if (!image_entry->image)
		    continue;
		
		image = &image_entry->image->base;
		status =
		    _cairo_glitz_surface_clone_similar (abstract_surface,
							image,
							(cairo_surface_t **)
							&clone);
		if (status)
		{
		    _cairo_unlock_global_image_glyph_cache ();
		    goto UNLOCK;
		}

		glitz_composite (_glitz_operator (op), 
				 src->surface, 
				 clone->surface,
				 dst->surface,
				 src_x + attributes.base.x_offset + x1,
				 src_y + attributes.base.y_offset + y1,
				 0, 0,
				 x1, y1,
				 image_entry->size.width,
				 image_entry->size.height);

		cairo_surface_destroy (&clone->base);

		if (glitz_surface_get_status (dst->surface) ==
		    GLITZ_STATUS_NOT_SUPPORTED)
		{
		    status = CAIRO_INT_STATUS_UNSUPPORTED;
		    _cairo_unlock_global_image_glyph_cache ();
		    goto UNLOCK;
		}
	    }
	}
	
	_cairo_unlock_global_image_glyph_cache ();
    }

    if (cached_glyphs)
    {
	glitz_set_geometry (dst->surface,
			    GLITZ_GEOMETRY_TYPE_VERTEX,
			    (glitz_geometry_format_t *) &format,
			    buffer);

	glitz_set_array (dst->surface, 0, 4, cached_glyphs * 4, 0, 0);

	glitz_composite (_glitz_operator (op),
			 src->surface,
			 cache->surface,
			 dst->surface,
			 src_x + attributes.base.x_offset,
			 src_y + attributes.base.y_offset,
			 0, 0,
			 dst_x, dst_y,
			 width, height);

	glitz_set_geometry (dst->surface,
			    GLITZ_GEOMETRY_TYPE_NONE,
			    NULL, NULL);
    }
    
UNLOCK:
    if (cached_glyphs)
    {
	for (i = 0; i < num_glyphs; i++)
	    entries[i]->locked = FALSE;
    }
    
    for (i = 0; i < num_glyphs; i++)
	_cairo_glitz_glyph_cache_destroy_entry (cache, entries[i]);

    glitz_buffer_destroy (buffer);

 FAIL2:
    if (num_glyphs > N_STACK_BUF)
	free (entries);

 FAIL1:
    if (attributes.n_params)
	free (attributes.params);

    _cairo_glitz_pattern_release_surface (pattern, src, &attributes);

    if (status)
	return status;
    
    if (glitz_surface_get_status (dst->surface) == GLITZ_STATUS_NOT_SUPPORTED)
	return CAIRO_INT_STATUS_UNSUPPORTED;
    
    return CAIRO_STATUS_SUCCESS;
}

static const cairo_surface_backend_t cairo_glitz_surface_backend = {
    _cairo_glitz_surface_create_similar,
    _cairo_glitz_surface_finish,
    _cairo_glitz_surface_acquire_source_image,
    _cairo_glitz_surface_release_source_image,
    _cairo_glitz_surface_acquire_dest_image,
    _cairo_glitz_surface_release_dest_image,
    _cairo_glitz_surface_clone_similar,
    _cairo_glitz_surface_composite,
    _cairo_glitz_surface_fill_rectangles,
    _cairo_glitz_surface_composite_trapezoids,
    NULL, /* copy_page */
    NULL, /* show_page */
    _cairo_glitz_surface_set_clip_region,
    NULL, /* intersect_clip_path */
    _cairo_glitz_surface_get_extents,
    _cairo_glitz_surface_show_glyphs
};

cairo_surface_t *
cairo_glitz_surface_create (glitz_surface_t *surface)
{
    cairo_glitz_surface_t *crsurface;

    if (surface == NULL)
	return (cairo_surface_t*) &_cairo_surface_nil;

    crsurface = malloc (sizeof (cairo_glitz_surface_t));
    if (crsurface == NULL) {
	_cairo_error (CAIRO_STATUS_NO_MEMORY);
	return (cairo_surface_t*) &_cairo_surface_nil;
    }

    _cairo_surface_init (&crsurface->base, &cairo_glitz_surface_backend);

    glitz_surface_reference (surface);
    
    crsurface->surface = surface;
    crsurface->format  = glitz_surface_get_format (surface);
    crsurface->clip    = NULL;
    
    return (cairo_surface_t *) crsurface;
}