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