Florin Malita | 38ea40e | 2018-01-29 16:31:14 -0500 | [diff] [blame] | 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 | |
| 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 | |
| 23 | ClipEffect::~ClipEffect() { |
| 24 | this->unobserveInval(fClipNode); |
| 25 | } |
| 26 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 27 | void ClipEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const { |
Florin Malita | 38ea40e | 2018-01-29 16:31:14 -0500 | [diff] [blame] | 28 | if (this->bounds().isEmpty()) |
| 29 | return; |
| 30 | |
| 31 | SkAutoCanvasRestore acr(canvas, !fNoop); |
| 32 | if (!fNoop) { |
| 33 | fClipNode->clip(canvas, fAntiAlias); |
| 34 | } |
| 35 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 36 | this->INHERITED::onRender(canvas, ctx); |
Florin Malita | 38ea40e | 2018-01-29 16:31:14 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 39 | const RenderNode* ClipEffect::onNodeAt(const SkPoint& p) const { |
| 40 | return fClipNode->contains(p) ? this->INHERITED::onNodeAt(p) : nullptr; |
| 41 | } |
| 42 | |
Florin Malita | 38ea40e | 2018-01-29 16:31:14 -0500 | [diff] [blame] | 43 | SkRect ClipEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
| 44 | SkASSERT(this->hasInval()); |
| 45 | |
| 46 | const auto clipBounds = fClipNode->revalidate(ic, ctm); |
| 47 | auto childBounds = this->INHERITED::onRevalidate(ic, ctm); |
| 48 | |
| 49 | fNoop = fClipNode->asPath().conservativelyContainsRect(childBounds); |
| 50 | |
| 51 | return childBounds.intersect(clipBounds) ? childBounds : SkRect::MakeEmpty(); |
| 52 | } |
| 53 | |
| 54 | } // namespace sksg |