skia2/experimental/sksg/effects/SkSGMaskEffect.cpp
Florin Malita 3ba3fa72ae [sksg] Refactor inval registration
... to avoid having too many Node friends.

TBR=
Change-Id: I8f8ff570d94ea48017935066a3d51cd8265ec120
Reviewed-on: https://skia-review.googlesource.com/97980
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
2018-01-22 15:50:49 +00:00

52 lines
1.2 KiB
C++

/*
* 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)) {
this->observeInval(fMaskNode);
}
MaskEffect::~MaskEffect() {
this->unobserveInval(fMaskNode);
}
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