summaryrefslogtreecommitdiff
path: root/src/cairo-pixman-surface.c
blob: 60f3d62bda66c87382585a222de11a5f57a970d6 (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
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2003 University of Southern California
 * Copyright © 2009,2010,2011 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is University of Southern
 * California.
 *
 * Contributor(s):
 *	Carl D. Worth <cworth@cworth.org>
 *	Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairoint.h"

#include "cairo-pixman.h"
#include "cairo-default-context-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-pattern-private.h"
#include "cairo-clip-inline.h"
#include "cairo-traps-private.h"
#include "cairoint.h"

extern const cairo_surface_backend_t cairo_pixman_surface_backend;

typedef struct _cairo_pixman_surface cairo_pixman_surface_t;

struct _cairo_pixman_surface
{
    cairo_surface_t	base;
    pixman_image_t *	pimage;
};

/**
 * SECTION:cairo-pixman
 * @Title: Pixman Surfaces
 * @Short_Description: Rendering to pixman images
 * @See_Also: #cairo_surface_t
 *
 * Pixman surfaces provide the ability to render to pixman images
 * allocated by the calling code. The supported pixman formats are
 * those for which pixman_supported_destination() returns TRUE.
 */

/**
 * CAIRO_HAS_PIXMAN_SURFACE:
 *
 * Defined if the pixman surface backend is available.
 *
 * @Since: 1.14
 */

static cairo_content_t
content_from_pixman_image (pixman_image_t *image)
{
    pixman_format_code_t format = pixman_image_get_format (image);
    cairo_content_t result;
    
    result = 0;
    if (PIXMAN_FORMAT_RGB (format))
	result |= CAIRO_CONTENT_COLOR;
    if (PIXMAN_FORMAT_A (format))
	result |= CAIRO_CONTENT_ALPHA;
    
    return result;
}

/**
 * cairo_pixman_surface_create:
 * @image: a pixman image
 *
 * Creates an pixman surface based on the given image
 * dimensions. Initially the surface contents are all
 * 0. (Specifically, within each pixel, each color or alpha channel
 * belonging to format will be 0. The contents of bits within a pixel,
 * but not belonging to the given format are undefined).
 *
 * Return value: a pointer to the newly created surface. The caller
 * owns the surface and should call cairo_surface_destroy() when done
 * with it.
 *
 * This function always returns a valid pointer, but it will return a
 * pointer to a "nil" surface if an error such as out of memory
 * occurs. You can use cairo_surface_status() to check for this.
 **/
cairo_surface_t *
cairo_pixman_surface_create (pixman_image_t *image)
{
    cairo_pixman_surface_t *surface;
    pixman_format_code_t format;
    
    if (!(surface = malloc (sizeof *surface))) {
	return _cairo_surface_create_in_error (
	    _cairo_error (CAIRO_STATUS_NO_MEMORY));
    }
    
    format = pixman_image_get_format (image);
    if (!format || !pixman_format_supported_destination (format)) {
	/* FIXME: This error status is intended for incorrect
	 * cairo_format_ts, not incorrect pixman formats. In
	 * principle a new error status should be added
	 */
	return _cairo_surface_create_in_error (
	    _cairo_error (CAIRO_STATUS_INVALID_FORMAT));
    }
    
    _cairo_surface_init (&surface->base, &cairo_pixman_surface_backend,
			 NULL, /* device */
			 content_from_pixman_image (image));
    
    surface->pimage = pixman_image_ref (image);
    
    return (cairo_surface_t *)surface;
}

static cairo_status_t
cairo_pixman_surface_finish (void *abstract_surface)
{
    cairo_pixman_surface_t *surface = abstract_surface;
    
    pixman_image_unref (surface->pimage);
    surface->pimage = NULL;
    
    return CAIRO_STATUS_SUCCESS;
}

static cairo_surface_t *
cairo_pixman_surface_create_similar (void	       *abstract_other,
				     cairo_content_t	content,
				     int		width,
				     int		height)
{
    cairo_pixman_surface_t *other = abstract_other;
    pixman_format_code_t format;
    pixman_image_t *copy;
    
    format = pixman_image_get_format (other->pimage);
    
    copy = pixman_image_create_bits (format, width, height, NULL, -1);
    
    return cairo_pixman_surface_create (copy);
}

static cairo_surface_t *
create_image_surface (cairo_pixman_surface_t *psurface)
{
    pixman_image_t *pimage = psurface->pimage;
    pixman_format_code_t pformat = pixman_image_get_format (pimage);
    cairo_bool_t must_copy = FALSE;
    cairo_surface_t *image;
    cairo_format_t format;
    
    switch (pformat)
    {
    case PIXMAN_a8r8g8b8:
	format = CAIRO_FORMAT_ARGB32;
	break;
	
    case PIXMAN_x8r8g8b8:
	format = CAIRO_FORMAT_RGB24;
	break;
	
    case PIXMAN_a8:
	format = CAIRO_FORMAT_A8;
	break;
	
    case PIXMAN_a1:
	format = CAIRO_FORMAT_A1;
	break;
	
    case PIXMAN_r5g6b5:
	format = CAIRO_FORMAT_RGB16_565;
	break;
	
    case PIXMAN_x2r10g10b10:
	format = CAIRO_FORMAT_RGB30;
	break;
	
    case PIXMAN_a8b8g8r8: case PIXMAN_x8b8g8r8: case PIXMAN_b8g8r8a8:
    case PIXMAN_b8g8r8x8: case PIXMAN_r8g8b8a8: case PIXMAN_r8g8b8x8:
    case PIXMAN_x14r6g6b6: case PIXMAN_a2r10g10b10: case PIXMAN_x2b10g10r10:
    case PIXMAN_a2b10g10r10: case PIXMAN_a8r8g8b8_sRGB: case PIXMAN_r8g8b8:
    case PIXMAN_b8g8r8: case PIXMAN_b5g6r5: case PIXMAN_a1r5g5b5:
    case PIXMAN_x1r5g5b5: case PIXMAN_a1b5g5r5: case PIXMAN_x1b5g5r5:
    case PIXMAN_a4r4g4b4: case PIXMAN_x4r4g4b4: case PIXMAN_a4b4g4r4:
    case PIXMAN_x4b4g4r4: case PIXMAN_r3g3b2: case PIXMAN_b2g3r3:
    case PIXMAN_a2r2g2b2: case PIXMAN_a2b2g2r2: case PIXMAN_g8:
    case PIXMAN_x4a4: case PIXMAN_a4: case PIXMAN_r1g2b1: case PIXMAN_c8:
    case PIXMAN_b1g2r1: case PIXMAN_a1r1g1b1: case PIXMAN_a1b1g1r1:
    case PIXMAN_c4: case PIXMAN_g4: case PIXMAN_g1:
    case PIXMAN_yuy2: case PIXMAN_yv12:
	
    default:
	format = CAIRO_FORMAT_ARGB32;
	must_copy = TRUE;
	break;
    }
    
    if (must_copy)
    {
	int width = pixman_image_get_width (pimage);
	int height = pixman_image_get_height (pimage);
	pixman_image_t *dest;
	
	image = cairo_image_surface_create (format, width, height);
	
	dest = ((cairo_image_surface_t *)image)->pixman_image;
	
	pixman_image_composite32 (PIXMAN_OP_SRC,
				  pimage, NULL, dest,
				  0, 0, 0, 0, 0, 0, width, height);
    }
    else
    {
	image = _cairo_image_surface_create_for_pixman_image (pimage, pformat);
    }
    
    return image;
}

static cairo_image_surface_t *
cairo_pixman_surface_map_to_image (void                        *abstract_other,
				   const cairo_rectangle_int_t *extents)
{
    cairo_pixman_surface_t *surface = abstract_other;
    
    /* FIXME: if we need to copy, we should only copy extents */
    return (cairo_image_surface_t *)create_image_surface (surface);
}

static cairo_int_status_t
cairo_pixman_surface_unmap_image (void                  *abstract_surface,
				  cairo_image_surface_t *image)
{
    cairo_pixman_surface_t *surface = abstract_surface;
    
    if (image->pixman_image != surface->pimage)
    {
	pixman_image_composite32 (PIXMAN_OP_SRC,
				  image->pixman_image, NULL, surface->pimage,
				  0, 0, 0, 0, 0, 0,
				  pixman_image_get_width (surface->pimage),
				  pixman_image_get_height (surface->pimage));
    }
    
    return CAIRO_STATUS_SUCCESS;
}

static cairo_surface_t *
cairo_pixman_surface_source (void		   *abstract_surface,
			     cairo_rectangle_int_t *extents)
{
    cairo_pixman_surface_t *surface = abstract_surface;
    
    extents->x = extents->y = 0;
    extents->width = pixman_image_get_width (surface->pimage);
    extents->height = pixman_image_get_height (surface->pimage);
    
    return &surface->base;
}

static cairo_status_t
cairo_pixman_surface_acquire_source_image (void                    *abstract_surface,
					   cairo_image_surface_t  **image_out,
					   void                   **image_extra)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    
    *image_out = (cairo_image_surface_t *)create_image_surface (psurface);
    *image_extra = NULL;
    
    /* FIXME: create_image_surface can fail (or should be able to fail) */
    return CAIRO_STATUS_SUCCESS;
}

