2012-07-13 15:55:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPathEffect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkDashPathEffect.h"
|
|
|
|
#include "include/effects/SkTrimPathEffect.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/utils/SkParsePath.h"
|
2019-07-11 20:32:53 +00:00
|
|
|
#include "tools/timer/TimeUtils.h"
|
2012-07-13 15:55:15 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <math.h>
|
2018-06-18 19:11:00 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2012-07-13 15:55:15 +00:00
|
|
|
/*
|
|
|
|
* Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
|
|
|
|
*/
|
2016-03-18 18:22:57 +00:00
|
|
|
static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
|
|
|
|
SkPaint::Join join) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2020-05-19 01:25:44 +00:00
|
|
|
paint.setStroke(true);
|
2016-03-18 18:22:57 +00:00
|
|
|
paint.setStrokeJoin(join);
|
|
|
|
paint.setStrokeWidth(42);
|
|
|
|
canvas->drawPath(path, paint);
|
2012-07-13 15:55:15 +00:00
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStrokeWidth(21);
|
|
|
|
paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
|
|
|
|
canvas->drawPath(path, paint);
|
2012-07-13 15:55:15 +00:00
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setPathEffect(nullptr);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
canvas->drawPath(path, paint);
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2012-07-13 15:55:15 +00:00
|
|
|
|
2016-04-11 14:46:38 +00:00
|
|
|
DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
|
2015-02-23 20:47:03 +00:00
|
|
|
SkPath path;
|
|
|
|
const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
|
|
|
|
"C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
|
|
|
|
"C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
|
|
|
|
"C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
|
|
|
|
"C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
|
|
|
|
"C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
|
|
|
|
"C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
|
|
|
|
"C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
|
|
|
|
"C 283,231 344,195 338,98";
|
|
|
|
|
|
|
|
SkParsePath::FromSVGString(d, &path);
|
|
|
|
canvas->translate(-35.f, -55.f);
|
|
|
|
for (int x = 0; x < 2; ++x) {
|
|
|
|
for (int y = 0; y < 2; ++y) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x * 430.f, y * 355.f);
|
|
|
|
SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
|
|
|
|
flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2018-03-07 22:02:47 +00:00
|
|
|
|
|
|
|
class TrimGM : public skiagm::GM {
|
|
|
|
public:
|
2018-03-09 21:08:58 +00:00
|
|
|
TrimGM() {
|
|
|
|
SkAssertResult(SkParsePath::FromSVGString(
|
|
|
|
"M 0,100 C 10, 50 190, 50 200,100"
|
|
|
|
"M 200,100 C 210,150 390,150 400,100"
|
|
|
|
"M 400,100 C 390, 50 210, 50 200,100"
|
|
|
|
"M 200,100 C 190,150 10,150 0,100",
|
|
|
|
&fPaths.push_back()));
|
|
|
|
|
|
|
|
SkAssertResult(SkParsePath::FromSVGString(
|
|
|
|
"M 0, 75 L 200, 75"
|
|
|
|
"M 200, 91 L 200, 91"
|
|
|
|
"M 200,108 L 200,108"
|
|
|
|
"M 200,125 L 400,125",
|
|
|
|
&fPaths.push_back()));
|
|
|
|
|
|
|
|
SkAssertResult(SkParsePath::FromSVGString(
|
|
|
|
"M 0,100 L 50, 50"
|
|
|
|
"M 50, 50 L 150,150"
|
|
|
|
"M 150,150 L 250, 50"
|
|
|
|
"M 250, 50 L 350,150"
|
|
|
|
"M 350,150 L 400,100",
|
|
|
|
&fPaths.push_back()));
|
|
|
|
|
|
|
|
}
|
2018-03-07 22:02:47 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return SkString("trimpatheffect"); }
|
|
|
|
|
2018-03-09 21:08:58 +00:00
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(1400, 1000);
|
|
|
|
}
|
2018-03-07 22:02:47 +00:00
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2018-03-09 21:08:58 +00:00
|
|
|
static constexpr SkSize kCellSize = { 440, 150 };
|
|
|
|
static constexpr SkScalar kOffsets[][2] = {
|
|
|
|
{ -0.33f, -0.66f },
|
|
|
|
{ 0 , 1 },
|
|
|
|
{ 0 , 0.25f},
|
|
|
|
{ 0.25f, 0.75f},
|
|
|
|
{ 0.75f, 1 },
|
|
|
|
{ 1 , 0.75f},
|
|
|
|
};
|
2018-03-07 22:02:47 +00:00
|
|
|
|
2018-03-09 21:08:58 +00:00
|
|
|
SkPaint hairlinePaint;
|
|
|
|
hairlinePaint.setAntiAlias(true);
|
2020-05-19 01:25:44 +00:00
|
|
|
hairlinePaint.setStroke(true);
|
2018-03-09 21:08:58 +00:00
|
|
|
hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
|
|
|
|
hairlinePaint.setStrokeWidth(2);
|
|
|
|
SkPaint normalPaint = hairlinePaint;
|
|
|
|
normalPaint.setStrokeWidth(10);
|
|
|
|
normalPaint.setColor(0x8000ff00);
|
|
|
|
SkPaint invertedPaint = normalPaint;
|
|
|
|
invertedPaint.setColor(0x80ff0000);
|
|
|
|
|
|
|
|
for (const auto& offset : kOffsets) {
|
|
|
|
auto start = offset[0] + fOffset,
|
|
|
|
stop = offset[1] + fOffset;
|
|
|
|
|
|
|
|
auto normalMode = SkTrimPathEffect::Mode::kNormal,
|
|
|
|
invertedMode = SkTrimPathEffect::Mode::kInverted;
|
|
|
|
if (fOffset) {
|
|
|
|
start -= SkScalarFloorToScalar(start);
|
|
|
|
stop -= SkScalarFloorToScalar(stop);
|
|
|
|
if (start > stop) {
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(start, stop);
|
|
|
|
swap(normalMode, invertedMode);
|
2018-03-09 21:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
|
|
|
|
invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
|
|
|
|
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (const auto& path : fPaths) {
|
|
|
|
canvas->drawPath(path, normalPaint);
|
|
|
|
canvas->drawPath(path, invertedPaint);
|
|
|
|
canvas->drawPath(path, hairlinePaint);
|
|
|
|
canvas->translate(kCellSize.width(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->translate(0, kCellSize.height());
|
|
|
|
}
|
2018-03-07 22:02:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f;
|
2018-03-09 21:08:58 +00:00
|
|
|
fOffset -= floorf(fOffset);
|
2018-03-07 22:02:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-09 21:08:58 +00:00
|
|
|
|
2018-03-07 22:02:47 +00:00
|
|
|
private:
|
2018-03-09 21:08:58 +00:00
|
|
|
SkTArray<SkPath> fPaths;
|
|
|
|
SkScalar fOffset = 0;
|
|
|
|
|
2018-03-07 22:02:47 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM(return new TrimGM;)
|
|
|
|
|