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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 16 | constexpr SkScalar SH = SkIntToScalar(H); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 17 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 18 | static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) { |
mtklein | 477ba0c | 2016-09-26 08:18:43 -0700 | [diff] [blame] | 19 | auto a = rand.nextRangeScalar(0,W), |
| 20 | b = rand.nextRangeScalar(0,H); |
| 21 | p->moveTo(a,b); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 22 | for (int x = 0; x < 2; ++x) { |
mtklein | 477ba0c | 2016-09-26 08:18:43 -0700 | [diff] [blame] | 23 | auto c = rand.nextRangeScalar(W/4, W), |
| 24 | d = rand.nextRangeScalar( 0, H), |
| 25 | e = rand.nextRangeScalar( 0, W), |
| 26 | f = rand.nextRangeScalar(H/4, H); |
| 27 | p->quadTo(c,d,e,f); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 28 | } |
| 29 | paint->setColor(rand.nextU()); |
| 30 | SkScalar width = rand.nextRangeScalar(1, 5); |
| 31 | width *= width; |
| 32 | paint->setStrokeWidth(width); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 33 | paint->setAlphaf(1.0f); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 34 | } |
| 35 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 36 | static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) { |
mtklein | 1c127cc | 2016-09-26 06:29:18 -0700 | [diff] [blame] | 37 | auto a = rand.nextRangeScalar(0,W), |
| 38 | b = rand.nextRangeScalar(0,H); |
| 39 | p->moveTo(a,b); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 40 | for (int x = 0; x < 2; ++x) { |
mtklein | 1c127cc | 2016-09-26 06:29:18 -0700 | [diff] [blame] | 41 | auto c = rand.nextRangeScalar(W/4, W), |
| 42 | d = rand.nextRangeScalar( 0, H), |
| 43 | e = rand.nextRangeScalar( 0, W), |
| 44 | f = rand.nextRangeScalar(H/4, H), |
| 45 | g = rand.nextRangeScalar(W/4, W), |
| 46 | h = rand.nextRangeScalar(H/4, H); |
| 47 | p->cubicTo(c,d,e,f,g,h); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 48 | } |
| 49 | paint->setColor(rand.nextU()); |
| 50 | SkScalar width = rand.nextRangeScalar(1, 5); |
| 51 | width *= width; |
| 52 | paint->setStrokeWidth(width); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 53 | paint->setAlphaf(1.0f); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | class BeziersGM : public skiagm::GM { |
| 57 | public: |
| 58 | BeziersGM() {} |
| 59 | |
| 60 | protected: |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | SkString onShortName() override { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 63 | return SkString("beziers"); |
| 64 | } |
| 65 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | SkISize onISize() override { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 67 | return SkISize::Make(W, H*2); |
| 68 | } |
| 69 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 70 | void onDraw(SkCanvas* canvas) override { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 71 | SkPaint paint; |
| 72 | paint.setStyle(SkPaint::kStroke_Style); |
| 73 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
| 74 | paint.setAntiAlias(true); |
| 75 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 76 | SkRandom rand; |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 77 | for (int i = 0; i < N; i++) { |
| 78 | SkPath p; |
| 79 | rnd_quad(&p, &paint, rand); |
| 80 | canvas->drawPath(p, paint); |
| 81 | } |
| 82 | canvas->translate(0, SH); |
| 83 | for (int i = 0; i < N; i++) { |
| 84 | SkPath p; |
| 85 | rnd_cubic(&p, &paint, rand); |
| 86 | canvas->drawPath(p, paint); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef skiagm::GM INHERITED; |
| 92 | }; |
| 93 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 94 | DEF_GM( return new BeziersGM; ) |