blob: b2d68fc8cf94d9c78507116c4340295e63e92b61 [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
27void 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
39SkRect 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