operator-alpha-alpha.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
  2. /* cairo - a vector graphics library with display and print output
  3. *
  4. * Copyright 2002 University of Southern California
  5. * Copyright 2005 Red Hat, Inc.
  6. * Copyright 2007 Emmanuel Pacaud
  7. * Copyright 2008 Benjamin Otte
  8. * Copyright 2008 Chris Wilson
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it either under the terms of the GNU Lesser General Public
  12. * License version 2.1 as published by the Free Software Foundation
  13. * (the "LGPL") or, at your option, under the terms of the Mozilla
  14. * Public License Version 1.1 (the "MPL"). If you do not alter this
  15. * notice, a recipient may use your version of this file under either
  16. * the MPL or the LGPL.
  17. *
  18. * You should have received a copy of the LGPL along with this library
  19. * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  21. * You should have received a copy of the MPL along with this library
  22. * in the file COPYING-MPL-1.1
  23. *
  24. * The contents of this file are subject to the Mozilla Public License
  25. * Version 1.1 (the "License"); you may not use this file except in
  26. * compliance with the License. You may obtain a copy of the License at
  27. * http://www.mozilla.org/MPL/
  28. *
  29. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  30. * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  31. * the specific language governing rights and limitations.
  32. *
  33. * The Original Code is the cairo graphics library.
  34. *
  35. * The Initial Developer of the Original Code is University of Southern
  36. * California.
  37. *
  38. * Contributor(s):
  39. * Owen Taylor <otaylor@redhat.com>
  40. * Kristian Høgsberg <krh@redhat.com>
  41. * Emmanuel Pacaud <emmanuel.pacaud@lapp.in2p3.fr>
  42. * Chris Wilson <chris@chris-wilson.co.uk>
  43. * Andrea Canciani <ranma42@gmail.com>
  44. */
  45. #include "cairo-test.h"
  46. #define STEPS 16
  47. #define START_OPERATOR CAIRO_OPERATOR_CLEAR
  48. #define STOP_OPERATOR CAIRO_OPERATOR_HSL_LUMINOSITY
  49. #define SIZE 3
  50. #define COUNT 6
  51. #define FULL_WIDTH ((STEPS + 1) * COUNT - 1)
  52. #define FULL_HEIGHT ((COUNT + STOP_OPERATOR - START_OPERATOR) / COUNT) * (STEPS + 1)
  53. static void
  54. create_patterns (cairo_t *bg, cairo_t *fg)
  55. {
  56. int x;
  57. for (x = 0; x < STEPS; x++) {
  58. double i = (double) x / (STEPS - 1);
  59. cairo_set_source_rgba (bg, 0, 0, 0, i);
  60. cairo_rectangle (bg, x, 0, 1, STEPS);
  61. cairo_fill (bg);
  62. cairo_set_source_rgba (fg, 0, 0, 0, i);
  63. cairo_rectangle (fg, 0, x, STEPS, 1);
  64. cairo_fill (fg);
  65. }
  66. }
  67. /* expects a STEP*STEP pixel rectangle */
  68. static void
  69. do_composite (cairo_t *cr, cairo_operator_t op, cairo_surface_t *bg, cairo_surface_t *fg)
  70. {
  71. cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  72. cairo_set_source_surface (cr, bg, 0, 0);
  73. cairo_paint (cr);
  74. cairo_set_operator (cr, op);
  75. cairo_set_source_surface (cr, fg, 0, 0);
  76. cairo_paint (cr);
  77. }
  78. static void
  79. subdraw (cairo_t *cr, int width, int height)
  80. {
  81. size_t i = 0;
  82. cairo_operator_t op;
  83. cairo_t *bgcr, *fgcr;
  84. cairo_surface_t *bg, *fg;
  85. bg = cairo_surface_create_similar (cairo_get_target (cr),
  86. CAIRO_CONTENT_ALPHA, SIZE * STEPS, SIZE * STEPS);
  87. fg = cairo_surface_create_similar (cairo_get_target (cr),
  88. CAIRO_CONTENT_ALPHA, SIZE * STEPS, SIZE * STEPS);
  89. bgcr = cairo_create (bg);
  90. fgcr = cairo_create (fg);
  91. cairo_scale (bgcr, SIZE, SIZE);
  92. cairo_scale (fgcr, SIZE, SIZE);
  93. create_patterns (bgcr, fgcr);
  94. cairo_destroy (bgcr);
  95. cairo_destroy (fgcr);
  96. for (op = START_OPERATOR; op <= STOP_OPERATOR; op++, i++) {
  97. cairo_save (cr);
  98. cairo_translate (cr,
  99. SIZE * (STEPS + 1) * (i % COUNT),
  100. SIZE * (STEPS + 1) * (i / COUNT));
  101. cairo_rectangle (cr, 0, 0, SIZE * (STEPS + 1), SIZE * (STEPS+1));
  102. cairo_clip (cr);
  103. do_composite (cr, op, bg, fg);
  104. cairo_restore (cr);
  105. }
  106. cairo_surface_destroy (fg);
  107. cairo_surface_destroy (bg);
  108. }
  109. static cairo_surface_t *
  110. create_source (cairo_surface_t *target, int width, int height)
  111. {
  112. cairo_surface_t *similar;
  113. cairo_t *cr;
  114. similar = cairo_surface_create_similar (target,
  115. CAIRO_CONTENT_ALPHA,
  116. width, height);
  117. cr = cairo_create (similar);
  118. cairo_surface_destroy (similar);
  119. subdraw (cr, width, height);
  120. similar = cairo_surface_reference (cairo_get_target (cr));
  121. cairo_destroy (cr);
  122. return similar;
  123. }
  124. static cairo_test_status_t
  125. draw (cairo_t *cr, int width, int height)
  126. {
  127. cairo_surface_t *source;
  128. cairo_set_source_rgb (cr, 1, 1, 1);
  129. cairo_paint (cr);
  130. source = create_source (cairo_get_target (cr), width, height);
  131. cairo_set_source_surface (cr, source, 0, 0);
  132. cairo_surface_destroy (source);
  133. cairo_paint (cr);
  134. return CAIRO_TEST_SUCCESS;
  135. }
  136. CAIRO_TEST (operator_alpha_alpha,
  137. "Tests result of compositing pure-alpha surfaces"
  138. "\nCompositing of pure-alpha sources is inconsistent across backends.",
  139. "alpha, similar, operator", /* keywords */
  140. NULL, /* requirements */
  141. FULL_WIDTH * SIZE, FULL_HEIGHT * SIZE,
  142. NULL, draw)