1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "platform/graphics/GraphicsContextState.h"
8 #include "platform/graphics/skia/SkiaUtils.h"
12 GraphicsContextState::GraphicsContextState()
13 : m_strokeColor(Color::black)
14 , m_fillColor(Color::black)
15 , m_fillRule(RULE_NONZERO)
16 , m_textDrawingMode(TextModeFill)
18 , m_compositeOperator(CompositeSourceOver)
19 , m_blendMode(WebBlendModeNormal)
20 , m_interpolationQuality(InterpolationDefault)
22 , m_shouldAntialias(true)
23 , m_shouldClampToSourceRect(true)
25 m_strokePaint.setStyle(SkPaint::kStroke_Style);
26 m_strokePaint.setStrokeWidth(SkFloatToScalar(m_strokeData.thickness()));
27 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
28 m_strokePaint.setStrokeCap(SkPaint::kDefault_Cap);
29 m_strokePaint.setStrokeJoin(SkPaint::kDefault_Join);
30 m_strokePaint.setStrokeMiter(SkFloatToScalar(m_strokeData.miterLimit()));
31 m_strokePaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(m_interpolationQuality));
32 m_strokePaint.setAntiAlias(m_shouldAntialias);
33 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
34 m_fillPaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(m_interpolationQuality));
35 m_fillPaint.setAntiAlias(m_shouldAntialias);
38 GraphicsContextState::GraphicsContextState(const GraphicsContextState& other)
39 : m_strokePaint(other.m_strokePaint)
40 , m_fillPaint(other.m_fillPaint)
41 , m_strokeData(other.m_strokeData)
42 , m_strokeColor(other.m_strokeColor)
43 , m_strokeGradient(other.m_strokeGradient)
44 , m_strokePattern(other.m_strokePattern)
45 , m_fillColor(other.m_fillColor)
46 , m_fillRule(other.m_fillRule)
47 , m_fillGradient(other.m_fillGradient)
48 , m_fillPattern(other.m_fillPattern)
49 , m_looper(other.m_looper)
50 , m_textDrawingMode(other.m_textDrawingMode)
51 , m_alpha(other.m_alpha)
52 , m_colorFilter(other.m_colorFilter)
53 , m_compositeOperator(other.m_compositeOperator)
54 , m_blendMode(other.m_blendMode)
55 , m_interpolationQuality(other.m_interpolationQuality)
57 , m_shouldAntialias(other.m_shouldAntialias)
58 , m_shouldClampToSourceRect(other.m_shouldClampToSourceRect) { }
60 void GraphicsContextState::copy(const GraphicsContextState& source)
62 this->~GraphicsContextState();
63 new (this) GraphicsContextState(source);
66 const SkPaint& GraphicsContextState::strokePaint(int strokedPathLength) const
68 if (m_strokeGradient && m_strokeGradient->shaderChanged())
69 m_strokePaint.setShader(m_strokeGradient->shader());
70 m_strokeData.setupPaintDashPathEffect(&m_strokePaint, strokedPathLength);
74 const SkPaint& GraphicsContextState::fillPaint() const
76 if (m_fillGradient && m_fillGradient->shaderChanged())
77 m_fillPaint.setShader(m_fillGradient->shader());
81 void GraphicsContextState::setStrokeStyle(StrokeStyle style)
83 m_strokeData.setStyle(style);
86 void GraphicsContextState::setStrokeThickness(float thickness)
88 m_strokeData.setThickness(thickness);
89 m_strokePaint.setStrokeWidth(SkFloatToScalar(thickness));
92 void GraphicsContextState::setStrokeColor(const Color& color)
94 m_strokeGradient.clear();
95 m_strokePattern.clear();
96 m_strokeColor = color;
97 m_strokePaint.setColor(applyAlpha(color.rgb()));
98 m_strokePaint.setShader(0);
101 void GraphicsContextState::setStrokeGradient(const PassRefPtr<Gradient> gradient)
103 m_strokeColor = Color::black;
104 m_strokePattern.clear();
105 m_strokeGradient = gradient;
106 m_strokePaint.setColor(applyAlpha(SK_ColorBLACK));
107 m_strokePaint.setShader(m_strokeGradient->shader());
110 void GraphicsContextState::clearStrokeGradient()
112 m_strokeGradient.clear();
113 ASSERT(!m_strokePattern);
114 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
117 void GraphicsContextState::setStrokePattern(const PassRefPtr<Pattern> pattern)
119 m_strokeColor = Color::black;
120 m_strokeGradient.clear();
121 m_strokePattern = pattern;
122 m_strokePaint.setColor(applyAlpha(SK_ColorBLACK));
123 m_strokePaint.setShader(m_strokePattern->shader());
126 void GraphicsContextState::clearStrokePattern()
128 m_strokePattern.clear();
129 ASSERT(!m_strokeGradient);
130 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
133 void GraphicsContextState::setLineCap(LineCap cap)
135 m_strokeData.setLineCap(cap);
136 m_strokePaint.setStrokeCap((SkPaint::Cap)cap);
139 void GraphicsContextState::setLineJoin(LineJoin join)
141 m_strokeData.setLineJoin(join);
142 m_strokePaint.setStrokeJoin((SkPaint::Join)join);
145 void GraphicsContextState::setMiterLimit(float miterLimit)
147 m_strokeData.setMiterLimit(miterLimit);
148 m_strokePaint.setStrokeMiter(SkFloatToScalar(miterLimit));
151 void GraphicsContextState::setFillColor(const Color& color)
154 m_fillGradient.clear();
155 m_fillPattern.clear();
156 m_fillPaint.setColor(applyAlpha(color.rgb()));
157 m_fillPaint.setShader(0);
160 void GraphicsContextState::setFillGradient(const PassRefPtr<Gradient> gradient)
162 m_fillColor = Color::black;
163 m_fillPattern.clear();
164 m_fillGradient = gradient;
165 m_fillPaint.setColor(applyAlpha(SK_ColorBLACK));
166 m_fillPaint.setShader(m_fillGradient->shader());
169 void GraphicsContextState::clearFillGradient()
171 m_fillGradient.clear();
172 ASSERT(!m_fillPattern);
173 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
176 void GraphicsContextState::setFillPattern(const PassRefPtr<Pattern> pattern)
178 m_fillColor = Color::black;
179 m_fillGradient.clear();
180 m_fillPattern = pattern;
181 m_fillPaint.setColor(applyAlpha(SK_ColorBLACK));
182 m_fillPaint.setShader(m_fillPattern->shader());
185 void GraphicsContextState::clearFillPattern()
187 m_fillPattern.clear();
188 ASSERT(!m_fillGradient);
189 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
192 // Shadow. (This will need tweaking if we use draw loopers for other things.)
193 void GraphicsContextState::setDrawLooper(PassRefPtr<SkDrawLooper> drawLooper)
195 m_looper = drawLooper;
196 m_strokePaint.setLooper(m_looper.get());
197 m_fillPaint.setLooper(m_looper.get());
200 void GraphicsContextState::clearDrawLooper()
203 m_strokePaint.setLooper(0);
204 m_fillPaint.setLooper(0);
207 void GraphicsContextState::setAlphaAsFloat(float alpha)
212 m_alpha = roundf(alpha * 256);
216 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
217 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
220 void GraphicsContextState::setLineDash(const DashArray& dashes, float dashOffset)
222 m_strokeData.setLineDash(dashes, dashOffset);
225 void GraphicsContextState::setColorFilter(PassRefPtr<SkColorFilter> colorFilter)
227 m_colorFilter = colorFilter;
228 m_strokePaint.setColorFilter(m_colorFilter.get());
229 m_fillPaint.setColorFilter(m_colorFilter.get());
232 void GraphicsContextState::setCompositeOperation(CompositeOperator compositeOperation, WebBlendMode blendMode)
234 m_compositeOperator = compositeOperation;
235 m_blendMode = blendMode;
236 SkXfermode::Mode xferMode = WebCoreCompositeToSkiaComposite(compositeOperation, blendMode);
237 m_strokePaint.setXfermodeMode(xferMode);
238 m_fillPaint.setXfermodeMode(xferMode);
241 void GraphicsContextState::setInterpolationQuality(InterpolationQuality quality)
243 m_interpolationQuality = quality;
244 m_strokePaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(quality));
245 m_fillPaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(quality));
248 void GraphicsContextState::setShouldAntialias(bool shouldAntialias)
250 m_shouldAntialias = shouldAntialias;
251 m_strokePaint.setAntiAlias(shouldAntialias);
252 m_fillPaint.setAntiAlias(shouldAntialias);