| 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 |
#ifndef SkSGGeometryEffect_DEFINED |
| 9 |
#define SkSGGeometryEffect_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPath.h" |
| 13 |
#include "include/core/SkPathTypes.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/effects/SkTrimPathEffect.h" |
| 18 |
#include "modules/sksg/include/SkSGGeometryNode.h" |
| 19 |
#include "modules/sksg/include/SkSGNode.h" |
| 20 |
#include "modules/sksg/include/SkSGTransform.h" |
| 21 |
|
| 22 |
#include <utility> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
class SkCanvas; |
| 26 |
class SkMatrix; |
| 27 |
struct SkPoint; |
| 28 |
|
| 29 |
namespace sksg { |
| 30 |
class InvalidationController; |
| 31 |
|
| 32 |
/** |
| 33 |
* Base class for geometry effects. |
| 34 |
*/ |
| 35 |
class GeometryEffect : public GeometryNode { |
| 36 |
protected: |
| 37 |
explicit GeometryEffect(sk_sp<GeometryNode>); |
| 38 |
~GeometryEffect() override; |
| 39 |
|
| 40 |
void onClip(SkCanvas*, bool antiAlias) const final; |
| 41 |
void onDraw(SkCanvas*, const SkPaint&) const final; |
| 42 |
bool onContains(const SkPoint&) const final; |
| 43 |
|
| 44 |
SkRect onRevalidate(InvalidationController*, const SkMatrix&) final; |
| 45 |
SkPath onAsPath() const final; |
| 46 |
|
| 47 |
virtual SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) = 0; |
| 48 |
|
| 49 |
private: |
| 50 |
const sk_sp<GeometryNode> fChild; |
| 51 |
SkPath fPath; // transformed child cache. |
| 52 |
|
| 53 |
using INHERITED = GeometryNode; |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* Apply a trim effect to the child geometry. |
| 58 |
*/ |
| 59 |
class TrimEffect final : public GeometryEffect { |
| 60 |
public: |
| 61 |
static sk_sp<TrimEffect> Make(sk_sp<GeometryNode> child) { |
| 62 |
return child ? sk_sp<TrimEffect>(new TrimEffect(std::move(child))) : nullptr; |
| 63 |
} |
| 64 |
|
| 65 |
SG_ATTRIBUTE(Start , SkScalar , fStart ) |
| 66 |
SG_ATTRIBUTE(Stop , SkScalar , fStop ) |
| 67 |
SG_ATTRIBUTE(Mode , SkTrimPathEffect::Mode, fMode ) |
| 68 |
|
| 69 |
private: |
| 70 |
explicit TrimEffect(sk_sp<GeometryNode> child) : INHERITED(std::move(child)) {} |
| 71 |
|
| 72 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) override; |
| 73 |
|
| 74 |
SkScalar fStart = 0, |
| 75 |
fStop = 1; |
| 76 |
SkTrimPathEffect::Mode fMode = SkTrimPathEffect::Mode::kNormal; |
| 77 |
|
| 78 |
using INHERITED = GeometryEffect; |
| 79 |
}; |
| 80 |
|
| 81 |
/** |
| 82 |
* Apply a transform to a GeometryNode. |
| 83 |
*/ |
| 84 |
class GeometryTransform final : public GeometryEffect { |
| 85 |
public: |
| 86 |
static sk_sp<GeometryTransform> Make(sk_sp<GeometryNode> child, sk_sp<Transform> transform) { |
| 87 |
return child && transform |
| 88 |
? sk_sp<GeometryTransform>(new GeometryTransform(std::move(child), |
| 89 |
std::move(transform))) |
| 90 |
: nullptr; |
| 91 |
} |
| 92 |
|
| 93 |
~GeometryTransform() override; |
| 94 |
|
| 95 |
const sk_sp<Transform>& getTransform() const { return fTransform; } |
| 96 |
|
| 97 |
private: |
| 98 |
GeometryTransform(sk_sp<GeometryNode>, sk_sp<Transform>); |
| 99 |
|
| 100 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) override; |
| 101 |
|
| 102 |
const sk_sp<Transform> fTransform; |
| 103 |
|
| 104 |
using INHERITED = GeometryEffect; |
| 105 |
}; |
| 106 |
|
| 107 |
/** |
| 108 |
* Overrides the child geometry fill type. |
| 109 |
*/ |
| 110 |
class FillTypeOverride final : public GeometryEffect { |
| 111 |
public: |
| 112 |
static sk_sp<FillTypeOverride> Make(sk_sp<GeometryNode> child, SkPathFillType ft) { |
| 113 |
return child ? sk_sp<FillTypeOverride>(new FillTypeOverride(std::move(child), ft)) |
| 114 |
: nullptr; |
| 115 |
} |
| 116 |
|
| 117 |
SG_ATTRIBUTE(FillType, SkPathFillType, fFillType) |
| 118 |
|
| 119 |
private: |
| 120 |
FillTypeOverride(sk_sp<GeometryNode> child, SkPathFillType ft) |
| 121 |
: INHERITED(std::move(child)) |
| 122 |
, fFillType(ft) |
| 123 |
{} |
| 124 |
|
| 125 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) override; |
| 126 |
|
| 127 |
SkPathFillType fFillType = SkPathFillType::kDefault; |
| 128 |
|
| 129 |
using INHERITED = GeometryEffect; |
| 130 |
}; |
| 131 |
|
| 132 |
/** |
| 133 |
* Apply a dash effect to the child geometry. |
| 134 |
* |
| 135 |
* Follows the same semantics as SkDashPathEffect, with one minor tweak: when the number of |
| 136 |
* intervals is odd, they are repeated once more to attain an even sequence (same as SVG |
| 137 |
* stroke-dasharray: https://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty). |
| 138 |
*/ |
| 139 |
class DashEffect final : public GeometryEffect { |
| 140 |
public: |
| 141 |
static sk_sp<DashEffect> Make(sk_sp<GeometryNode> child) { |
| 142 |
return child ? sk_sp<DashEffect>(new DashEffect(std::move(child))) : nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
SG_ATTRIBUTE(Intervals, std::vector<float>, fIntervals) |
| 146 |
SG_ATTRIBUTE(Phase, float , fPhase ) |
| 147 |
|
| 148 |
private: |
| 149 |
explicit DashEffect(sk_sp<GeometryNode> child) : INHERITED(std::move(child)) {} |
| 150 |
|
| 151 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) override; |
| 152 |
|
| 153 |
std::vector<float> fIntervals; |
| 154 |
float fPhase = 0; |
| 155 |
|
| 156 |
using INHERITED = GeometryEffect; |
| 157 |
}; |
| 158 |
|
| 159 |
/** |
| 160 |
* Apply a rounded-corner effect to the child geometry. |
| 161 |
*/ |
| 162 |
class RoundEffect final : public GeometryEffect { |
| 163 |
public: |
| 164 |
static sk_sp<RoundEffect> Make(sk_sp<GeometryNode> child) { |
| 165 |
return child ? sk_sp<RoundEffect>(new RoundEffect(std::move(child))) : nullptr; |
| 166 |
} |
| 167 |
|
| 168 |
SG_ATTRIBUTE(Radius, SkScalar, fRadius) |
| 169 |
|
| 170 |
private: |
| 171 |
explicit RoundEffect(sk_sp<GeometryNode> child) : INHERITED(std::move(child)) {} |
| 172 |
|
| 173 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) override; |
| 174 |
|
| 175 |
SkScalar fRadius = 0; |
| 176 |
|
| 177 |
using INHERITED = GeometryEffect; |
| 178 |
}; |
| 179 |
|
| 180 |
/** |
| 181 |
* Apply an offset effect to the child geometry. |
| 182 |
*/ |
| 183 |
class OffsetEffect final : public GeometryEffect { |
| 184 |
public: |
| 185 |
static sk_sp<OffsetEffect> Make(sk_sp<GeometryNode> child) { |
| 186 |
return child ? sk_sp<OffsetEffect>(new OffsetEffect(std::move(child))) : nullptr; |
| 187 |
} |
| 188 |
|
| 189 |
SG_ATTRIBUTE(Offset , SkScalar , fOffset ) |
| 190 |
SG_ATTRIBUTE(MiterLimit , SkScalar , fMiterLimit) |
| 191 |
SG_ATTRIBUTE(Join , SkPaint::Join, fJoin ) |
| 192 |
|
| 193 |
private: |
| 194 |
explicit OffsetEffect(sk_sp<GeometryNode> child) : INHERITED(std::move(child)) {} |
| 195 |
|
| 196 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>&, const SkMatrix&) override; |
| 197 |
|
| 198 |
SkScalar fOffset = 0, |
| 199 |
fMiterLimit = 4; |
| 200 |
SkPaint::Join fJoin = SkPaint::kMiter_Join; |
| 201 |
|
| 202 |
using INHERITED = GeometryEffect; |
| 203 |
}; |
| 204 |
|
| 205 |
} // namespace sksg |
| 206 |
|
| 207 |
#endif // SkSGGeometryEffect_DEFINED |