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