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 "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"
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 {
50 static PassOwnPtr<GraphicsContextState> create()
52 return adoptPtr(new GraphicsContextState());
55 static PassOwnPtr<GraphicsContextState> createAndCopy(const GraphicsContextState& other)
57 return adoptPtr(new GraphicsContextState(other));
60 void copy(const GraphicsContextState&);
62 // SkPaint objects that reflect the current state. If the length of the
63 // path to be stroked is known, pass it in for correct dash or dot placement.
64 const SkPaint& strokePaint(int strokedPathLength = 0) const;
65 const SkPaint& fillPaint() const;
67 uint16_t saveCount() const { return m_saveCount; }
68 void incrementSaveCount() { ++m_saveCount; }
69 void decrementSaveCount() { --m_saveCount; }
72 const StrokeData& strokeData() const { return m_strokeData; }
74 void setStrokeStyle(StrokeStyle);
76 void setStrokeThickness(float);
78 SkColor effectiveStrokeColor() const { return applyAlpha(m_strokeData.color().rgb()); }
79 void setStrokeColor(const Color&);
81 void setStrokeGradient(const PassRefPtr<Gradient>);
82 void clearStrokeGradient();
84 void setStrokePattern(const PassRefPtr<Pattern>);
85 void clearStrokePattern();
87 void setLineCap(LineCap);
89 void setLineJoin(LineJoin);
91 void setMiterLimit(float);
93 void setLineDash(const DashArray&, float);
96 Color fillColor() const { return m_fillColor; }
97 SkColor effectiveFillColor() const { return applyAlpha(m_fillColor.rgb()); }
98 void setFillColor(const Color&);
100 Gradient* fillGradient() const { return m_fillGradient.get(); }
101 void setFillGradient(const PassRefPtr<Gradient>);
102 void clearFillGradient();
104 Pattern* fillPattern() const { return m_fillPattern.get(); }
105 void setFillPattern(const PassRefPtr<Pattern>);
106 void clearFillPattern();
109 WindRule fillRule() const { return m_fillRule; }
110 void setFillRule(WindRule rule) { m_fillRule = rule; }
112 // Shadow. (This will need tweaking if we use draw loopers for other things.)
113 SkDrawLooper* drawLooper() const { return m_looper.get(); }
114 void setDrawLooper(PassRefPtr<SkDrawLooper>);
115 void clearDrawLooper();
117 // Text. (See TextModeFill & friends.)
118 TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
119 void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
121 // Common shader state.
122 int alpha() const { return m_alpha; }
123 void setAlphaAsFloat(float);
125 SkColorFilter* colorFilter() const { return m_colorFilter.get(); }
126 void setColorFilter(PassRefPtr<SkColorFilter>);
128 // Compositing control, for the CSS and Canvas compositing spec.
129 void setCompositeOperation(CompositeOperator, WebBlendMode);
130 CompositeOperator compositeOperator() const { return m_compositeOperator; }
131 WebBlendMode blendMode() const { return m_blendMode; }
132 SkXfermode* xferMode() const { return m_xferMode.get(); }
134 // Image interpolation control.
135 InterpolationQuality interpolationQuality() const { return m_interpolationQuality; }
136 void setInterpolationQuality(InterpolationQuality);
138 bool shouldAntialias() const { return m_shouldAntialias; }
139 void setShouldAntialias(bool);
141 bool shouldSmoothFonts() const { return m_shouldSmoothFonts; }
142 void setShouldSmoothFonts(bool shouldSmoothFonts) { m_shouldSmoothFonts = shouldSmoothFonts; }
144 bool shouldClampToSourceRect() const { return m_shouldClampToSourceRect; }
145 void setShouldClampToSourceRect(bool shouldClampToSourceRect) { m_shouldClampToSourceRect = shouldClampToSourceRect; }
148 GraphicsContextState();
149 explicit GraphicsContextState(const GraphicsContextState&);
150 GraphicsContextState& operator=(const GraphicsContextState&);
152 // Helper function for applying the state's alpha value to the given input
153 // color to produce a new output color.
154 SkColor applyAlpha(SkColor c) const
156 int a = SkAlphaMul(SkColorGetA(c), m_alpha);
157 return (c & 0x00FFFFFF) | (a << 24);
160 // These are mutbale to enable gradient updates when the paints are fetched for use.
161 mutable SkPaint m_strokePaint;
162 mutable SkPaint m_fillPaint;
164 StrokeData m_strokeData;
168 RefPtr<Gradient> m_fillGradient;
169 RefPtr<Pattern> m_fillPattern;
171 RefPtr<SkDrawLooper> m_looper;
173 TextDrawingModeFlags m_textDrawingMode;
176 RefPtr<SkXfermode> m_xferMode;
177 RefPtr<SkColorFilter> m_colorFilter;
179 CompositeOperator m_compositeOperator;
180 WebBlendMode m_blendMode;
182 InterpolationQuality m_interpolationQuality;
184 uint16_t m_saveCount;
186 bool m_shouldAntialias : 1;
187 bool m_shouldSmoothFonts : 1;
188 bool m_shouldClampToSourceRect : 1;
193 #endif // GraphicsContextState_h