blob: 275e036d0d0d69fd7a014c1711c6ca5b8c708a7c [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
John Stilesfbd050b2020-08-03 13:21:46 -04008#include <memory>
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "bench/Benchmark.h"
11#include "include/core/SkSize.h"
12#include "include/private/SkTDArray.h"
13#include "include/utils/SkRandom.h"
robertphillipsc2fce562014-06-05 07:18:03 -070014
Chris Daltone5a141d2020-05-19 11:57:53 -060015#include "src/gpu/GrRectanizerPow2.h"
Herb Derby73c75872020-01-22 18:09:16 -050016#include "src/gpu/GrRectanizerSkyline.h"
robertphillipsc2fce562014-06-05 07:18:03 -070017
18/**
19 * This bench exercises Ganesh' GrRectanizer classes. It exercises the following
20 * rectanizers:
21 * Pow2 Rectanizer
22 * Skyline Rectanizer
23 * in the following cases:
24 * random rects (e.g., pull-save-layers forward use case)
25 * random power of two rects
26 * small constant sized power of 2 rects (e.g., glyph cache use case)
27 */
tfarinaf168b862014-06-19 12:32:29 -070028class RectanizerBench : public Benchmark {
robertphillipsc2fce562014-06-05 07:18:03 -070029public:
John Stilesfbd050b2020-08-03 13:21:46 -040030 static constexpr int kWidth = 1024;
31 static constexpr int kHeight = 1024;
robertphillipsc2fce562014-06-05 07:18:03 -070032
33 enum RectanizerType {
Chris Daltone5a141d2020-05-19 11:57:53 -060034 kPow2_RectanizerType,
robertphillipsc2fce562014-06-05 07:18:03 -070035 kSkyline_RectanizerType,
36 };
37
38 enum RectType {
39 kRand_RectType,
40 kRandPow2_RectType,
41 kSmallPow2_RectType
42 };
43
halcanary9d524f22016-03-29 09:03:52 -070044 RectanizerBench(RectanizerType rectanizerType, RectType rectType)
robertphillipsc2fce562014-06-05 07:18:03 -070045 : fName("rectanizer_")
Chris Daltone5a141d2020-05-19 11:57:53 -060046 , fRectanizerType(rectanizerType)
robertphillipsc2fce562014-06-05 07:18:03 -070047 , fRectType(rectType) {
48
Chris Daltone5a141d2020-05-19 11:57:53 -060049 if (kPow2_RectanizerType == fRectanizerType) {
50 fName.append("pow2_");
51 } else {
52 SkASSERT(kSkyline_RectanizerType == fRectanizerType);
53 fName.append("skyline_");
54 }
robertphillipsc2fce562014-06-05 07:18:03 -070055
56 if (kRand_RectType == fRectType) {
57 fName.append("rand");
58 } else if (kRandPow2_RectType == fRectType) {
59 fName.append("rand2");
60 } else {
61 SkASSERT(kSmallPow2_RectType == fRectType);
62 fName.append("sm2");
63 }
64 }
65
66protected:
mtklein36352bf2015-03-25 18:17:31 -070067 bool isSuitableFor(Backend backend) override {
robertphillipsc2fce562014-06-05 07:18:03 -070068 return kNonRendering_Backend == backend;
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 const char* onGetName() override {
robertphillipsc2fce562014-06-05 07:18:03 -070072 return fName.c_str();
73 }
74
joshualitt8a6697a2015-09-30 12:11:07 -070075 void onDelayedSetup() override {
halcanary96fcdcc2015-08-27 07:41:13 -070076 SkASSERT(nullptr == fRectanizer.get());
robertphillipsc2fce562014-06-05 07:18:03 -070077
Chris Daltone5a141d2020-05-19 11:57:53 -060078 if (kPow2_RectanizerType == fRectanizerType) {
John Stilesfbd050b2020-08-03 13:21:46 -040079 fRectanizer = std::make_unique<GrRectanizerPow2>(kWidth, kHeight);
Chris Daltone5a141d2020-05-19 11:57:53 -060080 } else {
81 SkASSERT(kSkyline_RectanizerType == fRectanizerType);
John Stilesfbd050b2020-08-03 13:21:46 -040082 fRectanizer = std::make_unique<GrRectanizerSkyline>(kWidth, kHeight);
Chris Daltone5a141d2020-05-19 11:57:53 -060083 }
robertphillipsc2fce562014-06-05 07:18:03 -070084 }
85
mtkleina1ebeb22015-10-01 09:43:39 -070086 void onDraw(int loops, SkCanvas* canvas) override {
robertphillipsc2fce562014-06-05 07:18:03 -070087 SkRandom rand;
88 SkIPoint16 loc;
89 SkISize size;
90
91 for (int i = 0; i < loops; ++i) {
92 if (kRand_RectType == fRectType) {
93 size = SkISize::Make(rand.nextRangeU(1, kWidth / 2),
94 rand.nextRangeU(1, kHeight / 2));
95 } else if (kRandPow2_RectType == fRectType) {
96 size = SkISize::Make(GrNextPow2(rand.nextRangeU(1, kWidth / 2)),
97 GrNextPow2(rand.nextRangeU(1, kHeight / 2)));
98 } else {
99 SkASSERT(kSmallPow2_RectType == fRectType);
100 size = SkISize::Make(128, 128);
101 }
102
103 if (!fRectanizer->addRect(size.fWidth, size.fHeight, &loc)) {
104 // insert failed so clear out the rectanizer and give the
105 // current rect another try
106 fRectanizer->reset();
107 i--;
108 }
109 }
110
111 fRectanizer->reset();
112 }
113
114private:
115 SkString fName;
Chris Daltone5a141d2020-05-19 11:57:53 -0600116 RectanizerType fRectanizerType;
robertphillipsc2fce562014-06-05 07:18:03 -0700117 RectType fRectType;
Chris Daltone5a141d2020-05-19 11:57:53 -0600118 std::unique_ptr<GrRectanizer> fRectanizer;
robertphillipsc2fce562014-06-05 07:18:03 -0700119
John Stiles7571f9e2020-09-02 22:42:33 -0400120 using INHERITED = Benchmark;
robertphillipsc2fce562014-06-05 07:18:03 -0700121};
122
123//////////////////////////////////////////////////////////////////////////////
124
Chris Daltone5a141d2020-05-19 11:57:53 -0600125DEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
126 RectanizerBench::kRand_RectType);)
127DEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
128 RectanizerBench::kRandPow2_RectType);)
129DEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
130 RectanizerBench::kSmallPow2_RectType);)
robertphillipsc2fce562014-06-05 07:18:03 -0700131DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
132 RectanizerBench::kRand_RectType);)
133DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
134 RectanizerBench::kRandPow2_RectType);)
135DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
136 RectanizerBench::kSmallPow2_RectType);)