• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "Gradient.h"
33 
34 #include "CSSParser.h"
35 #include "GraphicsContext.h"
36 
37 #include "SkGradientShader.h"
38 #include "SkiaUtils.h"
39 
40 namespace WebCore {
41 
platformDestroy()42 void Gradient::platformDestroy()
43 {
44     if (m_gradient)
45         m_gradient->safeUnref();
46     m_gradient = 0;
47 }
48 
F2B(float x)49 static inline U8CPU F2B(float x)
50 {
51     return static_cast<int>(x * 255);
52 }
53 
makeSkColor(float a,float r,float g,float b)54 static SkColor makeSkColor(float a, float r, float g, float b)
55 {
56     return SkColorSetARGB(F2B(a), F2B(r), F2B(g), F2B(b));
57 }
58 
59 // Determine the total number of stops needed, including pseudo-stops at the
60 // ends as necessary.
totalStopsNeeded(const Gradient::ColorStop * stopData,size_t count)61 static size_t totalStopsNeeded(const Gradient::ColorStop* stopData, size_t count)
62 {
63     // N.B.:  The tests in this function should kept in sync with the ones in
64     // fillStops(), or badness happens.
65     const Gradient::ColorStop* stop = stopData;
66     size_t countUsed = count;
67     if (count < 1 || stop->stop > 0.0)
68         countUsed++;
69     stop += count - 1;
70     if (count < 1 || stop->stop < 1.0)
71         countUsed++;
72     return countUsed;
73 }
74 
75 // Collect sorted stop position and color information into the pos and colors
76 // buffers, ensuring stops at both 0.0 and 1.0.  The buffers must be large
77 // enough to hold information for all stops, including the new endpoints if
78 // stops at 0.0 and 1.0 aren't already included.
fillStops(const Gradient::ColorStop * stopData,size_t count,SkScalar * pos,SkColor * colors)79 static void fillStops(const Gradient::ColorStop* stopData,
80                        size_t count, SkScalar* pos, SkColor* colors)
81 {
82     const Gradient::ColorStop* stop = stopData;
83     size_t start = 0;
84     if (count < 1) {
85         // A gradient with no stops must be transparent black.
86         pos[0] = WebCoreFloatToSkScalar(0.0);
87         colors[0] = makeSkColor(0.0, 0.0, 0.0, 0.0);
88         start = 1;
89     } else if (stop->stop > 0.0) {
90         // Copy the first stop to 0.0. The first stop position may have a slight
91         // rounding error, but we don't care in this float comparison, since
92         // 0.0 comes through cleanly and people aren't likely to want a gradient
93         // with a stop at (0 + epsilon).
94         pos[0] = WebCoreFloatToSkScalar(0.0);
95         colors[0] = makeSkColor(stop->alpha, stop->red, stop->green, stop->blue);
96         start = 1;
97     }
98 
99     for (size_t i = start; i < start + count; i++) {
100         pos[i] = WebCoreFloatToSkScalar(stop->stop);
101         colors[i] = makeSkColor(stop->alpha, stop->red, stop->green, stop->blue);
102         ++stop;
103     }
104 
105     // Copy the last stop to 1.0 if needed.  See comment above about this float
106     // comparison.
107     if (count < 1 || (--stop)->stop < 1.0) {
108         pos[start + count] = WebCoreFloatToSkScalar(1.0);
109         colors[start + count] = colors[start + count - 1];
110     }
111 }
112 
compareStops(const Gradient::ColorStop & a,const Gradient::ColorStop & b)113 static inline bool compareStops(const Gradient::ColorStop& a, const Gradient::ColorStop& b)
114 {
115     return a.stop < b.stop;
116 }
117 
platformGradient()118 SkShader* Gradient::platformGradient()
119 {
120     if (m_gradient)
121         return m_gradient;
122 
123     // FIXME: This and compareStops() are also in Gradient.cpp and
124     // CSSGradientValue.cpp; probably should refactor in WebKit.
125     if (!m_stopsSorted) {
126         if (m_stops.size())
127             std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
128         m_stopsSorted = true;
129     }
130     size_t countUsed = totalStopsNeeded(m_stops.data(), m_stops.size());
131     ASSERT(countUsed >= 2);
132     ASSERT(countUsed >= m_stops.size());
133 
134     // FIXME: Why is all this manual pointer math needed?!
135     SkAutoMalloc storage(countUsed * (sizeof(SkColor) + sizeof(SkScalar)));
136     SkColor* colors = (SkColor*)storage.get();
137     SkScalar* pos = (SkScalar*)(colors + countUsed);
138 
139     fillStops(m_stops.data(), m_stops.size(), pos, colors);
140 
141     SkShader::TileMode tile = SkShader::kClamp_TileMode;
142     switch (m_spreadMethod) {
143     case SpreadMethodReflect:
144         tile = SkShader::kMirror_TileMode;
145         break;
146     case SpreadMethodRepeat:
147         tile = SkShader::kRepeat_TileMode;
148         break;
149     case SpreadMethodPad:
150         tile = SkShader::kClamp_TileMode;
151         break;
152     }
153 
154     if (m_radial) {
155         // FIXME: CSS radial Gradients allow an offset focal point (the
156         // "start circle"), but skia doesn't seem to support that, so this just
157         // ignores m_p0/m_r0 and draws the gradient centered in the "end
158         // circle" (m_p1/m_r1).
159         // See http://webkit.org/blog/175/introducing-css-gradients/ for a
160         // description of the expected behavior.
161 
162         // The radius we give to Skia must be positive (and non-zero).  If
163         // we're given a zero radius, just ask for a very small radius so
164         // Skia will still return an object.
165         SkScalar radius = m_r1 > 0 ? WebCoreFloatToSkScalar(m_r1) : SK_ScalarMin;
166         m_gradient = SkGradientShader::CreateRadial(m_p1,
167             radius, colors, pos, static_cast<int>(countUsed), tile);
168     } else {
169         SkPoint pts[2] = { m_p0, m_p1 };
170         m_gradient = SkGradientShader::CreateLinear(pts, colors, pos,
171             static_cast<int>(countUsed), tile);
172     }
173 
174     ASSERT(m_gradient);
175 
176     SkMatrix matrix = m_gradientSpaceTransformation;
177     m_gradient->setLocalMatrix(matrix);
178 
179     return m_gradient;
180 }
181 
fill(GraphicsContext * context,const FloatRect & rect)182 void Gradient::fill(GraphicsContext* context, const FloatRect& rect)
183 {
184     context->setFillGradient(this);
185     context->fillRect(rect);
186 }
187 
setPlatformGradientSpaceTransform(const TransformationMatrix & matrix)188 void Gradient::setPlatformGradientSpaceTransform(const TransformationMatrix& matrix)
189 {
190     if (m_gradient)
191         m_gradient->setLocalMatrix(m_gradientSpaceTransformation);
192 }
193 
194 } // namespace WebCore
195