jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 10 | #include "SkAnimTimer.h" |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
| 13 | |
| 14 | // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 |
| 15 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 16 | constexpr int kWidth = 440; |
| 17 | constexpr int kHeight = 440; |
| 18 | constexpr SkScalar kAngle = 0.305f; |
| 19 | constexpr int kMaxNumSteps = 140; |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 20 | |
| 21 | // Renders a string art shape. |
| 22 | // The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1 |
| 23 | |
| 24 | class StringArtGM : public skiagm::GM { |
| 25 | public: |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 26 | StringArtGM() : fNumSteps(kMaxNumSteps) {} |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 27 | |
| 28 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 31 | return SkString("stringart"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 35 | return SkISize::Make(kWidth, kHeight); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | void onDraw(SkCanvas* canvas) override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 39 | SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); |
jvanverth@google.com | eb95d6a | 2013-09-19 17:21:39 +0000 | [diff] [blame] | 40 | SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 41 | SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight)); |
| 42 | SkScalar length = 5; |
| 43 | SkScalar step = angle; |
| 44 | |
| 45 | SkPath path; |
| 46 | path.moveTo(center); |
| 47 | |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 48 | for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 49 | SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, |
| 50 | length*SkScalarSin(step) + center.fY); |
| 51 | path.lineTo(rp); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 52 | length += angle / SkScalarHalf(SK_ScalarPI); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 53 | step += angle; |
| 54 | } |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 55 | |
| 56 | SkPaint paint; |
| 57 | paint.setAntiAlias(true); |
| 58 | paint.setStyle(SkPaint::kStroke_Style); |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 59 | paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 60 | |
| 61 | canvas->drawPath(path, paint); |
| 62 | } |
| 63 | |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 64 | bool onAnimate(const SkAnimTimer& timer) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 65 | constexpr SkScalar kDesiredDurationSecs = 3.0f; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 66 | |
| 67 | // Make the animation ping-pong back and forth but start in the fully drawn state |
| 68 | SkScalar fraction = 1.0f - timer.scaled(2.0f/kDesiredDurationSecs, 2.0f); |
| 69 | if (fraction <= 0.0f) { |
| 70 | fraction = -fraction; |
| 71 | } |
| 72 | |
| 73 | SkASSERT(fraction >= 0.0f && fraction <= 1.0f); |
| 74 | |
| 75 | fNumSteps = (int) (fraction * kMaxNumSteps); |
| 76 | return true; |
| 77 | } |
| 78 | |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 79 | private: |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 80 | int fNumSteps; |
| 81 | |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 82 | typedef GM INHERITED; |
| 83 | }; |
| 84 | |
| 85 | DEF_GM( return new StringArtGM; ) |
Mike Reed | d4322a8 | 2018-08-13 16:03:25 -0400 | [diff] [blame] | 86 | |
| 87 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 88 | |
| 89 | #if 0 |
| 90 | #include "Skottie.h" |
| 91 | |
| 92 | class SkottieGM : public skiagm::GM { |
| 93 | enum { |
| 94 | kWidth = 800, |
| 95 | kHeight = 600, |
| 96 | }; |
| 97 | |
| 98 | enum { |
| 99 | N = 100, |
| 100 | }; |
| 101 | skottie::Animation* fAnims[N]; |
| 102 | SkRect fRects[N]; |
| 103 | SkScalar fDur; |
| 104 | |
| 105 | public: |
| 106 | SkottieGM() { |
| 107 | sk_bzero(fAnims, sizeof(fAnims)); |
| 108 | } |
| 109 | ~SkottieGM() override { |
| 110 | for (auto anim : fAnims) { |
| 111 | SkSafeUnref(anim); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | protected: |
| 116 | |
| 117 | SkString onShortName() override { return SkString("skottie"); } |
| 118 | |
| 119 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
| 120 | |
| 121 | void init() { |
| 122 | SkRandom rand; |
| 123 | auto data = SkData::MakeFromFileName("/Users/reed/Downloads/maps_pinlet.json"); |
| 124 | // for (;;) skottie::Animation::Make((const char*)data->data(), data->size()); |
| 125 | for (int i = 0; i < N; ++i) { |
| 126 | fAnims[i] = skottie::Animation::Make((const char*)data->data(), data->size()).release(); |
| 127 | SkScalar x = rand.nextF() * kWidth; |
| 128 | SkScalar y = rand.nextF() * kHeight; |
| 129 | fRects[i].setXYWH(x, y, 400, 400); |
| 130 | } |
| 131 | fDur = fAnims[0]->duration(); |
| 132 | } |
| 133 | |
| 134 | void onDraw(SkCanvas* canvas) override { |
| 135 | if (!fAnims[0]) { |
| 136 | this->init(); |
| 137 | } |
| 138 | canvas->drawColor(0xFFBBBBBB); |
| 139 | for (int i = 0; i < N; ++i) { |
| 140 | fAnims[0]->render(canvas, &fRects[i]); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool onAnimate(const SkAnimTimer& timer) override { |
| 145 | SkScalar time = (float)(fmod(timer.secs(), fDur) / fDur); |
| 146 | for (auto anim : fAnims) { |
| 147 | anim->seek(time); |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | typedef GM INHERITED; |
| 154 | }; |
| 155 | DEF_GM( return new SkottieGM; ) |
| 156 | #endif |
| 157 | |