solid-pattern-cache-stress.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright © 2007 Chris Wilson.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Chris Wilson. Not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Chris Wilson makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Chris Wilson <chris at chris-wilson.co.uk>
  24. */
  25. #if HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "cairo-test.h"
  29. #include <stdlib.h> /* drand48() */
  30. #define LOOPS 10
  31. #define NRAND 100
  32. #ifndef HAVE_DRAND48
  33. #define drand48() (rand () / (double) RAND_MAX)
  34. #endif
  35. static cairo_scaled_font_t *scaled_font;
  36. static cairo_t *
  37. _cairo_create_similar (cairo_t *cr, int width, int height)
  38. {
  39. cairo_surface_t *similar;
  40. similar = cairo_surface_create_similar (cairo_get_target (cr),
  41. cairo_surface_get_content (cairo_get_target (cr)),
  42. width, height);
  43. cr = cairo_create (similar);
  44. cairo_surface_destroy (similar);
  45. return cr;
  46. }
  47. static cairo_t *
  48. _cairo_create_image (cairo_t *cr, cairo_format_t format, int width, int height)
  49. {
  50. cairo_surface_t *image;
  51. image = cairo_image_surface_create (format, width, height);
  52. cr = cairo_create (image);
  53. cairo_surface_destroy (image);
  54. return cr;
  55. }
  56. static void
  57. _propagate_status (cairo_t *dst, cairo_t *src)
  58. {
  59. cairo_path_t path;
  60. path.status = cairo_status (src);
  61. if (path.status) {
  62. path.num_data = 0;
  63. path.data = NULL;
  64. cairo_append_path (dst, &path);
  65. }
  66. }
  67. static void
  68. _draw (cairo_t *cr,
  69. double red,
  70. double green,
  71. double blue)
  72. {
  73. cairo_text_extents_t extents;
  74. cairo_set_source_rgb (cr, red, green, blue);
  75. cairo_paint (cr);
  76. cairo_move_to (cr, 0, 0);
  77. cairo_line_to (cr, 1, 1);
  78. cairo_stroke (cr);
  79. cairo_mask (cr, cairo_get_source (cr));
  80. cairo_set_scaled_font (cr, scaled_font);
  81. cairo_text_extents (cr, "cairo", &extents);
  82. cairo_move_to (cr,
  83. -extents.x_bearing - .5 * extents.width,
  84. -extents.y_bearing - .5 * extents.height);
  85. cairo_show_text (cr, "cairo");
  86. }
  87. static void
  88. use_similar (cairo_t *cr,
  89. double red,
  90. double green,
  91. double blue)
  92. {
  93. cairo_t *cr2;
  94. if (cairo_status (cr))
  95. return;
  96. cr2 = _cairo_create_similar (cr, 1, 1);
  97. _draw (cr2, red, green, blue);
  98. _propagate_status (cr, cr2);
  99. cairo_destroy (cr2);
  100. }
  101. static void
  102. use_image (cairo_t *cr,
  103. cairo_format_t format,
  104. double red,
  105. double green,
  106. double blue)
  107. {
  108. cairo_t *cr2;
  109. if (cairo_status (cr))
  110. return;
  111. cr2 = _cairo_create_image (cr, format, 1, 1);
  112. _draw (cr2, red, green, blue);
  113. _propagate_status (cr, cr2);
  114. cairo_destroy (cr2);
  115. }
  116. static void
  117. use_solid (cairo_t *cr,
  118. double red,
  119. double green,
  120. double blue)
  121. {
  122. /* mix in dissimilar solids */
  123. use_image (cr, CAIRO_FORMAT_A1, red, green, blue);
  124. use_image (cr, CAIRO_FORMAT_A8, red, green, blue);
  125. use_image (cr, CAIRO_FORMAT_RGB24, red, green, blue);
  126. use_image (cr, CAIRO_FORMAT_ARGB32, red, green, blue);
  127. use_similar (cr, red, green, blue);
  128. _draw (cr, red, green, blue);
  129. }
  130. static cairo_test_status_t
  131. draw (cairo_t *cr, int width, int height)
  132. {
  133. const cairo_test_context_t *ctx = cairo_test_get_context (cr);
  134. cairo_status_t status;
  135. const double colors[8][3] = {
  136. { 1.0, 0.0, 0.0 }, /* red */
  137. { 0.0, 1.0, 0.0 }, /* green */
  138. { 1.0, 1.0, 0.0 }, /* yellow */
  139. { 0.0, 0.0, 1.0 }, /* blue */
  140. { 1.0, 0.0, 1.0 }, /* magenta */
  141. { 0.0, 1.0, 1.0 }, /* cyan */
  142. { 1.0, 1.0, 1.0 }, /* white */
  143. { 0.0, 0.0, 0.0 }, /* black */
  144. };
  145. int i, j, loop;
  146. /* cache a resolved scaled-font */
  147. scaled_font = cairo_get_scaled_font (cr);
  148. for (loop = 0; loop < LOOPS; loop++) {
  149. for (i = 0; i < LOOPS; i++) {
  150. for (j = 0; j < 8; j++) {
  151. use_solid (cr, colors[j][0], colors[j][1], colors[j][2]);
  152. status = cairo_status (cr);
  153. if (status)
  154. return cairo_test_status_from_status (ctx, status);
  155. }
  156. }
  157. for (i = 0; i < NRAND; i++) {
  158. use_solid (cr, drand48 (), drand48 (), drand48 ());
  159. status = cairo_status (cr);
  160. if (status)
  161. return cairo_test_status_from_status (ctx, status);
  162. }
  163. }
  164. /* stress test only, so clear the surface before comparing */
  165. cairo_set_source_rgb (cr, 0, 0, 1);
  166. cairo_paint (cr);
  167. return CAIRO_TEST_SUCCESS;
  168. }
  169. CAIRO_TEST (solid_pattern_cache_stress,
  170. "Stress the solid pattern cache and ensure it behaves",
  171. "stress", /* keywords */
  172. NULL, /* requirements */
  173. 1, 1,
  174. NULL, draw)