caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkRandom.h" |
| 10 | |
| 11 | #define W 400 |
| 12 | #define H 400 |
| 13 | #define N 10 |
| 14 | |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 15 | static const SkScalar SH = SkIntToScalar(H); |
| 16 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 17 | static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 18 | p->moveTo(rand.nextRangeScalar(0, W), rand.nextRangeScalar(0, H)); |
| 19 | for (int x = 0; x < 2; ++x) { |
| 20 | p->quadTo(rand.nextRangeScalar(W / 4, W), rand.nextRangeScalar(0, H), |
| 21 | rand.nextRangeScalar(0, W), rand.nextRangeScalar(H / 4, H)); |
| 22 | } |
| 23 | paint->setColor(rand.nextU()); |
| 24 | SkScalar width = rand.nextRangeScalar(1, 5); |
| 25 | width *= width; |
| 26 | paint->setStrokeWidth(width); |
| 27 | paint->setAlpha(0xFF); |
| 28 | } |
| 29 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 30 | static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 31 | p->moveTo(rand.nextRangeScalar(0, W), rand.nextRangeScalar(0, H)); |
| 32 | for (int x = 0; x < 2; ++x) { |
| 33 | p->cubicTo(rand.nextRangeScalar(W / 4, W), rand.nextRangeScalar(0, H), |
| 34 | rand.nextRangeScalar(0, W), rand.nextRangeScalar(H / 4, H), |
| 35 | rand.nextRangeScalar(W / 4, W), rand.nextRangeScalar(H / 4, H)); |
| 36 | } |
| 37 | paint->setColor(rand.nextU()); |
| 38 | SkScalar width = rand.nextRangeScalar(1, 5); |
| 39 | width *= width; |
| 40 | paint->setStrokeWidth(width); |
| 41 | paint->setAlpha(0xFF); |
| 42 | } |
| 43 | |
| 44 | class BeziersGM : public skiagm::GM { |
| 45 | public: |
| 46 | BeziersGM() {} |
| 47 | |
| 48 | protected: |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | SkString onShortName() override { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 51 | return SkString("beziers"); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | SkISize onISize() override { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 55 | return SkISize::Make(W, H*2); |
| 56 | } |
| 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | void onDraw(SkCanvas* canvas) override { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 59 | SkPaint paint; |
| 60 | paint.setStyle(SkPaint::kStroke_Style); |
| 61 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
| 62 | paint.setAntiAlias(true); |
| 63 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 64 | SkRandom rand; |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 65 | for (int i = 0; i < N; i++) { |
| 66 | SkPath p; |
| 67 | rnd_quad(&p, &paint, rand); |
| 68 | canvas->drawPath(p, paint); |
| 69 | } |
| 70 | canvas->translate(0, SH); |
| 71 | for (int i = 0; i < N; i++) { |
| 72 | SkPath p; |
| 73 | rnd_cubic(&p, &paint, rand); |
| 74 | canvas->drawPath(p, paint); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | typedef skiagm::GM INHERITED; |
| 80 | }; |
| 81 | |
| 82 | static skiagm::GM* F0(void*) { return new BeziersGM; } |
| 83 | static skiagm::GMRegistry R0(F0); |