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