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 "SkSGClipEffect.h"
9 
10 #include "SkCanvas.h"
11 #include "SkPath.h"
12 #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) const27 void ClipEffect::onRender(SkCanvas* canvas) const {
28     if (this->bounds().isEmpty())
29         return;
30 
31     SkAutoCanvasRestore acr(canvas, !fNoop);
32     if (!fNoop) {
33         fClipNode->clip(canvas, fAntiAlias);
34     }
35 
36     this->INHERITED::onRender(canvas);
37 }
38 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)39 SkRect ClipEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
40     SkASSERT(this->hasInval());
41 
42     const auto clipBounds = fClipNode->revalidate(ic, ctm);
43     auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
44 
45     fNoop = fClipNode->asPath().conservativelyContainsRect(childBounds);
46 
47     return childBounds.intersect(clipBounds) ? childBounds : SkRect::MakeEmpty();
48 }
49 
50 } // namespace sksg
51