rounded-rectangles.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright © 2005 Owen Taylor
  3. * Copyright © 2007 Dan Amelang
  4. * Copyright © 2007 Chris Wilson
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software
  7. * and its documentation for any purpose is hereby granted without
  8. * fee, provided that the above copyright notice appear in all copies
  9. * and that both that copyright notice and this permission notice
  10. * appear in supporting documentation, and that the name of
  11. * the authors not be used in advertising or publicity pertaining to
  12. * distribution of the software without specific, written prior
  13. * permission. The authors make no representations about the
  14. * suitability of this software for any purpose. It is provided "as
  15. * is" without express or implied warranty.
  16. *
  17. * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  18. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
  20. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  21. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  22. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  23. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * Authors: Chris Wilson <chris@chris-wilson.co.uk>
  26. */
  27. /* This perf case is derived from the bug report
  28. * Gradient on 'rounded rectangle' MUCH slower than normal rectangle
  29. * https://bugs.freedesktop.org/show_bug.cgi?id=4263.
  30. */
  31. #include "cairo-perf.h"
  32. #define RECTANGLE_COUNT (1000)
  33. #if 0
  34. #define MODE cairo_perf_run
  35. #else
  36. #define MODE cairo_perf_cover_sources_and_operators
  37. #endif
  38. static struct
  39. {
  40. double x;
  41. double y;
  42. double width;
  43. double height;
  44. } rects[RECTANGLE_COUNT];
  45. static void
  46. rounded_rectangle (cairo_t *cr,
  47. double x, double y, double w, double h,
  48. double radius)
  49. {
  50. cairo_move_to (cr, x+radius, y);
  51. cairo_arc (cr, x+w-radius, y+radius, radius, M_PI + M_PI / 2, M_PI * 2 );
  52. cairo_arc (cr, x+w-radius, y+h-radius, radius, 0, M_PI / 2 );
  53. cairo_arc (cr, x+radius, y+h-radius, radius, M_PI/2, M_PI );
  54. cairo_arc (cr, x+radius, y+radius, radius, M_PI, 270 * M_PI / 180);
  55. }
  56. static cairo_time_t
  57. do_rectangle (cairo_t *cr, int width, int height, int loops)
  58. {
  59. cairo_perf_timer_start ();
  60. while (loops--) {
  61. rounded_rectangle (cr, 0, 0, width, height, 3.0);
  62. cairo_fill (cr);
  63. }
  64. cairo_perf_timer_stop ();
  65. return cairo_perf_timer_elapsed ();
  66. }
  67. static cairo_time_t
  68. do_rectangles (cairo_t *cr, int width, int height, int loops)
  69. {
  70. int i;
  71. cairo_perf_timer_start ();
  72. while (loops--) {
  73. for (i = 0; i < RECTANGLE_COUNT; i++) {
  74. rounded_rectangle (cr,
  75. rects[i].x, rects[i].y,
  76. rects[i].width, rects[i].height,
  77. 3.0);
  78. cairo_fill (cr);
  79. }
  80. }
  81. cairo_perf_timer_stop ();
  82. return cairo_perf_timer_elapsed ();
  83. }
  84. static cairo_time_t
  85. do_rectangles_once (cairo_t *cr, int width, int height, int loops)
  86. {
  87. int i;
  88. cairo_perf_timer_start ();
  89. while (loops--) {
  90. for (i = 0; i < RECTANGLE_COUNT; i++) {
  91. rounded_rectangle (cr,
  92. rects[i].x, rects[i].y,
  93. rects[i].width, rects[i].height,
  94. 3.0);
  95. }
  96. cairo_fill (cr);
  97. }
  98. cairo_perf_timer_stop ();
  99. return cairo_perf_timer_elapsed ();
  100. }
  101. cairo_bool_t
  102. rounded_rectangles_enabled (cairo_perf_t *perf)
  103. {
  104. return cairo_perf_can_run (perf, "rounded-rectangles", NULL);
  105. }
  106. void
  107. rounded_rectangles (cairo_perf_t *perf, cairo_t *cr, int width, int height)
  108. {
  109. int i;
  110. srand (8478232);
  111. for (i = 0; i < RECTANGLE_COUNT; i++) {
  112. rects[i].x = rand () % width;
  113. rects[i].y = rand () % height;
  114. rects[i].width = (rand () % (width / 10)) + 1;
  115. rects[i].height = (rand () % (height / 10)) + 1;
  116. }
  117. MODE (perf, "one-rounded-rectangle", do_rectangle, NULL);
  118. MODE (perf, "rounded-rectangles", do_rectangles, NULL);
  119. MODE (perf, "rounded-rectangles-once", do_rectangles_once, NULL);
  120. }