OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. |
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> | 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> |
4 * Copyright (C) 2013 Google Inc. All rights reserved. | 4 * Copyright (C) 2013 Google Inc. All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 28 matching lines...) Expand all Loading... |
39 typedef Vector<SkColor, 8> ColorStopColorVector; | 39 typedef Vector<SkColor, 8> ColorStopColorVector; |
40 | 40 |
41 namespace blink { | 41 namespace blink { |
42 | 42 |
43 Gradient::Gradient(const FloatPoint& p0, const FloatPoint& p1) | 43 Gradient::Gradient(const FloatPoint& p0, const FloatPoint& p1) |
44 : m_p0(p0) | 44 : m_p0(p0) |
45 , m_p1(p1) | 45 , m_p1(p1) |
46 , m_r0(0) | 46 , m_r0(0) |
47 , m_r1(0) | 47 , m_r1(0) |
48 , m_aspectRatio(1) | 48 , m_aspectRatio(1) |
49 , m_radial(false) | 49 , m_class(LinearClass) |
50 , m_stopsSorted(false) | 50 , m_stopsSorted(false) |
51 , m_drawInPMColorSpace(false) | 51 , m_drawInPMColorSpace(false) |
52 , m_spreadMethod(SpreadMethodPad) | 52 , m_spreadMethod(SpreadMethodPad) |
53 { | 53 { |
54 } | 54 } |
55 | 55 |
56 Gradient::Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r
1, float aspectRatio) | 56 Gradient::Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r
1, float aspectRatio) |
57 : m_p0(p0) | 57 : m_p0(p0) |
58 , m_p1(p1) | 58 , m_p1(p1) |
59 , m_r0(r0) | 59 , m_r0(r0) |
60 , m_r1(r1) | 60 , m_r1(r1) |
61 , m_aspectRatio(aspectRatio) | 61 , m_aspectRatio(aspectRatio) |
62 , m_radial(true) | 62 , m_class(RadialClass) |
63 , m_stopsSorted(false) | 63 , m_stopsSorted(false) |
64 , m_drawInPMColorSpace(false) | 64 , m_drawInPMColorSpace(false) |
65 , m_spreadMethod(SpreadMethodPad) | 65 , m_spreadMethod(SpreadMethodPad) |
| 66 { |
| 67 } |
| 68 |
| 69 Gradient::Gradient(const FloatPoint& p0, float startAngle) |
| 70 : m_p0(p0) |
| 71 , m_r0(startAngle) // FIXME |
| 72 , m_r1(0) |
| 73 , m_aspectRatio(1) |
| 74 , m_class(ConicClass) |
| 75 , m_stopsSorted(false) |
| 76 , m_drawInPMColorSpace(false) |
| 77 , m_spreadMethod(SpreadMethodPad) |
66 { | 78 { |
67 } | 79 } |
68 | 80 |
69 Gradient::~Gradient() | 81 Gradient::~Gradient() |
70 { | 82 { |
71 } | 83 } |
72 | 84 |
73 static inline bool compareStops(const Gradient::ColorStop& a, const Gradient::Co
lorStop& b) | 85 static inline bool compareStops(const Gradient::ColorStop& a, const Gradient::Co
lorStop& b) |
74 { | 86 { |
75 return a.stop < b.stop; | 87 return a.stop < b.stop; |
76 } | 88 } |
77 | 89 |
78 void Gradient::addColorStop(const Gradient::ColorStop& stop) | 90 void Gradient::addColorStop(const Gradient::ColorStop& stop) |
79 { | 91 { |
| 92 printf("*** addColorStop: %f/%x\n", stop.stop, stop.color.rgb()); |
80 if (m_stops.isEmpty()) { | 93 if (m_stops.isEmpty()) { |
81 m_stopsSorted = true; | 94 m_stopsSorted = true; |
82 } else { | 95 } else { |
83 m_stopsSorted = m_stopsSorted && compareStops(m_stops.last(), stop); | 96 m_stopsSorted = m_stopsSorted && compareStops(m_stops.last(), stop); |
84 } | 97 } |
85 | 98 |
86 m_stops.append(stop); | 99 m_stops.append(stop); |
87 m_gradient.reset(); | 100 m_gradient.reset(); |
88 } | 101 } |
89 | 102 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 case SpreadMethodRepeat: | 223 case SpreadMethodRepeat: |
211 tile = SkShader::kRepeat_TileMode; | 224 tile = SkShader::kRepeat_TileMode; |
212 break; | 225 break; |
213 case SpreadMethodPad: | 226 case SpreadMethodPad: |
214 tile = SkShader::kClamp_TileMode; | 227 tile = SkShader::kClamp_TileMode; |
215 break; | 228 break; |
216 } | 229 } |
217 | 230 |
218 sk_sp<SkShader> shader; | 231 sk_sp<SkShader> shader; |
219 uint32_t shouldDrawInPMColorSpace = m_drawInPMColorSpace ? SkGradientShader:
:kInterpolateColorsInPremul_Flag : 0; | 232 uint32_t shouldDrawInPMColorSpace = m_drawInPMColorSpace ? SkGradientShader:
:kInterpolateColorsInPremul_Flag : 0; |
220 if (m_radial) { | 233 switch (m_class) { |
| 234 case RadialClass: { |
221 if (aspectRatio() != 1) { | 235 if (aspectRatio() != 1) { |
222 // CSS3 elliptical gradients: apply the elliptical scaling at the | 236 // CSS3 elliptical gradients: apply the elliptical scaling at the |
223 // gradient center point. | 237 // gradient center point. |
224 m_gradientSpaceTransformation.translate(m_p0.x(), m_p0.y()); | 238 m_gradientSpaceTransformation.translate(m_p0.x(), m_p0.y()); |
225 m_gradientSpaceTransformation.scale(1, 1 / aspectRatio()); | 239 m_gradientSpaceTransformation.scale(1, 1 / aspectRatio()); |
226 m_gradientSpaceTransformation.translate(-m_p0.x(), -m_p0.y()); | 240 m_gradientSpaceTransformation.translate(-m_p0.x(), -m_p0.y()); |
227 ASSERT(m_p0 == m_p1); | 241 ASSERT(m_p0 == m_p1); |
228 } | 242 } |
229 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); | 243 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); |
230 | 244 |
231 // Since the two-point radial gradient is slower than the plain radial, | 245 // Since the two-point radial gradient is slower than the plain radial, |
232 // only use it if we have to. | 246 // only use it if we have to. |
233 if (m_p0 == m_p1 && m_r0 <= 0.0f) { | 247 if (m_p0 == m_p1 && m_r0 <= 0.0f) { |
234 shader = SkGradientShader::MakeRadial(m_p1.data(), m_r1, colors.data
(), pos.data(), static_cast<int>(countUsed), tile, shouldDrawInPMColorSpace, &lo
calMatrix); | 248 shader = SkGradientShader::MakeRadial(m_p1.data(), m_r1, colors.data
(), pos.data(), static_cast<int>(countUsed), tile, shouldDrawInPMColorSpace, &lo
calMatrix); |
235 } else { | 249 } else { |
236 // The radii we give to Skia must be positive. If we're given a | 250 // The radii we give to Skia must be positive. If we're given a |
237 // negative radius, ask for zero instead. | 251 // negative radius, ask for zero instead. |
238 SkScalar radius0 = m_r0 >= 0.0f ? WebCoreFloatToSkScalar(m_r0) : 0; | 252 SkScalar radius0 = m_r0 >= 0.0f ? WebCoreFloatToSkScalar(m_r0) : 0; |
239 SkScalar radius1 = m_r1 >= 0.0f ? WebCoreFloatToSkScalar(m_r1) : 0; | 253 SkScalar radius1 = m_r1 >= 0.0f ? WebCoreFloatToSkScalar(m_r1) : 0; |
240 shader = SkGradientShader::MakeTwoPointConical(m_p0.data(), radius0,
m_p1.data(), radius1, colors.data(), pos.data(), static_cast<int>(countUsed), t
ile, shouldDrawInPMColorSpace, &localMatrix); | 254 shader = SkGradientShader::MakeTwoPointConical(m_p0.data(), radius0,
m_p1.data(), radius1, colors.data(), pos.data(), static_cast<int>(countUsed), t
ile, shouldDrawInPMColorSpace, &localMatrix); |
241 } | 255 } |
242 } else { | 256 } break; |
| 257 case LinearClass: { |
243 SkPoint pts[2] = { m_p0.data(), m_p1.data() }; | 258 SkPoint pts[2] = { m_p0.data(), m_p1.data() }; |
244 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); | 259 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); |
245 shader = SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), st
atic_cast<int>(countUsed), tile, shouldDrawInPMColorSpace, &localMatrix); | 260 shader = SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), st
atic_cast<int>(countUsed), tile, shouldDrawInPMColorSpace, &localMatrix); |
| 261 } break; |
| 262 case ConicClass: { |
| 263 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); |
| 264 shader = SkGradientShader::MakeSweep(m_p0.x(), m_p0.y(), colors.data(),
pos.data(), static_cast<int>(countUsed), shouldDrawInPMColorSpace, &localMatrix)
; |
| 265 } break; |
246 } | 266 } |
247 | 267 |
248 if (!shader) { | 268 if (!shader) { |
249 // use last color, since our "geometry" was degenerate (e.g. radius==0) | 269 // use last color, since our "geometry" was degenerate (e.g. radius==0) |
250 shader = SkShader::MakeColorShader(colors[countUsed - 1]); | 270 shader = SkShader::MakeColorShader(colors[countUsed - 1]); |
251 } | 271 } |
252 | 272 |
253 return shader; | 273 return shader; |
254 } | 274 } |
255 | 275 |
256 void Gradient::applyToPaint(SkPaint& paint) | 276 void Gradient::applyToPaint(SkPaint& paint) |
257 { | 277 { |
258 if (!m_gradient) | 278 if (!m_gradient) |
259 m_gradient = createShader(); | 279 m_gradient = createShader(); |
260 | 280 |
261 paint.setShader(m_gradient); | 281 paint.setShader(m_gradient); |
262 | 282 |
263 // Legacy behavior: gradients are always dithered. | 283 // Legacy behavior: gradients are always dithered. |
264 paint.setDither(true); | 284 paint.setDither(true); |
265 } | 285 } |
266 | 286 |
267 } // namespace blink | 287 } // namespace blink |
OLD | NEW |