blob: 2bd3efb2868f3b4348a3794c73cc9f80d628a853 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/SKPAnimationBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkSurface.h"
10#include "tools/flags/CommandLineFlags.h"
joshualitt261c3ad2015-04-27 09:16:57 -070011
cdaltonb4022962015-06-25 10:51:56 -070012SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip,
Ben Wagneracf98df2019-07-12 12:51:44 -040013 sk_sp<Animation> animation, bool doLooping)
cdaltonb4022962015-06-25 10:51:56 -070014 : INHERITED(name, pic, clip, 1.0, false, doLooping)
Ben Wagneracf98df2019-07-12 12:51:44 -040015 , fAnimation(std::move(animation)) {
cdalton63a82852015-06-29 14:06:10 -070016 fUniqueName.printf("%s_%s", name, fAnimation->getTag());
joshualitt261c3ad2015-04-27 09:16:57 -070017}
18
19const char* SKPAnimationBench::onGetUniqueName() {
20 return fUniqueName.c_str();
21}
22
23void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) {
24 INHERITED::onPerCanvasPreDraw(canvas);
Mike Reed918e1442017-01-23 11:39:45 -050025 fDevBounds = canvas->getDeviceClipBounds();
26 SkAssertResult(!fDevBounds.isEmpty());
joshualitt261c3ad2015-04-27 09:16:57 -070027}
28
29void SKPAnimationBench::drawPicture() {
cdalton63a82852015-06-29 14:06:10 -070030 for (int j = 0; j < this->tileRects().count(); ++j) {
Mike Reed1f607332020-05-21 12:11:27 -040031 SkMatrix trans = SkMatrix::Translate(-1.f * this->tileRects()[j].fLeft,
cdalton63a82852015-06-29 14:06:10 -070032 -1.f * this->tileRects()[j].fTop);
Ben Wagneracf98df2019-07-12 12:51:44 -040033 fAnimation->preConcatFrameMatrix(fAnimationTime.nextRangeF(0, 1000), fDevBounds, &trans);
halcanary96fcdcc2015-08-27 07:41:13 -070034 this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -070035 }
cdalton63a82852015-06-29 14:06:10 -070036
37 for (int j = 0; j < this->tileRects().count(); ++j) {
38 this->surfaces()[j]->getCanvas()->flush();
39 }
40}
41
42class ZoomAnimation : public SKPAnimationBench::Animation {
43public:
44 ZoomAnimation(SkScalar zoomMax, double zoomPeriodMs)
45 : fZoomMax(zoomMax)
46 , fZoomPeriodMs(zoomPeriodMs) {
47 }
48
49 virtual const char* getTag() { return "zoom"; }
50
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
64private:
65 double fZoomMax;
66 double fZoomPeriodMs;
67};
68
Ben Wagneracf98df2019-07-12 12:51:44 -040069sk_sp<SKPAnimationBench::Animation> SKPAnimationBench::MakeZoomAnimation(SkScalar zoomMax,
70 double zoomPeriodMs) {
71 return sk_make_sp<ZoomAnimation>(zoomMax, zoomPeriodMs);
joshualitt261c3ad2015-04-27 09:16:57 -070072}