• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modules/sksg/include/SkSGMaskEffect.h"
9 
10 #include "include/core/SkCanvas.h"
11 
12 namespace sksg {
13 
MaskEffect(sk_sp<RenderNode> child,sk_sp<RenderNode> mask,Mode mode)14 MaskEffect::MaskEffect(sk_sp<RenderNode> child, sk_sp<RenderNode> mask, Mode mode)
15     : INHERITED(std::move(child))
16     , fMaskNode(std::move(mask))
17     , fMaskMode(mode) {
18     this->observeInval(fMaskNode);
19 }
20 
~MaskEffect()21 MaskEffect::~MaskEffect() {
22     this->unobserveInval(fMaskNode);
23 }
24 
onRender(SkCanvas * canvas,const RenderContext * ctx) const25 void MaskEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
26     SkAutoCanvasRestore acr(canvas, false);
27 
28     canvas->saveLayer(this->bounds(), nullptr);
29     // Note: the paint overrides in ctx don't apply to the mask.
30     fMaskNode->render(canvas);
31 
32     SkPaint p;
33     p.setBlendMode(fMaskMode == Mode::kNormal ? SkBlendMode::kSrcIn : SkBlendMode::kSrcOut);
34     canvas->saveLayer(this->bounds(), &p);
35 
36     this->INHERITED::onRender(canvas, ctx);
37 }
38 
onNodeAt(const SkPoint & p) const39 const RenderNode* MaskEffect::onNodeAt(const SkPoint& p) const {
40     const auto mask_hit = (!!fMaskNode->nodeAt(p) == (fMaskMode == Mode::kNormal));
41 
42     return mask_hit ? this->INHERITED::onNodeAt(p) : nullptr;
43 }
44 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)45 SkRect MaskEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
46     SkASSERT(this->hasInval());
47 
48     const auto maskBounds = fMaskNode->revalidate(ic, ctm);
49     auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
50 
51     return (fMaskMode == Mode::kInvert || childBounds.intersect(maskBounds))
52         ? childBounds
53         : SkRect::MakeEmpty();
54 }
55 
56 } // namespace sksg
57