static void
cairo_pixman_surface_release_source_image (void                   *abstract_surface,
					   cairo_image_surface_t  *image,
					   void                   *image_extra)
{
    cairo_surface_destroy ((cairo_surface_t *)image);
}

static cairo_surface_t *
cairo_pixman_surface_snapshot (void *abstract_surface)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    
    return create_image_surface (psurface);
}

static cairo_bool_t
cairo_pixman_surface_get_extents (void			 *abstract_surface,
				  cairo_rectangle_int_t  *rectangle)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    
    rectangle->x = 0;
    rectangle->y = 0;
    rectangle->width = pixman_image_get_width (psurface->pimage);
    rectangle->height = pixman_image_get_height (psurface->pimage);
    
    return CAIRO_STATUS_SUCCESS;
}

static void
cairo_pixman_surface_get_font_options (void                  *abstract_surface,
				       cairo_font_options_t  *options)
{
    _cairo_font_options_init_default (options);
    
    cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
    _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
}

static void
set_properties (pixman_image_t *image, cairo_pattern_t *pattern)
{
    cairo_matrix_t *matrix = &(pattern->matrix);
    pixman_transform_t transform;
    pixman_filter_t filter;
    pixman_repeat_t repeat;
    
    /* Transform */
    transform.matrix[0][0] = _cairo_fixed_16_16_from_double (matrix->xx);
    transform.matrix[0][1] = _cairo_fixed_16_16_from_double (matrix->xy);
    transform.matrix[0][2] = _cairo_fixed_16_16_from_double (matrix->x0);
    transform.matrix[1][0] = _cairo_fixed_16_16_from_double (matrix->yx);
    transform.matrix[1][1] = _cairo_fixed_16_16_from_double (matrix->yy);
    transform.matrix[1][2] = _cairo_fixed_16_16_from_double (matrix->y0);
    
    transform.matrix[2][0] = 0;
    transform.matrix[2][1] = 0;
    transform.matrix[2][2] = pixman_fixed_1;
    
    pixman_image_set_transform (image, &transform);
    
    /* Filter */
    switch (pattern->filter)
    {
    case CAIRO_FILTER_FAST:
	filter = PIXMAN_FILTER_FAST;
	break;
    case CAIRO_FILTER_BEST:
	filter = PIXMAN_FILTER_BEST;
	break;
    case CAIRO_FILTER_NEAREST:
	filter = PIXMAN_FILTER_NEAREST;
	break;
    case CAIRO_FILTER_BILINEAR:
	filter = PIXMAN_FILTER_BILINEAR;
	break;
    case CAIRO_FILTER_GAUSSIAN:
    case CAIRO_FILTER_GOOD:
    default:
	filter = PIXMAN_FILTER_GOOD;
	break;
    }
    
    pixman_image_set_filter (image, filter, NULL, -1);
    
    /* Repeat mode */
    switch (pattern->extend)
    {
    default:
    case CAIRO_EXTEND_NONE:
	repeat = PIXMAN_REPEAT_NONE;
	break;
    case CAIRO_EXTEND_REPEAT:
	repeat = PIXMAN_REPEAT_NORMAL;
	break;
    case CAIRO_EXTEND_REFLECT:
	repeat = PIXMAN_REPEAT_REFLECT;
	break;
    case CAIRO_EXTEND_PAD:
	repeat = PIXMAN_REPEAT_PAD;
	break;
    }
    
    pixman_image_set_repeat (image, repeat);
    
    pixman_image_set_component_alpha (image, pattern->has_component_alpha);
}

