robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [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 | |
| 8 | #include "gm.h" |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 9 | #include "SkPathPriv.h" |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 10 | |
| 11 | static void create_ngon(int n, SkPoint* pts, SkScalar width, SkScalar height) { |
| 12 | float angleStep = 360.0f / n, angle = 0.0f, sin, cos; |
| 13 | if ((n % 2) == 1) { |
| 14 | angle = angleStep/2.0f; |
| 15 | } |
| 16 | |
| 17 | for (int i = 0; i < n; ++i) { |
| 18 | sin = SkScalarSinCos(SkDegreesToRadians(angle), &cos); |
| 19 | pts[i].fX = -sin * width; |
| 20 | pts[i].fY = cos * height; |
| 21 | angle += angleStep; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | namespace skiagm { |
| 26 | |
| 27 | // This GM is intended to exercise Ganesh's handling of convex line-only |
| 28 | // paths |
| 29 | class ConvexLineOnlyPathsGM : public GM { |
| 30 | public: |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 31 | ConvexLineOnlyPathsGM(bool doStrokeAndFill) : fDoStrokeAndFill(doStrokeAndFill) { |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 32 | this->setBGColor(0xFFFFFFFF); |
| 33 | } |
| 34 | |
| 35 | protected: |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 36 | SkString onShortName() override { |
| 37 | if (fDoStrokeAndFill) { |
| 38 | return SkString("convex-lineonly-paths-stroke-and-fill"); |
| 39 | } |
| 40 | return SkString("convex-lineonly-paths"); |
| 41 | } |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 42 | SkISize onISize() override { return SkISize::Make(kGMWidth, kGMHeight); } |
| 43 | bool runAsBench() const override { return true; } |
| 44 | |
| 45 | static SkPath GetPath(int index, int offset, SkPath::Direction dir) { |
| 46 | // narrow rect |
| 47 | const SkPoint gPoints0[] = { |
| 48 | { -1.5f, -50.0f }, |
| 49 | { 1.5f, -50.0f }, |
| 50 | { 1.5f, 50.0f }, |
| 51 | { -1.5f, 50.0f } |
| 52 | }; |
| 53 | // narrow rect on an angle |
| 54 | const SkPoint gPoints1[] = { |
| 55 | { -50.0f, -49.0f }, |
| 56 | { -49.0f, -50.0f }, |
| 57 | { 50.0f, 49.0f }, |
| 58 | { 49.0f, 50.0f } |
| 59 | }; |
| 60 | // trap - narrow on top - wide on bottom |
| 61 | const SkPoint gPoints2[] = { |
| 62 | { -10.0f, -50.0f }, |
| 63 | { 10.0f, -50.0f }, |
| 64 | { 50.0f, 50.0f }, |
| 65 | { -50.0f, 50.0f } |
| 66 | }; |
| 67 | // wide skewed rect |
| 68 | const SkPoint gPoints3[] = { |
| 69 | { -50.0f, -50.0f }, |
| 70 | { 0.0f, -50.0f }, |
| 71 | { 50.0f, 50.0f }, |
| 72 | { 0.0f, 50.0f } |
| 73 | }; |
| 74 | // thin rect with colinear-ish lines |
| 75 | const SkPoint gPoints4[] = { |
| 76 | { -6.0f, -50.0f }, |
| 77 | { 4.0f, -50.0f }, |
| 78 | { 5.0f, -25.0f }, |
| 79 | { 6.0f, 0.0f }, |
| 80 | { 5.0f, 25.0f }, |
| 81 | { 4.0f, 50.0f }, |
| 82 | { -4.0f, 50.0f } |
| 83 | }; |
| 84 | // degenerate |
| 85 | const SkPoint gPoints5[] = { |
| 86 | { -0.025f, -0.025f }, |
| 87 | { 0.025f, -0.025f }, |
| 88 | { 0.025f, 0.025f }, |
| 89 | { -0.025f, 0.025f } |
| 90 | }; |
| 91 | // Triangle in which the first point should fuse with last |
| 92 | const SkPoint gPoints6[] = { |
| 93 | { -20.0f, -13.0f }, |
| 94 | { -20.0f, -13.05f }, |
| 95 | { 20.0f, -13.0f }, |
| 96 | { 20.0f, 27.0f } |
| 97 | }; |
| 98 | // thin rect with colinear lines |
| 99 | const SkPoint gPoints7[] = { |
| 100 | { -10.0f, -50.0f }, |
| 101 | { 10.0f, -50.0f }, |
| 102 | { 10.0f, -25.0f }, |
| 103 | { 10.0f, 0.0f }, |
| 104 | { 10.0f, 25.0f }, |
| 105 | { 10.0f, 50.0f }, |
| 106 | { -10.0f, 50.0f } |
| 107 | }; |
| 108 | // capped teardrop |
| 109 | const SkPoint gPoints8[] = { |
| 110 | { 50.00f, 50.00f }, |
| 111 | { 0.00f, 50.00f }, |
| 112 | { -15.45f, 47.55f }, |
| 113 | { -29.39f, 40.45f }, |
| 114 | { -40.45f, 29.39f }, |
| 115 | { -47.55f, 15.45f }, |
| 116 | { -50.00f, 0.00f }, |
| 117 | { -47.55f, -15.45f }, |
| 118 | { -40.45f, -29.39f }, |
| 119 | { -29.39f, -40.45f }, |
| 120 | { -15.45f, -47.55f }, |
| 121 | { 0.00f, -50.00f }, |
| 122 | { 50.00f, -50.00f } |
| 123 | }; |
| 124 | // teardrop |
| 125 | const SkPoint gPoints9[] = { |
| 126 | { 4.39f, 40.45f }, |
| 127 | { -9.55f, 47.55f }, |
| 128 | { -25.00f, 50.00f }, |
| 129 | { -40.45f, 47.55f }, |
| 130 | { -54.39f, 40.45f }, |
| 131 | { -65.45f, 29.39f }, |
| 132 | { -72.55f, 15.45f }, |
| 133 | { -75.00f, 0.00f }, |
| 134 | { -72.55f, -15.45f }, |
| 135 | { -65.45f, -29.39f }, |
| 136 | { -54.39f, -40.45f }, |
| 137 | { -40.45f, -47.55f }, |
| 138 | { -25.0f, -50.0f }, |
| 139 | { -9.55f, -47.55f }, |
| 140 | { 4.39f, -40.45f }, |
| 141 | { 75.00f, 0.00f } |
| 142 | }; |
| 143 | // clipped triangle |
| 144 | const SkPoint gPoints10[] = { |
| 145 | { -10.0f, -50.0f }, |
| 146 | { 10.0f, -50.0f }, |
| 147 | { 50.0f, 31.0f }, |
| 148 | { 40.0f, 50.0f }, |
| 149 | { -40.0f, 50.0f }, |
| 150 | { -50.0f, 31.0f }, |
| 151 | }; |
| 152 | |
| 153 | const SkPoint* gPoints[] = { |
| 154 | gPoints0, gPoints1, gPoints2, gPoints3, gPoints4, gPoints5, gPoints6, |
| 155 | gPoints7, gPoints8, gPoints9, gPoints10, |
| 156 | }; |
| 157 | |
| 158 | const size_t gSizes[] = { |
| 159 | SK_ARRAY_COUNT(gPoints0), |
| 160 | SK_ARRAY_COUNT(gPoints1), |
| 161 | SK_ARRAY_COUNT(gPoints2), |
| 162 | SK_ARRAY_COUNT(gPoints3), |
| 163 | SK_ARRAY_COUNT(gPoints4), |
| 164 | SK_ARRAY_COUNT(gPoints5), |
| 165 | SK_ARRAY_COUNT(gPoints6), |
| 166 | SK_ARRAY_COUNT(gPoints7), |
| 167 | SK_ARRAY_COUNT(gPoints8), |
| 168 | SK_ARRAY_COUNT(gPoints9), |
| 169 | SK_ARRAY_COUNT(gPoints10), |
| 170 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 171 | static_assert(SK_ARRAY_COUNT(gSizes) == SK_ARRAY_COUNT(gPoints), "array_mismatch"); |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 172 | |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 173 | std::unique_ptr<SkPoint[]> data(nullptr); |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 174 | const SkPoint* points; |
| 175 | int numPts; |
| 176 | if (index < (int) SK_ARRAY_COUNT(gPoints)) { |
| 177 | // manually specified |
| 178 | points = gPoints[index]; |
| 179 | numPts = (int) gSizes[index]; |
| 180 | } else { |
| 181 | // procedurally generated |
| 182 | SkScalar width = kMaxPathHeight/2; |
| 183 | SkScalar height = kMaxPathHeight/2; |
| 184 | switch (index-SK_ARRAY_COUNT(gPoints)) { |
| 185 | case 0: |
| 186 | numPts = 3; |
| 187 | break; |
| 188 | case 1: |
| 189 | numPts = 4; |
| 190 | break; |
| 191 | case 2: |
| 192 | numPts = 5; |
| 193 | break; |
| 194 | case 3: // squashed pentagon |
| 195 | numPts = 5; |
| 196 | width = kMaxPathHeight/5; |
| 197 | break; |
| 198 | case 4: |
| 199 | numPts = 6; |
| 200 | break; |
| 201 | case 5: |
| 202 | numPts = 8; |
| 203 | break; |
| 204 | case 6: // squashed octogon |
| 205 | numPts = 8; |
| 206 | width = kMaxPathHeight/5; |
| 207 | break; |
| 208 | case 7: |
| 209 | numPts = 20; |
| 210 | break; |
| 211 | case 8: |
| 212 | numPts = 100; |
| 213 | break; |
| 214 | default: |
| 215 | numPts = 3; |
| 216 | break; |
| 217 | } |
| 218 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 219 | data.reset(new SkPoint[numPts]); |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 220 | |
| 221 | create_ngon(numPts, data.get(), width, height); |
| 222 | points = data.get(); |
| 223 | } |
| 224 | |
| 225 | SkPath path; |
| 226 | |
| 227 | if (SkPath::kCW_Direction == dir) { |
| 228 | path.moveTo(points[0]); |
| 229 | for (int i = 1; i < numPts; ++i) { |
| 230 | path.lineTo(points[i]); |
| 231 | } |
| 232 | } else { |
| 233 | path.moveTo(points[numPts-1]); |
| 234 | for (int i = numPts-2; i >= 0; --i) { |
| 235 | path.lineTo(points[i]); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | path.close(); |
| 240 | #ifdef SK_DEBUG |
| 241 | // Each path this method returns should be convex, only composed of |
| 242 | // lines, wound the right direction, and short enough to fit in one |
| 243 | // of the GMs rows. |
| 244 | SkASSERT(path.isConvex()); |
| 245 | SkASSERT(SkPath::kLine_SegmentMask == path.getSegmentMasks()); |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 246 | SkPathPriv::FirstDirection actualDir; |
| 247 | SkASSERT(SkPathPriv::CheapComputeFirstDirection(path, &actualDir)); |
| 248 | SkASSERT(SkPathPriv::AsFirstDirection(dir) == actualDir); |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 249 | SkRect bounds = path.getBounds(); |
| 250 | SkASSERT(SkScalarNearlyEqual(bounds.centerX(), 0.0f)); |
| 251 | SkASSERT(bounds.height() <= kMaxPathHeight); |
| 252 | #endif |
| 253 | return path; |
| 254 | } |
| 255 | |
| 256 | // Draw a single path several times, shrinking it, flipping its direction |
| 257 | // and changing its start vertex each time. |
robertphillips | 2a97462 | 2015-05-08 07:08:13 -0700 | [diff] [blame] | 258 | void drawPath(SkCanvas* canvas, int index, SkPoint* offset) { |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 259 | |
| 260 | SkPoint center; |
| 261 | { |
| 262 | SkPath path = GetPath(index, 0, SkPath::kCW_Direction); |
robertphillips | 2a97462 | 2015-05-08 07:08:13 -0700 | [diff] [blame] | 263 | if (offset->fX+path.getBounds().width() > kGMWidth) { |
| 264 | offset->fX = 0; |
| 265 | offset->fY += kMaxPathHeight; |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 266 | if (fDoStrokeAndFill) { |
| 267 | offset->fX += kStrokeWidth / 2.0f; |
| 268 | offset->fY += kStrokeWidth / 2.0f; |
| 269 | } |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 270 | } |
robertphillips | 2a97462 | 2015-05-08 07:08:13 -0700 | [diff] [blame] | 271 | center = { offset->fX + SkScalarHalf(path.getBounds().width()), offset->fY}; |
| 272 | offset->fX += path.getBounds().width(); |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 273 | if (fDoStrokeAndFill) { |
| 274 | offset->fX += kStrokeWidth; |
| 275 | } |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | const SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 279 | const SkPath::Direction dirs[2] = { SkPath::kCW_Direction, SkPath::kCCW_Direction }; |
| 280 | const float scales[] = { 1.0f, 0.75f, 0.5f, 0.25f, 0.1f, 0.01f, 0.001f }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 281 | const SkPaint::Join joins[3] = { SkPaint::kRound_Join, |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 282 | SkPaint::kBevel_Join, |
| 283 | SkPaint::kMiter_Join }; |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 284 | |
| 285 | SkPaint paint; |
| 286 | paint.setAntiAlias(true); |
| 287 | |
| 288 | for (size_t i = 0; i < SK_ARRAY_COUNT(scales); ++i) { |
| 289 | SkPath path = GetPath(index, (int) i, dirs[i%2]); |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 290 | if (fDoStrokeAndFill) { |
| 291 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 292 | paint.setStrokeJoin(joins[i%3]); |
| 293 | paint.setStrokeWidth(SkIntToScalar(kStrokeWidth)); |
| 294 | } |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 295 | |
| 296 | canvas->save(); |
| 297 | canvas->translate(center.fX, center.fY); |
| 298 | canvas->scale(scales[i], scales[i]); |
| 299 | paint.setColor(colors[i%2]); |
| 300 | canvas->drawPath(path, paint); |
| 301 | canvas->restore(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 2a97462 | 2015-05-08 07:08:13 -0700 | [diff] [blame] | 306 | // the right edge of the last drawn path |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 307 | SkPoint offset = { 0, SkScalarHalf(kMaxPathHeight) }; |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 308 | if (fDoStrokeAndFill) { |
| 309 | offset.fX += kStrokeWidth / 2.0f; |
| 310 | offset.fY += kStrokeWidth / 2.0f; |
| 311 | } |
robertphillips | 2a97462 | 2015-05-08 07:08:13 -0700 | [diff] [blame] | 312 | |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 313 | for (int i = 0; i < kNumPaths; ++i) { |
robertphillips | 2a97462 | 2015-05-08 07:08:13 -0700 | [diff] [blame] | 314 | this->drawPath(canvas, i, &offset); |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Repro for crbug.com/472723 (Missing AA on portions of graphic with GPU rasterization) |
| 318 | { |
| 319 | canvas->translate(356.0f, 50.0f); |
| 320 | |
| 321 | SkPaint p; |
| 322 | p.setAntiAlias(true); |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 323 | if (fDoStrokeAndFill) { |
| 324 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 325 | p.setStrokeJoin(SkPaint::kMiter_Join); |
| 326 | p.setStrokeWidth(SkIntToScalar(kStrokeWidth)); |
| 327 | } |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 328 | |
| 329 | SkPath p1; |
| 330 | p1.moveTo(60.8522949f, 364.671021f); |
| 331 | p1.lineTo(59.4380493f, 364.671021f); |
| 332 | p1.lineTo(385.414276f, 690.647217f); |
| 333 | p1.lineTo(386.121399f, 689.940125f); |
| 334 | canvas->drawPath(p1, p); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 339 | static constexpr int kStrokeWidth = 10; |
| 340 | static constexpr int kNumPaths = 20; |
| 341 | static constexpr int kMaxPathHeight = 100; |
| 342 | static constexpr int kGMWidth = 512; |
| 343 | static constexpr int kGMHeight = 512; |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 344 | |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 345 | bool fDoStrokeAndFill; |
| 346 | |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 347 | typedef GM INHERITED; |
| 348 | }; |
| 349 | |
| 350 | ////////////////////////////////////////////////////////////////////////////// |
| 351 | |
robertphillips | ad23446 | 2016-08-26 05:30:19 -0700 | [diff] [blame] | 352 | DEF_GM(return new ConvexLineOnlyPathsGM(false);) |
| 353 | DEF_GM(return new ConvexLineOnlyPathsGM(true);) |
robertphillips | 7272935 | 2015-04-28 07:42:04 -0700 | [diff] [blame] | 354 | } |