joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 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 | |
| 8 | #include "SKPAnimationBench.h" |
| 9 | #include "SkCommandLineFlags.h" |
| 10 | #include "SkMultiPictureDraw.h" |
| 11 | #include "SkSurface.h" |
| 12 | |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 13 | SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip, |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 14 | Animation* animation, bool doLooping) |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 15 | : INHERITED(name, pic, clip, 1.0, false, doLooping) |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 16 | , fAnimation(SkRef(animation)) { |
| 17 | fUniqueName.printf("%s_%s", name, fAnimation->getTag()); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | const char* SKPAnimationBench::onGetUniqueName() { |
| 21 | return fUniqueName.c_str(); |
| 22 | } |
| 23 | |
| 24 | void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
| 25 | INHERITED::onPerCanvasPreDraw(canvas); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 26 | fDevBounds = canvas->getDeviceClipBounds(); |
| 27 | SkAssertResult(!fDevBounds.isEmpty()); |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 28 | fAnimationTimer.start(); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void SKPAnimationBench::drawPicture() { |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 32 | fAnimationTimer.end(); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 33 | |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 34 | for (int j = 0; j < this->tileRects().count(); ++j) { |
| 35 | SkMatrix trans = SkMatrix::MakeTrans(-1.f * this->tileRects()[j].fLeft, |
| 36 | -1.f * this->tileRects()[j].fTop); |
| 37 | fAnimation->preConcatFrameMatrix(fAnimationTimer.fWall, fDevBounds, &trans); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, nullptr); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 39 | } |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 40 | |
| 41 | for (int j = 0; j < this->tileRects().count(); ++j) { |
| 42 | this->surfaces()[j]->getCanvas()->flush(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | class ZoomAnimation : public SKPAnimationBench::Animation { |
| 47 | public: |
| 48 | ZoomAnimation(SkScalar zoomMax, double zoomPeriodMs) |
| 49 | : fZoomMax(zoomMax) |
| 50 | , fZoomPeriodMs(zoomPeriodMs) { |
| 51 | } |
| 52 | |
| 53 | virtual const char* getTag() { return "zoom"; } |
| 54 | |
| 55 | virtual void preConcatFrameMatrix(double animationTimeMs, const SkIRect& devBounds, |
| 56 | SkMatrix* drawMatrix) { |
| 57 | double t = fmod(animationTimeMs / fZoomPeriodMs, 1.0); // t is in [0, 1). |
| 58 | t = fabs(2 * t - 1); // Make t ping-pong between 0 and 1 |
| 59 | SkScalar zoom = static_cast<SkScalar>(pow(fZoomMax, t)); |
| 60 | |
| 61 | SkPoint center = SkPoint::Make((devBounds.fLeft + devBounds.fRight) / 2.0f, |
| 62 | (devBounds.fTop + devBounds.fBottom) / 2.0f); |
| 63 | drawMatrix->preTranslate(center.fX, center.fY); |
| 64 | drawMatrix->preScale(zoom, zoom); |
| 65 | drawMatrix->preTranslate(-center.fX, -center.fY); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | double fZoomMax; |
| 70 | double fZoomPeriodMs; |
| 71 | }; |
| 72 | |
| 73 | SKPAnimationBench::Animation* SKPAnimationBench::CreateZoomAnimation(SkScalar zoomMax, |
| 74 | double zoomPeriodMs) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 75 | return new ZoomAnimation(zoomMax, zoomPeriodMs); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 76 | } |