xref: /aosp_15_r20/external/skia/modules/sksg/src/SkSGGeometryEffect.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 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/SkSGGeometryEffect.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkClipOp.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathEffect.h"
15 #include "include/core/SkPathUtils.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkStrokeRec.h"
18 #include "include/effects/SkCornerPathEffect.h"
19 #include "include/effects/SkDashPathEffect.h"
20 #include "include/effects/SkTrimPathEffect.h"
21 #include "include/pathops/SkPathOps.h"
22 #include "include/private/base/SkAssert.h"
23 #include "include/private/base/SkTArray.h"
24 #include "include/private/base/SkTo.h"
25 #include "modules/sksg/src/SkSGTransformPriv.h"
26 #include "src/core/SkPathPriv.h"
27 
28 #include <algorithm>
29 #include <cmath>
30 
31 using namespace skia_private;
32 
33 namespace sksg {
34 
GeometryEffect(sk_sp<GeometryNode> child)35 GeometryEffect::GeometryEffect(sk_sp<GeometryNode> child)
36     : fChild(std::move(child)) {
37     SkASSERT(fChild);
38 
39     this->observeInval(fChild);
40 }
41 
~GeometryEffect()42 GeometryEffect::~GeometryEffect() {
43     this->unobserveInval(fChild);
44 }
45 
onClip(SkCanvas * canvas,bool antiAlias) const46 void GeometryEffect::onClip(SkCanvas* canvas, bool antiAlias) const {
47     canvas->clipPath(fPath, SkClipOp::kIntersect, antiAlias);
48 }
49 
onDraw(SkCanvas * canvas,const SkPaint & paint) const50 void GeometryEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
51     canvas->drawPath(fPath, paint);
52 }
53 
onContains(const SkPoint & p) const54 bool GeometryEffect::onContains(const SkPoint& p) const {
55     return fPath.contains(p.x(), p.y());
56 }
57 
onAsPath() const58 SkPath GeometryEffect::onAsPath() const {
59     return fPath;
60 }
61 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)62 SkRect GeometryEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
63     SkASSERT(this->hasInval());
64 
65     fChild->revalidate(ic, ctm);
66 
67     fPath = this->onRevalidateEffect(fChild);
68     SkPathPriv::ShrinkToFit(&fPath);
69 
70     return fPath.computeTightBounds();
71 }
72 
onRevalidateEffect(const sk_sp<GeometryNode> & child)73 SkPath TrimEffect::onRevalidateEffect(const sk_sp<GeometryNode>& child) {
74     SkPath path = child->asPath();
75 
76     if (const auto trim = SkTrimPathEffect::Make(fStart, fStop, fMode)) {
77         SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
78         SkASSERT(!trim->needsCTM());
79         SkAssertResult(trim->filterPath(&path, path, &rec, nullptr));
80     }
81 
82     return path;
83 }
84 
GeometryTransform(sk_sp<GeometryNode> child,sk_sp<Transform> transform)85 GeometryTransform::GeometryTransform(sk_sp<GeometryNode> child, sk_sp<Transform> transform)
86     : INHERITED(std::move(child))
87     , fTransform(std::move(transform)) {
88     SkASSERT(fTransform);
89     this->observeInval(fTransform);
90 }
91 
~GeometryTransform()92 GeometryTransform::~GeometryTransform() {
93     this->unobserveInval(fTransform);
94 }
95 
onRevalidateEffect(const sk_sp<GeometryNode> & child)96 SkPath GeometryTransform::onRevalidateEffect(const sk_sp<GeometryNode>& child) {
97     fTransform->revalidate(nullptr, SkMatrix::I());
98     const auto m = TransformPriv::As<SkMatrix>(fTransform);
99 
100     SkPath path = child->asPath();
101     path.transform(m);
102 
103     return path;
104 }
105 
106 namespace  {
107 
make_dash(const std::vector<float> & intervals,float phase)108 sk_sp<SkPathEffect> make_dash(const std::vector<float>& intervals, float phase) {
109     if (intervals.empty()) {
110         return nullptr;
111     }
112 
113     const auto* intervals_ptr   = intervals.data();
114     auto        intervals_count = intervals.size();
115 
116     STArray<32, float, true> storage;
117     if (intervals_count & 1) {
118         intervals_count *= 2;
119         storage.resize(intervals_count);
120         intervals_ptr = storage.data();
121 
122         std::copy(intervals.begin(), intervals.end(), storage.begin());
123         std::copy(intervals.begin(), intervals.end(), storage.begin() + intervals.size());
124     }
125 
126     return SkDashPathEffect::Make(intervals_ptr, SkToInt(intervals_count), phase);
127 }
128 
129 } // namespace
130 
onRevalidateEffect(const sk_sp<GeometryNode> & child)131 SkPath DashEffect::onRevalidateEffect(const sk_sp<GeometryNode>& child) {
132     SkPath path = child->asPath();
133 
134     if (const auto dash_patheffect = make_dash(fIntervals, fPhase)) {
135         SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
136         SkASSERT(!dash_patheffect->needsCTM());
137         dash_patheffect->filterPath(&path, path, &rec, nullptr);
138     }
139 
140     return path;
141 }
142 
onRevalidateEffect(const sk_sp<GeometryNode> & child)143 SkPath RoundEffect::onRevalidateEffect(const sk_sp<GeometryNode>& child) {
144     SkPath path = child->asPath();
145 
146     if (const auto round = SkCornerPathEffect::Make(fRadius)) {
147         SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
148         SkASSERT(!round->needsCTM());
149         SkAssertResult(round->filterPath(&path, path, &rec, nullptr));
150     }
151 
152     return path;
153 }
154 
onRevalidateEffect(const sk_sp<GeometryNode> & child)155 SkPath OffsetEffect::onRevalidateEffect(const sk_sp<GeometryNode>& child) {
156     SkPath path = child->asPath();
157 
158     if (!SkScalarNearlyZero(fOffset)) {
159         SkPaint paint;
160         paint.setStyle(SkPaint::kStroke_Style);
161         paint.setStrokeWidth(std::abs(fOffset) * 2);
162         paint.setStrokeMiter(fMiterLimit);
163         paint.setStrokeJoin(fJoin);
164 
165         SkPath fill_path;
166         skpathutils::FillPathWithPaint(path, paint, &fill_path, nullptr);
167 
168         if (fOffset > 0) {
169             Op(path, fill_path, kUnion_SkPathOp, &path);
170         } else {
171             Op(path, fill_path, kDifference_SkPathOp, &path);
172         }
173 
174         // TODO: this seems to break path combining (winding mismatch?)
175         // Simplify(path, &path);
176     }
177 
178     return path;
179 }
180 
181 }  // namespace sksg
182