blob: 7278eed68c0eed64df223c748f02a4f7fefc94b9 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkCanvas.h"
8#include "include/core/SkPaint.h"
9#include "include/core/SkShader.h"
10#include "include/utils/SkRandom.h"
11#include "samplecode/Sample.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:
Hal Canary8a027312019-07-03 10:55:44 -040026 SkString name() override { return SkString("ManyRects"); }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000027
mtkleinf0599002015-07-13 06:18:39 -070028 void onDrawContent(SkCanvas* canvas) override {
Mike Reed3661bc92017-02-22 13:21:42 -050029 SkISize dsize = canvas->getBaseLayerSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000030 canvas->clear(0xFFF0E0F0);
31
32 for (int i = 0; i < N; ++i) {
33 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
34 SkIntToScalar(fRandom.nextRangeU(10, 100)));
35 int x = fRandom.nextRangeU(0, dsize.fWidth);
36 int y = fRandom.nextRangeU(0, dsize.fHeight);
37 canvas->save();
38
39 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
Brian Salomon09d994e2016-12-21 11:14:46 -050040 // Uncomment to test rotated rect draw combining.
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000041 if (false) {
42 SkMatrix rotate;
43 rotate.setRotate(fRandom.nextUScalar1() * 360,
44 SkIntToScalar(x) + SkScalarHalf(rect.fRight),
45 SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
46 canvas->concat(rotate);
47 }
48 SkRect clipRect = rect;
Brian Salomon09d994e2016-12-21 11:14:46 -050049 // 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 +000050 // code a little more challenge.
51 clipRect.outset(10, 10);
52 canvas->clipRect(clipRect);
53 SkPaint paint;
54 paint.setColor(fRandom.nextU());
55 canvas->drawRect(rect, paint);
56 canvas->restore();
57 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000058 }
59
60private:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000061 SkRandom fRandom;
John Stiles7571f9e2020-09-02 22:42:33 -040062 using INHERITED = Sample;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000063};
64
65//////////////////////////////////////////////////////////////////////////////
66
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040067DEF_SAMPLE( return new ManyRectsView(); )