aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/RotatedRectBench.cpp
blob: 522d358c3b4653f443c4988a0d095e4c55fededc (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Benchmark.h"
#include "SkCanvas.h"
#include "SkPaint.h"

/** This benchmark tests rendering rotated rectangles. It can optionally apply AA and/or change the
    paint color between each rect. */
class RotRectBench: public Benchmark {
public:
    RotRectBench(bool aa, bool changeColor)
        : fAA(aa)
        , fChangeColor(changeColor) {
        this->makeName();
    }

protected:
    virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); }

    virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
        SkPaint paint;
        paint.setAntiAlias(fAA);
        SkColor color = 0xFF000000;

        int w = canvas->getBaseLayerSize().width();
        int h = canvas->getBaseLayerSize().height();

        static const SkScalar kRectW = 25.1f;
        static const SkScalar kRectH = 25.9f;

        SkMatrix rotate;
        // This value was chosen so that we frequently hit the axis-aligned case.
        rotate.setRotate(30.f, kRectW / 2, kRectH / 2);
        SkMatrix m = rotate;

        SkScalar tx = 0, ty = 0;

        for (int i = 0; i < loops; ++i) {
            canvas->save();
            canvas->translate(tx, ty);
            canvas->concat(m);
            paint.setColor(color);
            if (fChangeColor) {
                color += 0x010203;
                color |= 0xFF000000;
            }
            canvas->drawRect(SkRect::MakeWH(kRectW, kRectH), paint);
            canvas->restore();

            tx += kRectW + 2;
            if (tx > w) {
                tx = 0;
                ty += kRectH + 2;
                if (ty > h) {
                    ty = 0;
                }
            }

            m.postConcat(rotate);
        }
    }

private:
    void makeName() {
        fName = "rotated_rects";
        if (fAA) {
            fName.append("_aa");
        } else {
            fName.append("_bw");
        }
        if (fChangeColor) {
            fName.append("_change_color");
        } else {
            fName.append("_same_color");
        }
    }

    bool     fAA;
    bool     fChangeColor;
    SkString fName;

    typedef Benchmark INHERITED;
};

DEF_BENCH(return new RotRectBench(true, true);)
DEF_BENCH(return new RotRectBench(true, false);)
DEF_BENCH(return new RotRectBench(false, true);)
DEF_BENCH(return new RotRectBench(false, false);)