blob: 11a9be6132d9db6b689cd0a8e496ffbf511e7452 [file] [log] [blame]
Florin Malita38ea40e2018-01-29 16:31:14 -05001/*
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
14namespace sksg {
15
16ClipEffect::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
23ClipEffect::~ClipEffect() {
24 this->unobserveInval(fClipNode);
25}
26
Florin Malitac0132ff2018-08-09 07:40:01 -040027void ClipEffect::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
Florin Malita38ea40e2018-01-29 16:31:14 -050028 if (this->bounds().isEmpty())
29 return;
30
31 SkAutoCanvasRestore acr(canvas, !fNoop);
32 if (!fNoop) {
33 fClipNode->clip(canvas, fAntiAlias);
34 }
35
Florin Malitac0132ff2018-08-09 07:40:01 -040036 this->INHERITED::onRender(canvas, ctx);
Florin Malita38ea40e2018-01-29 16:31:14 -050037}
38
Florin Malitaeb46bd82019-02-12 09:33:21 -050039const RenderNode* ClipEffect::onNodeAt(const SkPoint& p) const {
40 return fClipNode->contains(p) ? this->INHERITED::onNodeAt(p) : nullptr;
41}
42
Florin Malita38ea40e2018-01-29 16:31:14 -050043SkRect 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