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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 7 | #include "Sample.h" |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 9 | #include "SkPaint.h" |
| 10 | #include "SkRandom.h" |
| 11 | #include "SkShader.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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 26 | bool onQuery(Sample::Event* evt) override { |
| 27 | if (Sample::TitleQ(*evt)) { |
| 28 | Sample::TitleR(evt, "ManyRects"); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 29 | return true; |
| 30 | } |
| 31 | return this->INHERITED::onQuery(evt); |
| 32 | } |
| 33 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 34 | void onDrawContent(SkCanvas* canvas) override { |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 35 | SkISize dsize = canvas->getBaseLayerSize(); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 36 | 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 Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 46 | // Uncomment to test rotated rect draw combining. |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 47 | 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 Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 55 | // 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] | 56 | // 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.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 67 | SkRandom fRandom; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 68 | typedef Sample INHERITED; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 73 | DEF_SAMPLE( return new ManyRectsView(); ) |