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,bool force_clip)16 ClipEffect::ClipEffect(sk_sp<RenderNode> child, sk_sp<GeometryNode> clip, bool aa, bool force_clip)
17 : INHERITED(std::move(child))
18 , fClipNode(std::move(clip))
19 , fAntiAlias(aa)
20 , fForceClip(force_clip) {
21 this->observeInval(fClipNode);
22 }
23
~ClipEffect()24 ClipEffect::~ClipEffect() {
25 this->unobserveInval(fClipNode);
26 }
27
onRender(SkCanvas * canvas,const RenderContext * ctx) const28 void ClipEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
29 SkAutoCanvasRestore acr(canvas, !fNoop);
30 if (!fNoop) {
31 fClipNode->clip(canvas, fAntiAlias);
32 }
33
34 this->INHERITED::onRender(canvas, ctx);
35 }
36
onNodeAt(const SkPoint & p) const37 const RenderNode* ClipEffect::onNodeAt(const SkPoint& p) const {
38 return fClipNode->contains(p) ? this->INHERITED::onNodeAt(p) : nullptr;
39 }
40
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)41 SkRect ClipEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
42 SkASSERT(this->hasInval());
43
44 const auto clipBounds = fClipNode->revalidate(ic, ctm);
45 auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
46
47 // When the child node is fully contained within the clip, it is usually safe to elide.
48 // An exception is clip-dependent sizing for saveLayer buffers, where the clip is always
49 // significant. For those cases, we provide a mechanism to disable elision.
50 fNoop = !fForceClip && fClipNode->asPath().conservativelyContainsRect(childBounds);
51
52 return childBounds.intersect(clipBounds) ? childBounds : SkRect::MakeEmpty();
53 }
54
55 } // namespace sksg
56