aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/svg/model/SkSVGRenderContext.cpp
diff options
context:
space:
mode:
authorGravatar fmalita <fmalita@chromium.org>2016-09-20 15:45:57 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-09-20 15:45:57 -0700
commitbef51c23c314180760b07f88fab50f6670f37f9b (patch)
tree5b842aef28d9791c19ed2e9ed7c5ccc54873ae18 /experimental/svg/model/SkSVGRenderContext.cpp
parent768dc8f496e4cf888fd6113e00cd3fda269dd805 (diff)
[SVGDom] Opacity optimization
Apply opacity as fill/stroke paint alpha instead of saveLayer, when possible. R=robertphillips@google.com,stephana@google.com,reed@google.com GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2353503005 Committed: https://skia.googlesource.com/skia/+/3dbb7b9f196d793fbd16243157ee67638891f5dc Review-Url: https://codereview.chromium.org/2353503005
Diffstat (limited to 'experimental/svg/model/SkSVGRenderContext.cpp')
-rw-r--r--experimental/svg/model/SkSVGRenderContext.cpp35
1 files changed, 31 insertions, 4 deletions
diff --git a/experimental/svg/model/SkSVGRenderContext.cpp b/experimental/svg/model/SkSVGRenderContext.cpp
index 7e2e90e22c..147d64edb3 100644
--- a/experimental/svg/model/SkSVGRenderContext.cpp
+++ b/experimental/svg/model/SkSVGRenderContext.cpp
@@ -239,7 +239,8 @@ const SkSVGNode* SkSVGRenderContext::findNodeById(const SkString& id) const {
return v ? v->get() : nullptr;
}
-void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs) {
+void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs,
+ uint32_t flags) {
#define ApplyLazyInheritedAttribute(ATTR) \
do { \
@@ -267,10 +268,36 @@ void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr
// Uninherited attributes. Only apply to the current context.
- auto* opacity = attrs.fOpacity.getMaybeNull();
- if (opacity && opacity->value() < 1) {
+ if (auto* opacity = attrs.fOpacity.getMaybeNull()) {
+ this->applyOpacity(opacity->value(), flags);
+ }
+}
+
+void SkSVGRenderContext::applyOpacity(SkScalar opacity, uint32_t flags) {
+ if (opacity >= 1) {
+ return;
+ }
+
+ const bool hasFill = SkToBool(this->fillPaint());
+ const bool hasStroke = SkToBool(this->strokePaint());
+
+ // We can apply the opacity as paint alpha iif it only affects one atomic draw.
+ // For now, this means a) the target node doesn't have any descendants, and
+ // b) it only has a stroke or a fill (but not both). Going forward, we may need
+ // to refine this heuristic (e.g. to accommodate markers).
+ if ((flags & kLeaf) && (hasFill ^ hasStroke)) {
+ auto* pctx = fPresentationContext.writable();
+ if (hasFill) {
+ pctx->fFillPaint.setAlpha(
+ SkScalarRoundToInt(opacity * pctx->fFillPaint.getAlpha()));
+ } else {
+ pctx->fStrokePaint.setAlpha(
+ SkScalarRoundToInt(opacity * pctx->fStrokePaint.getAlpha()));
+ }
+ } else {
+ // Expensive, layer-based fall back.
SkPaint opacityPaint;
- opacityPaint.setAlpha(opacity_to_alpha(opacity->value()));
+ opacityPaint.setAlpha(opacity_to_alpha(opacity));
// Balanced in the destructor, via restoreToCount().
fCanvas->saveLayer(nullptr, &opacityPaint);
}