blob: 950b9833b06743e28e1489ac78fa004b905c9623 [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"
9#include "SkDevice.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkShader.h"
13#include "SkView.h"
14
15/**
joshualittb542bae2015-07-28 09:58:39 -070016 * Animated sample used to develop batched rect implementation in GrBufferedDrawTarget.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000017 */
18class ManyRectsView : public SampleView {
19private:
20 enum {
21 N = 1000,
22 };
23
24public:
25 ManyRectsView() {}
26
27protected:
mtklein36352bf2015-03-25 18:17:31 -070028 bool onQuery(SkEvent* evt) override {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000029 if (SampleCode::TitleQ(*evt)) {
30 SampleCode::TitleR(evt, "ManyRects");
31 return true;
32 }
33 return this->INHERITED::onQuery(evt);
34 }
35
mtkleinf0599002015-07-13 06:18:39 -070036 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000037 SkISize dsize = canvas->getDeviceSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000038 canvas->clear(0xFFF0E0F0);
39
40 for (int i = 0; i < N; ++i) {
41 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
42 SkIntToScalar(fRandom.nextRangeU(10, 100)));
43 int x = fRandom.nextRangeU(0, dsize.fWidth);
44 int y = fRandom.nextRangeU(0, dsize.fHeight);
45 canvas->save();
46
47 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
48 // Rotation messes up the GPU batching because of the clip below. We don't notice
49 // that the rect is inside the clip so the clip changes interrupt batching.
50 if (false) {
51 SkMatrix rotate;
52 rotate.setRotate(fRandom.nextUScalar1() * 360,
53 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
54 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
55 canvas->concat(rotate);
56 }
57 SkRect clipRect = rect;
58 // This clip will always contain the entire rect. It's here to give the GPU batching
59 // code a little more challenge.
60 clipRect.outset(10, 10);
61 canvas->clipRect(clipRect);
62 SkPaint paint;
63 paint.setColor(fRandom.nextU());
64 canvas->drawRect(rect, paint);
65 canvas->restore();
66 }
halcanary96fcdcc2015-08-27 07:41:13 -070067 this->inval(nullptr);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000068 }
69
70private:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000071 SkRandom fRandom;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000072 typedef SampleView INHERITED;
73};
74
75//////////////////////////////////////////////////////////////////////////////
76
77static SkView* MyFactory() { return new ManyRectsView; }
78static SkViewRegister reg(MyFactory);