xref: /aosp_15_r20/external/skia/bench/FSRectBench.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2013 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 
8 #include "bench/Benchmark.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "src/base/SkRandom.h"
12 
13 /**
14  * Draws full screen opaque rectangles. It is designed to test any optimizations in the GPU backend
15  * to turn such draws into clears.
16  */
17 class FSRectBench : public Benchmark {
18 public:
FSRectBench()19     FSRectBench() : fInit(false) { }
20 
21 protected:
onGetName()22     const char* onGetName() override { return "fullscreen_rects"; }
23 
onDelayedSetup()24     void onDelayedSetup() override {
25         if (!fInit) {
26             SkRandom rand;
27             static const SkScalar kMinOffset = 0;
28             static const SkScalar kMaxOffset = 100 * SK_Scalar1;
29             static const SkScalar kOffsetRange = kMaxOffset - kMinOffset;
30             for (int i = 0; i < N; ++i) {
31                 fRects[i].fLeft = -kMinOffset - rand.nextUScalar1() * kOffsetRange;
32                 fRects[i].fTop = -kMinOffset - rand.nextUScalar1() * kOffsetRange;
33                 fRects[i].fRight = W + kMinOffset + rand.nextUScalar1() * kOffsetRange;
34                 fRects[i].fBottom = H + kMinOffset + rand.nextUScalar1() * kOffsetRange;
35                 fColors[i] = rand.nextU() | 0xFF000000;
36             }
37             fInit = true;
38         }
39     }
40 
onDraw(int loops,SkCanvas * canvas)41     void onDraw(int loops, SkCanvas* canvas) override {
42         SkPaint paint;
43         for (int i = 0; i < loops; ++i) {
44             paint.setColor(fColors[i % N]);
45             canvas->drawRect(fRects[i % N], paint);
46         }
47     }
48 
49 private:
50     static constexpr int W = 640;
51     static constexpr int H = 480;
52     static constexpr int N = 300;
53 
54     SkRect  fRects[N];
55     SkColor fColors[N];
56     bool fInit;
57 
58     using INHERITED = Benchmark;
59 };
60 
61 DEF_BENCH(return new FSRectBench();)
62