blob: 04d8a41129cf64058cf045a2a8e7e2d38308cc10 [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/**
17 * Animated sample used to develop batched rect implementation in GrInOrderDrawBuffer.
18 */
19class ManyRectsView : public SampleView {
20private:
21 enum {
22 N = 1000,
23 };
24
25public:
26 ManyRectsView() {}
27
28protected:
29 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
30 if (SampleCode::TitleQ(*evt)) {
31 SampleCode::TitleR(evt, "ManyRects");
32 return true;
33 }
34 return this->INHERITED::onQuery(evt);
35 }
36
37 virtual void onDrawContent(SkCanvas* canvas) {
38 SkISize dsize = canvas->getDeviceSize();
39 SkRect canvasRect = SkRect::MakeWH(SkIntToScalar(dsize.fWidth),
40 SkIntToScalar(dsize.fHeight));
41 canvas->clear(0xFFF0E0F0);
42
43 for (int i = 0; i < N; ++i) {
44 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
45 SkIntToScalar(fRandom.nextRangeU(10, 100)));
46 int x = fRandom.nextRangeU(0, dsize.fWidth);
47 int y = fRandom.nextRangeU(0, dsize.fHeight);
48 canvas->save();
49
50 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
51 // Rotation messes up the GPU batching because of the clip below. We don't notice
52 // that the rect is inside the clip so the clip changes interrupt batching.
53 if (false) {
54 SkMatrix rotate;
55 rotate.setRotate(fRandom.nextUScalar1() * 360,
56 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
57 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
58 canvas->concat(rotate);
59 }
60 SkRect clipRect = rect;
61 // This clip will always contain the entire rect. It's here to give the GPU batching
62 // code a little more challenge.
63 clipRect.outset(10, 10);
64 canvas->clipRect(clipRect);
65 SkPaint paint;
66 paint.setColor(fRandom.nextU());
67 canvas->drawRect(rect, paint);
68 canvas->restore();
69 }
70 this->inval(NULL);
71 }
72
73private:
74 SkMWCRandom fRandom;
75 typedef SampleView INHERITED;
76};
77
78//////////////////////////////////////////////////////////////////////////////
79
80static SkView* MyFactory() { return new ManyRectsView; }
81static SkViewRegister reg(MyFactory);