bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkDevice.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkRandom.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkView.h" |
| 15 | |
| 16 | /** |
joshualitt | b542bae | 2015-07-28 09:58:39 -0700 | [diff] [blame] | 17 | * Animated sample used to develop batched rect implementation in GrBufferedDrawTarget. |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 18 | */ |
| 19 | class ManyRectsView : public SampleView { |
| 20 | private: |
| 21 | enum { |
| 22 | N = 1000, |
| 23 | }; |
| 24 | |
| 25 | public: |
| 26 | ManyRectsView() {} |
| 27 | |
| 28 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | bool onQuery(SkEvent* evt) override { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 30 | if (SampleCode::TitleQ(*evt)) { |
| 31 | SampleCode::TitleR(evt, "ManyRects"); |
| 32 | return true; |
| 33 | } |
| 34 | return this->INHERITED::onQuery(evt); |
| 35 | } |
| 36 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 37 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 38 | SkISize dsize = canvas->getDeviceSize(); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 39 | canvas->clear(0xFFF0E0F0); |
| 40 | |
| 41 | for (int i = 0; i < N; ++i) { |
| 42 | SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)), |
| 43 | SkIntToScalar(fRandom.nextRangeU(10, 100))); |
| 44 | int x = fRandom.nextRangeU(0, dsize.fWidth); |
| 45 | int y = fRandom.nextRangeU(0, dsize.fHeight); |
| 46 | canvas->save(); |
| 47 | |
| 48 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 49 | // Rotation messes up the GPU batching because of the clip below. We don't notice |
| 50 | // that the rect is inside the clip so the clip changes interrupt batching. |
| 51 | if (false) { |
| 52 | SkMatrix rotate; |
| 53 | rotate.setRotate(fRandom.nextUScalar1() * 360, |
| 54 | SkIntToScalar(x) + SkScalarHalf(rect.fRight), |
| 55 | SkIntToScalar(y) + SkScalarHalf(rect.fBottom)); |
| 56 | canvas->concat(rotate); |
| 57 | } |
| 58 | SkRect clipRect = rect; |
| 59 | // This clip will always contain the entire rect. It's here to give the GPU batching |
| 60 | // code a little more challenge. |
| 61 | clipRect.outset(10, 10); |
| 62 | canvas->clipRect(clipRect); |
| 63 | SkPaint paint; |
| 64 | paint.setColor(fRandom.nextU()); |
| 65 | canvas->drawRect(rect, paint); |
| 66 | canvas->restore(); |
| 67 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | this->inval(nullptr); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 72 | SkRandom fRandom; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 73 | typedef SampleView INHERITED; |
| 74 | }; |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| 78 | static SkView* MyFactory() { return new ManyRectsView; } |
| 79 | static SkViewRegister reg(MyFactory); |