return v ? v->get() : nullptr;
}
-void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs) {
+void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs,
+ uint32_t flags) {
#define ApplyLazyInheritedAttribute(ATTR) \
do { \
// 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);
}
SkCanvas* canvas() const { return fCanvas; }
- void applyPresentationAttributes(const SkSVGPresentationAttributes&);
+ enum ApplyFlags {
+ kLeaf = 1 << 0, // the target node doesn't have descendants
+ };
+ void applyPresentationAttributes(const SkSVGPresentationAttributes&, uint32_t flags);
const SkSVGNode* findNodeById(const SkString&) const;
void* operator new(size_t, void*) = delete;
SkSVGRenderContext& operator=(const SkSVGRenderContext&) = delete;
+ void applyOpacity(SkScalar opacity, uint32_t flags);
+
const SkSVGIDMapper& fIDMapper;
SkTCopyOnFirstWrite<SkSVGLengthContext> fLengthContext;
SkTCopyOnFirstWrite<SkSVGPresentationContext> fPresentationContext;