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
RoundEffect(sk_sp<GeometryNode> child)16 RoundEffect::RoundEffect(sk_sp<GeometryNode> child)
17 : fChild(std::move(child)) {
18 this->observeInval(fChild);
19 }
20
~RoundEffect()21 RoundEffect::~RoundEffect() {
22 this->unobserveInval(fChild);
23 }
24
onClip(SkCanvas * canvas,bool antiAlias) const25 void RoundEffect::onClip(SkCanvas* canvas, bool antiAlias) const {
26 canvas->clipPath(fRoundedPath, SkClipOp::kIntersect, antiAlias);
27 }
28
onDraw(SkCanvas * canvas,const SkPaint & paint) const29 void RoundEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
30 SkASSERT(!paint.getPathEffect());
31
32 canvas->drawPath(fRoundedPath, paint);
33 }
34
onAsPath() const35 SkPath RoundEffect::onAsPath() const {
36 return fRoundedPath;
37 }
38
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)39 SkRect RoundEffect::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 round = SkCornerPathEffect::Make(fRadius)) {
46 fRoundedPath.reset();
47 SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
48 SkAssertResult(round->filterPath(&fRoundedPath, path, &rec, &childbounds));
49 } else {
50 fRoundedPath = path;
51 }
52
53 fRoundedPath.shrinkToFit();
54
55 return fRoundedPath.computeTightBounds();
56 }
57
58 } // namespace sksg
59