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