aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/sksg/effects/SkSGMaskEffect.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'experimental/sksg/effects/SkSGMaskEffect.cpp')
-rw-r--r--experimental/sksg/effects/SkSGMaskEffect.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/experimental/sksg/effects/SkSGMaskEffect.cpp b/experimental/sksg/effects/SkSGMaskEffect.cpp
new file mode 100644
index 0000000000..d4ce6df4db
--- /dev/null
+++ b/experimental/sksg/effects/SkSGMaskEffect.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkSGMaskEffect.h"
+
+#include "SkCanvas.h"
+
+namespace sksg {
+
+MaskEffect::MaskEffect(sk_sp<RenderNode> child, sk_sp<RenderNode> mask)
+ : INHERITED(std::move(child))
+ , fMaskNode(std::move(mask)) {
+ fMaskNode->addInvalReceiver(this);
+}
+
+MaskEffect::~MaskEffect() {
+ fMaskNode->removeInvalReceiver(this);
+}
+
+void MaskEffect::onRender(SkCanvas* canvas) const {
+ if (this->bounds().isEmpty())
+ return;
+
+ SkAutoCanvasRestore acr(canvas, false);
+
+ canvas->saveLayer(this->bounds(), nullptr);
+ fMaskNode->render(canvas);
+
+
+ SkPaint p;
+ p.setBlendMode(SkBlendMode::kSrcIn);
+ canvas->saveLayer(this->bounds(), &p);
+
+ this->INHERITED::onRender(canvas);
+}
+
+
+SkRect MaskEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
+ SkASSERT(this->hasInval());
+
+ const auto maskBounds = fMaskNode->revalidate(ic, ctm);
+ auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
+
+ return childBounds.intersect(maskBounds) ? childBounds : SkRect::MakeEmpty();
+}
+
+} // namespace sksg