blob: 24dc1fe1b5d2fb889854461d0f4d3ab2bc938deb [file] [log] [blame]
robertphillipsc2fce562014-06-05 07:18:03 -07001/*
2* Copyright 2014 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkSize.h"
10#include "include/private/SkTDArray.h"
11#include "include/utils/SkRandom.h"
robertphillipsc2fce562014-06-05 07:18:03 -070012
Herb Derby73c75872020-01-22 18:09:16 -050013#include "src/core/SkMathPriv.h"
14#include "src/gpu/GrRectanizerSkyline.h"
robertphillipsc2fce562014-06-05 07:18:03 -070015
16/**
17 * This bench exercises Ganesh' GrRectanizer classes. It exercises the following
18 * rectanizers:
19 * Pow2 Rectanizer
20 * Skyline Rectanizer
21 * in the following cases:
22 * random rects (e.g., pull-save-layers forward use case)
23 * random power of two rects
24 * small constant sized power of 2 rects (e.g., glyph cache use case)
25 */
tfarinaf168b862014-06-19 12:32:29 -070026class RectanizerBench : public Benchmark {
robertphillipsc2fce562014-06-05 07:18:03 -070027public:
28 static const int kWidth = 1024;
29 static const int kHeight = 1024;
30
31 enum RectanizerType {
robertphillipsc2fce562014-06-05 07:18:03 -070032 kSkyline_RectanizerType,
33 };
34
35 enum RectType {
36 kRand_RectType,
37 kRandPow2_RectType,
38 kSmallPow2_RectType
39 };
40
halcanary9d524f22016-03-29 09:03:52 -070041 RectanizerBench(RectanizerType rectanizerType, RectType rectType)
robertphillipsc2fce562014-06-05 07:18:03 -070042 : fName("rectanizer_")
robertphillipsc2fce562014-06-05 07:18:03 -070043 , fRectType(rectType) {
44
Herb Derby73c75872020-01-22 18:09:16 -050045 fName.append("skyline_");
robertphillipsc2fce562014-06-05 07:18:03 -070046
47 if (kRand_RectType == fRectType) {
48 fName.append("rand");
49 } else if (kRandPow2_RectType == fRectType) {
50 fName.append("rand2");
51 } else {
52 SkASSERT(kSmallPow2_RectType == fRectType);
53 fName.append("sm2");
54 }
55 }
56
57protected:
mtklein36352bf2015-03-25 18:17:31 -070058 bool isSuitableFor(Backend backend) override {
robertphillipsc2fce562014-06-05 07:18:03 -070059 return kNonRendering_Backend == backend;
60 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 const char* onGetName() override {
robertphillipsc2fce562014-06-05 07:18:03 -070063 return fName.c_str();
64 }
65
joshualitt8a6697a2015-09-30 12:11:07 -070066 void onDelayedSetup() override {
halcanary96fcdcc2015-08-27 07:41:13 -070067 SkASSERT(nullptr == fRectanizer.get());
robertphillipsc2fce562014-06-05 07:18:03 -070068
Herb Derby73c75872020-01-22 18:09:16 -050069 fRectanizer.reset(new GrRectanizerSkyline(kWidth, kHeight));
robertphillipsc2fce562014-06-05 07:18:03 -070070 }
71
mtkleina1ebeb22015-10-01 09:43:39 -070072 void onDraw(int loops, SkCanvas* canvas) override {
robertphillipsc2fce562014-06-05 07:18:03 -070073 SkRandom rand;
74 SkIPoint16 loc;
75 SkISize size;
76
77 for (int i = 0; i < loops; ++i) {
78 if (kRand_RectType == fRectType) {
79 size = SkISize::Make(rand.nextRangeU(1, kWidth / 2),
80 rand.nextRangeU(1, kHeight / 2));
81 } else if (kRandPow2_RectType == fRectType) {
82 size = SkISize::Make(GrNextPow2(rand.nextRangeU(1, kWidth / 2)),
83 GrNextPow2(rand.nextRangeU(1, kHeight / 2)));
84 } else {
85 SkASSERT(kSmallPow2_RectType == fRectType);
86 size = SkISize::Make(128, 128);
87 }
88
89 if (!fRectanizer->addRect(size.fWidth, size.fHeight, &loc)) {
90 // insert failed so clear out the rectanizer and give the
91 // current rect another try
92 fRectanizer->reset();
93 i--;
94 }
95 }
96
97 fRectanizer->reset();
98 }
99
100private:
101 SkString fName;
robertphillipsc2fce562014-06-05 07:18:03 -0700102 RectType fRectType;
Herb Derby73c75872020-01-22 18:09:16 -0500103 std::unique_ptr<GrRectanizerSkyline> fRectanizer;
robertphillipsc2fce562014-06-05 07:18:03 -0700104
tfarinaf168b862014-06-19 12:32:29 -0700105 typedef Benchmark INHERITED;
robertphillipsc2fce562014-06-05 07:18:03 -0700106};
107
108//////////////////////////////////////////////////////////////////////////////
109
robertphillipsc2fce562014-06-05 07:18:03 -0700110DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
111 RectanizerBench::kRand_RectType);)
112DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
113 RectanizerBench::kRandPow2_RectType);)
114DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
115 RectanizerBench::kSmallPow2_RectType);)