blob: 0bcddcd8fdc2494ec5cf481b80c1bf5f32b1ddba [file] [log] [blame]
Florin Malita5f9102f2018-01-10 13:36:22 -05001/*
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
12namespace sksg {
13
Florin Malitaa016be92018-03-05 14:01:41 -050014MaskEffect::MaskEffect(sk_sp<RenderNode> child, sk_sp<RenderNode> mask, Mode mode)
Florin Malita5f9102f2018-01-10 13:36:22 -050015 : INHERITED(std::move(child))
Florin Malitaa016be92018-03-05 14:01:41 -050016 , fMaskNode(std::move(mask))
17 , fMaskMode(mode) {
Florin Malita3ba3fa72018-01-22 10:19:28 -050018 this->observeInval(fMaskNode);
Florin Malita5f9102f2018-01-10 13:36:22 -050019}
20
21MaskEffect::~MaskEffect() {
Florin Malita3ba3fa72018-01-22 10:19:28 -050022 this->unobserveInval(fMaskNode);
Florin Malita5f9102f2018-01-10 13:36:22 -050023}
24
Florin Malitac0132ff2018-08-09 07:40:01 -040025void MaskEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
Florin Malita5f9102f2018-01-10 13:36:22 -050026 SkAutoCanvasRestore acr(canvas, false);
27
28 canvas->saveLayer(this->bounds(), nullptr);
Florin Malitac0132ff2018-08-09 07:40:01 -040029 // Note: the paint overrides in ctx don't apply to the mask.
Florin Malita5f9102f2018-01-10 13:36:22 -050030 fMaskNode->render(canvas);
31
Florin Malita5f9102f2018-01-10 13:36:22 -050032 SkPaint p;
Florin Malitaa016be92018-03-05 14:01:41 -050033 p.setBlendMode(fMaskMode == Mode::kNormal ? SkBlendMode::kSrcIn : SkBlendMode::kSrcOut);
Florin Malita5f9102f2018-01-10 13:36:22 -050034 canvas->saveLayer(this->bounds(), &p);
35
Florin Malitac0132ff2018-08-09 07:40:01 -040036 this->INHERITED::onRender(canvas, ctx);
Florin Malita5f9102f2018-01-10 13:36:22 -050037}
38
Florin Malitaeb46bd82019-02-12 09:33:21 -050039const 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}
Florin Malita5f9102f2018-01-10 13:36:22 -050044
45SkRect 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
Florin Malitaa016be92018-03-05 14:01:41 -050051 return (fMaskMode == Mode::kInvert || childBounds.intersect(maskBounds))
52 ? childBounds
53 : SkRect::MakeEmpty();
Florin Malita5f9102f2018-01-10 13:36:22 -050054}
55
56} // namespace sksg