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