blob: 02fd48a0f1ea5efbe37abc1c8fe818b578f51a85 [file] [log] [blame]
joshualitt261c3ad2015-04-27 09:16:57 -07001/*
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
cdaltonb4022962015-06-25 10:51:56 -070013SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip,
cdalton63a82852015-06-29 14:06:10 -070014 Animation* animation, bool doLooping)
cdaltonb4022962015-06-25 10:51:56 -070015 : INHERITED(name, pic, clip, 1.0, false, doLooping)
cdalton63a82852015-06-29 14:06:10 -070016 , fAnimation(SkRef(animation)) {
17 fUniqueName.printf("%s_%s", name, fAnimation->getTag());
joshualitt261c3ad2015-04-27 09:16:57 -070018}
19
20const char* SKPAnimationBench::onGetUniqueName() {
21 return fUniqueName.c_str();
22}
23
24void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) {
25 INHERITED::onPerCanvasPreDraw(canvas);
cdalton63a82852015-06-29 14:06:10 -070026 SkAssertResult(canvas->getClipDeviceBounds(&fDevBounds));
27 fAnimationTimer.start();
joshualitt261c3ad2015-04-27 09:16:57 -070028}
29
30void SKPAnimationBench::drawPicture() {
cdalton63a82852015-06-29 14:06:10 -070031 fAnimationTimer.end();
joshualitt261c3ad2015-04-27 09:16:57 -070032
cdalton63a82852015-06-29 14:06:10 -070033 for (int j = 0; j < this->tileRects().count(); ++j) {
34 SkMatrix trans = SkMatrix::MakeTrans(-1.f * this->tileRects()[j].fLeft,
35 -1.f * this->tileRects()[j].fTop);
36 fAnimation->preConcatFrameMatrix(fAnimationTimer.fWall, fDevBounds, &trans);
37 this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, NULL);
joshualitt261c3ad2015-04-27 09:16:57 -070038 }
cdalton63a82852015-06-29 14:06:10 -070039
40 for (int j = 0; j < this->tileRects().count(); ++j) {
41 this->surfaces()[j]->getCanvas()->flush();
42 }
43}
44
45class ZoomAnimation : public SKPAnimationBench::Animation {
46public:
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& devBounds,
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
67private:
68 double fZoomMax;
69 double fZoomPeriodMs;
70};
71
72SKPAnimationBench::Animation* SKPAnimationBench::CreateZoomAnimation(SkScalar zoomMax,
73 double zoomPeriodMs) {
74 return SkNEW_ARGS(ZoomAnimation, (zoomMax, zoomPeriodMs));
joshualitt261c3ad2015-04-27 09:16:57 -070075}