1/*
2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2013 Google Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "platform/graphics/Gradient.h"
30
31#include "platform/geometry/FloatRect.h"
32#include "platform/graphics/Color.h"
33#include "platform/graphics/GraphicsContext.h"
34#include "platform/graphics/skia/SkiaUtils.h"
35#include "third_party/skia/include/core/SkColorShader.h"
36#include "third_party/skia/include/core/SkShader.h"
37#include "third_party/skia/include/effects/SkGradientShader.h"
38
39namespace WebCore {
40
41Gradient::Gradient(const FloatPoint& p0, const FloatPoint& p1)
42    : m_radial(false)
43    , m_p0(p0)
44    , m_p1(p1)
45    , m_r0(0)
46    , m_r1(0)
47    , m_aspectRatio(1)
48    , m_stopsSorted(false)
49    , m_spreadMethod(SpreadMethodPad)
50    , m_drawInPMColorSpace(false)
51{
52}
53
54Gradient::Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio)
55    : m_radial(true)
56    , m_p0(p0)
57    , m_p1(p1)
58    , m_r0(r0)
59    , m_r1(r1)
60    , m_aspectRatio(aspectRatio)
61    , m_stopsSorted(false)
62    , m_spreadMethod(SpreadMethodPad)
63    , m_drawInPMColorSpace(false)
64{
65}
66
67Gradient::~Gradient()
68{
69}
70
71void Gradient::addColorStop(float value, const Color& color)
72{
73    float r;
74    float g;
75    float b;
76    float a;
77    color.getRGBA(r, g, b, a);
78    m_stops.append(ColorStop(value, r, g, b, a));
79
80    m_stopsSorted = false;
81    m_gradient.clear();
82}
83
84void Gradient::addColorStop(const Gradient::ColorStop& stop)
85{
86    m_stops.append(stop);
87
88    m_stopsSorted = false;
89    m_gradient.clear();
90}
91
92static inline bool compareStops(const Gradient::ColorStop& a, const Gradient::ColorStop& b)
93{
94    return a.stop < b.stop;
95}
96
97void Gradient::sortStopsIfNecessary()
98{
99    if (m_stopsSorted)
100        return;
101
102    m_stopsSorted = true;
103
104    if (!m_stops.size())
105        return;
106
107    std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
108}
109
110bool Gradient::hasAlpha() const
111{
112    for (size_t i = 0; i < m_stops.size(); i++) {
113        if (m_stops[i].alpha < 1)
114            return true;
115    }
116
117    return false;
118}
119
120void Gradient::setSpreadMethod(GradientSpreadMethod spreadMethod)
121{
122    // FIXME: Should it become necessary, allow calls to this method after m_gradient has been set.
123    ASSERT(!m_gradient);
124
125    if (m_spreadMethod == spreadMethod)
126        return;
127
128    m_spreadMethod = spreadMethod;
129}
130
131void Gradient::setDrawsInPMColorSpace(bool drawInPMColorSpace)
132{
133    if (drawInPMColorSpace == m_drawInPMColorSpace)
134        return;
135
136    m_drawInPMColorSpace = drawInPMColorSpace;
137    m_gradient.clear();
138}
139
140void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation)
141{
142    if (m_gradientSpaceTransformation == gradientSpaceTransformation)
143        return;
144
145    m_gradientSpaceTransformation = gradientSpaceTransformation;
146    if (m_gradient)
147        m_gradient->setLocalMatrix(affineTransformToSkMatrix(m_gradientSpaceTransformation));
148}
149
150static inline U8CPU F2B(float x)
151{
152    return static_cast<int>(x * 255);
153}
154
155static SkColor makeSkColor(float a, float r, float g, float b)
156{
157    return SkColorSetARGB(F2B(a), F2B(r), F2B(g), F2B(b));
158}
159
160// Determine the total number of stops needed, including pseudo-stops at the
161// ends as necessary.
162static size_t totalStopsNeeded(const Gradient::ColorStop* stopData, size_t count)
163{
164    // N.B.: The tests in this function should kept in sync with the ones in
165    // fillStops(), or badness happens.
166    const Gradient::ColorStop* stop = stopData;
167    size_t countUsed = count;
168    if (count < 1 || stop->stop > 0.0)
169        countUsed++;
170    stop += count - 1;
171    if (count < 1 || stop->stop < 1.0)
172        countUsed++;
173    return countUsed;
174}
175
176// Collect sorted stop position and color information into the pos and colors
177// buffers, ensuring stops at both 0.0 and 1.0. The buffers must be large
178// enough to hold information for all stops, including the new endpoints if
179// stops at 0.0 and 1.0 aren't already included.
180static void fillStops(const Gradient::ColorStop* stopData,
181    size_t count, SkScalar* pos, SkColor* colors)
182{
183    const Gradient::ColorStop* stop = stopData;
184    size_t start = 0;
185    if (count < 1) {
186        // A gradient with no stops must be transparent black.
187        pos[0] = WebCoreFloatToSkScalar(0.0);
188        colors[0] = makeSkColor(0.0, 0.0, 0.0, 0.0);
189        start = 1;
190    } else if (stop->stop > 0.0) {
191        // Copy the first stop to 0.0. The first stop position may have a slight
192        // rounding error, but we don't care in this float comparison, since
193        // 0.0 comes through cleanly and people aren't likely to want a gradient
194        // with a stop at (0 + epsilon).
195        pos[0] = WebCoreFloatToSkScalar(0.0);
196        colors[0] = makeSkColor(stop->alpha, stop->red, stop->green, stop->blue);
197        start = 1;
198    }
199
200    for (size_t i = start; i < start + count; i++) {
201        pos[i] = WebCoreFloatToSkScalar(stop->stop);
202        colors[i] = makeSkColor(stop->alpha, stop->red, stop->green, stop->blue);
203        ++stop;
204    }
205
206    // Copy the last stop to 1.0 if needed. See comment above about this float
207    // comparison.
208    if (count < 1 || (--stop)->stop < 1.0) {
209        pos[start + count] = WebCoreFloatToSkScalar(1.0);
210        colors[start + count] = colors[start + count - 1];
211    }
212}
213
214SkShader* Gradient::shader()
215{
216    if (m_gradient)
217        return m_gradient.get();
218
219    sortStopsIfNecessary();
220    ASSERT(m_stopsSorted);
221
222    size_t countUsed = totalStopsNeeded(m_stops.data(), m_stops.size());
223    ASSERT(countUsed >= 2);
224    ASSERT(countUsed >= m_stops.size());
225
226    // FIXME: Why is all this manual pointer math needed?!
227    SkAutoMalloc storage(countUsed * (sizeof(SkColor) + sizeof(SkScalar)));
228    SkColor* colors = (SkColor*)storage.get();
229    SkScalar* pos = (SkScalar*)(colors + countUsed);
230
231    fillStops(m_stops.data(), m_stops.size(), pos, colors);
232
233    SkShader::TileMode tile = SkShader::kClamp_TileMode;
234    switch (m_spreadMethod) {
235    case SpreadMethodReflect:
236        tile = SkShader::kMirror_TileMode;
237        break;
238    case SpreadMethodRepeat:
239        tile = SkShader::kRepeat_TileMode;
240        break;
241    case SpreadMethodPad:
242        tile = SkShader::kClamp_TileMode;
243        break;
244    }
245
246    uint32_t shouldDrawInPMColorSpace = m_drawInPMColorSpace ? SkGradientShader::kInterpolateColorsInPremul_Flag : 0;
247    if (m_radial) {
248        // Since the two-point radial gradient is slower than the plain radial,
249        // only use it if we have to.
250        if (m_p0 == m_p1 && m_r0 <= 0.0f) {
251            m_gradient = adoptRef(SkGradientShader::CreateRadial(m_p1, m_r1, colors, pos, static_cast<int>(countUsed), tile, 0, shouldDrawInPMColorSpace));
252        } else {
253            // The radii we give to Skia must be positive. If we're given a
254            // negative radius, ask for zero instead.
255            SkScalar radius0 = m_r0 >= 0.0f ? WebCoreFloatToSkScalar(m_r0) : 0;
256            SkScalar radius1 = m_r1 >= 0.0f ? WebCoreFloatToSkScalar(m_r1) : 0;
257            m_gradient = adoptRef(SkGradientShader::CreateTwoPointConical(m_p0, radius0, m_p1, radius1, colors, pos, static_cast<int>(countUsed), tile, 0, shouldDrawInPMColorSpace));
258        }
259
260        if (aspectRatio() != 1) {
261            // CSS3 elliptical gradients: apply the elliptical scaling at the
262            // gradient center point.
263            m_gradientSpaceTransformation.translate(m_p0.x(), m_p0.y());
264            m_gradientSpaceTransformation.scale(1, 1 / aspectRatio());
265            m_gradientSpaceTransformation.translate(-m_p0.x(), -m_p0.y());
266            ASSERT(m_p0 == m_p1);
267        }
268    } else {
269        SkPoint pts[2] = { m_p0, m_p1 };
270        m_gradient = adoptRef(SkGradientShader::CreateLinear(pts, colors, pos, static_cast<int>(countUsed), tile, 0, shouldDrawInPMColorSpace));
271    }
272
273    if (!m_gradient) {
274        // use last color, since our "geometry" was degenerate (e.g. radius==0)
275        m_gradient = adoptRef(new SkColorShader(colors[countUsed - 1]));
276    } else {
277        m_gradient->setLocalMatrix(affineTransformToSkMatrix(m_gradientSpaceTransformation));
278    }
279    return m_gradient.get();
280}
281
282} //namespace
283