coverage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: Chris Wilson <chris@chris-wilson.co.uk>
  25. */
  26. #include "cairo-test.h"
  27. /* Test the fidelity of the rasterisation, because Cairo is my favourite
  28. * driver test suite.
  29. */
  30. #define GENERATE_REFERENCE 0
  31. #define WIDTH 256
  32. #define HEIGHT 40
  33. #include "../src/cairo-fixed-type-private.h"
  34. #define PRECISION (1 << CAIRO_FIXED_FRAC_BITS)
  35. /* XXX beware multithreading! */
  36. static uint32_t state;
  37. static uint32_t
  38. hars_petruska_f54_1_random (void)
  39. {
  40. #define rol(x,k) ((x << k) | (x >> (32-k)))
  41. return state = (state ^ rol (state, 5) ^ rol (state, 24)) + 0x37798849;
  42. #undef rol
  43. }
  44. static double
  45. random_offset (int range, int precise)
  46. {
  47. double x = hars_petruska_f54_1_random() / (double) UINT32_MAX * range / WIDTH;
  48. if (precise)
  49. x = floor (x * PRECISION) / PRECISION;
  50. return x;
  51. }
  52. static cairo_test_status_t
  53. rectangles (cairo_t *cr, int width, int height)
  54. {
  55. int x, y, channel;
  56. state = 0x12345678;
  57. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  58. cairo_paint (cr);
  59. #if GENERATE_REFERENCE
  60. for (x = 0; x < WIDTH; x++) {
  61. cairo_set_source_rgba (cr, 1, 1, 1, x * x * 1.0 / (WIDTH * WIDTH));
  62. cairo_rectangle (cr, x, 0, 1, HEIGHT);
  63. cairo_fill (cr);
  64. }
  65. #else
  66. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  67. for (channel = 0; channel < 3; channel++) {
  68. switch (channel) {
  69. default:
  70. case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
  71. case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
  72. case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
  73. }
  74. for (x = 0; x < WIDTH; x++) {
  75. for (y = 0; y < HEIGHT; y++) {
  76. double dx = random_offset (WIDTH - x, TRUE);
  77. double dy = random_offset (WIDTH - x, TRUE);
  78. cairo_rectangle (cr, x + dx, y + dy, x / (double) WIDTH, x / (double) WIDTH);
  79. }
  80. }
  81. cairo_fill (cr);
  82. }
  83. #endif
  84. return CAIRO_TEST_SUCCESS;
  85. }
  86. static cairo_test_status_t
  87. rhombus (cairo_t *cr, int width, int height)
  88. {
  89. int x, y;
  90. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  91. cairo_paint (cr);
  92. #if GENERATE_REFERENCE
  93. for (y = 0; y < WIDTH; y++) {
  94. for (x = 0; x < WIDTH; x++) {
  95. cairo_set_source_rgba (cr, 1, 1, 1,
  96. x * y / (2. * WIDTH * WIDTH));
  97. cairo_rectangle (cr, 2*x, 2*y, 2, 2);
  98. cairo_fill (cr);
  99. }
  100. }
  101. #else
  102. cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  103. cairo_set_source_rgb (cr, 1, 1, 1);
  104. for (y = 0; y < WIDTH; y++) {
  105. double yf = y / (double) WIDTH;
  106. for (x = 0; x < WIDTH; x++) {
  107. double xf = x / (double) WIDTH;
  108. cairo_move_to (cr,
  109. 2*x + 1 - xf,
  110. 2*y + 1);
  111. cairo_line_to (cr,
  112. 2*x + 1,
  113. 2*y + 1 - yf);
  114. cairo_line_to (cr,
  115. 2*x + 1 + xf,
  116. 2*y + 1);
  117. cairo_line_to (cr,
  118. 2*x + 1,
  119. 2*y + 1 + yf);
  120. cairo_close_path (cr);
  121. }
  122. }
  123. cairo_fill (cr);
  124. #endif
  125. return CAIRO_TEST_SUCCESS;
  126. }
  127. static cairo_test_status_t
  128. intersecting_quads (cairo_t *cr, int width, int height)
  129. {
  130. int x, y, channel;
  131. state = 0x12345678;
  132. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  133. cairo_paint (cr);
  134. #if GENERATE_REFERENCE
  135. for (x = 0; x < WIDTH; x++) {
  136. cairo_set_source_rgba (cr, 1, 1, 1, x * x * 0.5 / (WIDTH * WIDTH));
  137. cairo_rectangle (cr, x, 0, 1, HEIGHT);
  138. cairo_fill (cr);
  139. }
  140. #else
  141. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  142. for (channel = 0; channel < 3; channel++) {
  143. switch (channel) {
  144. default:
  145. case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
  146. case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
  147. case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
  148. }
  149. for (x = 0; x < WIDTH; x++) {
  150. double step = x / (double) WIDTH;
  151. for (y = 0; y < HEIGHT; y++) {
  152. double dx = random_offset (WIDTH - x, TRUE);
  153. double dy = random_offset (WIDTH - x, TRUE);
  154. cairo_move_to (cr, x + dx, y + dy);
  155. cairo_rel_line_to (cr, step, step);
  156. cairo_rel_line_to (cr, 0, -step);
  157. cairo_rel_line_to (cr, -step, step);
  158. cairo_close_path (cr);
  159. }
  160. }
  161. cairo_fill (cr);
  162. }
  163. #endif
  164. return CAIRO_TEST_SUCCESS;
  165. }
  166. static cairo_test_status_t
  167. intersecting_triangles (cairo_t *cr, int width, int height)
  168. {
  169. int x, y, channel;
  170. state = 0x12345678;
  171. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  172. cairo_paint (cr);
  173. #if GENERATE_REFERENCE
  174. for (x = 0; x < WIDTH; x++) {
  175. cairo_set_source_rgba (cr, 1, 1, 1, x * x * 0.75 / (WIDTH * WIDTH));
  176. cairo_rectangle (cr, x, 0, 1, HEIGHT);
  177. cairo_fill (cr);
  178. }
  179. #else
  180. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  181. for (channel = 0; channel < 3; channel++) {
  182. switch (channel) {
  183. default:
  184. case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
  185. case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
  186. case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
  187. }
  188. for (x = 0; x < WIDTH; x++) {
  189. double step = x / (double) WIDTH;
  190. for (y = 0; y < HEIGHT; y++) {
  191. double dx = random_offset (WIDTH - x, TRUE);
  192. double dy = random_offset (WIDTH - x, TRUE);
  193. /* left */
  194. cairo_move_to (cr, x + dx, y + dy);
  195. cairo_rel_line_to (cr, 0, step);
  196. cairo_rel_line_to (cr, step, 0);
  197. cairo_close_path (cr);
  198. /* right, mirrored */
  199. cairo_move_to (cr, x + dx + step, y + dy + step);
  200. cairo_rel_line_to (cr, 0, -step);
  201. cairo_rel_line_to (cr, -step, step);
  202. cairo_close_path (cr);
  203. }
  204. }
  205. cairo_fill (cr);
  206. }
  207. #endif
  208. return CAIRO_TEST_SUCCESS;
  209. }
  210. static cairo_test_status_t
  211. triangles (cairo_t *cr, int width, int height)
  212. {
  213. int x, y, channel;
  214. state = 0x12345678;
  215. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  216. cairo_paint (cr);
  217. #if GENERATE_REFERENCE
  218. for (x = 0; x < WIDTH; x++) {
  219. cairo_set_source_rgba (cr, 1, 1, 1, x * x * 0.5 / (WIDTH * WIDTH));
  220. cairo_rectangle (cr, x, 0, 1, HEIGHT);
  221. cairo_fill (cr);
  222. }
  223. #else
  224. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  225. for (channel = 0; channel < 3; channel++) {
  226. switch (channel) {
  227. default:
  228. case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
  229. case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
  230. case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
  231. }
  232. for (x = 0; x < WIDTH; x++) {
  233. for (y = 0; y < HEIGHT; y++) {
  234. double dx = random_offset (WIDTH - x, TRUE);
  235. double dy = random_offset (WIDTH - x, TRUE);
  236. cairo_move_to (cr, x + dx, y + dy);
  237. cairo_rel_line_to (cr, x / (double) WIDTH, 0);
  238. cairo_rel_line_to (cr, 0, x / (double) WIDTH);
  239. cairo_close_path (cr);
  240. }
  241. }
  242. cairo_fill (cr);
  243. }
  244. #endif
  245. return CAIRO_TEST_SUCCESS;
  246. }
  247. static cairo_test_status_t
  248. abutting (cairo_t *cr, int width, int height)
  249. {
  250. int x, y;
  251. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  252. cairo_paint (cr);
  253. cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.75);
  254. #if GENERATE_REFERENCE
  255. cairo_paint (cr);
  256. #else
  257. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  258. for (y = 0; y < 16; y++) {
  259. for (x = 0; x < 16; x++) {
  260. double theta = (y * 16 + x) * M_PI / 512;
  261. double cx = 16 * cos (theta) + x * 16;
  262. double cy = 16 * sin (theta) + y * 16;
  263. cairo_move_to (cr, x * 16, y * 16);
  264. cairo_line_to (cr, cx, cy);
  265. cairo_line_to (cr, (x + 1) * 16, y * 16);
  266. cairo_fill (cr);
  267. cairo_move_to (cr, (x + 1) * 16, y * 16);
  268. cairo_line_to (cr, cx, cy);
  269. cairo_line_to (cr, (x + 1) * 16, (y + 1) * 16);
  270. cairo_fill (cr);
  271. cairo_move_to (cr, (x + 1) * 16, (y + 1) * 16);
  272. cairo_line_to (cr, cx, cy);
  273. cairo_line_to (cr, x * 16, (y + 1) * 16);
  274. cairo_fill (cr);
  275. cairo_move_to (cr, x * 16, (y + 1) * 16);
  276. cairo_line_to (cr, cx, cy);
  277. cairo_line_to (cr, x * 16, y * 16);
  278. cairo_fill (cr);
  279. }
  280. }
  281. #endif
  282. return CAIRO_TEST_SUCCESS;
  283. }
  284. static cairo_test_status_t
  285. column_triangles (cairo_t *cr, int width, int height)
  286. {
  287. int x, y, i, channel;
  288. state = 0x12345678;
  289. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  290. cairo_paint (cr);
  291. #if GENERATE_REFERENCE
  292. for (x = 0; x < WIDTH; x++) {
  293. cairo_set_source_rgba (cr, 1, 1, 1, x * 0.5 / WIDTH);
  294. cairo_rectangle (cr, x, 0, 1, HEIGHT);
  295. cairo_fill (cr);
  296. }
  297. #else
  298. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  299. for (channel = 0; channel < 3; channel++) {
  300. switch (channel) {
  301. default:
  302. case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
  303. case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
  304. case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
  305. }
  306. for (x = 0; x < WIDTH; x++) {
  307. double step = x / (double) (2 * WIDTH);
  308. for (y = 0; y < HEIGHT; y++) {
  309. for (i = 0; i < PRECISION; i++) {
  310. double dy = random_offset (WIDTH - x, FALSE);
  311. /*
  312. * We want to test some sharing of edges to further
  313. * stress the rasterisers, so instead of using one
  314. * tall triangle, it is split into two, with vertical
  315. * edges on either side that may co-align with their
  316. * neighbours:
  317. *
  318. * s --- . ---
  319. * t | |\ |
  320. * e | | \ |
  321. * p --- .... | 2 * step = x / WIDTH
  322. * \ | |
  323. * \| |
  324. * . ---
  325. * |---|
  326. * 1 / PRECISION
  327. *
  328. * Each column contains two triangles of width one quantum and
  329. * total height of (x / WIDTH), thus the total area covered by all
  330. * columns in each pixel is .5 * (x / WIDTH).
  331. */
  332. cairo_move_to (cr, x + i / (double) PRECISION, y + dy);
  333. cairo_rel_line_to (cr, 0, step);
  334. cairo_rel_line_to (cr, 1 / (double) PRECISION, step);
  335. cairo_rel_line_to (cr, 0, -step);
  336. cairo_close_path (cr);
  337. }
  338. cairo_fill (cr); /* do these per-pixel due to the extra volume of edges */
  339. }
  340. }
  341. }
  342. #endif
  343. return CAIRO_TEST_SUCCESS;
  344. }
  345. static cairo_test_status_t
  346. row_triangles (cairo_t *cr, int width, int height)
  347. {
  348. int x, y, i, channel;
  349. state = 0x12345678;
  350. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  351. cairo_paint (cr);
  352. #if GENERATE_REFERENCE
  353. for (x = 0; x < WIDTH; x++) {
  354. cairo_set_source_rgba (cr, 1, 1, 1, x * 0.5 / WIDTH);
  355. cairo_rectangle (cr, x, 0, 1, HEIGHT);
  356. cairo_fill (cr);
  357. }
  358. #else
  359. cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  360. for (channel = 0; channel < 3; channel++) {
  361. switch (channel) {
  362. default:
  363. case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
  364. case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
  365. case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
  366. }
  367. for (x = 0; x < WIDTH; x++) {
  368. double step = x / (double) (2 * WIDTH);
  369. for (y = 0; y < HEIGHT; y++) {
  370. for (i = 0; i < PRECISION; i++) {
  371. double dx = random_offset (WIDTH - x, FALSE);
  372. /* See column_triangles() for a transposed description
  373. * of this geometry.
  374. */
  375. cairo_move_to (cr, x + dx, y + i / (double) PRECISION);
  376. cairo_rel_line_to (cr, step, 0);
  377. cairo_rel_line_to (cr, step, 1 / (double) PRECISION);
  378. cairo_rel_line_to (cr, -step, 0);
  379. cairo_close_path (cr);
  380. }
  381. cairo_fill (cr); /* do these per-pixel due to the extra volume of edges */
  382. }
  383. }
  384. }
  385. #endif
  386. return CAIRO_TEST_SUCCESS;
  387. }
  388. CAIRO_TEST (coverage_rectangles,
  389. "Check the fidelity of the rasterisation.",
  390. NULL, /* keywords */
  391. "target=raster", /* requirements */
  392. WIDTH, HEIGHT,
  393. NULL, rectangles)
  394. CAIRO_TEST (coverage_rhombus,
  395. "Check the fidelity of the rasterisation.",
  396. NULL, /* keywords */
  397. "target=raster", /* requirements */
  398. 2*WIDTH, 2*WIDTH,
  399. NULL, rhombus)
  400. CAIRO_TEST (coverage_intersecting_quads,
  401. "Check the fidelity of the rasterisation.",
  402. NULL, /* keywords */
  403. "target=raster", /* requirements */
  404. WIDTH, HEIGHT,
  405. NULL, intersecting_quads)
  406. CAIRO_TEST (coverage_intersecting_triangles,
  407. "Check the fidelity of the rasterisation.",
  408. NULL, /* keywords */
  409. "target=raster", /* requirements */
  410. WIDTH, HEIGHT,
  411. NULL, intersecting_triangles)
  412. CAIRO_TEST (coverage_row_triangles,
  413. "Check the fidelity of the rasterisation.",
  414. NULL, /* keywords */
  415. "target=raster", /* requirements */
  416. WIDTH, HEIGHT,
  417. NULL, row_triangles)
  418. CAIRO_TEST (coverage_column_triangles,
  419. "Check the fidelity of the rasterisation.",
  420. NULL, /* keywords */
  421. "target=raster", /* requirements */
  422. WIDTH, HEIGHT,
  423. NULL, column_triangles)
  424. CAIRO_TEST (coverage_triangles,
  425. "Check the fidelity of the rasterisation.",
  426. NULL, /* keywords */
  427. "target=raster", /* requirements */
  428. WIDTH, HEIGHT,
  429. NULL, triangles)
  430. CAIRO_TEST (coverage_abutting,
  431. "Check the fidelity of the rasterisation.",
  432. NULL, /* keywords */
  433. "target=raster", /* requirements */
  434. 16*16, 16*16,
  435. NULL, abutting)