7 #define fence_malloc malloc
8 #define fence_free free
9 #define make_random_bytes malloc
12 static const pixman_format_code_t image_formats[] =
64 static pixman_filter_t filters[] =
66 PIXMAN_FILTER_NEAREST,
67 PIXMAN_FILTER_BILINEAR,
71 PIXMAN_FILTER_CONVOLUTION
77 switch (prng_rand_n (28))
87 return prng_rand_n (100);
90 return prng_rand_n (2000) + 1000;
99 return prng_rand_n (64000) + 63000;
104 destroy (pixman_image_t *image, void *data)
106 if (image->type == BITS && image->bits.free_me != image->bits.bits)
110 if (image->bits.bits != (void *)0x01)
112 bits = image->bits.bits;
114 if (image->bits.rowstride < 0)
115 bits -= (- image->bits.rowstride * (image->bits.height - 1));
125 real_reader (const void *src, int size)
130 return *(uint8_t *)src;
132 return *(uint16_t *)src;
134 return *(uint32_t *)src;
137 return 0; /* silence MSVC */
142 real_writer (void *src, uint32_t value, int size)
147 *(uint8_t *)src = value;
151 *(uint16_t *)src = value;
155 *(uint32_t *)src = value;
165 fake_reader (const void *src, int size)
167 uint32_t r = prng_rand ();
169 assert (size == 1 || size == 2 || size == 4);
171 return r >> (32 - (size * 8));
175 fake_writer (void *src, uint32_t value, int size)
177 assert (size == 1 || size == 2 || size == 4);
185 mask = (1 << prng_rand_n (10)) - 1;
187 return (prng_rand () & mask) - (mask >> 1);
191 rand_x (pixman_image_t *image)
193 if (image->type == BITS)
194 return prng_rand_n (image->bits.width);
200 rand_y (pixman_image_t *image)
202 if (image->type == BITS)
203 return prng_rand_n (image->bits.height);
213 } alpha_preference_t;
215 static pixman_format_code_t
216 random_format (alpha_preference_t alpha)
218 pixman_format_code_t format;
219 int n = prng_rand_n (ARRAY_LENGTH (image_formats));
221 if (alpha >= PREFER_ALPHA &&
222 (alpha == REQUIRE_ALPHA || prng_rand_n (4) != 0))
226 format = image_formats[n++ % ARRAY_LENGTH (image_formats)];
227 } while (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_A);
231 format = image_formats[n];
237 static pixman_image_t *
238 create_random_bits_image (alpha_preference_t alpha_preference)
240 pixman_format_code_t format;
241 pixman_indexed_t *indexed;
242 pixman_image_t *image;
243 int width, height, stride;
245 pixman_read_memory_func_t read_func = NULL;
246 pixman_write_memory_func_t write_func = NULL;
247 pixman_filter_t filter;
248 pixman_fixed_t *coefficients = NULL;
249 int n_coefficients = 0;
252 format = random_format (alpha_preference);
255 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR)
257 indexed = malloc (sizeof (pixman_indexed_t));
259 initialize_palette (indexed, PIXMAN_FORMAT_BPP (format), TRUE);
261 else if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY)
263 indexed = malloc (sizeof (pixman_indexed_t));
265 initialize_palette (indexed, PIXMAN_FORMAT_BPP (format), FALSE);
274 height = get_size ();
276 while ((uint64_t)width * height > 200000)
278 if (prng_rand_n(2) == 0)
279 height = 200000 / width;
281 width = 200000 / height;
290 switch (prng_rand_n (7))
294 stride = width * PIXMAN_FORMAT_BPP (format) + prng_rand_n (17);
295 stride = (stride + 3) & (~3);
296 bits = (uint32_t *)make_random_bytes (height * stride);
304 case 2: /* Zero-filled */
305 stride = width * PIXMAN_FORMAT_BPP (format) + prng_rand_n (17);
306 stride = (stride + 3) & (~3);
307 bits = fence_malloc (height * stride);
310 memset (bits, 0, height * stride);
313 case 3: /* Filled with 0xFF */
314 stride = width * PIXMAN_FORMAT_BPP (format) + prng_rand_n (17);
315 stride = (stride + 3) & (~3);
316 bits = fence_malloc (height * stride);
319 memset (bits, 0xff, height * stride);
322 case 4: /* bits is a bad pointer, has read/write functions */
325 read_func = fake_reader;
326 write_func = fake_writer;
329 case 5: /* bits is a real pointer, has read/write functions */
330 stride = width * PIXMAN_FORMAT_BPP (format) + prng_rand_n (17);
331 stride = (stride + 3) & (~3);
332 bits = fence_malloc (height * stride);
335 memset (bits, 0xff, height * stride);
336 read_func = real_reader;
337 write_func = real_writer;
340 case 6: /* bits is a real pointer, stride is negative */
341 stride = (width * PIXMAN_FORMAT_BPP (format) + prng_rand_n (17));
342 stride = (stride + 3) & (~3);
343 bits = (uint32_t *)make_random_bytes (height * stride);
346 bits += ((height - 1) * stride) / 4;
352 filter = filters[prng_rand_n (ARRAY_LENGTH (filters))];
353 if (filter == PIXMAN_FILTER_CONVOLUTION)
355 int width = prng_rand_n (3);
356 int height = prng_rand_n (4);
358 n_coefficients = width * height + 2;
359 coefficients = malloc (n_coefficients * sizeof (pixman_fixed_t));
365 for (i = 0; i < width * height; ++i)
366 coefficients[i + 2] = prng_rand();
368 coefficients[0] = width << 16;
369 coefficients[1] = height << 16;
373 filter = PIXMAN_FILTER_BEST;
377 /* Finally create the image */
378 image = pixman_image_create_bits (format, width, height, bits, stride);
382 pixman_image_set_indexed (image, indexed);
383 pixman_image_set_destroy_function (image, destroy, indexed);
384 pixman_image_set_accessors (image, read_func, write_func);
385 pixman_image_set_filter (image, filter, coefficients, n_coefficients);
390 static pixman_repeat_t repeats[] =
393 PIXMAN_REPEAT_NORMAL,
394 PIXMAN_REPEAT_REFLECT,
401 return i < 0? -i : i;
405 set_general_properties (pixman_image_t *image, pixman_bool_t allow_alpha_map)
407 pixman_repeat_t repeat;
409 /* Set properties that are generic to all images */
412 repeat = repeats[prng_rand_n (ARRAY_LENGTH (repeats))];
413 pixman_image_set_repeat (image, repeat);
416 if (allow_alpha_map && prng_rand_n (4) == 0)
418 pixman_image_t *alpha_map;
421 alpha_map = create_random_bits_image (DONT_CARE);
425 set_general_properties (alpha_map, FALSE);
427 x = rand_x (image) - image->bits.width / 2;
428 y = rand_y (image) - image->bits.height / 2;
430 pixman_image_set_alpha_map (image, alpha_map, x, y);
432 pixman_image_unref (alpha_map);
436 /* Component alpha */
437 pixman_image_set_component_alpha (image, prng_rand_n (3) == 0);
440 if (prng_rand_n (8) < 2)
442 pixman_region32_t region;
445 pixman_region32_init (®ion);
447 switch (prng_rand_n (12))
453 case 1: case 2: case 3:
466 n_rects = prng_rand_n (100);
470 for (i = 0; i < n_rects; ++i)
472 uint32_t width, height;
477 width = absolute (log_rand ()) + 1;
478 height = absolute (log_rand ()) + 1;
480 pixman_region32_union_rect (
481 ®ion, ®ion, x, y, width, height);
484 if (image->type == BITS && prng_rand_n (8) != 0)
486 uint32_t width, height;
490 /* Also add a couple of clip rectangles inside the image
491 * so that compositing will actually take place.
493 for (i = 0; i < 5; ++i)
495 x = prng_rand_n (2 * image->bits.width) - image->bits.width;
496 y = prng_rand_n (2 * image->bits.height) - image->bits.height;
497 width = prng_rand_n (image->bits.width) - x + 10;
498 height = prng_rand_n (image->bits.height) - y + 10;
501 width = INT32_MAX - x;
503 height = INT32_MAX - y;
505 pixman_region32_union_rect (
506 ®ion, ®ion, x, y, width, height);
510 pixman_image_set_clip_region32 (image, ®ion);
512 pixman_region32_fini (®ion);
515 /* Whether source clipping is enabled */
516 pixman_image_set_source_clipping (image, !!prng_rand_n (2));
519 pixman_image_set_has_client_clip (image, !!prng_rand_n (2));
522 if (prng_rand_n (5) < 2)
524 pixman_transform_t xform;
526 uint32_t tx, ty, sx, sy;
529 memset (&xform, 0, sizeof xform);
530 xform.matrix[0][0] = pixman_fixed_1;
531 xform.matrix[1][1] = pixman_fixed_1;
532 xform.matrix[2][2] = pixman_fixed_1;
534 for (k = 0; k < 3; ++k)
536 switch (prng_rand_n (4))
540 c = prng_rand_n (2 * 65536) - 65536;
541 s = prng_rand_n (2 * 65536) - 65536;
542 pixman_transform_rotate (&xform, NULL, c, s);
549 pixman_transform_translate (&xform, NULL, tx, ty);
556 pixman_transform_scale (&xform, NULL, sx, sy);
560 if (prng_rand_n (16) == 0)
563 for (i = 0; i < 3; ++i)
564 for (j = 0; j < 3; ++j)
565 xform.matrix[i][j] = prng_rand();
568 else if (prng_rand_n (16) == 0)
571 memset (&xform, 0, sizeof xform);
577 pixman_image_set_transform (image, &xform);
581 static pixman_color_t
584 pixman_color_t color =
586 prng_rand() & 0xffff,
587 prng_rand() & 0xffff,
588 prng_rand() & 0xffff,
589 prng_rand() & 0xffff,
596 static pixman_image_t *
597 create_random_solid_image (void)
599 pixman_color_t color = random_color();
600 pixman_image_t *image = pixman_image_create_solid_fill (&color);
605 static pixman_gradient_stop_t *
606 create_random_stops (int *n_stops)
611 pixman_gradient_stop_t *stops;
613 *n_stops = prng_rand_n (50) + 1;
615 step = pixman_fixed_1 / *n_stops;
617 stops = malloc (*n_stops * sizeof (pixman_gradient_stop_t));
620 for (i = 0; i < (*n_stops) - 1; ++i)
623 stops[i].color = random_color();
628 stops[*n_stops - 1].x = pixman_fixed_1;
629 stops[*n_stops - 1].color = random_color();
634 static pixman_point_fixed_t
635 create_random_point (void)
637 pixman_point_fixed_t p;
645 static pixman_image_t *
646 create_random_linear_image (void)
649 pixman_gradient_stop_t *stops;
650 pixman_point_fixed_t p1, p2;
651 pixman_image_t *result;
653 stops = create_random_stops (&n_stops);
657 p1 = create_random_point ();
658 p2 = create_random_point ();
660 result = pixman_image_create_linear_gradient (&p1, &p2, stops, n_stops);
667 static pixman_image_t *
668 create_random_radial_image (void)
671 pixman_gradient_stop_t *stops;
672 pixman_point_fixed_t inner_c, outer_c;
673 pixman_fixed_t inner_r, outer_r;
674 pixman_image_t *result;
676 inner_c = create_random_point();
677 outer_c = create_random_point();
678 inner_r = prng_rand();
679 outer_r = prng_rand();
681 stops = create_random_stops (&n_stops);
686 result = pixman_image_create_radial_gradient (
687 &inner_c, &outer_c, inner_r, outer_r, stops, n_stops);
694 static pixman_image_t *
695 create_random_conical_image (void)
697 pixman_gradient_stop_t *stops;
699 pixman_point_fixed_t c;
700 pixman_fixed_t angle;
701 pixman_image_t *result;
703 c = create_random_point();
706 stops = create_random_stops (&n_stops);
711 result = pixman_image_create_conical_gradient (&c, angle, stops, n_stops);
718 static pixman_image_t *
719 create_random_image (void)
721 pixman_image_t *result;
723 switch (prng_rand_n (5))
727 result = create_random_bits_image (DONT_CARE);
731 result = create_random_solid_image ();
735 result = create_random_linear_image ();
739 result = create_random_radial_image ();
743 result = create_random_conical_image ();
748 set_general_properties (result, TRUE);
754 random_line (pixman_line_fixed_t *line, int width, int height)
756 line->p1.x = prng_rand_n (width) << 16;
757 line->p1.y = prng_rand_n (height) << 16;
758 line->p2.x = prng_rand_n (width) << 16;
759 line->p2.y = prng_rand_n (height) << 16;
762 static pixman_trapezoid_t *
763 create_random_trapezoids (int *n_traps, int height, int width)
765 pixman_trapezoid_t *trapezoids;
768 *n_traps = prng_rand_n (16) + 1;
770 trapezoids = malloc (sizeof (pixman_trapezoid_t) * *n_traps);
772 for (i = 0; i < *n_traps; ++i)
774 pixman_trapezoid_t *t = &(trapezoids[i]);
776 t->top = prng_rand_n (height) << 16;
777 t->bottom = prng_rand_n (height) << 16;
779 random_line (&t->left, height, width);
780 random_line (&t->right, height, width);
786 static const pixman_op_t op_list[] =
795 PIXMAN_OP_OVER_REVERSE,
797 PIXMAN_OP_IN_REVERSE,
799 PIXMAN_OP_OUT_REVERSE,
801 PIXMAN_OP_ATOP_REVERSE,
805 PIXMAN_OP_DISJOINT_CLEAR,
806 PIXMAN_OP_DISJOINT_SRC,
807 PIXMAN_OP_DISJOINT_DST,
808 PIXMAN_OP_DISJOINT_OVER,
809 PIXMAN_OP_DISJOINT_OVER_REVERSE,
810 PIXMAN_OP_DISJOINT_IN,
811 PIXMAN_OP_DISJOINT_IN_REVERSE,
812 PIXMAN_OP_DISJOINT_OUT,
813 PIXMAN_OP_DISJOINT_OUT_REVERSE,
814 PIXMAN_OP_DISJOINT_ATOP,
815 PIXMAN_OP_DISJOINT_ATOP_REVERSE,
816 PIXMAN_OP_DISJOINT_XOR,
817 PIXMAN_OP_CONJOINT_CLEAR,
818 PIXMAN_OP_CONJOINT_SRC,
819 PIXMAN_OP_CONJOINT_DST,
820 PIXMAN_OP_CONJOINT_OVER,
821 PIXMAN_OP_CONJOINT_OVER_REVERSE,
822 PIXMAN_OP_CONJOINT_IN,
823 PIXMAN_OP_CONJOINT_IN_REVERSE,
824 PIXMAN_OP_CONJOINT_OUT,
825 PIXMAN_OP_CONJOINT_OUT_REVERSE,
826 PIXMAN_OP_CONJOINT_ATOP,
827 PIXMAN_OP_CONJOINT_ATOP_REVERSE,
828 PIXMAN_OP_CONJOINT_XOR,
834 PIXMAN_OP_COLOR_DODGE,
835 PIXMAN_OP_COLOR_BURN,
836 PIXMAN_OP_HARD_LIGHT,
837 PIXMAN_OP_DIFFERENCE,
839 PIXMAN_OP_SOFT_LIGHT,
841 PIXMAN_OP_HSL_SATURATION,
843 PIXMAN_OP_HSL_LUMINOSITY,
847 run_test (uint32_t seed, pixman_bool_t verbose, uint32_t mod)
849 pixman_image_t *source, *mask, *dest;
854 if (mod == 0 || (seed % mod) == 0)
855 printf ("Seed 0x%08x\n", seed);
858 source = mask = dest = NULL;
862 if (prng_rand_n (8) == 0)
865 pixman_trapezoid_t *trapezoids;
866 int p = prng_rand_n (3);
869 dest = create_random_bits_image (DONT_CARE);
871 dest = create_random_bits_image (REQUIRE_ALPHA);
876 set_general_properties (dest, TRUE);
878 if (!(trapezoids = create_random_trapezoids (
879 &n_traps, dest->bits.width, dest->bits.height)))
887 source = create_random_image ();
891 op = op_list [prng_rand_n (ARRAY_LENGTH (op_list))];
893 pixman_composite_trapezoids (
895 random_format (REQUIRE_ALPHA),
896 rand_x (source), rand_y (source),
897 rand_x (dest), rand_y (dest),
898 n_traps, trapezoids);
903 pixman_rasterize_trapezoid (
904 dest, &trapezoids[prng_rand_n (n_traps)],
905 rand_x (dest), rand_y (dest));
909 pixman_add_trapezoids (
910 dest, rand_x (dest), rand_y (dest), n_traps, trapezoids);
918 dest = create_random_bits_image (DONT_CARE);
919 source = create_random_image ();
920 mask = create_random_image ();
922 if (source && mask && dest)
924 set_general_properties (dest, TRUE);
926 op = op_list [prng_rand_n (ARRAY_LENGTH (op_list))];
928 pixman_image_composite32 (op,
930 rand_x (source), rand_y (source),
931 rand_x (mask), rand_y (mask),
940 pixman_image_unref (source);
942 pixman_image_unref (mask);
944 pixman_image_unref (dest);
948 get_int (char *s, uint32_t *i)
953 p = strtol (s, &end, 0);
955 if (end != s && *end == 0)
965 main (int argc, char **argv)
969 uint32_t n_tests = 8000;
971 pixman_bool_t use_threads = TRUE;
974 pixman_disable_out_of_bounds_workaround ();
976 enable_divbyzero_exceptions();
978 if (getenv ("VERBOSE") != NULL)
981 for (i = 1; i < argc; ++i)
983 if (strcmp (argv[i], "-v") == 0)
989 get_int (argv[i + 1], &mod);
993 else if (strcmp (argv[i], "-s") == 0 && i + 1 < argc)
995 get_int (argv[i + 1], &seed);
999 else if (strcmp (argv[i], "-n") == 0 && i + 1 < argc)
1001 get_int (argv[i + 1], &n_tests);
1006 if (strcmp (argv[i], "-h") != 0)
1007 printf ("Unknown option '%s'\n\n", argv[i]);
1009 printf ("Options:\n\n"
1010 "-n <number> Number of tests to run\n"
1011 "-s <seed> Seed of first test (ignored if PIXMAN_RANDOMIZE_TESTS is set)\n"
1012 "-v Print out seeds\n"
1013 "-v <n> Print out every n'th seed\n\n");
1019 if (getenv ("PIXMAN_RANDOMIZE_TESTS"))
1021 seed = get_random_seed();
1022 printf ("First seed: 0x%08x\n", seed);
1028 # pragma omp parallel for default(none) shared(verbose, n_tests, mod, seed)
1030 for (i = 0; i < (int32_t)n_tests; ++i)
1031 run_test (seed + i, verbose, mod);
1035 for (i = 0; i < (int32_t)n_tests; ++i)
1036 run_test (seed + i, verbose, mod);