static cairo_int_status_t
pimage_from_solid_pattern (cairo_solid_pattern_t *solid,
			   pixman_image_t **result)
{
    pixman_color_t pcolor;
    
    pcolor.red = _cairo_color_double_to_short (solid->color.red);
    pcolor.green = _cairo_color_double_to_short (solid->color.green);
    pcolor.blue = _cairo_color_double_to_short (solid->color.blue);
    pcolor.alpha = _cairo_color_double_to_short (solid->color.alpha);

    if (!(*result = pixman_image_create_solid_fill (&pcolor)))
	return CAIRO_INT_STATUS_NO_MEMORY;

    return CAIRO_INT_STATUS_SUCCESS;
}

typedef struct acquire_source_cleanup_t
{
    cairo_surface_t *		surface;
    cairo_image_surface_t *	image;
    void *			extra;
} acquire_source_cleanup_t;

static void
clean_up_acquire (pixman_image_t *image, void *closure)
{
    acquire_source_cleanup_t *info = closure;
    
    if (info)
    {
	if (info->image)
	{
	    _cairo_surface_release_source_image (
		info->surface, info->image, info->extra);
	}

	free (info);
    }
}

static cairo_int_status_t
pimage_from_surface_pattern (cairo_surface_pattern_t *pattern,
			     pixman_image_t **result)
{
    pixman_image_t *simage;
    cairo_int_status_t status;
    acquire_source_cleanup_t *info = NULL;
    
    /* First, get a pixman image that has the right bits */
    switch (pattern->surface->type)
    {
    case CAIRO_SURFACE_TYPE_PIXMAN:
	simage = ((cairo_pixman_surface_t *)pattern->surface)->pimage;
	break;
	
    case CAIRO_SURFACE_TYPE_IMAGE:
	simage = ((cairo_image_surface_t *)pattern->surface)->pixman_image;
	break;
	
    case CAIRO_SURFACE_TYPE_PDF:
    case CAIRO_SURFACE_TYPE_PS:
    case CAIRO_SURFACE_TYPE_XLIB:
    case CAIRO_SURFACE_TYPE_XCB:
    case CAIRO_SURFACE_TYPE_GLITZ:
    case CAIRO_SURFACE_TYPE_QUARTZ:
    case CAIRO_SURFACE_TYPE_WIN32:
    case CAIRO_SURFACE_TYPE_BEOS:
    case CAIRO_SURFACE_TYPE_DIRECTFB:
    case CAIRO_SURFACE_TYPE_SVG:
    case CAIRO_SURFACE_TYPE_OS2:
    case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
    case CAIRO_SURFACE_TYPE_QUARTZ_IMAGE:
    case CAIRO_SURFACE_TYPE_SCRIPT:
    case CAIRO_SURFACE_TYPE_QT:
    case CAIRO_SURFACE_TYPE_RECORDING:
    case CAIRO_SURFACE_TYPE_VG:
    case CAIRO_SURFACE_TYPE_GL:
    case CAIRO_SURFACE_TYPE_DRM:
    case CAIRO_SURFACE_TYPE_TEE:
    case CAIRO_SURFACE_TYPE_XML:
    case CAIRO_SURFACE_TYPE_SKIA:
    case CAIRO_SURFACE_TYPE_SUBSURFACE:
    case CAIRO_SURFACE_TYPE_COGL:
    default:
	if (!(info = malloc (sizeof (acquire_source_cleanup_t))))
	{
	    status = CAIRO_INT_STATUS_NO_MEMORY;
	    goto out;
	}
	
	info->surface = pattern->surface;
	info->image = NULL;
	info->extra = NULL;

	status = _cairo_surface_acquire_source_image (
	    pattern->surface, &info->image, &info->extra);
	
	if (status != CAIRO_INT_STATUS_SUCCESS)
	    goto out;
	
	simage = info->image->pixman_image;
	break;
    }
    
    /* Then create a clone of that image */
    if (!(*result = pixman_image_create_bits (
	      pixman_image_get_format (simage),
	      pixman_image_get_width (simage),
	      pixman_image_get_height (simage),
	      pixman_image_get_data (simage),
	      pixman_image_get_stride (simage))))
    {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto out;
    }

    if (info)
	pixman_image_set_destroy_function (*result, clean_up_acquire, info);
    
    /* Then set the right properties on the clone */
    set_properties (*result, (cairo_pattern_t *)pattern);
    
out:
    if (status != CAIRO_INT_STATUS_SUCCESS)
	clean_up_acquire (NULL, info);
    
    return status;
}

