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