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