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) { | |
244 | 243 |
245 #define ApplyLazyInheritedAttribute(ATTR)
\ | 244 #define ApplyLazyInheritedAttribute(ATTR)
\ |
246 do {
\ | 245 do {
\ |
247 /* All attributes should be defined on the inherited context. */
\ | 246 /* All attributes should be defined on the inherited context. */
\ |
248 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid());
\ | 247 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid());
\ |
249 const auto* value = attrs.f ## ATTR.getMaybeNull();
\ | 248 const auto* value = attrs.f ## ATTR.getMaybeNull();
\ |
250 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()
) { \ | 249 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()
) { \ |
251 /* Update the local attribute value */
\ | 250 /* Update the local attribute value */
\ |
252 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value);
\ | 251 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value);
\ |
253 /* Update the cached paints */
\ | 252 /* Update the cached paints */
\ |
254 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \ | 253 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \ |
255 fPresentationContext.writab
le()); \ | 254 fPresentationContext.writab
le()); \ |
256 }
\ | 255 }
\ |
257 } while (false) | 256 } while (false) |
258 | 257 |
259 ApplyLazyInheritedAttribute(Fill); | 258 ApplyLazyInheritedAttribute(Fill); |
260 ApplyLazyInheritedAttribute(FillOpacity); | 259 ApplyLazyInheritedAttribute(FillOpacity); |
261 ApplyLazyInheritedAttribute(Stroke); | 260 ApplyLazyInheritedAttribute(Stroke); |
262 ApplyLazyInheritedAttribute(StrokeLineCap); | 261 ApplyLazyInheritedAttribute(StrokeLineCap); |
263 ApplyLazyInheritedAttribute(StrokeLineJoin); | 262 ApplyLazyInheritedAttribute(StrokeLineJoin); |
264 ApplyLazyInheritedAttribute(StrokeOpacity); | 263 ApplyLazyInheritedAttribute(StrokeOpacity); |
265 ApplyLazyInheritedAttribute(StrokeWidth); | 264 ApplyLazyInheritedAttribute(StrokeWidth); |
266 | 265 |
267 #undef ApplyLazyInheritedAttribute | 266 #undef ApplyLazyInheritedAttribute |
268 | 267 |
269 // Uninherited attributes. Only apply to the current context. | 268 // Uninherited attributes. Only apply to the current context. |
270 | 269 |
271 if (auto* opacity = attrs.fOpacity.getMaybeNull()) { | 270 auto* opacity = attrs.fOpacity.getMaybeNull(); |
272 this->applyOpacity(opacity->value(), flags); | 271 if (opacity && opacity->value() < 1) { |
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. | |
299 SkPaint opacityPaint; | 272 SkPaint opacityPaint; |
300 opacityPaint.setAlpha(opacity_to_alpha(opacity)); | 273 opacityPaint.setAlpha(opacity_to_alpha(opacity->value())); |
301 // Balanced in the destructor, via restoreToCount(). | 274 // Balanced in the destructor, via restoreToCount(). |
302 fCanvas->saveLayer(nullptr, &opacityPaint); | 275 fCanvas->saveLayer(nullptr, &opacityPaint); |
303 } | 276 } |
304 } | 277 } |
305 | 278 |
306 const SkPaint* SkSVGRenderContext::fillPaint() const { | 279 const SkPaint* SkSVGRenderContext::fillPaint() const { |
307 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge
t()->type(); | 280 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge
t()->type(); |
308 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa
int : nullptr; | 281 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa
int : nullptr; |
309 } | 282 } |
310 | 283 |
311 const SkPaint* SkSVGRenderContext::strokePaint() const { | 284 const SkPaint* SkSVGRenderContext::strokePaint() const { |
312 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke.
get()->type(); | 285 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke.
get()->type(); |
313 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke
Paint : nullptr; | 286 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke
Paint : nullptr; |
314 } | 287 } |
OLD | NEW |