DrawPathTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 2012 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/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkPathEffect.h"
  15. #include "include/core/SkPoint.h"
  16. #include "include/core/SkRRect.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkScalar.h"
  20. #include "include/core/SkStrokeRec.h"
  21. #include "include/core/SkSurface.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkDashPathEffect.h"
  24. #include "tests/Test.h"
  25. // test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
  26. static void test_big_aa_rect(skiatest::Reporter* reporter) {
  27. SkBitmap output;
  28. SkPMColor pixel[1];
  29. output.installPixels(SkImageInfo::MakeN32Premul(1, 1), pixel, 4);
  30. auto surf = SkSurface::MakeRasterN32Premul(300, 33300);
  31. SkCanvas* canvas = surf->getCanvas();
  32. SkRect r = { 0, 33000, 300, 33300 };
  33. int x = SkScalarRoundToInt(r.left());
  34. int y = SkScalarRoundToInt(r.top());
  35. // check that the pixel in question starts as transparent (by the surface)
  36. if (surf->readPixels(output, x, y)) {
  37. REPORTER_ASSERT(reporter, 0 == pixel[0]);
  38. } else {
  39. REPORTER_ASSERT(reporter, false, "readPixels failed");
  40. }
  41. SkPaint paint;
  42. paint.setAntiAlias(true);
  43. paint.setColor(SK_ColorWHITE);
  44. canvas->drawRect(r, paint);
  45. // Now check that it is BLACK
  46. if (surf->readPixels(output, x, y)) {
  47. // don't know what swizzling PMColor did, but white should always
  48. // appear the same.
  49. REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
  50. } else {
  51. REPORTER_ASSERT(reporter, false, "readPixels failed");
  52. }
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. static void moveToH(SkPath* path, const uint32_t raw[]) {
  56. const float* fptr = (const float*)raw;
  57. path->moveTo(fptr[0], fptr[1]);
  58. }
  59. static void cubicToH(SkPath* path, const uint32_t raw[]) {
  60. const float* fptr = (const float*)raw;
  61. path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
  62. }
  63. // This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
  64. // arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
  65. // was that the initial line-segment produced by the cubic was not monotonically
  66. // going down (i.e. the initial DY was negative). By rounding the floats, we get
  67. // the more proper result.
  68. //
  69. // http://code.google.com/p/chromium/issues/detail?id=131181
  70. //
  71. // we're not calling this test anymore; is that for a reason?
  72. static void test_crbug131181() {
  73. /*
  74. fX = 18.8943768,
  75. fY = 129.121277
  76. }, {
  77. fX = 18.8937435,
  78. fY = 129.121689
  79. }, {
  80. fX = 18.8950119,
  81. fY = 129.120422
  82. }, {
  83. fX = 18.5030727,
  84. fY = 129.13121
  85. */
  86. uint32_t data[] = {
  87. 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
  88. 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
  89. };
  90. SkPath path;
  91. moveToH(&path, &data[0]);
  92. cubicToH(&path, &data[2]);
  93. auto surface(SkSurface::MakeRasterN32Premul(640, 480));
  94. SkPaint paint;
  95. paint.setAntiAlias(true);
  96. surface->getCanvas()->drawPath(path, paint);
  97. }
  98. // This used to assert in debug builds (and crash writing bad memory in release)
  99. // because we overflowed an intermediate value (B coefficient) setting up our
  100. // stepper for the quadratic. Now we bias that value by 1/2 so we don't overflow
  101. static void test_crbug_140803() {
  102. SkBitmap bm;
  103. bm.allocN32Pixels(2700, 30*1024);
  104. SkCanvas canvas(bm);
  105. SkPaint paint;
  106. paint.setAntiAlias(true);
  107. canvas.drawPath(SkPath().moveTo(2762, 20).quadTo(11, 21702, 10, 21706), paint);
  108. }
  109. // Need to exercise drawing an inverse-path whose bounds intersect the clip,
  110. // but whose edges do not (since its a quad which draws only in the bottom half
  111. // of its bounds).
  112. // In the debug build, we used to assert in this case, until it was fixed.
  113. //
  114. static void test_inversepathwithclip() {
  115. SkPath path;
  116. path.moveTo(0, 20);
  117. path.quadTo(10, 10, 20, 20);
  118. path.toggleInverseFillType();
  119. SkPaint paint;
  120. auto surface(SkSurface::MakeRasterN32Premul(640, 480));
  121. SkCanvas* canvas = surface->getCanvas();
  122. canvas->save();
  123. canvas->clipRect(SkRect::MakeWH(19, 11));
  124. paint.setAntiAlias(false);
  125. canvas->drawPath(path, paint);
  126. paint.setAntiAlias(true);
  127. canvas->drawPath(path, paint);
  128. canvas->restore();
  129. // Now do the test again, with the path flipped, so we only draw in the
  130. // top half of our bounds, and have the clip intersect our bounds at the
  131. // bottom.
  132. path.reset(); // preserves our filltype
  133. path.moveTo(0, 10);
  134. path.quadTo(10, 20, 20, 10);
  135. canvas->clipRect(SkRect::MakeXYWH(0, 19, 19, 11));
  136. paint.setAntiAlias(false);
  137. canvas->drawPath(path, paint);
  138. paint.setAntiAlias(true);
  139. canvas->drawPath(path, paint);
  140. }
  141. static void test_bug533() {
  142. /*
  143. http://code.google.com/p/skia/issues/detail?id=533
  144. This particular test/bug only applies to the float case, where the
  145. coordinates are very large.
  146. */
  147. SkPath path;
  148. path.moveTo(64, 3);
  149. path.quadTo(-329936, -100000000, 1153, 330003);
  150. SkPaint paint;
  151. paint.setAntiAlias(true);
  152. auto surface(SkSurface::MakeRasterN32Premul(640, 480));
  153. surface->getCanvas()->drawPath(path, paint);
  154. }
  155. static void test_crbug_140642() {
  156. /*
  157. * We used to see this construct, and due to rounding as we accumulated
  158. * our length, the loop where we apply the phase would run off the end of
  159. * the array, since it relied on just -= each interval value, which did not
  160. * behave as "expected". Now the code explicitly checks for walking off the
  161. * end of that array.
  162. * A different (better) fix might be to rewrite dashing to do all of its
  163. * length/phase/measure math using double, but this may need to be
  164. * coordinated with SkPathMeasure, to be consistent between the two.
  165. <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
  166. stroke-dashoffset="-248.135982067">
  167. */
  168. const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
  169. auto dontAssert = SkDashPathEffect::Make(vals, 4, -248.135982067f);
  170. }
  171. static void test_crbug_124652() {
  172. /*
  173. http://code.google.com/p/chromium/issues/detail?id=124652
  174. This particular test/bug only applies to the float case, where
  175. large values can "swamp" small ones.
  176. */
  177. SkScalar intervals[2] = {837099584, 33450};
  178. auto dontAssert = SkDashPathEffect::Make(intervals, 2, -10);
  179. }
  180. static void test_bigcubic() {
  181. SkPath path;
  182. path.moveTo(64, 3);
  183. path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
  184. SkPaint paint;
  185. paint.setAntiAlias(true);
  186. auto surface(SkSurface::MakeRasterN32Premul(640, 480));
  187. surface->getCanvas()->drawPath(path, paint);
  188. }
  189. // asserts if halfway case is not handled
  190. static void test_halfway() {
  191. SkPaint paint;
  192. SkPath path;
  193. path.moveTo(16365.5f, 1394);
  194. path.lineTo(16365.5f, 1387.5f);
  195. path.quadTo(16365.5f, 1385.43f, 16367, 1383.96f);
  196. path.quadTo(16368.4f, 1382.5f, 16370.5f, 1382.5f);
  197. path.lineTo(16465.5f, 1382.5f);
  198. path.quadTo(16467.6f, 1382.5f, 16469, 1383.96f);
  199. path.quadTo(16470.5f, 1385.43f, 16470.5f, 1387.5f);
  200. path.lineTo(16470.5f, 1394);
  201. path.quadTo(16470.5f, 1396.07f, 16469, 1397.54f);
  202. path.quadTo(16467.6f, 1399, 16465.5f, 1399);
  203. path.lineTo(16370.5f, 1399);
  204. path.quadTo(16368.4f, 1399, 16367, 1397.54f);
  205. path.quadTo(16365.5f, 1396.07f, 16365.5f, 1394);
  206. path.close();
  207. SkPath p2;
  208. SkMatrix m;
  209. m.reset();
  210. m.postTranslate(0.001f, 0.001f);
  211. path.transform(m, &p2);
  212. auto surface(SkSurface::MakeRasterN32Premul(640, 480));
  213. SkCanvas* canvas = surface->getCanvas();
  214. canvas->translate(-16366, -1383);
  215. canvas->drawPath(p2, paint);
  216. m.reset();
  217. m.postTranslate(-0.001f, -0.001f);
  218. path.transform(m, &p2);
  219. canvas->drawPath(p2, paint);
  220. m.reset();
  221. path.transform(m, &p2);
  222. canvas->drawPath(p2, paint);
  223. }
  224. // we used to assert if the bounds of the device (clip) was larger than 32K
  225. // even when the path itself was smaller. We just draw and hope in the debug
  226. // version to not assert.
  227. static void test_giantaa() {
  228. const int W = 400;
  229. const int H = 400;
  230. auto surface(SkSurface::MakeRasterN32Premul(33000, 10));
  231. SkPaint paint;
  232. paint.setAntiAlias(true);
  233. SkPath path;
  234. path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
  235. surface->getCanvas()->drawPath(path, paint);
  236. }
  237. // Extremely large path_length/dash_length ratios may cause infinite looping
  238. // in SkDashPathEffect::filterPath() due to single precision rounding.
  239. // The test is quite expensive, but it should get much faster after the fix
  240. // for http://crbug.com/165432 goes in.
  241. static void test_infinite_dash(skiatest::Reporter* reporter) {
  242. SkPath path;
  243. path.moveTo(0, 0);
  244. path.lineTo(5000000, 0);
  245. SkScalar intervals[] = { 0.2f, 0.2f };
  246. sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
  247. SkPath filteredPath;
  248. SkPaint paint;
  249. paint.setStyle(SkPaint::kStroke_Style);
  250. paint.setPathEffect(dash);
  251. paint.getFillPath(path, &filteredPath);
  252. // If we reach this, we passed.
  253. REPORTER_ASSERT(reporter, true);
  254. }
  255. // http://crbug.com/165432
  256. // Limit extreme dash path effects to avoid exhausting the system memory.
  257. static void test_crbug_165432(skiatest::Reporter* reporter) {
  258. SkPath path;
  259. path.moveTo(0, 0);
  260. path.lineTo(10000000, 0);
  261. SkScalar intervals[] = { 0.5f, 0.5f };
  262. sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
  263. SkPaint paint;
  264. paint.setStyle(SkPaint::kStroke_Style);
  265. paint.setPathEffect(dash);
  266. SkPath filteredPath;
  267. SkStrokeRec rec(paint);
  268. REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, nullptr));
  269. REPORTER_ASSERT(reporter, filteredPath.isEmpty());
  270. }
  271. // http://crbug.com/472147
  272. // This is a simplified version from the bug. RRect radii not properly scaled.
  273. static void test_crbug_472147_simple(skiatest::Reporter* reporter) {
  274. auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
  275. SkCanvas* canvas = surface->getCanvas();
  276. SkPaint p;
  277. SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
  278. SkVector radii[4] = {
  279. { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
  280. };
  281. SkRRect rr;
  282. rr.setRectRadii(r, radii);
  283. canvas->drawRRect(rr, p);
  284. }
  285. // http://crbug.com/472147
  286. // RRect radii not properly scaled.
  287. static void test_crbug_472147_actual(skiatest::Reporter* reporter) {
  288. auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
  289. SkCanvas* canvas = surface->getCanvas();
  290. SkPaint p;
  291. SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
  292. SkVector radii[4] = {
  293. { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
  294. };
  295. SkRRect rr;
  296. rr.setRectRadii(r, radii);
  297. canvas->clipRRect(rr);
  298. SkRect r2 = SkRect::MakeLTRB(0, 33, 1102, 33554464);
  299. canvas->drawRect(r2, p);
  300. }
  301. DEF_TEST(DrawPath, reporter) {
  302. test_giantaa();
  303. test_bug533();
  304. test_bigcubic();
  305. test_crbug_124652();
  306. test_crbug_140642();
  307. test_crbug_140803();
  308. test_inversepathwithclip();
  309. // why?
  310. if (false) test_crbug131181();
  311. test_infinite_dash(reporter);
  312. test_crbug_165432(reporter);
  313. test_crbug_472147_simple(reporter);
  314. test_crbug_472147_actual(reporter);
  315. test_big_aa_rect(reporter);
  316. test_halfway();
  317. }