SampleFlutterAnimate.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkColorFilter.h"
  9. #include "include/core/SkColorPriv.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkTime.h"
  13. #include "include/core/SkTypeface.h"
  14. #include "include/utils/SkRandom.h"
  15. #include "samplecode/Sample.h"
  16. #include "tools/timer/Timer.h"
  17. #if SK_SUPPORT_GPU
  18. #include "include/gpu/GrContext.h"
  19. #endif
  20. // Create an animation of a bunch of letters that rotate in place. This is intended to stress
  21. // the glyph atlas and test that we don't see corruption or bad slowdowns.
  22. class FlutterAnimateView : public Sample {
  23. public:
  24. FlutterAnimateView() : fCurrTime(0), fResetTime(0) {}
  25. protected:
  26. void onOnceBeforeDraw() override {
  27. fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf");
  28. initChars();
  29. }
  30. SkString name() override { return SkString("FlutterAnimate"); }
  31. void onDrawContent(SkCanvas* canvas) override {
  32. SkFont font(fTypeface, 50);
  33. SkPaint paint;
  34. paint.setFilterQuality(kMedium_SkFilterQuality);
  35. // rough center of each glyph
  36. static constexpr auto kMidX = 35;
  37. static constexpr auto kMidY = 50;
  38. canvas->clear(SK_ColorWHITE);
  39. for (int i = 0; i < kNumChars; ++i) {
  40. canvas->save();
  41. double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation,
  42. fCurrTime/kDuration);
  43. canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY);
  44. canvas->rotate(SkRadiansToDegrees(rot));
  45. canvas->translate(-35,+50);
  46. canvas->drawString(fChars[i].fChar, 0, 0, font, paint);
  47. canvas->restore();
  48. }
  49. }
  50. bool onAnimate(double nanos) override {
  51. fCurrTime = 1e-9 * nanos - fResetTime;
  52. if (fCurrTime > kDuration) {
  53. this->initChars();
  54. fResetTime = 1e-9 * nanos;
  55. fCurrTime = 0;
  56. }
  57. return true;
  58. }
  59. private:
  60. void initChars() {
  61. for (int i = 0; i < kNumChars; ++i) {
  62. char c = fRand.nextULessThan(26) + 65;
  63. fChars[i].fChar[0] = c;
  64. fChars[i].fChar[1] = '\0';
  65. fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10);
  66. fChars[i].fStartRotation = fRand.nextF();
  67. fChars[i].fEndRotation = fRand.nextF() * 20 - 10;
  68. }
  69. }
  70. static constexpr double kDuration = 5.0;
  71. double fCurrTime;
  72. double fResetTime;
  73. SkRandom fRand;
  74. struct AnimatedChar {
  75. char fChar[2];
  76. SkPoint fPosition;
  77. SkScalar fStartRotation;
  78. SkScalar fEndRotation;
  79. };
  80. sk_sp<SkTypeface> fTypeface;
  81. static constexpr int kNumChars = 40;
  82. AnimatedChar fChars[kNumChars];
  83. typedef Sample INHERITED;
  84. };
  85. //////////////////////////////////////////////////////////////////////////////
  86. DEF_SAMPLE( return new FlutterAnimateView(); )