blob: edbb4de4e910b91ca87bfd90020c706fa00fabb8 [file] [log] [blame]
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001/*
2 * Copyright 2017 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 "Benchmark.h"
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04009
10#include "GrContextOptions.h"
11#include "SkCanvas.h"
12#include "SkImage.h"
13#include "SkRandom.h"
14#include "SkSurface.h"
15
16class MultitextureImages : public Benchmark {
17public:
Brian Salomonb5ef1f92018-01-11 11:46:21 -050018 MultitextureImages(int imageSize, int dstRectSize, bool disableMultitexturing, bool aa)
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040019 : fImageSize(imageSize)
20 , fDstRectSize(dstRectSize)
Brian Salomonb5ef1f92018-01-11 11:46:21 -050021 , fDisableMultitexturing(disableMultitexturing)
22 , fAA(aa) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040023 fName.appendf("multitexture_images_%dx%d_image_%dx%d_rect", imageSize, imageSize,
24 dstRectSize, dstRectSize);
Brian Salomonb5ef1f92018-01-11 11:46:21 -050025 if (aa) {
26 fName.append("_aa");
27 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040028 if (disableMultitexturing) {
29 fName.append("_disable_multitexturing");
30 }
31 }
32
33 bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
34
35protected:
36 const char* onGetName() override { return fName.c_str(); }
37
38 void onPerCanvasPreDraw(SkCanvas* canvas) override {
39 auto ii = SkImageInfo::Make(fImageSize, fImageSize, kRGBA_8888_SkColorType,
40 kPremul_SkAlphaType, nullptr);
41 SkRandom random;
42 for (int i = 0; i < kNumImages; ++i) {
43 auto surf = canvas->makeSurface(ii);
44 SkColor color = random.nextU();
45 surf->getCanvas()->clear(color);
46 SkPaint paint;
47 paint.setColor(~color);
48 paint.setBlendMode(SkBlendMode::kSrc);
49 surf->getCanvas()->drawRect(SkRect::MakeLTRB(3, 3, fImageSize - 3, fImageSize - 3),
50 paint);
51 fImages[i] = surf->makeImageSnapshot();
52 }
53 }
54
55 void onPerCanvasPostDraw(SkCanvas*) override {
56 for (int i = 0; i < kNumImages; ++i) {
57 fImages[i].reset();
58 }
59 }
60
61 void onDraw(int loops, SkCanvas* canvas) override {
62 SkRect rect = SkRect::MakeWH(fDstRectSize, fDstRectSize);
63 SkPaint paint;
64 paint.setAlpha(0x40);
65 paint.setFilterQuality(kLow_SkFilterQuality);
Brian Salomonb5ef1f92018-01-11 11:46:21 -050066 paint.setAntiAlias(fAA);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040067 for (int i = 0; i < loops; i++) {
68 for (int j = 0; j < kNumImages; ++j) {
69 SkVector translate = this->translation(i * kNumImages + j);
70 canvas->drawImageRect(fImages[j].get(), rect.makeOffset(translate.fX, translate.fY),
71 &paint);
72 }
73 // Prevent any batching except without multitexturing since we're trying to measure
74 // drawing distinct images and just repeating images here to increase the workload for
75 // timing reasons.
76 canvas->flush();
77 }
78 }
79
80 void modifyGrContextOptions(GrContextOptions* options) override {
81 options->fDisableImageMultitexturing = fDisableMultitexturing;
82 }
83
84private:
85 SkIPoint onGetSize() override {
86 // The rows and columns are spaced by kTranslate, but the images may overlap if they are
87 // larger than kTranslate and extend beyond the last row/column.
88 return SkIPoint::Make(kTranslate * (kNumColumns - 1) + fDstRectSize,
89 kTranslate * (kNumRows - 1) + fDstRectSize);
90 }
91
92 SkVector translation(int i) const {
93 SkVector offset;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050094 // Fractional offsets to ensure we can't ignore antialiasing.
95 offset.fX = i % kNumColumns * kTranslate + 0.1f;
96 offset.fY = (i / kNumColumns) % kNumRows * kTranslate + 0.1f;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040097 return offset;
98 }
99
100 static const int kTranslate = 200;
101 static const int kNumColumns = 5;
102 static const int kNumRows = 5;
103 static const int kNumImages = 8;
104
105 sk_sp<SkImage> fImages[kNumImages];
106 SkString fName;
107 int fImageSize;
108 int fDstRectSize;
109 bool fDisableMultitexturing;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500110 bool fAA;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400111
112 typedef Benchmark INHERITED;
113};
114
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500115// Non-AA
116DEF_BENCH(return new MultitextureImages(128, 32, false, false));
117DEF_BENCH(return new MultitextureImages(128, 32, true, false));
118DEF_BENCH(return new MultitextureImages(128, 128, false, false));
119DEF_BENCH(return new MultitextureImages(128, 128, true, false));
120DEF_BENCH(return new MultitextureImages(128, 256, false, false));
121DEF_BENCH(return new MultitextureImages(128, 256, true, false));
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400122
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500123DEF_BENCH(return new MultitextureImages(512, 32, false, false));
124DEF_BENCH(return new MultitextureImages(512, 32, true, false));
125DEF_BENCH(return new MultitextureImages(512, 128, false, false));
126DEF_BENCH(return new MultitextureImages(512, 128, true, false));
127DEF_BENCH(return new MultitextureImages(512, 256, false, false));
128DEF_BENCH(return new MultitextureImages(512, 256, true, false));
129DEF_BENCH(return new MultitextureImages(512, 512, false, false));
130DEF_BENCH(return new MultitextureImages(512, 512, true, false));
131
132// AA
133DEF_BENCH(return new MultitextureImages(512, 512, true, true));
134DEF_BENCH(return new MultitextureImages(512, 512, false, true));
135DEF_BENCH(return new MultitextureImages(128, 32, true, true));
136DEF_BENCH(return new MultitextureImages(128, 32, false, true));