static cairo_int_status_t
pimage_from_pattern (const cairo_pattern_t *pattern, pixman_image_t **image)
{
    switch (pattern->type)
    {
    case CAIRO_PATTERN_TYPE_SOLID:
	return pimage_from_solid_pattern (
	    (cairo_solid_pattern_t *)pattern, image);
	break;
	
    case CAIRO_PATTERN_TYPE_SURFACE:
	return pimage_from_surface_pattern (
	    (cairo_surface_pattern_t *)pattern, image);
	break;
	
    case CAIRO_PATTERN_TYPE_LINEAR:
    case CAIRO_PATTERN_TYPE_RADIAL:
    case CAIRO_PATTERN_TYPE_MESH:
    case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
	break;
    }
}

static const pixman_color_t white = { 0xffff, 0xffff, 0xffff, 0xffff };

#define CAIRO_FIXED_16_16_MIN _cairo_fixed_from_int (-32768)
#define CAIRO_FIXED_16_16_MAX _cairo_fixed_from_int (32767)

static cairo_bool_t
line_exceeds_16_16 (const cairo_line_t *line)
{
    return
        line->p1.x <= CAIRO_FIXED_16_16_MIN ||
        line->p1.x >= CAIRO_FIXED_16_16_MAX ||

        line->p2.x <= CAIRO_FIXED_16_16_MIN ||
        line->p2.x >= CAIRO_FIXED_16_16_MAX ||

        line->p1.y <= CAIRO_FIXED_16_16_MIN ||
        line->p1.y >= CAIRO_FIXED_16_16_MAX ||

        line->p2.y <= CAIRO_FIXED_16_16_MIN ||
        line->p2.y >= CAIRO_FIXED_16_16_MAX;
}

static void
project_line_x_onto_16_16 (const cairo_line_t *line,
                           cairo_fixed_t top,
                           cairo_fixed_t bottom,
                           pixman_line_fixed_t *out)
{
    /* XXX use fixed-point arithmetic? */
    cairo_point_double_t p1, p2;
    double m;

    p1.x = _cairo_fixed_to_double (line->p1.x);
    p1.y = _cairo_fixed_to_double (line->p1.y);

    p2.x = _cairo_fixed_to_double (line->p2.x);
    p2.y = _cairo_fixed_to_double (line->p2.y);

    m = (p2.x - p1.x) / (p2.y - p1.y);
    out->p1.x = _cairo_fixed_16_16_from_double (
	p1.x + m * _cairo_fixed_to_double (top - line->p1.y));
    out->p2.x = _cairo_fixed_16_16_from_double (
	p1.x + m * _cairo_fixed_to_double (bottom - line->p1.y));
}

static pixman_trapezoid_t *
traps_to_pixman_trapezoids (const cairo_traps_t *traps)
{
    pixman_trapezoid_t *ptraps;
    int i;

    if (!(ptraps = malloc (traps->num_traps * sizeof (pixman_trapezoid_t))))
	return NULL;

    for (i = 0; i < traps->num_traps; ++i)
    {
	cairo_trapezoid_t *trap = &(traps->traps[i]);
	pixman_trapezoid_t *ptrap = &ptraps[i];

	/* top/bottom will be clamped to surface bounds */
	ptrap->top = _cairo_fixed_to_16_16 (trap->top);
	ptrap->bottom = _cairo_fixed_to_16_16 (trap->bottom);
    
	/* However, all the other coordinates will have been left
	 * untouched so as not to introduce numerical error. Recompute
	 * them if they
	 * exceed the 16.16 limits.
	 */
	if (unlikely (line_exceeds_16_16 (&trap->left)))
	{
	    project_line_x_onto_16_16 (
		&trap->left, trap->top, trap->bottom, &ptrap->left);
	    
	    ptrap->left.p1.y = ptrap->top;
	    ptrap->left.p2.y = ptrap->bottom;
	}
	else
	{
	    ptrap->left.p1.x = _cairo_fixed_to_16_16 (trap->left.p1.x);
	    ptrap->left.p1.y = _cairo_fixed_to_16_16 (trap->left.p1.y);
	    ptrap->left.p2.x = _cairo_fixed_to_16_16 (trap->left.p2.x);
	    ptrap->left.p2.y = _cairo_fixed_to_16_16 (trap->left.p2.y);
	}
    
	if (unlikely (line_exceeds_16_16 (&trap->right)))
	{
	    project_line_x_onto_16_16 (
		&trap->right, trap->top, trap->bottom, &ptrap->right);
	    ptrap->right.p1.y = ptrap->top;
	    ptrap->right.p2.y = ptrap->bottom;
	}
	else
	{
	    ptrap->right.p1.x = _cairo_fixed_to_16_16 (trap->right.p1.x);
	    ptrap->right.p1.y = _cairo_fixed_to_16_16 (trap->right.p1.y);
	    ptrap->right.p2.x = _cairo_fixed_to_16_16 (trap->right.p2.x);
	    ptrap->right.p2.y = _cairo_fixed_to_16_16 (trap->right.p2.y);
	}
    }

    return ptraps;
}

