blob: b723d60f0ee4d02aa3a617faa2f14dd7a295c08b [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:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000035 SkString getName() const override { return SkString("stringart"); }
jvanverth@google.com4ea28782013-09-19 15:32:22 +000036
Leandro Lovisolo8f023882023-08-15 21:13:52 +000037 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
jvanverth@google.com4ea28782013-09-19 15:32:22 +000038
mtklein36352bf2015-03-25 18:17:31 -070039 void onDraw(SkCanvas* canvas) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000040 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
Brian Osman7f364052020-02-06 11:25:43 -050041 SkScalar size = SkIntToScalar(std::min(kWidth, kHeight));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000042 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight));
43 SkScalar length = 5;
44 SkScalar step = angle;
45
46 SkPath path;
47 path.moveTo(center);
48
robertphillips651bb5f2016-04-08 13:35:14 -070049 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000050 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
51 length*SkScalarSin(step) + center.fY);
52 path.lineTo(rp);
reed80ea19c2015-05-12 10:37:34 -070053 length += angle / SkScalarHalf(SK_ScalarPI);
jvanverth@google.com4ea28782013-09-19 15:32:22 +000054 step += angle;
55 }
jvanverth@google.com4ea28782013-09-19 15:32:22 +000056
57 SkPaint paint;
58 paint.setAntiAlias(true);
59 paint.setStyle(SkPaint::kStroke_Style);
Mike Kleinea3f0142019-03-20 11:12:10 -050060 paint.setColor(ToolUtils::color_to_565(0xFF007700));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000061
62 canvas->drawPath(path, paint);
63 }
64
Hal Canary41248072019-07-11 16:32:53 -040065 bool onAnimate(double nanos) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070066 constexpr SkScalar kDesiredDurationSecs = 3.0f;
robertphillips651bb5f2016-04-08 13:35:14 -070067
68 // Make the animation ping-pong back and forth but start in the fully drawn state
Hal Canary41248072019-07-11 16:32:53 -040069 SkScalar fraction = 1.0f - TimeUtils::Scaled(1e-9 * nanos, 2.0f/kDesiredDurationSecs, 2.0f);
robertphillips651bb5f2016-04-08 13:35:14 -070070 if (fraction <= 0.0f) {
71 fraction = -fraction;
72 }
73
74 SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
75
76 fNumSteps = (int) (fraction * kMaxNumSteps);
77 return true;
78 }
79
jvanverth@google.com4ea28782013-09-19 15:32:22 +000080private:
robertphillips651bb5f2016-04-08 13:35:14 -070081 int fNumSteps;
82
John Stiles7571f9e2020-09-02 22:42:33 -040083 using INHERITED = GM;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000084};
85
86DEF_GM( return new StringArtGM; )
Mike Reedd4322a82018-08-13 16:03:25 -040087
88/////////////////////////////////////////////////////////////////////////////////////////////////
89
90#if 0
Mike Kleinc0bd9f92019-04-23 12:05:21 -050091#include "modules/skottie/include/Skottie.h"
Mike Reedd4322a82018-08-13 16:03:25 -040092
93class SkottieGM : public skiagm::GM {
94 enum {
95 kWidth = 800,
96 kHeight = 600,
97 };
98
99 enum {
100 N = 100,
101 };
102 skottie::Animation* fAnims[N];
103 SkRect fRects[N];
104 SkScalar fDur;
105
106public:
107 SkottieGM() {
108 sk_bzero(fAnims, sizeof(fAnims));
109 }
110 ~SkottieGM() override {
111 for (auto anim : fAnims) {
112 SkSafeUnref(anim);
113 }
114 }
115
116protected:
117
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000118 SkString getName() const override { return SkString("skottie"); }
Mike Reedd4322a82018-08-13 16:03:25 -0400119
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000120 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
Mike Reedd4322a82018-08-13 16:03:25 -0400121
122 void init() {
123 SkRandom rand;
124 auto data = SkData::MakeFromFileName("/Users/reed/Downloads/maps_pinlet.json");
125 // for (;;) skottie::Animation::Make((const char*)data->data(), data->size());
126 for (int i = 0; i < N; ++i) {
127 fAnims[i] = skottie::Animation::Make((const char*)data->data(), data->size()).release();
128 SkScalar x = rand.nextF() * kWidth;
129 SkScalar y = rand.nextF() * kHeight;
130 fRects[i].setXYWH(x, y, 400, 400);
131 }
132 fDur = fAnims[0]->duration();
133 }
134
135 void onDraw(SkCanvas* canvas) override {
136 if (!fAnims[0]) {
137 this->init();
138 }
139 canvas->drawColor(0xFFBBBBBB);
140 for (int i = 0; i < N; ++i) {
141 fAnims[0]->render(canvas, &fRects[i]);
142 }
143 }
144
Hal Canary41248072019-07-11 16:32:53 -0400145 bool onAnimate(double nanos) override {
146 SkScalar time = (float)(fmod(1e-9 * nanos, fDur) / fDur);
Mike Reedd4322a82018-08-13 16:03:25 -0400147 for (auto anim : fAnims) {
148 anim->seek(time);
149 }
150 return true;
151 }
152
153private:
John Stiles7571f9e2020-09-02 22:42:33 -0400154 using INHERITED = GM;
Mike Reedd4322a82018-08-13 16:03:25 -0400155};
156DEF_GM( return new SkottieGM; )
157#endif
158