blob: 0290af2ce673d5fab6e230b7b9a26fb2ce2aa943 [file] [log] [blame]
bsalomon@google.comd62e88e2013-02-01 14:19:27 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkDevice.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkView.h"
15
16/**
joshualittb542bae2015-07-28 09:58:39 -070017 * Animated sample used to develop batched rect implementation in GrBufferedDrawTarget.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000018 */
19class ManyRectsView : public SampleView {
20private:
21 enum {
22 N = 1000,
23 };
24
25public:
26 ManyRectsView() {}
27
28protected:
mtklein36352bf2015-03-25 18:17:31 -070029 bool onQuery(SkEvent* evt) override {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000030 if (SampleCode::TitleQ(*evt)) {
31 SampleCode::TitleR(evt, "ManyRects");
32 return true;
33 }
34 return this->INHERITED::onQuery(evt);
35 }
36
mtkleinf0599002015-07-13 06:18:39 -070037 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000038 SkISize dsize = canvas->getDeviceSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000039 canvas->clear(0xFFF0E0F0);
40
41 for (int i = 0; i < N; ++i) {
42 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
43 SkIntToScalar(fRandom.nextRangeU(10, 100)));
44 int x = fRandom.nextRangeU(0, dsize.fWidth);
45 int y = fRandom.nextRangeU(0, dsize.fHeight);
46 canvas->save();
47
48 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
49 // Rotation messes up the GPU batching because of the clip below. We don't notice
50 // that the rect is inside the clip so the clip changes interrupt batching.
51 if (false) {
52 SkMatrix rotate;
53 rotate.setRotate(fRandom.nextUScalar1() * 360,
54 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
55 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
56 canvas->concat(rotate);
57 }
58 SkRect clipRect = rect;
59 // This clip will always contain the entire rect. It's here to give the GPU batching
60 // code a little more challenge.
61 clipRect.outset(10, 10);
62 canvas->clipRect(clipRect);
63 SkPaint paint;
64 paint.setColor(fRandom.nextU());
65 canvas->drawRect(rect, paint);
66 canvas->restore();
67 }
halcanary96fcdcc2015-08-27 07:41:13 -070068 this->inval(nullptr);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000069 }
70
71private:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000072 SkRandom fRandom;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000073 typedef SampleView INHERITED;
74};
75
76//////////////////////////////////////////////////////////////////////////////
77
78static SkView* MyFactory() { return new ManyRectsView; }
79static SkViewRegister reg(MyFactory);