1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkSGMaskEffect.h"
9
10 #include "SkCanvas.h"
11
12 namespace sksg {
13
MaskEffect(sk_sp<RenderNode> child,sk_sp<RenderNode> mask)14 MaskEffect::MaskEffect(sk_sp<RenderNode> child, sk_sp<RenderNode> mask)
15 : INHERITED(std::move(child))
16 , fMaskNode(std::move(mask)) {
17 this->observeInval(fMaskNode);
18 }
19
~MaskEffect()20 MaskEffect::~MaskEffect() {
21 this->unobserveInval(fMaskNode);
22 }
23
onRender(SkCanvas * canvas) const24 void MaskEffect::onRender(SkCanvas* canvas) const {
25 if (this->bounds().isEmpty())
26 return;
27
28 SkAutoCanvasRestore acr(canvas, false);
29
30 canvas->saveLayer(this->bounds(), nullptr);
31 fMaskNode->render(canvas);
32
33
34 SkPaint p;
35 p.setBlendMode(SkBlendMode::kSrcIn);
36 canvas->saveLayer(this->bounds(), &p);
37
38 this->INHERITED::onRender(canvas);
39 }
40
41
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)42 SkRect MaskEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
43 SkASSERT(this->hasInval());
44
45 const auto maskBounds = fMaskNode->revalidate(ic, ctm);
46 auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
47
48 return childBounds.intersect(maskBounds) ? childBounds : SkRect::MakeEmpty();
49 }
50
51 } // namespace sksg
52