static cairo_int_status_t
create_clip_image (const cairo_clip_t *clip, pixman_image_t **clip_image)
{
    cairo_int_status_t status;
    pixman_trapezoid_t *ptraps = NULL;
    pixman_image_t *white_img = NULL;
    cairo_clip_path_t *clip_path;
    int i;

    status = CAIRO_INT_STATUS_SUCCESS;
    
    if (!clip)
    {
	*clip_image = NULL;
	goto out;
    }
    else if (!(*clip_image = pixman_image_create_bits (
		   PIXMAN_a8, clip->extents.width, clip->extents.height,
		   NULL, -1)))
    {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto out;
    }

    /* First add the boxes */
    if (!(ptraps = malloc (clip->num_boxes * sizeof (pixman_trapezoid_t))))
    {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto out;
    }
    
    for (i = 0; i < clip->num_boxes; ++i)
    {
	cairo_box_t *box = &(clip->boxes[i]);
	pixman_trapezoid_t *trap = &(ptraps[i]);
	
	trap->top = _cairo_fixed_to_16_16 (box->p1.y);
	trap->bottom = _cairo_fixed_to_16_16 (box->p2.y);
	
	trap->left.p1.x = _cairo_fixed_to_16_16 (box->p1.x);
	trap->left.p1.y = _cairo_fixed_to_16_16 (box->p1.y);
	trap->left.p2.x = _cairo_fixed_to_16_16 (box->p1.x);
	trap->left.p2.y = _cairo_fixed_to_16_16 (box->p2.y);
	
	trap->right.p1.x = _cairo_fixed_to_16_16 (box->p2.x);
	trap->right.p1.y = _cairo_fixed_to_16_16 (box->p1.y);
	trap->right.p2.x = _cairo_fixed_to_16_16 (box->p2.x);
	trap->right.p2.y = _cairo_fixed_to_16_16 (box->p2.y);
    }
    
    pixman_add_trapezoids (*clip_image, 0, 0, clip->num_boxes, ptraps);
    
    /* Then intersect with all the paths */
    if (!(white_img = pixman_image_create_solid_fill (&white)))
    {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto out;
    }
    
    for (clip_path = clip->path; clip_path != NULL; clip_path = clip_path->prev)
    {
	cairo_polygon_t polygon;
	cairo_traps_t traps;
	pixman_trapezoid_t *ptraps = NULL;
	
	_cairo_polygon_init (&polygon, NULL, 0);
	_cairo_traps_init (&traps);
	
	status = _cairo_path_fixed_fill_to_polygon (
	    &clip_path->path, clip_path->tolerance, &polygon);

	if (status != CAIRO_INT_STATUS_SUCCESS)
	    goto exit_loop;

	status = _cairo_bentley_ottmann_tessellate_polygon (
	    &traps, &polygon, clip_path->fill_rule);
	
	if (status != CAIRO_INT_STATUS_SUCCESS)
	    goto exit_loop;

	if (!(ptraps = traps_to_pixman_trapezoids (&traps)))
	{
	    status = CAIRO_INT_STATUS_NO_MEMORY;
	    goto exit_loop;
	}
	
	pixman_composite_trapezoids (
	    PIXMAN_OP_IN, white_img, *clip_image, PIXMAN_a8, 0, 0, 0, 0,
	    traps.num_traps, ptraps);

    exit_loop:
	free (ptraps);
	_cairo_polygon_fini (&polygon);
	_cairo_traps_fini (&traps);

	if (status != CAIRO_INT_STATUS_SUCCESS)
	    break;
    }

out:
    if (status != CAIRO_INT_STATUS_SUCCESS && *clip_image)
	pixman_image_unref (*clip_image);
    
    if (white_img)
	pixman_image_unref (white_img);

    free (ptraps);
    
    return status;
}

static pixman_op_t
_pixman_operator (cairo_operator_t op)
{
    switch ((int) op) {
    case CAIRO_OPERATOR_CLEAR:
	return PIXMAN_OP_CLEAR;

    case CAIRO_OPERATOR_SOURCE:
	return PIXMAN_OP_SRC;
    case CAIRO_OPERATOR_OVER:
	return PIXMAN_OP_OVER;
    case CAIRO_OPERATOR_IN:
	return PIXMAN_OP_IN;
    case CAIRO_OPERATOR_OUT:
	return PIXMAN_OP_OUT;
    case CAIRO_OPERATOR_ATOP:
	return PIXMAN_OP_ATOP;

    case CAIRO_OPERATOR_DEST:
	return PIXMAN_OP_DST;
    case CAIRO_OPERATOR_DEST_OVER:
	return PIXMAN_OP_OVER_REVERSE;
    case CAIRO_OPERATOR_DEST_IN:
	return PIXMAN_OP_IN_REVERSE;
    case CAIRO_OPERATOR_DEST_OUT:
	return PIXMAN_OP_OUT_REVERSE;
    case CAIRO_OPERATOR_DEST_ATOP:
	return PIXMAN_OP_ATOP_REVERSE;

    case CAIRO_OPERATOR_XOR:
	return PIXMAN_OP_XOR;
    case CAIRO_OPERATOR_ADD:
	return PIXMAN_OP_ADD;
    case CAIRO_OPERATOR_SATURATE:
	return PIXMAN_OP_SATURATE;

    case CAIRO_OPERATOR_MULTIPLY:
	return PIXMAN_OP_MULTIPLY;
    case CAIRO_OPERATOR_SCREEN:
	return PIXMAN_OP_SCREEN;
    case CAIRO_OPERATOR_OVERLAY:
	return PIXMAN_OP_OVERLAY;
    case CAIRO_OPERATOR_DARKEN:
	return PIXMAN_OP_DARKEN;
    case CAIRO_OPERATOR_LIGHTEN:
	return PIXMAN_OP_LIGHTEN;
    case CAIRO_OPERATOR_COLOR_DODGE:
	return PIXMAN_OP_COLOR_DODGE;
    case CAIRO_OPERATOR_COLOR_BURN:
	return PIXMAN_OP_COLOR_BURN;
    case CAIRO_OPERATOR_HARD_LIGHT:
	return PIXMAN_OP_HARD_LIGHT;
    case CAIRO_OPERATOR_SOFT_LIGHT:
	return PIXMAN_OP_SOFT_LIGHT;
    case CAIRO_OPERATOR_DIFFERENCE:
	return PIXMAN_OP_DIFFERENCE;
    case CAIRO_OPERATOR_EXCLUSION:
	return PIXMAN_OP_EXCLUSION;
    case CAIRO_OPERATOR_HSL_HUE:
	return PIXMAN_OP_HSL_HUE;
    case CAIRO_OPERATOR_HSL_SATURATION:
	return PIXMAN_OP_HSL_SATURATION;
    case CAIRO_OPERATOR_HSL_COLOR:
	return PIXMAN_OP_HSL_COLOR;
    case CAIRO_OPERATOR_HSL_LUMINOSITY:
	return PIXMAN_OP_HSL_LUMINOSITY;

    default:
	ASSERT_NOT_REACHED;
	return PIXMAN_OP_OVER;
    }
}

