| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
| 11 #include "SkDashPathEffect.h" | 11 #include "SkDashPathEffect.h" |
| 12 | 12 |
| 13 static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint, | 13 static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint, |
| 14 SkScalar finalX = SkIntToScalar(600)) { | 14 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkI
ntToScalar(0), |
| 15 SkScalar phase = SkIntToScalar(0)) { |
| 15 SkPaint p(paint); | 16 SkPaint p(paint); |
| 16 | 17 |
| 17 const SkScalar intervals[] = { | 18 const SkScalar intervals[] = { |
| 18 SkIntToScalar(on), | 19 SkIntToScalar(on), |
| 19 SkIntToScalar(off), | 20 SkIntToScalar(off), |
| 20 }; | 21 }; |
| 21 | 22 |
| 22 p.setPathEffect(SkDashPathEffect::Create(intervals, 2, 0))->unref(); | 23 p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref(); |
| 23 canvas->drawLine(0, 0, finalX, 0, p); | 24 canvas->drawLine(0, 0, finalX, finalY, p); |
| 24 } | 25 } |
| 25 | 26 |
| 26 // earlier bug stopped us from drawing very long single-segment dashes, because | 27 // earlier bug stopped us from drawing very long single-segment dashes, because |
| 27 // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is | 28 // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is |
| 28 // now fixes, so this giant dash should appear. | 29 // now fixes, so this giant dash should appear. |
| 29 static void show_giant_dash(SkCanvas* canvas) { | 30 static void show_giant_dash(SkCanvas* canvas) { |
| 30 SkPaint paint; | 31 SkPaint paint; |
| 31 | 32 |
| 32 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); | 33 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); |
| 33 } | 34 } |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 } | 227 } |
| 227 | 228 |
| 228 p.setAntiAlias(true); | 229 p.setAntiAlias(true); |
| 229 | 230 |
| 230 for (int x = 0; x < 100; x += 14*strokeWidth) { | 231 for (int x = 0; x < 100; x += 14*strokeWidth) { |
| 231 pts[0].set(SkIntToScalar(x), 0); | 232 pts[0].set(SkIntToScalar(x), 0); |
| 232 pts[1].set(SkIntToScalar(x), lineLength); | 233 pts[1].set(SkIntToScalar(x), lineLength); |
| 233 | 234 |
| 234 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); | 235 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 235 } | 236 } |
| 237 |
| 236 } | 238 } |
| 237 | 239 |
| 238 virtual void onDraw(SkCanvas* canvas) { | 240 virtual void onDraw(SkCanvas* canvas) { |
| 239 // 1on/1off 1x1 squares with phase of 0 - points fastpath | 241 // 1on/1off 1x1 squares with phase of 0 - points fastpath |
| 240 canvas->save(); | 242 canvas->save(); |
| 241 canvas->translate(2, 0); | 243 canvas->translate(2, 0); |
| 242 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); | 244 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); |
| 243 canvas->restore(); | 245 canvas->restore(); |
| 244 | 246 |
| 245 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partia
l squares) | 247 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partia
l squares) |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 SkIntToScalar(330)); | 308 SkIntToScalar(330)); |
| 307 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntTo
Scalar(3), 1, false); | 309 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntTo
Scalar(3), 1, false); |
| 308 canvas->restore(); | 310 canvas->restore(); |
| 309 } | 311 } |
| 310 } | 312 } |
| 311 | 313 |
| 312 }; | 314 }; |
| 313 | 315 |
| 314 ////////////////////////////////////////////////////////////////////////////// | 316 ////////////////////////////////////////////////////////////////////////////// |
| 315 | 317 |
| 318 class Dashing4GM : public skiagm::GM { |
| 319 public: |
| 320 Dashing4GM() {} |
| 321 |
| 322 protected: |
| 323 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 324 return kSkipTiled_Flag; |
| 325 } |
| 326 |
| 327 SkString onShortName() { |
| 328 return SkString("dashing4"); |
| 329 } |
| 330 |
| 331 SkISize onISize() { return skiagm::make_isize(640, 950); } |
| 332 |
| 333 virtual void onDraw(SkCanvas* canvas) { |
| 334 static const struct { |
| 335 int fOnInterval; |
| 336 int fOffInterval; |
| 337 } gData[] = { |
| 338 { 1, 1 }, |
| 339 { 4, 2 }, |
| 340 { 0, 4 }, // test for zero length on interval |
| 341 }; |
| 342 |
| 343 SkPaint paint; |
| 344 paint.setStyle(SkPaint::kStroke_Style); |
| 345 |
| 346 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 347 canvas->translate(0, SK_ScalarHalf); |
| 348 |
| 349 for (int width = 0; width <= 2; ++width) { |
| 350 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) { |
| 351 for (int aa = 0; aa <= 1; ++aa) { |
| 352 for (int cap = 0; cap <= 1; ++cap) { |
| 353 int w = width * width * width; |
| 354 paint.setAntiAlias(SkToBool(aa)); |
| 355 paint.setStrokeWidth(SkIntToScalar(w)); |
| 356 |
| 357 SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap) |
| 358 : paint.setStrokeCap(SkPaint::kRound_Cap); |
| 359 |
| 360 int scale = w ? w : 1; |
| 361 |
| 362 drawline(canvas, gData[data].fOnInterval * scale, |
| 363 gData[data].fOffInterval * scale, |
| 364 paint); |
| 365 canvas->translate(0, SkIntToScalar(20)); |
| 366 } |
| 367 } |
| 368 } |
| 369 } |
| 370 |
| 371 for (int aa = 0; aa <= 1; ++aa) { |
| 372 paint.setAntiAlias(SkToBool(aa)); |
| 373 paint.setStrokeWidth(8.f); |
| 374 paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 375 |
| 376 // Single dash element that is cut off at start and end |
| 377 drawline(canvas, 32.f, 16.f, paint, 20.f, 0, 5.f); |
| 378 canvas->translate(0, SkIntToScalar(20)); |
| 379 |
| 380 // Two dash elements where each one is cut off at beginning and end
respectively |
| 381 drawline(canvas, 32.f, 16.f, paint, 56.f, 0, 5.f); |
| 382 canvas->translate(0, SkIntToScalar(20)); |
| 383 |
| 384 // Many dash elements where first and last are cut off at beginning
and end respectively |
| 385 drawline(canvas, 32.f, 16.f, paint, 584.f, 0, 5.f); |
| 386 canvas->translate(0, SkIntToScalar(20)); |
| 387 |
| 388 // Diagonal dash line where src pnts are not axis aligned (as appose
d to being diagonal from |
| 389 // a canvas rotation) |
| 390 drawline(canvas, 32.f, 16.f, paint, 600.f, 30.f); |
| 391 canvas->translate(0, SkIntToScalar(20)); |
| 392 |
| 393 // Case where only the off interval exists on the line. Thus nothing
should be drawn |
| 394 drawline(canvas, 32.f, 16.f, paint, 8.f, 0.f, 40.f); |
| 395 canvas->translate(0, SkIntToScalar(20)); |
| 396 } |
| 397 } |
| 398 }; |
| 399 |
| 400 ////////////////////////////////////////////////////////////////////////////// |
| 401 |
| 316 static skiagm::GM* F0(void*) { return new DashingGM; } | 402 static skiagm::GM* F0(void*) { return new DashingGM; } |
| 317 static skiagm::GM* F1(void*) { return new Dashing2GM; } | 403 static skiagm::GM* F1(void*) { return new Dashing2GM; } |
| 318 static skiagm::GM* F2(void*) { return new Dashing3GM; } | 404 static skiagm::GM* F2(void*) { return new Dashing3GM; } |
| 405 static skiagm::GM* F3(void*) { return new Dashing4GM; } |
| 319 | 406 |
| 320 static skiagm::GMRegistry gR0(F0); | 407 static skiagm::GMRegistry gR0(F0); |
| 321 static skiagm::GMRegistry gR1(F1); | 408 static skiagm::GMRegistry gR1(F1); |
| 322 static skiagm::GMRegistry gR2(F2); | 409 static skiagm::GMRegistry gR2(F2); |
| 410 static skiagm::GMRegistry gR3(F3); |
| OLD | NEW |