OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkSVGAttribute.h" | 9 #include "SkSVGAttribute.h" |
10 #include "SkSVGNode.h" | 10 #include "SkSVGNode.h" |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 SkSVGRenderContext::~SkSVGRenderContext() { | 233 SkSVGRenderContext::~SkSVGRenderContext() { |
234 fCanvas->restoreToCount(fCanvasSaveCount); | 234 fCanvas->restoreToCount(fCanvasSaveCount); |
235 } | 235 } |
236 | 236 |
237 const SkSVGNode* SkSVGRenderContext::findNodeById(const SkString& id) const { | 237 const SkSVGNode* SkSVGRenderContext::findNodeById(const SkString& id) const { |
238 const auto* v = fIDMapper.find(id); | 238 const auto* v = fIDMapper.find(id); |
239 return v ? v->get() : nullptr; | 239 return v ? v->get() : nullptr; |
240 } | 240 } |
241 | 241 |
242 void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr
ibutes& attrs) { | 242 void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr
ibutes& attrs, |
| 243 uint32_t flags) { |
243 | 244 |
244 #define ApplyLazyInheritedAttribute(ATTR)
\ | 245 #define ApplyLazyInheritedAttribute(ATTR)
\ |
245 do {
\ | 246 do {
\ |
246 /* All attributes should be defined on the inherited context. */
\ | 247 /* All attributes should be defined on the inherited context. */
\ |
247 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid());
\ | 248 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid());
\ |
248 const auto* value = attrs.f ## ATTR.getMaybeNull();
\ | 249 const auto* value = attrs.f ## ATTR.getMaybeNull();
\ |
249 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()
) { \ | 250 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()
) { \ |
250 /* Update the local attribute value */
\ | 251 /* Update the local attribute value */
\ |
251 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value);
\ | 252 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value);
\ |
252 /* Update the cached paints */
\ | 253 /* Update the cached paints */
\ |
253 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \ | 254 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \ |
254 fPresentationContext.writab
le()); \ | 255 fPresentationContext.writab
le()); \ |
255 }
\ | 256 }
\ |
256 } while (false) | 257 } while (false) |
257 | 258 |
258 ApplyLazyInheritedAttribute(Fill); | 259 ApplyLazyInheritedAttribute(Fill); |
259 ApplyLazyInheritedAttribute(FillOpacity); | 260 ApplyLazyInheritedAttribute(FillOpacity); |
260 ApplyLazyInheritedAttribute(Stroke); | 261 ApplyLazyInheritedAttribute(Stroke); |
261 ApplyLazyInheritedAttribute(StrokeLineCap); | 262 ApplyLazyInheritedAttribute(StrokeLineCap); |
262 ApplyLazyInheritedAttribute(StrokeLineJoin); | 263 ApplyLazyInheritedAttribute(StrokeLineJoin); |
263 ApplyLazyInheritedAttribute(StrokeOpacity); | 264 ApplyLazyInheritedAttribute(StrokeOpacity); |
264 ApplyLazyInheritedAttribute(StrokeWidth); | 265 ApplyLazyInheritedAttribute(StrokeWidth); |
265 | 266 |
266 #undef ApplyLazyInheritedAttribute | 267 #undef ApplyLazyInheritedAttribute |
267 | 268 |
268 // Uninherited attributes. Only apply to the current context. | 269 // Uninherited attributes. Only apply to the current context. |
269 | 270 |
270 auto* opacity = attrs.fOpacity.getMaybeNull(); | 271 if (auto* opacity = attrs.fOpacity.getMaybeNull()) { |
271 if (opacity && opacity->value() < 1) { | 272 this->applyOpacity(opacity->value(), flags); |
| 273 } |
| 274 } |
| 275 |
| 276 void SkSVGRenderContext::applyOpacity(SkScalar opacity, uint32_t flags) { |
| 277 if (opacity >= 1) { |
| 278 return; |
| 279 } |
| 280 |
| 281 const bool hasFill = SkToBool(this->fillPaint()); |
| 282 const bool hasStroke = SkToBool(this->strokePaint()); |
| 283 |
| 284 // We can apply the opacity as paint alpha iif it only affects one atomic dr
aw. |
| 285 // For now, this means a) the target node doesn't have any descendants, and |
| 286 // b) it only has a stroke or a fill (but not both). Going forward, we may
need |
| 287 // to refine this heuristic (e.g. to accommodate markers). |
| 288 if ((flags & kLeaf) && (hasFill ^ hasStroke)) { |
| 289 auto* pctx = fPresentationContext.writable(); |
| 290 if (hasFill) { |
| 291 pctx->fFillPaint.setAlpha( |
| 292 SkScalarRoundToInt(opacity * pctx->fFillPaint.getAlpha())); |
| 293 } else { |
| 294 pctx->fStrokePaint.setAlpha( |
| 295 SkScalarRoundToInt(opacity * pctx->fStrokePaint.getAlpha())); |
| 296 } |
| 297 } else { |
| 298 // Expensive, layer-based fall back. |
272 SkPaint opacityPaint; | 299 SkPaint opacityPaint; |
273 opacityPaint.setAlpha(opacity_to_alpha(opacity->value())); | 300 opacityPaint.setAlpha(opacity_to_alpha(opacity)); |
274 // Balanced in the destructor, via restoreToCount(). | 301 // Balanced in the destructor, via restoreToCount(). |
275 fCanvas->saveLayer(nullptr, &opacityPaint); | 302 fCanvas->saveLayer(nullptr, &opacityPaint); |
276 } | 303 } |
277 } | 304 } |
278 | 305 |
279 const SkPaint* SkSVGRenderContext::fillPaint() const { | 306 const SkPaint* SkSVGRenderContext::fillPaint() const { |
280 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge
t()->type(); | 307 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge
t()->type(); |
281 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa
int : nullptr; | 308 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa
int : nullptr; |
282 } | 309 } |
283 | 310 |
284 const SkPaint* SkSVGRenderContext::strokePaint() const { | 311 const SkPaint* SkSVGRenderContext::strokePaint() const { |
285 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke.
get()->type(); | 312 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke.
get()->type(); |
286 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke
Paint : nullptr; | 313 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke
Paint : nullptr; |
287 } | 314 } |
OLD | NEW |