1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci* Copyright 2014 Google Inc.
3cb93a386Sopenharmony_ci*
4cb93a386Sopenharmony_ci* Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci* found in the LICENSE file.
6cb93a386Sopenharmony_ci*/
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include <memory>
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "bench/Benchmark.h"
11cb93a386Sopenharmony_ci#include "include/core/SkSize.h"
12cb93a386Sopenharmony_ci#include "include/private/SkTDArray.h"
13cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci#include "src/gpu/GrRectanizerPow2.h"
16cb93a386Sopenharmony_ci#include "src/gpu/GrRectanizerSkyline.h"
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci/**
19cb93a386Sopenharmony_ci * This bench exercises Ganesh' GrRectanizer classes. It exercises the following
20cb93a386Sopenharmony_ci * rectanizers:
21cb93a386Sopenharmony_ci *      Pow2 Rectanizer
22cb93a386Sopenharmony_ci *      Skyline Rectanizer
23cb93a386Sopenharmony_ci * in the following cases:
24cb93a386Sopenharmony_ci *      random rects (e.g., pull-save-layers forward use case)
25cb93a386Sopenharmony_ci *      random power of two rects
26cb93a386Sopenharmony_ci *      small constant sized power of 2 rects (e.g., glyph cache use case)
27cb93a386Sopenharmony_ci */
28cb93a386Sopenharmony_ciclass RectanizerBench : public Benchmark {
29cb93a386Sopenharmony_cipublic:
30cb93a386Sopenharmony_ci    inline static constexpr int kWidth = 1024;
31cb93a386Sopenharmony_ci    inline static constexpr int kHeight = 1024;
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    enum RectanizerType {
34cb93a386Sopenharmony_ci        kPow2_RectanizerType,
35cb93a386Sopenharmony_ci        kSkyline_RectanizerType,
36cb93a386Sopenharmony_ci    };
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci    enum RectType {
39cb93a386Sopenharmony_ci        kRand_RectType,
40cb93a386Sopenharmony_ci        kRandPow2_RectType,
41cb93a386Sopenharmony_ci        kSmallPow2_RectType
42cb93a386Sopenharmony_ci    };
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    RectanizerBench(RectanizerType rectanizerType, RectType rectType)
45cb93a386Sopenharmony_ci        : fName("rectanizer_")
46cb93a386Sopenharmony_ci        , fRectanizerType(rectanizerType)
47cb93a386Sopenharmony_ci        , fRectType(rectType) {
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci        if (kPow2_RectanizerType == fRectanizerType) {
50cb93a386Sopenharmony_ci            fName.append("pow2_");
51cb93a386Sopenharmony_ci        } else {
52cb93a386Sopenharmony_ci            SkASSERT(kSkyline_RectanizerType == fRectanizerType);
53cb93a386Sopenharmony_ci            fName.append("skyline_");
54cb93a386Sopenharmony_ci        }
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci        if (kRand_RectType == fRectType) {
57cb93a386Sopenharmony_ci            fName.append("rand");
58cb93a386Sopenharmony_ci        } else if (kRandPow2_RectType == fRectType) {
59cb93a386Sopenharmony_ci            fName.append("rand2");
60cb93a386Sopenharmony_ci        } else {
61cb93a386Sopenharmony_ci            SkASSERT(kSmallPow2_RectType == fRectType);
62cb93a386Sopenharmony_ci            fName.append("sm2");
63cb93a386Sopenharmony_ci        }
64cb93a386Sopenharmony_ci    }
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ciprotected:
67cb93a386Sopenharmony_ci    bool isSuitableFor(Backend backend) override {
68cb93a386Sopenharmony_ci        return kNonRendering_Backend == backend;
69cb93a386Sopenharmony_ci    }
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci    const char* onGetName() override {
72cb93a386Sopenharmony_ci        return fName.c_str();
73cb93a386Sopenharmony_ci    }
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci    void onDelayedSetup() override {
76cb93a386Sopenharmony_ci        SkASSERT(nullptr == fRectanizer.get());
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci        if (kPow2_RectanizerType == fRectanizerType) {
79cb93a386Sopenharmony_ci            fRectanizer = std::make_unique<GrRectanizerPow2>(kWidth, kHeight);
80cb93a386Sopenharmony_ci        } else {
81cb93a386Sopenharmony_ci            SkASSERT(kSkyline_RectanizerType == fRectanizerType);
82cb93a386Sopenharmony_ci            fRectanizer = std::make_unique<GrRectanizerSkyline>(kWidth, kHeight);
83cb93a386Sopenharmony_ci        }
84cb93a386Sopenharmony_ci    }
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas* canvas) override {
87cb93a386Sopenharmony_ci        SkRandom rand;
88cb93a386Sopenharmony_ci        SkIPoint16 loc;
89cb93a386Sopenharmony_ci        SkISize size;
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci        for (int i = 0; i < loops; ++i) {
92cb93a386Sopenharmony_ci            if (kRand_RectType == fRectType) {
93cb93a386Sopenharmony_ci                size = SkISize::Make(rand.nextRangeU(1, kWidth / 2),
94cb93a386Sopenharmony_ci                                     rand.nextRangeU(1, kHeight / 2));
95cb93a386Sopenharmony_ci            } else if (kRandPow2_RectType == fRectType) {
96cb93a386Sopenharmony_ci                size = SkISize::Make(GrNextPow2(rand.nextRangeU(1, kWidth / 2)),
97cb93a386Sopenharmony_ci                                     GrNextPow2(rand.nextRangeU(1, kHeight / 2)));
98cb93a386Sopenharmony_ci            } else {
99cb93a386Sopenharmony_ci                SkASSERT(kSmallPow2_RectType == fRectType);
100cb93a386Sopenharmony_ci                size = SkISize::Make(128, 128);
101cb93a386Sopenharmony_ci            }
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci            if (!fRectanizer->addRect(size.fWidth, size.fHeight, &loc)) {
104cb93a386Sopenharmony_ci                // insert failed so clear out the rectanizer and give the
105cb93a386Sopenharmony_ci                // current rect another try
106cb93a386Sopenharmony_ci                fRectanizer->reset();
107cb93a386Sopenharmony_ci                i--;
108cb93a386Sopenharmony_ci            }
109cb93a386Sopenharmony_ci        }
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci        fRectanizer->reset();
112cb93a386Sopenharmony_ci    }
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ciprivate:
115cb93a386Sopenharmony_ci    SkString                    fName;
116cb93a386Sopenharmony_ci    RectanizerType              fRectanizerType;
117cb93a386Sopenharmony_ci    RectType                    fRectType;
118cb93a386Sopenharmony_ci    std::unique_ptr<GrRectanizer> fRectanizer;
119cb93a386Sopenharmony_ci
120cb93a386Sopenharmony_ci    using INHERITED = Benchmark;
121cb93a386Sopenharmony_ci};
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////
124cb93a386Sopenharmony_ci
125cb93a386Sopenharmony_ciDEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
126cb93a386Sopenharmony_ci                                     RectanizerBench::kRand_RectType);)
127cb93a386Sopenharmony_ciDEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
128cb93a386Sopenharmony_ci                                     RectanizerBench::kRandPow2_RectType);)
129cb93a386Sopenharmony_ciDEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
130cb93a386Sopenharmony_ci                                     RectanizerBench::kSmallPow2_RectType);)
131cb93a386Sopenharmony_ciDEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
132cb93a386Sopenharmony_ci                                     RectanizerBench::kRand_RectType);)
133cb93a386Sopenharmony_ciDEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
134cb93a386Sopenharmony_ci                                     RectanizerBench::kRandPow2_RectType);)
135cb93a386Sopenharmony_ciDEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
136cb93a386Sopenharmony_ci                                     RectanizerBench::kSmallPow2_RectType);)
137