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

Side by Side Diff: Source/platform/graphics/GraphicsContextState.h

Issue 169283008: Maintain SkPaint in GraphicsContextState. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Ready for review. Created 6 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (C) 2013 Google Inc. All rights reserved. 1 // Copyright (C) 2013 Google Inc. All rights reserved.
2 // 2 //
3 // Redistribution and use in source and binary forms, with or without 3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are 4 // modification, are permitted provided that the following conditions are
5 // met: 5 // met:
6 // 6 //
7 // * Redistributions of source code must retain the above copyright 7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer. 8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above 9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer 10 // copyright notice, this list of conditions and the following disclaimer
(...skipping 11 matching lines...) Expand all
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 28
29 #ifndef GraphicsContextState_h 29 #ifndef GraphicsContextState_h
30 #define GraphicsContextState_h 30 #define GraphicsContextState_h
31 31
32 #include "platform/graphics/DrawLooperBuilder.h"
32 #include "platform/graphics/Gradient.h" 33 #include "platform/graphics/Gradient.h"
33 #include "platform/graphics/GraphicsTypes.h" 34 #include "platform/graphics/GraphicsTypes.h"
34 #include "platform/graphics/Path.h" 35 #include "platform/graphics/Path.h"
35 #include "platform/graphics/Pattern.h" 36 #include "platform/graphics/Pattern.h"
36 #include "platform/graphics/StrokeData.h" 37 #include "platform/graphics/StrokeData.h"
38 #include "platform/graphics/skia/SkiaUtils.h"
37 #include "third_party/skia/include/core/SkColorFilter.h" 39 #include "third_party/skia/include/core/SkColorFilter.h"
38 #include "third_party/skia/include/core/SkColorPriv.h" 40 #include "third_party/skia/include/core/SkPaint.h"
39 #include "third_party/skia/include/core/SkDrawLooper.h"
40 #include "third_party/skia/include/effects/SkDashPathEffect.h"
41 #include "wtf/PassOwnPtr.h" 41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/RefPtr.h" 42 #include "wtf/RefPtr.h"
43 43
44 namespace WebCore { 44 namespace WebCore {
45 45
46 // Encapsulates the state information we store for each pushed graphics state. 46 // Encapsulates the state information we store for each pushed graphics state.
47 // Only GraphicsContext can use this class. 47 // Only GraphicsContext can use this class.
48 class GraphicsContextState { 48 class GraphicsContextState final {
Stephen White 2014/03/04 15:24:13 Unless I missed something, we can't use C++11 in B
49 WTF_MAKE_NONCOPYABLE(GraphicsContextState); 49 WTF_MAKE_NONCOPYABLE(GraphicsContextState);
50 private: 50 public:
51 friend class GraphicsContext;
52
53 static PassOwnPtr<GraphicsContextState> create() { return adoptPtr(new Graph icsContextState()); } 51 static PassOwnPtr<GraphicsContextState> create() { return adoptPtr(new Graph icsContextState()); }
54 52
55 GraphicsContextState() 53 GraphicsContextState();
56 : m_fillColor(Color::black) 54
57 , m_fillRule(RULE_NONZERO) 55 void copy(GraphicsContextState*);
58 , m_textDrawingMode(TextModeFill) 56
59 , m_alpha(1) 57 // SkPaint objects that reflect the current state, except:
60 , m_xferMode(nullptr) 58 // - Dashed and Dotted strokes.
61 , m_compositeOperator(CompositeSourceOver) 59 const SkPaint& strokePaint() const;
62 , m_blendMode(blink::WebBlendModeNormal) 60 const SkPaint& fillPaint() const;
63 #if USE(LOW_QUALITY_IMAGE_INTERPOLATION) 61
64 , m_interpolationQuality(InterpolationLow) 62 uint16_t saveCount() const { return m_saveCount; }
65 #else 63 void incrementSaveCount() { ++m_saveCount; }
66 , m_interpolationQuality(InterpolationHigh) 64 void decrementSaveCount() { --m_saveCount; }
67 #endif 65
68 , m_saveCount(0) 66 // Stroke data
69 , m_shouldAntialias(true) 67 const StrokeData& strokeData() const { return m_strokeData; }
70 , m_shouldSmoothFonts(true) 68
71 , m_shouldClampToSourceRect(true) 69 // Does not set up the necessary path effect for dotted or dashed lines.
Stephen White 2014/03/04 15:24:13 Why the exception? Performance?
Stephen Chennney 2014/03/04 17:41:29 We need the length of the path to correctly set up
70 // StrokeData::setupPaintDashPathEffect must be called if the dash effect is
71 // required.
72 void setStrokeStyle(const StrokeStyle style) { m_strokeData.setStyle(style); }
73
74 void setStrokeThickness(const float);
75
76 SkColor effectiveStrokeColor() const { return applyAlpha(m_strokeData.color( ).rgb()); }
77 void setStrokeColor(const Color&);
78
79 void setStrokeGradient(const PassRefPtr<Gradient>);
80 void clearStrokeGradient();
81
82 void setStrokePattern(const PassRefPtr<Pattern>);
83 void clearStrokePattern();
84
85 void setLineCap(const LineCap);
86
87 void setLineJoin(const LineJoin);
88
89 void setMiterLimit(const float);
90
91 void setLineDash(const DashArray& dashes, float dashOffset)
72 { 92 {
93 m_strokeData.setLineDash(dashes, dashOffset);
73 } 94 }
74 95
75 void copy(GraphicsContextState* source) 96 // Fill data
76 { 97 Color fillColor() const { return m_fillColor; }
77 m_strokeData = source->m_strokeData; 98 SkColor effectiveFillColor() const { return applyAlpha(m_fillColor.rgb()); }
78 m_fillColor = source->m_fillColor; 99 void setFillColor(const Color&);
79 m_fillRule = source->m_fillRule;
80 m_fillGradient = source->m_fillGradient;
81 m_fillPattern = source->m_fillPattern;
82 m_looper = source->m_looper;
83 m_textDrawingMode = source->m_textDrawingMode;
84 m_alpha = source->m_alpha;
85 m_xferMode = source->m_xferMode;
86 m_colorFilter = source->m_colorFilter;
87 m_compositeOperator = source->m_compositeOperator;
88 m_blendMode = source->m_blendMode;
89 m_interpolationQuality = source->m_interpolationQuality;
90 m_saveCount = 0;
91 m_shouldAntialias = source->m_shouldAntialias;
92 m_shouldSmoothFonts = source->m_shouldSmoothFonts;
93 m_shouldClampToSourceRect = source->m_shouldClampToSourceRect;
94 }
95 100
101 Gradient* fillGradient() const { return m_fillGradient.get(); }
102 void setFillGradient(const PassRefPtr<Gradient>);
103 void clearFillGradient();
104
105 Pattern* fillPattern() const { return m_fillPattern.get(); }
106 void setFillPattern(const PassRefPtr<Pattern>);
107 void clearFillPattern();
108
109 // Path fill rule
110 WindRule fillRule() const { return m_fillRule; }
111 void setFillRule(const WindRule rule) { m_fillRule = rule; }
112
113 // Shadow. (This will need tweaking if we use draw loopers for other things. )
114 SkDrawLooper* drawLooper() const { return m_looper.get(); }
115 void setDrawLooper(PassRefPtr<SkDrawLooper>);
116 void clearDrawLooper();
117
118 // Text. (See TextModeFill & friends.)
119 TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
120 void setTextDrawingMode(const TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
121
122 // Common shader state.
123 int alpha() const { return m_alpha; }
124 void setAlpha(const float);
Stephen White 2014/03/04 15:24:13 This is confusing (int 0->256 vs float 0->1, I ass
Stephen Chennney 2014/03/04 17:41:29 Fair call. The 256 is a Skia thing, due to the way
125
126 SkColorFilter* colorFilter() const { return m_colorFilter.get(); }
127 void setColorFilter(PassRefPtr<SkColorFilter>);
128
129 // Compositing control, for the CSS and Canvas compositing spec.
130 void setCompositeOperation(const CompositeOperator, const blink::WebBlendMod e);
131 CompositeOperator compositeOperator() const { return m_compositeOperator; }
132 blink::WebBlendMode blendMode() const { return m_blendMode; }
133 SkXfermode* xferMode() const { return m_xferMode.get(); }
134
135 // Image interpolation control.
136 InterpolationQuality interpolationQuality() const { return m_interpolationQu ality; }
137 void setInterpolationQuality(const InterpolationQuality);
138
139 bool shouldAntialias() const { return m_shouldAntialias; }
140 void setShouldAntialias(const bool);
141
142 bool shouldSmoothFonts() const { return m_shouldSmoothFonts; }
143 void setShouldSmoothFonts(const bool shouldSmoothFonts) { m_shouldSmoothFont s = shouldSmoothFonts; }
144
145 bool shouldClampToSourceRect() const { return m_shouldClampToSourceRect; }
146 void setShouldClampToSourceRect(const bool shouldClampToSourceRect) { m_shou ldClampToSourceRect = shouldClampToSourceRect; }
147
148 private:
96 // Helper function for applying the state's alpha value to the given input 149 // Helper function for applying the state's alpha value to the given input
97 // color to produce a new output color. 150 // color to produce a new output color.
98 SkColor applyAlpha(SkColor c) const 151 SkColor applyAlpha(SkColor c) const
99 { 152 {
100 int s = roundf(m_alpha * 256); 153 int a = SkAlphaMul(SkColorGetA(c), m_alpha);
101 if (s >= 256)
102 return c;
103 if (s < 0)
104 return 0;
105
106 int a = SkAlphaMul(SkColorGetA(c), s);
107 return (c & 0x00FFFFFF) | (a << 24); 154 return (c & 0x00FFFFFF) | (a << 24);
108 } 155 }
109 156
110 // Stroke. 157 // These are mutbale to enable gradient updates when the paints are fetched for use.
Stephen White 2014/03/04 15:24:13 Hmm.. if they're mutable, than won't that prevent
Stephen Chennney 2014/03/04 17:41:29 Yes. The fact that canvas gradients are mutable af
158 mutable SkPaint m_strokePaint;
159 mutable SkPaint m_fillPaint;
160
111 StrokeData m_strokeData; 161 StrokeData m_strokeData;
112 162
113 // Fill.
114 Color m_fillColor; 163 Color m_fillColor;
115 WindRule m_fillRule; 164 WindRule m_fillRule;
116 RefPtr<Gradient> m_fillGradient; 165 RefPtr<Gradient> m_fillGradient;
117 RefPtr<Pattern> m_fillPattern; 166 RefPtr<Pattern> m_fillPattern;
118 167
119 // Shadow. (This will need tweaking if we use draw loopers for other things. )
120 RefPtr<SkDrawLooper> m_looper; 168 RefPtr<SkDrawLooper> m_looper;
121 169
122 // Text. (See TextModeFill & friends.)
123 TextDrawingModeFlags m_textDrawingMode; 170 TextDrawingModeFlags m_textDrawingMode;
124 171
125 // Common shader state. 172 int m_alpha;
126 float m_alpha;
127 RefPtr<SkXfermode> m_xferMode; 173 RefPtr<SkXfermode> m_xferMode;
128 RefPtr<SkColorFilter> m_colorFilter; 174 RefPtr<SkColorFilter> m_colorFilter;
129 175
130 // Compositing control, for the CSS and Canvas compositing spec.
131 CompositeOperator m_compositeOperator; 176 CompositeOperator m_compositeOperator;
132 blink::WebBlendMode m_blendMode; 177 blink::WebBlendMode m_blendMode;
133 178
134 // Image interpolation control.
135 InterpolationQuality m_interpolationQuality; 179 InterpolationQuality m_interpolationQuality;
136 180
137 // The number of GraphicsContext::save calls since this state was pushed.
138 uint16_t m_saveCount; 181 uint16_t m_saveCount;
139 182
140 bool m_shouldAntialias : 1; 183 bool m_shouldAntialias : 1;
141 bool m_shouldSmoothFonts : 1; 184 bool m_shouldSmoothFonts : 1;
142 bool m_shouldClampToSourceRect : 1; 185 bool m_shouldClampToSourceRect : 1;
143 }; 186 };
144 187
145 } // namespace WebCore 188 } // namespace WebCore
146 189
147 #endif // GraphicsContextState_h 190 #endif // GraphicsContextState_h
148
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698