static cairo_int_status_t
combine_mask_and_clip (pixman_image_t *mask_image, pixman_image_t *clip_image,
		       pixman_image_t **combined)
{
    if (mask_image && clip_image)
    {
	int width = pixman_image_get_width (mask_image);
	int height = pixman_image_get_height (mask_image);
	
	if (!(*combined = pixman_image_create_bits (
		  pixman_image_get_format (mask_image),
		  width, height, NULL, -1)))
	{
	    return CAIRO_INT_STATUS_NO_MEMORY;
	}
	    
	pixman_image_composite32 (
	    PIXMAN_OP_SRC,
	    clip_image, mask_image, *combined,
	    0, 0, 0, 0, 0, 0, width, height);

	pixman_image_set_component_alpha (
	    *combined, pixman_image_get_component_alpha (mask_image));
    }
    else if (mask_image)
    {
	*combined = pixman_image_ref (mask_image);
    }
    else if (clip_image)
    {
	*combined = pixman_image_ref (clip_image);
    }
    else
    {
	*combined = NULL;
    }

    return CAIRO_INT_STATUS_SUCCESS;
}

static cairo_int_status_t
clip_and_composite (cairo_pixman_surface_t *psurface,
		    cairo_operator_t operator,
		    const cairo_pattern_t *source,
		    pixman_image_t *mask_image,
		    const cairo_clip_t *clip)
{
    pixman_image_t *dest_image = psurface->pimage;
    int width = pixman_image_get_width (dest_image);
    int height = pixman_image_get_height (dest_image);
    pixman_image_t *src_image = NULL;
    pixman_image_t *clip_image = NULL;
    pixman_image_t *combined = NULL;
    cairo_int_status_t status;
    pixman_op_t pop;

    status = CAIRO_INT_STATUS_SUCCESS;

    if (clip && _cairo_clip_is_all_clipped (clip))
	goto out;

    status = create_clip_image (clip, &clip_image);
    if (status != CAIRO_INT_STATUS_SUCCESS)
	goto out;

    status = pimage_from_pattern (source, &src_image);
    if (status != CAIRO_INT_STATUS_SUCCESS)
	goto out;
    
    if (!(status = pimage_from_pattern (source, &src_image)))
	goto out;

    pop = _pixman_operator (operator);

    if (pop == PIXMAN_OP_CLEAR)
    {
	if (clip_image)
	{
	    pixman_image_composite32 (
		PIXMAN_OP_OUT_REVERSE,
		clip_image, mask_image, dest_image,
		0, 0, 0, 0, 0, 0, width, height);
	}
	else
	{
	    pixman_image_composite32 (
		PIXMAN_OP_OUT_REVERSE,
		mask_image, NULL, dest_image,
		0, 0, 0, 0, 0, 0, width, height);
	}
    }
    else if (clip_image				&&
	     (pop == PIXMAN_OP_IN		||
	      pop == PIXMAN_OP_OUT		||
	      pop == PIXMAN_OP_IN_REVERSE	||
	      pop == PIXMAN_OP_ATOP_REVERSE))
    {
	/* First composite to a temporary surface */
	if (!(combined = pixman_image_create_bits (
		  pixman_image_get_format (psurface->pimage),
		  width, height, NULL, -1)))
	{
	    status = CAIRO_INT_STATUS_NO_MEMORY;

	    goto out;
	}

	pixman_image_composite32 (
	    PIXMAN_OP_SRC,
	    dest_image, NULL, combined,
	    0, 0, 0, 0, 0, 0, width, height);

	pixman_image_composite32 (
	    pop,
	    src_image, mask_image, combined,
	    0, 0, 0, 0, 0, 0, width, height);

	/* Then punch out the clip from the destination */
	pixman_image_composite32 (
	    PIXMAN_OP_OUT_REVERSE,
	    clip_image, NULL, dest_image,
	    0, 0, 0, 0, 0, 0, width, height);

	/* And add the pieces together */
	pixman_image_composite32 (
	    PIXMAN_OP_ADD,
	    combined, clip_image, dest_image,
	    0, 0, 0, 0, 0, 0, width, height);
    }
    else if (pop == PIXMAN_OP_SRC)
    {
	status = combine_mask_and_clip (mask_image, clip_image, &combined);
	if (status != CAIRO_INT_STATUS_SUCCESS)
	    goto out;

	pixman_image_composite32 (
	    PIXMAN_OP_OUT_REVERSE,
	    combined, NULL, dest_image,
	    0, 0, 0, 0, 0, 0, width, height);

	pixman_image_composite32 (
	    PIXMAN_OP_ADD,
	    src_image, combined, dest_image,
	    0, 0, 0, 0, 0, 0, width, height);
    }
    else
    {
	status = combine_mask_and_clip (mask_image, clip_image, &combined);
	if (status != CAIRO_INT_STATUS_SUCCESS)
	    goto out;

	pixman_image_composite32 (
	    pop,
	    src_image, combined, dest_image,
	    0, 0, 0, 0, 0, 0, width, height);
    }

out:
    if (combined)
	pixman_image_unref (combined);
    if (clip_image)
	pixman_image_unref (clip_image);
    if (src_image)
	pixman_image_unref (src_image);
    
    return status;
}

