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
|
/*
* Copyright © 2004 Red Hat, Inc.
*
* 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
* Red Hat, Inc. not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Red Hat, Inc. makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL RED HAT, INC. 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: Carl D. Worth <cworth@cworth.org>
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>
#if HAVE_FCFINI
#include <fontconfig/fontconfig.h>
#endif
#include "cairo-test.h"
#include "buffer-diff.h"
#include "read-png.h"
#include "write-png.h"
#include "xmalloc.h"
#ifdef _MSC_VER
#define vsnprintf _vsnprintf
#define access _access
#define F_OK 0
#endif
static void
xunlink (const char *pathname);
#define CAIRO_TEST_LOG_SUFFIX ".log"
#define CAIRO_TEST_PNG_SUFFIX "-out.png"
#define CAIRO_TEST_REF_SUFFIX "-ref.png"
#define CAIRO_TEST_RGB24_REF_SUFFIX "-rgb24-ref.png"
#define CAIRO_TEST_DIFF_SUFFIX "-diff.png"
/* Static data is messy, but we're coding for tests here, not a
* general-purpose library, and it keeps the tests cleaner to avoid a
* context object there, (though not a whole lot). */
FILE *cairo_test_log_file;
void
cairo_test_init (const char *test_name)
{
char *log_name;
xasprintf (&log_name, "%s%s", test_name, CAIRO_TEST_LOG_SUFFIX);
xunlink (log_name);
cairo_test_log_file = fopen (log_name, "a");
if (cairo_test_log_file == NULL) {
fprintf (stderr, "Error opening log file: %s\n", log_name);
cairo_test_log_file = stderr;
}
free (log_name);
}
void
cairo_test_log (const char *fmt, ...)
{
va_list va;
va_start (va, fmt);
vfprintf (cairo_test_log_file, fmt, va);
va_end (va);
}
void
xasprintf (char **strp, const char *fmt, ...)
{
#ifdef HAVE_VASPRINTF
va_list va;
int ret;
va_start (va, fmt);
ret = vasprintf (strp, fmt, va);
va_end (va);
if (ret < 0) {
cairo_test_log ("Out of memory\n");
exit (1);
}
#else /* !HAVE_VASNPRINTF */
#define BUF_SIZE 1024
va_list va;
char buffer[BUF_SIZE];
int ret;
va_start (va, fmt);
ret = vsnprintf (buffer, sizeof(buffer), fmt, va);
va_end (va);
if (ret < 0) {
cairo_test_log ("Failure in vsnprintf\n");
exit (1);
}
if (strlen (buffer) == sizeof(buffer) - 1) {
cairo_test_log ("Overflowed fixed buffer\n");
exit (1);
}
*strp = strdup (buffer);
if (!*strp) {
cairo_test_log ("Out of memory\n");
exit (1);
}
#endif /* !HAVE_VASNPRINTF */
}
static void
xunlink (const char *pathname)
{
if (unlink (pathname) < 0 && errno != ENOENT) {
cairo_test_log (" Error: Cannot remove %s: %s\n",
pathname, strerror (errno));
exit (1);
}
}
typedef cairo_surface_t *
(*cairo_test_create_target_surface_t) (cairo_test_t *test, cairo_format_t format,
void **closure);
typedef cairo_status_t
(*cairo_test_write_to_png_t) (cairo_surface_t *surface, const char *filename);
typedef void
(*cairo_test_cleanup_target_t) (void *closure);
typedef struct _cairo_test_target
{
const char *name;
cairo_format_t reference_format;
cairo_test_create_target_surface_t create_target_surface;
cairo_test_write_to_png_t write_to_png;
cairo_test_cleanup_target_t cleanup_target;
void *closure;
} cairo_test_target_t;
static char *
cairo_target_format_name (const cairo_test_target_t *target)
{
char *format;
if (target->reference_format == CAIRO_FORMAT_RGB24)
format = "rgb24";
else
format = "argb32";
return format;
}
static cairo_surface_t *
create_image_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
*closure = NULL;
return cairo_image_surface_create (format, test->width, test->height);
}
#ifdef CAIRO_HAS_TEST_SURFACES
#include "test-fallback-surface.h"
#include "test-meta-surface.h"
#include "test-paginated-surface.h"
static cairo_surface_t *
create_test_fallback_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
*closure = NULL;
return _test_fallback_surface_create (format, test->width, test->height);
}
static cairo_surface_t *
create_test_meta_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
*closure = NULL;
return _test_meta_surface_create (format, test->width, test->height);
}
static const cairo_user_data_key_t test_paginated_closure_key;
typedef struct {
unsigned char *data;
cairo_format_t format;
int width;
int height;
int stride;
} test_paginated_closure_t;
static cairo_surface_t *
create_test_paginated_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
test_paginated_closure_t *tpc;
cairo_surface_t *surface;
*closure = tpc = xmalloc (sizeof (test_paginated_closure_t));
tpc->format = format;
tpc->width = test->width;
tpc->height = test->height;
tpc->stride = test->width * 4;
tpc->data = xcalloc (tpc->stride * test->height, 1);
surface = _test_paginated_surface_create_for_data (tpc->data,
tpc->format,
tpc->width,
tpc->height,
tpc->stride);
cairo_surface_set_user_data (surface, &test_paginated_closure_key,
tpc, NULL);
return surface;
}
/* The only reason we go through all these machinations to write a PNG
* image is to _really ensure_ that the data actually landed in our
* buffer through the paginated surface to the test_paginated_surface.
*
* If we didn't implement this function then the default
* cairo_surface_write_to_png would result in the paginated_surface's
* acquire_source_image function replaying the meta-surface to an
* intermediate image surface. And in that case the
* test_paginated_surface would not be involved and wouldn't be
* tested.
*/
static cairo_status_t
test_paginated_write_to_png (cairo_surface_t *surface,
const char *filename)
{
cairo_surface_t *image;
test_paginated_closure_t *tpc;
tpc = cairo_surface_get_user_data (surface, &test_paginated_closure_key);
image = cairo_image_surface_create_for_data (tpc->data,
tpc->format,
tpc->width,
tpc->height,
tpc->stride);
cairo_surface_write_to_png (image, filename);
cairo_surface_destroy (image);
return CAIRO_STATUS_SUCCESS;
}
static void
cleanup_test_paginated (void *closure)
{
test_paginated_closure_t *tpc = closure;
free (tpc->data);
free (tpc);
}
#endif
#ifdef CAIRO_HAS_GLITZ_SURFACE
#include <glitz.h>
#include <cairo-glitz.h>
static const cairo_user_data_key_t glitz_closure_key;
typedef struct _glitz_target_closure_base {
int width;
int height;
cairo_format_t format;
} glitz_target_closure_base_t;
#if CAIRO_CAN_TEST_GLITZ_GLX_SURFACE
#include <glitz-glx.h>
typedef struct _glitz_glx_target_closure {
glitz_target_closure_base_t base;
Display *dpy;
int scr;
Window win;
} glitz_glx_target_closure_t;
static glitz_surface_t *
create_glitz_glx_surface (glitz_format_name_t formatname,
int width,
int height,
glitz_glx_target_closure_t *closure)
{
Display * dpy = closure->dpy;
int scr = closure->scr;
glitz_drawable_format_t templ;
glitz_drawable_format_t * dformat = NULL;
unsigned long mask;
glitz_drawable_t * drawable = NULL;
glitz_format_t * format;
glitz_surface_t * sr;
XSizeHints xsh;
XSetWindowAttributes xswa;
XVisualInfo * vinfo;
memset(&templ, 0, sizeof(templ));
templ.color.red_size = 8;
templ.color.green_size = 8;
templ.color.blue_size = 8;
templ.color.alpha_size = 8;
templ.color.fourcc = GLITZ_FOURCC_RGB;
templ.samples = 1;
glitz_glx_init (NULL);
mask = GLITZ_FORMAT_SAMPLES_MASK | GLITZ_FORMAT_FOURCC_MASK |
GLITZ_FORMAT_RED_SIZE_MASK | GLITZ_FORMAT_GREEN_SIZE_MASK |
GLITZ_FORMAT_BLUE_SIZE_MASK;
if (formatname == GLITZ_STANDARD_ARGB32)
mask |= GLITZ_FORMAT_ALPHA_SIZE_MASK;
/* Try for a pbuffer first */
if (!getenv("CAIRO_TEST_FORCE_GLITZ_WINDOW"))
dformat = glitz_glx_find_pbuffer_format (dpy, scr, mask, &templ, 0);
if (dformat) {
closure->win = None;
drawable = glitz_glx_create_pbuffer_drawable (dpy, scr, dformat,
width, height);
if (!drawable)
goto FAIL;
} else {
/* No pbuffer, try window */
dformat = glitz_glx_find_window_format (dpy, scr, mask, &templ, 0);
if (!dformat)
goto FAIL;
vinfo = glitz_glx_get_visual_info_from_format(dpy,
DefaultScreen(dpy),
dformat);
if (!vinfo)
goto FAIL;
xsh.flags = PSize;
xsh.x = 0;
xsh.y = 0;
xsh.width = width;
xsh.height = height;
xswa.colormap = XCreateColormap (dpy, RootWindow(dpy, scr),
vinfo->visual, AllocNone);
closure->win = XCreateWindow (dpy, RootWindow(dpy, scr),
xsh.x, xsh.y, xsh.width, xsh.height,
0, vinfo->depth, CopyFromParent,
vinfo->visual, CWColormap, &xswa);
XFree (vinfo);
drawable =
glitz_glx_create_drawable_for_window (dpy, scr,
dformat, closure->win,
width, height);
if (!drawable)
goto DESTROY_WINDOW;
}
format = glitz_find_standard_format (drawable, formatname);
if (!format)
goto DESTROY_DRAWABLE;
sr = glitz_surface_create (drawable, format, width, height, 0, NULL);
if (!sr)
goto DESTROY_DRAWABLE;
if (closure->win == None || dformat->doublebuffer) {
glitz_surface_attach (sr, drawable, GLITZ_DRAWABLE_BUFFER_BACK_COLOR);
} else {
XMapWindow (closure->dpy, closure->win);
glitz_surface_attach (sr, drawable, GLITZ_DRAWABLE_BUFFER_FRONT_COLOR);
}
glitz_drawable_destroy (drawable);
return sr;
DESTROY_DRAWABLE:
glitz_drawable_destroy (drawable);
DESTROY_WINDOW:
if (closure->win)
XDestroyWindow (dpy, closure->win);
FAIL:
return NULL;
}
static cairo_surface_t *
create_cairo_glitz_glx_surface (cairo_test_t *test,
cairo_format_t format,
void **closure)
{
int width = test->width;
int height = test->height;
glitz_glx_target_closure_t *gxtc;
glitz_surface_t * glitz_surface;
cairo_surface_t * surface;
*closure = gxtc = xmalloc (sizeof (glitz_glx_target_closure_t));
if (width == 0)
width = 1;
if (height == 0)
height = 1;
gxtc->dpy = XOpenDisplay (getenv("CAIRO_TEST_GLITZ_DISPLAY"));
if (!gxtc->dpy) {
cairo_test_log ("Failed to open display: %s\n", XDisplayName(0));
goto FAIL;
}
XSynchronize (gxtc->dpy, 1);
gxtc->scr = DefaultScreen(gxtc->dpy);
switch (format) {
case CAIRO_FORMAT_RGB24:
glitz_surface = create_glitz_glx_surface (GLITZ_STANDARD_RGB24, width, height, gxtc);
break;
case CAIRO_FORMAT_ARGB32:
glitz_surface = create_glitz_glx_surface (GLITZ_STANDARD_ARGB32, width, height, gxtc);
break;
default:
cairo_test_log ("Invalid format for glitz-glx test: %d\n", format);
goto FAIL_CLOSE_DISPLAY;
}
if (!glitz_surface) {
cairo_test_log ("Failed to create glitz-glx surface\n");
goto FAIL_CLOSE_DISPLAY;
}
surface = cairo_glitz_surface_create (glitz_surface);
gxtc->base.width = test->width;
gxtc->base.height = test->height;
gxtc->base.format = format;
cairo_surface_set_user_data (surface, &glitz_closure_key,
gxtc, NULL);
return surface;
FAIL_CLOSE_DISPLAY:
XCloseDisplay (gxtc->dpy);
FAIL:
return NULL;
}
static void
cleanup_cairo_glitz_glx (void *closure)
{
glitz_glx_target_closure_t *gxtc = closure;
glitz_glx_fini ();
if (gxtc->win)
XDestroyWindow (gxtc->dpy, gxtc->win);
XCloseDisplay (gxtc->dpy);
free (gxtc);
}
#endif /* CAIRO_CAN_TEST_GLITZ_GLX_SURFACE */
#if CAIRO_CAN_TEST_GLITZ_AGL_SURFACE
#include <glitz-agl.h>
typedef struct _glitz_agl_target_closure {
glitz_target_closure_base_t base;
} glitz_agl_target_closure_t;
static glitz_surface_t *
create_glitz_agl_surface (glitz_format_name_t formatname,
int width, int height,
glitz_agl_target_closure_t *closure)
{
glitz_drawable_format_t *dformat;
glitz_drawable_format_t templ;
glitz_drawable_t *gdraw;
glitz_format_t *format;
glitz_surface_t *sr = NULL;
unsigned long mask;
memset(&templ, 0, sizeof(templ));
templ.color.red_size = 8;
templ.color.green_size = 8;
templ.color.blue_size = 8;
templ.color.alpha_size = 8;
templ.color.fourcc = GLITZ_FOURCC_RGB;
templ.samples = 1;
mask = GLITZ_FORMAT_SAMPLES_MASK | GLITZ_FORMAT_FOURCC_MASK |
GLITZ_FORMAT_RED_SIZE_MASK | GLITZ_FORMAT_GREEN_SIZE_MASK |
GLITZ_FORMAT_BLUE_SIZE_MASK;
if (formatname == GLITZ_STANDARD_ARGB32)
mask |= GLITZ_FORMAT_ALPHA_SIZE_MASK;
dformat = glitz_agl_find_pbuffer_format (mask, &templ, 0);
if (!dformat) {
cairo_test_log ("Glitz failed to find pbuffer format for template.");
goto FAIL;
}
gdraw = glitz_agl_create_pbuffer_drawable (dformat, width, height);
if (!gdraw) {
cairo_test_log ("Glitz failed to create pbuffer drawable.");
goto FAIL;
}
format = glitz_find_standard_format (gdraw, formatname);
if (!format) {
cairo_test_log ("Glitz failed to find standard format for drawable.");
goto DESTROY_DRAWABLE;
}
sr = glitz_surface_create (gdraw, format, width, height, 0, NULL);
if (!sr) {
cairo_test_log ("Glitz failed to create a surface.");
goto DESTROY_DRAWABLE;
}
glitz_surface_attach (sr, gdraw, GLITZ_DRAWABLE_BUFFER_FRONT_COLOR);
DESTROY_DRAWABLE:
glitz_drawable_destroy (gdraw);
FAIL:
return sr; /* will be NULL unless we create it and attach */
}
static cairo_surface_t *
create_cairo_glitz_agl_surface (cairo_test_t *test,
cairo_format_t format,
void **closure)
{
glitz_surface_t *glitz_surface;
cairo_surface_t *surface;
glitz_agl_target_closure_t *aglc;
glitz_agl_init ();
*closure = aglc = xmalloc (sizeof (glitz_agl_target_closure_t));
switch (format) {
case CAIRO_FORMAT_RGB24:
glitz_surface = create_glitz_agl_surface (GLITZ_STANDARD_RGB24, test->width, test->height, NULL);
break;
case CAIRO_FORMAT_ARGB32:
glitz_surface = create_glitz_agl_surface (GLITZ_STANDARD_ARGB32, test->width, test->height, NULL);
break;
default:
cairo_test_log ("Invalid format for glitz-agl test: %d\n", format);
goto FAIL;
}
if (!glitz_surface)
goto FAIL;
surface = cairo_glitz_surface_create (glitz_surface);
aglc->base.width = test->width;
aglc->base.height = test->height;
aglc->base.format = format;
cairo_surface_set_user_data (surface, &glitz_closure_key, aglc, NULL);
return surface;
FAIL:
return NULL;
}
static void
cleanup_cairo_glitz_agl (void *closure)
{
free (closure);
glitz_agl_fini ();
}
#endif /* CAIRO_CAN_TEST_GLITZ_AGL_SURFACE */
#if CAIRO_CAN_TEST_GLITZ_WGL_SURFACE
#include <glitz-wgl.h>
typedef struct _glitz_wgl_target_closure {
glitz_target_closure_base_t base;
} glitz_wgl_target_closure_t;
static glitz_surface_t *
create_glitz_wgl_surface (glitz_format_name_t formatname,
int width, int height,
glitz_wgl_target_closure_t *closure)
{
glitz_drawable_format_t *dformat;
glitz_drawable_format_t templ;
glitz_drawable_t *gdraw;
glitz_format_t *format;
glitz_surface_t *sr = NULL;
unsigned long mask;
memset(&templ, 0, sizeof(templ));
templ.color.red_size = 8;
templ.color.green_size = 8;
templ.color.blue_size = 8;
templ.color.alpha_size = 8;
templ.color.fourcc = GLITZ_FOURCC_RGB;
templ.samples = 1;
mask = GLITZ_FORMAT_SAMPLES_MASK | GLITZ_FORMAT_FOURCC_MASK |
GLITZ_FORMAT_RED_SIZE_MASK | GLITZ_FORMAT_GREEN_SIZE_MASK |
GLITZ_FORMAT_BLUE_SIZE_MASK;
if (formatname == GLITZ_STANDARD_ARGB32)
mask |= GLITZ_FORMAT_ALPHA_SIZE_MASK;
dformat = glitz_wgl_find_pbuffer_format (mask, &templ, 0);
if (!dformat) {
cairo_test_log ("Glitz failed to find pbuffer format for template.");
goto FAIL;
}
gdraw = glitz_wgl_create_pbuffer_drawable (dformat, width, height);
if (!gdraw) {
cairo_test_log ("Glitz failed to create pbuffer drawable.");
goto FAIL;
}
format = glitz_find_standard_format (gdraw, formatname);
if (!format) {
cairo_test_log ("Glitz failed to find standard format for drawable.");
goto DESTROY_DRAWABLE;
}
sr = glitz_surface_create (gdraw, format, width, height, 0, NULL);
if (!sr) {
cairo_test_log ("Glitz failed to create a surface.");
goto DESTROY_DRAWABLE;
}
glitz_surface_attach (sr, gdraw, GLITZ_DRAWABLE_BUFFER_FRONT_COLOR);
DESTROY_DRAWABLE:
glitz_drawable_destroy (gdraw);
FAIL:
return sr; /* will be NULL unless we create it and attach */
}
static cairo_surface_t *
create_cairo_glitz_wgl_surface (cairo_test_t *test,
cairo_format_t format,
void **closure)
{
glitz_surface_t *glitz_surface;
cairo_surface_t *surface;
glitz_wgl_target_closure_t *wglc;
glitz_wgl_init (NULL);
*closure = wglc = xmalloc (sizeof (glitz_wgl_target_closure_t));
switch (format) {
case CAIRO_FORMAT_RGB24:
glitz_surface = create_glitz_wgl_surface (GLITZ_STANDARD_RGB24, test->width, test->height, NULL);
break;
case CAIRO_FORMAT_ARGB32:
glitz_surface = create_glitz_wgl_surface (GLITZ_STANDARD_ARGB32, test->width, test->height, NULL);
break;
default:
cairo_test_log ("Invalid format for glitz-wgl test: %d\n", format);
goto FAIL;
}
if (!glitz_surface)
goto FAIL;
surface = cairo_glitz_surface_create (glitz_surface);
wglc->base.width = test->width;
wglc->base.height = test->height;
wglc->base.format = format;
cairo_surface_set_user_data (surface, &glitz_closure_key, wglc, NULL);
return surface;
FAIL:
return NULL;
}
static void
cleanup_cairo_glitz_wgl (void *closure)
{
free (closure);
glitz_wgl_fini ();
}
#endif /* CAIRO_CAN_TEST_GLITZ_WGL_SURFACE */
#endif /* CAIRO_HAS_GLITZ_SURFACE */
#if 0 && CAIRO_HAS_QUARTZ_SURFACE
static cairo_surface_t *
create_quartz_surface (int width, int height, void **closure)
{
#error Not yet implemented
}
static void
cleanup_quartz (void *closure)
{
#error Not yet implemented
}
#endif
/* Testing the win32 surface isn't interesting, since for
* ARGB images it just chains to the image backend
*/
#if CAIRO_HAS_WIN32_SURFACE
#include "cairo-win32.h"
typedef struct _win32_target_closure
{
HDC dc;
HBITMAP bmp;
} win32_target_closure_t;
static cairo_surface_t *
create_win32_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
int width = test->width;
int height = test->height;
BITMAPINFO bmpInfo;
unsigned char *bits = NULL;
win32_target_closure_t *data = malloc(sizeof(win32_target_closure_t));
*closure = data;
data->dc = CreateCompatibleDC(NULL);
/* initialize the bitmapinfoheader */
memset(&bmpInfo.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = width;
bmpInfo.bmiHeader.biHeight = -height;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;
/* create a DIBSection */
data->bmp = CreateDIBSection(data->dc, &bmpInfo, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
/* Flush GDI to make sure the DIBSection is actually created */
GdiFlush();
/* Select the bitmap in to the DC */
SelectObject(data->dc, data->bmp);
return cairo_win32_surface_create(data->dc);
}
static void
cleanup_win32 (void *closure)
{
win32_target_closure_t *data = (win32_target_closure_t*)closure;
DeleteObject(data->bmp);
DeleteDC(data->dc);
free(closure);
}
#endif
#if CAIRO_HAS_XCB_SURFACE
#include "cairo-xcb-xrender.h"
typedef struct _xcb_target_closure
{
XCBConnection *c;
XCBDRAWABLE drawable;
} xcb_target_closure_t;
/* XXX: This is a nasty hack. Something like this should be in XCB's
* bindings for Render, not here in this test. */
static XCBRenderPICTFORMINFO
_format_from_cairo(XCBConnection *c, cairo_format_t fmt)
{
XCBRenderPICTFORMINFO ret = {{ 0 }};
struct tmpl_t {
XCBRenderDIRECTFORMAT direct;
CARD8 depth;
};
static const struct tmpl_t templates[] = {
/* CAIRO_FORMAT_ARGB32 */
{
{
16, 0xff,
8, 0xff,
0, 0xff,
24, 0xff
},
32
},
/* CAIRO_FORMAT_RGB24 */
{
{
16, 0xff,
8, 0xff,
0, 0xff,
0, 0x00
},
24
},
/* CAIRO_FORMAT_A8 */
{
{
0, 0x00,
0, 0x00,
0, 0x00,
0, 0xff
},
8
},
/* CAIRO_FORMAT_A1 */
{
{
0, 0x00,
0, 0x00,
0, 0x00,
0, 0x01
},
1
},
};
const struct tmpl_t *tmpl;
XCBRenderQueryPictFormatsRep *r;
XCBRenderPICTFORMINFOIter fi;
if(fmt < 0 || fmt >= (sizeof(templates) / sizeof(*templates)))
return ret;
tmpl = templates + fmt;
r = XCBRenderQueryPictFormatsReply(c, XCBRenderQueryPictFormats(c), 0);
if(!r)
return ret;
for(fi = XCBRenderQueryPictFormatsFormatsIter(r); fi.rem; XCBRenderPICTFORMINFONext(&fi))
{
const XCBRenderDIRECTFORMAT *t, *f;
if(fi.data->type != XCBRenderPictTypeDirect)
continue;
if(fi.data->depth != tmpl->depth)
continue;
t = &tmpl->direct;
f = &fi.data->direct;
if(t->red_mask && (t->red_mask != f->red_mask || t->red_shift != f->red_shift))
continue;
if(t->green_mask && (t->green_mask != f->green_mask || t->green_shift != f->green_shift))
continue;
if(t->blue_mask && (t->blue_mask != f->blue_mask || t->blue_shift != f->blue_shift))
continue;
if(t->alpha_mask && (t->alpha_mask != f->alpha_mask || t->alpha_shift != f->alpha_shift))
continue;
ret = *fi.data;
}
free(r);
return ret;
}
static cairo_surface_t *
create_xcb_surface (int width, int height, void **closure)
{
XCBSCREEN *root;
xcb_target_closure_t *xtc;
cairo_surface_t *surface;
XCBConnection *c;
XCBRenderPICTFORMINFO render_format;
*closure = xtc = xmalloc (sizeof (xcb_target_closure_t));
if (width == 0)
width = 1;
if (height == 0)
height = 1;
xtc->c = c = XCBConnectBasic();
if (c == NULL) {
cairo_test_log ("Failed to connect to X server through XCB\n");
return NULL;
}
root = XCBConnSetupSuccessRepRootsIter(XCBGetSetup(c)).data;
xtc->drawable.pixmap = XCBPIXMAPNew (c);
{
XCBDRAWABLE root_drawable;
root_drawable.window = root->root;
XCBCreatePixmap (c, 32, xtc->drawable.pixmap, root_drawable,
width, height);
}
render_format = _format_from_cairo (c, CAIRO_FORMAT_ARGB32);
if (render_format.id.xid == 0)
return NULL;
surface = cairo_xcb_surface_create_with_xrender_format (c, xtc->drawable,
&render_format,
width, height);
return surface;
}
static void
cleanup_xcb (void *closure)
{
xcb_target_closure_t *xtc = closure;
XCBFreePixmap (xtc->c, xtc->drawable.pixmap);
XCBDisconnect (xtc->c);
free (xtc);
}
#endif
#if CAIRO_HAS_XLIB_SURFACE
#include "cairo-xlib-xrender.h"
typedef struct _xlib_target_closure
{
Display *dpy;
Pixmap pixmap;
} xlib_target_closure_t;
static cairo_surface_t *
create_xlib_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
int width = test->width;
int height = test->height;
xlib_target_closure_t *xtc;
cairo_surface_t *surface;
Display *dpy;
XRenderPictFormat *xrender_format;
*closure = xtc = xmalloc (sizeof (xlib_target_closure_t));
if (width == 0)
width = 1;
if (height == 0)
height = 1;
xtc->dpy = dpy = XOpenDisplay (NULL);
if (xtc->dpy == NULL) {
cairo_test_log ("Failed to open display: %s\n", XDisplayName(0));
return NULL;
}
XSynchronize (xtc->dpy, 1);
/* XXX: Currently we don't do any xlib testing when the X server
* doesn't have the Render extension. We could do better here,
* (perhaps by converting the tests from ARGB32 to RGB24). One
* step better would be to always test the non-Render fallbacks
* for each test even if the server does have the Render
* extension. That would probably be through another
* cairo_test_target which would use an extended version of
* cairo_test_xlib_disable_render. */
switch (format) {
case CAIRO_FORMAT_ARGB32:
xrender_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
break;
case CAIRO_FORMAT_RGB24:
xrender_format = XRenderFindStandardFormat (dpy, PictStandardRGB24);
break;
default:
cairo_test_log ("Invalid format for xlib test: %d\n", format);
return NULL;
}
if (xrender_format == NULL) {
cairo_test_log ("X server does not have the Render extension.\n");
return NULL;
}
xtc->pixmap = XCreatePixmap (dpy, DefaultRootWindow (dpy),
width, height, xrender_format->depth);
surface = cairo_xlib_surface_create_with_xrender_format (dpy, xtc->pixmap,
DefaultScreenOfDisplay (dpy),
xrender_format,
width, height);
return surface;
}
static void
cleanup_xlib (void *closure)
{
xlib_target_closure_t *xtc = closure;
XFreePixmap (xtc->dpy, xtc->pixmap);
XCloseDisplay (xtc->dpy);
free (xtc);
}
#endif
#if CAIRO_HAS_BEOS_SURFACE
/* BeOS test functions are external as they need to be C++ */
#include "cairo-test-beos.h"
#endif
#if CAIRO_HAS_DIRECTFB_SURFACE
#include "cairo-test-directfb.h"
#endif
#if CAIRO_HAS_PS_SURFACE
#include "cairo-ps.h"
cairo_user_data_key_t ps_closure_key;
typedef struct _ps_target_closure
{
char *filename;
int width, height;
} ps_target_closure_t;
static cairo_surface_t *
create_ps_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
int width = test->width;
int height = test->height;
ps_target_closure_t *ptc;
cairo_surface_t *surface;
/* This is the only format supported by the PS surface backend. */
assert (format == CAIRO_FORMAT_RGB24);
*closure = ptc = xmalloc (sizeof (ps_target_closure_t));
ptc->width = width;
ptc->height = height;
xasprintf (&ptc->filename, "%s-%s%s", test->name, "ps-rgb24-out", ".ps");
surface = cairo_ps_surface_create (ptc->filename, width, height);
if (cairo_surface_status (surface)) {
free (ptc->filename);
free (ptc);
return NULL;
}
cairo_ps_surface_set_dpi (surface, 72., 72.);
cairo_surface_set_user_data (surface, &ps_closure_key, ptc, NULL);
return surface;
}
static cairo_status_t
ps_surface_write_to_png (cairo_surface_t *surface, const char *filename)
{
ps_target_closure_t *ptc = cairo_surface_get_user_data (surface, &ps_closure_key);
char command[4096];
cairo_surface_finish (surface);
sprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -sOutputFile=%s %s",
ptc->width, ptc->height, filename, ptc->filename);
if (system (command) == 0)
return CAIRO_STATUS_SUCCESS;
return CAIRO_STATUS_WRITE_ERROR;
}
static void
cleanup_ps (void *closure)
{
ps_target_closure_t *ptc = closure;
free (ptc->filename);
free (ptc);
}
#endif /* CAIRO_HAS_PS_SURFACE */
#if CAIRO_HAS_PDF_SURFACE && CAIRO_CAN_TEST_PDF_SURFACE
#include "cairo-pdf.h"
cairo_user_data_key_t pdf_closure_key;
typedef struct _pdf_target_closure
{
char *filename;
int width;
int height;
} pdf_target_closure_t;
static cairo_surface_t *
create_pdf_surface (cairo_test_t *test,
cairo_format_t format,
void **closure)
{
int width = test->width;
int height = test->height;
pdf_target_closure_t *ptc;
cairo_surface_t *surface;
/* XXX: Is this the only format supported by the PDF surface backend? */
assert (format == CAIRO_FORMAT_RGB24);
*closure = ptc = xmalloc (sizeof (pdf_target_closure_t));
ptc->width = width;
ptc->height = height;
xasprintf (&ptc->filename, "%s-%s%s", test->name, "pdf-rgb24-out", ".pdf");
surface = cairo_pdf_surface_create (ptc->filename, width, height);
if (cairo_surface_status (surface)) {
free (ptc->filename);
free (ptc);
return NULL;
}
cairo_pdf_surface_set_dpi (surface, 72., 72.);
cairo_surface_set_user_data (surface, &pdf_closure_key, ptc, NULL);
return surface;
}
static cairo_status_t
pdf_surface_write_to_png (cairo_surface_t *surface, const char *filename)
{
pdf_target_closure_t *ptc = cairo_surface_get_user_data (surface, &pdf_closure_key);
char command[4096];
cairo_surface_finish (surface);
sprintf (command, "./pdf2png %s %s 1",
ptc->filename, filename);
if (system (command) != 0)
return CAIRO_STATUS_WRITE_ERROR;
return CAIRO_STATUS_SUCCESS;
}
static void
cleanup_pdf (void *closure)
{
pdf_target_closure_t *ptc = closure;
free (ptc->filename);
free (ptc);
}
#endif /* CAIRO_HAS_PDF_SURFACE && CAIRO_CAN_TEST_PDF_SURFACE */
#if CAIRO_HAS_SVG_SURFACE && CAIRO_CAN_TEST_SVG_SURFACE
#include "cairo-svg.h"
cairo_user_data_key_t svg_closure_key;
typedef struct _svg_target_closure
{
char *filename;
int width, height;
} svg_target_closure_t;
static cairo_surface_t *
create_svg_surface (cairo_test_t *test, cairo_format_t format,
void **closure)
{
int width = test->width;
int height = test->height;
svg_target_closure_t *ptc;
cairo_surface_t *surface;
/* This is the only format supported by the PS surface backend. */
assert (format == CAIRO_FORMAT_RGB24);
*closure = ptc = xmalloc (sizeof (svg_target_closure_t));
ptc->width = width;
ptc->height = height;
xasprintf (&ptc->filename, "%s-%s%s", test->name, "svg-rgb24-out", ".svg");
surface = cairo_svg_surface_create (ptc->filename, width, height);
if (cairo_surface_status (surface)) {
free (ptc->filename);
free (ptc);
return NULL;
}
cairo_surface_set_user_data (surface, &svg_closure_key, ptc, NULL);
return surface;
}
static cairo_status_t
svg_surface_write_to_png (cairo_surface_t *surface, const char *filename)
{
svg_target_closure_t *ptc = cairo_surface_get_user_data (surface, &svg_closure_key);
char command[4096];
cairo_surface_finish (surface);
sprintf (command, "./svg2png %s %s",
ptc->filename, filename);
if (system (command) != 0)
return CAIRO_STATUS_WRITE_ERROR;
return CAIRO_STATUS_WRITE_ERROR;
}
static void
cleanup_svg (void *closure)
{
svg_target_closure_t *ptc = closure;
free (ptc->filename);
free (ptc);
}
#endif /* CAIRO_HAS_SVG_SURFACE && CAIRO_CAN_TEST_SVG_SURFACE */
static cairo_test_status_t
cairo_test_for_target (cairo_test_t *test,
cairo_test_draw_function_t draw,
cairo_test_target_t *target)
{
cairo_test_status_t status;
cairo_surface_t *surface;
cairo_t *cr;
char *png_name, *ref_name, *diff_name;
char *srcdir;
char *format;
cairo_test_status_t ret;
/* Get the strings ready that we'll need. */
srcdir = getenv ("srcdir");
if (!srcdir)
srcdir = ".";
format = cairo_target_format_name (target);
xasprintf (&png_name, "%s-%s-%s%s", test->name,
target->name, format, CAIRO_TEST_PNG_SUFFIX);
xasprintf (&ref_name, "%s/%s-%s-%s%s", srcdir, test->name,
target->name, format, CAIRO_TEST_REF_SUFFIX);
if (access (ref_name, F_OK) != 0) {
char *ref_suffix;
free (ref_name);
if (target->reference_format == CAIRO_FORMAT_RGB24)
ref_suffix = CAIRO_TEST_RGB24_REF_SUFFIX;
else
ref_suffix = CAIRO_TEST_REF_SUFFIX;
xasprintf (&ref_name, "%s/%s%s", srcdir, test->name,
ref_suffix);
}
xasprintf (&diff_name, "%s-%s-%s%s", test->name,
target->name, format, CAIRO_TEST_DIFF_SUFFIX);
/* Run the actual drawing code. */
surface = (target->create_target_surface) (test, target->reference_format, &target->closure);
if (surface == NULL) {
cairo_test_log ("Error: Failed to set %s target\n", target->name);
ret = CAIRO_TEST_UNTESTED;
goto UNWIND_STRINGS;
}
cr = cairo_create (surface);
cairo_save (cr);
if (target->reference_format == CAIRO_FORMAT_RGB24)
cairo_set_source_rgba (cr, 1, 1, 1, 1);
else
cairo_set_source_rgba (cr, 0, 0, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_restore (cr);
status = (draw) (cr, test->width, test->height);
/* Then, check all the different ways it could fail. */
if (status) {
cairo_test_log ("Error: Function under test failed\n");
ret = status;
goto UNWIND_CAIRO;
}
cairo_show_page (cr);
if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
cairo_test_log ("Error: Function under test left cairo status in an error state: %s\n",
cairo_status_to_string (cairo_status (cr)));
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
/* Skip image check for tests with no image (width,height == 0,0) */
if (test->width != 0 && test->height != 0) {
int pixels_changed;
(target->write_to_png) (surface, png_name);
pixels_changed = image_diff (png_name, ref_name, diff_name);
if (pixels_changed) {
if (pixels_changed > 0)
cairo_test_log ("Error: %d pixels differ from reference image %s\n",
pixels_changed, ref_name);
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
}
ret = CAIRO_TEST_SUCCESS;
UNWIND_CAIRO:
cairo_destroy (cr);
cairo_surface_destroy (surface);
cairo_debug_reset_static_data ();
if (target->cleanup_target)
target->cleanup_target (target->closure);
UNWIND_STRINGS:
free (png_name);
free (ref_name);
free (diff_name);
return ret;
}
static cairo_test_status_t
cairo_test_expecting (cairo_test_t *test, cairo_test_draw_function_t draw,
cairo_test_status_t expectation)
{
int i, num_targets;
const char *tname;
cairo_test_status_t status, ret;
cairo_test_target_t **targets_to_test;
cairo_test_target_t targets[] =
{
{ "image", CAIRO_FORMAT_ARGB32,
create_image_surface, cairo_surface_write_to_png, NULL},
{ "image", CAIRO_FORMAT_RGB24,
create_image_surface, cairo_surface_write_to_png, NULL},
#ifdef CAIRO_HAS_TEST_SURFACES
{ "test-fallback", CAIRO_FORMAT_ARGB32,
create_test_fallback_surface, cairo_surface_write_to_png, NULL },
{ "test-meta", CAIRO_FORMAT_ARGB32,
create_test_meta_surface, cairo_surface_write_to_png, NULL },
{ "test-paginated", CAIRO_FORMAT_ARGB32,
create_test_paginated_surface,
test_paginated_write_to_png,
cleanup_test_paginated },
#endif
#ifdef CAIRO_HAS_GLITZ_SURFACE
#if CAIRO_CAN_TEST_GLITZ_GLX_SURFACE
{ "glitz-glx", CAIRO_FORMAT_ARGB32,
create_cairo_glitz_glx_surface, cairo_surface_write_to_png,
cleanup_cairo_glitz_glx },
{ "glitz-glx", CAIRO_FORMAT_RGB24,
create_cairo_glitz_glx_surface, cairo_surface_write_to_png,
cleanup_cairo_glitz_glx },
#endif
#if CAIRO_CAN_TEST_GLITZ_AGL_SURFACE
{ "glitz-agl", CAIRO_FORMAT_ARGB32,
create_cairo_glitz_agl_surface, cairo_surface_write_to_png,
cleanup_cairo_glitz_agl },
{ "glitz-agl", CAIRO_FORMAT_RGB24,
create_cairo_glitz_agl_surface, cairo_surface_write_to_png,
cleanup_cairo_glitz_agl },
#endif
#if CAIRO_CAN_TEST_GLITZ_WGL_SURFACE
{ "glitz-wgl", CAIRO_FORMAT_ARGB32,
create_cairo_glitz_wgl_surface, cairo_surface_write_to_png,
cleanup_cairo_glitz_wgl },
{ "glitz-wgl", CAIRO_FORMAT_RGB24,
create_cairo_glitz_wgl_surface, cairo_surface_write_to_png,
cleanup_cairo_glitz_wgl },
#endif
#endif /* CAIRO_HAS_GLITZ_SURFACE */
#if 0 && CAIRO_HAS_QUARTZ_SURFACE
{ "quartz", CAIRO_FORMAT_RGB24,
create_quartz_surface, cairo_surface_write_to_png,
cleanup_quartz },
#endif
#if CAIRO_HAS_WIN32_SURFACE
{ "win32", CAIRO_FORMAT_RGB24,
create_win32_surface, cairo_surface_write_to_png, cleanup_win32 },
#endif
#if CAIRO_HAS_XCB_SURFACE
{ "xcb", CAIRO_FORMAT_ARGB32,
create_xcb_surface, cairo_surface_write_to_png, cleanup_xcb},
#endif
#if CAIRO_HAS_XLIB_SURFACE
{ "xlib", CAIRO_FORMAT_ARGB32,
create_xlib_surface, cairo_surface_write_to_png, cleanup_xlib},
{ "xlib", CAIRO_FORMAT_RGB24,
create_xlib_surface, cairo_surface_write_to_png, cleanup_xlib},
#endif
#if CAIRO_HAS_PS_SURFACE
{ "ps", CAIRO_FORMAT_RGB24,
create_ps_surface, ps_surface_write_to_png, cleanup_ps },
#endif
#if CAIRO_HAS_PDF_SURFACE && CAIRO_CAN_TEST_PDF_SURFACE
{ "pdf", CAIRO_FORMAT_RGB24,
create_pdf_surface, pdf_surface_write_to_png, cleanup_pdf },
#endif
#if CAIRO_HAS_SVG_SURFACE && CAIRO_CAN_TEST_SVG_SURFACE
{ "svg", CAIRO_FORMAT_RGB24,
create_svg_surface, svg_surface_write_to_png, cleanup_svg },
#endif
#if CAIRO_HAS_BEOS_SURFACE
{ "beos", CAIRO_FORMAT_RGB24,
create_beos_surface, cairo_surface_write_to_png, cleanup_beos},
{ "beos_bitmap", CAIRO_FORMAT_RGB24,
create_beos_bitmap_surface, cairo_surface_write_to_png, cleanup_beos_bitmap},
{ "beos_bitmap", CAIRO_FORMAT_ARGB32,
create_beos_bitmap_surface, cairo_surface_write_to_png, cleanup_beos_bitmap},
#endif
#if CAIRO_HAS_DIRECTFB_SURFACE
{ "directfb", CAIRO_FORMAT_RGB24,
create_directfb_surface, cairo_surface_write_to_png, cleanup_directfb},
{ "directfb_bitmap", CAIRO_FORMAT_ARGB32,
create_directfb_bitmap_surface, cairo_surface_write_to_png,cleanup_directfb},
#endif
};
if ((tname = getenv ("CAIRO_TEST_TARGET")) != NULL) {
const char *tname = getenv ("CAIRO_TEST_TARGET");
num_targets = 0;
targets_to_test = NULL;
/* realloc isn't exactly the best thing here, but meh. */
for (i = 0; i < sizeof(targets)/sizeof(targets[0]); i++) {
if (strcmp (targets[i].name, tname) == 0) {
targets_to_test = realloc (targets_to_test, sizeof(cairo_test_target_t *) * (num_targets+1));
targets_to_test[num_targets++] = &targets[i];
}
}
if (num_targets == 0) {
fprintf (stderr, "CAIRO_TEST_TARGET '%s' not found in targets list!\n", tname);
exit(-1);
}
} else {
num_targets = sizeof(targets)/sizeof(targets[0]);
targets_to_test = malloc (sizeof(cairo_test_target_t*) * num_targets);
for (i = 0; i < num_targets; i++)
targets_to_test[i] = &targets[i];
}
cairo_test_init (test->name);
/* The intended logic here is that we return overall SUCCESS
* iff. there is at least one tested backend and that all tested
* backends return SUCCESS. In other words:
*
* if any backend FAILURE
* -> FAILURE
* else if all backends UNTESTED
* -> FAILURE
* else (== some backend SUCCESS)
* -> SUCCESS
*/
ret = CAIRO_TEST_UNTESTED;
for (i = 0; i < num_targets; i++) {
cairo_test_target_t *target = targets_to_test[i];
cairo_test_log ("Testing %s with %s target\n", test->name, target->name);
printf ("%s-%s-%s:\t", test->name, target->name, cairo_target_format_name(target));
status = cairo_test_for_target (test, draw, target);
cairo_test_log ("TEST: %s TARGET: %s FORMAT: %s RESULT: ",
test->name, target->name, cairo_target_format_name(target));
switch (status) {
case CAIRO_TEST_SUCCESS:
printf ("PASS\n");
cairo_test_log ("PASS\n");
if (ret == CAIRO_TEST_UNTESTED)
ret = CAIRO_TEST_SUCCESS;
break;
case CAIRO_TEST_UNTESTED:
printf ("UNTESTED\n");
cairo_test_log ("UNTESTED\n");
break;
default:
case CAIRO_TEST_FAILURE:
if (expectation == CAIRO_TEST_FAILURE) {
printf ("XFAIL\n");
cairo_test_log ("XFAIL\n");
} else {
printf ("FAIL\n");
cairo_test_log ("FAIL\n");
}
ret = status;
break;
}
}
if (ret == CAIRO_TEST_UNTESTED)
ret = CAIRO_TEST_FAILURE;
fclose (cairo_test_log_file);
free (targets_to_test);
#if HAVE_FCFINI
FcFini ();
#endif
return ret;
}
cairo_test_status_t
cairo_test_expect_failure (cairo_test_t *test,
cairo_test_draw_function_t draw,
const char *because)
{
printf ("\n%s is expected to fail:\n\t%s\n", test->name, because);
return cairo_test_expecting (test, draw, CAIRO_TEST_FAILURE);
}
cairo_test_status_t
cairo_test (cairo_test_t *test, cairo_test_draw_function_t draw)
{
printf ("\n");
return cairo_test_expecting (test, draw, CAIRO_TEST_SUCCESS);
}
cairo_surface_t *
cairo_test_create_surface_from_png (const char *filename)
{
cairo_surface_t *image;
char *srcdir = getenv ("srcdir");
image = cairo_image_surface_create_from_png (filename);
if (cairo_surface_status(image)) {
/* expect not found when running with srcdir != builddir
* such as when 'make distcheck' is run
*/
if (srcdir) {
char *srcdir_filename;
xasprintf (&srcdir_filename, "%s/%s", srcdir, filename);
image = cairo_image_surface_create_from_png (srcdir_filename);
free (srcdir_filename);
}
if (cairo_surface_status(image))
return NULL;
}
return image;
}
cairo_pattern_t *
cairo_test_create_pattern_from_png (const char *filename)
{
cairo_surface_t *image;
cairo_pattern_t *pattern;
image = cairo_test_create_surface_from_png (filename);
pattern = cairo_pattern_create_for_surface (image);
cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
cairo_surface_destroy (image);
return pattern;
}
|