wave.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: Benjamin Otte <otte@redhat.com>
  25. */
  26. #include "cairo-perf.h"
  27. static cairo_surface_t *
  28. generate_random_waveform(cairo_t *target, int width, int height)
  29. {
  30. cairo_surface_t *surface;
  31. cairo_t *cr;
  32. int i, r;
  33. srand (0xdeadbeef);
  34. surface = cairo_surface_create_similar (cairo_get_target (target),
  35. CAIRO_CONTENT_ALPHA,
  36. width, height);
  37. cr = cairo_create (surface);
  38. r = height / 2;
  39. for (i = 0; i < width; i++)
  40. {
  41. r += rand () % (height / 4) - (height / 8);
  42. if (r < 0)
  43. r = 0;
  44. else if (r > height)
  45. r = height;
  46. cairo_rectangle (cr, i, (height - r) / 2, 1, r);
  47. }
  48. cairo_fill (cr);
  49. cairo_destroy (cr);
  50. return surface;
  51. }
  52. static cairo_time_t
  53. do_wave (cairo_t *cr, int width, int height, int loops)
  54. {
  55. cairo_surface_t *wave;
  56. cairo_pattern_t *mask;
  57. wave = generate_random_waveform (cr, width, height);
  58. cairo_perf_timer_start ();
  59. while (loops--) {
  60. /* paint outline (and contents) */
  61. cairo_set_source_rgb (cr, 1, 0, 0);
  62. cairo_mask_surface (cr, wave, 0, 0);
  63. /* overdraw inline */
  64. /* first, create a mask */
  65. cairo_push_group_with_content (cr, CAIRO_CONTENT_ALPHA);
  66. cairo_set_source_surface (cr, wave, 0, 0);
  67. cairo_paint (cr);
  68. cairo_set_operator (cr, CAIRO_OPERATOR_IN);
  69. cairo_set_source_surface (cr, wave, 1, 0);
  70. cairo_paint (cr);
  71. cairo_set_source_surface (cr, wave, -1, 0);
  72. cairo_paint (cr);
  73. cairo_set_source_surface (cr, wave, 0, 1);
  74. cairo_paint (cr);
  75. cairo_set_source_surface (cr, wave, 0, -1);
  76. cairo_paint (cr);
  77. mask = cairo_pop_group (cr);
  78. /* second, paint the mask */
  79. cairo_set_source_rgb (cr, 0, 1, 0);
  80. cairo_mask (cr, mask);
  81. cairo_pattern_destroy (mask);
  82. }
  83. cairo_perf_timer_stop ();
  84. cairo_surface_destroy (wave);
  85. return cairo_perf_timer_elapsed ();
  86. }
  87. cairo_bool_t
  88. wave_enabled (cairo_perf_t *perf)
  89. {
  90. return cairo_perf_can_run (perf, "wave", NULL);
  91. }
  92. void
  93. wave (cairo_perf_t *perf, cairo_t *cr, int width, int height)
  94. {
  95. cairo_perf_run (perf, "wave", do_wave, NULL);
  96. }