SKPAnimationBench.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2015 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 "bench/SKPAnimationBench.h"
  8. #include "include/core/SkMultiPictureDraw.h"
  9. #include "include/core/SkSurface.h"
  10. #include "tools/flags/CommandLineFlags.h"
  11. SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip,
  12. sk_sp<Animation> animation, bool doLooping)
  13. : INHERITED(name, pic, clip, 1.0, false, doLooping)
  14. , fAnimation(std::move(animation)) {
  15. fUniqueName.printf("%s_%s", name, fAnimation->getTag());
  16. }
  17. const char* SKPAnimationBench::onGetUniqueName() {
  18. return fUniqueName.c_str();
  19. }
  20. void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) {
  21. INHERITED::onPerCanvasPreDraw(canvas);
  22. fDevBounds = canvas->getDeviceClipBounds();
  23. SkAssertResult(!fDevBounds.isEmpty());
  24. }
  25. void SKPAnimationBench::drawPicture() {
  26. for (int j = 0; j < this->tileRects().count(); ++j) {
  27. SkMatrix trans = SkMatrix::MakeTrans(-1.f * this->tileRects()[j].fLeft,
  28. -1.f * this->tileRects()[j].fTop);
  29. fAnimation->preConcatFrameMatrix(fAnimationTime.nextRangeF(0, 1000), fDevBounds, &trans);
  30. this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, nullptr);
  31. }
  32. for (int j = 0; j < this->tileRects().count(); ++j) {
  33. this->surfaces()[j]->getCanvas()->flush();
  34. }
  35. }
  36. class ZoomAnimation : public SKPAnimationBench::Animation {
  37. public:
  38. ZoomAnimation(SkScalar zoomMax, double zoomPeriodMs)
  39. : fZoomMax(zoomMax)
  40. , fZoomPeriodMs(zoomPeriodMs) {
  41. }
  42. virtual const char* getTag() { return "zoom"; }
  43. virtual void preConcatFrameMatrix(double animationTimeMs, const SkIRect& devBounds,
  44. SkMatrix* drawMatrix) {
  45. double t = fmod(animationTimeMs / fZoomPeriodMs, 1.0); // t is in [0, 1).
  46. t = fabs(2 * t - 1); // Make t ping-pong between 0 and 1
  47. SkScalar zoom = static_cast<SkScalar>(pow(fZoomMax, t));
  48. SkPoint center = SkPoint::Make((devBounds.fLeft + devBounds.fRight) / 2.0f,
  49. (devBounds.fTop + devBounds.fBottom) / 2.0f);
  50. drawMatrix->preTranslate(center.fX, center.fY);
  51. drawMatrix->preScale(zoom, zoom);
  52. drawMatrix->preTranslate(-center.fX, -center.fY);
  53. }
  54. private:
  55. double fZoomMax;
  56. double fZoomPeriodMs;
  57. };
  58. sk_sp<SKPAnimationBench::Animation> SKPAnimationBench::MakeZoomAnimation(SkScalar zoomMax,
  59. double zoomPeriodMs) {
  60. return sk_make_sp<ZoomAnimation>(zoomMax, zoomPeriodMs);
  61. }