static cairo_int_status_t
cairo_pixman_surface_paint (void			*abstract_surface,
			    cairo_operator_t		 op,
			    const cairo_pattern_t	*source,
			    const cairo_clip_t		*clip)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    
    return clip_and_composite (psurface, op, source, NULL, clip);
}

static cairo_int_status_t
cairo_pixman_surface_mask (void				*abstract_surface,
			   cairo_operator_t		 op,
			   const cairo_pattern_t	*source,
			   const cairo_pattern_t	*mask,
			   const cairo_clip_t		*clip)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    pixman_image_t *mask_image;
    cairo_int_status_t status;

    status = pimage_from_pattern (mask, &mask_image);
    if (status == CAIRO_INT_STATUS_SUCCESS)
    {
	status = clip_and_composite (psurface, op, source, mask_image, clip);

	pixman_image_unref (mask_image);
    }

    return status;
}

static cairo_int_status_t
clip_and_composite_traps (cairo_pixman_surface_t *psurface,
			  cairo_operator_t operator,
			  const cairo_pattern_t *source,
			  const cairo_traps_t *traps,
			  cairo_antialias_t antialias,
			  const cairo_clip_t *clip)
{
    cairo_int_status_t status;
    pixman_trapezoid_t *ptraps = NULL;
    pixman_image_t *mask_image = NULL;
    pixman_format_code_t format;
    int w, h;

    w = pixman_image_get_width (psurface->pimage);
    h = pixman_image_get_height (psurface->pimage);

    switch (antialias)
    {
    case CAIRO_ANTIALIAS_NONE:
	format = PIXMAN_a1;
	break;

    case CAIRO_ANTIALIAS_FAST:
	format = PIXMAN_a4;
	break;

    default:
    case CAIRO_ANTIALIAS_GRAY:
    case CAIRO_ANTIALIAS_DEFAULT:
    case CAIRO_ANTIALIAS_SUBPIXEL:
    case CAIRO_ANTIALIAS_GOOD:
    case CAIRO_ANTIALIAS_BEST:
	format = PIXMAN_a8;
	break;
    }

    status = CAIRO_INT_STATUS_NO_MEMORY;

    if (!(mask_image = pixman_image_create_bits (format, w, h, NULL, -1)))
	goto out;

    if (!(ptraps = traps_to_pixman_trapezoids (traps)))
	goto out;
    
    status = CAIRO_INT_STATUS_SUCCESS;

    pixman_add_trapezoids (mask_image, 0, 0, traps->num_traps, ptraps);

    status = clip_and_composite (psurface, operator, source, mask_image, clip);

out:
    free (ptraps);

    if (mask_image)
	pixman_image_unref (mask_image);

    return status;
}

static cairo_int_status_t
cairo_pixman_surface_stroke (void			*abstract_surface,
			     cairo_operator_t		 op,
			     const cairo_pattern_t	*source,
			     const cairo_path_fixed_t	*path,
			     const cairo_stroke_style_t	*style,
			     const cairo_matrix_t	*ctm,
			     const cairo_matrix_t	*ctm_inverse,
			     double			 tolerance,
			     cairo_antialias_t		 antialias,
			     const cairo_clip_t		*clip)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    cairo_int_status_t status;
    cairo_traps_t traps;

    _cairo_traps_init (&traps);

    status = _cairo_path_fixed_stroke_to_traps (
	path, style, ctm, ctm_inverse, tolerance, &traps);

    if (status == CAIRO_INT_STATUS_SUCCESS)
    {
	status = clip_and_composite_traps (
	    psurface, op, source, &traps, antialias, clip);
    }

    _cairo_traps_fini (&traps);
    return status;
}

static cairo_int_status_t
cairo_pixman_surface_fill (void				*abstract_surface,
			   cairo_operator_t		 op,
			   const cairo_pattern_t	*source,
			   const cairo_path_fixed_t	*path,
			   cairo_fill_rule_t		 fill_rule,
			   double			 tolerance,
			   cairo_antialias_t		 antialias,
			   const cairo_clip_t		*clip)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    cairo_int_status_t status;
    cairo_traps_t traps;

    _cairo_traps_init (&traps);

    status = _cairo_path_fixed_fill_to_traps (
	path, fill_rule, tolerance, &traps);

    if (status == CAIRO_INT_STATUS_SUCCESS)
    {
	status = clip_and_composite_traps (
	    psurface, op, source, &traps, antialias, clip);
    }

    _cairo_traps_fini (&traps);
    return status;
}

