PathOpsBuilderTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/SkBitmap.h"
  8. #include "tests/PathOpsExtendedTest.h"
  9. #include "tests/PathOpsTestCommon.h"
  10. #include "tests/Test.h"
  11. DEF_TEST(PathOpsBuilder, reporter) {
  12. SkOpBuilder builder;
  13. SkPath result;
  14. REPORTER_ASSERT(reporter, builder.resolve(&result));
  15. REPORTER_ASSERT(reporter, result.isEmpty());
  16. builder.add(result, kDifference_SkPathOp);
  17. REPORTER_ASSERT(reporter, builder.resolve(&result));
  18. REPORTER_ASSERT(reporter, result.isEmpty());
  19. builder.add(result, kUnion_SkPathOp);
  20. REPORTER_ASSERT(reporter, builder.resolve(&result));
  21. REPORTER_ASSERT(reporter, result.isEmpty());
  22. SkPath rectPath;
  23. rectPath.setFillType(SkPath::kEvenOdd_FillType);
  24. rectPath.addRect(0, 1, 2, 3, SkPath::kCW_Direction);
  25. builder.add(rectPath, kUnion_SkPathOp);
  26. REPORTER_ASSERT(reporter, builder.resolve(&result));
  27. bool closed;
  28. SkPath::Direction dir;
  29. REPORTER_ASSERT(reporter, result.isRect(nullptr, &closed, &dir));
  30. REPORTER_ASSERT(reporter, closed);
  31. REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction);
  32. int pixelDiff = comparePaths(reporter, __FUNCTION__, rectPath, result);
  33. REPORTER_ASSERT(reporter, pixelDiff == 0);
  34. rectPath.reset();
  35. rectPath.setFillType(SkPath::kEvenOdd_FillType);
  36. rectPath.addRect(0, 1, 2, 3, SkPath::kCCW_Direction);
  37. builder.add(rectPath, kUnion_SkPathOp);
  38. REPORTER_ASSERT(reporter, builder.resolve(&result));
  39. REPORTER_ASSERT(reporter, result.isRect(nullptr, &closed, &dir));
  40. REPORTER_ASSERT(reporter, closed);
  41. REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction);
  42. REPORTER_ASSERT(reporter, rectPath == result);
  43. builder.add(rectPath, kDifference_SkPathOp);
  44. REPORTER_ASSERT(reporter, builder.resolve(&result));
  45. REPORTER_ASSERT(reporter, result.isEmpty());
  46. SkPath rect2, rect3;
  47. rect2.addRect(2, 1, 4, 3, SkPath::kCW_Direction);
  48. rect3.addRect(4, 1, 5, 3, SkPath::kCCW_Direction);
  49. builder.add(rectPath, kUnion_SkPathOp);
  50. builder.add(rect2, kUnion_SkPathOp);
  51. builder.add(rect3, kUnion_SkPathOp);
  52. REPORTER_ASSERT(reporter, builder.resolve(&result));
  53. REPORTER_ASSERT(reporter, result.isRect(nullptr, &closed, &dir));
  54. REPORTER_ASSERT(reporter, closed);
  55. SkRect expected;
  56. expected.set(0, 1, 5, 3);
  57. REPORTER_ASSERT(reporter, result.getBounds() == expected);
  58. SkPath circle1, circle2, circle3;
  59. circle1.addCircle(5, 6, 4, SkPath::kCW_Direction);
  60. circle2.addCircle(7, 4, 8, SkPath::kCCW_Direction);
  61. circle3.addCircle(6, 5, 6, SkPath::kCW_Direction);
  62. SkPath opCompare;
  63. Op(circle1, circle2, kUnion_SkPathOp, &opCompare);
  64. Op(opCompare, circle3, kDifference_SkPathOp, &opCompare);
  65. builder.add(circle1, kUnion_SkPathOp);
  66. builder.add(circle2, kUnion_SkPathOp);
  67. builder.add(circle3, kDifference_SkPathOp);
  68. REPORTER_ASSERT(reporter, builder.resolve(&result));
  69. pixelDiff = comparePaths(reporter, __FUNCTION__, opCompare, result);
  70. REPORTER_ASSERT(reporter, pixelDiff == 0);
  71. }
  72. DEF_TEST(BuilderIssue3838, reporter) {
  73. SkPath path;
  74. path.moveTo(200, 170);
  75. path.lineTo(220, 170);
  76. path.lineTo(220, 230);
  77. path.lineTo(240, 230);
  78. path.lineTo(240, 210);
  79. path.lineTo(180, 210);
  80. path.lineTo(180, 190);
  81. path.lineTo(260, 190);
  82. path.lineTo(260, 250);
  83. path.lineTo(200, 250);
  84. path.lineTo(200, 170);
  85. path.close();
  86. SkPath path2;
  87. SkOpBuilder builder;
  88. builder.add(path, kUnion_SkPathOp);
  89. builder.resolve(&path2);
  90. int pixelDiff = comparePaths(reporter, __FUNCTION__, path, path2);
  91. REPORTER_ASSERT(reporter, pixelDiff == 0);
  92. }
  93. DEF_TEST(BuilderIssue3838_2, reporter) {
  94. SkPath path;
  95. path.addCircle(100, 100, 50);
  96. SkOpBuilder builder;
  97. builder.add(path, kUnion_SkPathOp);
  98. builder.add(path, kUnion_SkPathOp);
  99. SkPath result;
  100. builder.resolve(&result);
  101. int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result);
  102. REPORTER_ASSERT(reporter, pixelDiff == 0);
  103. }
  104. DEF_TEST(BuilderIssue3838_3, reporter) {
  105. SkPath path;
  106. path.moveTo(40, 10);
  107. path.lineTo(60, 10);
  108. path.lineTo(60, 30);
  109. path.lineTo(40, 30);
  110. path.lineTo(40, 10);
  111. path.moveTo(41, 11);
  112. path.lineTo(41, 29);
  113. path.lineTo(59, 29);
  114. path.lineTo(59, 11);
  115. path.lineTo(41, 11);
  116. SkOpBuilder builder;
  117. builder.add(path, kUnion_SkPathOp);
  118. SkPath result;
  119. builder.resolve(&result);
  120. int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result);
  121. REPORTER_ASSERT(reporter, pixelDiff == 0);
  122. }
  123. DEF_TEST(BuilderIssue502792_2, reporter) {
  124. SkPath path, pathB;
  125. path.setFillType(SkPath::kWinding_FillType);
  126. path.addRect(0, 0, 1, 1, SkPath::kCW_Direction);
  127. path.addRect(2, 2, 3, 3, SkPath::kCW_Direction);
  128. pathB.setFillType(SkPath::kEvenOdd_FillType);
  129. pathB.addRect(3, 3, 4, 4, SkPath::kCW_Direction);
  130. pathB.addRect(3, 3, 4, 4, SkPath::kCW_Direction);
  131. SkOpBuilder builder;
  132. builder.add(path, kUnion_SkPathOp);
  133. builder.add(pathB, kDifference_SkPathOp);
  134. SkPath result;
  135. builder.resolve(&result);
  136. }
  137. DEF_TEST(Fuzz846, reporter) {
  138. /*
  139. <clipPath id="clip-circle">
  140. <circle id="circle" cx="60" cy="60" r="50" />
  141. </clipPath>
  142. <clipPath id="clip-rect">
  143. <clipPath id="clip-rect">
  144. <clipPath id="clip-rect">
  145. <clipPath id="clip-rect">
  146. <rect x="10" y="30" width="0" height="60" />
  147. <rect x="10" y="30" width="0" height="60" />
  148. <rect x="10" y="30" width="100" height="60" />
  149. <rect x="10" y="30" width="32668" />
  150. <rect x="10" y="30" width="100" height="18446744073709551615" />
  151. <rect x="10" y="255" width="100" height="60" />
  152. <rect width="100" height="60" />
  153. <rect x="10" y="30" width="100" height="60" />
  154. <rect x="10" y="30" width="100" height="4294967236" />
  155. <rect x="10" y="30" width="100" height="60" />
  156. </clipPath>
  157. <rect x="10" y="30" width="0" height="60" />
  158. <rect x="10" y="30" width="0" height="0.18093252719929986369568203" />
  159. <rect x="10" y="30" width="100" height="60" />
  160. <rect x="10" y="30" width="32668" height="60" />
  161. <rect x="10" y="30" width="100" height="18446744073709551615" />
  162. <rect x="10" y="255" width="100" height="60" />
  163. <rect x="2147483649" y="30" width="100" height="60" />
  164. <rect x="10" y="30" width="100" height="60" />
  165. <rect x="10" y="30" width="100" height="60" />
  166. <rect x="10" y="30" width="100" height="60" />
  167. </clipPath>
  168. <rect x="10" y="30" width="0" height="60" />
  169. <rect x="10" y="30" width="0" height="60" />
  170. <rect x="10" y="30" width="100" height="60" />
  171. <rect x="10" y="30" width="32668" height="60" />
  172. <rect x="10" y="30" width="100" height="18446744073709551615" />
  173. <rect x="10" y="255" width="100" height="60" />
  174. <rect x="2147483649" y="30" width="100" height="60" />
  175. <rect x="10" y="30" width="100" height="60" />
  176. <rect x="10" y="2879753595" width="100" height="60" />
  177. <rect x="10" y="30" width="100" height="60" />
  178. </clipPath>
  179. <rect x="10" y="30" width="100" height="60" />
  180. <rect x="10" y="30" width="0" height="60" />
  181. <rect x="10" y="30" width="100" height="60" />
  182. <rect x="10" y="30" width="32668" height="60" />
  183. <rect x="10" y="30" width="100" height="18446744073709551615" />
  184. <rect x="10" y="255" width="100" height="60" />
  185. <rect x="2147483649" y="30" width="100" height="60" />
  186. <rect x="10" y="30" width="100" height="60" />
  187. <rect x="10" y="30" width="100" height="4294967236" />
  188. <rect x="10" y="30" width="100" height="4294967236" />
  189. <rect x="10" y="30" width="100" height="4294967236" />
  190. <rect x="10" y="30" width="100" height="4294967236" />
  191. <rect x="10" y="30" width="100" height="60" />
  192. <rect x="757798030" y="30" width="100" height="60" />
  193. */
  194. SkPath clipCircle, clipRect;
  195. SkPath inner;
  196. clipCircle.addCircle(60, 60, 50); // <circle id="circle" cx="60" cy="60" r="50" />
  197. inner.addRect(10, 30, 10+0, 30+60); // <rect x="10" y="30" width="0" height="60" />
  198. inner.addRect(10, 30, 10+0, 30+60); // <rect x="10" y="30" width="0" height="60" />
  199. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  200. inner.addRect(10, 30, 10+32668, 30+0); // <rect x="10" y="30" width="32668" />
  201. inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
  202. inner.addRect(10, 255, 10+100, 255+60); // <rect x="10" y="255" width="100" height="60" />
  203. inner.addRect(0, 0, 0+100, 0+60); // <rect width="100" height="60" />
  204. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  205. inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
  206. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  207. clipRect.addPath(inner);
  208. inner.reset();
  209. inner.addRect(10, 30, 10+0, 30+60); // <rect x="10" y="30" width="0" height="60" />
  210. inner.addRect(10, 30, 10+0, 30+0.18093252719929986369568203f); // <rect x="10" y="30" width="0" height="0.18093252719929986369568203" />
  211. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  212. inner.addRect(10, 30, 10+32668, 30+60); // <rect x="10" y="30" width="32668" height="60" />
  213. inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
  214. inner.addRect(10, 255, 10+100, 255+60); // <rect x="10" y="255" width="100" height="60" />
  215. inner.addRect(2147483649.f, 30, 2147483649.f+100, 30+60); // <rect x="2147483649" y="30" width="100" height="60" />
  216. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  217. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  218. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  219. clipRect.addPath(inner);
  220. inner.reset();
  221. inner.addRect(10, 30, 10+0, 30+60); // <rect x="10" y="30" width="0" height="60" />
  222. inner.addRect(10, 30, 10+0, 30+60); // <rect x="10" y="30" width="0" height="60" />
  223. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  224. inner.addRect(10, 30, 10+32668, 30+60); // <rect x="10" y="30" width="32668" height="60" />
  225. inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
  226. inner.addRect(10, 255, 10+100, 255+60); // <rect x="10" y="255" width="100" height="60" />
  227. inner.addRect(2147483649.f, 30, 2147483649.f+100, 30+60); // <rect x="2147483649" y="30" width="100" height="60" />
  228. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  229. inner.addRect(10, 2879753595.f, 10+100, 30+2879753595.f); // <rect x="10" y="2879753595" width="100" height="60" />
  230. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  231. clipRect.addPath(inner);
  232. inner.reset();
  233. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  234. inner.addRect(10, 30, 10+0, 30+60); // <rect x="10" y="30" width="0" height="60" />
  235. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  236. inner.addRect(10, 30, 10+32668, 30+60); // <rect x="10" y="30" width="32668" height="60" />
  237. inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
  238. inner.addRect(10, 255, 10+100, 255+60); // <rect x="10" y="255" width="100" height="60" />
  239. inner.addRect(2147483649.f, 30, 2147483649.f+100, 30+60); // <rect x="2147483649" y="30" width="100" height="60" />
  240. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  241. inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
  242. inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
  243. inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
  244. inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
  245. inner.addRect(10, 30, 10+100, 30+60); // <rect x="10" y="30" width="100" height="60" />
  246. inner.addRect(757798030.f, 30, 757798030.f+100, 30+60); // <rect x="757798030" y="30" width="100" height="60" />
  247. clipRect.addPath(inner);
  248. SkOpBuilder builder;
  249. builder.add(clipCircle, kUnion_SkPathOp);
  250. builder.add(clipRect, kDifference_SkPathOp);
  251. SkPath result;
  252. builder.resolve(&result);
  253. }
  254. DEF_TEST(Issue569540, reporter) {
  255. SkPath path1;
  256. path1.moveTo(5, -225);
  257. path1.lineTo(-225, 7425);
  258. path1.lineTo(7425, 7425);
  259. path1.lineTo(7425, -225);
  260. path1.lineTo(-225, -225);
  261. path1.lineTo(5, -225);
  262. path1.close();
  263. SkPath path2;
  264. path2.moveTo(5940, 2790);
  265. path2.lineTo(5940, 2160);
  266. path2.lineTo(5970, 1980);
  267. path2.lineTo(5688, 773669888);
  268. path2.lineTo(5688, 2160);
  269. path2.lineTo(5688, 2430);
  270. path2.lineTo(5400, 4590);
  271. path2.lineTo(5220, 4590);
  272. path2.lineTo(5220, 4920);
  273. path2.cubicTo(5182.22900390625f, 4948.328125f, 5160, 4992.78662109375f, 5160, 5040.00048828125f);
  274. path2.lineTo(5940, 2790);
  275. path2.close();
  276. SkOpBuilder builder;
  277. builder.add(path1, kUnion_SkPathOp);
  278. builder.add(path2, kUnion_SkPathOp);
  279. SkPath result;
  280. builder.resolve(&result);
  281. }
  282. DEF_TEST(SkOpBuilderFuzz665, reporter) {
  283. SkPath path;
  284. path.setFillType(SkPath::kEvenOdd_FillType);
  285. path.moveTo(SkBits2Float(0xcc4264a7), SkBits2Float(0x4bb12e50)); // -5.0959e+07f, 2.32235e+07f
  286. path.lineTo(SkBits2Float(0xcc4264b0), SkBits2Float(0x4bb12e48)); // -5.0959e+07f, 2.32234e+07f
  287. path.lineTo(SkBits2Float(0xcc4264a7), SkBits2Float(0x4bb12e50)); // -5.0959e+07f, 2.32235e+07f
  288. path.close();
  289. SkPath path1(path);
  290. path.reset();
  291. path.setFillType(SkPath::kWinding_FillType);
  292. path.moveTo(SkBits2Float(0x43213333), SkBits2Float(0x43080000)); // 161.2f, 136
  293. path.lineTo(SkBits2Float(0x43038000), SkBits2Float(0x43080000)); // 131.5f, 136
  294. path.cubicTo(SkBits2Float(0x43038000), SkBits2Float(0x42f00000), SkBits2Float(0x42f16666), SkBits2Float(0x42d53333), SkBits2Float(0x42d3cccd), SkBits2Float(0x42cd6666)); // 131.5f, 120, 120.7f, 106.6f, 105.9f, 102.7f
  295. path.lineTo(SkBits2Float(0x42e33333), SkBits2Float(0x42940000)); // 113.6f, 74
  296. SkPath path2(path);
  297. SkOpBuilder builder;
  298. builder.add(path1, kUnion_SkPathOp);
  299. builder.add(path2, kUnion_SkPathOp);
  300. SkPath result;
  301. builder.resolve(&result);
  302. }
  303. DEF_TEST(SkOpBuilder618991, reporter) {
  304. SkPath path0;
  305. path0.moveTo(140, 40);
  306. path0.lineTo(200, 210);
  307. path0.lineTo(40, 100);
  308. path0.lineTo(2.22223e+07f, 2.22222e+14f);
  309. path0.lineTo(2.22223e+07f, 2.22222e+14f);
  310. SkPath path1;
  311. path1.moveTo(160, 60);
  312. path1.lineTo(220, 230);
  313. path1.lineTo(60, 120);
  314. path1.lineTo(2.22223e+07f, 2.22222e+14f);
  315. path1.lineTo(2.22223e+07f, 2.22222e+14f);
  316. SkOpBuilder builder;
  317. builder.add(path0, SkPathOp::kUnion_SkPathOp);
  318. builder.add(path1, SkPathOp::kUnion_SkPathOp);
  319. builder.resolve(&path0);
  320. }
  321. DEF_TEST(SkOpBuilderKFuzz1, reporter) {
  322. SkPath path;
  323. path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
  324. path.lineTo(SkBits2Float(0x39008001), SkBits2Float(0xd31fbc1d)); // 0.000122547f, -6.86056e+11f
  325. path.conicTo(SkBits2Float(0x246a205a), SkBits2Float(0x0080d3fb), SkBits2Float(0xce000001), SkBits2Float(0x04d31fbc), SkBits2Float(0x57a82c00)); // 5.07681e-17f, 1.1831e-38f, -5.36871e+08f, 4.9635e-36f, 3.69814e+14f
  326. SkPath path0(path);
  327. path.reset();
  328. path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
  329. path.cubicTo(SkBits2Float(0x80d3f924), SkBits2Float(0xcecece4f), SkBits2Float(0xcececece), SkBits2Float(0xcececece), SkBits2Float(0x9a9a9ace), SkBits2Float(0x9a9a9a9a)); // -1.94667e-38f, -1.73481e+09f, -1.73483e+09f, -1.73483e+09f, -6.3943e-23f, -6.39427e-23f
  330. path.moveTo(SkBits2Float(0x9a9a019a), SkBits2Float(0xa59a9a9a)); // -6.36955e-23f, -2.68195e-16f
  331. SkPath path1(path);
  332. SkOpBuilder builder;
  333. builder.add(path0, SkPathOp::kUnion_SkPathOp);
  334. builder.add(path1, SkPathOp::kUnion_SkPathOp);
  335. builder.resolve(&path);
  336. }