| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2011 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) 2008 Torch Mobile, Inc. | 4 * Copyright (C) 2008 Torch Mobile, Inc. |
| 5 * Copyright (C) 2013 Google Inc. All rights reserved. | 5 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 6 * | 6 * |
| 7 * Redistribution and use in source and binary forms, with or without | 7 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions | 8 * modification, are permitted provided that the following conditions |
| 9 * are met: | 9 * are met: |
| 10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 */ | 27 */ |
| 28 | 28 |
| 29 #ifndef Gradient_h | 29 #ifndef Gradient_h |
| 30 #define Gradient_h | 30 #define Gradient_h |
| 31 | 31 |
| 32 #include "platform/PlatformExport.h" | 32 #include "platform/PlatformExport.h" |
| 33 #include "platform/geometry/FloatPoint.h" | |
| 34 #include "platform/graphics/Color.h" | 33 #include "platform/graphics/Color.h" |
| 35 #include "platform/graphics/GraphicsTypes.h" | 34 #include "platform/graphics/GraphicsTypes.h" |
| 36 #include "platform/graphics/paint/PaintFlags.h" | 35 #include "platform/graphics/paint/PaintFlags.h" |
| 37 #include "platform/graphics/paint/PaintShader.h" | 36 #include "platform/graphics/paint/PaintShader.h" |
| 38 #include "third_party/skia/include/core/SkRefCnt.h" | 37 #include "third_party/skia/include/core/SkRefCnt.h" |
| 39 #include "wtf/Noncopyable.h" | 38 #include "wtf/Noncopyable.h" |
| 40 #include "wtf/PassRefPtr.h" | 39 #include "wtf/PassRefPtr.h" |
| 41 #include "wtf/RefCounted.h" | 40 #include "wtf/RefCounted.h" |
| 42 #include "wtf/Vector.h" | 41 #include "wtf/Vector.h" |
| 43 | 42 |
| 44 class SkMatrix; | 43 class SkMatrix; |
| 44 class SkShader; |
| 45 | 45 |
| 46 namespace blink { | 46 namespace blink { |
| 47 | 47 |
| 48 class FloatPoint; |
| 49 |
| 48 class PLATFORM_EXPORT Gradient : public RefCounted<Gradient> { | 50 class PLATFORM_EXPORT Gradient : public RefCounted<Gradient> { |
| 49 WTF_MAKE_NONCOPYABLE(Gradient); | 51 WTF_MAKE_NONCOPYABLE(Gradient); |
| 50 | 52 |
| 51 public: | 53 public: |
| 54 enum class Type { Linear, Radial }; |
| 55 |
| 52 enum class ColorInterpolation { | 56 enum class ColorInterpolation { |
| 53 Premultiplied, | 57 Premultiplied, |
| 54 Unpremultiplied, | 58 Unpremultiplied, |
| 55 }; | 59 }; |
| 56 | 60 |
| 57 static PassRefPtr<Gradient> create( | 61 static PassRefPtr<Gradient> createLinear( |
| 58 const FloatPoint& p0, | 62 const FloatPoint& p0, |
| 59 const FloatPoint& p1, | 63 const FloatPoint& p1, |
| 60 GradientSpreadMethod spreadMethod = SpreadMethodPad, | 64 GradientSpreadMethod = SpreadMethodPad, |
| 61 ColorInterpolation interpolation = ColorInterpolation::Unpremultiplied) { | 65 ColorInterpolation = ColorInterpolation::Unpremultiplied); |
| 62 return adoptRef(new Gradient(p0, p1, spreadMethod, interpolation)); | 66 |
| 63 } | 67 static PassRefPtr<Gradient> createRadial( |
| 64 static PassRefPtr<Gradient> create( | |
| 65 const FloatPoint& p0, | 68 const FloatPoint& p0, |
| 66 float r0, | 69 float r0, |
| 67 const FloatPoint& p1, | 70 const FloatPoint& p1, |
| 68 float r1, | 71 float r1, |
| 69 float aspectRatio = 1, | 72 float aspectRatio = 1, |
| 70 GradientSpreadMethod spreadMethod = SpreadMethodPad, | 73 GradientSpreadMethod = SpreadMethodPad, |
| 71 ColorInterpolation interpolation = ColorInterpolation::Unpremultiplied) { | 74 ColorInterpolation = ColorInterpolation::Unpremultiplied); |
| 72 return adoptRef( | 75 |
| 73 new Gradient(p0, r0, p1, r1, aspectRatio, spreadMethod, interpolation)); | 76 virtual ~Gradient(); |
| 74 } | 77 |
| 75 ~Gradient(); | 78 Type getType() const { return m_type; } |
| 76 | 79 |
| 77 struct ColorStop { | 80 struct ColorStop { |
| 78 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | 81 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
| 79 float stop; | 82 float stop; |
| 80 Color color; | 83 Color color; |
| 81 | 84 |
| 82 ColorStop(float s, const Color& c) : stop(s), color(c) {} | 85 ColorStop(float s, const Color& c) : stop(s), color(c) {} |
| 83 }; | 86 }; |
| 84 void addColorStop(const ColorStop&); | 87 void addColorStop(const ColorStop&); |
| 85 void addColorStop(float value, const Color& color) { | 88 void addColorStop(float value, const Color& color) { |
| 86 addColorStop(ColorStop(value, color)); | 89 addColorStop(ColorStop(value, color)); |
| 87 } | 90 } |
| 88 void addColorStops(const Vector<Gradient::ColorStop>&); | 91 void addColorStops(const Vector<Gradient::ColorStop>&); |
| 89 | 92 |
| 90 bool isRadial() const { return m_radial; } | |
| 91 bool isZeroSize() const { | |
| 92 return m_p0.x() == m_p1.x() && m_p0.y() == m_p1.y() && | |
| 93 (!m_radial || m_r0 == m_r1); | |
| 94 } | |
| 95 | |
| 96 const FloatPoint& p0() const { return m_p0; } | |
| 97 const FloatPoint& p1() const { return m_p1; } | |
| 98 | |
| 99 float startRadius() const { return m_r0; } | |
| 100 float endRadius() const { return m_r1; } | |
| 101 | |
| 102 void applyToFlags(PaintFlags&, const SkMatrix& localMatrix); | 93 void applyToFlags(PaintFlags&, const SkMatrix& localMatrix); |
| 103 | 94 |
| 104 GradientSpreadMethod spreadMethod() const { return m_spreadMethod; } | 95 protected: |
| 96 Gradient(Type, GradientSpreadMethod, ColorInterpolation); |
| 97 |
| 98 using ColorBuffer = Vector<SkColor, 8>; |
| 99 using OffsetBuffer = Vector<SkScalar, 8>; |
| 100 virtual sk_sp<SkShader> createShader(const ColorBuffer&, |
| 101 const OffsetBuffer&, |
| 102 SkShader::TileMode, |
| 103 uint32_t flags, |
| 104 const SkMatrix&) const = 0; |
| 105 | 105 |
| 106 private: | 106 private: |
| 107 Gradient(const FloatPoint& p0, | 107 sk_sp<PaintShader> createShaderInternal(const SkMatrix& localMatrix); |
| 108 const FloatPoint& p1, | |
| 109 GradientSpreadMethod, | |
| 110 ColorInterpolation); | |
| 111 Gradient(const FloatPoint& p0, | |
| 112 float r0, | |
| 113 const FloatPoint& p1, | |
| 114 float r1, | |
| 115 float aspectRatio, | |
| 116 GradientSpreadMethod, | |
| 117 ColorInterpolation); | |
| 118 | |
| 119 sk_sp<PaintShader> createShader(const SkMatrix& localMatrix); | |
| 120 | 108 |
| 121 void sortStopsIfNecessary(); | 109 void sortStopsIfNecessary(); |
| 110 void fillSkiaStops(ColorBuffer&, OffsetBuffer&) const; |
| 122 | 111 |
| 123 FloatPoint m_p0; | 112 const Type m_type; |
| 124 FloatPoint m_p1; | 113 const GradientSpreadMethod m_spreadMethod; |
| 125 float m_r0; | 114 const ColorInterpolation m_colorInterpolation; |
| 126 float m_r1; | 115 |
| 127 float m_aspectRatio; // For elliptical gradient, width / height. | |
| 128 Vector<ColorStop, 2> m_stops; | 116 Vector<ColorStop, 2> m_stops; |
| 129 bool m_radial; | |
| 130 bool m_stopsSorted; | 117 bool m_stopsSorted; |
| 131 GradientSpreadMethod m_spreadMethod; | |
| 132 ColorInterpolation m_colorInterpolation; | |
| 133 | 118 |
| 134 mutable sk_sp<PaintShader> m_cachedShader; | 119 mutable sk_sp<PaintShader> m_cachedShader; |
| 135 }; | 120 }; |
| 136 | 121 |
| 137 } // namespace blink | 122 } // namespace blink |
| 138 | 123 |
| 139 #endif | 124 #endif |
| OLD | NEW |