| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 114 |
| 115 // Copy the last stop to 1.0 if needed. See comment above about this float | 115 // Copy the last stop to 1.0 if needed. See comment above about this float |
| 116 // comparison. | 116 // comparison. |
| 117 DCHECK(!pos.IsEmpty()); | 117 DCHECK(!pos.IsEmpty()); |
| 118 if (pos.back() < 1) { | 118 if (pos.back() < 1) { |
| 119 pos.push_back(WebCoreFloatToSkScalar(1)); | 119 pos.push_back(WebCoreFloatToSkScalar(1)); |
| 120 colors.push_back(colors.back()); | 120 colors.push_back(colors.back()); |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 std::unique_ptr<PaintShader> Gradient::CreateShaderInternal( | 124 sk_sp<PaintShader> Gradient::CreateShaderInternal( |
| 125 const SkMatrix& local_matrix) { | 125 const SkMatrix& local_matrix) { |
| 126 SortStopsIfNecessary(); | 126 SortStopsIfNecessary(); |
| 127 DCHECK(stops_sorted_); | 127 DCHECK(stops_sorted_); |
| 128 | 128 |
| 129 ColorBuffer colors; | 129 ColorBuffer colors; |
| 130 colors.ReserveCapacity(stops_.size()); | 130 colors.ReserveCapacity(stops_.size()); |
| 131 OffsetBuffer pos; | 131 OffsetBuffer pos; |
| 132 pos.ReserveCapacity(stops_.size()); | 132 pos.ReserveCapacity(stops_.size()); |
| 133 | 133 |
| 134 FillSkiaStops(colors, pos); | 134 FillSkiaStops(colors, pos); |
| 135 DCHECK_GE(colors.size(), 2ul); | 135 DCHECK_GE(colors.size(), 2ul); |
| 136 DCHECK_EQ(pos.size(), colors.size()); | 136 DCHECK_EQ(pos.size(), colors.size()); |
| 137 | 137 |
| 138 SkShader::TileMode tile = SkShader::kClamp_TileMode; | 138 SkShader::TileMode tile = SkShader::kClamp_TileMode; |
| 139 switch (spread_method_) { | 139 switch (spread_method_) { |
| 140 case kSpreadMethodReflect: | 140 case kSpreadMethodReflect: |
| 141 tile = SkShader::kMirror_TileMode; | 141 tile = SkShader::kMirror_TileMode; |
| 142 break; | 142 break; |
| 143 case kSpreadMethodRepeat: | 143 case kSpreadMethodRepeat: |
| 144 tile = SkShader::kRepeat_TileMode; | 144 tile = SkShader::kRepeat_TileMode; |
| 145 break; | 145 break; |
| 146 case kSpreadMethodPad: | 146 case kSpreadMethodPad: |
| 147 tile = SkShader::kClamp_TileMode; | 147 tile = SkShader::kClamp_TileMode; |
| 148 break; | 148 break; |
| 149 } | 149 } |
| 150 | 150 |
| 151 uint32_t flags = color_interpolation_ == ColorInterpolation::kPremultiplied | 151 uint32_t flags = color_interpolation_ == ColorInterpolation::kPremultiplied |
| 152 ? SkGradientShader::kInterpolateColorsInPremul_Flag | 152 ? SkGradientShader::kInterpolateColorsInPremul_Flag |
| 153 : 0; | 153 : 0; |
| 154 std::unique_ptr<PaintShader> shader = | 154 sk_sp<SkShader> shader = CreateShader(colors, pos, tile, flags, local_matrix); |
| 155 CreateShader(colors, pos, tile, flags, local_matrix, colors.back()); | 155 if (!shader) { |
| 156 DCHECK(shader); | 156 // use last color, since our "geometry" was degenerate (e.g. radius==0) |
| 157 shader = SkShader::MakeColorShader(colors.back()); |
| 158 } |
| 157 | 159 |
| 158 return shader; | 160 return WrapSkShader(std::move(shader)); |
| 159 } | 161 } |
| 160 | 162 |
| 161 void Gradient::ApplyToFlags(PaintFlags& flags, const SkMatrix& local_matrix) { | 163 void Gradient::ApplyToFlags(PaintFlags& flags, const SkMatrix& local_matrix) { |
| 162 if (!cached_shader_ || | 164 if (!cached_shader_ || local_matrix != cached_shader_->getLocalMatrix()) |
| 163 local_matrix != cached_shader_->sk_shader()->getLocalMatrix()) { | |
| 164 cached_shader_ = CreateShaderInternal(local_matrix); | 165 cached_shader_ = CreateShaderInternal(local_matrix); |
| 165 } | |
| 166 | 166 |
| 167 flags.setShader(WTF::MakeUnique<PaintShader>(*cached_shader_)); | 167 flags.setShader(cached_shader_); |
| 168 | 168 |
| 169 // Legacy behavior: gradients are always dithered. | 169 // Legacy behavior: gradients are always dithered. |
| 170 flags.setDither(true); | 170 flags.setDither(true); |
| 171 } | 171 } |
| 172 | 172 |
| 173 namespace { | 173 namespace { |
| 174 | 174 |
| 175 class LinearGradient final : public Gradient { | 175 class LinearGradient final : public Gradient { |
| 176 public: | 176 public: |
| 177 LinearGradient(const FloatPoint& p0, | 177 LinearGradient(const FloatPoint& p0, |
| 178 const FloatPoint& p1, | 178 const FloatPoint& p1, |
| 179 GradientSpreadMethod spread_method, | 179 GradientSpreadMethod spread_method, |
| 180 ColorInterpolation interpolation) | 180 ColorInterpolation interpolation) |
| 181 : Gradient(Type::kLinear, spread_method, interpolation), | 181 : Gradient(Type::kLinear, spread_method, interpolation), |
| 182 p0_(p0), | 182 p0_(p0), |
| 183 p1_(p1) {} | 183 p1_(p1) {} |
| 184 | 184 |
| 185 protected: | 185 protected: |
| 186 std::unique_ptr<PaintShader> CreateShader( | 186 sk_sp<SkShader> CreateShader(const ColorBuffer& colors, |
| 187 const ColorBuffer& colors, | 187 const OffsetBuffer& pos, |
| 188 const OffsetBuffer& pos, | 188 SkShader::TileMode tile_mode, |
| 189 SkShader::TileMode tile_mode, | 189 uint32_t flags, |
| 190 uint32_t flags, | 190 const SkMatrix& local_matrix) const override { |
| 191 const SkMatrix& local_matrix, | |
| 192 SkColor fallback_color) const override { | |
| 193 SkPoint pts[2] = {p0_.Data(), p1_.Data()}; | 191 SkPoint pts[2] = {p0_.Data(), p1_.Data()}; |
| 194 return PaintShader::MakeLinearGradient( | 192 return SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), |
| 195 pts, colors.data(), pos.data(), static_cast<int>(colors.size()), | 193 static_cast<int>(colors.size()), |
| 196 tile_mode, flags, &local_matrix, fallback_color); | 194 tile_mode, flags, &local_matrix); |
| 197 } | 195 } |
| 198 | 196 |
| 199 private: | 197 private: |
| 200 const FloatPoint p0_; | 198 const FloatPoint p0_; |
| 201 const FloatPoint p1_; | 199 const FloatPoint p1_; |
| 202 }; | 200 }; |
| 203 | 201 |
| 204 class RadialGradient final : public Gradient { | 202 class RadialGradient final : public Gradient { |
| 205 public: | 203 public: |
| 206 RadialGradient(const FloatPoint& p0, | 204 RadialGradient(const FloatPoint& p0, |
| 207 float r0, | 205 float r0, |
| 208 const FloatPoint& p1, | 206 const FloatPoint& p1, |
| 209 float r1, | 207 float r1, |
| 210 float aspect_ratio, | 208 float aspect_ratio, |
| 211 GradientSpreadMethod spread_method, | 209 GradientSpreadMethod spread_method, |
| 212 ColorInterpolation interpolation) | 210 ColorInterpolation interpolation) |
| 213 : Gradient(Type::kRadial, spread_method, interpolation), | 211 : Gradient(Type::kRadial, spread_method, interpolation), |
| 214 p0_(p0), | 212 p0_(p0), |
| 215 p1_(p1), | 213 p1_(p1), |
| 216 r0_(r0), | 214 r0_(r0), |
| 217 r1_(r1), | 215 r1_(r1), |
| 218 aspect_ratio_(aspect_ratio) {} | 216 aspect_ratio_(aspect_ratio) {} |
| 219 | 217 |
| 220 protected: | 218 protected: |
| 221 std::unique_ptr<PaintShader> CreateShader( | 219 sk_sp<SkShader> CreateShader(const ColorBuffer& colors, |
| 222 const ColorBuffer& colors, | 220 const OffsetBuffer& pos, |
| 223 const OffsetBuffer& pos, | 221 SkShader::TileMode tile_mode, |
| 224 SkShader::TileMode tile_mode, | 222 uint32_t flags, |
| 225 uint32_t flags, | 223 const SkMatrix& local_matrix) const override { |
| 226 const SkMatrix& local_matrix, | |
| 227 SkColor fallback_color) const override { | |
| 228 SkTCopyOnFirstWrite<SkMatrix> adjusted_local_matrix(local_matrix); | 224 SkTCopyOnFirstWrite<SkMatrix> adjusted_local_matrix(local_matrix); |
| 229 if (aspect_ratio_ != 1) { | 225 if (aspect_ratio_ != 1) { |
| 230 // CSS3 elliptical gradients: apply the elliptical scaling at the | 226 // CSS3 elliptical gradients: apply the elliptical scaling at the |
| 231 // gradient center point. | 227 // gradient center point. |
| 232 DCHECK(p0_ == p1_); | 228 DCHECK(p0_ == p1_); |
| 233 adjusted_local_matrix.writable()->preScale(1, 1 / aspect_ratio_, p0_.X(), | 229 adjusted_local_matrix.writable()->preScale(1, 1 / aspect_ratio_, p0_.X(), |
| 234 p0_.Y()); | 230 p0_.Y()); |
| 235 } | 231 } |
| 236 | 232 |
| 237 // 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, |
| 238 // only use it if we have to. | 234 // only use it if we have to. |
| 239 if (p0_ == p1_ && r0_ <= 0.0f) { | 235 if (p0_ == p1_ && r0_ <= 0.0f) { |
| 240 return PaintShader::MakeRadialGradient( | 236 return SkGradientShader::MakeRadial( |
| 241 p1_.Data(), r1_, colors.data(), pos.data(), | 237 p1_.Data(), r1_, colors.data(), pos.data(), |
| 242 static_cast<int>(colors.size()), tile_mode, flags, | 238 static_cast<int>(colors.size()), tile_mode, flags, |
| 243 adjusted_local_matrix, fallback_color); | 239 adjusted_local_matrix); |
| 244 } | 240 } |
| 245 | 241 |
| 246 // The radii we give to Skia must be positive. If we're given a | 242 // The radii we give to Skia must be positive. If we're given a |
| 247 // negative radius, ask for zero instead. | 243 // negative radius, ask for zero instead. |
| 248 const SkScalar radius0 = std::max(WebCoreFloatToSkScalar(r0_), 0.0f); | 244 const SkScalar radius0 = std::max(WebCoreFloatToSkScalar(r0_), 0.0f); |
| 249 const SkScalar radius1 = std::max(WebCoreFloatToSkScalar(r1_), 0.0f); | 245 const SkScalar radius1 = std::max(WebCoreFloatToSkScalar(r1_), 0.0f); |
| 250 return PaintShader::MakeTwoPointConicalGradient( | 246 return SkGradientShader::MakeTwoPointConical( |
| 251 p0_.Data(), radius0, p1_.Data(), radius1, colors.data(), pos.data(), | 247 p0_.Data(), radius0, p1_.Data(), radius1, colors.data(), pos.data(), |
| 252 static_cast<int>(colors.size()), tile_mode, flags, | 248 static_cast<int>(colors.size()), tile_mode, flags, |
| 253 adjusted_local_matrix, fallback_color); | 249 adjusted_local_matrix); |
| 254 } | 250 } |
| 255 | 251 |
| 256 private: | 252 private: |
| 257 const FloatPoint p0_; | 253 const FloatPoint p0_; |
| 258 const FloatPoint p1_; | 254 const FloatPoint p1_; |
| 259 const float r0_; | 255 const float r0_; |
| 260 const float r1_; | 256 const float r1_; |
| 261 const float aspect_ratio_; // For elliptical gradient, width / height. | 257 const float aspect_ratio_; // For elliptical gradient, width / height. |
| 262 }; | 258 }; |
| 263 | 259 |
| 264 class ConicGradient final : public Gradient { | 260 class ConicGradient final : public Gradient { |
| 265 public: | 261 public: |
| 266 ConicGradient(const FloatPoint& position, | 262 ConicGradient(const FloatPoint& position, |
| 267 float angle, | 263 float angle, |
| 268 ColorInterpolation interpolation) | 264 ColorInterpolation interpolation) |
| 269 : Gradient(Type::kConic, kSpreadMethodPad, interpolation), | 265 : Gradient(Type::kConic, kSpreadMethodPad, interpolation), |
| 270 position_(position), | 266 position_(position), |
| 271 angle_(angle) {} | 267 angle_(angle) {} |
| 272 | 268 |
| 273 protected: | 269 protected: |
| 274 std::unique_ptr<PaintShader> CreateShader( | 270 sk_sp<SkShader> CreateShader(const ColorBuffer& colors, |
| 275 const ColorBuffer& colors, | 271 const OffsetBuffer& pos, |
| 276 const OffsetBuffer& pos, | 272 SkShader::TileMode tile_mode, |
| 277 SkShader::TileMode tile_mode, | 273 uint32_t flags, |
| 278 uint32_t flags, | 274 const SkMatrix& local_matrix) const override { |
| 279 const SkMatrix& local_matrix, | |
| 280 SkColor fallback_color) const override { | |
| 281 DCHECK_NE(tile_mode, SkShader::kMirror_TileMode); | 275 DCHECK_NE(tile_mode, SkShader::kMirror_TileMode); |
| 282 | 276 |
| 283 // Skia's sweep gradient angles are relative to the x-axis, not the y-axis. | 277 // Skia's sweep gradient angles are relative to the x-axis, not the y-axis. |
| 284 const float skia_angle = angle_ - 90; | 278 const float skia_angle = angle_ - 90; |
| 285 SkTCopyOnFirstWrite<SkMatrix> adjusted_local_matrix(local_matrix); | 279 SkTCopyOnFirstWrite<SkMatrix> adjusted_local_matrix(local_matrix); |
| 286 if (skia_angle) { | 280 if (skia_angle) { |
| 287 adjusted_local_matrix.writable()->preRotate(skia_angle, position_.X(), | 281 adjusted_local_matrix.writable()->preRotate(skia_angle, position_.X(), |
| 288 position_.Y()); | 282 position_.Y()); |
| 289 } | 283 } |
| 290 | 284 |
| 291 return PaintShader::MakeSweepGradient( | 285 return SkGradientShader::MakeSweep( |
| 292 position_.X(), position_.Y(), colors.data(), pos.data(), | 286 position_.X(), position_.Y(), colors.data(), pos.data(), |
| 293 static_cast<int>(colors.size()), flags, adjusted_local_matrix, | 287 static_cast<int>(colors.size()), flags, adjusted_local_matrix); |
| 294 fallback_color); | |
| 295 } | 288 } |
| 296 | 289 |
| 297 private: | 290 private: |
| 298 const FloatPoint position_; | 291 const FloatPoint position_; |
| 299 const float angle_; | 292 const float angle_; |
| 300 }; | 293 }; |
| 301 | 294 |
| 302 } // anonymous ns | 295 } // anonymous ns |
| 303 | 296 |
| 304 PassRefPtr<Gradient> Gradient::CreateLinear(const FloatPoint& p0, | 297 PassRefPtr<Gradient> Gradient::CreateLinear(const FloatPoint& p0, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 319 spread_method, interpolation)); | 312 spread_method, interpolation)); |
| 320 } | 313 } |
| 321 | 314 |
| 322 PassRefPtr<Gradient> Gradient::CreateConic(const FloatPoint& position, | 315 PassRefPtr<Gradient> Gradient::CreateConic(const FloatPoint& position, |
| 323 float angle, | 316 float angle, |
| 324 ColorInterpolation interpolation) { | 317 ColorInterpolation interpolation) { |
| 325 return AdoptRef(new ConicGradient(position, angle, interpolation)); | 318 return AdoptRef(new ConicGradient(position, angle, interpolation)); |
| 326 } | 319 } |
| 327 | 320 |
| 328 } // namespace blink | 321 } // namespace blink |
| OLD | NEW |