| 1 |
/* |
| 2 |
* Copyright 2012 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 "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPathBuilder.h" |
| 13 |
#include "include/core/SkPathEffect.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/effects/Sk1DPathEffect.h" |
| 21 |
#include "include/effects/Sk2DPathEffect.h" |
| 22 |
#include "include/effects/SkCornerPathEffect.h" |
| 23 |
#include "include/effects/SkDashPathEffect.h" |
| 24 |
#include "include/effects/SkDiscretePathEffect.h" |
| 25 |
|
| 26 |
#if defined(SK_GANESH) |
| 27 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <initializer_list> |
| 31 |
|
| 32 |
namespace skiagm { |
| 33 |
|
| 34 |
static void compose_pe(SkPaint* paint) { |
| 35 |
SkPathEffect* pe = paint->getPathEffect(); |
| 36 |
sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25); |
| 37 |
sk_sp<SkPathEffect> compose; |
| 38 |
if (pe) { |
| 39 |
compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner); |
| 40 |
} else { |
| 41 |
compose = corner; |
| 42 |
} |
| 43 |
paint->setPathEffect(compose); |
| 44 |
} |
| 45 |
|
| 46 |
static void hair_pe(SkPaint* paint) { |
| 47 |
paint->setStrokeWidth(0); |
| 48 |
} |
| 49 |
|
| 50 |
static void hair2_pe(SkPaint* paint) { |
| 51 |
paint->setStrokeWidth(0); |
| 52 |
compose_pe(paint); |
| 53 |
} |
| 54 |
|
| 55 |
static void stroke_pe(SkPaint* paint) { |
| 56 |
paint->setStrokeWidth(12); |
| 57 |
compose_pe(paint); |
| 58 |
} |
| 59 |
|
| 60 |
static void dash_pe(SkPaint* paint) { |
| 61 |
SkScalar inter[] = { 20, 10, 10, 10 }; |
| 62 |
paint->setStrokeWidth(12); |
| 63 |
paint->setPathEffect(SkDashPathEffect::Make(inter, 0)); |
| 64 |
compose_pe(paint); |
| 65 |
} |
| 66 |
|
| 67 |
constexpr int gXY[] = { |
| 68 |
4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 69 |
}; |
| 70 |
|
| 71 |
static SkPath scale(const SkPath& path, SkScalar scale) { |
| 72 |
SkMatrix m; |
| 73 |
m.setScale(scale, scale); |
| 74 |
return path.makeTransform(m); |
| 75 |
} |
| 76 |
|
| 77 |
static void one_d_pe(SkPaint* paint) { |
| 78 |
SkPathBuilder b; |
| 79 |
b.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 80 |
for (unsigned i = 2; i < std::size(gXY); i += 2) { |
| 81 |
b.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 82 |
} |
| 83 |
b.close().offset(SkIntToScalar(-6), 0); |
| 84 |
SkPath path = scale(b.detach(), 1.5f); |
| 85 |
|
| 86 |
paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0, |
| 87 |
SkPath1DPathEffect::kRotate_Style)); |
| 88 |
compose_pe(paint); |
| 89 |
} |
| 90 |
|
| 91 |
typedef void (*PE_Proc)(SkPaint*); |
| 92 |
constexpr PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; |
| 93 |
|
| 94 |
static void fill_pe(SkPaint* paint) { |
| 95 |
paint->setStyle(SkPaint::kFill_Style); |
| 96 |
paint->setPathEffect(nullptr); |
| 97 |
} |
| 98 |
|
| 99 |
static void discrete_pe(SkPaint* paint) { |
| 100 |
paint->setPathEffect(SkDiscretePathEffect::Make(10, 4)); |
| 101 |
} |
| 102 |
|
| 103 |
static sk_sp<SkPathEffect> MakeTileEffect() { |
| 104 |
SkMatrix m; |
| 105 |
m.setScale(SkIntToScalar(12), SkIntToScalar(12)); |
| 106 |
|
| 107 |
return SkPath2DPathEffect::Make(m, SkPath::Circle(0,0,5)); |
| 108 |
} |
| 109 |
|
| 110 |
static void tile_pe(SkPaint* paint) { |
| 111 |
paint->setPathEffect(MakeTileEffect()); |
| 112 |
} |
| 113 |
|
| 114 |
constexpr PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; |
| 115 |
|
| 116 |
class PathEffectGM : public GM { |
| 117 |
public: |
| 118 |
PathEffectGM() {} |
| 119 |
|
| 120 |
protected: |
| 121 |
SkString getName() const override { return SkString("patheffect"); } |
| 122 |
|
| 123 |
SkISize getISize() override { return SkISize::Make(800, 600); } |
| 124 |
|
| 125 |
void onDraw(SkCanvas* canvas) override { |
| 126 |
SkPaint paint; |
| 127 |
paint.setAntiAlias(true); |
| 128 |
paint.setStyle(SkPaint::kStroke_Style); |
| 129 |
|
| 130 |
SkPath path = SkPath::Polygon({{ |
| 131 |
{20, 20}, |
| 132 |
{70, 120}, |
| 133 |
{120, 30}, |
| 134 |
{170, 80}, |
| 135 |
{240, 50}, |
| 136 |
}}, false); |
| 137 |
|
| 138 |
canvas->save(); |
| 139 |
for (size_t i = 0; i < std::size(gPE); i++) { |
| 140 |
gPE[i](&paint); |
| 141 |
canvas->drawPath(path, paint); |
| 142 |
canvas->translate(0, 75); |
| 143 |
} |
| 144 |
canvas->restore(); |
| 145 |
|
| 146 |
SkRect r = { 0, 0, 250, 120 }; |
| 147 |
path = SkPathBuilder().addOval(r, SkPathDirection::kCW) |
| 148 |
.addRect(r.makeInset(50, 50), SkPathDirection::kCCW) |
| 149 |
.detach(); |
| 150 |
|
| 151 |
canvas->translate(320, 20); |
| 152 |
for (size_t i = 0; i < std::size(gPE2); i++) { |
| 153 |
gPE2[i](&paint); |
| 154 |
canvas->drawPath(path, paint); |
| 155 |
canvas->translate(0, 160); |
| 156 |
} |
| 157 |
|
| 158 |
const SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60); |
| 159 |
for (size_t i = 0; i < std::size(gPE); i++) { |
| 160 |
SkPaint p; |
| 161 |
p.setAntiAlias(true); |
| 162 |
p.setStyle(SkPaint::kFill_Style); |
| 163 |
gPE[i](&p); |
| 164 |
canvas->drawIRect(rect, p); |
| 165 |
canvas->translate(75, 0); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
private: |
| 170 |
using INHERITED = GM; |
| 171 |
}; |
| 172 |
|
| 173 |
DEF_GM( return new PathEffectGM; ) |
| 174 |
|
| 175 |
} // namespace skiagm |
| 176 |
|
| 177 |
////////////////////////////////////////////////////////////////////////////// |
| 178 |
|
| 179 |
#include "include/core/SkStrokeRec.h" |
| 180 |
#include "src/core/SkPathEffectBase.h" |
| 181 |
|
| 182 |
namespace { |
| 183 |
/** |
| 184 |
* Example path effect using CTM. This "strokes" a single line segment with some stroke width, |
| 185 |
* and then inflates the result by some number of pixels. |
| 186 |
*/ |
| 187 |
class StrokeLineInflated : public SkPathEffectBase { |
| 188 |
public: |
| 189 |
StrokeLineInflated(float strokeWidth, float pxInflate) |
| 190 |
: fRadius(strokeWidth / 2.f), fPxInflate(pxInflate) {} |
| 191 |
|
| 192 |
bool onNeedsCTM() const final { return true; } |
| 193 |
|
| 194 |
bool onFilterPath(SkPathBuilder* dst, |
| 195 |
const SkPath& src, |
| 196 |
SkStrokeRec* rec, |
| 197 |
const SkRect* cullR, |
| 198 |
const SkMatrix& ctm) const final { |
| 199 |
SkSpan<const SkPoint> pts = src.points(); |
| 200 |
SkASSERT(pts.size() == 2); |
| 201 |
|
| 202 |
SkMatrix invCtm; |
| 203 |
if (!ctm.invert(&invCtm)) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// For a line segment, we can just map the (scaled) normal vector to pixel-space, |
| 208 |
// increase its length by the desired number of pixels, and then map back to canvas space. |
| 209 |
SkPoint n = {pts[0].fY - pts[1].fY, pts[1].fX - pts[0].fX}; |
| 210 |
if (!n.setLength(fRadius)) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
SkPoint mappedN = ctm.mapVector(n.fX, n.fY); |
| 215 |
if (!mappedN.setLength(mappedN.length() + fPxInflate)) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
n = invCtm.mapVector(mappedN.fX, mappedN.fY); |
| 219 |
|
| 220 |
dst->moveTo(pts[0] + n); |
| 221 |
dst->lineTo(pts[1] + n); |
| 222 |
dst->lineTo(pts[1] - n); |
| 223 |
dst->lineTo(pts[0] - n); |
| 224 |
dst->close(); |
| 225 |
|
| 226 |
rec->setFillStyle(); |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
protected: |
| 232 |
void flatten(SkWriteBuffer&) const final {} |
| 233 |
|
| 234 |
private: |
| 235 |
SK_FLATTENABLE_HOOKS(StrokeLineInflated) |
| 236 |
|
| 237 |
bool computeFastBounds(SkRect* bounds) const final { return false; } |
| 238 |
|
| 239 |
const float fRadius; |
| 240 |
const float fPxInflate; |
| 241 |
}; |
| 242 |
|
| 243 |
sk_sp<SkFlattenable> StrokeLineInflated::CreateProc(SkReadBuffer&) { return nullptr; } |
| 244 |
|
| 245 |
} // namespace |
| 246 |
|
| 247 |
class CTMPathEffectGM : public skiagm::GM { |
| 248 |
protected: |
| 249 |
SkString getName() const override { return SkString("ctmpatheffect"); } |
| 250 |
|
| 251 |
SkISize getISize() override { return SkISize::Make(800, 600); } |
| 252 |
|
| 253 |
#if defined(SK_GANESH) |
| 254 |
// CTM-aware path effects are not supported by Ganesh |
| 255 |
DrawResult onGpuSetup(SkCanvas* canvas, SkString*, GraphiteTestContext*) override { |
| 256 |
auto dctx = GrAsDirectContext(canvas->recordingContext()); |
| 257 |
return dctx == nullptr ? DrawResult::kOk : DrawResult::kSkip; |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
void onDraw(SkCanvas* canvas) override { |
| 262 |
const float strokeWidth = 16; |
| 263 |
const float pxInflate = 0.5f; |
| 264 |
sk_sp<SkPathEffect> pathEffect(new StrokeLineInflated(strokeWidth, pxInflate)); |
| 265 |
|
| 266 |
SkPath path = SkPath::Line({100, 100}, {200, 200}); |
| 267 |
|
| 268 |
// Draw the inflated path, and a scaled version, in blue. |
| 269 |
SkPaint paint; |
| 270 |
paint.setAntiAlias(true); |
| 271 |
paint.setColor(SkColorSetA(SK_ColorBLUE, 0xff)); |
| 272 |
paint.setPathEffect(pathEffect); |
| 273 |
canvas->drawPath(path, paint); |
| 274 |
canvas->save(); |
| 275 |
canvas->translate(150, 0); |
| 276 |
canvas->scale(2.5, 0.5f); |
| 277 |
canvas->drawPath(path, paint); |
| 278 |
canvas->restore(); |
| 279 |
|
| 280 |
// Draw the regular stroked version on top in green. |
| 281 |
// The inflated version should be visible underneath as a blue "border". |
| 282 |
paint.setPathEffect(nullptr); |
| 283 |
paint.setStyle(SkPaint::kStroke_Style); |
| 284 |
paint.setStrokeWidth(strokeWidth); |
| 285 |
paint.setColor(SkColorSetA(SK_ColorGREEN, 0xff)); |
| 286 |
canvas->drawPath(path, paint); |
| 287 |
canvas->save(); |
| 288 |
canvas->translate(150, 0); |
| 289 |
canvas->scale(2.5, 0.5f); |
| 290 |
canvas->drawPath(path, paint); |
| 291 |
canvas->restore(); |
| 292 |
} |
| 293 |
|
| 294 |
private: |
| 295 |
using INHERITED = GM; |
| 296 |
}; |
| 297 |
DEF_GM(return new CTMPathEffectGM;) |