Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/Gradient.cpp

Issue 2787113002: Initial conic-gradient() implementation (Closed)
Patch Set: silence msvc warning Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 SkShader::TileMode tileMode, 185 SkShader::TileMode tileMode,
186 uint32_t flags, 186 uint32_t flags,
187 const SkMatrix& localMatrix) const override { 187 const SkMatrix& localMatrix) const override {
188 SkPoint pts[2] = {m_p0.data(), m_p1.data()}; 188 SkPoint pts[2] = {m_p0.data(), m_p1.data()};
189 return SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), 189 return SkGradientShader::MakeLinear(pts, colors.data(), pos.data(),
190 static_cast<int>(colors.size()), 190 static_cast<int>(colors.size()),
191 tileMode, flags, &localMatrix); 191 tileMode, flags, &localMatrix);
192 } 192 }
193 193
194 private: 194 private:
195 FloatPoint m_p0; 195 const FloatPoint m_p0;
196 FloatPoint m_p1; 196 const FloatPoint m_p1;
197 }; 197 };
198 198
199 class RadialGradient final : public Gradient { 199 class RadialGradient final : public Gradient {
200 public: 200 public:
201 RadialGradient(const FloatPoint& p0, 201 RadialGradient(const FloatPoint& p0,
202 float r0, 202 float r0,
203 const FloatPoint& p1, 203 const FloatPoint& p1,
204 float r1, 204 float r1,
205 float aspectRatio, 205 float aspectRatio,
206 GradientSpreadMethod spreadMethod, 206 GradientSpreadMethod spreadMethod,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 // The radii we give to Skia must be positive. If we're given a 239 // The radii we give to Skia must be positive. If we're given a
240 // negative radius, ask for zero instead. 240 // negative radius, ask for zero instead.
241 const SkScalar radius0 = std::max(WebCoreFloatToSkScalar(m_r0), 0.0f); 241 const SkScalar radius0 = std::max(WebCoreFloatToSkScalar(m_r0), 0.0f);
242 const SkScalar radius1 = std::max(WebCoreFloatToSkScalar(m_r1), 0.0f); 242 const SkScalar radius1 = std::max(WebCoreFloatToSkScalar(m_r1), 0.0f);
243 return SkGradientShader::MakeTwoPointConical( 243 return SkGradientShader::MakeTwoPointConical(
244 m_p0.data(), radius0, m_p1.data(), radius1, colors.data(), pos.data(), 244 m_p0.data(), radius0, m_p1.data(), radius1, colors.data(), pos.data(),
245 static_cast<int>(colors.size()), tileMode, flags, adjustedLocalMatrix); 245 static_cast<int>(colors.size()), tileMode, flags, adjustedLocalMatrix);
246 } 246 }
247 247
248 private: 248 private:
249 FloatPoint m_p0; 249 const FloatPoint m_p0;
250 FloatPoint m_p1; 250 const FloatPoint m_p1;
251 float m_r0; 251 const float m_r0;
252 float m_r1; 252 const float m_r1;
253 float m_aspectRatio; // For elliptical gradient, width / height. 253 const float m_aspectRatio; // For elliptical gradient, width / height.
254 };
255
256 class ConicGradient final : public Gradient {
257 public:
258 ConicGradient(const FloatPoint& position,
259 float angle,
260 GradientSpreadMethod spreadMethod,
261 ColorInterpolation interpolation)
262 : Gradient(Type::Conic, spreadMethod, interpolation),
263 m_position(position),
264 m_angle(angle) {}
265
266 protected:
267 sk_sp<SkShader> createShader(const ColorBuffer& colors,
268 const OffsetBuffer& pos,
269 SkShader::TileMode tileMode,
270 uint32_t flags,
271 const SkMatrix& localMatrix) const override {
272 if (tileMode != SkShader::kClamp_TileMode) {
273 // TODO(fmalita): kRepeat support
274 return nullptr;
275 }
276
277 // Skia's sweep gradient angles are relative to the x-axis, not the y-axis.
278 const float skiaAngle = m_angle - 90;
279 SkTCopyOnFirstWrite<SkMatrix> adjustedLocalMatrix(localMatrix);
280 if (skiaAngle) {
281 adjustedLocalMatrix.writable()->preRotate(skiaAngle, m_position.x(),
282 m_position.y());
283 }
284
285 return SkGradientShader::MakeSweep(
286 m_position.x(), m_position.y(), colors.data(), pos.data(),
287 static_cast<int>(colors.size()), flags, adjustedLocalMatrix);
288 }
289
290 private:
291 const FloatPoint m_position;
292 const float m_angle;
254 }; 293 };
255 294
256 } // anonymous ns 295 } // anonymous ns
257 296
258 PassRefPtr<Gradient> Gradient::createLinear(const FloatPoint& p0, 297 PassRefPtr<Gradient> Gradient::createLinear(const FloatPoint& p0,
259 const FloatPoint& p1, 298 const FloatPoint& p1,
260 GradientSpreadMethod spreadMethod, 299 GradientSpreadMethod spreadMethod,
261 ColorInterpolation interpolation) { 300 ColorInterpolation interpolation) {
262 return adoptRef(new LinearGradient(p0, p1, spreadMethod, interpolation)); 301 return adoptRef(new LinearGradient(p0, p1, spreadMethod, interpolation));
263 } 302 }
264 303
265 PassRefPtr<Gradient> Gradient::createRadial(const FloatPoint& p0, 304 PassRefPtr<Gradient> Gradient::createRadial(const FloatPoint& p0,
266 float r0, 305 float r0,
267 const FloatPoint& p1, 306 const FloatPoint& p1,
268 float r1, 307 float r1,
269 float aspectRatio, 308 float aspectRatio,
270 GradientSpreadMethod spreadMethod, 309 GradientSpreadMethod spreadMethod,
271 ColorInterpolation interpolation) { 310 ColorInterpolation interpolation) {
272 return adoptRef(new RadialGradient(p0, r0, p1, r1, aspectRatio, spreadMethod, 311 return adoptRef(new RadialGradient(p0, r0, p1, r1, aspectRatio, spreadMethod,
273 interpolation)); 312 interpolation));
274 } 313 }
275 314
315 PassRefPtr<Gradient> Gradient::createConic(const FloatPoint& position,
316 float angle,
317 GradientSpreadMethod spreadMethod,
318 ColorInterpolation interpolation) {
319 return adoptRef(
320 new ConicGradient(position, angle, spreadMethod, interpolation));
321 }
322
276 } // namespace blink 323 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698