static pixman_glyph_cache_t *global_glyph_cache;

static inline pixman_glyph_cache_t *
get_glyph_cache (void)
{
    if (!global_glyph_cache)
        global_glyph_cache = pixman_glyph_cache_create ();

    return global_glyph_cache;
}

/* FIXME: make sure this gets called when glyphs are finalized */
void
_cairo_pixman_surface_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
					 cairo_scaled_glyph_t *scaled_glyph)
{
    CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);

    if (global_glyph_cache) {
        pixman_glyph_cache_remove (
            global_glyph_cache, scaled_font,
            (void *)_cairo_scaled_glyph_index (scaled_glyph));
    }

    CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
}

static cairo_int_status_t
cairo_pixman_surface_glyphs (void			*abstract_surface,
			     cairo_operator_t		 operator,
			     const cairo_pattern_t	*source,
			     cairo_glyph_t		*glyphs,
			     int			 num_glyphs,
			     cairo_scaled_font_t	*scaled_font,
			     const cairo_clip_t		*clip)
{
    cairo_pixman_surface_t *psurface = abstract_surface;
    cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
    pixman_glyph_cache_t *glyph_cache;
    pixman_glyph_t pglyphs_stack[CAIRO_STACK_ARRAY_LENGTH (pixman_glyph_t)];
    pixman_glyph_t *pglyphs = pglyphs_stack;
    pixman_format_code_t mask_format;
    pixman_image_t *white_image = NULL;
    pixman_image_t *mask_image = NULL;
    int width, height;
    pixman_glyph_t *pg;
    int i;

    width = pixman_image_get_width (psurface->pimage);
    height = pixman_image_get_height (psurface->pimage);

    if (!(white_image = pixman_image_create_solid_fill (&white)))
	return CAIRO_INT_STATUS_NO_MEMORY;

    CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);

    glyph_cache = get_glyph_cache();
    if (unlikely (glyph_cache == NULL)) {
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
        goto out_unlock;
    }
    pixman_glyph_cache_freeze (glyph_cache);

    if (num_glyphs > ARRAY_LENGTH (pglyphs_stack)) {
        pglyphs = _cairo_malloc_ab (num_glyphs, sizeof (pixman_glyph_t));
        if (unlikely (pglyphs == NULL)) {
            status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
            goto out_thaw;
        }
    }

    pg = pglyphs;
    for (i = 0; i < num_glyphs; i++)
    {
        unsigned long index = glyphs[i].index;
        const void *glyph;

        glyph = pixman_glyph_cache_lookup (glyph_cache, scaled_font, (void *)index);
        if (!glyph)
	{
            cairo_scaled_glyph_t *scaled_glyph;
            cairo_image_surface_t *glyph_surface;

            /* This call can actually end up recursing, so we have to
             * drop the mutex around it.
             */
            CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
            status = _cairo_scaled_glyph_lookup (scaled_font, index,
                                                 CAIRO_SCALED_GLYPH_INFO_SURFACE,
                                                 &scaled_glyph);
            CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);

            if (unlikely (status))
                goto out_thaw;

	    glyph_surface = scaled_glyph->surface;
            glyph = pixman_glyph_cache_insert (glyph_cache, scaled_font, (void *)index,
                                               glyph_surface->base.device_transform.x0,
                                               glyph_surface->base.device_transform.y0,
                                               glyph_surface->pixman_image);
            if (unlikely (!glyph))
	    {
                status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
                goto out_thaw;
            }
        }

        pg->x = _cairo_lround (glyphs[i].x);
        pg->y = _cairo_lround (glyphs[i].y);
        pg->glyph = glyph;
        pg++;
    }

    mask_format = pixman_glyph_get_mask_format (glyph_cache, pg - pglyphs, pglyphs);

    mask_image = pixman_image_create_bits (mask_format, width, height, NULL, -1);
    if (!mask_image)
    {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto out_thaw;
    }

    pixman_composite_glyphs (PIXMAN_OP_ADD,
			     white_image, mask_image,
			     mask_format,
			     0, 0, 0, 0, 0, 0, width, height,
			     glyph_cache, pg - pglyphs, pglyphs);

    status = clip_and_composite (psurface, operator, source, mask_image, clip);
    
out_thaw:
    if (mask_image)
	pixman_image_unref (mask_image);
    
    pixman_image_unref (white_image);
    
    pixman_glyph_cache_thaw (glyph_cache);

    if (pglyphs != pglyphs_stack)
        free (pglyphs);

out_unlock:
    CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
    return status;
}

const cairo_surface_backend_t cairo_pixman_surface_backend =
{
    CAIRO_SURFACE_TYPE_IMAGE,
    cairo_pixman_surface_finish,
    
    _cairo_default_context_create,
    
    cairo_pixman_surface_create_similar,
    NULL, /* create similar image */
    cairo_pixman_surface_map_to_image,
    cairo_pixman_surface_unmap_image,
    
    cairo_pixman_surface_source,
    cairo_pixman_surface_acquire_source_image,
    cairo_pixman_surface_release_source_image,
    cairo_pixman_surface_snapshot,
    
    NULL, /* copy_page */
    NULL, /* show_page */
    
    cairo_pixman_surface_get_extents,
    cairo_pixman_surface_get_font_options,
    
    NULL, /* flush */
    NULL,
    
    cairo_pixman_surface_paint,
    cairo_pixman_surface_mask,
    cairo_pixman_surface_stroke,
    cairo_pixman_surface_fill,
    NULL, /* fill-stroke */
    cairo_pixman_surface_glyphs,
};