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 "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "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:
19     FSRectBench() : fInit(false) { }
20 
21 protected:
22     const char* onGetName() override { return "fullscreen_rects"; }
23 
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 
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     enum {
51         W = 640,
52         H = 480,
53         N = 300,
54     };
55     SkRect  fRects[N];
56     SkColor fColors[N];
57     bool fInit;
58 
59     typedef Benchmark INHERITED;
60 };
61 
62 DEF_BENCH(return new FSRectBench();)
63