hatching.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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) 2011 Intel Corporation
  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. #include <assert.h>
  29. #define STEP 5
  30. #define WIDTH 100
  31. #define HEIGHT 100
  32. static void path (cairo_t *cr, unsigned int width, unsigned int height)
  33. {
  34. unsigned int i;
  35. for (i = 0; i < width+1; i += STEP) {
  36. cairo_rectangle (cr, i-1, -1, 2, height+2);
  37. cairo_rectangle (cr, -1, i-1, width+2, 2);
  38. }
  39. }
  40. static void aa (cairo_t *cr)
  41. {
  42. cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
  43. }
  44. static void mono (cairo_t *cr)
  45. {
  46. cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
  47. }
  48. static void aligned (cairo_t *cr, int width, int height)
  49. {
  50. }
  51. static void misaligned (cairo_t *cr, int width, int height)
  52. {
  53. cairo_translate (cr, 0.25, 0.25);
  54. }
  55. static void rotated (cairo_t *cr, int width, int height)
  56. {
  57. cairo_translate (cr, width/2, height/2);
  58. cairo_rotate (cr, M_PI/4);
  59. cairo_translate (cr, -width/2, -height/2);
  60. }
  61. static void clip (cairo_t *cr)
  62. {
  63. cairo_clip (cr);
  64. cairo_paint (cr);
  65. }
  66. static void clip_alpha (cairo_t *cr)
  67. {
  68. cairo_clip (cr);
  69. cairo_paint_with_alpha (cr, .5);
  70. }
  71. static cairo_time_t
  72. draw (cairo_t *cr,
  73. void (*prepare) (cairo_t *cr),
  74. void (*transform) (cairo_t *cr, int width, int height),
  75. void (*op) (cairo_t *cr),
  76. int width, int height, int loops)
  77. {
  78. cairo_save (cr);
  79. cairo_set_source_rgb (cr, 1, 1, 1);
  80. cairo_paint (cr);
  81. cairo_set_source_rgb (cr, 1, 0, 0);
  82. prepare (cr);
  83. cairo_perf_timer_start ();
  84. while (loops--) {
  85. cairo_save (cr);
  86. transform (cr, width, height);
  87. path (cr, width, height);
  88. op (cr);
  89. cairo_restore (cr);
  90. }
  91. cairo_perf_timer_stop ();
  92. cairo_restore (cr);
  93. return cairo_perf_timer_elapsed ();
  94. }
  95. static cairo_time_t
  96. draw_aligned_aa (cairo_t *cr, int width, int height, int loops)
  97. {
  98. return draw(cr, aa, aligned, cairo_fill,
  99. width, height, loops);
  100. }
  101. static cairo_time_t
  102. draw_misaligned_aa (cairo_t *cr, int width, int height, int loops)
  103. {
  104. return draw(cr, aa, misaligned, cairo_fill,
  105. width, height, loops);
  106. }
  107. static cairo_time_t
  108. draw_rotated_aa (cairo_t *cr, int width, int height, int loops)
  109. {
  110. return draw(cr, aa, rotated, cairo_fill,
  111. width, height, loops);
  112. }
  113. static cairo_time_t
  114. draw_aligned_mono (cairo_t *cr, int width, int height, int loops)
  115. {
  116. return draw(cr, mono, aligned, cairo_fill,
  117. width, height, loops);
  118. }
  119. static cairo_time_t
  120. draw_misaligned_mono (cairo_t *cr, int width, int height, int loops)
  121. {
  122. return draw(cr, mono, misaligned, cairo_fill,
  123. width, height, loops);
  124. }
  125. static cairo_time_t
  126. draw_rotated_mono (cairo_t *cr, int width, int height, int loops)
  127. {
  128. return draw(cr, mono, rotated, cairo_fill,
  129. width, height, loops);
  130. }
  131. #define F(name, op,transform,aa) \
  132. static cairo_time_t \
  133. draw_##name (cairo_t *cr, int width, int height, int loops) \
  134. { return draw(cr, (aa), (transform), (op), width, height, loops); }
  135. F(clip_aligned, clip, aligned, aa)
  136. F(clip_misaligned, clip, misaligned, aa)
  137. F(clip_rotated, clip, rotated, aa)
  138. F(clip_aligned_mono, clip, aligned, mono)
  139. F(clip_misaligned_mono, clip, misaligned, mono)
  140. F(clip_rotated_mono, clip, rotated, mono)
  141. F(clip_alpha_aligned, clip_alpha, aligned, aa)
  142. F(clip_alpha_misaligned, clip_alpha, misaligned, aa)
  143. F(clip_alpha_rotated, clip_alpha, rotated, aa)
  144. F(clip_alpha_aligned_mono, clip_alpha, aligned, mono)
  145. F(clip_alpha_misaligned_mono, clip_alpha, misaligned, mono)
  146. F(clip_alpha_rotated_mono, clip_alpha, rotated, mono)
  147. cairo_bool_t
  148. hatching_enabled (cairo_perf_t *perf)
  149. {
  150. return cairo_perf_can_run (perf, "hatching", NULL);
  151. }
  152. void
  153. hatching (cairo_perf_t *perf, cairo_t *cr, int width, int height)
  154. {
  155. cairo_perf_run (perf, "hatching-aligned-aa", draw_aligned_aa, NULL);
  156. cairo_perf_run (perf, "hatching-misaligned-aa", draw_misaligned_aa, NULL);
  157. cairo_perf_run (perf, "hatching-rotated-aa", draw_rotated_aa, NULL);
  158. cairo_perf_run (perf, "hatching-aligned-mono", draw_aligned_mono, NULL);
  159. cairo_perf_run (perf, "hatching-misaligned-mono", draw_misaligned_mono, NULL);
  160. cairo_perf_run (perf, "hatching-rotated-mono", draw_rotated_mono, NULL);
  161. cairo_perf_run (perf, "hatching-clip-aligned-aa", draw_clip_aligned, NULL);
  162. cairo_perf_run (perf, "hatching-clip-misaligned-aa", draw_clip_misaligned, NULL);
  163. cairo_perf_run (perf, "hatching-clip-rotated-aa", draw_clip_rotated, NULL);
  164. cairo_perf_run (perf, "hatching-clip-aligned-mono", draw_clip_aligned_mono, NULL);
  165. cairo_perf_run (perf, "hatching-clip-misaligned-mono", draw_clip_misaligned_mono, NULL);
  166. cairo_perf_run (perf, "hatching-clip-rotated-mono", draw_clip_rotated_mono, NULL);
  167. cairo_perf_run (perf, "hatching-clip-alpha-aligned-aa", draw_clip_alpha_aligned, NULL);
  168. cairo_perf_run (perf, "hatching-clip-alpha-misaligned-aa", draw_clip_alpha_misaligned, NULL);
  169. cairo_perf_run (perf, "hatching-clip-alpha-rotated-aa", draw_clip_alpha_rotated, NULL);
  170. cairo_perf_run (perf, "hatching-clip-alpha-aligned-mono", draw_clip_alpha_aligned_mono, NULL);
  171. cairo_perf_run (perf, "hatching-clip-alpha-misaligned-mono", draw_clip_alpha_misaligned_mono, NULL);
  172. cairo_perf_run (perf, "hatching-clip-alpha-rotated-mono", draw_clip_alpha_rotated_mono, NULL);
  173. }