surface-source.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright © 2008 Chris Wilson
  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. * Chris Wilson not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Chris Wilson makes 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. * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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. * Author: Chris Wilson <chris@chris-wilson.co.uk>
  24. */
  25. #include "cairo-test.h"
  26. static cairo_surface_t *create_source_surface (int size);
  27. /* We use a relatively large source to exercise bug:
  28. * Bug 7360 painting huge surfaces fails
  29. * [https://bugs.freedesktop.org/show_bug.cgi?id=7360]
  30. * but still keep the resultant image small for reasonably quick checking.
  31. */
  32. #define SOURCE_SIZE 2000
  33. #define INTER_SIZE 512
  34. #define SIZE 96
  35. static void
  36. draw_pattern (cairo_surface_t **surface_inout, int surface_size)
  37. {
  38. cairo_t *cr;
  39. int mid = surface_size/2;
  40. cr = cairo_create (*surface_inout);
  41. cairo_surface_destroy (*surface_inout);
  42. cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  43. cairo_set_source_rgba (cr, 0, 0, 0, 0);
  44. cairo_paint (cr);
  45. cairo_rectangle (cr, 0, 0, surface_size, surface_size);
  46. cairo_rectangle (cr, mid - SIZE/4, mid + SIZE/4, SIZE/2, -SIZE/2);
  47. cairo_clip (cr);
  48. /* outside squares -> opaque */
  49. cairo_set_source_rgb (cr, 1, 1, 1);
  50. cairo_rectangle (cr,
  51. 0, 0,
  52. surface_size / 2, surface_size / 2);
  53. cairo_fill (cr);
  54. cairo_set_source_rgb (cr, 1, 0, 0);
  55. cairo_rectangle (cr,
  56. surface_size / 2, 0,
  57. surface_size / 2, surface_size / 2);
  58. cairo_fill (cr);
  59. cairo_set_source_rgb (cr, 0, 1, 0);
  60. cairo_rectangle (cr,
  61. 0, surface_size / 2,
  62. surface_size / 2, surface_size / 2);
  63. cairo_fill (cr);
  64. cairo_set_source_rgb (cr, 0, 0, 1);
  65. cairo_rectangle (cr,
  66. surface_size / 2, surface_size / 2,
  67. surface_size / 2, surface_size / 2);
  68. cairo_fill (cr);
  69. cairo_reset_clip (cr);
  70. cairo_rectangle (cr, mid - SIZE/4, mid - SIZE/4, SIZE/2, SIZE/2);
  71. cairo_clip (cr);
  72. /* inside squares -> translucent */
  73. cairo_set_source_rgba (cr, 0, 0, 1, .5);
  74. cairo_rectangle (cr,
  75. 0, 0,
  76. surface_size / 2, surface_size / 2);
  77. cairo_fill (cr);
  78. cairo_set_source_rgba (cr, 0, 1, 0, .5);
  79. cairo_rectangle (cr,
  80. surface_size / 2, 0,
  81. surface_size / 2, surface_size / 2);
  82. cairo_fill (cr);
  83. cairo_set_source_rgba (cr, 1, 0, 0, .5);
  84. cairo_rectangle (cr,
  85. 0, surface_size / 2,
  86. surface_size / 2, surface_size / 2);
  87. cairo_fill (cr);
  88. cairo_set_source_rgba (cr, 1, 1, 1, .5);
  89. cairo_rectangle (cr,
  90. surface_size / 2, surface_size / 2,
  91. surface_size / 2, surface_size / 2);
  92. cairo_fill (cr);
  93. *surface_inout = cairo_surface_reference (cairo_get_target (cr));
  94. cairo_destroy (cr);
  95. }
  96. static cairo_test_status_t
  97. draw (cairo_t *cr, int width, int height)
  98. {
  99. cairo_surface_t *surface;
  100. cairo_surface_t *similar;
  101. cairo_status_t status;
  102. cairo_t *cr2;
  103. cairo_set_source_rgb (cr, 0, 0, 0);
  104. cairo_paint (cr);
  105. surface = create_source_surface (SOURCE_SIZE);
  106. if (surface == NULL) /* can't create the source so skip the test */
  107. return CAIRO_TEST_UNTESTED;
  108. draw_pattern (&surface, SOURCE_SIZE);
  109. /* copy a subregion to a smaller intermediate surface */
  110. similar = cairo_surface_create_similar (surface,
  111. CAIRO_CONTENT_COLOR_ALPHA,
  112. INTER_SIZE, INTER_SIZE);
  113. cr2 = cairo_create (similar);
  114. cairo_surface_destroy (similar);
  115. cairo_set_source_surface (cr2, surface,
  116. (INTER_SIZE - SOURCE_SIZE)/2,
  117. (INTER_SIZE - SOURCE_SIZE)/2);
  118. cairo_paint (cr2);
  119. /* and then paint onto a small surface for checking */
  120. cairo_set_source_surface (cr, cairo_get_target (cr2),
  121. (width - INTER_SIZE)/2,
  122. (height - INTER_SIZE)/2);
  123. cairo_destroy (cr2);
  124. cairo_rectangle (cr, 16, 16, 64, 64);
  125. cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  126. cairo_fill (cr);
  127. /* destroy the surface last, as this triggers XCloseDisplay */
  128. cairo_surface_finish (surface);
  129. status = cairo_surface_status (surface);
  130. cairo_surface_destroy (surface);
  131. return cairo_test_status_from_status (cairo_test_get_context (cr),
  132. status);
  133. }
  134. static cairo_test_status_t
  135. preamble (cairo_test_context_t *ctx)
  136. {
  137. cairo_surface_t *surface;
  138. cairo_status_t status;
  139. surface = create_source_surface (SOURCE_SIZE);
  140. if (surface == NULL) /* can't create the source so skip the test */
  141. return CAIRO_TEST_UNTESTED;
  142. cairo_surface_finish (surface);
  143. status = cairo_surface_status (surface);
  144. cairo_surface_destroy (surface);
  145. return cairo_test_status_from_status (ctx, status);
  146. }