zero-mask.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright © 2010 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@gnome.org>
  25. */
  26. #include "cairo-test.h"
  27. #define RECT 10
  28. #define SPACE 5
  29. static void
  30. paint_with_alpha (cairo_t *cr)
  31. {
  32. cairo_paint_with_alpha (cr, 0.0);
  33. }
  34. static void
  35. mask_with_solid (cairo_t *cr)
  36. {
  37. cairo_pattern_t *pattern = cairo_pattern_create_rgba (1, 0, 0, 0);
  38. cairo_mask (cr, pattern);
  39. cairo_pattern_destroy (pattern);
  40. }
  41. static void
  42. mask_with_empty_gradient (cairo_t *cr)
  43. {
  44. cairo_pattern_t *pattern = cairo_pattern_create_linear (1, 2, 3, 4);
  45. cairo_mask (cr, pattern);
  46. cairo_pattern_destroy (pattern);
  47. }
  48. static void
  49. mask_with_gradient (cairo_t *cr)
  50. {
  51. cairo_pattern_t *pattern = cairo_pattern_create_radial (1, 2, 3, 4, 5, 6);
  52. cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 0, 0, 0);
  53. cairo_pattern_add_color_stop_rgba (pattern, 0, 0, 0, 1, 0);
  54. cairo_mask (cr, pattern);
  55. cairo_pattern_destroy (pattern);
  56. }
  57. static void
  58. mask_with_surface (cairo_t *cr)
  59. {
  60. cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
  61. CAIRO_CONTENT_COLOR_ALPHA,
  62. RECT,
  63. RECT);
  64. cairo_mask_surface (cr, surface, 0, 0);
  65. cairo_surface_destroy (surface);
  66. }
  67. static void
  68. mask_with_alpha_surface (cairo_t *cr)
  69. {
  70. cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
  71. CAIRO_CONTENT_ALPHA,
  72. RECT / 2,
  73. RECT / 2);
  74. cairo_pattern_t *pattern = cairo_pattern_create_for_surface (surface);
  75. cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REFLECT);
  76. cairo_mask (cr, pattern);
  77. cairo_pattern_destroy (pattern);
  78. cairo_surface_destroy (surface);
  79. }
  80. static void
  81. mask_with_nonclear_surface (cairo_t *cr)
  82. {
  83. static unsigned char data[8 * 4] = { 0, };
  84. cairo_surface_t *surface = cairo_image_surface_create_for_data (data,
  85. CAIRO_FORMAT_A1,
  86. 16, 8, 4);
  87. cairo_mask_surface (cr, surface, 0, 0);
  88. cairo_surface_destroy (surface);
  89. }
  90. static void
  91. mask_with_0x0_surface (cairo_t *cr)
  92. {
  93. cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
  94. CAIRO_CONTENT_COLOR_ALPHA,
  95. 0, 0);
  96. cairo_mask_surface (cr, surface, 0, 0);
  97. cairo_surface_destroy (surface);
  98. }
  99. static void
  100. mask_with_extend_none (cairo_t *cr)
  101. {
  102. cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
  103. CAIRO_CONTENT_COLOR_ALPHA,
  104. RECT,
  105. RECT);
  106. cairo_mask_surface (cr, surface, 2 * RECT, 2 * RECT);
  107. cairo_surface_destroy (surface);
  108. }
  109. typedef void (* mask_func_t) (cairo_t *);
  110. static mask_func_t mask_funcs[] = {
  111. paint_with_alpha,
  112. mask_with_solid,
  113. mask_with_empty_gradient,
  114. mask_with_gradient,
  115. mask_with_surface,
  116. mask_with_alpha_surface,
  117. mask_with_nonclear_surface,
  118. mask_with_0x0_surface,
  119. mask_with_extend_none
  120. };
  121. static cairo_operator_t operators[] = {
  122. CAIRO_OPERATOR_CLEAR,
  123. CAIRO_OPERATOR_SOURCE,
  124. CAIRO_OPERATOR_OVER,
  125. CAIRO_OPERATOR_IN,
  126. CAIRO_OPERATOR_DEST_ATOP,
  127. CAIRO_OPERATOR_SATURATE,
  128. CAIRO_OPERATOR_MULTIPLY
  129. };
  130. static cairo_test_status_t
  131. draw (cairo_t *cr, int width, int height)
  132. {
  133. unsigned int i, op;
  134. /* 565-compatible gray background */
  135. cairo_set_source_rgb (cr, 0.51613, 0.55555, 0.51613);
  136. cairo_paint (cr);
  137. cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); /* green */
  138. /* mask with zero-alpha in several ways */
  139. cairo_translate (cr, SPACE, SPACE);
  140. for (op = 0; op < ARRAY_LENGTH (operators); op++) {
  141. cairo_set_operator (cr, operators[op]);
  142. for (i = 0; i < ARRAY_LENGTH (mask_funcs); i++) {
  143. cairo_save (cr);
  144. cairo_translate (cr, i * (RECT + SPACE), op * (RECT + SPACE));
  145. cairo_rectangle (cr, 0, 0, RECT, RECT);
  146. cairo_clip (cr);
  147. mask_funcs[i] (cr);
  148. cairo_restore (cr);
  149. }
  150. }
  151. return CAIRO_TEST_SUCCESS;
  152. }
  153. CAIRO_TEST (zero_mask,
  154. "Testing that masking with zero alpha works",
  155. "alpha, mask", /* keywords */
  156. NULL, /* requirements */
  157. SPACE + (RECT + SPACE) * ARRAY_LENGTH (mask_funcs),
  158. SPACE + (RECT + SPACE) * ARRAY_LENGTH (operators),
  159. NULL, draw)