1 /*
2  * Copyright 2020 Google LLC
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 "bench/Benchmark.h"
9 #include "include/utils/SkRandom.h"
10 #include "src/gpu/geometry/GrQuad.h"
11 #include "src/gpu/geometry/GrQuadUtils.h"
12 
13 class GrQuadBoundsBench : public Benchmark {
14 public:
GrQuadBoundsBench(bool perspective)15     GrQuadBoundsBench(bool perspective)
16             : fPerspective(perspective) {
17         fName.printf("grquad_bounds_%s", perspective ? "3d" : "2d");
18     }
19 
isSuitableFor(Backend backend)20     bool isSuitableFor(Backend backend) override {
21         return backend == kNonRendering_Backend;
22     }
23 
24 protected:
25     static constexpr int kQuadCount = 1000;
26 
onGetName()27     const char* onGetName() override {
28         return fName.c_str();
29     }
30 
onDelayedSetup()31     void onDelayedSetup() override {
32         SkRandom r;
33         for (int i = 0; i < kQuadCount; ++i) {
34             for (int j = 0; j < 4; ++j) {
35                 fQuads[i].xs()[j] = r.nextRangeF(-100.f, 100.f);
36                 fQuads[i].ys()[j] = r.nextRangeF(-100.f, 100.f);
37                 if (fPerspective) {
38                     // Biased towards in front of the viewpoint, but do include some that require
39                     // the vertices to be clipped against w = 0.
40                     fQuads[i].ws()[j] = r.nextRangeF(-1.f, 10.f);
41                 } else {
42                     fQuads[i].ws()[j] = 1.f;
43                 }
44             }
45             fQuads[i].setQuadType(fPerspective ? GrQuad::Type::kPerspective
46                                                : GrQuad::Type::kGeneral);
47         }
48     }
49 
onDraw(int loops,SkCanvas *)50     void onDraw(int loops, SkCanvas*) override {
51         SkScalar area = 0.f;
52         for (int i = 0; i < loops; ++i) {
53             for (int j = 0; j < kQuadCount; ++j) {
54                 SkRect qb = fQuads[j].bounds();
55                 area += qb.width() + qb.height();
56             }
57         }
58         // Must persist this calculation in order to prevent the compiler from optimizing the
59         // loops away.
60         fArea = area;
61     }
62 
63     SkString     fName;
64     bool         fPerspective;
65     GrQuad       fQuads[kQuadCount];
66     SkScalar     fArea;
67 
68     using INHERITED = Benchmark;
69 };
70 
71 ///////////////////////////////////////////////////////////////////////////////////////////////////
72 
73 DEF_BENCH( return new GrQuadBoundsBench(/* persp */ false); )
74 DEF_BENCH( return new GrQuadBoundsBench(/* persp */ true); )
75