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