From dafebc5bb70303f0b5baf0b087cf4d9a64b5c7f0 Mon Sep 17 00:00:00 2001 From: marha Date: Mon, 12 Sep 2011 11:27:51 +0200 Subject: Synchronised line endinge with release branch --- pixman/test/Makefile.am | 100 +- pixman/test/blitters-test.c | 856 ++++++++--------- pixman/test/composite-traps-test.c | 514 +++++----- pixman/test/composite.c | 1842 ++++++++++++++++++------------------ pixman/test/fetch-test.c | 412 ++++---- pixman/test/stress-test.c | 1744 +++++++++++++++++----------------- pixman/test/trap-crasher.c | 54 +- pixman/test/utils.c | 1408 +++++++++++++-------------- pixman/test/utils.h | 302 +++--- 9 files changed, 3616 insertions(+), 3616 deletions(-) (limited to 'pixman/test') diff --git a/pixman/test/Makefile.am b/pixman/test/Makefile.am index 56d56aeb1..52ef8ad96 100644 --- a/pixman/test/Makefile.am +++ b/pixman/test/Makefile.am @@ -1,50 +1,50 @@ -AM_CFLAGS = @OPENMP_CFLAGS@ -AM_LDFLAGS = @OPENMP_CFLAGS@ @TESTPROGS_EXTRA_LDFLAGS@ -LDADD = $(top_builddir)/pixman/libpixman-1.la -lm -lpng -INCLUDES = -I$(top_srcdir)/pixman -I$(top_builddir)/pixman - -TESTPROGRAMS = \ - a1-trap-test \ - pdf-op-test \ - region-test \ - region-translate-test \ - fetch-test \ - oob-test \ - trap-crasher \ - alpha-loop \ - scaling-crash-test \ - scaling-helpers-test \ - gradient-crash-test \ - region-contains-test \ - alphamap \ - stress-test \ - composite-traps-test \ - blitters-test \ - scaling-test \ - affine-test \ - composite - -pdf_op_test_SOURCES = pdf-op-test.c utils.c utils.h -region_test_SOURCES = region-test.c utils.c utils.h -blitters_test_SOURCES = blitters-test.c utils.c utils.h -region_contains_test_SOURCES = region-contains-test.c utils.c utils.h -composite_traps_test_SOURCES = composite-traps-test.c utils.c utils.h -scaling_test_SOURCES = scaling-test.c utils.c utils.h -affine_test_SOURCES = affine-test.c utils.c utils.h -alphamap_SOURCES = alphamap.c utils.c utils.h -alpha_loop_SOURCES = alpha-loop.c utils.c utils.h -composite_SOURCES = composite.c utils.c utils.h -gradient_crash_test_SOURCES = gradient-crash-test.c utils.c utils.h -stress_test_SOURCES = stress-test.c utils.c utils.h -scaling_helpers_test_SOURCES = scaling-helpers-test.c utils.c utils.h - -# Benchmarks - -BENCHMARKS = \ - lowlevel-blt-bench - -lowlevel_blt_bench_SOURCES = lowlevel-blt-bench.c utils.c utils.h - -noinst_PROGRAMS = $(TESTPROGRAMS) $(BENCHMARKS) - -TESTS = $(TESTPROGRAMS) +AM_CFLAGS = @OPENMP_CFLAGS@ +AM_LDFLAGS = @OPENMP_CFLAGS@ @TESTPROGS_EXTRA_LDFLAGS@ +LDADD = $(top_builddir)/pixman/libpixman-1.la -lm -lpng +INCLUDES = -I$(top_srcdir)/pixman -I$(top_builddir)/pixman + +TESTPROGRAMS = \ + a1-trap-test \ + pdf-op-test \ + region-test \ + region-translate-test \ + fetch-test \ + oob-test \ + trap-crasher \ + alpha-loop \ + scaling-crash-test \ + scaling-helpers-test \ + gradient-crash-test \ + region-contains-test \ + alphamap \ + stress-test \ + composite-traps-test \ + blitters-test \ + scaling-test \ + affine-test \ + composite + +pdf_op_test_SOURCES = pdf-op-test.c utils.c utils.h +region_test_SOURCES = region-test.c utils.c utils.h +blitters_test_SOURCES = blitters-test.c utils.c utils.h +region_contains_test_SOURCES = region-contains-test.c utils.c utils.h +composite_traps_test_SOURCES = composite-traps-test.c utils.c utils.h +scaling_test_SOURCES = scaling-test.c utils.c utils.h +affine_test_SOURCES = affine-test.c utils.c utils.h +alphamap_SOURCES = alphamap.c utils.c utils.h +alpha_loop_SOURCES = alpha-loop.c utils.c utils.h +composite_SOURCES = composite.c utils.c utils.h +gradient_crash_test_SOURCES = gradient-crash-test.c utils.c utils.h +stress_test_SOURCES = stress-test.c utils.c utils.h +scaling_helpers_test_SOURCES = scaling-helpers-test.c utils.c utils.h + +# Benchmarks + +BENCHMARKS = \ + lowlevel-blt-bench + +lowlevel_blt_bench_SOURCES = lowlevel-blt-bench.c utils.c utils.h + +noinst_PROGRAMS = $(TESTPROGRAMS) $(BENCHMARKS) + +TESTS = $(TESTPROGRAMS) diff --git a/pixman/test/blitters-test.c b/pixman/test/blitters-test.c index ba81fc71a..594ec548b 100644 --- a/pixman/test/blitters-test.c +++ b/pixman/test/blitters-test.c @@ -1,428 +1,428 @@ -/* - * Test program, which stresses the use of different color formats and - * compositing operations. - * - * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in - * the case of test failure. - */ -#include -#include -#include -#include -#include "utils.h" - -static pixman_indexed_t rgb_palette[9]; -static pixman_indexed_t y_palette[9]; - -/* The first eight format in the list are by far the most widely - * used formats, so we test those more than the others - */ -#define N_MOST_LIKELY_FORMATS 8 - -/* Create random image for testing purposes */ -static pixman_image_t * -create_random_image (pixman_format_code_t *allowed_formats, - int max_width, - int max_height, - int max_extra_stride, - pixman_format_code_t *used_fmt) -{ - int n = 0, i, width, height, stride; - pixman_format_code_t fmt; - uint32_t *buf; - pixman_image_t *img; - - while (allowed_formats[n] != PIXMAN_null) - n++; - - if (n > N_MOST_LIKELY_FORMATS && lcg_rand_n (4) != 0) - n = N_MOST_LIKELY_FORMATS; - fmt = allowed_formats[lcg_rand_n (n)]; - - width = lcg_rand_n (max_width) + 1; - height = lcg_rand_n (max_height) + 1; - stride = (width * PIXMAN_FORMAT_BPP (fmt) + 7) / 8 + - lcg_rand_n (max_extra_stride + 1); - stride = (stride + 3) & ~3; - - /* do the allocation */ - buf = aligned_malloc (64, stride * height); - - /* initialize image with random data */ - for (i = 0; i < stride * height; i++) - { - /* generation is biased to having more 0 or 255 bytes as - * they are more likely to be special-cased in code - */ - *((uint8_t *)buf + i) = lcg_rand_n (4) ? lcg_rand_n (256) : - (lcg_rand_n (2) ? 0 : 255); - } - - img = pixman_image_create_bits (fmt, width, height, buf, stride); - - if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR) - { - pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)])); - } - else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY) - { - pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)])); - } - - image_endian_swap (img); - - if (used_fmt) *used_fmt = fmt; - return img; -} - -/* Free random image, and optionally update crc32 based on its data */ -static uint32_t -free_random_image (uint32_t initcrc, - pixman_image_t *img, - pixman_format_code_t fmt) -{ - uint32_t crc32 = 0; - int stride = pixman_image_get_stride (img); - uint32_t *data = pixman_image_get_data (img); - int height = pixman_image_get_height (img); - - if (fmt != PIXMAN_null) - { - /* mask unused 'x' part */ - if (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt) && - PIXMAN_FORMAT_DEPTH (fmt) != 0) - { - int i; - uint32_t *data = pixman_image_get_data (img); - uint32_t mask = (1 << PIXMAN_FORMAT_DEPTH (fmt)) - 1; - - if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_BGRA || - PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_RGBA) - { - mask <<= (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt)); - } - - for (i = 0; i < 32; i++) - mask |= mask << (i * PIXMAN_FORMAT_BPP (fmt)); - - for (i = 0; i < stride * height / 4; i++) - data[i] &= mask; - } - - /* swap endiannes in order to provide identical results on both big - * and litte endian systems - */ - image_endian_swap (img); - crc32 = compute_crc32 (initcrc, data, stride * height); - } - - pixman_image_unref (img); - free (data); - - return crc32; -} - -static pixman_op_t op_list[] = { - PIXMAN_OP_SRC, - PIXMAN_OP_OVER, - PIXMAN_OP_ADD, - PIXMAN_OP_CLEAR, - PIXMAN_OP_SRC, - PIXMAN_OP_DST, - PIXMAN_OP_OVER, - PIXMAN_OP_OVER_REVERSE, - PIXMAN_OP_IN, - PIXMAN_OP_IN_REVERSE, - PIXMAN_OP_OUT, - PIXMAN_OP_OUT_REVERSE, - PIXMAN_OP_ATOP, - PIXMAN_OP_ATOP_REVERSE, - PIXMAN_OP_XOR, - PIXMAN_OP_ADD, - PIXMAN_OP_SATURATE, - PIXMAN_OP_DISJOINT_CLEAR, - PIXMAN_OP_DISJOINT_SRC, - PIXMAN_OP_DISJOINT_DST, - PIXMAN_OP_DISJOINT_OVER, - PIXMAN_OP_DISJOINT_OVER_REVERSE, - PIXMAN_OP_DISJOINT_IN, - PIXMAN_OP_DISJOINT_IN_REVERSE, - PIXMAN_OP_DISJOINT_OUT, - PIXMAN_OP_DISJOINT_OUT_REVERSE, - PIXMAN_OP_DISJOINT_ATOP, - PIXMAN_OP_DISJOINT_ATOP_REVERSE, - PIXMAN_OP_DISJOINT_XOR, - PIXMAN_OP_CONJOINT_CLEAR, - PIXMAN_OP_CONJOINT_SRC, - PIXMAN_OP_CONJOINT_DST, - PIXMAN_OP_CONJOINT_OVER, - PIXMAN_OP_CONJOINT_OVER_REVERSE, - PIXMAN_OP_CONJOINT_IN, - PIXMAN_OP_CONJOINT_IN_REVERSE, - PIXMAN_OP_CONJOINT_OUT, - PIXMAN_OP_CONJOINT_OUT_REVERSE, - PIXMAN_OP_CONJOINT_ATOP, - PIXMAN_OP_CONJOINT_ATOP_REVERSE, - PIXMAN_OP_CONJOINT_XOR, - PIXMAN_OP_MULTIPLY, - PIXMAN_OP_SCREEN, - PIXMAN_OP_OVERLAY, - PIXMAN_OP_DARKEN, - PIXMAN_OP_LIGHTEN, - PIXMAN_OP_COLOR_DODGE, - PIXMAN_OP_COLOR_BURN, - PIXMAN_OP_HARD_LIGHT, - PIXMAN_OP_DIFFERENCE, - PIXMAN_OP_EXCLUSION, -#if 0 /* these use floating point math and are not always bitexact on different platforms */ - PIXMAN_OP_SOFT_LIGHT, - PIXMAN_OP_HSL_HUE, - PIXMAN_OP_HSL_SATURATION, - PIXMAN_OP_HSL_COLOR, - PIXMAN_OP_HSL_LUMINOSITY, -#endif -}; - -static pixman_format_code_t img_fmt_list[] = { - PIXMAN_a8r8g8b8, - PIXMAN_a8b8g8r8, - PIXMAN_x8r8g8b8, - PIXMAN_x8b8g8r8, - PIXMAN_r5g6b5, - PIXMAN_b5g6r5, - PIXMAN_a8, - PIXMAN_a1, - PIXMAN_r3g3b2, - PIXMAN_b8g8r8a8, - PIXMAN_b8g8r8x8, - PIXMAN_r8g8b8a8, - PIXMAN_r8g8b8x8, - PIXMAN_x14r6g6b6, - PIXMAN_r8g8b8, - PIXMAN_b8g8r8, - PIXMAN_x2r10g10b10, - PIXMAN_a2r10g10b10, - PIXMAN_x2b10g10r10, - PIXMAN_a2b10g10r10, - PIXMAN_a1r5g5b5, - PIXMAN_x1r5g5b5, - PIXMAN_a1b5g5r5, - PIXMAN_x1b5g5r5, - PIXMAN_a4r4g4b4, - PIXMAN_x4r4g4b4, - PIXMAN_a4b4g4r4, - PIXMAN_x4b4g4r4, - PIXMAN_r3g3b2, - PIXMAN_b2g3r3, - PIXMAN_a2r2g2b2, - PIXMAN_a2b2g2r2, - PIXMAN_c8, - PIXMAN_g8, - PIXMAN_x4c4, - PIXMAN_x4g4, - PIXMAN_c4, - PIXMAN_g4, - PIXMAN_g1, - PIXMAN_x4a4, - PIXMAN_a4, - PIXMAN_r1g2b1, - PIXMAN_b1g2r1, - PIXMAN_a1r1g1b1, - PIXMAN_a1b1g1r1, - PIXMAN_null -}; - -static pixman_format_code_t mask_fmt_list[] = { - PIXMAN_a8r8g8b8, - PIXMAN_a8, - PIXMAN_a4, - PIXMAN_a1, - PIXMAN_null -}; - - -/* - * Composite operation with pseudorandom images - */ -uint32_t -test_composite (int testnum, int verbose) -{ - int i; - pixman_image_t *src_img = NULL; - pixman_image_t *dst_img = NULL; - pixman_image_t *mask_img = NULL; - int src_width, src_height; - int dst_width, dst_height; - int src_stride, dst_stride; - int src_x, src_y; - int dst_x, dst_y; - int mask_x, mask_y; - int w, h; - pixman_op_t op; - pixman_format_code_t src_fmt, dst_fmt, mask_fmt; - uint32_t *dstbuf, *srcbuf, *maskbuf; - uint32_t crc32; - int max_width, max_height, max_extra_stride; - FLOAT_REGS_CORRUPTION_DETECTOR_START (); - - max_width = max_height = 24 + testnum / 10000; - max_extra_stride = 4 + testnum / 1000000; - - if (max_width > 256) - max_width = 256; - - if (max_height > 16) - max_height = 16; - - if (max_extra_stride > 8) - max_extra_stride = 8; - - lcg_srand (testnum); - - op = op_list[lcg_rand_n (sizeof (op_list) / sizeof (op_list[0]))]; - - if (lcg_rand_n (8)) - { - /* normal image */ - src_img = create_random_image (img_fmt_list, max_width, max_height, - max_extra_stride, &src_fmt); - } - else - { - /* solid case */ - src_img = create_random_image (img_fmt_list, 1, 1, - max_extra_stride, &src_fmt); - - pixman_image_set_repeat (src_img, PIXMAN_REPEAT_NORMAL); - } - - dst_img = create_random_image (img_fmt_list, max_width, max_height, - max_extra_stride, &dst_fmt); - - src_width = pixman_image_get_width (src_img); - src_height = pixman_image_get_height (src_img); - src_stride = pixman_image_get_stride (src_img); - - dst_width = pixman_image_get_width (dst_img); - dst_height = pixman_image_get_height (dst_img); - dst_stride = pixman_image_get_stride (dst_img); - - dstbuf = pixman_image_get_data (dst_img); - srcbuf = pixman_image_get_data (src_img); - - src_x = lcg_rand_n (src_width); - src_y = lcg_rand_n (src_height); - dst_x = lcg_rand_n (dst_width); - dst_y = lcg_rand_n (dst_height); - - mask_img = NULL; - mask_fmt = PIXMAN_null; - mask_x = 0; - mask_y = 0; - maskbuf = NULL; - - if ((src_fmt == PIXMAN_x8r8g8b8 || src_fmt == PIXMAN_x8b8g8r8) && - (lcg_rand_n (4) == 0)) - { - /* PIXBUF */ - mask_fmt = lcg_rand_n (2) ? PIXMAN_a8r8g8b8 : PIXMAN_a8b8g8r8; - mask_img = pixman_image_create_bits (mask_fmt, - src_width, - src_height, - srcbuf, - src_stride); - mask_x = src_x; - mask_y = src_y; - maskbuf = srcbuf; - } - else if (lcg_rand_n (2)) - { - if (lcg_rand_n (2)) - { - mask_img = create_random_image (mask_fmt_list, max_width, max_height, - max_extra_stride, &mask_fmt); - } - else - { - /* solid case */ - mask_img = create_random_image (mask_fmt_list, 1, 1, - max_extra_stride, &mask_fmt); - pixman_image_set_repeat (mask_img, PIXMAN_REPEAT_NORMAL); - } - - if (lcg_rand_n (2)) - pixman_image_set_component_alpha (mask_img, 1); - - mask_x = lcg_rand_n (pixman_image_get_width (mask_img)); - mask_y = lcg_rand_n (pixman_image_get_height (mask_img)); - } - - - w = lcg_rand_n (dst_width - dst_x + 1); - h = lcg_rand_n (dst_height - dst_y + 1); - - if (verbose) - { - printf ("op=%d, src_fmt=%08X, dst_fmt=%08X, mask_fmt=%08X\n", - op, src_fmt, dst_fmt, mask_fmt); - printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n", - src_width, src_height, dst_width, dst_height); - printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n", - src_x, src_y, dst_x, dst_y); - printf ("src_stride=%d, dst_stride=%d\n", - src_stride, dst_stride); - printf ("w=%d, h=%d\n", w, h); - } - - pixman_image_composite (op, src_img, mask_img, dst_img, - src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h); - - if (verbose) - { - int j; - - printf ("---\n"); - for (i = 0; i < dst_height; i++) - { - for (j = 0; j < dst_stride; j++) - { - if (j == (dst_width * PIXMAN_FORMAT_BPP (dst_fmt) + 7) / 8) - printf ("| "); - - printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j)); - } - printf ("\n"); - } - printf ("---\n"); - } - - free_random_image (0, src_img, PIXMAN_null); - crc32 = free_random_image (0, dst_img, dst_fmt); - - if (mask_img) - { - if (srcbuf == maskbuf) - pixman_image_unref(mask_img); - else - free_random_image (0, mask_img, PIXMAN_null); - } - - FLOAT_REGS_CORRUPTION_DETECTOR_FINISH (); - return crc32; -} - -int -main (int argc, const char *argv[]) -{ - int i; - - for (i = 1; i <= 8; i++) - { - initialize_palette (&(rgb_palette[i]), i, TRUE); - initialize_palette (&(y_palette[i]), i, FALSE); - } - - return fuzzer_test_main("blitters", 2000000, - 0xB610300B, - test_composite, argc, argv); -} +/* + * Test program, which stresses the use of different color formats and + * compositing operations. + * + * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in + * the case of test failure. + */ +#include +#include +#include +#include +#include "utils.h" + +static pixman_indexed_t rgb_palette[9]; +static pixman_indexed_t y_palette[9]; + +/* The first eight format in the list are by far the most widely + * used formats, so we test those more than the others + */ +#define N_MOST_LIKELY_FORMATS 8 + +/* Create random image for testing purposes */ +static pixman_image_t * +create_random_image (pixman_format_code_t *allowed_formats, + int max_width, + int max_height, + int max_extra_stride, + pixman_format_code_t *used_fmt) +{ + int n = 0, i, width, height, stride; + pixman_format_code_t fmt; + uint32_t *buf; + pixman_image_t *img; + + while (allowed_formats[n] != PIXMAN_null) + n++; + + if (n > N_MOST_LIKELY_FORMATS && lcg_rand_n (4) != 0) + n = N_MOST_LIKELY_FORMATS; + fmt = allowed_formats[lcg_rand_n (n)]; + + width = lcg_rand_n (max_width) + 1; + height = lcg_rand_n (max_height) + 1; + stride = (width * PIXMAN_FORMAT_BPP (fmt) + 7) / 8 + + lcg_rand_n (max_extra_stride + 1); + stride = (stride + 3) & ~3; + + /* do the allocation */ + buf = aligned_malloc (64, stride * height); + + /* initialize image with random data */ + for (i = 0; i < stride * height; i++) + { + /* generation is biased to having more 0 or 255 bytes as + * they are more likely to be special-cased in code + */ + *((uint8_t *)buf + i) = lcg_rand_n (4) ? lcg_rand_n (256) : + (lcg_rand_n (2) ? 0 : 255); + } + + img = pixman_image_create_bits (fmt, width, height, buf, stride); + + if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR) + { + pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)])); + } + else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY) + { + pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)])); + } + + image_endian_swap (img); + + if (used_fmt) *used_fmt = fmt; + return img; +} + +/* Free random image, and optionally update crc32 based on its data */ +static uint32_t +free_random_image (uint32_t initcrc, + pixman_image_t *img, + pixman_format_code_t fmt) +{ + uint32_t crc32 = 0; + int stride = pixman_image_get_stride (img); + uint32_t *data = pixman_image_get_data (img); + int height = pixman_image_get_height (img); + + if (fmt != PIXMAN_null) + { + /* mask unused 'x' part */ + if (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt) && + PIXMAN_FORMAT_DEPTH (fmt) != 0) + { + int i; + uint32_t *data = pixman_image_get_data (img); + uint32_t mask = (1 << PIXMAN_FORMAT_DEPTH (fmt)) - 1; + + if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_BGRA || + PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_RGBA) + { + mask <<= (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt)); + } + + for (i = 0; i < 32; i++) + mask |= mask << (i * PIXMAN_FORMAT_BPP (fmt)); + + for (i = 0; i < stride * height / 4; i++) + data[i] &= mask; + } + + /* swap endiannes in order to provide identical results on both big + * and litte endian systems + */ + image_endian_swap (img); + crc32 = compute_crc32 (initcrc, data, stride * height); + } + + pixman_image_unref (img); + free (data); + + return crc32; +} + +static pixman_op_t op_list[] = { + PIXMAN_OP_SRC, + PIXMAN_OP_OVER, + PIXMAN_OP_ADD, + PIXMAN_OP_CLEAR, + PIXMAN_OP_SRC, + PIXMAN_OP_DST, + PIXMAN_OP_OVER, + PIXMAN_OP_OVER_REVERSE, + PIXMAN_OP_IN, + PIXMAN_OP_IN_REVERSE, + PIXMAN_OP_OUT, + PIXMAN_OP_OUT_REVERSE, + PIXMAN_OP_ATOP, + PIXMAN_OP_ATOP_REVERSE, + PIXMAN_OP_XOR, + PIXMAN_OP_ADD, + PIXMAN_OP_SATURATE, + PIXMAN_OP_DISJOINT_CLEAR, + PIXMAN_OP_DISJOINT_SRC, + PIXMAN_OP_DISJOINT_DST, + PIXMAN_OP_DISJOINT_OVER, + PIXMAN_OP_DISJOINT_OVER_REVERSE, + PIXMAN_OP_DISJOINT_IN, + PIXMAN_OP_DISJOINT_IN_REVERSE, + PIXMAN_OP_DISJOINT_OUT, + PIXMAN_OP_DISJOINT_OUT_REVERSE, + PIXMAN_OP_DISJOINT_ATOP, + PIXMAN_OP_DISJOINT_ATOP_REVERSE, + PIXMAN_OP_DISJOINT_XOR, + PIXMAN_OP_CONJOINT_CLEAR, + PIXMAN_OP_CONJOINT_SRC, + PIXMAN_OP_CONJOINT_DST, + PIXMAN_OP_CONJOINT_OVER, + PIXMAN_OP_CONJOINT_OVER_REVERSE, + PIXMAN_OP_CONJOINT_IN, + PIXMAN_OP_CONJOINT_IN_REVERSE, + PIXMAN_OP_CONJOINT_OUT, + PIXMAN_OP_CONJOINT_OUT_REVERSE, + PIXMAN_OP_CONJOINT_ATOP, + PIXMAN_OP_CONJOINT_ATOP_REVERSE, + PIXMAN_OP_CONJOINT_XOR, + PIXMAN_OP_MULTIPLY, + PIXMAN_OP_SCREEN, + PIXMAN_OP_OVERLAY, + PIXMAN_OP_DARKEN, + PIXMAN_OP_LIGHTEN, + PIXMAN_OP_COLOR_DODGE, + PIXMAN_OP_COLOR_BURN, + PIXMAN_OP_HARD_LIGHT, + PIXMAN_OP_DIFFERENCE, + PIXMAN_OP_EXCLUSION, +#if 0 /* these use floating point math and are not always bitexact on different platforms */ + PIXMAN_OP_SOFT_LIGHT, + PIXMAN_OP_HSL_HUE, + PIXMAN_OP_HSL_SATURATION, + PIXMAN_OP_HSL_COLOR, + PIXMAN_OP_HSL_LUMINOSITY, +#endif +}; + +static pixman_format_code_t img_fmt_list[] = { + PIXMAN_a8r8g8b8, + PIXMAN_a8b8g8r8, + PIXMAN_x8r8g8b8, + PIXMAN_x8b8g8r8, + PIXMAN_r5g6b5, + PIXMAN_b5g6r5, + PIXMAN_a8, + PIXMAN_a1, + PIXMAN_r3g3b2, + PIXMAN_b8g8r8a8, + PIXMAN_b8g8r8x8, + PIXMAN_r8g8b8a8, + PIXMAN_r8g8b8x8, + PIXMAN_x14r6g6b6, + PIXMAN_r8g8b8, + PIXMAN_b8g8r8, + PIXMAN_x2r10g10b10, + PIXMAN_a2r10g10b10, + PIXMAN_x2b10g10r10, + PIXMAN_a2b10g10r10, + PIXMAN_a1r5g5b5, + PIXMAN_x1r5g5b5, + PIXMAN_a1b5g5r5, + PIXMAN_x1b5g5r5, + PIXMAN_a4r4g4b4, + PIXMAN_x4r4g4b4, + PIXMAN_a4b4g4r4, + PIXMAN_x4b4g4r4, + PIXMAN_r3g3b2, + PIXMAN_b2g3r3, + PIXMAN_a2r2g2b2, + PIXMAN_a2b2g2r2, + PIXMAN_c8, + PIXMAN_g8, + PIXMAN_x4c4, + PIXMAN_x4g4, + PIXMAN_c4, + PIXMAN_g4, + PIXMAN_g1, + PIXMAN_x4a4, + PIXMAN_a4, + PIXMAN_r1g2b1, + PIXMAN_b1g2r1, + PIXMAN_a1r1g1b1, + PIXMAN_a1b1g1r1, + PIXMAN_null +}; + +static pixman_format_code_t mask_fmt_list[] = { + PIXMAN_a8r8g8b8, + PIXMAN_a8, + PIXMAN_a4, + PIXMAN_a1, + PIXMAN_null +}; + + +/* + * Composite operation with pseudorandom images + */ +uint32_t +test_composite (int testnum, int verbose) +{ + int i; + pixman_image_t *src_img = NULL; + pixman_image_t *dst_img = NULL; + pixman_image_t *mask_img = NULL; + int src_width, src_height; + int dst_width, dst_height; + int src_stride, dst_stride; + int src_x, src_y; + int dst_x, dst_y; + int mask_x, mask_y; + int w, h; + pixman_op_t op; + pixman_format_code_t src_fmt, dst_fmt, mask_fmt; + uint32_t *dstbuf, *srcbuf, *maskbuf; + uint32_t crc32; + int max_width, max_height, max_extra_stride; + FLOAT_REGS_CORRUPTION_DETECTOR_START (); + + max_width = max_height = 24 + testnum / 10000; + max_extra_stride = 4 + testnum / 1000000; + + if (max_width > 256) + max_width = 256; + + if (max_height > 16) + max_height = 16; + + if (max_extra_stride > 8) + max_extra_stride = 8; + + lcg_srand (testnum); + + op = op_list[lcg_rand_n (sizeof (op_list) / sizeof (op_list[0]))]; + + if (lcg_rand_n (8)) + { + /* normal image */ + src_img = create_random_image (img_fmt_list, max_width, max_height, + max_extra_stride, &src_fmt); + } + else + { + /* solid case */ + src_img = create_random_image (img_fmt_list, 1, 1, + max_extra_stride, &src_fmt); + + pixman_image_set_repeat (src_img, PIXMAN_REPEAT_NORMAL); + } + + dst_img = create_random_image (img_fmt_list, max_width, max_height, + max_extra_stride, &dst_fmt); + + src_width = pixman_image_get_width (src_img); + src_height = pixman_image_get_height (src_img); + src_stride = pixman_image_get_stride (src_img); + + dst_width = pixman_image_get_width (dst_img); + dst_height = pixman_image_get_height (dst_img); + dst_stride = pixman_image_get_stride (dst_img); + + dstbuf = pixman_image_get_data (dst_img); + srcbuf = pixman_image_get_data (src_img); + + src_x = lcg_rand_n (src_width); + src_y = lcg_rand_n (src_height); + dst_x = lcg_rand_n (dst_width); + dst_y = lcg_rand_n (dst_height); + + mask_img = NULL; + mask_fmt = PIXMAN_null; + mask_x = 0; + mask_y = 0; + maskbuf = NULL; + + if ((src_fmt == PIXMAN_x8r8g8b8 || src_fmt == PIXMAN_x8b8g8r8) && + (lcg_rand_n (4) == 0)) + { + /* PIXBUF */ + mask_fmt = lcg_rand_n (2) ? PIXMAN_a8r8g8b8 : PIXMAN_a8b8g8r8; + mask_img = pixman_image_create_bits (mask_fmt, + src_width, + src_height, + srcbuf, + src_stride); + mask_x = src_x; + mask_y = src_y; + maskbuf = srcbuf; + } + else if (lcg_rand_n (2)) + { + if (lcg_rand_n (2)) + { + mask_img = create_random_image (mask_fmt_list, max_width, max_height, + max_extra_stride, &mask_fmt); + } + else + { + /* solid case */ + mask_img = create_random_image (mask_fmt_list, 1, 1, + max_extra_stride, &mask_fmt); + pixman_image_set_repeat (mask_img, PIXMAN_REPEAT_NORMAL); + } + + if (lcg_rand_n (2)) + pixman_image_set_component_alpha (mask_img, 1); + + mask_x = lcg_rand_n (pixman_image_get_width (mask_img)); + mask_y = lcg_rand_n (pixman_image_get_height (mask_img)); + } + + + w = lcg_rand_n (dst_width - dst_x + 1); + h = lcg_rand_n (dst_height - dst_y + 1); + + if (verbose) + { + printf ("op=%d, src_fmt=%08X, dst_fmt=%08X, mask_fmt=%08X\n", + op, src_fmt, dst_fmt, mask_fmt); + printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n", + src_width, src_height, dst_width, dst_height); + printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n", + src_x, src_y, dst_x, dst_y); + printf ("src_stride=%d, dst_stride=%d\n", + src_stride, dst_stride); + printf ("w=%d, h=%d\n", w, h); + } + + pixman_image_composite (op, src_img, mask_img, dst_img, + src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h); + + if (verbose) + { + int j; + + printf ("---\n"); + for (i = 0; i < dst_height; i++) + { + for (j = 0; j < dst_stride; j++) + { + if (j == (dst_width * PIXMAN_FORMAT_BPP (dst_fmt) + 7) / 8) + printf ("| "); + + printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j)); + } + printf ("\n"); + } + printf ("---\n"); + } + + free_random_image (0, src_img, PIXMAN_null); + crc32 = free_random_image (0, dst_img, dst_fmt); + + if (mask_img) + { + if (srcbuf == maskbuf) + pixman_image_unref(mask_img); + else + free_random_image (0, mask_img, PIXMAN_null); + } + + FLOAT_REGS_CORRUPTION_DETECTOR_FINISH (); + return crc32; +} + +int +main (int argc, const char *argv[]) +{ + int i; + + for (i = 1; i <= 8; i++) + { + initialize_palette (&(rgb_palette[i]), i, TRUE); + initialize_palette (&(y_palette[i]), i, FALSE); + } + + return fuzzer_test_main("blitters", 2000000, + 0xB610300B, + test_composite, argc, argv); +} diff --git a/pixman/test/composite-traps-test.c b/pixman/test/composite-traps-test.c index fceeb1c7d..fa6d8a988 100644 --- a/pixman/test/composite-traps-test.c +++ b/pixman/test/composite-traps-test.c @@ -1,257 +1,257 @@ -/* Based loosely on scaling-test */ - -#include -#include -#include -#include "utils.h" - -#define MAX_SRC_WIDTH 48 -#define MAX_SRC_HEIGHT 48 -#define MAX_DST_WIDTH 48 -#define MAX_DST_HEIGHT 48 -#define MAX_STRIDE 4 - -static pixman_format_code_t formats[] = -{ - PIXMAN_a8r8g8b8, PIXMAN_a8, PIXMAN_r5g6b5, PIXMAN_a1, PIXMAN_a4 -}; - -static pixman_format_code_t mask_formats[] = -{ - PIXMAN_a1, PIXMAN_a4, PIXMAN_a8, -}; - -static pixman_op_t operators[] = -{ - PIXMAN_OP_OVER, PIXMAN_OP_ADD, PIXMAN_OP_SRC, PIXMAN_OP_IN -}; - -#define RANDOM_ELT(array) \ - ((array)[lcg_rand_n(ARRAY_LENGTH((array)))]) - -static void -destroy_bits (pixman_image_t *image, void *data) -{ - fence_free (data); -} - -static pixman_fixed_t -random_fixed (int n) -{ - return lcg_rand_N (n << 16); -} - -/* - * Composite operation with pseudorandom images - */ -uint32_t -test_composite (int testnum, - int verbose) -{ - int i; - pixman_image_t * src_img; - pixman_image_t * dst_img; - pixman_region16_t clip; - int dst_width, dst_height; - int dst_stride; - int dst_x, dst_y; - int dst_bpp; - pixman_op_t op; - uint32_t * dst_bits; - uint32_t crc32; - pixman_format_code_t mask_format, dst_format; - pixman_trapezoid_t *traps; - int src_x, src_y; - int n_traps; - - static pixman_color_t colors[] = - { - { 0xffff, 0xffff, 0xffff, 0xffff }, - { 0x0000, 0x0000, 0x0000, 0x0000 }, - { 0xabcd, 0xabcd, 0x0000, 0xabcd }, - { 0x0000, 0x0000, 0x0000, 0xffff }, - { 0x0101, 0x0101, 0x0101, 0x0101 }, - { 0x7777, 0x6666, 0x5555, 0x9999 }, - }; - - FLOAT_REGS_CORRUPTION_DETECTOR_START (); - - lcg_srand (testnum); - - op = RANDOM_ELT (operators); - mask_format = RANDOM_ELT (mask_formats); - - /* Create source image */ - - if (lcg_rand_n (4) == 0) - { - src_img = pixman_image_create_solid_fill ( - &(colors[lcg_rand_n (ARRAY_LENGTH (colors))])); - - src_x = 10; - src_y = 234; - } - else - { - pixman_format_code_t src_format = RANDOM_ELT(formats); - int src_bpp = (PIXMAN_FORMAT_BPP (src_format) + 7) / 8; - int src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1; - int src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1; - int src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp; - uint32_t *bits; - - src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2); - src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2); - - src_stride = (src_stride + 3) & ~3; - - bits = (uint32_t *)make_random_bytes (src_stride * src_height); - - src_img = pixman_image_create_bits ( - src_format, src_width, src_height, bits, src_stride); - - pixman_image_set_destroy_function (src_img, destroy_bits, bits); - - if (lcg_rand_n (8) == 0) - { - pixman_box16_t clip_boxes[2]; - int n = lcg_rand_n (2) + 1; - - for (i = 0; i < n; i++) - { - clip_boxes[i].x1 = lcg_rand_n (src_width); - clip_boxes[i].y1 = lcg_rand_n (src_height); - clip_boxes[i].x2 = - clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1); - clip_boxes[i].y2 = - clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1); - - if (verbose) - { - printf ("source clip box: [%d,%d-%d,%d]\n", - clip_boxes[i].x1, clip_boxes[i].y1, - clip_boxes[i].x2, clip_boxes[i].y2); - } - } - - pixman_region_init_rects (&clip, clip_boxes, n); - pixman_image_set_clip_region (src_img, &clip); - pixman_image_set_source_clipping (src_img, 1); - pixman_region_fini (&clip); - } - - image_endian_swap (src_img); - } - - /* Create destination image */ - { - dst_format = RANDOM_ELT(formats); - dst_bpp = (PIXMAN_FORMAT_BPP (dst_format) + 7) / 8; - dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1; - dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1; - dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp; - dst_stride = (dst_stride + 3) & ~3; - - dst_bits = (uint32_t *)make_random_bytes (dst_stride * dst_height); - - dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2); - dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2); - - dst_img = pixman_image_create_bits ( - dst_format, dst_width, dst_height, dst_bits, dst_stride); - - image_endian_swap (dst_img); - } - - /* Create traps */ - { - int i; - - n_traps = lcg_rand_n (25); - traps = fence_malloc (n_traps * sizeof (pixman_trapezoid_t)); - - for (i = 0; i < n_traps; ++i) - { - pixman_trapezoid_t *t = &(traps[i]); - - t->top = random_fixed (MAX_DST_HEIGHT) - MAX_DST_HEIGHT / 2; - t->bottom = t->top + random_fixed (MAX_DST_HEIGHT); - t->left.p1.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2; - t->left.p1.y = t->top - random_fixed (50); - t->left.p2.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2; - t->left.p2.y = t->bottom + random_fixed (50); - t->right.p1.x = t->left.p1.x + random_fixed (MAX_DST_WIDTH); - t->right.p1.y = t->top - random_fixed (50); - t->right.p2.x = t->left.p2.x + random_fixed (MAX_DST_WIDTH); - t->right.p2.y = t->bottom - random_fixed (50); - } - } - - if (lcg_rand_n (8) == 0) - { - pixman_box16_t clip_boxes[2]; - int n = lcg_rand_n (2) + 1; - for (i = 0; i < n; i++) - { - clip_boxes[i].x1 = lcg_rand_n (dst_width); - clip_boxes[i].y1 = lcg_rand_n (dst_height); - clip_boxes[i].x2 = - clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1); - clip_boxes[i].y2 = - clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1); - - if (verbose) - { - printf ("destination clip box: [%d,%d-%d,%d]\n", - clip_boxes[i].x1, clip_boxes[i].y1, - clip_boxes[i].x2, clip_boxes[i].y2); - } - } - pixman_region_init_rects (&clip, clip_boxes, n); - pixman_image_set_clip_region (dst_img, &clip); - pixman_region_fini (&clip); - } - - pixman_composite_trapezoids (op, src_img, dst_img, mask_format, - src_x, src_y, dst_x, dst_y, n_traps, traps); - - if (dst_format == PIXMAN_x8r8g8b8) - { - /* ignore unused part */ - for (i = 0; i < dst_stride * dst_height / 4; i++) - dst_bits[i] &= 0xFFFFFF; - } - - image_endian_swap (dst_img); - - if (verbose) - { - int j; - - for (i = 0; i < dst_height; i++) - { - for (j = 0; j < dst_stride; j++) - printf ("%02X ", *((uint8_t *)dst_bits + i * dst_stride + j)); - - printf ("\n"); - } - } - - crc32 = compute_crc32 (0, dst_bits, dst_stride * dst_height); - - fence_free (dst_bits); - - pixman_image_unref (src_img); - pixman_image_unref (dst_img); - fence_free (traps); - - FLOAT_REGS_CORRUPTION_DETECTOR_FINISH (); - return crc32; -} - -int -main (int argc, const char *argv[]) -{ - return fuzzer_test_main("composite traps", 40000, 0xE3112106, - test_composite, argc, argv); -} +/* Based loosely on scaling-test */ + +#include +#include +#include +#include "utils.h" + +#define MAX_SRC_WIDTH 48 +#define MAX_SRC_HEIGHT 48 +#define MAX_DST_WIDTH 48 +#define MAX_DST_HEIGHT 48 +#define MAX_STRIDE 4 + +static pixman_format_code_t formats[] = +{ + PIXMAN_a8r8g8b8, PIXMAN_a8, PIXMAN_r5g6b5, PIXMAN_a1, PIXMAN_a4 +}; + +static pixman_format_code_t mask_formats[] = +{ + PIXMAN_a1, PIXMAN_a4, PIXMAN_a8, +}; + +static pixman_op_t operators[] = +{ + PIXMAN_OP_OVER, PIXMAN_OP_ADD, PIXMAN_OP_SRC, PIXMAN_OP_IN +}; + +#define RANDOM_ELT(array) \ + ((array)[lcg_rand_n(ARRAY_LENGTH((array)))]) + +static void +destroy_bits (pixman_image_t *image, void *data) +{ + fence_free (data); +} + +static pixman_fixed_t +random_fixed (int n) +{ + return lcg_rand_N (n << 16); +} + +/* + * Composite operation with pseudorandom images + */ +uint32_t +test_composite (int testnum, + int verbose) +{ + int i; + pixman_image_t * src_img; + pixman_image_t * dst_img; + pixman_region16_t clip; + int dst_width, dst_height; + int dst_stride; + int dst_x, dst_y; + int dst_bpp; + pixman_op_t op; + uint32_t * dst_bits; + uint32_t crc32; + pixman_format_code_t mask_format, dst_format; + pixman_trapezoid_t *traps; + int src_x, src_y; + int n_traps; + + static pixman_color_t colors[] = + { + { 0xffff, 0xffff, 0xffff, 0xffff }, + { 0x0000, 0x0000, 0x0000, 0x0000 }, + { 0xabcd, 0xabcd, 0x0000, 0xabcd }, + { 0x0000, 0x0000, 0x0000, 0xffff }, + { 0x0101, 0x0101, 0x0101, 0x0101 }, + { 0x7777, 0x6666, 0x5555, 0x9999 }, + }; + + FLOAT_REGS_CORRUPTION_DETECTOR_START (); + + lcg_srand (testnum); + + op = RANDOM_ELT (operators); + mask_format = RANDOM_ELT (mask_formats); + + /* Create source image */ + + if (lcg_rand_n (4) == 0) + { + src_img = pixman_image_create_solid_fill ( + &(colors[lcg_rand_n (ARRAY_LENGTH (colors))])); + + src_x = 10; + src_y = 234; + } + else + { + pixman_format_code_t src_format = RANDOM_ELT(formats); + int src_bpp = (PIXMAN_FORMAT_BPP (src_format) + 7) / 8; + int src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1; + int src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1; + int src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp; + uint32_t *bits; + + src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2); + src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2); + + src_stride = (src_stride + 3) & ~3; + + bits = (uint32_t *)make_random_bytes (src_stride * src_height); + + src_img = pixman_image_create_bits ( + src_format, src_width, src_height, bits, src_stride); + + pixman_image_set_destroy_function (src_img, destroy_bits, bits); + + if (lcg_rand_n (8) == 0) + { + pixman_box16_t clip_boxes[2]; + int n = lcg_rand_n (2) + 1; + + for (i = 0; i < n; i++) + { + clip_boxes[i].x1 = lcg_rand_n (src_width); + clip_boxes[i].y1 = lcg_rand_n (src_height); + clip_boxes[i].x2 = + clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1); + clip_boxes[i].y2 = + clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1); + + if (verbose) + { + printf ("source clip box: [%d,%d-%d,%d]\n", + clip_boxes[i].x1, clip_boxes[i].y1, + clip_boxes[i].x2, clip_boxes[i].y2); + } + } + + pixman_region_init_rects (&clip, clip_boxes, n); + pixman_image_set_clip_region (src_img, &clip); + pixman_image_set_source_clipping (src_img, 1); + pixman_region_fini (&clip); + } + + image_endian_swap (src_img); + } + + /* Create destination image */ + { + dst_format = RANDOM_ELT(formats); + dst_bpp = (PIXMAN_FORMAT_BPP (dst_format) + 7) / 8; + dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1; + dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1; + dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp; + dst_stride = (dst_stride + 3) & ~3; + + dst_bits = (uint32_t *)make_random_bytes (dst_stride * dst_height); + + dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2); + dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2); + + dst_img = pixman_image_create_bits ( + dst_format, dst_width, dst_height, dst_bits, dst_stride); + + image_endian_swap (dst_img); + } + + /* Create traps */ + { + int i; + + n_traps = lcg_rand_n (25); + traps = fence_malloc (n_traps * sizeof (pixman_trapezoid_t)); + + for (i = 0; i < n_traps; ++i) + { + pixman_trapezoid_t *t = &(traps[i]); + + t->top = random_fixed (MAX_DST_HEIGHT) - MAX_DST_HEIGHT / 2; + t->bottom = t->top + random_fixed (MAX_DST_HEIGHT); + t->left.p1.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2; + t->left.p1.y = t->top - random_fixed (50); + t->left.p2.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2; + t->left.p2.y = t->bottom + random_fixed (50); + t->right.p1.x = t->left.p1.x + random_fixed (MAX_DST_WIDTH); + t->right.p1.y = t->top - random_fixed (50); + t->right.p2.x = t->left.p2.x + random_fixed (MAX_DST_WIDTH); + t->right.p2.y = t->bottom - random_fixed (50); + } + } + + if (lcg_rand_n (8) == 0) + { + pixman_box16_t clip_boxes[2]; + int n = lcg_rand_n (2) + 1; + for (i = 0; i < n; i++) + { + clip_boxes[i].x1 = lcg_rand_n (dst_width); + clip_boxes[i].y1 = lcg_rand_n (dst_height); + clip_boxes[i].x2 = + clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1); + clip_boxes[i].y2 = + clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1); + + if (verbose) + { + printf ("destination clip box: [%d,%d-%d,%d]\n", + clip_boxes[i].x1, clip_boxes[i].y1, + clip_boxes[i].x2, clip_boxes[i].y2); + } + } + pixman_region_init_rects (&clip, clip_boxes, n); + pixman_image_set_clip_region (dst_img, &clip); + pixman_region_fini (&clip); + } + + pixman_composite_trapezoids (op, src_img, dst_img, mask_format, + src_x, src_y, dst_x, dst_y, n_traps, traps); + + if (dst_format == PIXMAN_x8r8g8b8) + { + /* ignore unused part */ + for (i = 0; i < dst_stride * dst_height / 4; i++) + dst_bits[i] &= 0xFFFFFF; + } + + image_endian_swap (dst_img); + + if (verbose) + { + int j; + + for (i = 0; i < dst_height; i++) + { + for (j = 0; j < dst_stride; j++) + printf ("%02X ", *((uint8_t *)dst_bits + i * dst_stride + j)); + + printf ("\n"); + } + } + + crc32 = compute_crc32 (0, dst_bits, dst_stride * dst_height); + + fence_free (dst_bits); + + pixman_image_unref (src_img); + pixman_image_unref (dst_img); + fence_free (traps); + + FLOAT_REGS_CORRUPTION_DETECTOR_FINISH (); + return crc32; +} + +int +main (int argc, const char *argv[]) +{ + return fuzzer_test_main("composite traps", 40000, 0xE3112106, + test_composite, argc, argv); +} diff --git a/pixman/test/composite.c b/pixman/test/composite.c index fccf18b53..edea9a96b 100644 --- a/pixman/test/composite.c +++ b/pixman/test/composite.c @@ -1,921 +1,921 @@ -/* - * Copyright © 2005 Eric Anholt - * Copyright © 2009 Chris Wilson - * Copyright © 2010 Soeren Sandmann - * Copyright © 2010 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 Eric Anholt not be used in - * advertising or publicity pertaining to distribution of the software without - * specific, written prior permission. Eric Anholt makes no - * representations about the suitability of this software for any purpose. It - * is provided "as is" without express or implied warranty. - * - * ERIC ANHOLT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO - * EVENT SHALL ERIC ANHOLT 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. - */ -#define PIXMAN_USE_INTERNAL_API -#include -#include -#include /* abort() */ -#include -#include -#include -#include "utils.h" - -typedef struct color_t color_t; -typedef struct format_t format_t; -typedef struct image_t image_t; -typedef struct operator_t operator_t; - -struct color_t -{ - double r, g, b, a; -}; - -struct format_t -{ - pixman_format_code_t format; - const char *name; -}; - -static const color_t colors[] = -{ - { 1.0, 1.0, 1.0, 1.0 }, - { 1.0, 1.0, 1.0, 0.0 }, - { 0.0, 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 0.0, 0.0 }, - { 1.0, 0.0, 0.0, 1.0 }, - { 0.0, 1.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0, 1.0 }, - { 0.5, 0.0, 0.0, 0.5 }, -}; - -static uint16_t -_color_double_to_short (double d) -{ - uint32_t i; - - i = (uint32_t) (d * 65536); - i -= (i >> 16); - - return i; -} - -static void -compute_pixman_color (const color_t *color, - pixman_color_t *out) -{ - out->red = _color_double_to_short (color->r); - out->green = _color_double_to_short (color->g); - out->blue = _color_double_to_short (color->b); - out->alpha = _color_double_to_short (color->a); -} - -#define REPEAT 0x01000000 -#define FLAGS 0xff000000 - -static const int sizes[] = -{ - 0, - 1, - 1 | REPEAT, - 10 -}; - -static const format_t formats[] = -{ -#define P(x) { PIXMAN_##x, #x } - - /* 32 bpp formats */ - P(a8r8g8b8), - P(x8r8g8b8), - P(a8b8g8r8), - P(x8b8g8r8), - P(b8g8r8a8), - P(b8g8r8x8), - P(r8g8b8a8), - P(r8g8b8x8), - P(x2r10g10b10), - P(x2b10g10r10), - P(a2r10g10b10), - P(a2b10g10r10), - - /* 24 bpp formats */ - P(r8g8b8), - P(b8g8r8), - P(r5g6b5), - P(b5g6r5), - - /* 16 bpp formats */ - P(x1r5g5b5), - P(x1b5g5r5), - P(a1r5g5b5), - P(a1b5g5r5), - P(a4b4g4r4), - P(x4b4g4r4), - P(a4r4g4b4), - P(x4r4g4b4), - - /* 8 bpp formats */ - P(a8), - P(r3g3b2), - P(b2g3r3), - P(a2r2g2b2), - P(a2b2g2r2), - P(x4a4), - - /* 4 bpp formats */ - P(a4), - P(r1g2b1), - P(b1g2r1), - P(a1r1g1b1), - P(a1b1g1r1), - - /* 1 bpp formats */ - P(a1) -#undef P -}; - -struct image_t -{ - pixman_image_t *image; - const format_t *format; - const color_t *color; - pixman_repeat_t repeat; - int size; -}; - -struct operator_t -{ - pixman_op_t op; - const char *name; -}; - -static const operator_t operators[] = -{ -#define P(x) { PIXMAN_OP_##x, #x } - P(CLEAR), - P(SRC), - P(DST), - P(OVER), - P(OVER_REVERSE), - P(IN), - P(IN_REVERSE), - P(OUT), - P(OUT_REVERSE), - P(ATOP), - P(ATOP_REVERSE), - P(XOR), - P(ADD), - P(SATURATE), - - P(DISJOINT_CLEAR), - P(DISJOINT_SRC), - P(DISJOINT_DST), - P(DISJOINT_OVER), - P(DISJOINT_OVER_REVERSE), - P(DISJOINT_IN), - P(DISJOINT_IN_REVERSE), - P(DISJOINT_OUT), - P(DISJOINT_OUT_REVERSE), - P(DISJOINT_ATOP), - P(DISJOINT_ATOP_REVERSE), - P(DISJOINT_XOR), - - P(CONJOINT_CLEAR), - P(CONJOINT_SRC), - P(CONJOINT_DST), - P(CONJOINT_OVER), - P(CONJOINT_OVER_REVERSE), - P(CONJOINT_IN), - P(CONJOINT_IN_REVERSE), - P(CONJOINT_OUT), - P(CONJOINT_OUT_REVERSE), - P(CONJOINT_ATOP), - P(CONJOINT_ATOP_REVERSE), - P(CONJOINT_XOR), -#undef P -}; - -static double -calc_op (pixman_op_t op, double src, double dst, double srca, double dsta) -{ -#define mult_chan(src, dst, Fa, Fb) MIN ((src) * (Fa) + (dst) * (Fb), 1.0) - - double Fa, Fb; - - switch (op) - { - case PIXMAN_OP_CLEAR: - case PIXMAN_OP_DISJOINT_CLEAR: - case PIXMAN_OP_CONJOINT_CLEAR: - return mult_chan (src, dst, 0.0, 0.0); - - case PIXMAN_OP_SRC: - case PIXMAN_OP_DISJOINT_SRC: - case PIXMAN_OP_CONJOINT_SRC: - return mult_chan (src, dst, 1.0, 0.0); - - case PIXMAN_OP_DST: - case PIXMAN_OP_DISJOINT_DST: - case PIXMAN_OP_CONJOINT_DST: - return mult_chan (src, dst, 0.0, 1.0); - - case PIXMAN_OP_OVER: - return mult_chan (src, dst, 1.0, 1.0 - srca); - - case PIXMAN_OP_OVER_REVERSE: - return mult_chan (src, dst, 1.0 - dsta, 1.0); - - case PIXMAN_OP_IN: - return mult_chan (src, dst, dsta, 0.0); - - case PIXMAN_OP_IN_REVERSE: - return mult_chan (src, dst, 0.0, srca); - - case PIXMAN_OP_OUT: - return mult_chan (src, dst, 1.0 - dsta, 0.0); - - case PIXMAN_OP_OUT_REVERSE: - return mult_chan (src, dst, 0.0, 1.0 - srca); - - case PIXMAN_OP_ATOP: - return mult_chan (src, dst, dsta, 1.0 - srca); - - case PIXMAN_OP_ATOP_REVERSE: - return mult_chan (src, dst, 1.0 - dsta, srca); - - case PIXMAN_OP_XOR: - return mult_chan (src, dst, 1.0 - dsta, 1.0 - srca); - - case PIXMAN_OP_ADD: - return mult_chan (src, dst, 1.0, 1.0); - - case PIXMAN_OP_SATURATE: - case PIXMAN_OP_DISJOINT_OVER_REVERSE: - if (srca == 0.0) - Fa = 1.0; - else - Fa = MIN (1.0, (1.0 - dsta) / srca); - return mult_chan (src, dst, Fa, 1.0); - - case PIXMAN_OP_DISJOINT_OVER: - if (dsta == 0.0) - Fb = 1.0; - else - Fb = MIN (1.0, (1.0 - srca) / dsta); - return mult_chan (src, dst, 1.0, Fb); - - case PIXMAN_OP_DISJOINT_IN: - if (srca == 0.0) - Fa = 0.0; - else - Fa = MAX (0.0, 1.0 - (1.0 - dsta) / srca); - return mult_chan (src, dst, Fa, 0.0); - - case PIXMAN_OP_DISJOINT_IN_REVERSE: - if (dsta == 0.0) - Fb = 0.0; - else - Fb = MAX (0.0, 1.0 - (1.0 - srca) / dsta); - return mult_chan (src, dst, 0.0, Fb); - - case PIXMAN_OP_DISJOINT_OUT: - if (srca == 0.0) - Fa = 1.0; - else - Fa = MIN (1.0, (1.0 - dsta) / srca); - return mult_chan (src, dst, Fa, 0.0); - - case PIXMAN_OP_DISJOINT_OUT_REVERSE: - if (dsta == 0.0) - Fb = 1.0; - else - Fb = MIN (1.0, (1.0 - srca) / dsta); - return mult_chan (src, dst, 0.0, Fb); - - case PIXMAN_OP_DISJOINT_ATOP: - if (srca == 0.0) - Fa = 0.0; - else - Fa = MAX (0.0, 1.0 - (1.0 - dsta) / srca); - if (dsta == 0.0) - Fb = 1.0; - else - Fb = MIN (1.0, (1.0 - srca) / dsta); - return mult_chan (src, dst, Fa, Fb); - - case PIXMAN_OP_DISJOINT_ATOP_REVERSE: - if (srca == 0.0) - Fa = 1.0; - else - Fa = MIN (1.0, (1.0 - dsta) / srca); - if (dsta == 0.0) - Fb = 0.0; - else - Fb = MAX (0.0, 1.0 - (1.0 - srca) / dsta); - return mult_chan (src, dst, Fa, Fb); - - case PIXMAN_OP_DISJOINT_XOR: - if (srca == 0.0) - Fa = 1.0; - else - Fa = MIN (1.0, (1.0 - dsta) / srca); - if (dsta == 0.0) - Fb = 1.0; - else - Fb = MIN (1.0, (1.0 - srca) / dsta); - return mult_chan (src, dst, Fa, Fb); - - case PIXMAN_OP_CONJOINT_OVER: - if (dsta == 0.0) - Fb = 0.0; - else - Fb = MAX (0.0, 1.0 - srca / dsta); - return mult_chan (src, dst, 1.0, Fb); - - case PIXMAN_OP_CONJOINT_OVER_REVERSE: - if (srca == 0.0) - Fa = 0.0; - else - Fa = MAX (0.0, 1.0 - dsta / srca); - return mult_chan (src, dst, Fa, 1.0); - - case PIXMAN_OP_CONJOINT_IN: - if (srca == 0.0) - Fa = 1.0; - else - Fa = MIN (1.0, dsta / srca); - return mult_chan (src, dst, Fa, 0.0); - - case PIXMAN_OP_CONJOINT_IN_REVERSE: - if (dsta == 0.0) - Fb = 1.0; - else - Fb = MIN (1.0, srca / dsta); - return mult_chan (src, dst, 0.0, Fb); - - case PIXMAN_OP_CONJOINT_OUT: - if (srca == 0.0) - Fa = 0.0; - else - Fa = MAX (0.0, 1.0 - dsta / srca); - return mult_chan (src, dst, Fa, 0.0); - - case PIXMAN_OP_CONJOINT_OUT_REVERSE: - if (dsta == 0.0) - Fb = 0.0; - else - Fb = MAX (0.0, 1.0 - srca / dsta); - return mult_chan (src, dst, 0.0, Fb); - - case PIXMAN_OP_CONJOINT_ATOP: - if (srca == 0.0) - Fa = 1.0; - else - Fa = MIN (1.0, dsta / srca); - if (dsta == 0.0) - Fb = 0.0; - else - Fb = MAX (0.0, 1.0 - srca / dsta); - return mult_chan (src, dst, Fa, Fb); - - case PIXMAN_OP_CONJOINT_ATOP_REVERSE: - if (srca == 0.0) - Fa = 0.0; - else - Fa = MAX (0.0, 1.0 - dsta / srca); - if (dsta == 0.0) - Fb = 1.0; - else - Fb = MIN (1.0, srca / dsta); - return mult_chan (src, dst, Fa, Fb); - - case PIXMAN_OP_CONJOINT_XOR: - if (srca == 0.0) - Fa = 0.0; - else - Fa = MAX (0.0, 1.0 - dsta / srca); - if (dsta == 0.0) - Fb = 0.0; - else - Fb = MAX (0.0, 1.0 - srca / dsta); - return mult_chan (src, dst, Fa, Fb); - - case PIXMAN_OP_MULTIPLY: - case PIXMAN_OP_SCREEN: - case PIXMAN_OP_OVERLAY: - case PIXMAN_OP_DARKEN: - case PIXMAN_OP_LIGHTEN: - case PIXMAN_OP_COLOR_DODGE: - case PIXMAN_OP_COLOR_BURN: - case PIXMAN_OP_HARD_LIGHT: - case PIXMAN_OP_SOFT_LIGHT: - case PIXMAN_OP_DIFFERENCE: - case PIXMAN_OP_EXCLUSION: - case PIXMAN_OP_HSL_HUE: - case PIXMAN_OP_HSL_SATURATION: - case PIXMAN_OP_HSL_COLOR: - case PIXMAN_OP_HSL_LUMINOSITY: - default: - abort(); - return 0; /* silence MSVC */ - } -#undef mult_chan -} - -static void -do_composite (pixman_op_t op, - const color_t *src, - const color_t *mask, - const color_t *dst, - color_t *result, - pixman_bool_t component_alpha) -{ - color_t srcval, srcalpha; - - if (mask == NULL) - { - srcval = *src; - - srcalpha.r = src->a; - srcalpha.g = src->a; - srcalpha.b = src->a; - srcalpha.a = src->a; - } - else if (component_alpha) - { - srcval.r = src->r * mask->r; - srcval.g = src->g * mask->g; - srcval.b = src->b * mask->b; - srcval.a = src->a * mask->a; - - srcalpha.r = src->a * mask->r; - srcalpha.g = src->a * mask->g; - srcalpha.b = src->a * mask->b; - srcalpha.a = src->a * mask->a; - } - else - { - srcval.r = src->r * mask->a; - srcval.g = src->g * mask->a; - srcval.b = src->b * mask->a; - srcval.a = src->a * mask->a; - - srcalpha.r = src->a * mask->a; - srcalpha.g = src->a * mask->a; - srcalpha.b = src->a * mask->a; - srcalpha.a = src->a * mask->a; - } - - result->r = calc_op (op, srcval.r, dst->r, srcalpha.r, dst->a); - result->g = calc_op (op, srcval.g, dst->g, srcalpha.g, dst->a); - result->b = calc_op (op, srcval.b, dst->b, srcalpha.b, dst->a); - result->a = calc_op (op, srcval.a, dst->a, srcalpha.a, dst->a); -} - -static void -color_correct (pixman_format_code_t format, - color_t *color) -{ -#define MASK(x) ((1 << (x)) - 1) -#define round_pix(pix, m) \ - ((int)((pix) * (MASK(m)) + .5) / (double) (MASK(m))) - - if (PIXMAN_FORMAT_R (format) == 0) - { - color->r = 0.0; - color->g = 0.0; - color->b = 0.0; - } - else - { - color->r = round_pix (color->r, PIXMAN_FORMAT_R (format)); - color->g = round_pix (color->g, PIXMAN_FORMAT_G (format)); - color->b = round_pix (color->b, PIXMAN_FORMAT_B (format)); - } - - if (PIXMAN_FORMAT_A (format) == 0) - color->a = 1.0; - else - color->a = round_pix (color->a, PIXMAN_FORMAT_A (format)); - -#undef round_pix -#undef MASK -} - -static void -get_pixel (pixman_image_t *image, - pixman_format_code_t format, - color_t *color) -{ -#define MASK(N) ((1UL << (N))-1) - - unsigned long rs, gs, bs, as; - int a, r, g, b; - unsigned long val; - - val = *(unsigned long *) pixman_image_get_data (image); -#ifdef WORDS_BIGENDIAN - val >>= 8 * sizeof(val) - PIXMAN_FORMAT_BPP (format); -#endif - - /* Number of bits in each channel */ - a = PIXMAN_FORMAT_A (format); - r = PIXMAN_FORMAT_R (format); - g = PIXMAN_FORMAT_G (format); - b = PIXMAN_FORMAT_B (format); - - switch (PIXMAN_FORMAT_TYPE (format)) - { - case PIXMAN_TYPE_ARGB: - bs = 0; - gs = b + bs; - rs = g + gs; - as = r + rs; - break; - - case PIXMAN_TYPE_ABGR: - rs = 0; - gs = r + rs; - bs = g + gs; - as = b + bs; - break; - - case PIXMAN_TYPE_BGRA: - as = 0; - rs = PIXMAN_FORMAT_BPP (format) - (b + g + r); - gs = r + rs; - bs = g + gs; - break; - - case PIXMAN_TYPE_RGBA: - as = 0; - bs = PIXMAN_FORMAT_BPP (format) - (b + g + r); - gs = b + bs; - rs = g + gs; - break; - - case PIXMAN_TYPE_A: - as = 0; - rs = 0; - gs = 0; - bs = 0; - break; - - case PIXMAN_TYPE_OTHER: - case PIXMAN_TYPE_COLOR: - case PIXMAN_TYPE_GRAY: - case PIXMAN_TYPE_YUY2: - case PIXMAN_TYPE_YV12: - default: - abort (); - as = 0; - rs = 0; - gs = 0; - bs = 0; - break; - } - - if (MASK (a) != 0) - color->a = ((val >> as) & MASK (a)) / (double) MASK (a); - else - color->a = 1.0; - - if (MASK (r) != 0) - { - color->r = ((val >> rs) & MASK (r)) / (double) MASK (r); - color->g = ((val >> gs) & MASK (g)) / (double) MASK (g); - color->b = ((val >> bs) & MASK (b)) / (double) MASK (b); - } - else - { - color->r = 0.0; - color->g = 0.0; - color->b = 0.0; - } - -#undef MASK -} - -static double -eval_diff (color_t *expected, color_t *test, pixman_format_code_t format) -{ - double rscale, gscale, bscale, ascale; - double rdiff, gdiff, bdiff, adiff; - - rscale = 1.0 * ((1 << PIXMAN_FORMAT_R (format)) - 1); - gscale = 1.0 * ((1 << PIXMAN_FORMAT_G (format)) - 1); - bscale = 1.0 * ((1 << PIXMAN_FORMAT_B (format)) - 1); - ascale = 1.0 * ((1 << PIXMAN_FORMAT_A (format)) - 1); - - rdiff = fabs (test->r - expected->r) * rscale; - bdiff = fabs (test->g - expected->g) * gscale; - gdiff = fabs (test->b - expected->b) * bscale; - adiff = fabs (test->a - expected->a) * ascale; - - return MAX (MAX (MAX (rdiff, gdiff), bdiff), adiff); -} - -static char * -describe_image (image_t *info, char *buf) -{ - if (info->size) - { - sprintf (buf, "%s %dx%d%s", - info->format->name, - info->size, info->size, - info->repeat ? "R" :""); - } - else - { - sprintf (buf, "solid"); - } - - return buf; -} - -/* Test a composite of a given operation, source, mask, and destination - * picture. - * Fills the window, and samples from the 0,0 pixel corner. - */ -static pixman_bool_t -composite_test (image_t *dst, - const operator_t *op, - image_t *src, - image_t *mask, - pixman_bool_t component_alpha) -{ - pixman_color_t fill; - pixman_rectangle16_t rect; - color_t expected, result, tdst, tsrc, tmsk; - double diff; - pixman_bool_t success = TRUE; - - compute_pixman_color (dst->color, &fill); - rect.x = rect.y = 0; - rect.width = rect.height = dst->size; - pixman_image_fill_rectangles (PIXMAN_OP_SRC, dst->image, - &fill, 1, &rect); - - if (mask != NULL) - { - pixman_image_set_component_alpha (mask->image, component_alpha); - pixman_image_composite (op->op, src->image, mask->image, dst->image, - 0, 0, - 0, 0, - 0, 0, - dst->size, dst->size); - - tmsk = *mask->color; - if (mask->size) - { - color_correct (mask->format->format, &tmsk); - - if (component_alpha && - PIXMAN_FORMAT_R (mask->format->format) == 0) - { - /* Ax component-alpha masks expand alpha into - * all color channels. - */ - tmsk.r = tmsk.g = tmsk.b = tmsk.a; - } - } - } - else - { - pixman_image_composite (op->op, src->image, NULL, dst->image, - 0, 0, - 0, 0, - 0, 0, - dst->size, dst->size); - } - get_pixel (dst->image, dst->format->format, &result); - - tdst = *dst->color; - color_correct (dst->format->format, &tdst); - tsrc = *src->color; - if (src->size) - color_correct (src->format->format, &tsrc); - do_composite (op->op, &tsrc, mask ? &tmsk : NULL, &tdst, - &expected, component_alpha); - color_correct (dst->format->format, &expected); - - diff = eval_diff (&expected, &result, dst->format->format); - - /* FIXME: We should find out what deviation is acceptable. 3.0 - * is clearly absurd for 2 bit formats for example. On the other - * hand currently 1.0 does not work. - */ - if (diff > 3.0) - { - char buf[40]; - - sprintf (buf, "%s %scomposite", - op->name, - component_alpha ? "CA " : ""); - - printf ("%s test error of %.4f --\n" - " R G B A\n" - "got: %.2f %.2f %.2f %.2f [%08lx]\n" - "expected: %.2f %.2f %.2f %.2f\n", - buf, diff, - result.r, result.g, result.b, result.a, - *(unsigned long *) pixman_image_get_data (dst->image), - expected.r, expected.g, expected.b, expected.a); - - if (mask != NULL) - { - printf ("src color: %.2f %.2f %.2f %.2f\n" - "msk color: %.2f %.2f %.2f %.2f\n" - "dst color: %.2f %.2f %.2f %.2f\n", - src->color->r, src->color->g, - src->color->b, src->color->a, - mask->color->r, mask->color->g, - mask->color->b, mask->color->a, - dst->color->r, dst->color->g, - dst->color->b, dst->color->a); - printf ("src: %s, ", describe_image (src, buf)); - printf ("mask: %s, ", describe_image (mask, buf)); - printf ("dst: %s\n\n", describe_image (dst, buf)); - } - else - { - printf ("src color: %.2f %.2f %.2f %.2f\n" - "dst color: %.2f %.2f %.2f %.2f\n", - src->color->r, src->color->g, - src->color->b, src->color->a, - dst->color->r, dst->color->g, - dst->color->b, dst->color->a); - printf ("src: %s, ", describe_image (src, buf)); - printf ("dst: %s\n\n", describe_image (dst, buf)); - } - - success = FALSE; - } - - return success; -} - -static void -image_init (image_t *info, - int color, - int format, - int size) -{ - pixman_color_t fill; - - info->color = &colors[color]; - compute_pixman_color (info->color, &fill); - - info->format = &formats[format]; - info->size = sizes[size] & ~FLAGS; - info->repeat = PIXMAN_REPEAT_NONE; - - if (info->size) - { - pixman_rectangle16_t rect; - - info->image = pixman_image_create_bits (info->format->format, - info->size, info->size, - NULL, 0); - - rect.x = rect.y = 0; - rect.width = rect.height = info->size; - pixman_image_fill_rectangles (PIXMAN_OP_SRC, info->image, &fill, - 1, &rect); - - if (size & REPEAT) - { - pixman_image_set_repeat (info->image, PIXMAN_REPEAT_NORMAL); - info->repeat = PIXMAN_REPEAT_NORMAL; - } - } - else - { - info->image = pixman_image_create_solid_fill (&fill); - } -} - -static void -image_fini (image_t *info) -{ - pixman_image_unref (info->image); -} - -static int -random_size (void) -{ - return lcg_rand_n (ARRAY_LENGTH (sizes)); -} - -static int -random_color (void) -{ - return lcg_rand_n (ARRAY_LENGTH (colors)); -} - -static int -random_format (void) -{ - return lcg_rand_n (ARRAY_LENGTH (formats)); -} - -static pixman_bool_t -run_test (uint32_t seed) -{ - image_t src, mask, dst; - const operator_t *op; - int ca; - int ok; - - lcg_srand (seed); - - image_init (&dst, random_color(), random_format(), 1); - image_init (&src, random_color(), random_format(), random_size()); - image_init (&mask, random_color(), random_format(), random_size()); - - op = &(operators [lcg_rand_n (ARRAY_LENGTH (operators))]); - - ca = lcg_rand_n (3); - - switch (ca) - { - case 0: - ok = composite_test (&dst, op, &src, NULL, FALSE); - break; - case 1: - ok = composite_test (&dst, op, &src, &mask, FALSE); - break; - case 2: - ok = composite_test (&dst, op, &src, &mask, - mask.size? TRUE : FALSE); - break; - default: - ok = FALSE; - break; - } - - image_fini (&src); - image_fini (&mask); - image_fini (&dst); - - return ok; -} - -int -main (int argc, char **argv) -{ -#define N_TESTS (8 * 1024 * 1024) - int result = 0; - uint32_t i, seed; - - if (argc > 1) - { - char *end; - - i = strtol (argv[1], &end, 0); - - if (end != argv[1]) - { - if (!run_test (i)) - return 1; - else - return 0; - } - else - { - printf ("Usage:\n\n %s \n\n", argv[0]); - return -1; - } - } - - if (getenv ("PIXMAN_RANDOMIZE_TESTS")) - seed = get_random_seed(); - else - seed = 1; - -#ifdef USE_OPENMP -# pragma omp parallel for default(none) shared(result, argv, seed) -#endif - for (i = 0; i <= N_TESTS; ++i) - { - if (!result && !run_test (i + seed)) - { - printf ("Test 0x%08X failed.\n", seed + i); - - result = seed + i; - } - } - - return result; -} +/* + * Copyright © 2005 Eric Anholt + * Copyright © 2009 Chris Wilson + * Copyright © 2010 Soeren Sandmann + * Copyright © 2010 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 Eric Anholt not be used in + * advertising or publicity pertaining to distribution of the software without + * specific, written prior permission. Eric Anholt makes no + * representations about the suitability of this software for any purpose. It + * is provided "as is" without express or implied warranty. + * + * ERIC ANHOLT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL ERIC ANHOLT 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. + */ +#define PIXMAN_USE_INTERNAL_API +#include +#include +#include /* abort() */ +#include +#include +#include +#include "utils.h" + +typedef struct color_t color_t; +typedef struct format_t format_t; +typedef struct image_t image_t; +typedef struct operator_t operator_t; + +struct color_t +{ + double r, g, b, a; +}; + +struct format_t +{ + pixman_format_code_t format; + const char *name; +}; + +static const color_t colors[] = +{ + { 1.0, 1.0, 1.0, 1.0 }, + { 1.0, 1.0, 1.0, 0.0 }, + { 0.0, 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 0.0, 0.0 }, + { 1.0, 0.0, 0.0, 1.0 }, + { 0.0, 1.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0, 1.0 }, + { 0.5, 0.0, 0.0, 0.5 }, +}; + +static uint16_t +_color_double_to_short (double d) +{ + uint32_t i; + + i = (uint32_t) (d * 65536); + i -= (i >> 16); + + return i; +} + +static void +compute_pixman_color (const color_t *color, + pixman_color_t *out) +{ + out->red = _color_double_to_short (color->r); + out->green = _color_double_to_short (color->g); + out->blue = _color_double_to_short (color->b); + out->alpha = _color_double_to_short (color->a); +} + +#define REPEAT 0x01000000 +#define FLAGS 0xff000000 + +static const int sizes[] = +{ + 0, + 1, + 1 | REPEAT, + 10 +}; + +static const format_t formats[] = +{ +#define P(x) { PIXMAN_##x, #x } + + /* 32 bpp formats */ + P(a8r8g8b8), + P(x8r8g8b8), + P(a8b8g8r8), + P(x8b8g8r8), + P(b8g8r8a8), + P(b8g8r8x8), + P(r8g8b8a8), + P(r8g8b8x8), + P(x2r10g10b10), + P(x2b10g10r10), + P(a2r10g10b10), + P(a2b10g10r10), + + /* 24 bpp formats */ + P(r8g8b8), + P(b8g8r8), + P(r5g6b5), + P(b5g6r5), + + /* 16 bpp formats */ + P(x1r5g5b5), + P(x1b5g5r5), + P(a1r5g5b5), + P(a1b5g5r5), + P(a4b4g4r4), + P(x4b4g4r4), + P(a4r4g4b4), + P(x4r4g4b4), + + /* 8 bpp formats */ + P(a8), + P(r3g3b2), + P(b2g3r3), + P(a2r2g2b2), + P(a2b2g2r2), + P(x4a4), + + /* 4 bpp formats */ + P(a4), + P(r1g2b1), + P(b1g2r1), + P(a1r1g1b1), + P(a1b1g1r1), + + /* 1 bpp formats */ + P(a1) +#undef P +}; + +struct image_t +{ + pixman_image_t *image; + const format_t *format; + const color_t *color; + pixman_repeat_t repeat; + int size; +}; + +struct operator_t +{ + pixman_op_t op; + const char *name; +}; + +static const operator_t operators[] = +{ +#define P(x) { PIXMAN_OP_##x, #x } + P(CLEAR), + P(SRC), + P(DST), + P(OVER), + P(OVER_REVERSE), + P(IN), + P(IN_REVERSE), + P(OUT), + P(OUT_REVERSE), + P(ATOP), + P(ATOP_REVERSE), + P(XOR), + P(ADD), + P(SATURATE), + + P(DISJOINT_CLEAR), + P(DISJOINT_SRC), + P(DISJOINT_DST), + P(DISJOINT_OVER), + P(DISJOINT_OVER_REVERSE), + P(DISJOINT_IN), + P(DISJOINT_IN_REVERSE), + P(DISJOINT_OUT), + P(DISJOINT_OUT_REVERSE), + P(DISJOINT_ATOP), + P(DISJOINT_ATOP_REVERSE), + P(DISJOINT_XOR), + + P(CONJOINT_CLEAR), + P(CONJOINT_SRC), + P(CONJOINT_DST), + P(CONJOINT_OVER), + P(CONJOINT_OVER_REVERSE), + P(CONJOINT_IN), + P(CONJOINT_IN_REVERSE), + P(CONJOINT_OUT), + P(CONJOINT_OUT_REVERSE), + P(CONJOINT_ATOP), + P(CONJOINT_ATOP_REVERSE), + P(CONJOINT_XOR), +#undef P +}; + +static double +calc_op (pixman_op_t op, double src, double dst, double srca, double dsta) +{ +#define mult_chan(src, dst, Fa, Fb) MIN ((src) * (Fa) + (dst) * (Fb), 1.0) + + double Fa, Fb; + + switch (op) + { + case PIXMAN_OP_CLEAR: + case PIXMAN_OP_DISJOINT_CLEAR: + case PIXMAN_OP_CONJOINT_CLEAR: + return mult_chan (src, dst, 0.0, 0.0); + + case PIXMAN_OP_SRC: + case PIXMAN_OP_DISJOINT_SRC: + case PIXMAN_OP_CONJOINT_SRC: + return mult_chan (src, dst, 1.0, 0.0); + + case PIXMAN_OP_DST: + case PIXMAN_OP_DISJOINT_DST: + case PIXMAN_OP_CONJOINT_DST: + return mult_chan (src, dst, 0.0, 1.0); + + case PIXMAN_OP_OVER: + return mult_chan (src, dst, 1.0, 1.0 - srca); + + case PIXMAN_OP_OVER_REVERSE: + return mult_chan (src, dst, 1.0 - dsta, 1.0); + + case PIXMAN_OP_IN: + return mult_chan (src, dst, dsta, 0.0); + + case PIXMAN_OP_IN_REVERSE: + return mult_chan (src, dst, 0.0, srca); + + case PIXMAN_OP_OUT: + return mult_chan (src, dst, 1.0 - dsta, 0.0); + + case PIXMAN_OP_OUT_REVERSE: + return mult_chan (src, dst, 0.0, 1.0 - srca); + + case PIXMAN_OP_ATOP: + return mult_chan (src, dst, dsta, 1.0 - srca); + + case PIXMAN_OP_ATOP_REVERSE: + return mult_chan (src, dst, 1.0 - dsta, srca); + + case PIXMAN_OP_XOR: + return mult_chan (src, dst, 1.0 - dsta, 1.0 - srca); + + case PIXMAN_OP_ADD: + return mult_chan (src, dst, 1.0, 1.0); + + case PIXMAN_OP_SATURATE: + case PIXMAN_OP_DISJOINT_OVER_REVERSE: + if (srca == 0.0) + Fa = 1.0; + else + Fa = MIN (1.0, (1.0 - dsta) / srca); + return mult_chan (src, dst, Fa, 1.0); + + case PIXMAN_OP_DISJOINT_OVER: + if (dsta == 0.0) + Fb = 1.0; + else + Fb = MIN (1.0, (1.0 - srca) / dsta); + return mult_chan (src, dst, 1.0, Fb); + + case PIXMAN_OP_DISJOINT_IN: + if (srca == 0.0) + Fa = 0.0; + else + Fa = MAX (0.0, 1.0 - (1.0 - dsta) / srca); + return mult_chan (src, dst, Fa, 0.0); + + case PIXMAN_OP_DISJOINT_IN_REVERSE: + if (dsta == 0.0) + Fb = 0.0; + else + Fb = MAX (0.0, 1.0 - (1.0 - srca) / dsta); + return mult_chan (src, dst, 0.0, Fb); + + case PIXMAN_OP_DISJOINT_OUT: + if (srca == 0.0) + Fa = 1.0; + else + Fa = MIN (1.0, (1.0 - dsta) / srca); + return mult_chan (src, dst, Fa, 0.0); + + case PIXMAN_OP_DISJOINT_OUT_REVERSE: + if (dsta == 0.0) + Fb = 1.0; + else + Fb = MIN (1.0, (1.0 - srca) / dsta); + return mult_chan (src, dst, 0.0, Fb); + + case PIXMAN_OP_DISJOINT_ATOP: + if (srca == 0.0) + Fa = 0.0; + else + Fa = MAX (0.0, 1.0 - (1.0 - dsta) / srca); + if (dsta == 0.0) + Fb = 1.0; + else + Fb = MIN (1.0, (1.0 - srca) / dsta); + return mult_chan (src, dst, Fa, Fb); + + case PIXMAN_OP_DISJOINT_ATOP_REVERSE: + if (srca == 0.0) + Fa = 1.0; + else + Fa = MIN (1.0, (1.0 - dsta) / srca); + if (dsta == 0.0) + Fb = 0.0; + else + Fb = MAX (0.0, 1.0 - (1.0 - srca) / dsta); + return mult_chan (src, dst, Fa, Fb); + + case PIXMAN_OP_DISJOINT_XOR: + if (srca == 0.0) + Fa = 1.0; + else + Fa = MIN (1.0, (1.0 - dsta) / srca); + if (dsta == 0.0) + Fb = 1.0; + else + Fb = MIN (1.0, (1.0 - srca) / dsta); + return mult_chan (src, dst, Fa, Fb); + + case PIXMAN_OP_CONJOINT_OVER: + if (dsta == 0.0) + Fb = 0.0; + else + Fb = MAX (0.0, 1.0 - srca / dsta); + return mult_chan (src, dst, 1.0, Fb); + + case PIXMAN_OP_CONJOINT_OVER_REVERSE: + if (srca == 0.0) + Fa = 0.0; + else + Fa = MAX (0.0, 1.0 - dsta / srca); + return mult_chan (src, dst, Fa, 1.0); + + case PIXMAN_OP_CONJOINT_IN: + if (srca == 0.0) + Fa = 1.0; + else + Fa = MIN (1.0, dsta / srca); + return mult_chan (src, dst, Fa, 0.0); + + case PIXMAN_OP_CONJOINT_IN_REVERSE: + if (dsta == 0.0) + Fb = 1.0; + else + Fb = MIN (1.0, srca / dsta); + return mult_chan (src, dst, 0.0, Fb); + + case PIXMAN_OP_CONJOINT_OUT: + if (srca == 0.0) + Fa = 0.0; + else + Fa = MAX (0.0, 1.0 - dsta / srca); + return mult_chan (src, dst, Fa, 0.0); + + case PIXMAN_OP_CONJOINT_OUT_REVERSE: + if (dsta == 0.0) + Fb = 0.0; + else + Fb = MAX (0.0, 1.0 - srca / dsta); + return mult_chan (src, dst, 0.0, Fb); + + case PIXMAN_OP_CONJOINT_ATOP: + if (srca == 0.0) + Fa = 1.0; + else + Fa = MIN (1.0, dsta / srca); + if (dsta == 0.0) + Fb = 0.0; + else + Fb = MAX (0.0, 1.0 - srca / dsta); + return mult_chan (src, dst, Fa, Fb); + + case PIXMAN_OP_CONJOINT_ATOP_REVERSE: + if (srca == 0.0) + Fa = 0.0; + else + Fa = MAX (0.0, 1.0 - dsta / srca); + if (dsta == 0.0) + Fb = 1.0; + else + Fb = MIN (1.0, srca / dsta); + return mult_chan (src, dst, Fa, Fb); + + case PIXMAN_OP_CONJOINT_XOR: + if (srca == 0.0) + Fa = 0.0; + else + Fa = MAX (0.0, 1.0 - dsta / srca); + if (dsta == 0.0) + Fb = 0.0; + else + Fb = MAX (0.0, 1.0 - srca / dsta); + return mult_chan (src, dst, Fa, Fb); + + case PIXMAN_OP_MULTIPLY: + case PIXMAN_OP_SCREEN: + case PIXMAN_OP_OVERLAY: + case PIXMAN_OP_DARKEN: + case PIXMAN_OP_LIGHTEN: + case PIXMAN_OP_COLOR_DODGE: + case PIXMAN_OP_COLOR_BURN: + case PIXMAN_OP_HARD_LIGHT: + case PIXMAN_OP_SOFT_LIGHT: + case PIXMAN_OP_DIFFERENCE: + case PIXMAN_OP_EXCLUSION: + case PIXMAN_OP_HSL_HUE: + case PIXMAN_OP_HSL_SATURATION: + case PIXMAN_OP_HSL_COLOR: + case PIXMAN_OP_HSL_LUMINOSITY: + default: + abort(); + return 0; /* silence MSVC */ + } +#undef mult_chan +} + +static void +do_composite (pixman_op_t op, + const color_t *src, + const color_t *mask, + const color_t *dst, + color_t *result, + pixman_bool_t component_alpha) +{ + color_t srcval, srcalpha; + + if (mask == NULL) + { + srcval = *src; + + srcalpha.r = src->a; + srcalpha.g = src->a; + srcalpha.b = src->a; + srcalpha.a = src->a; + } + else if (component_alpha) + { + srcval.r = src->r * mask->r; + srcval.g = src->g * mask->g; + srcval.b = src->b * mask->b; + srcval.a = src->a * mask->a; + + srcalpha.r = src->a * mask->r; + srcalpha.g = src->a * mask->g; + srcalpha.b = src->a * mask->b; + srcalpha.a = src->a * mask->a; + } + else + { + srcval.r = src->r * mask->a; + srcval.g = src->g * mask->a; + srcval.b = src->b * mask->a; + srcval.a = src->a * mask->a; + + srcalpha.r = src->a * mask->a; + srcalpha.g = src->a * mask->a; + srcalpha.b = src->a * mask->a; + srcalpha.a = src->a * mask->a; + } + + result->r = calc_op (op, srcval.r, dst->r, srcalpha.r, dst->a); + result->g = calc_op (op, srcval.g, dst->g, srcalpha.g, dst->a); + result->b = calc_op (op, srcval.b, dst->b, srcalpha.b, dst->a); + result->a = calc_op (op, srcval.a, dst->a, srcalpha.a, dst->a); +} + +static void +color_correct (pixman_format_code_t format, + color_t *color) +{ +#define MASK(x) ((1 << (x)) - 1) +#define round_pix(pix, m) \ + ((int)((pix) * (MASK(m)) + .5) / (double) (MASK(m))) + + if (PIXMAN_FORMAT_R (format) == 0) + { + color->r = 0.0; + color->g = 0.0; + color->b = 0.0; + } + else + { + color->r = round_pix (color->r, PIXMAN_FORMAT_R (format)); + color->g = round_pix (color->g, PIXMAN_FORMAT_G (format)); + color->b = round_pix (color->b, PIXMAN_FORMAT_B (format)); + } + + if (PIXMAN_FORMAT_A (format) == 0) + color->a = 1.0; + else + color->a = round_pix (color->a, PIXMAN_FORMAT_A (format)); + +#undef round_pix +#undef MASK +} + +static void +get_pixel (pixman_image_t *image, + pixman_format_code_t format, + color_t *color) +{ +#define MASK(N) ((1UL << (N))-1) + + unsigned long rs, gs, bs, as; + int a, r, g, b; + unsigned long val; + + val = *(unsigned long *) pixman_image_get_data (image); +#ifdef WORDS_BIGENDIAN + val >>= 8 * sizeof(val) - PIXMAN_FORMAT_BPP (format); +#endif + + /* Number of bits in each channel */ + a = PIXMAN_FORMAT_A (format); + r = PIXMAN_FORMAT_R (format); + g = PIXMAN_FORMAT_G (format); + b = PIXMAN_FORMAT_B (format); + + switch (PIXMAN_FORMAT_TYPE (format)) + { + case PIXMAN_TYPE_ARGB: + bs = 0; + gs = b + bs; + rs = g + gs; + as = r + rs; + break; + + case PIXMAN_TYPE_ABGR: + rs = 0; + gs = r + rs; + bs = g + gs; + as = b + bs; + break; + + case PIXMAN_TYPE_BGRA: + as = 0; + rs = PIXMAN_FORMAT_BPP (format) - (b + g + r); + gs = r + rs; + bs = g + gs; + break; + + case PIXMAN_TYPE_RGBA: + as = 0; + bs = PIXMAN_FORMAT_BPP (format) - (b + g + r); + gs = b + bs; + rs = g + gs; + break; + + case PIXMAN_TYPE_A: + as = 0; + rs = 0; + gs = 0; + bs = 0; + break; + + case PIXMAN_TYPE_OTHER: + case PIXMAN_TYPE_COLOR: + case PIXMAN_TYPE_GRAY: + case PIXMAN_TYPE_YUY2: + case PIXMAN_TYPE_YV12: + default: + abort (); + as = 0; + rs = 0; + gs = 0; + bs = 0; + break; + } + + if (MASK (a) != 0) + color->a = ((val >> as) & MASK (a)) / (double) MASK (a); + else + color->a = 1.0; + + if (MASK (r) != 0) + { + color->r = ((val >> rs) & MASK (r)) / (double) MASK (r); + color->g = ((val >> gs) & MASK (g)) / (double) MASK (g); + color->b = ((val >> bs) & MASK (b)) / (double) MASK (b); + } + else + { + color->r = 0.0; + color->g = 0.0; + color->b = 0.0; + } + +#undef MASK +} + +static double +eval_diff (color_t *expected, color_t *test, pixman_format_code_t format) +{ + double rscale, gscale, bscale, ascale; + double rdiff, gdiff, bdiff, adiff; + + rscale = 1.0 * ((1 << PIXMAN_FORMAT_R (format)) - 1); + gscale = 1.0 * ((1 << PIXMAN_FORMAT_G (format)) - 1); + bscale = 1.0 * ((1 << PIXMAN_FORMAT_B (format)) - 1); + ascale = 1.0 * ((1 << PIXMAN_FORMAT_A (format)) - 1); + + rdiff = fabs (test->r - expected->r) * rscale; + bdiff = fabs (test->g - expected->g) * gscale; + gdiff = fabs (test->b - expected->b) * bscale; + adiff = fabs (test->a - expected->a) * ascale; + + return MAX (MAX (MAX (rdiff, gdiff), bdiff), adiff); +} + +static char * +describe_image (image_t *info, char *buf) +{ + if (info->size) + { + sprintf (buf, "%s %dx%d%s", + info->format->name, + info->size, info->size, + info->repeat ? "R" :""); + } + else + { + sprintf (buf, "solid"); + } + + return buf; +} + +/* Test a composite of a given operation, source, mask, and destination + * picture. + * Fills the window, and samples from the 0,0 pixel corner. + */ +static pixman_bool_t +composite_test (image_t *dst, + const operator_t *op, + image_t *src, + image_t *mask, + pixman_bool_t component_alpha) +{ + pixman_color_t fill; + pixman_rectangle16_t rect; + color_t expected, result, tdst, tsrc, tmsk; + double diff; + pixman_bool_t success = TRUE; + + compute_pixman_color (dst->color, &fill); + rect.x = rect.y = 0; + rect.width = rect.height = dst->size; + pixman_image_fill_rectangles (PIXMAN_OP_SRC, dst->image, + &fill, 1, &rect); + + if (mask != NULL) + { + pixman_image_set_component_alpha (mask->image, component_alpha); + pixman_image_composite (op->op, src->image, mask->image, dst->image, + 0, 0, + 0, 0, + 0, 0, + dst->size, dst->size); + + tmsk = *mask->color; + if (mask->size) + { + color_correct (mask->format->format, &tmsk); + + if (component_alpha && + PIXMAN_FORMAT_R (mask->format->format) == 0) + { + /* Ax component-alpha masks expand alpha into + * all color channels. + */ + tmsk.r = tmsk.g = tmsk.b = tmsk.a; + } + } + } + else + { + pixman_image_composite (op->op, src->image, NULL, dst->image, + 0, 0, + 0, 0, + 0, 0, + dst->size, dst->size); + } + get_pixel (dst->image, dst->format->format, &result); + + tdst = *dst->color; + color_correct (dst->format->format, &tdst); + tsrc = *src->color; + if (src->size) + color_correct (src->format->format, &tsrc); + do_composite (op->op, &tsrc, mask ? &tmsk : NULL, &tdst, + &expected, component_alpha); + color_correct (dst->format->format, &expected); + + diff = eval_diff (&expected, &result, dst->format->format); + + /* FIXME: We should find out what deviation is acceptable. 3.0 + * is clearly absurd for 2 bit formats for example. On the other + * hand currently 1.0 does not work. + */ + if (diff > 3.0) + { + char buf[40]; + + sprintf (buf, "%s %scomposite", + op->name, + component_alpha ? "CA " : ""); + + printf ("%s test error of %.4f --\n" + " R G B A\n" + "got: %.2f %.2f %.2f %.2f [%08lx]\n" + "expected: %.2f %.2f %.2f %.2f\n", + buf, diff, + result.r, result.g, result.b, result.a, + *(unsigned long *) pixman_image_get_data (dst->image), + expected.r, expected.g, expected.b, expected.a); + + if (mask != NULL) + { + printf ("src color: %.2f %.2f %.2f %.2f\n" + "msk color: %.2f %.2f %.2f %.2f\n" + "dst color: %.2f %.2f %.2f %.2f\n", + src->color->r, src->color->g, + src->color->b, src->color->a, + mask->color->r, mask->color->g, + mask->color->b, mask->color->a, + dst->color->r, dst->color->g, + dst->color->b, dst->color->a); + printf ("src: %s, ", describe_image (src, buf)); + printf ("mask: %s, ", describe_image (mask, buf)); + printf ("dst: %s\n\n", describe_image (dst, buf)); + } + else + { + printf ("src color: %.2f %.2f %.2f %.2f\n" + "dst color: %.2f %.2f %.2f %.2f\n", + src->color->r, src->color->g, + src->color->b, src->color->a, + dst->color->r, dst->color->g, + dst->color->b, dst->color->a); + printf ("src: %s, ", describe_image (src, buf)); + printf ("dst: %s\n\n", describe_image (dst, buf)); + } + + success = FALSE; + } + + return success; +} + +static void +image_init (image_t *info, + int color, + int format, + int size) +{ + pixman_color_t fill; + + info->color = &colors[color]; + compute_pixman_color (info->color, &fill); + + info->format = &formats[format]; + info->size = sizes[size] & ~FLAGS; + info->repeat = PIXMAN_REPEAT_NONE; + + if (info->size) + { + pixman_rectangle16_t rect; + + info->image = pixman_image_create_bits (info->format->format, + info->size, info->size, + NULL, 0); + + rect.x = rect.y = 0; + rect.width = rect.height = info->size; + pixman_image_fill_rectangles (PIXMAN_OP_SRC, info->image, &fill, + 1, &rect); + + if (size & REPEAT) + { + pixman_image_set_repeat (info->image, PIXMAN_REPEAT_NORMAL); + info->repeat = PIXMAN_REPEAT_NORMAL; + } + } + else + { + info->image = pixman_image_create_solid_fill (&fill); + } +} + +static void +image_fini (image_t *info) +{ + pixman_image_unref (info->image); +} + +static int +random_size (void) +{ + return lcg_rand_n (ARRAY_LENGTH (sizes)); +} + +static int +random_color (void) +{ + return lcg_rand_n (ARRAY_LENGTH (colors)); +} + +static int +random_format (void) +{ + return lcg_rand_n (ARRAY_LENGTH (formats)); +} + +static pixman_bool_t +run_test (uint32_t seed) +{ + image_t src, mask, dst; + const operator_t *op; + int ca; + int ok; + + lcg_srand (seed); + + image_init (&dst, random_color(), random_format(), 1); + image_init (&src, random_color(), random_format(), random_size()); + image_init (&mask, random_color(), random_format(), random_size()); + + op = &(operators [lcg_rand_n (ARRAY_LENGTH (operators))]); + + ca = lcg_rand_n (3); + + switch (ca) + { + case 0: + ok = composite_test (&dst, op, &src, NULL, FALSE); + break; + case 1: + ok = composite_test (&dst, op, &src, &mask, FALSE); + break; + case 2: + ok = composite_test (&dst, op, &src, &mask, + mask.size? TRUE : FALSE); + break; + default: + ok = FALSE; + break; + } + + image_fini (&src); + image_fini (&mask); + image_fini (&dst); + + return ok; +} + +int +main (int argc, char **argv) +{ +#define N_TESTS (8 * 1024 * 1024) + int result = 0; + uint32_t i, seed; + + if (argc > 1) + { + char *end; + + i = strtol (argv[1], &end, 0); + + if (end != argv[1]) + { + if (!run_test (i)) + return 1; + else + return 0; + } + else + { + printf ("Usage:\n\n %s \n\n", argv[0]); + return -1; + } + } + + if (getenv ("PIXMAN_RANDOMIZE_TESTS")) + seed = get_random_seed(); + else + seed = 1; + +#ifdef USE_OPENMP +# pragma omp parallel for default(none) shared(result, argv, seed) +#endif + for (i = 0; i <= N_TESTS; ++i) + { + if (!result && !run_test (i + seed)) + { + printf ("Test 0x%08X failed.\n", seed + i); + + result = seed + i; + } + } + + return result; +} diff --git a/pixman/test/fetch-test.c b/pixman/test/fetch-test.c index cbb1ee63e..feb98d9b7 100644 --- a/pixman/test/fetch-test.c +++ b/pixman/test/fetch-test.c @@ -1,206 +1,206 @@ -#include -#include -#include -#include "pixman.h" -#include - -#define SIZE 1024 - -static pixman_indexed_t mono_palette = -{ - 0, { 0x00000000, 0x00ffffff }, -}; - - -typedef struct { - pixman_format_code_t format; - int width, height; - int stride; - uint32_t src[SIZE]; - uint32_t dst[SIZE]; - pixman_indexed_t *indexed; -} testcase_t; - -static testcase_t testcases[] = -{ - { - PIXMAN_a8r8g8b8, - 2, 2, - 8, - { 0x00112233, 0x44556677, - 0x8899aabb, 0xccddeeff }, - { 0x00112233, 0x44556677, - 0x8899aabb, 0xccddeeff }, - NULL, - }, - { - PIXMAN_r8g8b8a8, - 2, 2, - 8, - { 0x11223300, 0x55667744, - 0x99aabb88, 0xddeeffcc }, - { 0x00112233, 0x44556677, - 0x8899aabb, 0xccddeeff }, - NULL, - }, - { - PIXMAN_g1, - 8, 2, - 4, -#ifdef WORDS_BIGENDIAN - { - 0xaa000000, - 0x55000000 - }, -#else - { - 0x00000055, - 0x000000aa - }, -#endif - { - 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, - 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff - }, - &mono_palette, - }, -#if 0 - { - PIXMAN_g8, - 4, 2, - 4, - { 0x01234567, - 0x89abcdef }, - { 0x00010101, 0x00232323, 0x00454545, 0x00676767, - 0x00898989, 0x00ababab, 0x00cdcdcd, 0x00efefef, }, - }, -#endif - /* FIXME: make this work on big endian */ - { - PIXMAN_yv12, - 8, 2, - 8, -#ifdef WORDS_BIGENDIAN - { - 0x00ff00ff, 0x00ff00ff, - 0xff00ff00, 0xff00ff00, - 0x80ff8000, - 0x800080ff - }, -#else - { - 0xff00ff00, 0xff00ff00, - 0x00ff00ff, 0x00ff00ff, - 0x0080ff80, - 0xff800080 - }, -#endif - { - 0xff000000, 0xffffffff, 0xffb80000, 0xffffe113, - 0xff000000, 0xffffffff, 0xff0023ee, 0xff4affff, - 0xffffffff, 0xff000000, 0xffffe113, 0xffb80000, - 0xffffffff, 0xff000000, 0xff4affff, 0xff0023ee, - }, - }, -}; - -int n_test_cases = sizeof(testcases)/sizeof(testcases[0]); - - -static uint32_t -reader (const void *src, int size) -{ - switch (size) - { - case 1: - return *(uint8_t *)src; - case 2: - return *(uint16_t *)src; - case 4: - return *(uint32_t *)src; - default: - assert(0); - return 0; /* silence MSVC */ - } -} - - -static void -writer (void *src, uint32_t value, int size) -{ - switch (size) - { - case 1: - *(uint8_t *)src = value; - break; - case 2: - *(uint16_t *)src = value; - break; - case 4: - *(uint32_t *)src = value; - break; - default: - assert(0); - } -} - - -int -main (int argc, char **argv) -{ - uint32_t dst[SIZE]; - pixman_image_t *src_img; - pixman_image_t *dst_img; - int i, j, x, y; - int ret = 0; - - for (i = 0; i < n_test_cases; ++i) - { - for (j = 0; j < 2; ++j) - { - src_img = pixman_image_create_bits (testcases[i].format, - testcases[i].width, - testcases[i].height, - testcases[i].src, - testcases[i].stride); - pixman_image_set_indexed(src_img, testcases[i].indexed); - - dst_img = pixman_image_create_bits (PIXMAN_a8r8g8b8, - testcases[i].width, - testcases[i].height, - dst, - testcases[i].width*4); - - if (j) - { - pixman_image_set_accessors (src_img, reader, writer); - pixman_image_set_accessors (dst_img, reader, writer); - } - - pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img, - 0, 0, 0, 0, 0, 0, testcases[i].width, testcases[i].height); - - pixman_image_unref (src_img); - pixman_image_unref (dst_img); - - for (y = 0; y < testcases[i].height; ++y) - { - for (x = 0; x < testcases[i].width; ++x) - { - int offset = y * testcases[i].width + x; - - if (dst[offset] != testcases[i].dst[offset]) - { - printf ("test %i%c: pixel mismatch at (x=%d,y=%d): %08x expected, %08x obtained\n", - i + 1, 'a' + j, - x, y, - testcases[i].dst[offset], dst[offset]); - ret = 1; - } - } - } - } - } - - return ret; -} +#include +#include +#include +#include "pixman.h" +#include + +#define SIZE 1024 + +static pixman_indexed_t mono_palette = +{ + 0, { 0x00000000, 0x00ffffff }, +}; + + +typedef struct { + pixman_format_code_t format; + int width, height; + int stride; + uint32_t src[SIZE]; + uint32_t dst[SIZE]; + pixman_indexed_t *indexed; +} testcase_t; + +static testcase_t testcases[] = +{ + { + PIXMAN_a8r8g8b8, + 2, 2, + 8, + { 0x00112233, 0x44556677, + 0x8899aabb, 0xccddeeff }, + { 0x00112233, 0x44556677, + 0x8899aabb, 0xccddeeff }, + NULL, + }, + { + PIXMAN_r8g8b8a8, + 2, 2, + 8, + { 0x11223300, 0x55667744, + 0x99aabb88, 0xddeeffcc }, + { 0x00112233, 0x44556677, + 0x8899aabb, 0xccddeeff }, + NULL, + }, + { + PIXMAN_g1, + 8, 2, + 4, +#ifdef WORDS_BIGENDIAN + { + 0xaa000000, + 0x55000000 + }, +#else + { + 0x00000055, + 0x000000aa + }, +#endif + { + 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, + 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff + }, + &mono_palette, + }, +#if 0 + { + PIXMAN_g8, + 4, 2, + 4, + { 0x01234567, + 0x89abcdef }, + { 0x00010101, 0x00232323, 0x00454545, 0x00676767, + 0x00898989, 0x00ababab, 0x00cdcdcd, 0x00efefef, }, + }, +#endif + /* FIXME: make this work on big endian */ + { + PIXMAN_yv12, + 8, 2, + 8, +#ifdef WORDS_BIGENDIAN + { + 0x00ff00ff, 0x00ff00ff, + 0xff00ff00, 0xff00ff00, + 0x80ff8000, + 0x800080ff + }, +#else + { + 0xff00ff00, 0xff00ff00, + 0x00ff00ff, 0x00ff00ff, + 0x0080ff80, + 0xff800080 + }, +#endif + { + 0xff000000, 0xffffffff, 0xffb80000, 0xffffe113, + 0xff000000, 0xffffffff, 0xff0023ee, 0xff4affff, + 0xffffffff, 0xff000000, 0xffffe113, 0xffb80000, + 0xffffffff, 0xff000000, 0xff4affff, 0xff0023ee, + }, + }, +}; + +int n_test_cases = sizeof(testcases)/sizeof(testcases[0]); + + +static uint32_t +reader (const void *src, int size) +{ + switch (size) + { + case 1: + return *(uint8_t *)src; + case 2: + return *(uint16_t *)src; + case 4: + return *(uint32_t *)src; + default: + assert(0); + return 0; /* silence MSVC */ + } +} + + +static void +writer (void *src, uint32_t value, int size) +{ + switch (size) + { + case 1: + *(uint8_t *)src = value; + break; + case 2: + *(uint16_t *)src = value; + break; + case 4: + *(uint32_t *)src = value; + break; + default: + assert(0); + } +} + + +int +main (int argc, char **argv) +{ + uint32_t dst[SIZE]; + pixman_image_t *src_img; + pixman_image_t *dst_img; + int i, j, x, y; + int ret = 0; + + for (i = 0; i < n_test_cases; ++i) + { + for (j = 0; j < 2; ++j) + { + src_img = pixman_image_create_bits (testcases[i].format, + testcases[i].width, + testcases[i].height, + testcases[i].src, + testcases[i].stride); + pixman_image_set_indexed(src_img, testcases[i].indexed); + + dst_img = pixman_image_create_bits (PIXMAN_a8r8g8b8, + testcases[i].width, + testcases[i].height, + dst, + testcases[i].width*4); + + if (j) + { + pixman_image_set_accessors (src_img, reader, writer); + pixman_image_set_accessors (dst_img, reader, writer); + } + + pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img, + 0, 0, 0, 0, 0, 0, testcases[i].width, testcases[i].height); + + pixman_image_unref (src_img); + pixman_image_unref (dst_img); + + for (y = 0; y < testcases[i].height; ++y) + { + for (x = 0; x < testcases[i].width; ++x) + { + int offset = y * testcases[i].width + x; + + if (dst[offset] != testcases[i].dst[offset]) + { + printf ("test %i%c: pixel mismatch at (x=%d,y=%d): %08x expected, %08x obtained\n", + i + 1, 'a' + j, + x, y, + testcases[i].dst[offset], dst[offset]); + ret = 1; + } + } + } + } + } + + return ret; +} diff --git a/pixman/test/stress-test.c b/pixman/test/stress-test.c index 92b5f0bca..571420ab0 100644 --- a/pixman/test/stress-test.c +++ b/pixman/test/stress-test.c @@ -1,872 +1,872 @@ -#include -#include "utils.h" -#include - -#if 0 -#define fence_malloc malloc -#define fence_free free -#define make_random_bytes malloc -#endif - -static const pixman_format_code_t image_formats[] = -{ - PIXMAN_a8r8g8b8, - PIXMAN_x8r8g8b8, - PIXMAN_r5g6b5, - PIXMAN_r3g3b2, - PIXMAN_a8, - PIXMAN_a8b8g8r8, - PIXMAN_x8b8g8r8, - PIXMAN_b8g8r8a8, - PIXMAN_b8g8r8x8, - PIXMAN_r8g8b8a8, - PIXMAN_r8g8b8x8, - PIXMAN_x14r6g6b6, - PIXMAN_r8g8b8, - PIXMAN_b8g8r8, - PIXMAN_r5g6b5, - PIXMAN_b5g6r5, - PIXMAN_x2r10g10b10, - PIXMAN_a2r10g10b10, - PIXMAN_x2b10g10r10, - PIXMAN_a2b10g10r10, - PIXMAN_a1r5g5b5, - PIXMAN_x1r5g5b5, - PIXMAN_a1b5g5r5, - PIXMAN_x1b5g5r5, - PIXMAN_a4r4g4b4, - PIXMAN_x4r4g4b4, - PIXMAN_a4b4g4r4, - PIXMAN_x4b4g4r4, - PIXMAN_a8, - PIXMAN_r3g3b2, - PIXMAN_b2g3r3, - PIXMAN_a2r2g2b2, - PIXMAN_a2b2g2r2, - PIXMAN_c8, - PIXMAN_g8, - PIXMAN_x4c4, - PIXMAN_x4g4, - PIXMAN_c4, - PIXMAN_g4, - PIXMAN_g1, - PIXMAN_x4a4, - PIXMAN_a4, - PIXMAN_r1g2b1, - PIXMAN_b1g2r1, - PIXMAN_a1r1g1b1, - PIXMAN_a1b1g1r1, - PIXMAN_a1 -}; - -static pixman_filter_t filters[] = -{ - PIXMAN_FILTER_NEAREST, - PIXMAN_FILTER_BILINEAR, - PIXMAN_FILTER_FAST, - PIXMAN_FILTER_GOOD, - PIXMAN_FILTER_BEST, - PIXMAN_FILTER_CONVOLUTION -}; - -static int -get_size (void) -{ - switch (lcg_rand_n (28)) - { - case 0: - return 1; - - case 1: - return 2; - - default: - case 2: - return lcg_rand_n (200); - - case 4: - return lcg_rand_n (2000) + 1000; - - case 5: - return 65535; - - case 6: - return 65536; - - case 7: - return lcg_rand_N (64000) + 63000; - } -} - -static void -destroy (pixman_image_t *image, void *data) -{ - if (image->type == BITS && image->bits.free_me != image->bits.bits) - { - uint32_t *bits; - - if (image->bits.bits != (void *)0x01) - { - bits = image->bits.bits; - - if (image->bits.rowstride < 0) - bits -= (- image->bits.rowstride * (image->bits.height - 1)); - - fence_free (bits); - } - } - - free (data); -} - -static uint32_t -real_reader (const void *src, int size) -{ - switch (size) - { - case 1: - return *(uint8_t *)src; - case 2: - return *(uint16_t *)src; - case 4: - return *(uint32_t *)src; - default: - assert (0); - return 0; /* silence MSVC */ - } -} - -static void -real_writer (void *src, uint32_t value, int size) -{ - switch (size) - { - case 1: - *(uint8_t *)src = value; - break; - - case 2: - *(uint16_t *)src = value; - break; - - case 4: - *(uint32_t *)src = value; - break; - - default: - assert (0); - break; - } -} - -static uint32_t -fake_reader (const void *src, int size) -{ - uint32_t r = lcg_rand_u32 (); - - assert (size == 1 || size == 2 || size == 4); - return r & ((1 << (size * 8)) - 1); -} - -static void -fake_writer (void *src, uint32_t value, int size) -{ - assert (size == 1 || size == 2 || size == 4); -} - -static int32_t -log_rand (void) -{ - uint32_t mask; - - mask = (1 << lcg_rand_n (31)) - 1; - - return (lcg_rand () & mask) - (mask >> 1); -} - -static pixman_image_t * -create_random_bits_image (void) -{ - pixman_format_code_t format; - pixman_indexed_t *indexed; - pixman_image_t *image; - int width, height, stride; - uint32_t *bits; - pixman_read_memory_func_t read_func = NULL; - pixman_write_memory_func_t write_func = NULL; - pixman_filter_t filter; - pixman_fixed_t *coefficients = NULL; - int n_coefficients = 0; - - /* format */ - format = image_formats[lcg_rand_n (ARRAY_LENGTH (image_formats))]; - - indexed = NULL; - if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR) - { - indexed = malloc (sizeof (pixman_indexed_t)); - - initialize_palette (indexed, PIXMAN_FORMAT_BPP (format), TRUE); - } - else if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY) - { - indexed = malloc (sizeof (pixman_indexed_t)); - - initialize_palette (indexed, PIXMAN_FORMAT_BPP (format), FALSE); - } - else - { - indexed = NULL; - } - - /* size */ - width = get_size (); - height = get_size (); - - if ((uint64_t)width * height > 200000) - { - if (lcg_rand_n(2) == 0) - height = 200000 / width; - else - width = 200000 / height; - } - - if (height == 0) - height = 1; - if (width == 0) - width = 1; - - /* bits */ - switch (lcg_rand_n (7)) - { - default: - case 0: - stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); - stride = (stride + 3) & (~3); - bits = (uint32_t *)make_random_bytes (height * stride); - break; - - case 1: - stride = 0; - bits = NULL; - break; - - case 2: /* Zero-filled */ - stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); - stride = (stride + 3) & (~3); - bits = fence_malloc (height * stride); - if (!bits) - return NULL; - memset (bits, 0, height * stride); - break; - - case 3: /* Filled with 0xFF */ - stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); - stride = (stride + 3) & (~3); - bits = fence_malloc (height * stride); - if (!bits) - return NULL; - memset (bits, 0xff, height * stride); - break; - - case 4: /* bits is a bad pointer, has read/write functions */ - stride = 232; - bits = (void *)0x01; - read_func = fake_reader; - write_func = fake_writer; - break; - - case 5: /* bits is a real pointer, has read/write functions */ - stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); - stride = (stride + 3) & (~3); - bits = fence_malloc (height * stride); - if (!bits) - return NULL; - memset (bits, 0xff, height * stride); - read_func = real_reader; - write_func = real_writer; - break; - - case 6: /* bits is a real pointer, stride is negative */ - stride = (width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17)); - stride = (stride + 3) & (~3); - bits = (uint32_t *)make_random_bytes (height * stride); - if (!bits) - return NULL; - bits += ((height - 1) * stride) / 4; - stride = - stride; - break; - } - - /* Filter */ - filter = filters[lcg_rand_n (ARRAY_LENGTH (filters))]; - if (filter == PIXMAN_FILTER_CONVOLUTION) - { - int width = lcg_rand_n (17); - int height = lcg_rand_n (19); - - n_coefficients = width * height + 2; - coefficients = malloc (n_coefficients * sizeof (pixman_fixed_t)); - - if (coefficients) - { - int i; - - for (i = 0; i < width * height; ++i) - coefficients[i + 2] = lcg_rand_u32(); - - coefficients[0] = width << 16; - coefficients[1] = height << 16; - } - else - { - filter = PIXMAN_FILTER_BEST; - } - } - - /* Finally create the image */ - image = pixman_image_create_bits (format, width, height, bits, stride); - if (!image) - return NULL; - - pixman_image_set_indexed (image, indexed); - pixman_image_set_destroy_function (image, destroy, indexed); - pixman_image_set_accessors (image, read_func, write_func); - pixman_image_set_filter (image, filter, coefficients, n_coefficients); - - return image; -} - -static pixman_repeat_t repeats[] = -{ - PIXMAN_REPEAT_NONE, - PIXMAN_REPEAT_NORMAL, - PIXMAN_REPEAT_REFLECT, - PIXMAN_REPEAT_PAD -}; - -static uint32_t -absolute (int32_t i) -{ - return i < 0? -i : i; -} - -static void -set_general_properties (pixman_image_t *image, pixman_bool_t allow_alpha_map) -{ - pixman_repeat_t repeat; - - /* Set properties that are generic to all images */ - - /* Repeat */ - repeat = repeats[lcg_rand_n (ARRAY_LENGTH (repeats))]; - pixman_image_set_repeat (image, repeat); - - /* Alpha map */ - if (allow_alpha_map && lcg_rand_n (3) == 0) - { - pixman_image_t *alpha_map; - int16_t x, y; - - alpha_map = create_random_bits_image (); - - if (alpha_map) - { - set_general_properties (alpha_map, FALSE); - - x = lcg_rand_N (100000) - 65536; - y = lcg_rand_N (100000) - 65536; - - pixman_image_set_alpha_map (image, alpha_map, x, y); - - pixman_image_unref (alpha_map); - } - } - - /* Component alpha */ - pixman_image_set_component_alpha (image, lcg_rand_n (3) == 0); - - /* Clip region */ - if (lcg_rand_n (8) != 0) - { - pixman_region32_t region; - int i, n_rects; - - pixman_region32_init (®ion); - - switch (lcg_rand_n (10)) - { - case 0: - n_rects = 0; - break; - - case 1: case 2: case 3: - n_rects = 1; - break; - - case 4: case 5: - n_rects = 2; - break; - - case 6: case 7: - n_rects = 3; - - default: - n_rects = lcg_rand_n (100); - break; - } - - for (i = 0; i < n_rects; ++i) - { - uint32_t width, height; - int x, y; - - x = log_rand(); - y = log_rand(); - width = absolute (log_rand ()) + 1; - height = absolute (log_rand ()) + 1; - - pixman_region32_union_rect ( - ®ion, ®ion, x, y, width, height); - } - - pixman_image_set_clip_region32 (image, ®ion); - - pixman_region32_fini (®ion); - } - - /* Whether source clipping is enabled */ - pixman_image_set_source_clipping (image, !!lcg_rand_n (2)); - - /* Client clip */ - pixman_image_set_has_client_clip (image, !!lcg_rand_n (2)); - - /* Transform */ - if (lcg_rand_n (5) < 2) - { - pixman_transform_t xform; - int i, j, k; - uint32_t tx, ty, sx, sy; - uint32_t c, s; - - memset (&xform, 0, sizeof xform); - xform.matrix[0][0] = pixman_fixed_1; - xform.matrix[1][1] = pixman_fixed_1; - xform.matrix[2][2] = pixman_fixed_1; - - for (k = 0; k < 3; ++k) - { - switch (lcg_rand_n (4)) - { - case 0: - /* rotation */ - c = lcg_rand_N (2 * 65536) - 65536; - s = lcg_rand_N (2 * 65536) - 65536; - pixman_transform_rotate (&xform, NULL, c, s); - break; - - case 1: - /* translation */ - tx = lcg_rand_u32(); - ty = lcg_rand_u32(); - pixman_transform_translate (&xform, NULL, tx, ty); - break; - - case 2: - /* scale */ - sx = lcg_rand_u32(); - sy = lcg_rand_u32(); - pixman_transform_scale (&xform, NULL, sx, sy); - break; - - case 3: - if (lcg_rand_n (16) == 0) - { - /* random */ - for (i = 0; i < 3; ++i) - for (j = 0; j < 3; ++j) - xform.matrix[i][j] = lcg_rand_u32(); - break; - } - else if (lcg_rand_n (16) == 0) - { - /* zero */ - memset (&xform, 0, sizeof xform); - } - break; - } - } - - pixman_image_set_transform (image, &xform); - } -} - -static pixman_color_t -random_color (void) -{ - pixman_color_t color = - { - lcg_rand() & 0xffff, - lcg_rand() & 0xffff, - lcg_rand() & 0xffff, - lcg_rand() & 0xffff, - }; - - return color; -} - - -static pixman_image_t * -create_random_solid_image (void) -{ - pixman_color_t color = random_color(); - pixman_image_t *image = pixman_image_create_solid_fill (&color); - - return image; -} - -static pixman_gradient_stop_t * -create_random_stops (int *n_stops) -{ - pixman_fixed_t step; - pixman_fixed_t s; - int i; - pixman_gradient_stop_t *stops; - - *n_stops = lcg_rand_n (50) + 1; - - step = pixman_fixed_1 / *n_stops; - - stops = malloc (*n_stops * sizeof (pixman_gradient_stop_t)); - - s = 0; - for (i = 0; i < (*n_stops) - 1; ++i) - { - stops[i].x = s; - stops[i].color = random_color(); - - s += step; - } - - stops[*n_stops - 1].x = pixman_fixed_1; - stops[*n_stops - 1].color = random_color(); - - return stops; -} - -static pixman_point_fixed_t -create_random_point (void) -{ - pixman_point_fixed_t p; - - p.x = log_rand (); - p.y = log_rand (); - - return p; -} - -static pixman_image_t * -create_random_linear_image (void) -{ - int n_stops; - pixman_gradient_stop_t *stops; - pixman_point_fixed_t p1, p2; - pixman_image_t *result; - - stops = create_random_stops (&n_stops); - if (!stops) - return NULL; - - p1 = create_random_point (); - p2 = create_random_point (); - - result = pixman_image_create_linear_gradient (&p1, &p2, stops, n_stops); - - free (stops); - - return result; -} - -static pixman_image_t * -create_random_radial_image (void) -{ - int n_stops; - pixman_gradient_stop_t *stops; - pixman_point_fixed_t inner_c, outer_c; - pixman_fixed_t inner_r, outer_r; - pixman_image_t *result; - - inner_c = create_random_point(); - outer_c = create_random_point(); - inner_r = lcg_rand(); - outer_r = lcg_rand(); - - stops = create_random_stops (&n_stops); - - if (!stops) - return NULL; - - result = pixman_image_create_radial_gradient ( - &inner_c, &outer_c, inner_r, outer_r, stops, n_stops); - - free (stops); - - return result; -} - -static pixman_image_t * -create_random_conical_image (void) -{ - pixman_gradient_stop_t *stops; - int n_stops; - pixman_point_fixed_t c; - pixman_fixed_t angle; - pixman_image_t *result; - - c = create_random_point(); - angle = lcg_rand(); - - stops = create_random_stops (&n_stops); - - if (!stops) - return NULL; - - result = pixman_image_create_conical_gradient (&c, angle, stops, n_stops); - - free (stops); - - return result; -} - -static pixman_image_t * -create_random_image (void) -{ - pixman_image_t *result; - - switch (lcg_rand_n (5)) - { - default: - case 0: - result = create_random_bits_image (); - break; - - case 1: - result = create_random_solid_image (); - break; - - case 2: - result = create_random_linear_image (); - break; - - case 3: - result = create_random_radial_image (); - break; - - case 4: - result = create_random_conical_image (); - break; - } - - if (result) - set_general_properties (result, TRUE); - - return result; -} - -static const pixman_op_t op_list[] = -{ - PIXMAN_OP_SRC, - PIXMAN_OP_OVER, - PIXMAN_OP_ADD, - PIXMAN_OP_CLEAR, - PIXMAN_OP_SRC, - PIXMAN_OP_DST, - PIXMAN_OP_OVER, - PIXMAN_OP_OVER_REVERSE, - PIXMAN_OP_IN, - PIXMAN_OP_IN_REVERSE, - PIXMAN_OP_OUT, - PIXMAN_OP_OUT_REVERSE, - PIXMAN_OP_ATOP, - PIXMAN_OP_ATOP_REVERSE, - PIXMAN_OP_XOR, - PIXMAN_OP_ADD, - PIXMAN_OP_SATURATE, - PIXMAN_OP_DISJOINT_CLEAR, - PIXMAN_OP_DISJOINT_SRC, - PIXMAN_OP_DISJOINT_DST, - PIXMAN_OP_DISJOINT_OVER, - PIXMAN_OP_DISJOINT_OVER_REVERSE, - PIXMAN_OP_DISJOINT_IN, - PIXMAN_OP_DISJOINT_IN_REVERSE, - PIXMAN_OP_DISJOINT_OUT, - PIXMAN_OP_DISJOINT_OUT_REVERSE, - PIXMAN_OP_DISJOINT_ATOP, - PIXMAN_OP_DISJOINT_ATOP_REVERSE, - PIXMAN_OP_DISJOINT_XOR, - PIXMAN_OP_CONJOINT_CLEAR, - PIXMAN_OP_CONJOINT_SRC, - PIXMAN_OP_CONJOINT_DST, - PIXMAN_OP_CONJOINT_OVER, - PIXMAN_OP_CONJOINT_OVER_REVERSE, - PIXMAN_OP_CONJOINT_IN, - PIXMAN_OP_CONJOINT_IN_REVERSE, - PIXMAN_OP_CONJOINT_OUT, - PIXMAN_OP_CONJOINT_OUT_REVERSE, - PIXMAN_OP_CONJOINT_ATOP, - PIXMAN_OP_CONJOINT_ATOP_REVERSE, - PIXMAN_OP_CONJOINT_XOR, - PIXMAN_OP_MULTIPLY, - PIXMAN_OP_SCREEN, - PIXMAN_OP_OVERLAY, - PIXMAN_OP_DARKEN, - PIXMAN_OP_LIGHTEN, - PIXMAN_OP_COLOR_DODGE, - PIXMAN_OP_COLOR_BURN, - PIXMAN_OP_HARD_LIGHT, - PIXMAN_OP_DIFFERENCE, - PIXMAN_OP_EXCLUSION, - PIXMAN_OP_SOFT_LIGHT, - PIXMAN_OP_HSL_HUE, - PIXMAN_OP_HSL_SATURATION, - PIXMAN_OP_HSL_COLOR, - PIXMAN_OP_HSL_LUMINOSITY, -}; - -static void -run_test (uint32_t seed, pixman_bool_t verbose, uint32_t mod) -{ - pixman_image_t *source, *mask, *dest; - pixman_op_t op; - - if (verbose) - { - if (mod == 0 || (seed % mod) == 0) - printf ("Seed 0x%08x\n", seed); - } - - lcg_srand (seed); - - source = create_random_image (); - mask = create_random_image (); - dest = create_random_bits_image (); - - if (source && mask && dest) - { - set_general_properties (dest, TRUE); - - op = op_list [lcg_rand_n (ARRAY_LENGTH (op_list))]; - - pixman_image_composite32 (op, - source, mask, dest, - log_rand(), log_rand(), - log_rand(), log_rand(), - log_rand(), log_rand(), - absolute (log_rand()), - absolute (log_rand())); - } - if (source) - pixman_image_unref (source); - if (mask) - pixman_image_unref (mask); - if (dest) - pixman_image_unref (dest); -} - -static pixman_bool_t -get_int (char *s, uint32_t *i) -{ - char *end; - int p; - - p = strtol (s, &end, 0); - - if (end != s && *end == 0) - { - *i = p; - return TRUE; - } - - return FALSE; -} - -int -main (int argc, char **argv) -{ - int verbose = FALSE; - uint32_t seed = 1; - uint32_t n_tests = 0xffffffff; - uint32_t mod = 0; - pixman_bool_t use_threads = TRUE; - uint32_t i; - - pixman_disable_out_of_bounds_workaround (); - - enable_fp_exceptions(); - - if (getenv ("VERBOSE") != NULL) - verbose = TRUE; - - for (i = 1; i < argc; ++i) - { - if (strcmp (argv[i], "-v") == 0) - { - verbose = TRUE; - - if (i + 1 < argc) - { - get_int (argv[i + 1], &mod); - i++; - } - } - else if (strcmp (argv[i], "-s") == 0 && i + 1 < argc) - { - get_int (argv[i + 1], &seed); - use_threads = FALSE; - i++; - } - else if (strcmp (argv[i], "-n") == 0 && i + 1 < argc) - { - get_int (argv[i + 1], &n_tests); - i++; - } - else - { - if (strcmp (argv[i], "-h") != 0) - printf ("Unknown option '%s'\n\n", argv[i]); - - printf ("Options:\n\n" - "-n Number of tests to run\n" - "-s Seed of first test (ignored if PIXMAN_RANDOMIZE_TESTS is set)\n" - "-v Print out seeds\n" - "-v Print out every n'th seed\n\n"); - - exit (-1); - } - } - - if (n_tests == 0xffffffff) - n_tests = 8000; - - if (getenv ("PIXMAN_RANDOMIZE_TESTS")) - { - seed = get_random_seed(); - printf ("First seed: 0x%08x\n", seed); - } - - if (use_threads) - { -#ifdef USE_OPENMP -# pragma omp parallel for default(none) shared(verbose, n_tests, mod, seed) -#endif - for (i = seed; i < seed + n_tests; ++i) - run_test (i, verbose, mod); - } - else - { - for (i = seed; i < seed + n_tests; ++i) - run_test (i, verbose, mod); - } - - return 0; -} +#include +#include "utils.h" +#include + +#if 0 +#define fence_malloc malloc +#define fence_free free +#define make_random_bytes malloc +#endif + +static const pixman_format_code_t image_formats[] = +{ + PIXMAN_a8r8g8b8, + PIXMAN_x8r8g8b8, + PIXMAN_r5g6b5, + PIXMAN_r3g3b2, + PIXMAN_a8, + PIXMAN_a8b8g8r8, + PIXMAN_x8b8g8r8, + PIXMAN_b8g8r8a8, + PIXMAN_b8g8r8x8, + PIXMAN_r8g8b8a8, + PIXMAN_r8g8b8x8, + PIXMAN_x14r6g6b6, + PIXMAN_r8g8b8, + PIXMAN_b8g8r8, + PIXMAN_r5g6b5, + PIXMAN_b5g6r5, + PIXMAN_x2r10g10b10, + PIXMAN_a2r10g10b10, + PIXMAN_x2b10g10r10, + PIXMAN_a2b10g10r10, + PIXMAN_a1r5g5b5, + PIXMAN_x1r5g5b5, + PIXMAN_a1b5g5r5, + PIXMAN_x1b5g5r5, + PIXMAN_a4r4g4b4, + PIXMAN_x4r4g4b4, + PIXMAN_a4b4g4r4, + PIXMAN_x4b4g4r4, + PIXMAN_a8, + PIXMAN_r3g3b2, + PIXMAN_b2g3r3, + PIXMAN_a2r2g2b2, + PIXMAN_a2b2g2r2, + PIXMAN_c8, + PIXMAN_g8, + PIXMAN_x4c4, + PIXMAN_x4g4, + PIXMAN_c4, + PIXMAN_g4, + PIXMAN_g1, + PIXMAN_x4a4, + PIXMAN_a4, + PIXMAN_r1g2b1, + PIXMAN_b1g2r1, + PIXMAN_a1r1g1b1, + PIXMAN_a1b1g1r1, + PIXMAN_a1 +}; + +static pixman_filter_t filters[] = +{ + PIXMAN_FILTER_NEAREST, + PIXMAN_FILTER_BILINEAR, + PIXMAN_FILTER_FAST, + PIXMAN_FILTER_GOOD, + PIXMAN_FILTER_BEST, + PIXMAN_FILTER_CONVOLUTION +}; + +static int +get_size (void) +{ + switch (lcg_rand_n (28)) + { + case 0: + return 1; + + case 1: + return 2; + + default: + case 2: + return lcg_rand_n (200); + + case 4: + return lcg_rand_n (2000) + 1000; + + case 5: + return 65535; + + case 6: + return 65536; + + case 7: + return lcg_rand_N (64000) + 63000; + } +} + +static void +destroy (pixman_image_t *image, void *data) +{ + if (image->type == BITS && image->bits.free_me != image->bits.bits) + { + uint32_t *bits; + + if (image->bits.bits != (void *)0x01) + { + bits = image->bits.bits; + + if (image->bits.rowstride < 0) + bits -= (- image->bits.rowstride * (image->bits.height - 1)); + + fence_free (bits); + } + } + + free (data); +} + +static uint32_t +real_reader (const void *src, int size) +{ + switch (size) + { + case 1: + return *(uint8_t *)src; + case 2: + return *(uint16_t *)src; + case 4: + return *(uint32_t *)src; + default: + assert (0); + return 0; /* silence MSVC */ + } +} + +static void +real_writer (void *src, uint32_t value, int size) +{ + switch (size) + { + case 1: + *(uint8_t *)src = value; + break; + + case 2: + *(uint16_t *)src = value; + break; + + case 4: + *(uint32_t *)src = value; + break; + + default: + assert (0); + break; + } +} + +static uint32_t +fake_reader (const void *src, int size) +{ + uint32_t r = lcg_rand_u32 (); + + assert (size == 1 || size == 2 || size == 4); + return r & ((1 << (size * 8)) - 1); +} + +static void +fake_writer (void *src, uint32_t value, int size) +{ + assert (size == 1 || size == 2 || size == 4); +} + +static int32_t +log_rand (void) +{ + uint32_t mask; + + mask = (1 << lcg_rand_n (31)) - 1; + + return (lcg_rand () & mask) - (mask >> 1); +} + +static pixman_image_t * +create_random_bits_image (void) +{ + pixman_format_code_t format; + pixman_indexed_t *indexed; + pixman_image_t *image; + int width, height, stride; + uint32_t *bits; + pixman_read_memory_func_t read_func = NULL; + pixman_write_memory_func_t write_func = NULL; + pixman_filter_t filter; + pixman_fixed_t *coefficients = NULL; + int n_coefficients = 0; + + /* format */ + format = image_formats[lcg_rand_n (ARRAY_LENGTH (image_formats))]; + + indexed = NULL; + if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR) + { + indexed = malloc (sizeof (pixman_indexed_t)); + + initialize_palette (indexed, PIXMAN_FORMAT_BPP (format), TRUE); + } + else if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY) + { + indexed = malloc (sizeof (pixman_indexed_t)); + + initialize_palette (indexed, PIXMAN_FORMAT_BPP (format), FALSE); + } + else + { + indexed = NULL; + } + + /* size */ + width = get_size (); + height = get_size (); + + if ((uint64_t)width * height > 200000) + { + if (lcg_rand_n(2) == 0) + height = 200000 / width; + else + width = 200000 / height; + } + + if (height == 0) + height = 1; + if (width == 0) + width = 1; + + /* bits */ + switch (lcg_rand_n (7)) + { + default: + case 0: + stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); + stride = (stride + 3) & (~3); + bits = (uint32_t *)make_random_bytes (height * stride); + break; + + case 1: + stride = 0; + bits = NULL; + break; + + case 2: /* Zero-filled */ + stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); + stride = (stride + 3) & (~3); + bits = fence_malloc (height * stride); + if (!bits) + return NULL; + memset (bits, 0, height * stride); + break; + + case 3: /* Filled with 0xFF */ + stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); + stride = (stride + 3) & (~3); + bits = fence_malloc (height * stride); + if (!bits) + return NULL; + memset (bits, 0xff, height * stride); + break; + + case 4: /* bits is a bad pointer, has read/write functions */ + stride = 232; + bits = (void *)0x01; + read_func = fake_reader; + write_func = fake_writer; + break; + + case 5: /* bits is a real pointer, has read/write functions */ + stride = width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17); + stride = (stride + 3) & (~3); + bits = fence_malloc (height * stride); + if (!bits) + return NULL; + memset (bits, 0xff, height * stride); + read_func = real_reader; + write_func = real_writer; + break; + + case 6: /* bits is a real pointer, stride is negative */ + stride = (width * PIXMAN_FORMAT_BPP (format) + lcg_rand_n (17)); + stride = (stride + 3) & (~3); + bits = (uint32_t *)make_random_bytes (height * stride); + if (!bits) + return NULL; + bits += ((height - 1) * stride) / 4; + stride = - stride; + break; + } + + /* Filter */ + filter = filters[lcg_rand_n (ARRAY_LENGTH (filters))]; + if (filter == PIXMAN_FILTER_CONVOLUTION) + { + int width = lcg_rand_n (17); + int height = lcg_rand_n (19); + + n_coefficients = width * height + 2; + coefficients = malloc (n_coefficients * sizeof (pixman_fixed_t)); + + if (coefficients) + { + int i; + + for (i = 0; i < width * height; ++i) + coefficients[i + 2] = lcg_rand_u32(); + + coefficients[0] = width << 16; + coefficients[1] = height << 16; + } + else + { + filter = PIXMAN_FILTER_BEST; + } + } + + /* Finally create the image */ + image = pixman_image_create_bits (format, width, height, bits, stride); + if (!image) + return NULL; + + pixman_image_set_indexed (image, indexed); + pixman_image_set_destroy_function (image, destroy, indexed); + pixman_image_set_accessors (image, read_func, write_func); + pixman_image_set_filter (image, filter, coefficients, n_coefficients); + + return image; +} + +static pixman_repeat_t repeats[] = +{ + PIXMAN_REPEAT_NONE, + PIXMAN_REPEAT_NORMAL, + PIXMAN_REPEAT_REFLECT, + PIXMAN_REPEAT_PAD +}; + +static uint32_t +absolute (int32_t i) +{ + return i < 0? -i : i; +} + +static void +set_general_properties (pixman_image_t *image, pixman_bool_t allow_alpha_map) +{ + pixman_repeat_t repeat; + + /* Set properties that are generic to all images */ + + /* Repeat */ + repeat = repeats[lcg_rand_n (ARRAY_LENGTH (repeats))]; + pixman_image_set_repeat (image, repeat); + + /* Alpha map */ + if (allow_alpha_map && lcg_rand_n (3) == 0) + { + pixman_image_t *alpha_map; + int16_t x, y; + + alpha_map = create_random_bits_image (); + + if (alpha_map) + { + set_general_properties (alpha_map, FALSE); + + x = lcg_rand_N (100000) - 65536; + y = lcg_rand_N (100000) - 65536; + + pixman_image_set_alpha_map (image, alpha_map, x, y); + + pixman_image_unref (alpha_map); + } + } + + /* Component alpha */ + pixman_image_set_component_alpha (image, lcg_rand_n (3) == 0); + + /* Clip region */ + if (lcg_rand_n (8) != 0) + { + pixman_region32_t region; + int i, n_rects; + + pixman_region32_init (®ion); + + switch (lcg_rand_n (10)) + { + case 0: + n_rects = 0; + break; + + case 1: case 2: case 3: + n_rects = 1; + break; + + case 4: case 5: + n_rects = 2; + break; + + case 6: case 7: + n_rects = 3; + + default: + n_rects = lcg_rand_n (100); + break; + } + + for (i = 0; i < n_rects; ++i) + { + uint32_t width, height; + int x, y; + + x = log_rand(); + y = log_rand(); + width = absolute (log_rand ()) + 1; + height = absolute (log_rand ()) + 1; + + pixman_region32_union_rect ( + ®ion, ®ion, x, y, width, height); + } + + pixman_image_set_clip_region32 (image, ®ion); + + pixman_region32_fini (®ion); + } + + /* Whether source clipping is enabled */ + pixman_image_set_source_clipping (image, !!lcg_rand_n (2)); + + /* Client clip */ + pixman_image_set_has_client_clip (image, !!lcg_rand_n (2)); + + /* Transform */ + if (lcg_rand_n (5) < 2) + { + pixman_transform_t xform; + int i, j, k; + uint32_t tx, ty, sx, sy; + uint32_t c, s; + + memset (&xform, 0, sizeof xform); + xform.matrix[0][0] = pixman_fixed_1; + xform.matrix[1][1] = pixman_fixed_1; + xform.matrix[2][2] = pixman_fixed_1; + + for (k = 0; k < 3; ++k) + { + switch (lcg_rand_n (4)) + { + case 0: + /* rotation */ + c = lcg_rand_N (2 * 65536) - 65536; + s = lcg_rand_N (2 * 65536) - 65536; + pixman_transform_rotate (&xform, NULL, c, s); + break; + + case 1: + /* translation */ + tx = lcg_rand_u32(); + ty = lcg_rand_u32(); + pixman_transform_translate (&xform, NULL, tx, ty); + break; + + case 2: + /* scale */ + sx = lcg_rand_u32(); + sy = lcg_rand_u32(); + pixman_transform_scale (&xform, NULL, sx, sy); + break; + + case 3: + if (lcg_rand_n (16) == 0) + { + /* random */ + for (i = 0; i < 3; ++i) + for (j = 0; j < 3; ++j) + xform.matrix[i][j] = lcg_rand_u32(); + break; + } + else if (lcg_rand_n (16) == 0) + { + /* zero */ + memset (&xform, 0, sizeof xform); + } + break; + } + } + + pixman_image_set_transform (image, &xform); + } +} + +static pixman_color_t +random_color (void) +{ + pixman_color_t color = + { + lcg_rand() & 0xffff, + lcg_rand() & 0xffff, + lcg_rand() & 0xffff, + lcg_rand() & 0xffff, + }; + + return color; +} + + +static pixman_image_t * +create_random_solid_image (void) +{ + pixman_color_t color = random_color(); + pixman_image_t *image = pixman_image_create_solid_fill (&color); + + return image; +} + +static pixman_gradient_stop_t * +create_random_stops (int *n_stops) +{ + pixman_fixed_t step; + pixman_fixed_t s; + int i; + pixman_gradient_stop_t *stops; + + *n_stops = lcg_rand_n (50) + 1; + + step = pixman_fixed_1 / *n_stops; + + stops = malloc (*n_stops * sizeof (pixman_gradient_stop_t)); + + s = 0; + for (i = 0; i < (*n_stops) - 1; ++i) + { + stops[i].x = s; + stops[i].color = random_color(); + + s += step; + } + + stops[*n_stops - 1].x = pixman_fixed_1; + stops[*n_stops - 1].color = random_color(); + + return stops; +} + +static pixman_point_fixed_t +create_random_point (void) +{ + pixman_point_fixed_t p; + + p.x = log_rand (); + p.y = log_rand (); + + return p; +} + +static pixman_image_t * +create_random_linear_image (void) +{ + int n_stops; + pixman_gradient_stop_t *stops; + pixman_point_fixed_t p1, p2; + pixman_image_t *result; + + stops = create_random_stops (&n_stops); + if (!stops) + return NULL; + + p1 = create_random_point (); + p2 = create_random_point (); + + result = pixman_image_create_linear_gradient (&p1, &p2, stops, n_stops); + + free (stops); + + return result; +} + +static pixman_image_t * +create_random_radial_image (void) +{ + int n_stops; + pixman_gradient_stop_t *stops; + pixman_point_fixed_t inner_c, outer_c; + pixman_fixed_t inner_r, outer_r; + pixman_image_t *result; + + inner_c = create_random_point(); + outer_c = create_random_point(); + inner_r = lcg_rand(); + outer_r = lcg_rand(); + + stops = create_random_stops (&n_stops); + + if (!stops) + return NULL; + + result = pixman_image_create_radial_gradient ( + &inner_c, &outer_c, inner_r, outer_r, stops, n_stops); + + free (stops); + + return result; +} + +static pixman_image_t * +create_random_conical_image (void) +{ + pixman_gradient_stop_t *stops; + int n_stops; + pixman_point_fixed_t c; + pixman_fixed_t angle; + pixman_image_t *result; + + c = create_random_point(); + angle = lcg_rand(); + + stops = create_random_stops (&n_stops); + + if (!stops) + return NULL; + + result = pixman_image_create_conical_gradient (&c, angle, stops, n_stops); + + free (stops); + + return result; +} + +static pixman_image_t * +create_random_image (void) +{ + pixman_image_t *result; + + switch (lcg_rand_n (5)) + { + default: + case 0: + result = create_random_bits_image (); + break; + + case 1: + result = create_random_solid_image (); + break; + + case 2: + result = create_random_linear_image (); + break; + + case 3: + result = create_random_radial_image (); + break; + + case 4: + result = create_random_conical_image (); + break; + } + + if (result) + set_general_properties (result, TRUE); + + return result; +} + +static const pixman_op_t op_list[] = +{ + PIXMAN_OP_SRC, + PIXMAN_OP_OVER, + PIXMAN_OP_ADD, + PIXMAN_OP_CLEAR, + PIXMAN_OP_SRC, + PIXMAN_OP_DST, + PIXMAN_OP_OVER, + PIXMAN_OP_OVER_REVERSE, + PIXMAN_OP_IN, + PIXMAN_OP_IN_REVERSE, + PIXMAN_OP_OUT, + PIXMAN_OP_OUT_REVERSE, + PIXMAN_OP_ATOP, + PIXMAN_OP_ATOP_REVERSE, + PIXMAN_OP_XOR, + PIXMAN_OP_ADD, + PIXMAN_OP_SATURATE, + PIXMAN_OP_DISJOINT_CLEAR, + PIXMAN_OP_DISJOINT_SRC, + PIXMAN_OP_DISJOINT_DST, + PIXMAN_OP_DISJOINT_OVER, + PIXMAN_OP_DISJOINT_OVER_REVERSE, + PIXMAN_OP_DISJOINT_IN, + PIXMAN_OP_DISJOINT_IN_REVERSE, + PIXMAN_OP_DISJOINT_OUT, + PIXMAN_OP_DISJOINT_OUT_REVERSE, + PIXMAN_OP_DISJOINT_ATOP, + PIXMAN_OP_DISJOINT_ATOP_REVERSE, + PIXMAN_OP_DISJOINT_XOR, + PIXMAN_OP_CONJOINT_CLEAR, + PIXMAN_OP_CONJOINT_SRC, + PIXMAN_OP_CONJOINT_DST, + PIXMAN_OP_CONJOINT_OVER, + PIXMAN_OP_CONJOINT_OVER_REVERSE, + PIXMAN_OP_CONJOINT_IN, + PIXMAN_OP_CONJOINT_IN_REVERSE, + PIXMAN_OP_CONJOINT_OUT, + PIXMAN_OP_CONJOINT_OUT_REVERSE, + PIXMAN_OP_CONJOINT_ATOP, + PIXMAN_OP_CONJOINT_ATOP_REVERSE, + PIXMAN_OP_CONJOINT_XOR, + PIXMAN_OP_MULTIPLY, + PIXMAN_OP_SCREEN, + PIXMAN_OP_OVERLAY, + PIXMAN_OP_DARKEN, + PIXMAN_OP_LIGHTEN, + PIXMAN_OP_COLOR_DODGE, + PIXMAN_OP_COLOR_BURN, + PIXMAN_OP_HARD_LIGHT, + PIXMAN_OP_DIFFERENCE, + PIXMAN_OP_EXCLUSION, + PIXMAN_OP_SOFT_LIGHT, + PIXMAN_OP_HSL_HUE, + PIXMAN_OP_HSL_SATURATION, + PIXMAN_OP_HSL_COLOR, + PIXMAN_OP_HSL_LUMINOSITY, +}; + +static void +run_test (uint32_t seed, pixman_bool_t verbose, uint32_t mod) +{ + pixman_image_t *source, *mask, *dest; + pixman_op_t op; + + if (verbose) + { + if (mod == 0 || (seed % mod) == 0) + printf ("Seed 0x%08x\n", seed); + } + + lcg_srand (seed); + + source = create_random_image (); + mask = create_random_image (); + dest = create_random_bits_image (); + + if (source && mask && dest) + { + set_general_properties (dest, TRUE); + + op = op_list [lcg_rand_n (ARRAY_LENGTH (op_list))]; + + pixman_image_composite32 (op, + source, mask, dest, + log_rand(), log_rand(), + log_rand(), log_rand(), + log_rand(), log_rand(), + absolute (log_rand()), + absolute (log_rand())); + } + if (source) + pixman_image_unref (source); + if (mask) + pixman_image_unref (mask); + if (dest) + pixman_image_unref (dest); +} + +static pixman_bool_t +get_int (char *s, uint32_t *i) +{ + char *end; + int p; + + p = strtol (s, &end, 0); + + if (end != s && *end == 0) + { + *i = p; + return TRUE; + } + + return FALSE; +} + +int +main (int argc, char **argv) +{ + int verbose = FALSE; + uint32_t seed = 1; + uint32_t n_tests = 0xffffffff; + uint32_t mod = 0; + pixman_bool_t use_threads = TRUE; + uint32_t i; + + pixman_disable_out_of_bounds_workaround (); + + enable_fp_exceptions(); + + if (getenv ("VERBOSE") != NULL) + verbose = TRUE; + + for (i = 1; i < argc; ++i) + { + if (strcmp (argv[i], "-v") == 0) + { + verbose = TRUE; + + if (i + 1 < argc) + { + get_int (argv[i + 1], &mod); + i++; + } + } + else if (strcmp (argv[i], "-s") == 0 && i + 1 < argc) + { + get_int (argv[i + 1], &seed); + use_threads = FALSE; + i++; + } + else if (strcmp (argv[i], "-n") == 0 && i + 1 < argc) + { + get_int (argv[i + 1], &n_tests); + i++; + } + else + { + if (strcmp (argv[i], "-h") != 0) + printf ("Unknown option '%s'\n\n", argv[i]); + + printf ("Options:\n\n" + "-n Number of tests to run\n" + "-s Seed of first test (ignored if PIXMAN_RANDOMIZE_TESTS is set)\n" + "-v Print out seeds\n" + "-v Print out every n'th seed\n\n"); + + exit (-1); + } + } + + if (n_tests == 0xffffffff) + n_tests = 8000; + + if (getenv ("PIXMAN_RANDOMIZE_TESTS")) + { + seed = get_random_seed(); + printf ("First seed: 0x%08x\n", seed); + } + + if (use_threads) + { +#ifdef USE_OPENMP +# pragma omp parallel for default(none) shared(verbose, n_tests, mod, seed) +#endif + for (i = seed; i < seed + n_tests; ++i) + run_test (i, verbose, mod); + } + else + { + for (i = seed; i < seed + n_tests; ++i) + run_test (i, verbose, mod); + } + + return 0; +} diff --git a/pixman/test/trap-crasher.c b/pixman/test/trap-crasher.c index 96f3b0bab..7485e62fd 100644 --- a/pixman/test/trap-crasher.c +++ b/pixman/test/trap-crasher.c @@ -1,27 +1,27 @@ -#include -#include - -int -main() -{ - pixman_image_t *dst; - pixman_trapezoid_t traps[1] = { - { - 2147483646, - 2147483647, - { - { 0, 0 }, - { 0, 2147483647 } - }, - { - { 65536, 0 }, - { 0, 2147483647 } - } - }, - }; - - dst = pixman_image_create_bits (PIXMAN_a8, 1, 1, NULL, -1); - - pixman_add_trapezoids (dst, 0, 0, sizeof (traps)/sizeof (traps[0]), traps); - return (0); -} +#include +#include + +int +main() +{ + pixman_image_t *dst; + pixman_trapezoid_t traps[1] = { + { + 2147483646, + 2147483647, + { + { 0, 0 }, + { 0, 2147483647 } + }, + { + { 65536, 0 }, + { 0, 2147483647 } + } + }, + }; + + dst = pixman_image_create_bits (PIXMAN_a8, 1, 1, NULL, -1); + + pixman_add_trapezoids (dst, 0, 0, sizeof (traps)/sizeof (traps[0]), traps); + return (0); +} diff --git a/pixman/test/utils.c b/pixman/test/utils.c index 46b48415f..adabd75dd 100644 --- a/pixman/test/utils.c +++ b/pixman/test/utils.c @@ -1,704 +1,704 @@ -#define _GNU_SOURCE - -#include "utils.h" -#include - -#ifdef HAVE_GETTIMEOFDAY -#include -#else -#include -#endif - -#ifdef HAVE_UNISTD_H -#include -#endif - -#ifdef HAVE_SYS_MMAN_H -#include -#endif - -#ifdef HAVE_FENV_H -#include -#endif - -#ifdef HAVE_LIBPNG -#include -#endif - -/* Random number seed - */ - -uint32_t lcg_seed; - -/*----------------------------------------------------------------------------*\ - * CRC-32 version 2.0.0 by Craig Bruce, 2006-04-29. - * - * This program generates the CRC-32 values for the files named in the - * command-line arguments. These are the same CRC-32 values used by GZIP, - * PKZIP, and ZMODEM. The Crc32_ComputeBuf () can also be detached and - * used independently. - * - * THIS PROGRAM IS PUBLIC-DOMAIN SOFTWARE. - * - * Based on the byte-oriented implementation "File Verification Using CRC" - * by Mark R. Nelson in Dr. Dobb's Journal, May 1992, pp. 64-67. - * - * v1.0.0: original release. - * v1.0.1: fixed printf formats. - * v1.0.2: fixed something else. - * v1.0.3: replaced CRC constant table by generator function. - * v1.0.4: reformatted code, made ANSI C. 1994-12-05. - * v2.0.0: rewrote to use memory buffer & static table, 2006-04-29. -\*----------------------------------------------------------------------------*/ - -/*----------------------------------------------------------------------------*\ - * NAME: - * Crc32_ComputeBuf () - computes the CRC-32 value of a memory buffer - * DESCRIPTION: - * Computes or accumulates the CRC-32 value for a memory buffer. - * The 'inCrc32' gives a previously accumulated CRC-32 value to allow - * a CRC to be generated for multiple sequential buffer-fuls of data. - * The 'inCrc32' for the first buffer must be zero. - * ARGUMENTS: - * inCrc32 - accumulated CRC-32 value, must be 0 on first call - * buf - buffer to compute CRC-32 value for - * bufLen - number of bytes in buffer - * RETURNS: - * crc32 - computed CRC-32 value - * ERRORS: - * (no errors are possible) -\*----------------------------------------------------------------------------*/ - -uint32_t -compute_crc32 (uint32_t in_crc32, - const void *buf, - size_t buf_len) -{ - static const uint32_t crc_table[256] = { - 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, - 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, - 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, - 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, - 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, - 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, - 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, - 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, - 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, - 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, - 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, - 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, - 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, - 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, - 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, - 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, - 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, - 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, - 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, - 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, - 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, - 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, - 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, - 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, - 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, - 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, - 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, - 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, - 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, - 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, - 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, - 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, - 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, - 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, - 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, - 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, - 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, - 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, - 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, - 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, - 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, - 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, - 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D - }; - - uint32_t crc32; - unsigned char * byte_buf; - size_t i; - - /* accumulate crc32 for buffer */ - crc32 = in_crc32 ^ 0xFFFFFFFF; - byte_buf = (unsigned char*) buf; - - for (i = 0; i < buf_len; i++) - crc32 = (crc32 >> 8) ^ crc_table[(crc32 ^ byte_buf[i]) & 0xFF]; - - return (crc32 ^ 0xFFFFFFFF); -} - -pixman_bool_t -is_little_endian (void) -{ - volatile uint16_t endian_check_var = 0x1234; - - return (*(volatile uint8_t *)&endian_check_var == 0x34); -} - -/* perform endian conversion of pixel data - */ -void -image_endian_swap (pixman_image_t *img) -{ - int stride = pixman_image_get_stride (img); - uint32_t *data = pixman_image_get_data (img); - int height = pixman_image_get_height (img); - int bpp = PIXMAN_FORMAT_BPP (pixman_image_get_format (img)); - int i, j; - - /* swap bytes only on big endian systems */ - if (is_little_endian()) - return; - - if (bpp == 8) - return; - - for (i = 0; i < height; i++) - { - uint8_t *line_data = (uint8_t *)data + stride * i; - - switch (bpp) - { - case 1: - for (j = 0; j < stride; j++) - { - line_data[j] = - ((line_data[j] & 0x80) >> 7) | - ((line_data[j] & 0x40) >> 5) | - ((line_data[j] & 0x20) >> 3) | - ((line_data[j] & 0x10) >> 1) | - ((line_data[j] & 0x08) << 1) | - ((line_data[j] & 0x04) << 3) | - ((line_data[j] & 0x02) << 5) | - ((line_data[j] & 0x01) << 7); - } - break; - case 4: - for (j = 0; j < stride; j++) - { - line_data[j] = (line_data[j] >> 4) | (line_data[j] << 4); - } - break; - case 16: - for (j = 0; j + 2 <= stride; j += 2) - { - char t1 = line_data[j + 0]; - char t2 = line_data[j + 1]; - - line_data[j + 1] = t1; - line_data[j + 0] = t2; - } - break; - case 24: - for (j = 0; j + 3 <= stride; j += 3) - { - char t1 = line_data[j + 0]; - char t2 = line_data[j + 1]; - char t3 = line_data[j + 2]; - - line_data[j + 2] = t1; - line_data[j + 1] = t2; - line_data[j + 0] = t3; - } - break; - case 32: - for (j = 0; j + 4 <= stride; j += 4) - { - char t1 = line_data[j + 0]; - char t2 = line_data[j + 1]; - char t3 = line_data[j + 2]; - char t4 = line_data[j + 3]; - - line_data[j + 3] = t1; - line_data[j + 2] = t2; - line_data[j + 1] = t3; - line_data[j + 0] = t4; - } - break; - default: - assert (FALSE); - break; - } - } -} - -#define N_LEADING_PROTECTED 10 -#define N_TRAILING_PROTECTED 10 - -typedef struct -{ - void *addr; - uint32_t len; - uint8_t *trailing; - int n_bytes; -} info_t; - -#if defined(HAVE_MPROTECT) && defined(HAVE_GETPAGESIZE) && defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP) - -/* This is apparently necessary on at least OS X */ -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif - -void * -fence_malloc (int64_t len) -{ - unsigned long page_size = getpagesize(); - unsigned long page_mask = page_size - 1; - uint32_t n_payload_bytes = (len + page_mask) & ~page_mask; - uint32_t n_bytes = - (page_size * (N_LEADING_PROTECTED + N_TRAILING_PROTECTED + 2) + - n_payload_bytes) & ~page_mask; - uint8_t *initial_page; - uint8_t *leading_protected; - uint8_t *trailing_protected; - uint8_t *payload; - uint8_t *addr; - - if (len < 0) - abort(); - - addr = mmap (NULL, n_bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, - -1, 0); - - if (addr == MAP_FAILED) - { - printf ("mmap failed on %lld %u\n", (long long int)len, n_bytes); - return NULL; - } - - initial_page = (uint8_t *)(((unsigned long)addr + page_mask) & ~page_mask); - leading_protected = initial_page + page_size; - payload = leading_protected + N_LEADING_PROTECTED * page_size; - trailing_protected = payload + n_payload_bytes; - - ((info_t *)initial_page)->addr = addr; - ((info_t *)initial_page)->len = len; - ((info_t *)initial_page)->trailing = trailing_protected; - ((info_t *)initial_page)->n_bytes = n_bytes; - - if ((mprotect (leading_protected, N_LEADING_PROTECTED * page_size, - PROT_NONE) == -1) || - (mprotect (trailing_protected, N_TRAILING_PROTECTED * page_size, - PROT_NONE) == -1)) - { - munmap (addr, n_bytes); - return NULL; - } - - return payload; -} - -void -fence_free (void *data) -{ - uint32_t page_size = getpagesize(); - uint8_t *payload = data; - uint8_t *leading_protected = payload - N_LEADING_PROTECTED * page_size; - uint8_t *initial_page = leading_protected - page_size; - info_t *info = (info_t *)initial_page; - - munmap (info->addr, info->n_bytes); -} - -#else - -void * -fence_malloc (int64_t len) -{ - return malloc (len); -} - -void -fence_free (void *data) -{ - free (data); -} - -#endif - -uint8_t * -make_random_bytes (int n_bytes) -{ - uint8_t *bytes = fence_malloc (n_bytes); - int i; - - if (!bytes) - return NULL; - - for (i = 0; i < n_bytes; ++i) - bytes[i] = lcg_rand () & 0xff; - - return bytes; -} - -#ifdef HAVE_LIBPNG - -static void -pngify_pixels (uint32_t *pixels, int n_pixels) -{ - int i; - - for (i = 0; i < n_pixels; ++i) - { - uint32_t p = pixels[i]; - uint8_t *out = (uint8_t *)&(pixels[i]); - uint8_t a, r, g, b; - - a = (p & 0xff000000) >> 24; - r = (p & 0x00ff0000) >> 16; - g = (p & 0x0000ff00) >> 8; - b = (p & 0x000000ff) >> 0; - - if (a != 0) - { - r = (r * 255) / a; - g = (g * 255) / a; - b = (b * 255) / a; - } - - *out++ = r; - *out++ = g; - *out++ = b; - *out++ = a; - } -} - -pixman_bool_t -write_png (pixman_image_t *image, const char *filename) -{ - int width = pixman_image_get_width (image); - int height = pixman_image_get_height (image); - int stride = width * 4; - uint32_t *data = malloc (height * stride); - pixman_image_t *copy; - png_struct *write_struct; - png_info *info_struct; - pixman_bool_t result = FALSE; - FILE *f = fopen (filename, "wb"); - png_bytep *row_pointers; - int i; - - if (!f) - return FALSE; - - row_pointers = malloc (height * sizeof (png_bytep)); - - copy = pixman_image_create_bits ( - PIXMAN_a8r8g8b8, width, height, data, stride); - - pixman_image_composite32 ( - PIXMAN_OP_SRC, image, NULL, copy, 0, 0, 0, 0, 0, 0, width, height); - - pngify_pixels (data, height * width); - - for (i = 0; i < height; ++i) - row_pointers[i] = (png_bytep)(data + i * width); - - if (!(write_struct = png_create_write_struct ( - PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) - goto out1; - - if (!(info_struct = png_create_info_struct (write_struct))) - goto out2; - - png_init_io (write_struct, f); - - png_set_IHDR (write_struct, info_struct, width, height, - 8, PNG_COLOR_TYPE_RGB_ALPHA, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, - PNG_FILTER_TYPE_BASE); - - png_write_info (write_struct, info_struct); - - png_write_image (write_struct, row_pointers); - - png_write_end (write_struct, NULL); - - result = TRUE; - -out2: - png_destroy_write_struct (&write_struct, &info_struct); - -out1: - if (fclose (f) != 0) - result = FALSE; - - pixman_image_unref (copy); - free (row_pointers); - free (data); - return result; -} - -#else /* no libpng */ - -pixman_bool_t -write_png (pixman_image_t *image, const char *filename) -{ - return FALSE; -} - -#endif - -/* - * A function, which can be used as a core part of the test programs, - * intended to detect various problems with the help of fuzzing input - * to pixman API (according to some templates, aka "smart" fuzzing). - * Some general information about such testing can be found here: - * http://en.wikipedia.org/wiki/Fuzz_testing - * - * It may help detecting: - * - crashes on bad handling of valid or reasonably invalid input to - * pixman API. - * - deviations from the behavior of older pixman releases. - * - deviations from the behavior of the same pixman release, but - * configured in a different way (for example with SIMD optimizations - * disabled), or running on a different OS or hardware. - * - * The test is performed by calling a callback function a huge number - * of times. The callback function is expected to run some snippet of - * pixman code with pseudorandom variations to the data feeded to - * pixman API. A result of running each callback function should be - * some deterministic value which depends on test number (test number - * can be used as a seed for PRNG). When 'verbose' argument is nonzero, - * callback function is expected to print to stdout some information - * about what it does. - * - * Return values from many small tests are accumulated together and - * used as final checksum, which can be compared to some expected - * value. Running the tests not individually, but in a batch helps - * to reduce process start overhead and also allows to parallelize - * testing and utilize multiple CPU cores. - * - * The resulting executable can be run without any arguments. In - * this case it runs a batch of tests starting from 1 and up to - * 'default_number_of_iterations'. The resulting checksum is - * compared with 'expected_checksum' and FAIL or PASS verdict - * depends on the result of this comparison. - * - * If the executable is run with 2 numbers provided as command line - * arguments, they specify the starting and ending numbers for a test - * batch. - * - * If the executable is run with only one number provided as a command - * line argument, then this number is used to call the callback function - * once, and also with verbose flag set. - */ -int -fuzzer_test_main (const char *test_name, - int default_number_of_iterations, - uint32_t expected_checksum, - uint32_t (*test_function)(int testnum, int verbose), - int argc, - const char *argv[]) -{ - int i, n1 = 1, n2 = 0; - uint32_t checksum = 0; - int verbose = getenv ("VERBOSE") != NULL; - - if (argc >= 3) - { - n1 = atoi (argv[1]); - n2 = atoi (argv[2]); - if (n2 < n1) - { - printf ("invalid test range\n"); - return 1; - } - } - else if (argc >= 2) - { - n2 = atoi (argv[1]); - checksum = test_function (n2, 1); - printf ("%d: checksum=%08X\n", n2, checksum); - return 0; - } - else - { - n1 = 1; - n2 = default_number_of_iterations; - } - -#ifdef USE_OPENMP - #pragma omp parallel for reduction(+:checksum) default(none) \ - shared(n1, n2, test_function, verbose) -#endif - for (i = n1; i <= n2; i++) - { - uint32_t crc = test_function (i, 0); - if (verbose) - printf ("%d: %08X\n", i, crc); - checksum += crc; - } - - if (n1 == 1 && n2 == default_number_of_iterations) - { - if (checksum == expected_checksum) - { - printf ("%s test passed (checksum=%08X)\n", - test_name, checksum); - } - else - { - printf ("%s test failed! (checksum=%08X, expected %08X)\n", - test_name, checksum, expected_checksum); - return 1; - } - } - else - { - printf ("%d-%d: checksum=%08X\n", n1, n2, checksum); - } - - return 0; -} - -/* Try to obtain current time in seconds */ -double -gettime (void) -{ -#ifdef HAVE_GETTIMEOFDAY - struct timeval tv; - - gettimeofday (&tv, NULL); - return (double)((int64_t)tv.tv_sec * 1000000 + tv.tv_usec) / 1000000.; -#else - return (double)clock() / (double)CLOCKS_PER_SEC; -#endif -} - -uint32_t -get_random_seed (void) -{ - double d = gettime(); - - lcg_srand (*(uint32_t *)&d); - - return lcg_rand_u32 (); -} - -static const char *global_msg; - -static void -on_alarm (int signo) -{ - printf ("%s\n", global_msg); - exit (1); -} - -void -fail_after (int seconds, const char *msg) -{ -#ifdef HAVE_SIGACTION -#ifdef HAVE_ALARM - struct sigaction action; - - global_msg = msg; - - memset (&action, 0, sizeof (action)); - action.sa_handler = on_alarm; - - alarm (seconds); - - sigaction (SIGALRM, &action, NULL); -#endif -#endif -} - -void -enable_fp_exceptions (void) -{ -#ifdef HAVE_FENV_H -#ifdef HAVE_FEENABLEEXCEPT - /* Note: we don't enable the FE_INEXACT trap because - * that happens quite commonly. It is possible that - * over- and underflow should similarly be considered - * okay, but for now the test suite passes with them - * enabled, and it's useful to know if they start - * occuring. - */ - feenableexcept (FE_DIVBYZERO | - FE_INVALID | - FE_OVERFLOW | - FE_UNDERFLOW); -#endif -#endif -} - -void * -aligned_malloc (size_t align, size_t size) -{ - void *result; - -#ifdef HAVE_POSIX_MEMALIGN - if (posix_memalign (&result, align, size) != 0) - result = NULL; -#else - result = malloc (size); -#endif - - return result; -} - -#define CONVERT_15(c, is_rgb) \ - (is_rgb? \ - ((((c) >> 3) & 0x001f) | \ - (((c) >> 6) & 0x03e0) | \ - (((c) >> 9) & 0x7c00)) : \ - (((((c) >> 16) & 0xff) * 153 + \ - (((c) >> 8) & 0xff) * 301 + \ - (((c) ) & 0xff) * 58) >> 2)) - -void -initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb) -{ - int i; - uint32_t mask = (1 << depth) - 1; - - for (i = 0; i < 32768; ++i) - palette->ent[i] = lcg_rand() & mask; - - memset (palette->rgba, 0, sizeof (palette->rgba)); - - for (i = 0; i < mask + 1; ++i) - { - uint32_t rgba24; - pixman_bool_t retry; - uint32_t i15; - - /* We filled the rgb->index map with random numbers, but we - * do need the ability to round trip, that is if some indexed - * color expands to an argb24, then the 15 bit version of that - * color must map back to the index. Anything else, we don't - * care about too much. - */ - do - { - uint32_t old_idx; - - rgba24 = lcg_rand(); - i15 = CONVERT_15 (rgba24, is_rgb); - - old_idx = palette->ent[i15]; - if (CONVERT_15 (palette->rgba[old_idx], is_rgb) == i15) - retry = 1; - else - retry = 0; - } while (retry); - - palette->rgba[i] = rgba24; - palette->ent[i15] = i; - } - - for (i = 0; i < mask + 1; ++i) - { - assert (palette->ent[CONVERT_15 (palette->rgba[i], is_rgb)] == i); - } -} +#define _GNU_SOURCE + +#include "utils.h" +#include + +#ifdef HAVE_GETTIMEOFDAY +#include +#else +#include +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif + +#ifdef HAVE_SYS_MMAN_H +#include +#endif + +#ifdef HAVE_FENV_H +#include +#endif + +#ifdef HAVE_LIBPNG +#include +#endif + +/* Random number seed + */ + +uint32_t lcg_seed; + +/*----------------------------------------------------------------------------*\ + * CRC-32 version 2.0.0 by Craig Bruce, 2006-04-29. + * + * This program generates the CRC-32 values for the files named in the + * command-line arguments. These are the same CRC-32 values used by GZIP, + * PKZIP, and ZMODEM. The Crc32_ComputeBuf () can also be detached and + * used independently. + * + * THIS PROGRAM IS PUBLIC-DOMAIN SOFTWARE. + * + * Based on the byte-oriented implementation "File Verification Using CRC" + * by Mark R. Nelson in Dr. Dobb's Journal, May 1992, pp. 64-67. + * + * v1.0.0: original release. + * v1.0.1: fixed printf formats. + * v1.0.2: fixed something else. + * v1.0.3: replaced CRC constant table by generator function. + * v1.0.4: reformatted code, made ANSI C. 1994-12-05. + * v2.0.0: rewrote to use memory buffer & static table, 2006-04-29. +\*----------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------------*\ + * NAME: + * Crc32_ComputeBuf () - computes the CRC-32 value of a memory buffer + * DESCRIPTION: + * Computes or accumulates the CRC-32 value for a memory buffer. + * The 'inCrc32' gives a previously accumulated CRC-32 value to allow + * a CRC to be generated for multiple sequential buffer-fuls of data. + * The 'inCrc32' for the first buffer must be zero. + * ARGUMENTS: + * inCrc32 - accumulated CRC-32 value, must be 0 on first call + * buf - buffer to compute CRC-32 value for + * bufLen - number of bytes in buffer + * RETURNS: + * crc32 - computed CRC-32 value + * ERRORS: + * (no errors are possible) +\*----------------------------------------------------------------------------*/ + +uint32_t +compute_crc32 (uint32_t in_crc32, + const void *buf, + size_t buf_len) +{ + static const uint32_t crc_table[256] = { + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, + 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, + 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, + 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, + 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, + 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, + 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, + 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, + 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, + 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, + 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, + 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, + 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, + 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, + 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, + 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, + 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, + 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, + 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, + 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, + 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, + 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D + }; + + uint32_t crc32; + unsigned char * byte_buf; + size_t i; + + /* accumulate crc32 for buffer */ + crc32 = in_crc32 ^ 0xFFFFFFFF; + byte_buf = (unsigned char*) buf; + + for (i = 0; i < buf_len; i++) + crc32 = (crc32 >> 8) ^ crc_table[(crc32 ^ byte_buf[i]) & 0xFF]; + + return (crc32 ^ 0xFFFFFFFF); +} + +pixman_bool_t +is_little_endian (void) +{ + volatile uint16_t endian_check_var = 0x1234; + + return (*(volatile uint8_t *)&endian_check_var == 0x34); +} + +/* perform endian conversion of pixel data + */ +void +image_endian_swap (pixman_image_t *img) +{ + int stride = pixman_image_get_stride (img); + uint32_t *data = pixman_image_get_data (img); + int height = pixman_image_get_height (img); + int bpp = PIXMAN_FORMAT_BPP (pixman_image_get_format (img)); + int i, j; + + /* swap bytes only on big endian systems */ + if (is_little_endian()) + return; + + if (bpp == 8) + return; + + for (i = 0; i < height; i++) + { + uint8_t *line_data = (uint8_t *)data + stride * i; + + switch (bpp) + { + case 1: + for (j = 0; j < stride; j++) + { + line_data[j] = + ((line_data[j] & 0x80) >> 7) | + ((line_data[j] & 0x40) >> 5) | + ((line_data[j] & 0x20) >> 3) | + ((line_data[j] & 0x10) >> 1) | + ((line_data[j] & 0x08) << 1) | + ((line_data[j] & 0x04) << 3) | + ((line_data[j] & 0x02) << 5) | + ((line_data[j] & 0x01) << 7); + } + break; + case 4: + for (j = 0; j < stride; j++) + { + line_data[j] = (line_data[j] >> 4) | (line_data[j] << 4); + } + break; + case 16: + for (j = 0; j + 2 <= stride; j += 2) + { + char t1 = line_data[j + 0]; + char t2 = line_data[j + 1]; + + line_data[j + 1] = t1; + line_data[j + 0] = t2; + } + break; + case 24: + for (j = 0; j + 3 <= stride; j += 3) + { + char t1 = line_data[j + 0]; + char t2 = line_data[j + 1]; + char t3 = line_data[j + 2]; + + line_data[j + 2] = t1; + line_data[j + 1] = t2; + line_data[j + 0] = t3; + } + break; + case 32: + for (j = 0; j + 4 <= stride; j += 4) + { + char t1 = line_data[j + 0]; + char t2 = line_data[j + 1]; + char t3 = line_data[j + 2]; + char t4 = line_data[j + 3]; + + line_data[j + 3] = t1; + line_data[j + 2] = t2; + line_data[j + 1] = t3; + line_data[j + 0] = t4; + } + break; + default: + assert (FALSE); + break; + } + } +} + +#define N_LEADING_PROTECTED 10 +#define N_TRAILING_PROTECTED 10 + +typedef struct +{ + void *addr; + uint32_t len; + uint8_t *trailing; + int n_bytes; +} info_t; + +#if defined(HAVE_MPROTECT) && defined(HAVE_GETPAGESIZE) && defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP) + +/* This is apparently necessary on at least OS X */ +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + +void * +fence_malloc (int64_t len) +{ + unsigned long page_size = getpagesize(); + unsigned long page_mask = page_size - 1; + uint32_t n_payload_bytes = (len + page_mask) & ~page_mask; + uint32_t n_bytes = + (page_size * (N_LEADING_PROTECTED + N_TRAILING_PROTECTED + 2) + + n_payload_bytes) & ~page_mask; + uint8_t *initial_page; + uint8_t *leading_protected; + uint8_t *trailing_protected; + uint8_t *payload; + uint8_t *addr; + + if (len < 0) + abort(); + + addr = mmap (NULL, n_bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + + if (addr == MAP_FAILED) + { + printf ("mmap failed on %lld %u\n", (long long int)len, n_bytes); + return NULL; + } + + initial_page = (uint8_t *)(((unsigned long)addr + page_mask) & ~page_mask); + leading_protected = initial_page + page_size; + payload = leading_protected + N_LEADING_PROTECTED * page_size; + trailing_protected = payload + n_payload_bytes; + + ((info_t *)initial_page)->addr = addr; + ((info_t *)initial_page)->len = len; + ((info_t *)initial_page)->trailing = trailing_protected; + ((info_t *)initial_page)->n_bytes = n_bytes; + + if ((mprotect (leading_protected, N_LEADING_PROTECTED * page_size, + PROT_NONE) == -1) || + (mprotect (trailing_protected, N_TRAILING_PROTECTED * page_size, + PROT_NONE) == -1)) + { + munmap (addr, n_bytes); + return NULL; + } + + return payload; +} + +void +fence_free (void *data) +{ + uint32_t page_size = getpagesize(); + uint8_t *payload = data; + uint8_t *leading_protected = payload - N_LEADING_PROTECTED * page_size; + uint8_t *initial_page = leading_protected - page_size; + info_t *info = (info_t *)initial_page; + + munmap (info->addr, info->n_bytes); +} + +#else + +void * +fence_malloc (int64_t len) +{ + return malloc (len); +} + +void +fence_free (void *data) +{ + free (data); +} + +#endif + +uint8_t * +make_random_bytes (int n_bytes) +{ + uint8_t *bytes = fence_malloc (n_bytes); + int i; + + if (!bytes) + return NULL; + + for (i = 0; i < n_bytes; ++i) + bytes[i] = lcg_rand () & 0xff; + + return bytes; +} + +#ifdef HAVE_LIBPNG + +static void +pngify_pixels (uint32_t *pixels, int n_pixels) +{ + int i; + + for (i = 0; i < n_pixels; ++i) + { + uint32_t p = pixels[i]; + uint8_t *out = (uint8_t *)&(pixels[i]); + uint8_t a, r, g, b; + + a = (p & 0xff000000) >> 24; + r = (p & 0x00ff0000) >> 16; + g = (p & 0x0000ff00) >> 8; + b = (p & 0x000000ff) >> 0; + + if (a != 0) + { + r = (r * 255) / a; + g = (g * 255) / a; + b = (b * 255) / a; + } + + *out++ = r; + *out++ = g; + *out++ = b; + *out++ = a; + } +} + +pixman_bool_t +write_png (pixman_image_t *image, const char *filename) +{ + int width = pixman_image_get_width (image); + int height = pixman_image_get_height (image); + int stride = width * 4; + uint32_t *data = malloc (height * stride); + pixman_image_t *copy; + png_struct *write_struct; + png_info *info_struct; + pixman_bool_t result = FALSE; + FILE *f = fopen (filename, "wb"); + png_bytep *row_pointers; + int i; + + if (!f) + return FALSE; + + row_pointers = malloc (height * sizeof (png_bytep)); + + copy = pixman_image_create_bits ( + PIXMAN_a8r8g8b8, width, height, data, stride); + + pixman_image_composite32 ( + PIXMAN_OP_SRC, image, NULL, copy, 0, 0, 0, 0, 0, 0, width, height); + + pngify_pixels (data, height * width); + + for (i = 0; i < height; ++i) + row_pointers[i] = (png_bytep)(data + i * width); + + if (!(write_struct = png_create_write_struct ( + PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) + goto out1; + + if (!(info_struct = png_create_info_struct (write_struct))) + goto out2; + + png_init_io (write_struct, f); + + png_set_IHDR (write_struct, info_struct, width, height, + 8, PNG_COLOR_TYPE_RGB_ALPHA, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); + + png_write_info (write_struct, info_struct); + + png_write_image (write_struct, row_pointers); + + png_write_end (write_struct, NULL); + + result = TRUE; + +out2: + png_destroy_write_struct (&write_struct, &info_struct); + +out1: + if (fclose (f) != 0) + result = FALSE; + + pixman_image_unref (copy); + free (row_pointers); + free (data); + return result; +} + +#else /* no libpng */ + +pixman_bool_t +write_png (pixman_image_t *image, const char *filename) +{ + return FALSE; +} + +#endif + +/* + * A function, which can be used as a core part of the test programs, + * intended to detect various problems with the help of fuzzing input + * to pixman API (according to some templates, aka "smart" fuzzing). + * Some general information about such testing can be found here: + * http://en.wikipedia.org/wiki/Fuzz_testing + * + * It may help detecting: + * - crashes on bad handling of valid or reasonably invalid input to + * pixman API. + * - deviations from the behavior of older pixman releases. + * - deviations from the behavior of the same pixman release, but + * configured in a different way (for example with SIMD optimizations + * disabled), or running on a different OS or hardware. + * + * The test is performed by calling a callback function a huge number + * of times. The callback function is expected to run some snippet of + * pixman code with pseudorandom variations to the data feeded to + * pixman API. A result of running each callback function should be + * some deterministic value which depends on test number (test number + * can be used as a seed for PRNG). When 'verbose' argument is nonzero, + * callback function is expected to print to stdout some information + * about what it does. + * + * Return values from many small tests are accumulated together and + * used as final checksum, which can be compared to some expected + * value. Running the tests not individually, but in a batch helps + * to reduce process start overhead and also allows to parallelize + * testing and utilize multiple CPU cores. + * + * The resulting executable can be run without any arguments. In + * this case it runs a batch of tests starting from 1 and up to + * 'default_number_of_iterations'. The resulting checksum is + * compared with 'expected_checksum' and FAIL or PASS verdict + * depends on the result of this comparison. + * + * If the executable is run with 2 numbers provided as command line + * arguments, they specify the starting and ending numbers for a test + * batch. + * + * If the executable is run with only one number provided as a command + * line argument, then this number is used to call the callback function + * once, and also with verbose flag set. + */ +int +fuzzer_test_main (const char *test_name, + int default_number_of_iterations, + uint32_t expected_checksum, + uint32_t (*test_function)(int testnum, int verbose), + int argc, + const char *argv[]) +{ + int i, n1 = 1, n2 = 0; + uint32_t checksum = 0; + int verbose = getenv ("VERBOSE") != NULL; + + if (argc >= 3) + { + n1 = atoi (argv[1]); + n2 = atoi (argv[2]); + if (n2 < n1) + { + printf ("invalid test range\n"); + return 1; + } + } + else if (argc >= 2) + { + n2 = atoi (argv[1]); + checksum = test_function (n2, 1); + printf ("%d: checksum=%08X\n", n2, checksum); + return 0; + } + else + { + n1 = 1; + n2 = default_number_of_iterations; + } + +#ifdef USE_OPENMP + #pragma omp parallel for reduction(+:checksum) default(none) \ + shared(n1, n2, test_function, verbose) +#endif + for (i = n1; i <= n2; i++) + { + uint32_t crc = test_function (i, 0); + if (verbose) + printf ("%d: %08X\n", i, crc); + checksum += crc; + } + + if (n1 == 1 && n2 == default_number_of_iterations) + { + if (checksum == expected_checksum) + { + printf ("%s test passed (checksum=%08X)\n", + test_name, checksum); + } + else + { + printf ("%s test failed! (checksum=%08X, expected %08X)\n", + test_name, checksum, expected_checksum); + return 1; + } + } + else + { + printf ("%d-%d: checksum=%08X\n", n1, n2, checksum); + } + + return 0; +} + +/* Try to obtain current time in seconds */ +double +gettime (void) +{ +#ifdef HAVE_GETTIMEOFDAY + struct timeval tv; + + gettimeofday (&tv, NULL); + return (double)((int64_t)tv.tv_sec * 1000000 + tv.tv_usec) / 1000000.; +#else + return (double)clock() / (double)CLOCKS_PER_SEC; +#endif +} + +uint32_t +get_random_seed (void) +{ + double d = gettime(); + + lcg_srand (*(uint32_t *)&d); + + return lcg_rand_u32 (); +} + +static const char *global_msg; + +static void +on_alarm (int signo) +{ + printf ("%s\n", global_msg); + exit (1); +} + +void +fail_after (int seconds, const char *msg) +{ +#ifdef HAVE_SIGACTION +#ifdef HAVE_ALARM + struct sigaction action; + + global_msg = msg; + + memset (&action, 0, sizeof (action)); + action.sa_handler = on_alarm; + + alarm (seconds); + + sigaction (SIGALRM, &action, NULL); +#endif +#endif +} + +void +enable_fp_exceptions (void) +{ +#ifdef HAVE_FENV_H +#ifdef HAVE_FEENABLEEXCEPT + /* Note: we don't enable the FE_INEXACT trap because + * that happens quite commonly. It is possible that + * over- and underflow should similarly be considered + * okay, but for now the test suite passes with them + * enabled, and it's useful to know if they start + * occuring. + */ + feenableexcept (FE_DIVBYZERO | + FE_INVALID | + FE_OVERFLOW | + FE_UNDERFLOW); +#endif +#endif +} + +void * +aligned_malloc (size_t align, size_t size) +{ + void *result; + +#ifdef HAVE_POSIX_MEMALIGN + if (posix_memalign (&result, align, size) != 0) + result = NULL; +#else + result = malloc (size); +#endif + + return result; +} + +#define CONVERT_15(c, is_rgb) \ + (is_rgb? \ + ((((c) >> 3) & 0x001f) | \ + (((c) >> 6) & 0x03e0) | \ + (((c) >> 9) & 0x7c00)) : \ + (((((c) >> 16) & 0xff) * 153 + \ + (((c) >> 8) & 0xff) * 301 + \ + (((c) ) & 0xff) * 58) >> 2)) + +void +initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb) +{ + int i; + uint32_t mask = (1 << depth) - 1; + + for (i = 0; i < 32768; ++i) + palette->ent[i] = lcg_rand() & mask; + + memset (palette->rgba, 0, sizeof (palette->rgba)); + + for (i = 0; i < mask + 1; ++i) + { + uint32_t rgba24; + pixman_bool_t retry; + uint32_t i15; + + /* We filled the rgb->index map with random numbers, but we + * do need the ability to round trip, that is if some indexed + * color expands to an argb24, then the 15 bit version of that + * color must map back to the index. Anything else, we don't + * care about too much. + */ + do + { + uint32_t old_idx; + + rgba24 = lcg_rand(); + i15 = CONVERT_15 (rgba24, is_rgb); + + old_idx = palette->ent[i15]; + if (CONVERT_15 (palette->rgba[old_idx], is_rgb) == i15) + retry = 1; + else + retry = 0; + } while (retry); + + palette->rgba[i] = rgba24; + palette->ent[i15] = i; + } + + for (i = 0; i < mask + 1; ++i) + { + assert (palette->ent[CONVERT_15 (palette->rgba[i], is_rgb)] == i); + } +} diff --git a/pixman/test/utils.h b/pixman/test/utils.h index cd9a3c263..3790483db 100644 --- a/pixman/test/utils.h +++ b/pixman/test/utils.h @@ -1,151 +1,151 @@ -#include -#include -#include -#include "pixman-private.h" /* For 'inline' definition */ - -#define ARRAY_LENGTH(A) ((int) (sizeof (A) / sizeof ((A) [0]))) - -/* A primitive pseudorandom number generator, - * taken from POSIX.1-2001 example - */ - -extern uint32_t lcg_seed; -#ifdef USE_OPENMP -#pragma omp threadprivate(lcg_seed) -#endif - -static inline uint32_t -lcg_rand (void) -{ - lcg_seed = lcg_seed * 1103515245 + 12345; - return ((uint32_t)(lcg_seed / 65536) % 32768); -} - -static inline void -lcg_srand (uint32_t seed) -{ - lcg_seed = seed; -} - -static inline uint32_t -lcg_rand_n (int max) -{ - return lcg_rand () % max; -} - -static inline uint32_t -lcg_rand_N (int max) -{ - uint32_t lo = lcg_rand (); - uint32_t hi = lcg_rand () << 15; - return (lo | hi) % max; -} - -static inline uint32_t -lcg_rand_u32 (void) -{ - /* This uses the 10/11 most significant bits from the 3 lcg results - * (and mixes them with the low from the adjacent one). - */ - uint32_t lo = lcg_rand() >> -(32 - 15 - 11 * 2); - uint32_t mid = lcg_rand() << (32 - 15 - 11 * 1); - uint32_t hi = lcg_rand() << (32 - 15 - 11 * 0); - - return (hi ^ mid ^ lo); -} - -/* CRC 32 computation - */ -uint32_t -compute_crc32 (uint32_t in_crc32, - const void *buf, - size_t buf_len); - -/* Returns TRUE if running on a little endian system */ -pixman_bool_t -is_little_endian (void); - -/* perform endian conversion of pixel data - */ -void -image_endian_swap (pixman_image_t *img); - -/* Allocate memory that is bounded by protected pages, - * so that out-of-bounds access will cause segfaults - */ -void * -fence_malloc (int64_t len); - -void -fence_free (void *data); - -/* Generate n_bytes random bytes in fence_malloced memory */ -uint8_t * -make_random_bytes (int n_bytes); - -/* Return current time in seconds */ -double -gettime (void); - -uint32_t -get_random_seed (void); - -/* main body of the fuzzer test */ -int -fuzzer_test_main (const char *test_name, - int default_number_of_iterations, - uint32_t expected_checksum, - uint32_t (*test_function)(int testnum, int verbose), - int argc, - const char *argv[]); - -void -fail_after (int seconds, const char *msg); - -/* If possible, enable traps for floating point exceptions */ -void enable_fp_exceptions(void); - -pixman_bool_t -write_png (pixman_image_t *image, const char *filename); - -/* A pair of macros which can help to detect corruption of - * floating point registers after a function call. This may - * happen if _mm_empty() call is forgotten in MMX/SSE2 fast - * path code, or ARM NEON assembly optimized function forgets - * to save/restore d8-d15 registers before use. - */ - -#define FLOAT_REGS_CORRUPTION_DETECTOR_START() \ - static volatile double frcd_volatile_constant1 = 123451; \ - static volatile double frcd_volatile_constant2 = 123452; \ - static volatile double frcd_volatile_constant3 = 123453; \ - static volatile double frcd_volatile_constant4 = 123454; \ - static volatile double frcd_volatile_constant5 = 123455; \ - static volatile double frcd_volatile_constant6 = 123456; \ - static volatile double frcd_volatile_constant7 = 123457; \ - static volatile double frcd_volatile_constant8 = 123458; \ - double frcd_canary_variable1 = frcd_volatile_constant1; \ - double frcd_canary_variable2 = frcd_volatile_constant2; \ - double frcd_canary_variable3 = frcd_volatile_constant3; \ - double frcd_canary_variable4 = frcd_volatile_constant4; \ - double frcd_canary_variable5 = frcd_volatile_constant5; \ - double frcd_canary_variable6 = frcd_volatile_constant6; \ - double frcd_canary_variable7 = frcd_volatile_constant7; \ - double frcd_canary_variable8 = frcd_volatile_constant8; - -#define FLOAT_REGS_CORRUPTION_DETECTOR_FINISH() \ - assert (frcd_canary_variable1 == frcd_volatile_constant1); \ - assert (frcd_canary_variable2 == frcd_volatile_constant2); \ - assert (frcd_canary_variable3 == frcd_volatile_constant3); \ - assert (frcd_canary_variable4 == frcd_volatile_constant4); \ - assert (frcd_canary_variable5 == frcd_volatile_constant5); \ - assert (frcd_canary_variable6 == frcd_volatile_constant6); \ - assert (frcd_canary_variable7 == frcd_volatile_constant7); \ - assert (frcd_canary_variable8 == frcd_volatile_constant8); - -/* Try to get an aligned memory chunk */ -void * -aligned_malloc (size_t align, size_t size); - -void -initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb); +#include +#include +#include +#include "pixman-private.h" /* For 'inline' definition */ + +#define ARRAY_LENGTH(A) ((int) (sizeof (A) / sizeof ((A) [0]))) + +/* A primitive pseudorandom number generator, + * taken from POSIX.1-2001 example + */ + +extern uint32_t lcg_seed; +#ifdef USE_OPENMP +#pragma omp threadprivate(lcg_seed) +#endif + +static inline uint32_t +lcg_rand (void) +{ + lcg_seed = lcg_seed * 1103515245 + 12345; + return ((uint32_t)(lcg_seed / 65536) % 32768); +} + +static inline void +lcg_srand (uint32_t seed) +{ + lcg_seed = seed; +} + +static inline uint32_t +lcg_rand_n (int max) +{ + return lcg_rand () % max; +} + +static inline uint32_t +lcg_rand_N (int max) +{ + uint32_t lo = lcg_rand (); + uint32_t hi = lcg_rand () << 15; + return (lo | hi) % max; +} + +static inline uint32_t +lcg_rand_u32 (void) +{ + /* This uses the 10/11 most significant bits from the 3 lcg results + * (and mixes them with the low from the adjacent one). + */ + uint32_t lo = lcg_rand() >> -(32 - 15 - 11 * 2); + uint32_t mid = lcg_rand() << (32 - 15 - 11 * 1); + uint32_t hi = lcg_rand() << (32 - 15 - 11 * 0); + + return (hi ^ mid ^ lo); +} + +/* CRC 32 computation + */ +uint32_t +compute_crc32 (uint32_t in_crc32, + const void *buf, + size_t buf_len); + +/* Returns TRUE if running on a little endian system */ +pixman_bool_t +is_little_endian (void); + +/* perform endian conversion of pixel data + */ +void +image_endian_swap (pixman_image_t *img); + +/* Allocate memory that is bounded by protected pages, + * so that out-of-bounds access will cause segfaults + */ +void * +fence_malloc (int64_t len); + +void +fence_free (void *data); + +/* Generate n_bytes random bytes in fence_malloced memory */ +uint8_t * +make_random_bytes (int n_bytes); + +/* Return current time in seconds */ +double +gettime (void); + +uint32_t +get_random_seed (void); + +/* main body of the fuzzer test */ +int +fuzzer_test_main (const char *test_name, + int default_number_of_iterations, + uint32_t expected_checksum, + uint32_t (*test_function)(int testnum, int verbose), + int argc, + const char *argv[]); + +void +fail_after (int seconds, const char *msg); + +/* If possible, enable traps for floating point exceptions */ +void enable_fp_exceptions(void); + +pixman_bool_t +write_png (pixman_image_t *image, const char *filename); + +/* A pair of macros which can help to detect corruption of + * floating point registers after a function call. This may + * happen if _mm_empty() call is forgotten in MMX/SSE2 fast + * path code, or ARM NEON assembly optimized function forgets + * to save/restore d8-d15 registers before use. + */ + +#define FLOAT_REGS_CORRUPTION_DETECTOR_START() \ + static volatile double frcd_volatile_constant1 = 123451; \ + static volatile double frcd_volatile_constant2 = 123452; \ + static volatile double frcd_volatile_constant3 = 123453; \ + static volatile double frcd_volatile_constant4 = 123454; \ + static volatile double frcd_volatile_constant5 = 123455; \ + static volatile double frcd_volatile_constant6 = 123456; \ + static volatile double frcd_volatile_constant7 = 123457; \ + static volatile double frcd_volatile_constant8 = 123458; \ + double frcd_canary_variable1 = frcd_volatile_constant1; \ + double frcd_canary_variable2 = frcd_volatile_constant2; \ + double frcd_canary_variable3 = frcd_volatile_constant3; \ + double frcd_canary_variable4 = frcd_volatile_constant4; \ + double frcd_canary_variable5 = frcd_volatile_constant5; \ + double frcd_canary_variable6 = frcd_volatile_constant6; \ + double frcd_canary_variable7 = frcd_volatile_constant7; \ + double frcd_canary_variable8 = frcd_volatile_constant8; + +#define FLOAT_REGS_CORRUPTION_DETECTOR_FINISH() \ + assert (frcd_canary_variable1 == frcd_volatile_constant1); \ + assert (frcd_canary_variable2 == frcd_volatile_constant2); \ + assert (frcd_canary_variable3 == frcd_volatile_constant3); \ + assert (frcd_canary_variable4 == frcd_volatile_constant4); \ + assert (frcd_canary_variable5 == frcd_volatile_constant5); \ + assert (frcd_canary_variable6 == frcd_volatile_constant6); \ + assert (frcd_canary_variable7 == frcd_volatile_constant7); \ + assert (frcd_canary_variable8 == frcd_volatile_constant8); + +/* Try to get an aligned memory chunk */ +void * +aligned_malloc (size_t align, size_t size); + +void +initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb); -- cgit v1.2.3