rectangles.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright © 2006 Dan Amelang
  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. * the authors not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. The authors make 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. * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL THE AUTHORS 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. * Authors: Dan Amelang <dan@amelang.net>
  24. */
  25. #include "cairo-perf.h"
  26. #if 0
  27. #define MODE cairo_perf_run
  28. #else
  29. #define MODE cairo_perf_cover_sources_and_operators
  30. #endif
  31. #define RECTANGLE_COUNT (1000)
  32. static struct {
  33. double x;
  34. double y;
  35. double width;
  36. double height;
  37. } rects[RECTANGLE_COUNT];
  38. static cairo_time_t
  39. do_rectangles (cairo_t *cr, int width, int height, int loops)
  40. {
  41. int i;
  42. cairo_perf_timer_start ();
  43. while (loops--) {
  44. for (i = 0; i < RECTANGLE_COUNT; i++) {
  45. cairo_rectangle (cr, rects[i].x, rects[i].y,
  46. rects[i].width, rects[i].height);
  47. cairo_fill (cr);
  48. }
  49. }
  50. cairo_perf_timer_stop ();
  51. return cairo_perf_timer_elapsed ();
  52. }
  53. static cairo_time_t
  54. do_rectangles_once (cairo_t *cr, int width, int height, int loops)
  55. {
  56. int i;
  57. cairo_perf_timer_start ();
  58. while (loops--) {
  59. for (i = 0; i < RECTANGLE_COUNT; i++) {
  60. cairo_rectangle (cr, rects[i].x, rects[i].y,
  61. rects[i].width, rects[i].height);
  62. }
  63. cairo_fill (cr);
  64. }
  65. cairo_perf_timer_stop ();
  66. return cairo_perf_timer_elapsed ();
  67. }
  68. static cairo_time_t
  69. do_rectangle (cairo_t *cr, int width, int height, int loops)
  70. {
  71. cairo_perf_timer_start ();
  72. while (loops--) {
  73. cairo_rectangle (cr, 0, 0, width, height);
  74. cairo_fill (cr);
  75. }
  76. cairo_perf_timer_stop ();
  77. return cairo_perf_timer_elapsed ();
  78. }
  79. cairo_bool_t
  80. rectangles_enabled (cairo_perf_t *perf)
  81. {
  82. return cairo_perf_can_run (perf, "rectangles", NULL);
  83. }
  84. void
  85. rectangles (cairo_perf_t *perf, cairo_t *cr, int width, int height)
  86. {
  87. int i;
  88. srand (8478232);
  89. for (i = 0; i < RECTANGLE_COUNT; i++)
  90. {
  91. rects[i].x = rand () % width;
  92. rects[i].y = rand () % height;
  93. rects[i].width = (rand () % (width / 10)) + 1;
  94. rects[i].height = (rand () % (height / 10)) + 1;
  95. }
  96. MODE (perf, "one-rectangle", do_rectangle, NULL);
  97. MODE (perf, "rectangles", do_rectangles, NULL);
  98. MODE (perf, "rectangles-once", do_rectangles_once, NULL);
  99. }