2015-04-27 16:16:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/SKPAnimationBench.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
2015-04-27 16:16:57 +00:00
|
|
|
|
2015-06-25 17:51:56 +00:00
|
|
|
SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip,
|
2019-07-12 16:51:44 +00:00
|
|
|
sk_sp<Animation> animation, bool doLooping)
|
2020-08-27 14:54:36 +00:00
|
|
|
: INHERITED(name, pic, clip, 1.0, doLooping)
|
2019-07-12 16:51:44 +00:00
|
|
|
, fAnimation(std::move(animation)) {
|
2015-06-29 21:06:10 +00:00
|
|
|
fUniqueName.printf("%s_%s", name, fAnimation->getTag());
|
2015-04-27 16:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* SKPAnimationBench::onGetUniqueName() {
|
|
|
|
return fUniqueName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) {
|
|
|
|
INHERITED::onPerCanvasPreDraw(canvas);
|
2017-01-23 16:39:45 +00:00
|
|
|
fDevBounds = canvas->getDeviceClipBounds();
|
|
|
|
SkAssertResult(!fDevBounds.isEmpty());
|
2015-04-27 16:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SKPAnimationBench::drawPicture() {
|
2015-06-29 21:06:10 +00:00
|
|
|
for (int j = 0; j < this->tileRects().count(); ++j) {
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix trans = SkMatrix::Translate(-1.f * this->tileRects()[j].fLeft,
|
2015-06-29 21:06:10 +00:00
|
|
|
-1.f * this->tileRects()[j].fTop);
|
2019-07-12 16:51:44 +00:00
|
|
|
fAnimation->preConcatFrameMatrix(fAnimationTime.nextRangeF(0, 1000), fDevBounds, &trans);
|
2015-08-27 14:41:13 +00:00
|
|
|
this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &trans, nullptr);
|
2015-04-27 16:16:57 +00:00
|
|
|
}
|
2015-06-29 21:06:10 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < this->tileRects().count(); ++j) {
|
|
|
|
this->surfaces()[j]->getCanvas()->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ZoomAnimation : public SKPAnimationBench::Animation {
|
|
|
|
public:
|
|
|
|
ZoomAnimation(SkScalar zoomMax, double zoomPeriodMs)
|
|
|
|
: fZoomMax(zoomMax)
|
|
|
|
, fZoomPeriodMs(zoomPeriodMs) {
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:03:56 +00:00
|
|
|
const char* getTag() override { return "zoom"; }
|
2015-06-29 21:06:10 +00:00
|
|
|
|
2020-07-21 21:03:56 +00:00
|
|
|
void preConcatFrameMatrix(double animationTimeMs, const SkIRect& devBounds,
|
|
|
|
SkMatrix* drawMatrix) override {
|
2015-06-29 21:06:10 +00:00
|
|
|
double t = fmod(animationTimeMs / fZoomPeriodMs, 1.0); // t is in [0, 1).
|
|
|
|
t = fabs(2 * t - 1); // Make t ping-pong between 0 and 1
|
|
|
|
SkScalar zoom = static_cast<SkScalar>(pow(fZoomMax, t));
|
|
|
|
|
|
|
|
SkPoint center = SkPoint::Make((devBounds.fLeft + devBounds.fRight) / 2.0f,
|
|
|
|
(devBounds.fTop + devBounds.fBottom) / 2.0f);
|
|
|
|
drawMatrix->preTranslate(center.fX, center.fY);
|
|
|
|
drawMatrix->preScale(zoom, zoom);
|
|
|
|
drawMatrix->preTranslate(-center.fX, -center.fY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double fZoomMax;
|
|
|
|
double fZoomPeriodMs;
|
|
|
|
};
|
|
|
|
|
2019-07-12 16:51:44 +00:00
|
|
|
sk_sp<SKPAnimationBench::Animation> SKPAnimationBench::MakeZoomAnimation(SkScalar zoomMax,
|
|
|
|
double zoomPeriodMs) {
|
|
|
|
return sk_make_sp<ZoomAnimation>(zoomMax, zoomPeriodMs);
|
2015-04-27 16:16:57 +00:00
|
|
|
}
|