blob: 737787abc2de96d91c7afa45a8afeff88de6e5fa [file] [log] [blame]
bsalomon@google.comd62e88e2013-02-01 14:19:27 +00001/*
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 "SampleCode.h"
8#include "SkCanvas.h"
bsalomon@google.comd62e88e2013-02-01 14:19:27 +00009#include "SkPaint.h"
10#include "SkRandom.h"
11#include "SkShader.h"
12#include "SkView.h"
13
14/**
Brian Salomon09d994e2016-12-21 11:14:46 -050015 * Animated sample used to develop a predecessor of GrDrawOp combining.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000016 */
17class ManyRectsView : public SampleView {
18private:
19 enum {
20 N = 1000,
21 };
22
23public:
24 ManyRectsView() {}
25
26protected:
mtklein36352bf2015-03-25 18:17:31 -070027 bool onQuery(SkEvent* evt) override {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000028 if (SampleCode::TitleQ(*evt)) {
29 SampleCode::TitleR(evt, "ManyRects");
30 return true;
31 }
32 return this->INHERITED::onQuery(evt);
33 }
34
mtkleinf0599002015-07-13 06:18:39 -070035 void onDrawContent(SkCanvas* canvas) override {
Mike Reed3661bc92017-02-22 13:21:42 -050036 SkISize dsize = canvas->getBaseLayerSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000037 canvas->clear(0xFFF0E0F0);
38
39 for (int i = 0; i < N; ++i) {
40 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
41 SkIntToScalar(fRandom.nextRangeU(10, 100)));
42 int x = fRandom.nextRangeU(0, dsize.fWidth);
43 int y = fRandom.nextRangeU(0, dsize.fHeight);
44 canvas->save();
45
46 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
Brian Salomon09d994e2016-12-21 11:14:46 -050047 // Uncomment to test rotated rect draw combining.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000048 if (false) {
49 SkMatrix rotate;
50 rotate.setRotate(fRandom.nextUScalar1() * 360,
51 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
52 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
53 canvas->concat(rotate);
54 }
55 SkRect clipRect = rect;
Brian Salomon09d994e2016-12-21 11:14:46 -050056 // This clip will always contain the entire rect. It's here to give the GPU op combining
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000057 // code a little more challenge.
58 clipRect.outset(10, 10);
59 canvas->clipRect(clipRect);
60 SkPaint paint;
61 paint.setColor(fRandom.nextU());
62 canvas->drawRect(rect, paint);
63 canvas->restore();
64 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000065 }
66
67private:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000068 SkRandom fRandom;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000069 typedef SampleView INHERITED;
70};
71
72//////////////////////////////////////////////////////////////////////////////
73
74static SkView* MyFactory() { return new ManyRectsView; }
75static SkViewRegister reg(MyFactory);