| 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/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPath.h" |
| 13 |
#include "include/core/SkPathEffect.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "include/core/SkTypes.h" |
| 18 |
#include "include/effects/SkDashPathEffect.h" |
| 19 |
#include "include/effects/SkTrimPathEffect.h" |
| 20 |
#include "include/private/base/SkTArray.h" |
| 21 |
#include "include/utils/SkParsePath.h" |
| 22 |
#include "tools/timer/TimeUtils.h" |
| 23 |
|
| 24 |
#include <math.h> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
using namespace skia_private; |
| 28 |
|
| 29 |
/* |
| 30 |
* Inspired by http://code.google.com/p/chromium/issues/detail?id=112145 |
| 31 |
*/ |
| 32 |
static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2], |
| 33 |
SkPaint::Join join) { |
| 34 |
SkPaint paint; |
| 35 |
paint.setAntiAlias(true); |
| 36 |
paint.setStroke(true); |
| 37 |
paint.setStrokeJoin(join); |
| 38 |
paint.setStrokeWidth(42); |
| 39 |
canvas->drawPath(path, paint); |
| 40 |
|
| 41 |
paint.setColor(SK_ColorRED); |
| 42 |
paint.setStrokeWidth(21); |
| 43 |
paint.setPathEffect(SkDashPathEffect::Make({intervals, 2}, 0)); |
| 44 |
canvas->drawPath(path, paint); |
| 45 |
|
| 46 |
paint.setColor(SK_ColorGREEN); |
| 47 |
paint.setPathEffect(nullptr); |
| 48 |
paint.setStrokeWidth(0); |
| 49 |
canvas->drawPath(path, paint); |
| 50 |
} |
| 51 |
|
| 52 |
DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) { |
| 53 |
const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212" |
| 54 |
"C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231" |
| 55 |
"C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250" |
| 56 |
"C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269" |
| 57 |
"C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288" |
| 58 |
"C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269" |
| 59 |
"C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250" |
| 60 |
"C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231" |
| 61 |
"C 283,231 344,195 338,98"; |
| 62 |
|
| 63 |
SkPath path = SkParsePath::FromSVGString(d).value_or(SkPath()); |
| 64 |
canvas->translate(-35.f, -55.f); |
| 65 |
for (int x = 0; x < 2; ++x) { |
| 66 |
for (int y = 0; y < 2; ++y) { |
| 67 |
canvas->save(); |
| 68 |
canvas->translate(x * 430.f, y * 355.f); |
| 69 |
SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 }; |
| 70 |
flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join); |
| 71 |
canvas->restore(); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
class TrimGM : public skiagm::GM { |
| 77 |
public: |
| 78 |
TrimGM() {} |
| 79 |
|
| 80 |
void onOnceBeforeDraw() override { |
| 81 |
std::optional<SkPath> path; |
| 82 |
SkAssertResult((path = SkParsePath::FromSVGString( |
| 83 |
"M 0,100 C 10, 50 190, 50 200,100" |
| 84 |
"M 200,100 C 210,150 390,150 400,100" |
| 85 |
"M 400,100 C 390, 50 210, 50 200,100" |
| 86 |
"M 200,100 C 190,150 10,150 0,100"))); |
| 87 |
fPaths.push_back(*path); |
| 88 |
|
| 89 |
SkAssertResult((path = SkParsePath::FromSVGString( |
| 90 |
"M 0, 75 L 200, 75" |
| 91 |
"M 200, 91 L 200, 91" |
| 92 |
"M 200,108 L 200,108" |
| 93 |
"M 200,125 L 400,125"))); |
| 94 |
fPaths.push_back(*path); |
| 95 |
|
| 96 |
SkAssertResult((path = SkParsePath::FromSVGString( |
| 97 |
"M 0,100 L 50, 50" |
| 98 |
"M 50, 50 L 150,150" |
| 99 |
"M 150,150 L 250, 50" |
| 100 |
"M 250, 50 L 350,150" |
| 101 |
"M 350,150 L 400,100"))); |
| 102 |
fPaths.push_back(*path); |
| 103 |
} |
| 104 |
|
| 105 |
protected: |
| 106 |
SkString getName() const override { return SkString("trimpatheffect"); } |
| 107 |
|
| 108 |
SkISize getISize() override { return SkISize::Make(1400, 1000); } |
| 109 |
|
| 110 |
void onDraw(SkCanvas* canvas) override { |
| 111 |
static constexpr SkSize kCellSize = { 440, 150 }; |
| 112 |
static constexpr SkScalar kOffsets[][2] = { |
| 113 |
{ -0.33f, -0.66f }, |
| 114 |
{ 0 , 1 }, |
| 115 |
{ 0 , 0.25f}, |
| 116 |
{ 0.25f, 0.75f}, |
| 117 |
{ 0.75f, 1 }, |
| 118 |
{ 1 , 0.75f}, |
| 119 |
}; |
| 120 |
|
| 121 |
SkPaint hairlinePaint; |
| 122 |
hairlinePaint.setAntiAlias(true); |
| 123 |
hairlinePaint.setStroke(true); |
| 124 |
hairlinePaint.setStrokeCap(SkPaint::kRound_Cap); |
| 125 |
hairlinePaint.setStrokeWidth(2); |
| 126 |
SkPaint normalPaint = hairlinePaint; |
| 127 |
normalPaint.setStrokeWidth(10); |
| 128 |
normalPaint.setColor(0x8000ff00); |
| 129 |
SkPaint invertedPaint = normalPaint; |
| 130 |
invertedPaint.setColor(0x80ff0000); |
| 131 |
|
| 132 |
for (const auto& offset : kOffsets) { |
| 133 |
auto start = offset[0] + fOffset, |
| 134 |
stop = offset[1] + fOffset; |
| 135 |
|
| 136 |
auto normalMode = SkTrimPathEffect::Mode::kNormal, |
| 137 |
invertedMode = SkTrimPathEffect::Mode::kInverted; |
| 138 |
if (fOffset) { |
| 139 |
start -= SkScalarFloorToScalar(start); |
| 140 |
stop -= SkScalarFloorToScalar(stop); |
| 141 |
if (start > stop) { |
| 142 |
using std::swap; |
| 143 |
swap(start, stop); |
| 144 |
swap(normalMode, invertedMode); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode)); |
| 149 |
invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode)); |
| 150 |
|
| 151 |
{ |
| 152 |
SkAutoCanvasRestore acr(canvas, true); |
| 153 |
for (const auto& path : fPaths) { |
| 154 |
canvas->drawPath(path, normalPaint); |
| 155 |
canvas->drawPath(path, invertedPaint); |
| 156 |
canvas->drawPath(path, hairlinePaint); |
| 157 |
canvas->translate(kCellSize.width(), 0); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
canvas->translate(0, kCellSize.height()); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
bool onAnimate(double nanos) override { |
| 166 |
fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f; |
| 167 |
fOffset -= floorf(fOffset); |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
private: |
| 172 |
TArray<SkPath> fPaths; |
| 173 |
SkScalar fOffset = 0; |
| 174 |
|
| 175 |
using INHERITED = skiagm::GM; |
| 176 |
}; |
| 177 |
DEF_GM(return new TrimGM;) |
| 178 |
|