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