DashPathEffectTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2014 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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkImageInfo.h"
  9. #include "include/core/SkMatrix.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkPathEffect.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkStrokeRec.h"
  18. #include "include/core/SkSurface.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/effects/SkDashPathEffect.h"
  21. #include "tests/Test.h"
  22. // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
  23. // the effect is nonsense. Here we test that it fails when passed nonsense parameters.
  24. DEF_TEST(DashPathEffectTest_crbug_348821, r) {
  25. SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug.
  26. const int count = 2;
  27. SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect.
  28. sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
  29. REPORTER_ASSERT(r, dash == nullptr);
  30. }
  31. // Test out the asPoint culling behavior.
  32. DEF_TEST(DashPathEffectTest_asPoints, r) {
  33. const SkScalar intervals[] = { 1.0f, 1.0f };
  34. const int count = 2;
  35. sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
  36. SkRect cull = SkRect::MakeWH(1.0f, 1.0f);
  37. const struct {
  38. SkPoint fPts[2];
  39. bool fExpectedResult;
  40. } testCases[] = {
  41. { { { -5.0f, 0.5f }, { -4.0f, 0.5f } }, false }, // off to the left
  42. { { { 4.0f, 0.5f }, { 5.0f, 0.5f } }, false }, // off to the right
  43. { { { 0.5f, 4.0f }, { 0.5f, 5.0f } }, false }, // off the bottom
  44. { { { 0.5f, -5.0f }, { 0.5f, -4.0f } }, false }, // off the top
  45. { { { 0.5f, 0.2f }, { 0.5f, 0.8f } }, true }, // entirely inside vertical
  46. { { { 0.2f, 0.5f }, { 0.8f, 0.5f } }, true }, // entirely inside horizontal
  47. { { { 0.5f, -5.0f }, { 0.5f, 5.0f } }, true }, // straddles both sides vertically
  48. { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both sides horizontally
  49. { { { 0.5f, -5.0f }, { 0.5f, 0.5f } }, true }, // straddles top
  50. { { { 0.5f, 5.0f }, { 0.5f, 0.5f } }, true }, // straddles bottom
  51. { { { -5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles left
  52. { { { 5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles right
  53. { { { 0.5f, 0.5f }, { 0.5f, 0.5f } }, false }, // zero length
  54. };
  55. SkPaint paint;
  56. paint.setStyle(SkPaint::kStroke_Style);
  57. paint.setStrokeWidth(1.0f);
  58. SkStrokeRec rec(paint);
  59. static const int kNumMats = 3;
  60. SkMatrix mats[kNumMats];
  61. mats[0].reset();
  62. mats[1].setRotate(90, 0.5f, 0.5f);
  63. mats[2].setTranslate(10.0f, 10.0f);
  64. for (int i = 0; i < kNumMats; ++i) {
  65. for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) {
  66. for (int k = 0; k < 2; ++k) { // exercise alternating endpoints
  67. SkPathEffect::PointData results;
  68. SkPath src;
  69. src.moveTo(testCases[j].fPts[k]);
  70. src.lineTo(testCases[j].fPts[(k+1)%2]);
  71. bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull);
  72. if (i < 2) {
  73. REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult);
  74. } else {
  75. // On the third pass all the lines should be outside the translated cull rect
  76. REPORTER_ASSERT(r, !actualResult);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. DEF_TEST(DashPath_bug4871, r) {
  83. SkPath path;
  84. path.moveTo(30, 24);
  85. path.cubicTo(30.002f, 24, 30, 24, 30, 24);
  86. path.close();
  87. SkScalar intervals[2] = { 1, 1 };
  88. sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
  89. SkPaint paint;
  90. paint.setStyle(SkPaint::kStroke_Style);
  91. paint.setPathEffect(dash);
  92. SkPath fill;
  93. paint.getFillPath(path, &fill);
  94. }
  95. // Verify that long lines with many dashes don't cause overflows/OOMs.
  96. DEF_TEST(DashPathEffectTest_asPoints_limit, r) {
  97. sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(256, 256)));
  98. SkCanvas* canvas = surface->getCanvas();
  99. SkPaint p;
  100. p.setStyle(SkPaint::kStroke_Style);
  101. // force the bounds to outset by a large amount
  102. p.setStrokeWidth(5.0e10f);
  103. const SkScalar intervals[] = { 1, 1 };
  104. p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
  105. canvas->drawLine(1, 1, 1, 5.0e10f, p);
  106. }
  107. // This used to cause SkDashImpl to walk off the end of the intervals array, due to underflow
  108. // trying to substract a smal value from a large one in floats.
  109. DEF_TEST(DashCrazy_crbug_875494, r) {
  110. SkScalar vals[] = { 98, 94, 2888458849.f, 227, 0, 197 };
  111. const int N = SK_ARRAY_COUNT(vals);
  112. SkRect cull = SkRect::MakeXYWH(43,236,57,149);
  113. SkPath path;
  114. path.addRect(cull);
  115. SkPath path2;
  116. SkPaint paint;
  117. paint.setStyle(SkPaint::kStroke_Style);
  118. paint.setPathEffect(SkDashPathEffect::Make(vals, N, 222));
  119. paint.getFillPath(path, &path2, &cull);
  120. }