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_dropShadowImageFilter(other.m_dropShadowImageFilter)
51 , m_textDrawingMode(other.m_textDrawingMode)
52 , m_alpha(other.m_alpha)
53 , m_colorFilter(other.m_colorFilter)
54 , m_compositeOperator(other.m_compositeOperator)
55 , m_blendMode(other.m_blendMode)
56 , m_interpolationQuality(other.m_interpolationQuality)
58 , m_shouldAntialias(other.m_shouldAntialias)
59 , m_shouldClampToSourceRect(other.m_shouldClampToSourceRect) { }
61 void GraphicsContextState::copy(const GraphicsContextState& source)
63 this->~GraphicsContextState();
64 new (this) GraphicsContextState(source);
67 const SkPaint& GraphicsContextState::strokePaint(int strokedPathLength) const
69 if (m_strokeGradient && m_strokeGradient->shaderChanged())
70 m_strokePaint.setShader(m_strokeGradient->shader());
71 m_strokeData.setupPaintDashPathEffect(&m_strokePaint, strokedPathLength);
75 const SkPaint& GraphicsContextState::fillPaint() const
77 if (m_fillGradient && m_fillGradient->shaderChanged())
78 m_fillPaint.setShader(m_fillGradient->shader());
82 void GraphicsContextState::setStrokeStyle(StrokeStyle style)
84 m_strokeData.setStyle(style);
87 void GraphicsContextState::setStrokeThickness(float thickness)
89 m_strokeData.setThickness(thickness);
90 m_strokePaint.setStrokeWidth(SkFloatToScalar(thickness));
93 void GraphicsContextState::setStrokeColor(const Color& color)
95 m_strokeGradient.clear();
96 m_strokePattern.clear();
97 m_strokeColor = color;
98 m_strokePaint.setColor(applyAlpha(color.rgb()));
99 m_strokePaint.setShader(0);
102 void GraphicsContextState::setStrokeGradient(const PassRefPtr<Gradient> gradient)
104 m_strokeColor = Color::black;
105 m_strokePattern.clear();
106 m_strokeGradient = gradient;
107 m_strokePaint.setColor(applyAlpha(SK_ColorBLACK));
108 m_strokePaint.setShader(m_strokeGradient->shader());
111 void GraphicsContextState::clearStrokeGradient()
113 m_strokeGradient.clear();
114 ASSERT(!m_strokePattern);
115 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
118 void GraphicsContextState::setStrokePattern(const PassRefPtr<Pattern> pattern)
120 m_strokeColor = Color::black;
121 m_strokeGradient.clear();
122 m_strokePattern = pattern;
123 m_strokePaint.setColor(applyAlpha(SK_ColorBLACK));
124 m_strokePaint.setShader(m_strokePattern->shader());
127 void GraphicsContextState::clearStrokePattern()
129 m_strokePattern.clear();
130 ASSERT(!m_strokeGradient);
131 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
134 void GraphicsContextState::setLineCap(LineCap cap)
136 m_strokeData.setLineCap(cap);
137 m_strokePaint.setStrokeCap((SkPaint::Cap)cap);
140 void GraphicsContextState::setLineJoin(LineJoin join)
142 m_strokeData.setLineJoin(join);
143 m_strokePaint.setStrokeJoin((SkPaint::Join)join);
146 void GraphicsContextState::setMiterLimit(float miterLimit)
148 m_strokeData.setMiterLimit(miterLimit);
149 m_strokePaint.setStrokeMiter(SkFloatToScalar(miterLimit));
152 void GraphicsContextState::setFillColor(const Color& color)
155 m_fillGradient.clear();
156 m_fillPattern.clear();
157 m_fillPaint.setColor(applyAlpha(color.rgb()));
158 m_fillPaint.setShader(0);
161 void GraphicsContextState::setFillGradient(const PassRefPtr<Gradient> gradient)
163 m_fillColor = Color::black;
164 m_fillPattern.clear();
165 m_fillGradient = gradient;
166 m_fillPaint.setColor(applyAlpha(SK_ColorBLACK));
167 m_fillPaint.setShader(m_fillGradient->shader());
170 void GraphicsContextState::clearFillGradient()
172 m_fillGradient.clear();
173 ASSERT(!m_fillPattern);
174 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
177 void GraphicsContextState::setFillPattern(const PassRefPtr<Pattern> pattern)
179 m_fillColor = Color::black;
180 m_fillGradient.clear();
181 m_fillPattern = pattern;
182 m_fillPaint.setColor(applyAlpha(SK_ColorBLACK));
183 m_fillPaint.setShader(m_fillPattern->shader());
186 void GraphicsContextState::clearFillPattern()
188 m_fillPattern.clear();
189 ASSERT(!m_fillGradient);
190 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
193 // Shadow. (This will need tweaking if we use draw loopers for other things.)
194 void GraphicsContextState::setDrawLooper(PassRefPtr<SkDrawLooper> drawLooper)
196 m_looper = drawLooper;
197 m_strokePaint.setLooper(m_looper.get());
198 m_fillPaint.setLooper(m_looper.get());
201 void GraphicsContextState::clearDrawLooper()
204 m_strokePaint.setLooper(0);
205 m_fillPaint.setLooper(0);
208 void GraphicsContextState::setDropShadowImageFilter(PassRefPtr<SkImageFilter> dropShadowImageFilter)
210 m_dropShadowImageFilter = dropShadowImageFilter;
213 void GraphicsContextState::clearDropShadowImageFilter()
215 m_dropShadowImageFilter.clear();
218 void GraphicsContextState::setAlphaAsFloat(float alpha)
223 m_alpha = roundf(alpha * 256);
227 m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
228 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
231 void GraphicsContextState::setLineDash(const DashArray& dashes, float dashOffset)
233 m_strokeData.setLineDash(dashes, dashOffset);
236 void GraphicsContextState::setColorFilter(PassRefPtr<SkColorFilter> colorFilter)
238 m_colorFilter = colorFilter;
239 m_strokePaint.setColorFilter(m_colorFilter.get());
240 m_fillPaint.setColorFilter(m_colorFilter.get());
243 void GraphicsContextState::setCompositeOperation(CompositeOperator compositeOperation, WebBlendMode blendMode)
245 m_compositeOperator = compositeOperation;
246 m_blendMode = blendMode;
247 SkXfermode::Mode xferMode = WebCoreCompositeToSkiaComposite(compositeOperation, blendMode);
248 m_strokePaint.setXfermodeMode(xferMode);
249 m_fillPaint.setXfermodeMode(xferMode);
252 void GraphicsContextState::setInterpolationQuality(InterpolationQuality quality)
254 m_interpolationQuality = quality;
255 m_strokePaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(quality));
256 m_fillPaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(quality));
259 void GraphicsContextState::setShouldAntialias(bool shouldAntialias)
261 m_shouldAntialias = shouldAntialias;
262 m_strokePaint.setAntiAlias(shouldAntialias);
263 m_fillPaint.setAntiAlias(shouldAntialias);