1 // Copyright (C) 2013 Google Inc. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef GraphicsContextState_h
30 #define GraphicsContextState_h
32 #include "platform/graphics/DrawLooperBuilder.h"
33 #include "platform/graphics/Gradient.h"
34 #include "platform/graphics/GraphicsTypes.h"
35 #include "platform/graphics/Path.h"
36 #include "platform/graphics/Pattern.h"
37 #include "platform/graphics/StrokeData.h"
38 #include "third_party/skia/include/core/SkColorFilter.h"
39 #include "third_party/skia/include/core/SkPaint.h"
40 #include "wtf/PassOwnPtr.h"
41 #include "wtf/RefPtr.h"
45 // Encapsulates the state information we store for each pushed graphics state.
46 // Only GraphicsContext can use this class.
47 class PLATFORM_EXPORT GraphicsContextState FINAL {
49 static PassOwnPtr<GraphicsContextState> create()
51 return adoptPtr(new GraphicsContextState());
54 static PassOwnPtr<GraphicsContextState> createAndCopy(const GraphicsContextState& other)
56 return adoptPtr(new GraphicsContextState(other));
59 void copy(const GraphicsContextState&);
61 // SkPaint objects that reflect the current state. If the length of the
62 // path to be stroked is known, pass it in for correct dash or dot placement.
63 const SkPaint& strokePaint(int strokedPathLength = 0) const;
64 const SkPaint& fillPaint() const;
66 uint16_t saveCount() const { return m_saveCount; }
67 void incrementSaveCount() { ++m_saveCount; }
68 void decrementSaveCount() { --m_saveCount; }
71 Color strokeColor() const { return m_strokeColor; }
72 SkColor effectiveStrokeColor() const { return applyAlpha(m_strokeColor.rgb()); }
73 void setStrokeColor(const Color&);
75 Gradient* strokeGradient() const { return m_strokeGradient.get(); }
76 void setStrokeGradient(const PassRefPtr<Gradient>);
77 void clearStrokeGradient();
79 Pattern* strokePattern() const { return m_strokePattern.get(); }
80 void setStrokePattern(const PassRefPtr<Pattern>);
81 void clearStrokePattern();
83 const StrokeData& strokeData() const { return m_strokeData; }
84 void setStrokeStyle(StrokeStyle);
85 void setStrokeThickness(float);
86 void setLineCap(LineCap);
87 void setLineJoin(LineJoin);
88 void setMiterLimit(float);
89 void setLineDash(const DashArray&, float);
92 Color fillColor() const { return m_fillColor; }
93 SkColor effectiveFillColor() const { return applyAlpha(m_fillColor.rgb()); }
94 void setFillColor(const Color&);
96 Gradient* fillGradient() const { return m_fillGradient.get(); }
97 void setFillGradient(const PassRefPtr<Gradient>);
98 void clearFillGradient();
100 Pattern* fillPattern() const { return m_fillPattern.get(); }
101 void setFillPattern(const PassRefPtr<Pattern>);
102 void clearFillPattern();
105 WindRule fillRule() const { return m_fillRule; }
106 void setFillRule(WindRule rule) { m_fillRule = rule; }
108 // Shadow. (This will need tweaking if we use draw loopers for other things.)
109 SkDrawLooper* drawLooper() const { return m_looper.get(); }
110 void setDrawLooper(PassRefPtr<SkDrawLooper>);
111 void clearDrawLooper();
113 // Text. (See TextModeFill & friends.)
114 TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
115 void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
117 // Common shader state.
118 int alpha() const { return m_alpha; }
119 void setAlphaAsFloat(float);
121 SkColorFilter* colorFilter() const { return m_colorFilter.get(); }
122 void setColorFilter(PassRefPtr<SkColorFilter>);
124 // Compositing control, for the CSS and Canvas compositing spec.
125 void setCompositeOperation(CompositeOperator, WebBlendMode);
126 CompositeOperator compositeOperator() const { return m_compositeOperator; }
127 WebBlendMode blendMode() const { return m_blendMode; }
129 // Image interpolation control.
130 InterpolationQuality interpolationQuality() const { return m_interpolationQuality; }
131 void setInterpolationQuality(InterpolationQuality);
133 bool shouldAntialias() const { return m_shouldAntialias; }
134 void setShouldAntialias(bool);
136 bool shouldClampToSourceRect() const { return m_shouldClampToSourceRect; }
137 void setShouldClampToSourceRect(bool shouldClampToSourceRect) { m_shouldClampToSourceRect = shouldClampToSourceRect; }
140 GraphicsContextState();
141 explicit GraphicsContextState(const GraphicsContextState&);
142 GraphicsContextState& operator=(const GraphicsContextState&);
144 // Helper function for applying the state's alpha value to the given input
145 // color to produce a new output color.
146 SkColor applyAlpha(SkColor c) const
148 int a = SkAlphaMul(SkColorGetA(c), m_alpha);
149 return (c & 0x00FFFFFF) | (a << 24);
152 // These are mutbale to enable gradient updates when the paints are fetched for use.
153 mutable SkPaint m_strokePaint;
154 mutable SkPaint m_fillPaint;
156 StrokeData m_strokeData;
159 RefPtr<Gradient> m_strokeGradient;
160 RefPtr<Pattern> m_strokePattern;
164 RefPtr<Gradient> m_fillGradient;
165 RefPtr<Pattern> m_fillPattern;
167 RefPtr<SkDrawLooper> m_looper;
169 TextDrawingModeFlags m_textDrawingMode;
172 RefPtr<SkColorFilter> m_colorFilter;
174 CompositeOperator m_compositeOperator;
175 WebBlendMode m_blendMode;
177 InterpolationQuality m_interpolationQuality;
179 uint16_t m_saveCount;
181 bool m_shouldAntialias : 1;
182 bool m_shouldClampToSourceRect : 1;
187 #endif // GraphicsContextState_h