Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 11 | #include "SkUTF.h" |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
| 13 | #include "SkColorFilter.h" |
| 14 | #include "SkImage.h" |
| 15 | #include "SkRandom.h" |
| 16 | #include "SkTime.h" |
| 17 | #include "SkTypeface.h" |
| 18 | #include "Timer.h" |
| 19 | |
| 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrContext.h" |
| 22 | #endif |
| 23 | |
| 24 | // Create an animation of a bunch of letters that rotate in place. This is intended to stress |
| 25 | // the glyph atlas and test that we don't see corruption or bad slowdowns. |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 26 | class FlutterAnimateView : public Sample { |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 27 | public: |
| 28 | FlutterAnimateView() : fCurrTime(0), fResetTime(0) {} |
| 29 | |
| 30 | protected: |
| 31 | void onOnceBeforeDraw() override { |
| 32 | fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf"); |
| 33 | initChars(); |
| 34 | } |
| 35 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 36 | bool onQuery(Sample::Event* evt) override { |
| 37 | if (Sample::TitleQ(*evt)) { |
| 38 | Sample::TitleR(evt, "FlutterAnimate"); |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
| 42 | return this->INHERITED::onQuery(evt); |
| 43 | } |
| 44 | |
| 45 | void onDrawContent(SkCanvas* canvas) override { |
| 46 | SkPaint paint; |
| 47 | paint.setTypeface(fTypeface); |
| 48 | paint.setAntiAlias(true); |
| 49 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 50 | paint.setTextSize(50); |
| 51 | |
| 52 | // rough center of each glyph |
| 53 | static constexpr auto kMidX = 35; |
| 54 | static constexpr auto kMidY = 50; |
| 55 | |
| 56 | canvas->clear(SK_ColorWHITE); |
| 57 | for (int i = 0; i < kNumChars; ++i) { |
| 58 | canvas->save(); |
| 59 | double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation, |
| 60 | fCurrTime/kDuration); |
| 61 | canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY); |
| 62 | canvas->rotate(SkRadiansToDegrees(rot)); |
| 63 | canvas->translate(-35,+50); |
| 64 | canvas->drawString(fChars[i].fChar, 0, 0, paint); |
| 65 | canvas->restore(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bool onAnimate(const SkAnimTimer& timer) override { |
| 70 | fCurrTime = timer.secs() - fResetTime; |
| 71 | if (fCurrTime > kDuration) { |
| 72 | this->initChars(); |
| 73 | fResetTime = timer.secs(); |
| 74 | fCurrTime = 0; |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | void initChars() { |
| 82 | for (int i = 0; i < kNumChars; ++i) { |
| 83 | char c = fRand.nextULessThan(26) + 65; |
| 84 | fChars[i].fChar[0] = c; |
| 85 | fChars[i].fChar[1] = '\0'; |
| 86 | fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10); |
| 87 | fChars[i].fStartRotation = fRand.nextF(); |
| 88 | fChars[i].fEndRotation = fRand.nextF() * 20 - 10; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static constexpr double kDuration = 5.0; |
| 93 | double fCurrTime; |
| 94 | double fResetTime; |
| 95 | SkRandom fRand; |
| 96 | |
| 97 | struct AnimatedChar { |
| 98 | char fChar[2]; |
| 99 | SkPoint fPosition; |
| 100 | SkScalar fStartRotation; |
| 101 | SkScalar fEndRotation; |
| 102 | }; |
| 103 | sk_sp<SkTypeface> fTypeface; |
| 104 | static constexpr int kNumChars = 40; |
| 105 | AnimatedChar fChars[kNumChars]; |
| 106 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 107 | typedef Sample INHERITED; |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 112 | DEF_SAMPLE( return new FlutterAnimateView(); ) |