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/SkSGClipEffect.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPath.h"
12 #include "modules/sksg/include/SkSGGeometryNode.h"
13
14 namespace sksg {
15
ClipEffect(sk_sp<RenderNode> child,sk_sp<GeometryNode> clip,bool aa)16 ClipEffect::ClipEffect(sk_sp<RenderNode> child, sk_sp<GeometryNode> clip, bool aa)
17 : INHERITED(std::move(child))
18 , fClipNode(std::move(clip))
19 , fAntiAlias(aa) {
20 this->observeInval(fClipNode);
21 }
22
~ClipEffect()23 ClipEffect::~ClipEffect() {
24 this->unobserveInval(fClipNode);
25 }
26
onRender(SkCanvas * canvas,const RenderContext * ctx) const27 void ClipEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
28 SkAutoCanvasRestore acr(canvas, !fNoop);
29 if (!fNoop) {
30 fClipNode->clip(canvas, fAntiAlias);
31 }
32
33 this->INHERITED::onRender(canvas, ctx);
34 }
35
onNodeAt(const SkPoint & p) const36 const RenderNode* ClipEffect::onNodeAt(const SkPoint& p) const {
37 return fClipNode->contains(p) ? this->INHERITED::onNodeAt(p) : nullptr;
38 }
39
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)40 SkRect ClipEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
41 SkASSERT(this->hasInval());
42
43 const auto clipBounds = fClipNode->revalidate(ic, ctm);
44 auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
45
46 fNoop = fClipNode->asPath().conservativelyContainsRect(childBounds);
47
48 return childBounds.intersect(clipBounds) ? childBounds : SkRect::MakeEmpty();
49 }
50
51 } // namespace sksg
52