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