blob: b5eb6b00bf9ac41ee902061e9c7672ce3d036d75 [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/**
joshualittb542bae2015-07-28 09:58:39 -070015 * Animated sample used to develop batched rect implementation in GrBufferedDrawTarget.
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 {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000036 SkISize dsize = canvas->getDeviceSize();
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));
47 // Rotation messes up the GPU batching because of the clip below. We don't notice
48 // that the rect is inside the clip so the clip changes interrupt batching.
49 if (false) {
50 SkMatrix rotate;
51 rotate.setRotate(fRandom.nextUScalar1() * 360,
52 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
53 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
54 canvas->concat(rotate);
55 }
56 SkRect clipRect = rect;
57 // This clip will always contain the entire rect. It's here to give the GPU batching
58 // code a little more challenge.
59 clipRect.outset(10, 10);
60 canvas->clipRect(clipRect);
61 SkPaint paint;
62 paint.setColor(fRandom.nextU());
63 canvas->drawRect(rect, paint);
64 canvas->restore();
65 }
halcanary96fcdcc2015-08-27 07:41:13 -070066 this->inval(nullptr);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000067 }
68
69private:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000070 SkRandom fRandom;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000071 typedef SampleView INHERITED;
72};
73
74//////////////////////////////////////////////////////////////////////////////
75
76static SkView* MyFactory() { return new ManyRectsView; }
77static SkViewRegister reg(MyFactory);