intersections.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
  2. /* cairo - a vector graphics library with display and print output
  3. *
  4. * Copyright (c) 2008 M Joonas Pihlaja
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use,
  10. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "cairo-perf.h"
  28. #define NUM_SEGMENTS 256
  29. static unsigned state;
  30. static double
  31. uniform_random (double minval, double maxval)
  32. {
  33. static unsigned const poly = 0x9a795537U;
  34. unsigned n = 32;
  35. while (n-->0)
  36. state = 2*state < state ? (2*state ^ poly) : 2*state;
  37. return minval + state * (maxval - minval) / 4294967296.0;
  38. }
  39. static cairo_time_t
  40. draw_random (cairo_t *cr, cairo_fill_rule_t fill_rule,
  41. int width, int height, int loops)
  42. {
  43. double x[NUM_SEGMENTS];
  44. double y[NUM_SEGMENTS];
  45. int i;
  46. cairo_save (cr);
  47. cairo_set_source_rgb (cr, 0, 0, 0);
  48. cairo_paint (cr);
  49. for (i = 0; i < NUM_SEGMENTS; i++) {
  50. x[i] = uniform_random (0, width);
  51. y[i] = uniform_random (0, height);
  52. }
  53. state = 0x12345678;
  54. cairo_translate (cr, 1, 1);
  55. cairo_set_fill_rule (cr, fill_rule);
  56. cairo_set_source_rgb (cr, 1, 0, 0);
  57. cairo_new_path (cr);
  58. cairo_move_to (cr, 0, 0);
  59. for (i = 0; i < NUM_SEGMENTS; i++)
  60. cairo_line_to (cr, x[i], y[i]);
  61. cairo_close_path (cr);
  62. cairo_perf_timer_start ();
  63. while (loops--)
  64. cairo_fill_preserve (cr);
  65. cairo_perf_timer_stop ();
  66. cairo_restore (cr);
  67. return cairo_perf_timer_elapsed ();
  68. }
  69. static cairo_time_t
  70. draw_random_curve (cairo_t *cr, cairo_fill_rule_t fill_rule,
  71. int width, int height, int loops)
  72. {
  73. double x[3*NUM_SEGMENTS];
  74. double y[3*NUM_SEGMENTS];
  75. int i;
  76. cairo_save (cr);
  77. cairo_set_source_rgb (cr, 0, 0, 0);
  78. cairo_paint (cr);
  79. for (i = 0; i < 3*NUM_SEGMENTS; i++) {
  80. x[i] = uniform_random (0, width);
  81. y[i] = uniform_random (0, height);
  82. }
  83. state = 0x12345678;
  84. cairo_translate (cr, 1, 1);
  85. cairo_set_fill_rule (cr, fill_rule);
  86. cairo_set_source_rgb (cr, 1, 0, 0);
  87. cairo_new_path (cr);
  88. cairo_move_to (cr, 0, 0);
  89. for (i = 0; i < NUM_SEGMENTS; i++) {
  90. cairo_curve_to (cr,
  91. x[3*i+0], y[3*i+0],
  92. x[3*i+1], y[3*i+1],
  93. x[3*i+2], y[3*i+2]);
  94. }
  95. cairo_close_path (cr);
  96. cairo_perf_timer_start ();
  97. while (loops--)
  98. cairo_fill_preserve (cr);
  99. cairo_perf_timer_stop ();
  100. cairo_restore (cr);
  101. return cairo_perf_timer_elapsed ();
  102. }
  103. static cairo_time_t
  104. random_eo (cairo_t *cr, int width, int height, int loops)
  105. {
  106. return draw_random (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
  107. }
  108. static cairo_time_t
  109. random_nz (cairo_t *cr, int width, int height, int loops)
  110. {
  111. return draw_random (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
  112. }
  113. static cairo_time_t
  114. random_curve_eo (cairo_t *cr, int width, int height, int loops)
  115. {
  116. return draw_random_curve (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
  117. }
  118. static cairo_time_t
  119. random_curve_nz (cairo_t *cr, int width, int height, int loops)
  120. {
  121. return draw_random_curve (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
  122. }
  123. cairo_bool_t
  124. intersections_enabled (cairo_perf_t *perf)
  125. {
  126. return cairo_perf_can_run (perf, "intersections", NULL);
  127. }
  128. void
  129. intersections (cairo_perf_t *perf, cairo_t *cr, int width, int height)
  130. {
  131. cairo_perf_run (perf, "intersections-nz-fill", random_nz, NULL);
  132. cairo_perf_run (perf, "intersections-eo-fill", random_eo, NULL);
  133. cairo_perf_run (perf, "intersections-nz-curve-fill", random_curve_nz, NULL);
  134. cairo_perf_run (perf, "intersections-eo-curve-fill", random_curve_eo, NULL);
  135. }