bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #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.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 12 | |
| 13 | /** |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 14 | * Animated sample used to develop a predecessor of GrDrawOp combining. |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 15 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 16 | class ManyRectsView : public Sample { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 17 | private: |
| 18 | enum { |
| 19 | N = 1000, |
| 20 | }; |
| 21 | |
| 22 | public: |
| 23 | ManyRectsView() {} |
| 24 | |
| 25 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 26 | SkString name() override { return SkString("ManyRects"); } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 27 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 28 | void onDrawContent(SkCanvas* canvas) override { |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 29 | SkISize dsize = canvas->getBaseLayerSize(); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 30 | 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 Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 40 | // Uncomment to test rotated rect draw combining. |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 41 | 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 Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 49 | // This clip will always contain the entire rect. It's here to give the GPU op combining |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 50 | // 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.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 61 | SkRandom fRandom; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 62 | using INHERITED = Sample; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | ////////////////////////////////////////////////////////////////////////////// |
| 66 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 67 | DEF_SAMPLE( return new ManyRectsView(); ) |