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