joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/SKPAnimationBench.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkSurface.h" |
| 10 | #include "tools/flags/CommandLineFlags.h" |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 11 | |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 12 | SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip, |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 13 | sk_sp<Animation> animation, bool doLooping) |
Brian Salomon | 626349b | 2020-08-27 10:54:36 -0400 | [diff] [blame] | 14 | : INHERITED(name, pic, clip, 1.0, doLooping) |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 15 | , fAnimation(std::move(animation)) { |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 16 | fUniqueName.printf("%s_%s", name, fAnimation->getTag()); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | const char* SKPAnimationBench::onGetUniqueName() { |
| 20 | return fUniqueName.c_str(); |
| 21 | } |
| 22 | |
| 23 | void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
| 24 | INHERITED::onPerCanvasPreDraw(canvas); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 25 | fDevBounds = canvas->getDeviceClipBounds(); |
| 26 | SkAssertResult(!fDevBounds.isEmpty()); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void SKPAnimationBench::drawPicture() { |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 30 | for (int j = 0; j < this->tileRects().count(); ++j) { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 31 | SkMatrix trans = SkMatrix::Translate(-1.f * this->tileRects()[j].fLeft, |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 32 | -1.f * this->tileRects()[j].fTop); |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 33 | fAnimation->preConcatFrameMatrix(fAnimationTime.nextRangeF(0, 1000), fDevBounds, &trans); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, nullptr); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 35 | } |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 36 | |
| 37 | for (int j = 0; j < this->tileRects().count(); ++j) { |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 38 | this->surfaces()[j]->flush(); |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | class ZoomAnimation : public SKPAnimationBench::Animation { |
| 43 | public: |
| 44 | ZoomAnimation(SkScalar zoomMax, double zoomPeriodMs) |
| 45 | : fZoomMax(zoomMax) |
| 46 | , fZoomPeriodMs(zoomPeriodMs) { |
| 47 | } |
| 48 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 49 | const char* getTag() override { return "zoom"; } |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 50 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 51 | void preConcatFrameMatrix(double animationTimeMs, const SkIRect& devBounds, |
| 52 | SkMatrix* drawMatrix) override { |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 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 | |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 69 | sk_sp<SKPAnimationBench::Animation> SKPAnimationBench::MakeZoomAnimation(SkScalar zoomMax, |
| 70 | double zoomPeriodMs) { |
| 71 | return sk_make_sp<ZoomAnimation>(zoomMax, zoomPeriodMs); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 72 | } |