aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/MultitextureImageBench.cpp
blob: 2c87efb414a86274f67ab76c6816baa984f0eb10 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright 2017 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"
#if SK_SUPPORT_GPU

#include "GrContextOptions.h"
#include "SkCanvas.h"
#include "SkImage.h"
#include "SkRandom.h"
#include "SkSurface.h"

class MultitextureImages : public Benchmark {
public:
    MultitextureImages(int imageSize, int dstRectSize, bool disableMultitexturing, bool aa)
            : fImageSize(imageSize)
            , fDstRectSize(dstRectSize)
            , fDisableMultitexturing(disableMultitexturing)
            , fAA(aa) {
        fName.appendf("multitexture_images_%dx%d_image_%dx%d_rect", imageSize, imageSize,
                      dstRectSize, dstRectSize);
        if (aa) {
            fName.append("_aa");
        }
        if (disableMultitexturing) {
            fName.append("_disable_multitexturing");
        }
    }

    bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }

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

    void onPerCanvasPreDraw(SkCanvas* canvas) override {
        auto ii = SkImageInfo::Make(fImageSize, fImageSize, kRGBA_8888_SkColorType,
                                    kPremul_SkAlphaType, nullptr);
        SkRandom random;
        for (int i = 0; i < kNumImages; ++i) {
            auto surf = canvas->makeSurface(ii);
            SkColor color = random.nextU();
            surf->getCanvas()->clear(color);
            SkPaint paint;
            paint.setColor(~color);
            paint.setBlendMode(SkBlendMode::kSrc);
            surf->getCanvas()->drawRect(SkRect::MakeLTRB(3, 3, fImageSize - 3, fImageSize - 3),
                                        paint);
            fImages[i] = surf->makeImageSnapshot();
        }
    }

    void onPerCanvasPostDraw(SkCanvas*) override {
        for (int i = 0; i < kNumImages; ++i) {
            fImages[i].reset();
        }
    }

    void onDraw(int loops, SkCanvas* canvas) override {
        SkRect rect = SkRect::MakeWH(fDstRectSize, fDstRectSize);
        SkPaint paint;
        paint.setAlpha(0x40);
        paint.setFilterQuality(kLow_SkFilterQuality);
        paint.setAntiAlias(fAA);
        for (int i = 0; i < loops; i++) {
            for (int j = 0; j < kNumImages; ++j) {
                SkVector translate = this->translation(i * kNumImages + j);
                canvas->drawImageRect(fImages[j].get(), rect.makeOffset(translate.fX, translate.fY),
                                      &paint);
            }
            // Prevent any batching except without multitexturing since we're trying to measure
            // drawing distinct images and just repeating images here to increase the workload for
            // timing reasons.
            canvas->flush();
        }
    }

    void modifyGrContextOptions(GrContextOptions* options) override {
        options->fDisableImageMultitexturing = fDisableMultitexturing;
    }

private:
    SkIPoint onGetSize() override {
        // The rows and columns are spaced by kTranslate, but the images may overlap if they are
        // larger than kTranslate and extend beyond the last row/column.
        return SkIPoint::Make(kTranslate * (kNumColumns - 1) + fDstRectSize,
                              kTranslate * (kNumRows - 1) + fDstRectSize);
    }

    SkVector translation(int i) const {
        SkVector offset;
        // Fractional offsets to ensure we can't ignore antialiasing.
        offset.fX = i % kNumColumns * kTranslate + 0.1f;
        offset.fY = (i / kNumColumns) % kNumRows * kTranslate + 0.1f;
        return offset;
    }

    static const int kTranslate = 200;
    static const int kNumColumns = 5;
    static const int kNumRows = 5;
    static const int kNumImages = 8;

    sk_sp<SkImage> fImages[kNumImages];
    SkString fName;
    int fImageSize;
    int fDstRectSize;
    bool fDisableMultitexturing;
    bool fAA;

    typedef Benchmark INHERITED;
};

// Non-AA
DEF_BENCH(return new MultitextureImages(128, 32, false, false));
DEF_BENCH(return new MultitextureImages(128, 32, true, false));
DEF_BENCH(return new MultitextureImages(128, 128, false, false));
DEF_BENCH(return new MultitextureImages(128, 128, true, false));
DEF_BENCH(return new MultitextureImages(128, 256, false, false));
DEF_BENCH(return new MultitextureImages(128, 256, true, false));

DEF_BENCH(return new MultitextureImages(512, 32, false, false));
DEF_BENCH(return new MultitextureImages(512, 32, true, false));
DEF_BENCH(return new MultitextureImages(512, 128, false, false));
DEF_BENCH(return new MultitextureImages(512, 128, true, false));
DEF_BENCH(return new MultitextureImages(512, 256, false, false));
DEF_BENCH(return new MultitextureImages(512, 256, true, false));
DEF_BENCH(return new MultitextureImages(512, 512, false, false));
DEF_BENCH(return new MultitextureImages(512, 512, true, false));

// AA
DEF_BENCH(return new MultitextureImages(512, 512, true, true));
DEF_BENCH(return new MultitextureImages(512, 512, false, true));
DEF_BENCH(return new MultitextureImages(128, 32, true, true));
DEF_BENCH(return new MultitextureImages(128, 32, false, true));

#endif