pthread-same-source.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2009 Benjamin Otte
  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. #define GENERATE_REFERENCE 0
  27. #include "cairo-test.h"
  28. #if !GENERATE_REFERENCE
  29. #include <pthread.h>
  30. #endif
  31. #define N_THREADS 8
  32. #define WIDTH 64
  33. #define HEIGHT 8
  34. typedef struct {
  35. cairo_surface_t *target;
  36. cairo_surface_t *source;
  37. int id;
  38. } thread_data_t;
  39. static void *
  40. draw_thread (void *arg)
  41. {
  42. thread_data_t *thread_data = arg;
  43. cairo_surface_t *surface;
  44. cairo_pattern_t *pattern;
  45. cairo_matrix_t pattern_matrix = { 2, 0, 0, 2, 0, 0 };
  46. cairo_t *cr;
  47. int x, y;
  48. cr = cairo_create (thread_data->target);
  49. cairo_surface_destroy (thread_data->target);
  50. pattern = cairo_pattern_create_for_surface (thread_data->source);
  51. cairo_surface_destroy (thread_data->source);
  52. cairo_pattern_set_extend (pattern, thread_data->id % 4);
  53. cairo_pattern_set_filter (pattern, thread_data->id >= 4 ? CAIRO_FILTER_BILINEAR : CAIRO_FILTER_NEAREST);
  54. cairo_pattern_set_matrix (pattern, &pattern_matrix);
  55. for (y = 0; y < HEIGHT; y++) {
  56. for (x = 0; x < WIDTH; x++) {
  57. cairo_save (cr);
  58. cairo_translate (cr, 4 * x + 1, 4 * y + 1);
  59. cairo_rectangle (cr, 0, 0, 2, 2);
  60. cairo_set_source (cr, pattern);
  61. cairo_fill (cr);
  62. cairo_restore (cr);
  63. }
  64. }
  65. cairo_pattern_destroy (pattern);
  66. surface = cairo_surface_reference (cairo_get_target (cr));
  67. cairo_destroy (cr);
  68. return surface;
  69. }
  70. static cairo_surface_t *
  71. create_source (cairo_surface_t *similar)
  72. {
  73. cairo_surface_t *source;
  74. cairo_t *cr;
  75. double colors[4][3] = {
  76. { 0.75, 0, 0 },
  77. { 0, 0.75, 0 },
  78. { 0, 0, 0.75 },
  79. { 0.75, 0.75, 0 }
  80. };
  81. int i;
  82. source = cairo_surface_create_similar (similar,
  83. CAIRO_CONTENT_COLOR_ALPHA,
  84. 2, 2);
  85. cr = cairo_create (source);
  86. cairo_surface_destroy (source);
  87. for (i = 0; i < 4; i++) {
  88. cairo_set_source_rgb (cr, colors[i][0], colors[i][1], colors[i][2]);
  89. cairo_rectangle (cr, i % 2, i / 2, 1, 1);
  90. cairo_fill (cr);
  91. }
  92. source = cairo_surface_reference (cairo_get_target (cr));
  93. cairo_destroy (cr);
  94. return source;
  95. }
  96. static cairo_test_status_t
  97. draw (cairo_t *cr, int width, int height)
  98. {
  99. #if !GENERATE_REFERENCE
  100. pthread_t threads[N_THREADS];
  101. #endif
  102. thread_data_t thread_data[N_THREADS];
  103. cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;
  104. cairo_surface_t *source;
  105. cairo_status_t status;
  106. int i;
  107. source = create_source (cairo_get_target (cr));
  108. status = cairo_surface_status (source);
  109. if (status) {
  110. cairo_surface_destroy (source);
  111. return cairo_test_status_from_status (cairo_test_get_context (cr),
  112. status);
  113. }
  114. cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
  115. cairo_paint (cr);
  116. for (i = 0; i < N_THREADS; i++) {
  117. thread_data[i].target = cairo_surface_create_similar (cairo_get_target (cr),
  118. CAIRO_CONTENT_COLOR_ALPHA,
  119. 4 * WIDTH, 4 * HEIGHT);
  120. thread_data[i].source = cairo_surface_reference (source);
  121. thread_data[i].id = i;
  122. #if !GENERATE_REFERENCE
  123. if (pthread_create (&threads[i], NULL, draw_thread, &thread_data[i]) != 0) {
  124. threads[i] = pthread_self (); /* to indicate error */
  125. cairo_surface_destroy (thread_data[i].target);
  126. cairo_surface_destroy (thread_data[i].source);
  127. test_status = CAIRO_TEST_FAILURE;
  128. break;
  129. }
  130. #else
  131. {
  132. cairo_surface_t *surface = draw_thread(&thread_data[i]);
  133. cairo_set_source_surface (cr, surface, 0, 0);
  134. cairo_surface_destroy (surface);
  135. cairo_paint (cr);
  136. cairo_translate (cr, 0, 4 * HEIGHT);
  137. }
  138. #endif
  139. }
  140. cairo_surface_destroy (source);
  141. #if !GENERATE_REFERENCE
  142. for (i = 0; i < N_THREADS; i++) {
  143. void *surface;
  144. if (pthread_equal (threads[i], pthread_self ()))
  145. break;
  146. if (pthread_join (threads[i], &surface) == 0) {
  147. cairo_set_source_surface (cr, surface, 0, 0);
  148. cairo_surface_destroy (surface);
  149. cairo_paint (cr);
  150. cairo_translate (cr, 0, 4 * HEIGHT);
  151. } else {
  152. test_status = CAIRO_TEST_FAILURE;
  153. }
  154. }
  155. #endif
  156. return test_status;
  157. }
  158. CAIRO_TEST (pthread_same_source,
  159. "Use the same source for drawing in different threads",
  160. "threads", /* keywords */
  161. NULL, /* requirements */
  162. 4 * WIDTH, 4 * HEIGHT * N_THREADS,
  163. NULL, draw)