| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SKPAnimationBench.h" | 8 #include "SKPAnimationBench.h" |
| 9 #include "SkCommandLineFlags.h" | 9 #include "SkCommandLineFlags.h" |
| 10 #include "SkMultiPictureDraw.h" | 10 #include "SkMultiPictureDraw.h" |
| 11 #include "SkSurface.h" | 11 #include "SkSurface.h" |
| 12 | 12 |
| 13 SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, con
st SkIRect& clip, | 13 SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, con
st SkIRect& clip, |
| 14 SkMatrix animationMatrix, int steps, bool d
oLooping) | 14 Animation* animation, bool doLooping) |
| 15 : INHERITED(name, pic, clip, 1.0, false, doLooping) | 15 : INHERITED(name, pic, clip, 1.0, false, doLooping) |
| 16 , fSteps(steps) | 16 , fAnimation(SkRef(animation)) { |
| 17 , fAnimationMatrix(animationMatrix) | 17 fUniqueName.printf("%s_%s", name, fAnimation->getTag()); |
| 18 , fName(name) { | |
| 19 fUniqueName.printf("%s_animation", name); | |
| 20 } | |
| 21 | |
| 22 const char* SKPAnimationBench::onGetName() { | |
| 23 return fName.c_str(); | |
| 24 } | 18 } |
| 25 | 19 |
| 26 const char* SKPAnimationBench::onGetUniqueName() { | 20 const char* SKPAnimationBench::onGetUniqueName() { |
| 27 return fUniqueName.c_str(); | 21 return fUniqueName.c_str(); |
| 28 } | 22 } |
| 29 | 23 |
| 30 void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) { | 24 void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
| 31 INHERITED::onPerCanvasPreDraw(canvas); | 25 INHERITED::onPerCanvasPreDraw(canvas); |
| 32 SkIRect bounds; | 26 SkAssertResult(canvas->getClipDeviceBounds(&fDevBounds)); |
| 33 SkAssertResult(canvas->getClipDeviceBounds(&bounds)); | 27 fAnimationTimer.start(); |
| 34 | |
| 35 fCenter.set((bounds.fRight - bounds.fLeft) / 2.0f, | |
| 36 (bounds.fBottom - bounds.fTop) / 2.0f); | |
| 37 } | 28 } |
| 38 | 29 |
| 39 void SKPAnimationBench::drawPicture() { | 30 void SKPAnimationBench::drawPicture() { |
| 40 SkMatrix frameMatrix = SkMatrix::MakeTrans(-fCenter.fX, -fCenter.fY); | 31 fAnimationTimer.end(); |
| 41 frameMatrix.postConcat(fAnimationMatrix); | |
| 42 SkMatrix reverseTranslate = SkMatrix::MakeTrans(fCenter.fX, fCenter.fY); | |
| 43 frameMatrix.postConcat(reverseTranslate); | |
| 44 | 32 |
| 45 SkMatrix currentMatrix = frameMatrix; | 33 for (int j = 0; j < this->tileRects().count(); ++j) { |
| 46 for (int i = 0; i < fSteps; i++) { | 34 SkMatrix trans = SkMatrix::MakeTrans(-1.f * this->tileRects()[j].fLeft, |
| 47 for (int j = 0; j < this->tileRects().count(); ++j) { | 35 -1.f * this->tileRects()[j].fTop); |
| 48 SkMatrix trans = SkMatrix::MakeTrans(-1.f * this->tileRects()[j].fLe
ft, | 36 fAnimation->preConcatFrameMatrix(fAnimationTimer.fWall, fDevBounds, &tra
ns); |
| 49 -1.f * this->tileRects()[j].fTo
p); | 37 this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, N
ULL); |
| 50 SkMatrix tileMatrix = currentMatrix; | 38 } |
| 51 tileMatrix.postConcat(trans); | |
| 52 this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &tile
Matrix, NULL); | |
| 53 } | |
| 54 | 39 |
| 55 for (int j = 0; j < this->tileRects().count(); ++j) { | 40 for (int j = 0; j < this->tileRects().count(); ++j) { |
| 56 this->surfaces()[j]->getCanvas()->flush(); | 41 this->surfaces()[j]->getCanvas()->flush(); |
| 57 } | |
| 58 currentMatrix.postConcat(frameMatrix); | |
| 59 } | 42 } |
| 60 } | 43 } |
| 44 |
| 45 class ZoomAnimation : public SKPAnimationBench::Animation { |
| 46 public: |
| 47 ZoomAnimation(SkScalar zoomMax, double zoomPeriodMs) |
| 48 : fZoomMax(zoomMax) |
| 49 , fZoomPeriodMs(zoomPeriodMs) { |
| 50 } |
| 51 |
| 52 virtual const char* getTag() { return "zoom"; } |
| 53 |
| 54 virtual void preConcatFrameMatrix(double animationTimeMs, const SkIRect& dev
Bounds, |
| 55 SkMatrix* drawMatrix) { |
| 56 double t = fmod(animationTimeMs / fZoomPeriodMs, 1.0); // t is in [0, 1)
. |
| 57 t = fabs(2 * t - 1); // Make t ping-pong between 0 and 1 |
| 58 SkScalar zoom = static_cast<SkScalar>(pow(fZoomMax, t)); |
| 59 |
| 60 SkPoint center = SkPoint::Make((devBounds.fLeft + devBounds.fRight) / 2.
0f, |
| 61 (devBounds.fTop + devBounds.fBottom) / 2.
0f); |
| 62 drawMatrix->preTranslate(center.fX, center.fY); |
| 63 drawMatrix->preScale(zoom, zoom); |
| 64 drawMatrix->preTranslate(-center.fX, -center.fY); |
| 65 } |
| 66 |
| 67 private: |
| 68 double fZoomMax; |
| 69 double fZoomPeriodMs; |
| 70 }; |
| 71 |
| 72 SKPAnimationBench::Animation* SKPAnimationBench::CreateZoomAnimation(SkScalar zo
omMax, |
| 73 double zoom
PeriodMs) { |
| 74 return SkNEW_ARGS(ZoomAnimation, (zoomMax, zoomPeriodMs)); |
| 75 } |
| OLD | NEW |