aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/SKPAnimationBench.cpp
blob: 55a85966a4c2e0226ca8ea5a63865919671b889d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SKPAnimationBench.h"
#include "SkCommandLineFlags.h"
#include "SkMultiPictureDraw.h"
#include "SkSurface.h"

SKPAnimationBench::SKPAnimationBench(const char* name, const SkPicture* pic, const SkIRect& clip,
                                     SkMatrix animationMatrix, int steps, bool doLooping)
    : INHERITED(name, pic, clip, 1.0, false, doLooping)
    , fSteps(steps)
    , fAnimationMatrix(animationMatrix)
    , fName(name) {
    fUniqueName.printf("%s_animation", name);
}

const char* SKPAnimationBench::onGetName() {
    return fName.c_str();
}

const char* SKPAnimationBench::onGetUniqueName() {
    return fUniqueName.c_str();
}

void SKPAnimationBench::onPerCanvasPreDraw(SkCanvas* canvas) {
    INHERITED::onPerCanvasPreDraw(canvas);
    SkIRect bounds;
    SkAssertResult(canvas->getClipDeviceBounds(&bounds));

    fCenter.set((bounds.fRight - bounds.fLeft) / 2.0f,
                (bounds.fBottom - bounds.fTop) / 2.0f);
}

void SKPAnimationBench::drawPicture() {
    SkMatrix frameMatrix = SkMatrix::MakeTrans(-fCenter.fX, -fCenter.fY);
    frameMatrix.postConcat(fAnimationMatrix);
    SkMatrix reverseTranslate = SkMatrix::MakeTrans(fCenter.fX, fCenter.fY);
    frameMatrix.postConcat(reverseTranslate);

    SkMatrix currentMatrix = frameMatrix;
    for (int i = 0; i < fSteps; i++) {
        for (int j = 0; j < this->tileRects().count(); ++j) {
            SkMatrix trans = SkMatrix::MakeTrans(-1.f * this->tileRects()[j].fLeft,
                                                 -1.f * this->tileRects()[j].fTop);
            SkMatrix tileMatrix = currentMatrix;
            tileMatrix.postConcat(trans);
            this->surfaces()[j]->getCanvas()->drawPicture(this->picture(), &tileMatrix, NULL);
        }

        for (int j = 0; j < this->tileRects().count(); ++j) {
           this->surfaces()[j]->getCanvas()->flush();
        }
        currentMatrix.postConcat(frameMatrix);
    }
}