Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsContextState.h
1 // Copyright (C) 2013 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
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
12 // distribution.
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.
16 //
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.
28
29 #ifndef GraphicsContextState_h
30 #define GraphicsContextState_h
31
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 "platform/graphics/skia/SkiaUtils.h"
39 #include "third_party/skia/include/core/SkColorFilter.h"
40 #include "third_party/skia/include/core/SkPaint.h"
41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/RefPtr.h"
43
44 namespace WebCore {
45
46 // Encapsulates the state information we store for each pushed graphics state.
47 // Only GraphicsContext can use this class.
48 class PLATFORM_EXPORT GraphicsContextState FINAL {
49     WTF_MAKE_NONCOPYABLE(GraphicsContextState);
50 public:
51     static PassOwnPtr<GraphicsContextState> create()
52     {
53         return adoptPtr(new GraphicsContextState());
54     }
55
56     void copy(GraphicsContextState*);
57
58     // SkPaint objects that reflect the current state. If the length of the
59     // path to be stroked is known, pass it in for correct dash or dot placement.
60     const SkPaint& strokePaint(int strokedPathLength = 0) const;
61     const SkPaint& fillPaint() const;
62
63     uint16_t saveCount() const { return m_saveCount; }
64     void incrementSaveCount() { ++m_saveCount; }
65     void decrementSaveCount() { --m_saveCount; }
66
67     // Stroke data
68     const StrokeData& strokeData() const { return m_strokeData; }
69
70     void setStrokeStyle(StrokeStyle);
71
72     void setStrokeThickness(float);
73
74     SkColor effectiveStrokeColor() const { return applyAlpha(m_strokeData.color().rgb()); }
75     void setStrokeColor(const Color&);
76
77     void setStrokeGradient(const PassRefPtr<Gradient>);
78     void clearStrokeGradient();
79
80     void setStrokePattern(const PassRefPtr<Pattern>);
81     void clearStrokePattern();
82
83     void setLineCap(LineCap);
84
85     void setLineJoin(LineJoin);
86
87     void setMiterLimit(float);
88
89     void setLineDash(const DashArray&, float);
90
91     // Fill data
92     Color fillColor() const { return m_fillColor; }
93     SkColor effectiveFillColor() const { return applyAlpha(m_fillColor.rgb()); }
94     void setFillColor(const Color&);
95
96     Gradient* fillGradient() const { return m_fillGradient.get(); }
97     void setFillGradient(const PassRefPtr<Gradient>);
98     void clearFillGradient();
99
100     Pattern* fillPattern() const { return m_fillPattern.get(); }
101     void setFillPattern(const PassRefPtr<Pattern>);
102     void clearFillPattern();
103
104     // Path fill rule
105     WindRule fillRule() const { return m_fillRule; }
106     void setFillRule(WindRule rule) { m_fillRule = rule; }
107
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();
112
113     // Text. (See TextModeFill & friends.)
114     TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
115     void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
116
117     // Common shader state.
118     int alpha() const { return m_alpha; }
119     void setAlphaAsFloat(float);
120
121     SkColorFilter* colorFilter() const { return m_colorFilter.get(); }
122     void setColorFilter(PassRefPtr<SkColorFilter>);
123
124     // Compositing control, for the CSS and Canvas compositing spec.
125     void setCompositeOperation(CompositeOperator, blink::WebBlendMode);
126     CompositeOperator compositeOperator() const { return m_compositeOperator; }
127     blink::WebBlendMode blendMode() const { return m_blendMode; }
128     SkXfermode* xferMode() const { return m_xferMode.get(); }
129
130     // Image interpolation control.
131     InterpolationQuality interpolationQuality() const { return m_interpolationQuality; }
132     void setInterpolationQuality(InterpolationQuality);
133
134     bool shouldAntialias() const { return m_shouldAntialias; }
135     void setShouldAntialias(bool);
136
137     bool shouldSmoothFonts() const { return m_shouldSmoothFonts; }
138     void setShouldSmoothFonts(bool shouldSmoothFonts) { m_shouldSmoothFonts = shouldSmoothFonts; }
139
140     bool shouldClampToSourceRect() const { return m_shouldClampToSourceRect; }
141     void setShouldClampToSourceRect(bool shouldClampToSourceRect) { m_shouldClampToSourceRect = shouldClampToSourceRect; }
142
143 private:
144     GraphicsContextState();
145
146     // Helper function for applying the state's alpha value to the given input
147     // color to produce a new output color.
148     SkColor applyAlpha(SkColor c) const
149     {
150         int a = SkAlphaMul(SkColorGetA(c), m_alpha);
151         return (c & 0x00FFFFFF) | (a << 24);
152     }
153
154     // These are mutbale to enable gradient updates when the paints are fetched for use.
155     mutable SkPaint m_strokePaint;
156     mutable SkPaint m_fillPaint;
157
158     StrokeData m_strokeData;
159
160     Color m_fillColor;
161     WindRule m_fillRule;
162     RefPtr<Gradient> m_fillGradient;
163     RefPtr<Pattern> m_fillPattern;
164
165     RefPtr<SkDrawLooper> m_looper;
166
167     TextDrawingModeFlags m_textDrawingMode;
168
169     int m_alpha;
170     RefPtr<SkXfermode> m_xferMode;
171     RefPtr<SkColorFilter> m_colorFilter;
172
173     CompositeOperator m_compositeOperator;
174     blink::WebBlendMode m_blendMode;
175
176     InterpolationQuality m_interpolationQuality;
177
178     uint16_t m_saveCount;
179
180     bool m_shouldAntialias : 1;
181     bool m_shouldSmoothFonts : 1;
182     bool m_shouldClampToSourceRect : 1;
183 };
184
185 } // namespace WebCore
186
187 #endif // GraphicsContextState_h