jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | 7fde8e1 | 2019-05-01 17:28:53 -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 | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/core/SkSize.h" |
| 15 | #include "include/core/SkString.h" |
| 16 | #include "include/core/SkTypes.h" |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 17 | |
| 18 | typedef SkScalar (*MakePathProc)(SkPath*); |
| 19 | |
| 20 | static SkScalar make_triangle(SkPath* path) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 21 | constexpr int gCoord[] = { |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 22 | 10, 20, 15, 5, 30, 30 |
| 23 | }; |
| 24 | path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1])); |
| 25 | path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3])); |
| 26 | path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5])); |
| 27 | path->close(); |
| 28 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
| 29 | return SkIntToScalar(30); |
| 30 | } |
| 31 | |
| 32 | static SkScalar make_rect(SkPath* path) { |
| 33 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 34 | SkIntToScalar(30), SkIntToScalar(30) }; |
| 35 | path->addRect(r); |
| 36 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
| 37 | return SkIntToScalar(30); |
| 38 | } |
| 39 | |
| 40 | static SkScalar make_oval(SkPath* path) { |
| 41 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 42 | SkIntToScalar(30), SkIntToScalar(30) }; |
| 43 | path->addOval(r); |
| 44 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
| 45 | return SkIntToScalar(30); |
| 46 | } |
| 47 | |
| 48 | static SkScalar make_star(SkPath* path, int n) { |
| 49 | const SkScalar c = SkIntToScalar(45); |
| 50 | const SkScalar r = SkIntToScalar(20); |
| 51 | |
| 52 | SkScalar rad = -SK_ScalarPI / 2; |
| 53 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
| 54 | |
| 55 | path->moveTo(c, c - r); |
| 56 | for (int i = 1; i < n; i++) { |
| 57 | rad += drad; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 58 | path->lineTo(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r); |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 59 | } |
| 60 | path->close(); |
| 61 | return r * 2 * 6 / 5; |
| 62 | } |
| 63 | |
| 64 | static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); } |
| 65 | static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); } |
| 66 | |
| 67 | static SkScalar make_three_line(SkPath* path) { |
| 68 | static SkScalar xOffset = 34.f; |
| 69 | static SkScalar yOffset = 50.f; |
| 70 | path->moveTo(-32.5f + xOffset, 0.0f + yOffset); |
| 71 | path->lineTo(32.5f + xOffset, 0.0f + yOffset); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 72 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 73 | path->moveTo(-32.5f + xOffset, 19 + yOffset); |
| 74 | path->lineTo(32.5f + xOffset, 19 + yOffset); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 75 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 76 | path->moveTo(-32.5f + xOffset, -19 + yOffset); |
| 77 | path->lineTo(32.5f + xOffset, -19 + yOffset); |
| 78 | path->lineTo(-32.5f + xOffset, -19 + yOffset); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 79 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 80 | path->close(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 81 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 82 | return SkIntToScalar(70); |
| 83 | } |
| 84 | |
| 85 | static SkScalar make_arrow(SkPath* path) { |
| 86 | static SkScalar xOffset = 34.f; |
| 87 | static SkScalar yOffset = 40.f; |
| 88 | path->moveTo(-26.f + xOffset, 0.0f + yOffset); |
| 89 | path->lineTo(26.f + xOffset, 0.0f + yOffset); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 90 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 91 | path->moveTo(-28.f + xOffset, -2.4748745f + yOffset); |
| 92 | path->lineTo(0 + xOffset, 25.525126f + yOffset); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 93 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 94 | path->moveTo(-28.f + xOffset, 2.4748745f + yOffset); |
| 95 | path->lineTo(0 + xOffset, -25.525126f + yOffset); |
| 96 | path->lineTo(-28.f + xOffset, 2.4748745f + yOffset); |
| 97 | |
| 98 | path->close(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 99 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 100 | return SkIntToScalar(70); |
| 101 | } |
| 102 | |
| 103 | static SkScalar make_curve(SkPath* path) { |
| 104 | static SkScalar xOffset = -382.f; |
| 105 | static SkScalar yOffset = -50.f; |
| 106 | path->moveTo(491 + xOffset, 56 + yOffset); |
| 107 | path->conicTo(435.93292f + xOffset, 56.000031f + yOffset, |
| 108 | 382.61078f + xOffset, 69.752716f + yOffset, |
| 109 | 0.9920463f); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 110 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 111 | return SkIntToScalar(40); |
| 112 | } |
| 113 | |
| 114 | static SkScalar make_battery(SkPath* path) { |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 115 | static SkScalar xOffset = 5.0f; |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 116 | |
| 117 | path->moveTo(24.67f + xOffset, 0.33000004f); |
| 118 | path->lineTo(8.3299999f + xOffset, 0.33000004f); |
| 119 | path->lineTo(8.3299999f + xOffset, 5.3299999f); |
| 120 | path->lineTo(0.33000004f + xOffset, 5.3299999f); |
| 121 | path->lineTo(0.33000004f + xOffset, 50.669998f); |
| 122 | path->lineTo(32.669998f + xOffset, 50.669998f); |
| 123 | path->lineTo(32.669998f + xOffset, 5.3299999f); |
| 124 | path->lineTo(24.67f + xOffset, 5.3299999f); |
| 125 | path->lineTo(24.67f + xOffset, 0.33000004f); |
| 126 | path->close(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 127 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 128 | path->moveTo(25.727224f + xOffset, 12.886665f); |
| 129 | path->lineTo(10.907918f + xOffset, 12.886665f); |
| 130 | path->lineTo(7.5166659f + xOffset, 28.683645f); |
| 131 | path->lineTo(14.810181f + xOffset, 28.683645f); |
| 132 | path->lineTo(7.7024879f + xOffset, 46.135998f); |
| 133 | path->lineTo(28.049999f + xOffset, 25.136419f); |
| 134 | path->lineTo(16.854223f + xOffset, 25.136419f); |
| 135 | path->lineTo(25.727224f + xOffset, 12.886665f); |
| 136 | path->close(); |
jvanverth | 76f160c | 2015-11-19 07:46:07 -0800 | [diff] [blame] | 137 | return SkIntToScalar(50); |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static SkScalar make_battery2(SkPath* path) { |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 141 | static SkScalar xOffset = 225.625f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 142 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 143 | path->moveTo(32.669998f + xOffset, 9.8640003f); |
| 144 | path->lineTo(0.33000004f + xOffset, 9.8640003f); |
| 145 | path->lineTo(0.33000004f + xOffset, 50.669998f); |
| 146 | path->lineTo(32.669998f + xOffset, 50.669998f); |
| 147 | path->lineTo(32.669998f + xOffset, 9.8640003f); |
| 148 | path->close(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 149 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 150 | path->moveTo(10.907918f + xOffset, 12.886665f); |
| 151 | path->lineTo(25.727224f + xOffset, 12.886665f); |
| 152 | path->lineTo(16.854223f + xOffset, 25.136419f); |
| 153 | path->lineTo(28.049999f + xOffset, 25.136419f); |
| 154 | path->lineTo(7.7024879f + xOffset, 46.135998f); |
| 155 | path->lineTo(14.810181f + xOffset, 28.683645f); |
| 156 | path->lineTo(7.5166659f + xOffset, 28.683645f); |
| 157 | path->lineTo(10.907918f + xOffset, 12.886665f); |
| 158 | path->close(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 159 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 160 | return SkIntToScalar(70); |
| 161 | } |
| 162 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 163 | constexpr MakePathProc gProcs[] = { |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 164 | make_triangle, |
| 165 | make_rect, |
| 166 | make_oval, |
| 167 | make_star_5, |
| 168 | make_star_13, |
| 169 | make_three_line, |
| 170 | make_arrow, |
| 171 | make_curve, |
| 172 | make_battery, |
| 173 | make_battery2 |
| 174 | }; |
| 175 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 176 | constexpr SkScalar gWidths[] = { |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 177 | 2.0f, |
| 178 | 3.0f, |
| 179 | 4.0f, |
| 180 | 5.0f, |
| 181 | 6.0f, |
| 182 | 7.0f, |
| 183 | 7.0f, |
| 184 | 14.0f, |
| 185 | 0.0f, |
| 186 | 0.0f, |
| 187 | }; |
| 188 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 189 | constexpr SkScalar gMiters[] = { |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 190 | 2.0f, |
| 191 | 3.0f, |
| 192 | 3.0f, |
| 193 | 3.0f, |
| 194 | 4.0f, |
| 195 | 4.0f, |
| 196 | 4.0f, |
| 197 | 4.0f, |
| 198 | 4.0f, |
| 199 | 4.0f, |
| 200 | }; |
| 201 | |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 202 | constexpr SkScalar gXTranslate[] = { |
| 203 | 0.0f, |
| 204 | 0.0f, |
| 205 | 0.0f, |
| 206 | 0.0f, |
| 207 | 0.0f, |
| 208 | 0.0f, |
| 209 | 0.0f, |
| 210 | 0.0f, |
| 211 | -220.625f, |
| 212 | 0.0f, |
| 213 | }; |
| 214 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 215 | #define N SK_ARRAY_COUNT(gProcs) |
| 216 | |
| 217 | // This GM tests out drawing small paths (i.e., for Ganesh, using the Distance |
| 218 | // Field path renderer) which are filled, stroked and filledAndStroked. In |
| 219 | // particular this ensures that any cache keys in use include the stroking |
| 220 | // parameters. |
| 221 | class SmallPathsGM : public skiagm::GM { |
| 222 | SkPath fPath[N]; |
| 223 | SkScalar fDY[N]; |
| 224 | protected: |
| 225 | void onOnceBeforeDraw() override { |
| 226 | for (size_t i = 0; i < N; i++) { |
| 227 | fDY[i] = gProcs[i](&fPath[i]); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | SkString onShortName() override { |
| 232 | return SkString("smallpaths"); |
| 233 | } |
| 234 | |
| 235 | SkISize onISize() override { |
| 236 | return SkISize::Make(640, 480); |
| 237 | } |
| 238 | |
| 239 | void onDraw(SkCanvas* canvas) override { |
| 240 | SkPaint paint; |
| 241 | paint.setAntiAlias(true); |
| 242 | |
| 243 | // first column: filled paths |
| 244 | canvas->save(); |
| 245 | for (size_t i = 0; i < N; i++) { |
| 246 | canvas->drawPath(fPath[i], paint); |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 247 | canvas->translate(gXTranslate[i], fDY[i]); |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 248 | } |
| 249 | canvas->restore(); |
| 250 | canvas->translate(SkIntToScalar(120), SkIntToScalar(0)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 251 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 252 | // second column: stroked paths |
| 253 | canvas->save(); |
| 254 | paint.setStyle(SkPaint::kStroke_Style); |
| 255 | paint.setStrokeCap(SkPaint::kButt_Cap); |
| 256 | for (size_t i = 0; i < N; i++) { |
| 257 | paint.setStrokeWidth(gWidths[i]); |
| 258 | paint.setStrokeMiter(gMiters[i]); |
| 259 | canvas->drawPath(fPath[i], paint); |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 260 | canvas->translate(gXTranslate[i], fDY[i]); |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 261 | } |
| 262 | canvas->restore(); |
| 263 | canvas->translate(SkIntToScalar(120), SkIntToScalar(0)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 264 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 265 | // third column: stroked paths with different widths |
| 266 | canvas->save(); |
| 267 | paint.setStyle(SkPaint::kStroke_Style); |
| 268 | paint.setStrokeCap(SkPaint::kButt_Cap); |
| 269 | for (size_t i = 0; i < N; i++) { |
| 270 | paint.setStrokeWidth(gWidths[i] + 2.0f); |
| 271 | paint.setStrokeMiter(gMiters[i]); |
| 272 | canvas->drawPath(fPath[i], paint); |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 273 | canvas->translate(gXTranslate[i], fDY[i]); |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 274 | } |
| 275 | canvas->restore(); |
| 276 | canvas->translate(SkIntToScalar(120), SkIntToScalar(0)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 277 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 278 | // fourth column: stroked and filled paths |
| 279 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 280 | paint.setStrokeCap(SkPaint::kButt_Cap); |
| 281 | for (size_t i = 0; i < N; i++) { |
| 282 | paint.setStrokeWidth(gWidths[i]); |
| 283 | paint.setStrokeMiter(gMiters[i]); |
| 284 | canvas->drawPath(fPath[i], paint); |
Jim Van Verth | 90432c0 | 2018-03-09 15:35:00 -0500 | [diff] [blame] | 285 | canvas->translate(gXTranslate[i], fDY[i]); |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 286 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 287 | |
jvanverth | 8450cc3 | 2015-11-18 13:40:15 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | private: |
| 291 | typedef skiagm::GM INHERITED; |
| 292 | }; |
| 293 | |
| 294 | DEF_GM(return new SmallPathsGM;) |