Florin Malita | 41dff6e | 2018-04-30 23:08:15 -0400 | [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 "SkSGRoundEffect.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkCornerPathEffect.h" |
| 12 | #include "SkStrokeRec.h" |
| 13 | |
| 14 | namespace sksg { |
| 15 | |
| 16 | RoundEffect::RoundEffect(sk_sp<GeometryNode> child) |
| 17 | : fChild(std::move(child)) { |
| 18 | this->observeInval(fChild); |
| 19 | } |
| 20 | |
| 21 | RoundEffect::~RoundEffect() { |
| 22 | this->unobserveInval(fChild); |
| 23 | } |
| 24 | |
| 25 | void RoundEffect::onClip(SkCanvas* canvas, bool antiAlias) const { |
| 26 | canvas->clipPath(fRoundedPath, SkClipOp::kIntersect, antiAlias); |
| 27 | } |
| 28 | |
| 29 | void RoundEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const { |
| 30 | SkASSERT(!paint.getPathEffect()); |
| 31 | |
| 32 | canvas->drawPath(fRoundedPath, paint); |
| 33 | } |
| 34 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 35 | bool RoundEffect::onContains(const SkPoint& p) const { |
| 36 | return fRoundedPath.contains(p.x(), p.y()); |
| 37 | } |
| 38 | |
Florin Malita | 41dff6e | 2018-04-30 23:08:15 -0400 | [diff] [blame] | 39 | SkPath RoundEffect::onAsPath() const { |
| 40 | return fRoundedPath; |
| 41 | } |
| 42 | |
| 43 | SkRect RoundEffect::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 round = SkCornerPathEffect::Make(fRadius)) { |
| 50 | fRoundedPath.reset(); |
| 51 | SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle); |
| 52 | SkAssertResult(round->filterPath(&fRoundedPath, path, &rec, &childbounds)); |
| 53 | } else { |
| 54 | fRoundedPath = path; |
| 55 | } |
| 56 | |
Florin Malita | f2ade42 | 2018-09-11 16:38:39 -0400 | [diff] [blame] | 57 | fRoundedPath.shrinkToFit(); |
| 58 | |
Florin Malita | 41dff6e | 2018-04-30 23:08:15 -0400 | [diff] [blame] | 59 | return fRoundedPath.computeTightBounds(); |
| 60 | } |
| 61 | |
| 62 | } // namespace sksg |