solid-test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright © 2015 RISC OS Open Ltd
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that
  7. * copyright notice and this permission notice appear in supporting
  8. * documentation, and that the name of the copyright holders not be used in
  9. * advertising or publicity pertaining to distribution of the software without
  10. * specific, written prior permission. The copyright holders make no
  11. * representations about the suitability of this software for any purpose. It
  12. * is provided "as is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  15. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
  17. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  19. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  20. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. * SOFTWARE.
  22. *
  23. * Author: Ben Avison (bavison@riscosopen.org)
  24. *
  25. */
  26. #include "utils.h"
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #define WIDTH 32
  30. #define HEIGHT 32
  31. static const pixman_op_t op_list[] = {
  32. PIXMAN_OP_SRC,
  33. PIXMAN_OP_OVER,
  34. PIXMAN_OP_ADD,
  35. PIXMAN_OP_CLEAR,
  36. PIXMAN_OP_SRC,
  37. PIXMAN_OP_DST,
  38. PIXMAN_OP_OVER,
  39. PIXMAN_OP_OVER_REVERSE,
  40. PIXMAN_OP_IN,
  41. PIXMAN_OP_IN_REVERSE,
  42. PIXMAN_OP_OUT,
  43. PIXMAN_OP_OUT_REVERSE,
  44. PIXMAN_OP_ATOP,
  45. PIXMAN_OP_ATOP_REVERSE,
  46. PIXMAN_OP_XOR,
  47. PIXMAN_OP_ADD,
  48. PIXMAN_OP_MULTIPLY,
  49. PIXMAN_OP_SCREEN,
  50. PIXMAN_OP_OVERLAY,
  51. PIXMAN_OP_DARKEN,
  52. PIXMAN_OP_LIGHTEN,
  53. PIXMAN_OP_HARD_LIGHT,
  54. PIXMAN_OP_DIFFERENCE,
  55. PIXMAN_OP_EXCLUSION,
  56. #if 0 /* these use floating point math and are not always bitexact on different platforms */
  57. PIXMAN_OP_SATURATE,
  58. PIXMAN_OP_DISJOINT_CLEAR,
  59. PIXMAN_OP_DISJOINT_SRC,
  60. PIXMAN_OP_DISJOINT_DST,
  61. PIXMAN_OP_DISJOINT_OVER,
  62. PIXMAN_OP_DISJOINT_OVER_REVERSE,
  63. PIXMAN_OP_DISJOINT_IN,
  64. PIXMAN_OP_DISJOINT_IN_REVERSE,
  65. PIXMAN_OP_DISJOINT_OUT,
  66. PIXMAN_OP_DISJOINT_OUT_REVERSE,
  67. PIXMAN_OP_DISJOINT_ATOP,
  68. PIXMAN_OP_DISJOINT_ATOP_REVERSE,
  69. PIXMAN_OP_DISJOINT_XOR,
  70. PIXMAN_OP_CONJOINT_CLEAR,
  71. PIXMAN_OP_CONJOINT_SRC,
  72. PIXMAN_OP_CONJOINT_DST,
  73. PIXMAN_OP_CONJOINT_OVER,
  74. PIXMAN_OP_CONJOINT_OVER_REVERSE,
  75. PIXMAN_OP_CONJOINT_IN,
  76. PIXMAN_OP_CONJOINT_IN_REVERSE,
  77. PIXMAN_OP_CONJOINT_OUT,
  78. PIXMAN_OP_CONJOINT_OUT_REVERSE,
  79. PIXMAN_OP_CONJOINT_ATOP,
  80. PIXMAN_OP_CONJOINT_ATOP_REVERSE,
  81. PIXMAN_OP_CONJOINT_XOR,
  82. PIXMAN_OP_COLOR_DODGE,
  83. PIXMAN_OP_COLOR_BURN,
  84. PIXMAN_OP_SOFT_LIGHT,
  85. PIXMAN_OP_HSL_HUE,
  86. PIXMAN_OP_HSL_SATURATION,
  87. PIXMAN_OP_HSL_COLOR,
  88. PIXMAN_OP_HSL_LUMINOSITY,
  89. #endif
  90. };
  91. /* The first eight format in the list are by far the most widely
  92. * used formats, so we test those more than the others
  93. */
  94. #define N_MOST_LIKELY_FORMATS 8
  95. static const pixman_format_code_t img_fmt_list[] = {
  96. PIXMAN_a8r8g8b8,
  97. PIXMAN_a8b8g8r8,
  98. PIXMAN_x8r8g8b8,
  99. PIXMAN_x8b8g8r8,
  100. PIXMAN_r5g6b5,
  101. PIXMAN_b5g6r5,
  102. PIXMAN_a8,
  103. PIXMAN_a1,
  104. PIXMAN_r3g3b2,
  105. PIXMAN_b8g8r8a8,
  106. PIXMAN_b8g8r8x8,
  107. PIXMAN_r8g8b8a8,
  108. PIXMAN_r8g8b8x8,
  109. PIXMAN_x14r6g6b6,
  110. PIXMAN_r8g8b8,
  111. PIXMAN_b8g8r8,
  112. #if 0 /* These are going to use floating point in the near future */
  113. PIXMAN_x2r10g10b10,
  114. PIXMAN_a2r10g10b10,
  115. PIXMAN_x2b10g10r10,
  116. PIXMAN_a2b10g10r10,
  117. #endif
  118. PIXMAN_a1r5g5b5,
  119. PIXMAN_x1r5g5b5,
  120. PIXMAN_a1b5g5r5,
  121. PIXMAN_x1b5g5r5,
  122. PIXMAN_a4r4g4b4,
  123. PIXMAN_x4r4g4b4,
  124. PIXMAN_a4b4g4r4,
  125. PIXMAN_x4b4g4r4,
  126. PIXMAN_r3g3b2,
  127. PIXMAN_b2g3r3,
  128. PIXMAN_a2r2g2b2,
  129. PIXMAN_a2b2g2r2,
  130. PIXMAN_c8,
  131. PIXMAN_g8,
  132. PIXMAN_x4c4,
  133. PIXMAN_x4g4,
  134. PIXMAN_c4,
  135. PIXMAN_g4,
  136. PIXMAN_g1,
  137. PIXMAN_x4a4,
  138. PIXMAN_a4,
  139. PIXMAN_r1g2b1,
  140. PIXMAN_b1g2r1,
  141. PIXMAN_a1r1g1b1,
  142. PIXMAN_a1b1g1r1,
  143. PIXMAN_null
  144. };
  145. static const pixman_format_code_t mask_fmt_list[] = {
  146. PIXMAN_a8r8g8b8,
  147. PIXMAN_a8,
  148. PIXMAN_a4,
  149. PIXMAN_a1,
  150. PIXMAN_null
  151. };
  152. static pixman_indexed_t rgb_palette[9];
  153. static pixman_indexed_t y_palette[9];
  154. static pixman_format_code_t
  155. random_format (const pixman_format_code_t *allowed_formats)
  156. {
  157. int n = 0;
  158. while (allowed_formats[n] != PIXMAN_null)
  159. n++;
  160. if (n > N_MOST_LIKELY_FORMATS && prng_rand_n (4) != 0)
  161. n = N_MOST_LIKELY_FORMATS;
  162. return allowed_formats[prng_rand_n (n)];
  163. }
  164. static pixman_image_t *
  165. create_multi_pixel_image (const pixman_format_code_t *allowed_formats,
  166. uint32_t *buffer,
  167. pixman_format_code_t *used_fmt)
  168. {
  169. pixman_format_code_t fmt;
  170. pixman_image_t *img;
  171. int stride;
  172. fmt = random_format (allowed_formats);
  173. stride = (WIDTH * PIXMAN_FORMAT_BPP (fmt) + 31) / 32 * 4;
  174. img = pixman_image_create_bits (fmt, WIDTH, HEIGHT, buffer, stride);
  175. if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
  176. pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)]));
  177. else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
  178. pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)]));
  179. prng_randmemset (buffer, WIDTH * HEIGHT * 4, 0);
  180. image_endian_swap (img);
  181. if (used_fmt)
  182. *used_fmt = fmt;
  183. return img;
  184. }
  185. static pixman_image_t *
  186. create_solid_image (const pixman_format_code_t *allowed_formats,
  187. uint32_t *buffer,
  188. pixman_format_code_t *used_fmt)
  189. {
  190. if (prng_rand_n (2))
  191. {
  192. /* Use a repeating 1x1 bitmap image for solid */
  193. pixman_format_code_t fmt;
  194. pixman_image_t *img, *dummy_img;
  195. uint32_t bpp, dummy_buf;
  196. fmt = random_format (allowed_formats);
  197. bpp = PIXMAN_FORMAT_BPP (fmt);
  198. img = pixman_image_create_bits (fmt, 1, 1, buffer, 4);
  199. pixman_image_set_repeat (img, PIXMAN_REPEAT_NORMAL);
  200. if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
  201. pixman_image_set_indexed (img, &(rgb_palette[bpp]));
  202. else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
  203. pixman_image_set_indexed (img, &(y_palette[bpp]));
  204. /* Force the flags to be calculated for image with initial
  205. * bitmap contents of 0 or 2^bpp-1 by plotting from it into a
  206. * separate throwaway image. It is simplest to write all 0s
  207. * or all 1s to the first word irrespective of the colour
  208. * depth even though we actually only care about the first
  209. * pixel since the stride has to be a whole number of words.
  210. */
  211. *buffer = prng_rand_n (2) ? 0xFFFFFFFFu : 0;
  212. dummy_img = pixman_image_create_bits (PIXMAN_a8r8g8b8, 1, 1,
  213. &dummy_buf, 4);
  214. pixman_image_composite (PIXMAN_OP_SRC, img, NULL, dummy_img,
  215. 0, 0, 0, 0, 0, 0, 1, 1);
  216. pixman_image_unref (dummy_img);
  217. /* Now set the bitmap contents to a random value */
  218. prng_randmemset (buffer, 4, 0);
  219. image_endian_swap (img);
  220. if (used_fmt)
  221. *used_fmt = fmt;
  222. return img;
  223. }
  224. else
  225. {
  226. /* Use a native solid image */
  227. pixman_color_t color;
  228. pixman_image_t *img;
  229. color.alpha = prng_rand_n (UINT16_MAX + 1);
  230. color.red = prng_rand_n (UINT16_MAX + 1);
  231. color.green = prng_rand_n (UINT16_MAX + 1);
  232. color.blue = prng_rand_n (UINT16_MAX + 1);
  233. img = pixman_image_create_solid_fill (&color);
  234. if (used_fmt)
  235. *used_fmt = PIXMAN_solid;
  236. return img;
  237. }
  238. }
  239. static uint32_t
  240. test_solid (int testnum, int verbose)
  241. {
  242. pixman_op_t op;
  243. uint32_t src_buf[WIDTH * HEIGHT];
  244. uint32_t dst_buf[WIDTH * HEIGHT];
  245. uint32_t mask_buf[WIDTH * HEIGHT];
  246. pixman_image_t *src_img;
  247. pixman_image_t *dst_img;
  248. pixman_image_t *mask_img = NULL;
  249. pixman_format_code_t src_fmt, dst_fmt, mask_fmt = PIXMAN_null;
  250. pixman_bool_t ca = 0;
  251. uint32_t crc32;
  252. prng_srand (testnum);
  253. op = op_list[prng_rand_n (ARRAY_LENGTH (op_list))];
  254. dst_img = create_multi_pixel_image (img_fmt_list, dst_buf, &dst_fmt);
  255. switch (prng_rand_n (3))
  256. {
  257. case 0: /* Solid source, no mask */
  258. src_img = create_solid_image (img_fmt_list, src_buf, &src_fmt);
  259. break;
  260. case 1: /* Solid source, bitmap mask */
  261. src_img = create_solid_image (img_fmt_list, src_buf, &src_fmt);
  262. mask_img = create_multi_pixel_image (mask_fmt_list, mask_buf, &mask_fmt);
  263. break;
  264. case 2: /* Bitmap image, solid mask */
  265. src_img = create_multi_pixel_image (img_fmt_list, src_buf, &src_fmt);
  266. mask_img = create_solid_image (mask_fmt_list, mask_buf, &mask_fmt);
  267. break;
  268. default:
  269. abort ();
  270. }
  271. if (mask_img)
  272. {
  273. ca = prng_rand_n (2);
  274. pixman_image_set_component_alpha (mask_img, ca);
  275. }
  276. if (verbose)
  277. {
  278. printf ("op=%s\n", operator_name (op));
  279. printf ("src_fmt=%s, dst_fmt=%s, mask_fmt=%s\n",
  280. format_name (src_fmt), format_name (dst_fmt),
  281. format_name (mask_fmt));
  282. printf ("src_size=%u, mask_size=%u, component_alpha=%u\n",
  283. src_fmt == PIXMAN_solid ? 1 : src_img->bits.width,
  284. !mask_img || mask_fmt == PIXMAN_solid ? 1 : mask_img->bits.width,
  285. ca);
  286. }
  287. pixman_image_composite (op, src_img, mask_img, dst_img,
  288. 0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
  289. if (verbose)
  290. print_image (dst_img);
  291. crc32 = compute_crc32_for_image (0, dst_img);
  292. pixman_image_unref (src_img);
  293. pixman_image_unref (dst_img);
  294. if (mask_img)
  295. pixman_image_unref (mask_img);
  296. return crc32;
  297. }
  298. int
  299. main (int argc, const char *argv[])
  300. {
  301. int i;
  302. prng_srand (0);
  303. for (i = 1; i <= 8; i++)
  304. {
  305. initialize_palette (&(rgb_palette[i]), i, TRUE);
  306. initialize_palette (&(y_palette[i]), i, FALSE);
  307. }
  308. return fuzzer_test_main ("solid", 500000,
  309. 0xC30FD380,
  310. test_solid, argc, argv);
  311. }