| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 77 |
| 78 void Gradient::addColorStop(const Gradient::ColorStop& stop) | 78 void Gradient::addColorStop(const Gradient::ColorStop& stop) |
| 79 { | 79 { |
| 80 if (m_stops.isEmpty()) { | 80 if (m_stops.isEmpty()) { |
| 81 m_stopsSorted = true; | 81 m_stopsSorted = true; |
| 82 } else { | 82 } else { |
| 83 m_stopsSorted = m_stopsSorted && compareStops(m_stops.last(), stop); | 83 m_stopsSorted = m_stopsSorted && compareStops(m_stops.last(), stop); |
| 84 } | 84 } |
| 85 | 85 |
| 86 m_stops.append(stop); | 86 m_stops.append(stop); |
| 87 m_gradient.reset(); | 87 m_gradient.clear(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void Gradient::sortStopsIfNecessary() | 90 void Gradient::sortStopsIfNecessary() |
| 91 { | 91 { |
| 92 if (m_stopsSorted) | 92 if (m_stopsSorted) |
| 93 return; | 93 return; |
| 94 | 94 |
| 95 m_stopsSorted = true; | 95 m_stopsSorted = true; |
| 96 | 96 |
| 97 if (!m_stops.size()) | 97 if (!m_stops.size()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 110 | 110 |
| 111 m_spreadMethod = spreadMethod; | 111 m_spreadMethod = spreadMethod; |
| 112 } | 112 } |
| 113 | 113 |
| 114 void Gradient::setDrawsInPMColorSpace(bool drawInPMColorSpace) | 114 void Gradient::setDrawsInPMColorSpace(bool drawInPMColorSpace) |
| 115 { | 115 { |
| 116 if (drawInPMColorSpace == m_drawInPMColorSpace) | 116 if (drawInPMColorSpace == m_drawInPMColorSpace) |
| 117 return; | 117 return; |
| 118 | 118 |
| 119 m_drawInPMColorSpace = drawInPMColorSpace; | 119 m_drawInPMColorSpace = drawInPMColorSpace; |
| 120 m_gradient.reset(); | 120 m_gradient.clear(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTra
nsformation) | 123 void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTra
nsformation) |
| 124 { | 124 { |
| 125 if (m_gradientSpaceTransformation == gradientSpaceTransformation) | 125 if (m_gradientSpaceTransformation == gradientSpaceTransformation) |
| 126 return; | 126 return; |
| 127 | 127 |
| 128 m_gradientSpaceTransformation = gradientSpaceTransformation; | 128 m_gradientSpaceTransformation = gradientSpaceTransformation; |
| 129 m_gradient.reset(); | 129 m_gradient.clear(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Determine the total number of stops needed, including pseudo-stops at the | 132 // Determine the total number of stops needed, including pseudo-stops at the |
| 133 // ends as necessary. | 133 // ends as necessary. |
| 134 static size_t totalStopsNeeded(const Gradient::ColorStop* stopData, size_t count
) | 134 static size_t totalStopsNeeded(const Gradient::ColorStop* stopData, size_t count
) |
| 135 { | 135 { |
| 136 // N.B.: The tests in this function should kept in sync with the ones in | 136 // N.B.: The tests in this function should kept in sync with the ones in |
| 137 // fillStops(), or badness happens. | 137 // fillStops(), or badness happens. |
| 138 const Gradient::ColorStop* stop = stopData; | 138 const Gradient::ColorStop* stop = stopData; |
| 139 size_t countUsed = count; | 139 size_t countUsed = count; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 } | 182 } |
| 183 | 183 |
| 184 // Copy the last stop to 1.0 if needed. See comment above about this float | 184 // Copy the last stop to 1.0 if needed. See comment above about this float |
| 185 // comparison. | 185 // comparison. |
| 186 if (count < 1 || (--stop)->stop < 1.0) { | 186 if (count < 1 || (--stop)->stop < 1.0) { |
| 187 pos[start + count] = WebCoreFloatToSkScalar(1.0); | 187 pos[start + count] = WebCoreFloatToSkScalar(1.0); |
| 188 colors[start + count] = colors[start + count - 1]; | 188 colors[start + count] = colors[start + count - 1]; |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 | 191 |
| 192 sk_sp<SkShader> Gradient::shader() | 192 SkShader* Gradient::shader() |
| 193 { | 193 { |
| 194 if (m_gradient) | 194 if (m_gradient) |
| 195 return m_gradient; | 195 return m_gradient.get(); |
| 196 | 196 |
| 197 sortStopsIfNecessary(); | 197 sortStopsIfNecessary(); |
| 198 ASSERT(m_stopsSorted); | 198 ASSERT(m_stopsSorted); |
| 199 | 199 |
| 200 size_t countUsed = totalStopsNeeded(m_stops.data(), m_stops.size()); | 200 size_t countUsed = totalStopsNeeded(m_stops.data(), m_stops.size()); |
| 201 ASSERT(countUsed >= 2); | 201 ASSERT(countUsed >= 2); |
| 202 ASSERT(countUsed >= m_stops.size()); | 202 ASSERT(countUsed >= m_stops.size()); |
| 203 | 203 |
| 204 ColorStopOffsetVector pos(countUsed); | 204 ColorStopOffsetVector pos(countUsed); |
| 205 ColorStopColorVector colors(countUsed); | 205 ColorStopColorVector colors(countUsed); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 226 m_gradientSpaceTransformation.translate(m_p0.x(), m_p0.y()); | 226 m_gradientSpaceTransformation.translate(m_p0.x(), m_p0.y()); |
| 227 m_gradientSpaceTransformation.scale(1, 1 / aspectRatio()); | 227 m_gradientSpaceTransformation.scale(1, 1 / aspectRatio()); |
| 228 m_gradientSpaceTransformation.translate(-m_p0.x(), -m_p0.y()); | 228 m_gradientSpaceTransformation.translate(-m_p0.x(), -m_p0.y()); |
| 229 ASSERT(m_p0 == m_p1); | 229 ASSERT(m_p0 == m_p1); |
| 230 } | 230 } |
| 231 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); | 231 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); |
| 232 | 232 |
| 233 // Since the two-point radial gradient is slower than the plain radial, | 233 // Since the two-point radial gradient is slower than the plain radial, |
| 234 // only use it if we have to. | 234 // only use it if we have to. |
| 235 if (m_p0 == m_p1 && m_r0 <= 0.0f) { | 235 if (m_p0 == m_p1 && m_r0 <= 0.0f) { |
| 236 m_gradient = SkGradientShader::MakeRadial(m_p1.data(), m_r1, colors.
data(), pos.data(), static_cast<int>(countUsed), tile, shouldDrawInPMColorSpace,
&localMatrix); | 236 m_gradient = adoptRef(SkGradientShader::CreateRadial(m_p1.data(), m_
r1, colors.data(), pos.data(), static_cast<int>(countUsed), tile, shouldDrawInPM
ColorSpace, &localMatrix)); |
| 237 } else { | 237 } else { |
| 238 // The radii we give to Skia must be positive. If we're given a | 238 // The radii we give to Skia must be positive. If we're given a |
| 239 // negative radius, ask for zero instead. | 239 // negative radius, ask for zero instead. |
| 240 SkScalar radius0 = m_r0 >= 0.0f ? WebCoreFloatToSkScalar(m_r0) : 0; | 240 SkScalar radius0 = m_r0 >= 0.0f ? WebCoreFloatToSkScalar(m_r0) : 0; |
| 241 SkScalar radius1 = m_r1 >= 0.0f ? WebCoreFloatToSkScalar(m_r1) : 0; | 241 SkScalar radius1 = m_r1 >= 0.0f ? WebCoreFloatToSkScalar(m_r1) : 0; |
| 242 m_gradient = SkGradientShader::MakeTwoPointConical(m_p0.data(), radi
us0, m_p1.data(), radius1, colors.data(), pos.data(), static_cast<int>(countUsed
), tile, shouldDrawInPMColorSpace, &localMatrix); | 242 m_gradient = adoptRef(SkGradientShader::CreateTwoPointConical(m_p0.d
ata(), radius0, m_p1.data(), radius1, colors.data(), pos.data(), static_cast<int
>(countUsed), tile, shouldDrawInPMColorSpace, &localMatrix)); |
| 243 } | 243 } |
| 244 } else { | 244 } else { |
| 245 SkPoint pts[2] = { m_p0.data(), m_p1.data() }; | 245 SkPoint pts[2] = { m_p0.data(), m_p1.data() }; |
| 246 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); | 246 SkMatrix localMatrix = affineTransformToSkMatrix(m_gradientSpaceTransfor
mation); |
| 247 m_gradient = SkGradientShader::MakeLinear(pts, colors.data(), pos.data()
, static_cast<int>(countUsed), tile, shouldDrawInPMColorSpace, &localMatrix); | 247 m_gradient = adoptRef(SkGradientShader::CreateLinear(pts, colors.data(),
pos.data(), static_cast<int>(countUsed), tile, shouldDrawInPMColorSpace, &local
Matrix)); |
| 248 } | 248 } |
| 249 | 249 |
| 250 if (!m_gradient) { | 250 if (!m_gradient) { |
| 251 // use last color, since our "geometry" was degenerate (e.g. radius==0) | 251 // use last color, since our "geometry" was degenerate (e.g. radius==0) |
| 252 m_gradient = SkShader::MakeColorShader(colors[countUsed - 1]); | 252 m_gradient = adoptRef(SkShader::CreateColorShader(colors[countUsed - 1])
); |
| 253 } | 253 } |
| 254 return m_gradient; | 254 return m_gradient.get(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 void Gradient::applyToPaint(SkPaint& paint) | 257 void Gradient::applyToPaint(SkPaint& paint) |
| 258 { | 258 { |
| 259 paint.setShader(shader()); | 259 paint.setShader(shader()); |
| 260 | 260 |
| 261 // Legacy behavior: gradients are always dithered. | 261 // Legacy behavior: gradients are always dithered. |
| 262 paint.setDither(true); | 262 paint.setDither(true); |
| 263 } | 263 } |
| 264 } // namespace blink | 264 } // namespace blink |
| OLD | NEW |