1 /*
2 * Copyright 2017 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/SkSGTrimEffect.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkStrokeRec.h"
12 #include "include/effects/SkTrimPathEffect.h"
13
14 namespace sksg {
15
TrimEffect(sk_sp<GeometryNode> child)16 TrimEffect::TrimEffect(sk_sp<GeometryNode> child)
17 : fChild(std::move(child)) {
18 this->observeInval(fChild);
19 }
20
~TrimEffect()21 TrimEffect::~TrimEffect() {
22 this->unobserveInval(fChild);
23 }
24
onClip(SkCanvas * canvas,bool antiAlias) const25 void TrimEffect::onClip(SkCanvas* canvas, bool antiAlias) const {
26 canvas->clipPath(fTrimmedPath, SkClipOp::kIntersect, antiAlias);
27 }
28
onDraw(SkCanvas * canvas,const SkPaint & paint) const29 void TrimEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
30 SkASSERT(!paint.getPathEffect());
31
32 canvas->drawPath(fTrimmedPath, paint);
33 }
34
onContains(const SkPoint & p) const35 bool TrimEffect::onContains(const SkPoint& p) const {
36 return fTrimmedPath.contains(p.x(), p.y());
37 }
38
onAsPath() const39 SkPath TrimEffect::onAsPath() const {
40 return fTrimmedPath;
41 }
42
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)43 SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
44 SkASSERT(this->hasInval());
45
46 const auto childbounds = fChild->revalidate(ic, ctm);
47 const auto path = fChild->asPath();
48
49 if (auto trim = SkTrimPathEffect::Make(fStart, fStop, fMode)) {
50 fTrimmedPath.reset();
51 SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
52 SkAssertResult(trim->filterPath(&fTrimmedPath, path, &rec, &childbounds));
53 } else {
54 fTrimmedPath = path;
55 }
56
57 fTrimmedPath.shrinkToFit();
58
59 return fTrimmedPath.computeTightBounds();
60 }
61
62 } // namespace sksg
63