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 | /** |
joshualitt | b542bae | 2015-07-28 09:58:39 -0700 | [diff] [blame] | 15 | * Animated sample used to develop batched rect implementation in GrBufferedDrawTarget. |
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 { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 36 | SkISize dsize = canvas->getDeviceSize(); |
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)); |
| 47 | // Rotation messes up the GPU batching because of the clip below. We don't notice |
| 48 | // that the rect is inside the clip so the clip changes interrupt batching. |
| 49 | if (false) { |
| 50 | SkMatrix rotate; |
| 51 | rotate.setRotate(fRandom.nextUScalar1() * 360, |
| 52 | SkIntToScalar(x) + SkScalarHalf(rect.fRight), |
| 53 | SkIntToScalar(y) + SkScalarHalf(rect.fBottom)); |
| 54 | canvas->concat(rotate); |
| 55 | } |
| 56 | SkRect clipRect = rect; |
| 57 | // This clip will always contain the entire rect. It's here to give the GPU batching |
| 58 | // code a little more challenge. |
| 59 | clipRect.outset(10, 10); |
| 60 | canvas->clipRect(clipRect); |
| 61 | SkPaint paint; |
| 62 | paint.setColor(fRandom.nextU()); |
| 63 | canvas->drawRect(rect, paint); |
| 64 | canvas->restore(); |
| 65 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | this->inval(nullptr); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 70 | SkRandom fRandom; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 71 | typedef SampleView INHERITED; |
| 72 | }; |
| 73 | |
| 74 | ////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | static SkView* MyFactory() { return new ManyRectsView; } |
| 77 | static SkViewRegister reg(MyFactory); |