blob: 779c9e99ac8525e8e35f5a8712d4822cfe6cfb58 [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
bsalomon@google.comd62e88e2013-02-01 14:19:27 +00008#include "SkCanvas.h"
bsalomon@google.comd62e88e2013-02-01 14:19:27 +00009#include "SkPaint.h"
10#include "SkRandom.h"
11#include "SkShader.h"
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000012
13/**
Brian Salomon09d994e2016-12-21 11:14:46 -050014 * Animated sample used to develop a predecessor of GrDrawOp combining.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000015 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040016class ManyRectsView : public Sample {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000017private:
18 enum {
19 N = 1000,
20 };
21
22public:
23 ManyRectsView() {}
24
25protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040026 bool onQuery(Sample::Event* evt) override {
27 if (Sample::TitleQ(*evt)) {
28 Sample::TitleR(evt, "ManyRects");
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000029 return true;
30 }
31 return this->INHERITED::onQuery(evt);
32 }
33
mtkleinf0599002015-07-13 06:18:39 -070034 void onDrawContent(SkCanvas* canvas) override {
Mike Reed3661bc92017-02-22 13:21:42 -050035 SkISize dsize = canvas->getBaseLayerSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000036 canvas->clear(0xFFF0E0F0);
37
38 for (int i = 0; i < N; ++i) {
39 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
40 SkIntToScalar(fRandom.nextRangeU(10, 100)));
41 int x = fRandom.nextRangeU(0, dsize.fWidth);
42 int y = fRandom.nextRangeU(0, dsize.fHeight);
43 canvas->save();
44
45 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
Brian Salomon09d994e2016-12-21 11:14:46 -050046 // Uncomment to test rotated rect draw combining.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000047 if (false) {
48 SkMatrix rotate;
49 rotate.setRotate(fRandom.nextUScalar1() * 360,
50 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
51 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
52 canvas->concat(rotate);
53 }
54 SkRect clipRect = rect;
Brian Salomon09d994e2016-12-21 11:14:46 -050055 // 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 +000056 // code a little more challenge.
57 clipRect.outset(10, 10);
58 canvas->clipRect(clipRect);
59 SkPaint paint;
60 paint.setColor(fRandom.nextU());
61 canvas->drawRect(rect, paint);
62 canvas->restore();
63 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000064 }
65
66private:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000067 SkRandom fRandom;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040068 typedef Sample INHERITED;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000069};
70
71//////////////////////////////////////////////////////////////////////////////
72
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040073DEF_SAMPLE( return new ManyRectsView(); )