Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsContextState.cpp
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.
4
5 #include "config.h"
6 #include "platform/graphics/GraphicsContextState.h"
7
8 #include "platform/graphics/skia/SkiaUtils.h"
9
10 namespace blink {
11
12 GraphicsContextState::GraphicsContextState()
13     : m_strokeColor(Color::black)
14     , m_fillColor(Color::black)
15     , m_fillRule(RULE_NONZERO)
16     , m_textDrawingMode(TextModeFill)
17     , m_alpha(256)
18     , m_compositeOperator(CompositeSourceOver)
19     , m_blendMode(WebBlendModeNormal)
20     , m_interpolationQuality(InterpolationDefault)
21     , m_saveCount(0)
22     , m_shouldAntialias(true)
23     , m_shouldClampToSourceRect(true)
24 {
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);
36 }
37
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)
57     , m_saveCount(0)
58     , m_shouldAntialias(other.m_shouldAntialias)
59     , m_shouldClampToSourceRect(other.m_shouldClampToSourceRect) { }
60
61 void GraphicsContextState::copy(const GraphicsContextState& source)
62 {
63     this->~GraphicsContextState();
64     new (this) GraphicsContextState(source);
65 }
66
67 const SkPaint& GraphicsContextState::strokePaint(int strokedPathLength) const
68 {
69     if (m_strokeGradient && m_strokeGradient->shaderChanged())
70         m_strokePaint.setShader(m_strokeGradient->shader());
71     m_strokeData.setupPaintDashPathEffect(&m_strokePaint, strokedPathLength);
72     return m_strokePaint;
73 }
74
75 const SkPaint& GraphicsContextState::fillPaint() const
76 {
77     if (m_fillGradient && m_fillGradient->shaderChanged())
78         m_fillPaint.setShader(m_fillGradient->shader());
79     return m_fillPaint;
80 }
81
82 void GraphicsContextState::setStrokeStyle(StrokeStyle style)
83 {
84     m_strokeData.setStyle(style);
85 }
86
87 void GraphicsContextState::setStrokeThickness(float thickness)
88 {
89     m_strokeData.setThickness(thickness);
90     m_strokePaint.setStrokeWidth(SkFloatToScalar(thickness));
91 }
92
93 void GraphicsContextState::setStrokeColor(const Color& color)
94 {
95     m_strokeGradient.clear();
96     m_strokePattern.clear();
97     m_strokeColor = color;
98     m_strokePaint.setColor(applyAlpha(color.rgb()));
99     m_strokePaint.setShader(0);
100 }
101
102 void GraphicsContextState::setStrokeGradient(const PassRefPtr<Gradient> gradient)
103 {
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());
109 }
110
111 void GraphicsContextState::clearStrokeGradient()
112 {
113     m_strokeGradient.clear();
114     ASSERT(!m_strokePattern);
115     m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
116 }
117
118 void GraphicsContextState::setStrokePattern(const PassRefPtr<Pattern> pattern)
119 {
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());
125 }
126
127 void GraphicsContextState::clearStrokePattern()
128 {
129     m_strokePattern.clear();
130     ASSERT(!m_strokeGradient);
131     m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
132 }
133
134 void GraphicsContextState::setLineCap(LineCap cap)
135 {
136     m_strokeData.setLineCap(cap);
137     m_strokePaint.setStrokeCap((SkPaint::Cap)cap);
138 }
139
140 void GraphicsContextState::setLineJoin(LineJoin join)
141 {
142     m_strokeData.setLineJoin(join);
143     m_strokePaint.setStrokeJoin((SkPaint::Join)join);
144 }
145
146 void GraphicsContextState::setMiterLimit(float miterLimit)
147 {
148     m_strokeData.setMiterLimit(miterLimit);
149     m_strokePaint.setStrokeMiter(SkFloatToScalar(miterLimit));
150 }
151
152 void GraphicsContextState::setFillColor(const Color& color)
153 {
154     m_fillColor = color;
155     m_fillGradient.clear();
156     m_fillPattern.clear();
157     m_fillPaint.setColor(applyAlpha(color.rgb()));
158     m_fillPaint.setShader(0);
159 }
160
161 void GraphicsContextState::setFillGradient(const PassRefPtr<Gradient> gradient)
162 {
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());
168 }
169
170 void GraphicsContextState::clearFillGradient()
171 {
172     m_fillGradient.clear();
173     ASSERT(!m_fillPattern);
174     m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
175 }
176
177 void GraphicsContextState::setFillPattern(const PassRefPtr<Pattern> pattern)
178 {
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());
184 }
185
186 void GraphicsContextState::clearFillPattern()
187 {
188     m_fillPattern.clear();
189     ASSERT(!m_fillGradient);
190     m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
191 }
192
193 // Shadow. (This will need tweaking if we use draw loopers for other things.)
194 void GraphicsContextState::setDrawLooper(PassRefPtr<SkDrawLooper> drawLooper)
195 {
196     m_looper = drawLooper;
197     m_strokePaint.setLooper(m_looper.get());
198     m_fillPaint.setLooper(m_looper.get());
199 }
200
201 void GraphicsContextState::clearDrawLooper()
202 {
203     m_looper.clear();
204     m_strokePaint.setLooper(0);
205     m_fillPaint.setLooper(0);
206 }
207
208 void GraphicsContextState::setDropShadowImageFilter(PassRefPtr<SkImageFilter> dropShadowImageFilter)
209 {
210     m_dropShadowImageFilter = dropShadowImageFilter;
211 }
212
213 void GraphicsContextState::clearDropShadowImageFilter()
214 {
215     m_dropShadowImageFilter.clear();
216 }
217
218 void GraphicsContextState::setAlphaAsFloat(float alpha)
219 {
220     if (alpha < 0) {
221         m_alpha = 0;
222     } else {
223         m_alpha = roundf(alpha * 256);
224         if (m_alpha > 256)
225             m_alpha = 256;
226     }
227     m_strokePaint.setColor(applyAlpha(m_strokeColor.rgb()));
228     m_fillPaint.setColor(applyAlpha(m_fillColor.rgb()));
229 }
230
231 void GraphicsContextState::setLineDash(const DashArray& dashes, float dashOffset)
232 {
233     m_strokeData.setLineDash(dashes, dashOffset);
234 }
235
236 void GraphicsContextState::setColorFilter(PassRefPtr<SkColorFilter> colorFilter)
237 {
238     m_colorFilter = colorFilter;
239     m_strokePaint.setColorFilter(m_colorFilter.get());
240     m_fillPaint.setColorFilter(m_colorFilter.get());
241 }
242
243 void GraphicsContextState::setCompositeOperation(CompositeOperator compositeOperation, WebBlendMode blendMode)
244 {
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);
250 }
251
252 void GraphicsContextState::setInterpolationQuality(InterpolationQuality quality)
253 {
254     m_interpolationQuality = quality;
255     m_strokePaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(quality));
256     m_fillPaint.setFilterLevel(WebCoreInterpolationQualityToSkFilterLevel(quality));
257 }
258
259 void GraphicsContextState::setShouldAntialias(bool shouldAntialias)
260 {
261     m_shouldAntialias = shouldAntialias;
262     m_strokePaint.setAntiAlias(shouldAntialias);
263     m_fillPaint.setAntiAlias(shouldAntialias);
264 }
265
266
267 } // namespace blink