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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkPoint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkScalar.h" |
| 16 | #include "include/core/SkSize.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTypes.h" |
| 19 | #include "include/private/SkFloatBits.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/private/SkTArray.h" |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 21 | |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 22 | class ConicPathsGM : public skiagm::GM { |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 23 | protected: |
| 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | SkString onShortName() override { |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 26 | return SkString("conicpaths"); |
| 27 | } |
| 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkISize onISize() override { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 30 | return SkISize::Make(920, 960); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | void onOnceBeforeDraw() override { |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 34 | { |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 35 | const SkScalar w = SkScalarSqrt(2)/2; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 36 | SkPath* conicCirlce = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 37 | conicCirlce->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 38 | conicCirlce->conicTo(0, 50, 50, 50, w); |
| 39 | conicCirlce->rConicTo(50, 0, 50, -50, w); |
| 40 | conicCirlce->rConicTo(0, -50, -50, -50, w); |
| 41 | conicCirlce->rConicTo(-50, 0, -50, 50, w); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 42 | |
| 43 | } |
| 44 | { |
| 45 | SkPath* hyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 46 | hyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 47 | hyperbola->conicTo(0, 100, 100, 100, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 48 | } |
| 49 | { |
| 50 | SkPath* thinHyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 51 | thinHyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 52 | thinHyperbola->conicTo(100, 100, 5, 0, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 53 | } |
| 54 | { |
| 55 | SkPath* veryThinHyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 56 | veryThinHyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 57 | veryThinHyperbola->conicTo(100, 100, 1, 0, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 58 | } |
| 59 | { |
| 60 | SkPath* closedHyperbola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 61 | closedHyperbola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 62 | closedHyperbola->conicTo(100, 100, 0, 0, 2); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 63 | } |
| 64 | { |
| 65 | // using 1 as weight defaults to using quadTo |
| 66 | SkPath* nearParabola = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 67 | nearParabola->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 68 | nearParabola->conicTo(0, 100, 100, 100, 0.999f); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 69 | } |
| 70 | { |
| 71 | SkPath* thinEllipse = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 72 | thinEllipse->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 73 | thinEllipse->conicTo(100, 100, 5, 0, SK_ScalarHalf); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 74 | } |
| 75 | { |
| 76 | SkPath* veryThinEllipse = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 77 | veryThinEllipse->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 78 | veryThinEllipse->conicTo(100, 100, 1, 0, SK_ScalarHalf); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 79 | } |
| 80 | { |
| 81 | SkPath* closedEllipse = &fPaths.push_back(); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 82 | closedEllipse->moveTo(0, 0); |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 83 | closedEllipse->conicTo(100, 100, 0, 0, SK_ScalarHalf); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 84 | } |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 85 | { |
| 86 | const SkScalar w = SkScalarSqrt(2)/2; |
| 87 | fGiantCircle.moveTo(2.1e+11f, -1.05e+11f); |
| 88 | fGiantCircle.conicTo(2.1e+11f, 0, 1.05e+11f, 0, w); |
| 89 | fGiantCircle.conicTo(0, 0, 0, -1.05e+11f, w); |
| 90 | fGiantCircle.conicTo(0, -2.1e+11f, 1.05e+11f, -2.1e+11f, w); |
| 91 | fGiantCircle.conicTo(2.1e+11f, -2.1e+11f, 2.1e+11f, -1.05e+11f, w); |
| 92 | |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void drawGiantCircle(SkCanvas* canvas) { |
| 97 | SkPaint paint; |
| 98 | canvas->drawPath(fGiantCircle, paint); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 99 | } |
| 100 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 101 | void onDraw(SkCanvas* canvas) override { |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 102 | const SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 103 | |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 104 | const SkScalar margin = 15; |
| 105 | canvas->translate(margin, margin); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 106 | |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 107 | SkPaint paint; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 108 | for (int p = 0; p < fPaths.count(); ++p) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 109 | canvas->save(); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 110 | for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 111 | paint.setARGB(kAlphaValue[a], 0, 0, 0); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 112 | for (int aa = 0; aa < 2; ++aa) { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 113 | paint.setAntiAlias(SkToBool(aa)); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 114 | for (int fh = 0; fh < 2; ++fh) { |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 115 | paint.setStroke(fh != 0); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 116 | |
| 117 | const SkRect& bounds = fPaths[p].getBounds(); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 118 | canvas->save(); |
| 119 | canvas->translate(-bounds.fLeft, -bounds.fTop); |
| 120 | canvas->drawPath(fPaths[p], paint); |
| 121 | canvas->restore(); |
| 122 | |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 123 | canvas->translate(110, 0); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 127 | canvas->restore(); |
| 128 | canvas->translate(0, 110); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 129 | } |
| 130 | canvas->restore(); |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 131 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 132 | this->drawGiantCircle(canvas); |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 133 | } |
| 134 | |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 135 | private: |
| 136 | SkTArray<SkPath> fPaths; |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 137 | SkPath fGiantCircle; |
reed | d1bd1d7 | 2014-12-31 20:07:01 -0800 | [diff] [blame] | 138 | typedef skiagm::GM INHERITED; |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 139 | }; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 140 | DEF_GM(return new ConicPathsGM;) |
egdaniel@google.com | def9f6e | 2013-06-20 16:54:31 +0000 | [diff] [blame] | 141 | |
| 142 | ////////////////////////////////////////////////////////////////////////////// |
| 143 | |
caryclark | 2b39ffc | 2016-01-20 07:46:05 -0800 | [diff] [blame] | 144 | /* arc should be on top of circle */ |
| 145 | DEF_SIMPLE_GM(arccirclegap, canvas, 250, 250) { |
| 146 | canvas->translate(50, 100); |
| 147 | SkPoint c = { 1052.5390625f, 506.8760978034711f }; |
| 148 | SkScalar radius = 1096.702150363923f; |
| 149 | SkPaint paint; |
| 150 | paint.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 151 | paint.setStroke(true); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 152 | canvas->drawCircle(c, radius, paint); |
caryclark | 2b39ffc | 2016-01-20 07:46:05 -0800 | [diff] [blame] | 153 | SkPath path; |
| 154 | path.moveTo(288.88884710654133f, -280.26680862609f); |
| 155 | path.arcTo(0, 0, -39.00216443306411f, 400.6058925796476f, radius); |
| 156 | paint.setColor(0xff007f00); |
| 157 | canvas->drawPath(path, paint); |
| 158 | } |
caryclark | 531191f | 2016-08-24 11:59:30 -0700 | [diff] [blame] | 159 | |
Jim Van Verth | 6750e91 | 2016-12-19 14:45:19 -0500 | [diff] [blame] | 160 | /* circle should be antialiased */ |
| 161 | DEF_SIMPLE_GM(largecircle, canvas, 250, 250) { |
| 162 | canvas->translate(50, 100); |
| 163 | SkPoint c = { 1052.5390625f, 506.8760978034711f }; |
| 164 | SkScalar radius = 1096.702150363923f; |
| 165 | SkPaint paint; |
| 166 | paint.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 167 | paint.setStroke(true); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 168 | canvas->drawCircle(c, radius, paint); |
Jim Van Verth | 6750e91 | 2016-12-19 14:45:19 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 171 | /* ovals should not be blurry */ |
| 172 | DEF_SIMPLE_GM(largeovals, canvas, 250, 250) { |
| 173 | // Test EllipseOp |
| 174 | SkRect r = SkRect::MakeXYWH(-520, -520, 5000, 4000); |
| 175 | SkPaint paint; |
| 176 | paint.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 177 | paint.setStroke(true); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 178 | paint.setStrokeWidth(100); |
| 179 | canvas->drawOval(r, paint); |
| 180 | r.offset(-15, -15); |
| 181 | paint.setColor(SK_ColorDKGRAY); |
| 182 | // we use stroke and fill to avoid falling into the SimpleFill path |
| 183 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 184 | paint.setStrokeWidth(1); |
| 185 | canvas->drawOval(r, paint); |
| 186 | |
| 187 | // Test DIEllipseOp |
| 188 | canvas->rotate(1.0f); |
| 189 | r.offset(55, 55); |
| 190 | paint.setColor(SK_ColorGRAY); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 191 | paint.setStroke(true); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 192 | paint.setStrokeWidth(100); |
| 193 | canvas->drawOval(r, paint); |
| 194 | r.offset(-15, -15); |
| 195 | paint.setColor(SK_ColorLTGRAY); |
| 196 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 197 | paint.setStrokeWidth(1); |
| 198 | canvas->drawOval(r, paint); |
| 199 | } |
| 200 | |
caryclark | 531191f | 2016-08-24 11:59:30 -0700 | [diff] [blame] | 201 | DEF_SIMPLE_GM(crbug_640176, canvas, 250, 250) { |
| 202 | SkPath path; |
| 203 | path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0 |
| 204 | path.lineTo(SkBits2Float(0x42cfd89a), SkBits2Float(0xc2700000)); // 103.923f, -60 |
| 205 | path.lineTo(SkBits2Float(0x42cfd899), SkBits2Float(0xc2700006)); // 103.923f, -60 |
| 206 | path.conicTo(SkBits2Float(0x42f00000), SkBits2Float(0xc2009d9c), |
| 207 | SkBits2Float(0x42f00001), SkBits2Float(0x00000000), |
| 208 | SkBits2Float(0x3f7746ea)); // 120, -32.1539f, 120, 0, 0.965926f |
| 209 | |
| 210 | SkPaint paint; |
| 211 | paint.setAntiAlias(true); |
| 212 | canvas->translate(125, 125); |
| 213 | canvas->drawPath(path, paint); |
| 214 | } |