blob: 5d5302671c6a461500cc9df54006b818ecbe0864 [file] [log] [blame]
jvanverth@google.com4ea28782013-09-19 15:32:22 +00001/*
2 * Copyright 2013 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkPoint.h"
13#include "include/core/SkScalar.h"
14#include "include/core/SkSize.h"
15#include "include/core/SkString.h"
16#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "tools/ToolUtils.h"
Hal Canary41248072019-07-11 16:32:53 -040018#include "tools/timer/TimeUtils.h"
jvanverth@google.com4ea28782013-09-19 15:32:22 +000019
20// Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
21
mtkleindbfd7ab2016-09-01 11:24:54 -070022constexpr int kWidth = 440;
23constexpr int kHeight = 440;
24constexpr SkScalar kAngle = 0.305f;
25constexpr int kMaxNumSteps = 140;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000026
27// Renders a string art shape.
28// The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
29
30class StringArtGM : public skiagm::GM {
31public:
robertphillips651bb5f2016-04-08 13:35:14 -070032 StringArtGM() : fNumSteps(kMaxNumSteps) {}
jvanverth@google.com4ea28782013-09-19 15:32:22 +000033
34protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000035
mtklein36352bf2015-03-25 18:17:31 -070036 SkString onShortName() override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000037 return SkString("stringart");
38 }
39
mtklein36352bf2015-03-25 18:17:31 -070040 SkISize onISize() override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000041 return SkISize::Make(kWidth, kHeight);
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 void onDraw(SkCanvas* canvas) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000045 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
Brian Osman7f364052020-02-06 11:25:43 -050046 SkScalar size = SkIntToScalar(std::min(kWidth, kHeight));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000047 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight));
48 SkScalar length = 5;
49 SkScalar step = angle;
50
51 SkPath path;
52 path.moveTo(center);
53
robertphillips651bb5f2016-04-08 13:35:14 -070054 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000055 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
56 length*SkScalarSin(step) + center.fY);
57 path.lineTo(rp);
reed80ea19c2015-05-12 10:37:34 -070058 length += angle / SkScalarHalf(SK_ScalarPI);
jvanverth@google.com4ea28782013-09-19 15:32:22 +000059 step += angle;
60 }
jvanverth@google.com4ea28782013-09-19 15:32:22 +000061
62 SkPaint paint;
63 paint.setAntiAlias(true);
64 paint.setStyle(SkPaint::kStroke_Style);
Mike Kleinea3f0142019-03-20 11:12:10 -050065 paint.setColor(ToolUtils::color_to_565(0xFF007700));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000066
67 canvas->drawPath(path, paint);
68 }
69
Hal Canary41248072019-07-11 16:32:53 -040070 bool onAnimate(double nanos) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070071 constexpr SkScalar kDesiredDurationSecs = 3.0f;
robertphillips651bb5f2016-04-08 13:35:14 -070072
73 // Make the animation ping-pong back and forth but start in the fully drawn state
Hal Canary41248072019-07-11 16:32:53 -040074 SkScalar fraction = 1.0f - TimeUtils::Scaled(1e-9 * nanos, 2.0f/kDesiredDurationSecs, 2.0f);
robertphillips651bb5f2016-04-08 13:35:14 -070075 if (fraction <= 0.0f) {
76 fraction = -fraction;
77 }
78
79 SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
80
81 fNumSteps = (int) (fraction * kMaxNumSteps);
82 return true;
83 }
84
jvanverth@google.com4ea28782013-09-19 15:32:22 +000085private:
robertphillips651bb5f2016-04-08 13:35:14 -070086 int fNumSteps;
87
John Stiles7571f9e2020-09-02 22:42:33 -040088 using INHERITED = GM;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000089};
90
91DEF_GM( return new StringArtGM; )
Mike Reedd4322a82018-08-13 16:03:25 -040092
93/////////////////////////////////////////////////////////////////////////////////////////////////
94
95#if 0
Mike Kleinc0bd9f92019-04-23 12:05:21 -050096#include "modules/skottie/include/Skottie.h"
Mike Reedd4322a82018-08-13 16:03:25 -040097
98class SkottieGM : public skiagm::GM {
99 enum {
100 kWidth = 800,
101 kHeight = 600,
102 };
103
104 enum {
105 N = 100,
106 };
107 skottie::Animation* fAnims[N];
108 SkRect fRects[N];
109 SkScalar fDur;
110
111public:
112 SkottieGM() {
113 sk_bzero(fAnims, sizeof(fAnims));
114 }
115 ~SkottieGM() override {
116 for (auto anim : fAnims) {
117 SkSafeUnref(anim);
118 }
119 }
120
121protected:
122
123 SkString onShortName() override { return SkString("skottie"); }
124
125 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
126
127 void init() {
128 SkRandom rand;
129 auto data = SkData::MakeFromFileName("/Users/reed/Downloads/maps_pinlet.json");
130 // for (;;) skottie::Animation::Make((const char*)data->data(), data->size());
131 for (int i = 0; i < N; ++i) {
132 fAnims[i] = skottie::Animation::Make((const char*)data->data(), data->size()).release();
133 SkScalar x = rand.nextF() * kWidth;
134 SkScalar y = rand.nextF() * kHeight;
135 fRects[i].setXYWH(x, y, 400, 400);
136 }
137 fDur = fAnims[0]->duration();
138 }
139
140 void onDraw(SkCanvas* canvas) override {
141 if (!fAnims[0]) {
142 this->init();
143 }
144 canvas->drawColor(0xFFBBBBBB);
145 for (int i = 0; i < N; ++i) {
146 fAnims[0]->render(canvas, &fRects[i]);
147 }
148 }
149
Hal Canary41248072019-07-11 16:32:53 -0400150 bool onAnimate(double nanos) override {
151 SkScalar time = (float)(fmod(1e-9 * nanos, fDur) / fDur);
Mike Reedd4322a82018-08-13 16:03:25 -0400152 for (auto anim : fAnims) {
153 anim->seek(time);
154 }
155 return true;
156 }
157
158private:
John Stiles7571f9e2020-09-02 22:42:33 -0400159 using INHERITED = GM;
Mike Reedd4322a82018-08-13 16:03:25 -0400160};
161DEF_GM( return new SkottieGM; )
162#endif
163