xref: /aosp_15_r20/external/skia/tools/viewer/ManyRectsSlide.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 #include "include/core/SkCanvas.h"
8 #include "include/core/SkPaint.h"
9 #include "include/core/SkShader.h"
10 #include "src/base/SkRandom.h"
11 #include "tools/viewer/Slide.h"
12 
13 /**
14  * Animated sample used to develop a predecessor of GrDrawOp combining.
15  */
16 class ManyRectsSlide : public Slide {
17 private:
18     enum {
19         N = 1000,
20     };
21 
22 public:
ManyRectsSlide()23     ManyRectsSlide() { fName = "ManyRects"; }
24 
draw(SkCanvas * canvas)25     void draw(SkCanvas* canvas) override {
26         SkISize dsize = canvas->getBaseLayerSize();
27         canvas->clear(0xFFF0E0F0);
28 
29         for (int i = 0; i < N; ++i) {
30             SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
31                                          SkIntToScalar(fRandom.nextRangeU(10, 100)));
32             int x = fRandom.nextRangeU(0, dsize.fWidth);
33             int y = fRandom.nextRangeU(0, dsize.fHeight);
34             canvas->save();
35 
36             canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
37             // Uncomment to test rotated rect draw combining.
38             if ((false)) {
39                 SkMatrix rotate;
40                 rotate.setRotate(fRandom.nextUScalar1() * 360,
41                                  SkIntToScalar(x) + SkScalarHalf(rect.fRight),
42                                  SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
43                 canvas->concat(rotate);
44             }
45             SkRect clipRect = rect;
46             // This clip will always contain the entire rect. It's here to give the GPU op combining
47             // code a little more challenge.
48             clipRect.outset(10, 10);
49             canvas->clipRect(clipRect);
50             SkPaint paint;
51             paint.setColor(fRandom.nextU());
52             canvas->drawRect(rect, paint);
53             canvas->restore();
54         }
55     }
56 
57 private:
58     SkRandom fRandom;
59 };
60 
61 //////////////////////////////////////////////////////////////////////////////
62 
63 DEF_SLIDE(return new ManyRectsSlide();)
64