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