xref: /aosp_15_r20/external/skia/modules/sksg/src/SkSGClipEffect.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/private/base/SkAssert.h"
13 #include "modules/sksg/include/SkSGGeometryNode.h"
14 #include "modules/sksg/include/SkSGNode.h"
15 
16 class SkMatrix;
17 struct SkPoint;
18 
19 namespace sksg {
20 class InvalidationController;
21 
ClipEffect(sk_sp<RenderNode> child,sk_sp<GeometryNode> clip,bool aa,bool force_clip)22 ClipEffect::ClipEffect(sk_sp<RenderNode> child, sk_sp<GeometryNode> clip, bool aa, bool force_clip)
23     : INHERITED(std::move(child))
24     , fClipNode(std::move(clip))
25     , fAntiAlias(aa)
26     , fForceClip(force_clip) {
27     this->observeInval(fClipNode);
28 }
29 
~ClipEffect()30 ClipEffect::~ClipEffect() {
31     this->unobserveInval(fClipNode);
32 }
33 
onRender(SkCanvas * canvas,const RenderContext * ctx) const34 void ClipEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
35     SkAutoCanvasRestore acr(canvas, !fNoop);
36     if (!fNoop) {
37         fClipNode->clip(canvas, fAntiAlias);
38     }
39 
40     this->INHERITED::onRender(canvas, ctx);
41 }
42 
onNodeAt(const SkPoint & p) const43 const RenderNode* ClipEffect::onNodeAt(const SkPoint& p) const {
44     return fClipNode->contains(p) ? this->INHERITED::onNodeAt(p) : nullptr;
45 }
46 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)47 SkRect ClipEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
48     SkASSERT(this->hasInval());
49 
50     const auto clipBounds = fClipNode->revalidate(ic, ctm);
51     auto childBounds = this->INHERITED::onRevalidate(ic, ctm);
52 
53     // When the child node is fully contained within the clip, it is usually safe to elide.
54     // An exception is clip-dependent sizing for saveLayer buffers, where the clip is always
55     // significant.  For those cases, we provide a mechanism to disable elision.
56     fNoop = !fForceClip && fClipNode->asPath().conservativelyContainsRect(childBounds);
57 
58     return childBounds.intersect(clipBounds) ? childBounds : SkRect::MakeEmpty();
59 }
60 
61 } // namespace sksg
62