egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkCanvas.h" |
| 10 | #include "SkTArray.h" |
| 11 | |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 12 | class ConicPathsGM : public skiagm::GM { |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 13 | protected: |
| 14 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 15 | SkString onShortName() override { |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 16 | return SkString("conicpaths"); |
| 17 | } |
| 18 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 | SkISize onISize() override { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 20 | return SkISize::Make(920, 960); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 21 | } |
| 22 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | void onOnceBeforeDraw() override { |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 24 | { |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 25 | const SkScalar w = SkScalarSqrt(2)/2; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 26 | SkPath* conicCirlce = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 27 | conicCirlce->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 28 | conicCirlce->conicTo(0, 50, 50, 50, w); |
| 29 | conicCirlce->rConicTo(50, 0, 50, -50, w); |
| 30 | conicCirlce->rConicTo(0, -50, -50, -50, w); |
| 31 | conicCirlce->rConicTo(-50, 0, -50, 50, w); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 32 | |
| 33 | } |
| 34 | { |
| 35 | SkPath* hyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 36 | hyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 37 | hyperbola->conicTo(0, 100, 100, 100, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 38 | } |
| 39 | { |
| 40 | SkPath* thinHyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 41 | thinHyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 42 | thinHyperbola->conicTo(100, 100, 5, 0, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 43 | } |
| 44 | { |
| 45 | SkPath* veryThinHyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 46 | veryThinHyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 47 | veryThinHyperbola->conicTo(100, 100, 1, 0, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 48 | } |
| 49 | { |
| 50 | SkPath* closedHyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 51 | closedHyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 52 | closedHyperbola->conicTo(100, 100, 0, 0, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 53 | } |
| 54 | { |
| 55 | // using 1 as weight defaults to using quadTo |
| 56 | SkPath* nearParabola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 57 | nearParabola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 58 | nearParabola->conicTo(0, 100, 100, 100, 0.999f); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 59 | } |
| 60 | { |
| 61 | SkPath* thinEllipse = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 62 | thinEllipse->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 63 | thinEllipse->conicTo(100, 100, 5, 0, SK_ScalarHalf); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 64 | } |
| 65 | { |
| 66 | SkPath* veryThinEllipse = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 67 | veryThinEllipse->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 68 | veryThinEllipse->conicTo(100, 100, 1, 0, SK_ScalarHalf); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 69 | } |
| 70 | { |
| 71 | SkPath* closedEllipse = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 72 | closedEllipse->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 73 | closedEllipse->conicTo(100, 100, 0, 0, SK_ScalarHalf); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 74 | } |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 75 | { |
| 76 | const SkScalar w = SkScalarSqrt(2)/2; |
| 77 | fGiantCircle.moveTo(2.1e+11f, -1.05e+11f); |
| 78 | fGiantCircle.conicTo(2.1e+11f, 0, 1.05e+11f, 0, w); |
| 79 | fGiantCircle.conicTo(0, 0, 0, -1.05e+11f, w); |
| 80 | fGiantCircle.conicTo(0, -2.1e+11f, 1.05e+11f, -2.1e+11f, w); |
| 81 | fGiantCircle.conicTo(2.1e+11f, -2.1e+11f, 2.1e+11f, -1.05e+11f, w); |
| 82 | |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void drawGiantCircle(SkCanvas* canvas) { |
| 87 | SkPaint paint; |
| 88 | canvas->drawPath(fGiantCircle, paint); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 89 | } |
| 90 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 91 | void onDraw(SkCanvas* canvas) override { |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 92 | const SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 93 | |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 94 | const SkScalar margin = 15; |
| 95 | canvas->translate(margin, margin); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 96 | |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 97 | SkPaint paint; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 98 | for (int p = 0; p < fPaths.count(); ++p) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 99 | canvas->save(); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 100 | for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 101 | paint.setARGB(kAlphaValue[a], 0, 0, 0); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 102 | for (int aa = 0; aa < 2; ++aa) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 103 | paint.setAntiAlias(SkToBool(aa)); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 104 | for (int fh = 0; fh < 2; ++fh) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 105 | paint.setStyle(fh ? SkPaint::kStroke_Style : SkPaint::kFill_Style); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 106 | |
| 107 | const SkRect& bounds = fPaths[p].getBounds(); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 108 | canvas->save(); |
| 109 | canvas->translate(-bounds.fLeft, -bounds.fTop); |
| 110 | canvas->drawPath(fPaths[p], paint); |
| 111 | canvas->restore(); |
| 112 | |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 113 | canvas->translate(110, 0); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 117 | canvas->restore(); |
| 118 | canvas->translate(0, 110); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 119 | } |
| 120 | canvas->restore(); |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 121 | |
| 122 | this->drawGiantCircle(canvas); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 123 | } |
| 124 | |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 125 | private: |
| 126 | SkTArray<SkPath> fPaths; |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 127 | SkPath fGiantCircle; |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 128 | typedef skiagm::GM INHERITED; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 129 | }; |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 130 | DEF_GM( return SkNEW(ConicPathsGM); ) |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 131 | |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |