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 | */ |
| 7 | #include "SampleCode.h" |
| 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" |
| 12 | #include "SkView.h" |
| 13 | |
| 14 | /** |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 15 | * Animated sample used to develop a predecessor of GrDrawOp combining. |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 16 | */ |
| 17 | class ManyRectsView : public SampleView { |
| 18 | private: |
| 19 | enum { |
| 20 | N = 1000, |
| 21 | }; |
| 22 | |
| 23 | public: |
| 24 | ManyRectsView() {} |
| 25 | |
| 26 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | bool onQuery(SkEvent* evt) override { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 28 | if (SampleCode::TitleQ(*evt)) { |
| 29 | SampleCode::TitleR(evt, "ManyRects"); |
| 30 | return true; |
| 31 | } |
| 32 | return this->INHERITED::onQuery(evt); |
| 33 | } |
| 34 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 35 | void onDrawContent(SkCanvas* canvas) override { |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 36 | SkISize dsize = canvas->getBaseLayerSize(); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 37 | canvas->clear(0xFFF0E0F0); |
| 38 | |
| 39 | for (int i = 0; i < N; ++i) { |
| 40 | SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)), |
| 41 | SkIntToScalar(fRandom.nextRangeU(10, 100))); |
| 42 | int x = fRandom.nextRangeU(0, dsize.fWidth); |
| 43 | int y = fRandom.nextRangeU(0, dsize.fHeight); |
| 44 | canvas->save(); |
| 45 | |
| 46 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 47 | // Uncomment to test rotated rect draw combining. |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 48 | if (false) { |
| 49 | SkMatrix rotate; |
| 50 | rotate.setRotate(fRandom.nextUScalar1() * 360, |
| 51 | SkIntToScalar(x) + SkScalarHalf(rect.fRight), |
| 52 | SkIntToScalar(y) + SkScalarHalf(rect.fBottom)); |
| 53 | canvas->concat(rotate); |
| 54 | } |
| 55 | SkRect clipRect = rect; |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 56 | // 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] | 57 | // code a little more challenge. |
| 58 | clipRect.outset(10, 10); |
| 59 | canvas->clipRect(clipRect); |
| 60 | SkPaint paint; |
| 61 | paint.setColor(fRandom.nextU()); |
| 62 | canvas->drawRect(rect, paint); |
| 63 | canvas->restore(); |
| 64 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 68 | SkRandom fRandom; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 69 | typedef SampleView INHERITED; |
| 70 | }; |
| 71 | |
| 72 | ////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static SkView* MyFactory() { return new ManyRectsView; } |
| 75 | static SkViewRegister reg(MyFactory); |