OLD | NEW |
(Empty) | |
| 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 namespace WebCore { |
| 9 |
| 10 GraphicsContextState::GraphicsContextState() |
| 11 : m_fillColor(Color::black) |
| 12 , m_fillRule(RULE_NONZERO) |
| 13 , m_textDrawingMode(TextModeFill) |
| 14 , m_alpha(256) |
| 15 , m_xferMode(nullptr) |
| 16 , m_compositeOperator(CompositeSourceOver) |
| 17 , m_blendMode(blink::WebBlendModeNormal) |
| 18 #if USE(LOW_QUALITY_IMAGE_INTERPOLATION) |
| 19 , m_interpolationQuality(InterpolationLow) |
| 20 #else |
| 21 , m_interpolationQuality(InterpolationHigh) |
| 22 #endif |
| 23 , m_saveCount(0) |
| 24 , m_shouldAntialias(true) |
| 25 , m_shouldSmoothFonts(true) |
| 26 , m_shouldClampToSourceRect(true) |
| 27 { |
| 28 m_strokePaint.setStyle(SkPaint::kStroke_Style); |
| 29 m_strokePaint.setStrokeWidth(SkFloatToScalar(m_strokeData.thickness())); |
| 30 m_strokePaint.setColor(applyAlpha(m_strokeData.color().rgb())); |
| 31 m_strokePaint.setStrokeCap(SkPaint::kDefault_Cap); |
| 32 m_strokePaint.setStrokeJoin(SkPaint::kDefault_Join); |
| 33 m_strokePaint.setStrokeMiter(SkFloatToScalar(m_strokeData.miterLimit())); |
| 34 m_strokePaint.setFilterBitmap(m_interpolationQuality != InterpolationNone); |
| 35 m_strokePaint.setAntiAlias(m_shouldAntialias); |
| 36 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb())); |
| 37 m_fillPaint.setFilterBitmap(m_interpolationQuality != InterpolationNone); |
| 38 m_fillPaint.setAntiAlias(m_shouldAntialias); |
| 39 } |
| 40 |
| 41 void GraphicsContextState::copy(GraphicsContextState* source) |
| 42 { |
| 43 m_strokePaint = source->m_strokePaint; |
| 44 m_fillPaint = source->m_fillPaint; |
| 45 m_strokeData = source->m_strokeData; |
| 46 m_fillColor = source->m_fillColor; |
| 47 m_fillRule = source->m_fillRule; |
| 48 m_fillGradient = source->m_fillGradient; |
| 49 m_fillPattern = source->m_fillPattern; |
| 50 m_looper = source->m_looper; |
| 51 m_textDrawingMode = source->m_textDrawingMode; |
| 52 m_alpha = source->m_alpha; |
| 53 m_xferMode = source->m_xferMode; |
| 54 m_colorFilter = source->m_colorFilter; |
| 55 m_compositeOperator = source->m_compositeOperator; |
| 56 m_blendMode = source->m_blendMode; |
| 57 m_interpolationQuality = source->m_interpolationQuality; |
| 58 m_saveCount = 0; |
| 59 m_shouldAntialias = source->m_shouldAntialias; |
| 60 m_shouldSmoothFonts = source->m_shouldSmoothFonts; |
| 61 m_shouldClampToSourceRect = source->m_shouldClampToSourceRect; |
| 62 } |
| 63 |
| 64 const SkPaint& GraphicsContextState::strokePaint() const |
| 65 { |
| 66 if (m_strokeData.gradient() && m_strokeData.gradient()->shaderChanged()) |
| 67 m_strokePaint.setShader(m_strokeData.gradient()->shader()); |
| 68 return m_strokePaint; |
| 69 } |
| 70 |
| 71 const SkPaint& GraphicsContextState::fillPaint() const |
| 72 { |
| 73 if (m_fillGradient && m_fillGradient->shaderChanged()) |
| 74 m_fillPaint.setShader(m_fillGradient->shader()); |
| 75 return m_fillPaint; |
| 76 } |
| 77 |
| 78 void GraphicsContextState::setStrokeStyle(const StrokeStyle style) |
| 79 { |
| 80 m_strokeData.setStyle(style); |
| 81 m_strokeData.setupPaintDashPathEffect(&m_strokePaint, 0); |
| 82 } |
| 83 |
| 84 void GraphicsContextState::setStrokeThickness(const float thickness) |
| 85 { |
| 86 m_strokeData.setThickness(thickness); |
| 87 m_strokePaint.setStrokeWidth(SkFloatToScalar(thickness)); |
| 88 m_strokeData.setupPaintDashPathEffect(&m_strokePaint, 0); |
| 89 } |
| 90 |
| 91 void GraphicsContextState::setStrokeColor(const Color& color) |
| 92 { |
| 93 m_strokeData.clearGradient(); |
| 94 m_strokeData.clearPattern(); |
| 95 m_strokeData.setColor(color); |
| 96 m_strokePaint.setColor(applyAlpha(color.rgb())); |
| 97 m_strokePaint.setShader(0); |
| 98 } |
| 99 |
| 100 void GraphicsContextState::setStrokeGradient(const PassRefPtr<Gradient> gradient
) |
| 101 { |
| 102 m_strokeData.setColor(Color::black); |
| 103 m_strokeData.clearPattern(); |
| 104 m_strokeData.setGradient(gradient); |
| 105 m_strokePaint.setColor(applyAlpha(SK_ColorBLACK)); |
| 106 m_strokePaint.setShader(m_strokeData.gradient()->shader()); |
| 107 } |
| 108 |
| 109 void GraphicsContextState::clearStrokeGradient() |
| 110 { |
| 111 m_strokeData.clearGradient(); |
| 112 ASSERT(!m_strokeData.pattern()); |
| 113 m_strokePaint.setColor(applyAlpha(m_strokeData.color().rgb())); |
| 114 } |
| 115 |
| 116 void GraphicsContextState::setStrokePattern(const PassRefPtr<Pattern> pattern) |
| 117 { |
| 118 m_strokeData.setColor(Color::black); |
| 119 m_strokeData.clearGradient(); |
| 120 m_strokeData.setPattern(pattern); |
| 121 m_strokePaint.setColor(applyAlpha(SK_ColorBLACK)); |
| 122 m_strokePaint.setShader(m_strokeData.pattern()->shader()); |
| 123 } |
| 124 |
| 125 void GraphicsContextState::clearStrokePattern() |
| 126 { |
| 127 m_strokeData.clearPattern(); |
| 128 ASSERT(!m_strokeData.gradient()); |
| 129 m_strokePaint.setColor(applyAlpha(m_strokeData.color().rgb())); |
| 130 } |
| 131 |
| 132 void GraphicsContextState::setLineCap(const LineCap cap) |
| 133 { |
| 134 m_strokeData.setLineCap(cap); |
| 135 m_strokePaint.setStrokeCap((SkPaint::Cap)cap); |
| 136 } |
| 137 |
| 138 void GraphicsContextState::setLineJoin(const LineJoin join) |
| 139 { |
| 140 m_strokeData.setLineJoin(join); |
| 141 m_strokePaint.setStrokeJoin((SkPaint::Join)join); |
| 142 } |
| 143 |
| 144 void GraphicsContextState::setMiterLimit(const float miterLimit) |
| 145 { |
| 146 m_strokeData.setMiterLimit(miterLimit); |
| 147 m_strokePaint.setStrokeMiter(SkFloatToScalar(miterLimit)); |
| 148 } |
| 149 |
| 150 void GraphicsContextState::setFillColor(const Color& color) |
| 151 { |
| 152 m_fillColor = color; |
| 153 m_fillGradient.clear(); |
| 154 m_fillPattern.clear(); |
| 155 m_fillPaint.setColor(applyAlpha(color.rgb())); |
| 156 m_fillPaint.setShader(0); |
| 157 } |
| 158 |
| 159 void GraphicsContextState::setFillGradient(const PassRefPtr<Gradient> gradient) |
| 160 { |
| 161 m_fillColor = Color::black; |
| 162 m_fillPattern.clear(); |
| 163 m_fillGradient = gradient; |
| 164 m_fillPaint.setColor(applyAlpha(SK_ColorBLACK)); |
| 165 m_fillPaint.setShader(m_fillGradient->shader()); |
| 166 } |
| 167 |
| 168 void GraphicsContextState::clearFillGradient() |
| 169 { |
| 170 m_fillGradient.clear(); |
| 171 ASSERT(!m_fillPattern); |
| 172 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb())); |
| 173 } |
| 174 |
| 175 void GraphicsContextState::setFillPattern(const PassRefPtr<Pattern> pattern) |
| 176 { |
| 177 m_fillColor = Color::black; |
| 178 m_fillGradient.clear(); |
| 179 m_fillPattern = pattern; |
| 180 m_fillPaint.setColor(applyAlpha(SK_ColorBLACK)); |
| 181 m_fillPaint.setShader(m_fillPattern->shader()); |
| 182 } |
| 183 |
| 184 void GraphicsContextState::clearFillPattern() |
| 185 { |
| 186 m_fillPattern.clear(); |
| 187 ASSERT(!m_fillGradient); |
| 188 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb())); |
| 189 } |
| 190 |
| 191 // Shadow. (This will need tweaking if we use draw loopers for other things.) |
| 192 void GraphicsContextState::setDrawLooper(const DrawLooper& drawLooper) |
| 193 { |
| 194 m_looper = drawLooper.skDrawLooper(); |
| 195 m_strokePaint.setLooper(m_looper.get()); |
| 196 m_fillPaint.setLooper(m_looper.get()); |
| 197 } |
| 198 |
| 199 void GraphicsContextState::clearDrawLooper() |
| 200 { |
| 201 m_looper.clear(); |
| 202 m_strokePaint.setLooper(0); |
| 203 m_fillPaint.setLooper(0); |
| 204 } |
| 205 |
| 206 void GraphicsContextState::setAlphaAsFloat(const float alpha) |
| 207 { |
| 208 if (alpha < 0) { |
| 209 m_alpha = 0; |
| 210 } else { |
| 211 m_alpha = roundf(alpha * 256); |
| 212 if (m_alpha > 256) |
| 213 m_alpha = 256; |
| 214 } |
| 215 m_strokePaint.setColor(applyAlpha(m_strokeData.color().rgb())); |
| 216 m_fillPaint.setColor(applyAlpha(m_fillColor.rgb())); |
| 217 } |
| 218 |
| 219 void GraphicsContextState::setLineDash(const DashArray& dashes, float dashOffset
) |
| 220 { |
| 221 m_strokeData.setLineDash(dashes, dashOffset); |
| 222 m_strokeData.setupPaintDashPathEffect(&m_strokePaint, 0); |
| 223 } |
| 224 |
| 225 void GraphicsContextState::setColorFilter(PassRefPtr<SkColorFilter> colorFilter) |
| 226 { |
| 227 m_colorFilter = colorFilter; |
| 228 m_strokePaint.setColorFilter(m_colorFilter.get()); |
| 229 m_fillPaint.setColorFilter(m_colorFilter.get()); |
| 230 } |
| 231 |
| 232 void GraphicsContextState::setCompositeOperation(const CompositeOperator composi
teOperation, const blink::WebBlendMode blendMode) |
| 233 { |
| 234 m_compositeOperator = compositeOperation; |
| 235 m_blendMode = blendMode; |
| 236 m_xferMode = WebCoreCompositeToSkiaComposite(compositeOperation, blendMode); |
| 237 m_strokePaint.setXfermode(m_xferMode.get()); |
| 238 m_fillPaint.setXfermode(m_xferMode.get()); |
| 239 } |
| 240 |
| 241 void GraphicsContextState::setInterpolationQuality(const InterpolationQuality qu
ality) |
| 242 { |
| 243 m_interpolationQuality = quality; |
| 244 m_strokePaint.setFilterBitmap(quality != InterpolationNone); |
| 245 m_fillPaint.setFilterBitmap(quality != InterpolationNone); |
| 246 } |
| 247 |
| 248 void GraphicsContextState::setShouldAntialias(const bool shouldAntialias) |
| 249 { |
| 250 m_shouldAntialias = shouldAntialias; |
| 251 m_strokePaint.setAntiAlias(shouldAntialias); |
| 252 m_fillPaint.setAntiAlias(shouldAntialias); |
| 253 } |
| 254 |
| 255 |
| 256 } // namespace WebCore |
OLD | NEW |