PathOpsExtendedTest.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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/SkMatrix.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkRegion.h"
  12. #include "include/core/SkStream.h"
  13. #include "include/private/SkMutex.h"
  14. #include "include/utils/SkParsePath.h"
  15. #include "tests/PathOpsDebug.h"
  16. #include "tests/PathOpsExtendedTest.h"
  17. #include "tests/PathOpsThreadedCommon.h"
  18. #include <stdlib.h>
  19. #include <vector>
  20. #include <string>
  21. #include <algorithm>
  22. std::vector<std::string> gUniqueNames;
  23. #ifdef SK_BUILD_FOR_MAC
  24. #include <sys/sysctl.h>
  25. #endif
  26. // std::to_string isn't implemented on android
  27. #include <sstream>
  28. template <typename T>
  29. std::string std_to_string(T value)
  30. {
  31. std::ostringstream os ;
  32. os << value ;
  33. return os.str() ;
  34. }
  35. bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
  36. SkDEBUGPARAMS(bool skipAssert)
  37. SkDEBUGPARAMS(const char* testName));
  38. bool SimplifyDebug(const SkPath& one, SkPath* result
  39. SkDEBUGPARAMS(bool skipAssert)
  40. SkDEBUGPARAMS(const char* testName));
  41. static const char marker[] =
  42. "</div>\n"
  43. "\n"
  44. "<script type=\"text/javascript\">\n"
  45. "\n"
  46. "var testDivs = [\n";
  47. static const char* opStrs[] = {
  48. "kDifference_SkPathOp",
  49. "kIntersect_SkPathOp",
  50. "kUnion_SkPathOp",
  51. "kXOR_PathOp",
  52. "kReverseDifference_SkPathOp",
  53. };
  54. static const char* opSuffixes[] = {
  55. "d",
  56. "i",
  57. "u",
  58. "o",
  59. "r",
  60. };
  61. enum class ExpectSuccess {
  62. kNo,
  63. kYes,
  64. kFlaky
  65. };
  66. enum class SkipAssert {
  67. kNo,
  68. kYes
  69. };
  70. enum class ExpectMatch {
  71. kNo,
  72. kYes,
  73. kFlaky
  74. };
  75. #if DEBUG_SHOW_TEST_NAME
  76. static void showPathData(const SkPath& path) {
  77. SkPath::RawIter iter(path);
  78. uint8_t verb;
  79. SkPoint pts[4];
  80. SkPoint firstPt = {0, 0}, lastPt = {0, 0};
  81. bool firstPtSet = false;
  82. bool lastPtSet = true;
  83. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
  84. switch (verb) {
  85. case SkPath::kMove_Verb:
  86. if (firstPtSet && lastPtSet && firstPt != lastPt) {
  87. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
  88. firstPt.fX, firstPt.fY);
  89. lastPtSet = false;
  90. }
  91. firstPt = pts[0];
  92. firstPtSet = true;
  93. continue;
  94. case SkPath::kLine_Verb:
  95. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
  96. pts[1].fX, pts[1].fY);
  97. lastPt = pts[1];
  98. lastPtSet = true;
  99. break;
  100. case SkPath::kQuad_Verb:
  101. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
  102. pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
  103. lastPt = pts[2];
  104. lastPtSet = true;
  105. break;
  106. case SkPath::kConic_Verb:
  107. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
  108. pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
  109. iter.conicWeight());
  110. lastPt = pts[2];
  111. lastPtSet = true;
  112. break;
  113. case SkPath::kCubic_Verb:
  114. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
  115. pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
  116. pts[3].fX, pts[3].fY);
  117. lastPt = pts[3];
  118. lastPtSet = true;
  119. break;
  120. case SkPath::kClose_Verb:
  121. if (firstPtSet && lastPtSet && firstPt != lastPt) {
  122. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
  123. firstPt.fX, firstPt.fY);
  124. }
  125. firstPtSet = lastPtSet = false;
  126. break;
  127. default:
  128. SkDEBUGFAIL("bad verb");
  129. return;
  130. }
  131. }
  132. if (firstPtSet && lastPtSet && firstPt != lastPt) {
  133. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
  134. firstPt.fX, firstPt.fY);
  135. }
  136. }
  137. #endif
  138. void showOp(const SkPathOp op) {
  139. switch (op) {
  140. case kDifference_SkPathOp:
  141. SkDebugf("op difference\n");
  142. break;
  143. case kIntersect_SkPathOp:
  144. SkDebugf("op intersect\n");
  145. break;
  146. case kUnion_SkPathOp:
  147. SkDebugf("op union\n");
  148. break;
  149. case kXOR_SkPathOp:
  150. SkDebugf("op xor\n");
  151. break;
  152. case kReverseDifference_SkPathOp:
  153. SkDebugf("op reverse difference\n");
  154. break;
  155. default:
  156. SkASSERT(0);
  157. }
  158. }
  159. #if DEBUG_SHOW_TEST_NAME
  160. static char hexorator(int x) {
  161. if (x < 10) {
  162. return x + '0';
  163. }
  164. x -= 10;
  165. SkASSERT(x < 26);
  166. return x + 'A';
  167. }
  168. #endif
  169. void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
  170. #if DEBUG_SHOW_TEST_NAME
  171. state->fSerialNo[0] = hexorator(state->fA);
  172. state->fSerialNo[1] = hexorator(state->fB);
  173. state->fSerialNo[2] = hexorator(state->fC);
  174. state->fSerialNo[3] = hexorator(state->fD);
  175. state->fSerialNo[4] = hexorator(a);
  176. state->fSerialNo[5] = hexorator(b);
  177. state->fSerialNo[6] = hexorator(c);
  178. state->fSerialNo[7] = hexorator(d);
  179. state->fSerialNo[8] = '\0';
  180. SkDebugf("%s\n", state->fSerialNo);
  181. if (strcmp(state->fSerialNo, state->fKey) == 0) {
  182. SkDebugf("%s\n", state->fPathStr.c_str());
  183. }
  184. #endif
  185. }
  186. const int bitWidth = 64;
  187. const int bitHeight = 64;
  188. static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
  189. SkRect larger = one.getBounds();
  190. larger.join(two.getBounds());
  191. SkScalar largerWidth = larger.width();
  192. if (largerWidth < 4) {
  193. largerWidth = 4;
  194. }
  195. SkScalar largerHeight = larger.height();
  196. if (largerHeight < 4) {
  197. largerHeight = 4;
  198. }
  199. SkScalar hScale = (bitWidth - 2) / largerWidth;
  200. SkScalar vScale = (bitHeight - 2) / largerHeight;
  201. scale.reset();
  202. scale.preScale(hScale, vScale);
  203. larger.fLeft *= hScale;
  204. larger.fRight *= hScale;
  205. larger.fTop *= vScale;
  206. larger.fBottom *= vScale;
  207. SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
  208. : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
  209. SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
  210. : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
  211. scale.postTranslate(dx, dy);
  212. }
  213. static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
  214. int& error2x2) {
  215. if (bits.width() == 0) {
  216. bits.allocN32Pixels(bitWidth * 2, bitHeight);
  217. }
  218. SkCanvas canvas(bits);
  219. canvas.drawColor(SK_ColorWHITE);
  220. SkPaint paint;
  221. canvas.save();
  222. const SkRect& bounds1 = scaledOne.getBounds();
  223. canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
  224. canvas.drawPath(scaledOne, paint);
  225. canvas.restore();
  226. canvas.save();
  227. canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
  228. canvas.drawPath(scaledTwo, paint);
  229. canvas.restore();
  230. int errors2 = 0;
  231. int errors = 0;
  232. for (int y = 0; y < bitHeight - 1; ++y) {
  233. uint32_t* addr1 = bits.getAddr32(0, y);
  234. uint32_t* addr2 = bits.getAddr32(0, y + 1);
  235. uint32_t* addr3 = bits.getAddr32(bitWidth, y);
  236. uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
  237. for (int x = 0; x < bitWidth - 1; ++x) {
  238. // count 2x2 blocks
  239. bool err = addr1[x] != addr3[x];
  240. if (err) {
  241. errors2 += addr1[x + 1] != addr3[x + 1]
  242. && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
  243. errors++;
  244. }
  245. }
  246. }
  247. error2x2 = errors2;
  248. return errors;
  249. }
  250. static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
  251. SkPath& scaledTwo, int& error2x2) {
  252. SkMatrix scale;
  253. scaleMatrix(one, two, scale);
  254. one.transform(scale, &scaledOne);
  255. two.transform(scale, &scaledTwo);
  256. return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
  257. }
  258. bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
  259. if (!drawPaths) {
  260. return true;
  261. }
  262. const SkRect& bounds1 = one.getBounds();
  263. const SkRect& bounds2 = two.getBounds();
  264. SkRect larger = bounds1;
  265. larger.join(bounds2);
  266. SkBitmap bits;
  267. char out[256];
  268. int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
  269. if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
  270. return false;
  271. }
  272. int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
  273. if (bitHeight >= (int) sizeof(out)) {
  274. return false;
  275. }
  276. bits.allocN32Pixels(bitWidth * 2, bitHeight);
  277. SkCanvas canvas(bits);
  278. canvas.drawColor(SK_ColorWHITE);
  279. SkPaint paint;
  280. canvas.save();
  281. canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
  282. canvas.drawPath(one, paint);
  283. canvas.restore();
  284. canvas.save();
  285. canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
  286. canvas.drawPath(two, paint);
  287. canvas.restore();
  288. for (int y = 0; y < bitHeight; ++y) {
  289. uint32_t* addr1 = bits.getAddr32(0, y);
  290. int x;
  291. char* outPtr = out;
  292. for (x = 0; x < bitWidth; ++x) {
  293. *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
  294. }
  295. *outPtr++ = '|';
  296. for (x = bitWidth; x < bitWidth * 2; ++x) {
  297. *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
  298. }
  299. *outPtr++ = '\0';
  300. SkDebugf("%s\n", out);
  301. }
  302. return true;
  303. }
  304. int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
  305. const SkPath& two, SkBitmap& bitmap) {
  306. int errors2x2;
  307. SkPath scaledOne, scaledTwo;
  308. (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
  309. if (errors2x2 == 0) {
  310. return 0;
  311. }
  312. const int MAX_ERRORS = 9;
  313. return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
  314. }
  315. static SkTDArray<SkPathOp> gTestOp;
  316. static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
  317. const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
  318. const SkPathOp shapeOp, const SkMatrix& scale) {
  319. SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
  320. if (!testName) {
  321. testName = "xOp";
  322. }
  323. SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
  324. testName, opSuffixes[shapeOp]);
  325. *gTestOp.append() = shapeOp;
  326. SkDebugf(" SkPath path, pathB;\n");
  327. SkPathOpsDebug::ShowOnePath(a, "path", false);
  328. SkPathOpsDebug::ShowOnePath(b, "pathB", false);
  329. SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
  330. SkDebugf("}\n");
  331. drawAsciiPaths(scaledOne, scaledTwo, true);
  332. }
  333. static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
  334. const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
  335. const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
  336. ExpectMatch expectMatch) {
  337. static SkMutex& compareDebugOut3 = *(new SkMutex);
  338. int errors2x2;
  339. const int MAX_ERRORS = 8;
  340. (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
  341. if (ExpectMatch::kNo == expectMatch) {
  342. if (errors2x2 < MAX_ERRORS) {
  343. REPORTER_ASSERT(reporter, 0);
  344. }
  345. return 0;
  346. }
  347. if (errors2x2 == 0) {
  348. return 0;
  349. }
  350. if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
  351. SkAutoMutexExclusive autoM(compareDebugOut3);
  352. showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
  353. SkDebugf("\n/*");
  354. REPORTER_ASSERT(reporter, 0);
  355. SkDebugf(" */\n");
  356. }
  357. return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
  358. }
  359. // Default values for when reporter->verbose() is false.
  360. static int testNumber = 55;
  361. static const char* testName = "pathOpTest";
  362. static void appendTestName(const char* nameSuffix, std::string& out) {
  363. out += testName;
  364. out += std_to_string(testNumber);
  365. ++testNumber;
  366. if (nameSuffix) {
  367. out.append(nameSuffix);
  368. }
  369. }
  370. static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
  371. const char* testFunction, bool twoPaths, std::string& out) {
  372. #if 0
  373. out.append("\n<div id=\"");
  374. appendTestName(nameSuffix, out);
  375. out.append("\">\n");
  376. if (pathPrefix) {
  377. out.append(pathPrefix);
  378. }
  379. out.append(pathStr);
  380. out.append("</div>\n\n");
  381. out.append(marker);
  382. out.append(" ");
  383. appendTestName(nameSuffix, out);
  384. out.append(",\n\n\n");
  385. #endif
  386. out.append("static void ");
  387. appendTestName(nameSuffix, out);
  388. out.append("(skiatest::Reporter* reporter) {\n SkPath path");
  389. if (twoPaths) {
  390. out.append(", pathB");
  391. }
  392. out.append(";\n");
  393. if (pathPrefix) {
  394. out.append(pathPrefix);
  395. }
  396. out += pathStr;
  397. out += " ";
  398. out += testFunction;
  399. #if 0
  400. out.append("static void (*firstTest)() = ");
  401. appendTestName(nameSuffix, out);
  402. out.append(";\n\n");
  403. out.append("static struct {\n");
  404. out.append(" void (*fun)();\n");
  405. out.append(" const char* str;\n");
  406. out.append("} tests[] = {\n");
  407. out.append(" TEST(");
  408. appendTestName(nameSuffix, out);
  409. out.append("),\n");
  410. #endif
  411. }
  412. void markTestFlakyForPathKit() {
  413. if (PathOpsDebug::gJson) {
  414. SkASSERT(!PathOpsDebug::gMarkJsonFlaky);
  415. PathOpsDebug::gMarkJsonFlaky = true;
  416. }
  417. }
  418. bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
  419. const char* pathStr) {
  420. static SkMutex& simplifyDebugOut = *(new SkMutex);
  421. SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
  422. path.setFillType(fillType);
  423. state.fReporter->bumpTestCount();
  424. if (!Simplify(path, &out)) {
  425. SkDebugf("%s did not expect failure\n", __FUNCTION__);
  426. REPORTER_ASSERT(state.fReporter, 0);
  427. return false;
  428. }
  429. if (!state.fReporter->verbose()) {
  430. return true;
  431. }
  432. int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
  433. if (result) {
  434. SkAutoMutexExclusive autoM(simplifyDebugOut);
  435. std::string str;
  436. const char* pathPrefix = nullptr;
  437. const char* nameSuffix = nullptr;
  438. if (fillType == SkPath::kEvenOdd_FillType) {
  439. pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
  440. nameSuffix = "x";
  441. }
  442. const char testFunction[] = "testSimplify(reporter, path);";
  443. appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str);
  444. SkDebugf("%s", str.c_str());
  445. REPORTER_ASSERT(state.fReporter, 0);
  446. }
  447. state.fReporter->bumpTestCount();
  448. return result == 0;
  449. }
  450. static void json_status(ExpectSuccess expectSuccess, ExpectMatch expectMatch, bool opSucceeded) {
  451. fprintf(PathOpsDebug::gOut, " \"expectSuccess\": \"%s\",\n",
  452. ExpectSuccess::kNo == expectSuccess ? "no" :
  453. ExpectSuccess::kYes == expectSuccess ? "yes" : "flaky");
  454. if (PathOpsDebug::gMarkJsonFlaky) {
  455. expectMatch = ExpectMatch::kFlaky;
  456. PathOpsDebug::gMarkJsonFlaky = false;
  457. }
  458. fprintf(PathOpsDebug::gOut, " \"expectMatch\": \"%s\",\n",
  459. ExpectMatch::kNo == expectMatch ? "no" :
  460. ExpectMatch::kYes == expectMatch ? "yes" : "flaky");
  461. fprintf(PathOpsDebug::gOut, " \"succeeded\": %s,\n", opSucceeded ? "true" : "false");
  462. }
  463. static void json_path_out(const SkPath& path, const char* pathName, const char* fillTypeName,
  464. bool lastField) {
  465. char const * const gFillTypeStrs[] = {
  466. "Winding",
  467. "EvenOdd",
  468. "InverseWinding",
  469. "InverseEvenOdd",
  470. };
  471. if (PathOpsDebug::gOutputSVG) {
  472. SkString svg;
  473. SkParsePath::ToSVGString(path, &svg);
  474. fprintf(PathOpsDebug::gOut, " \"%s\": \"%s\",\n", pathName, svg.c_str());
  475. } else {
  476. SkPath::RawIter iter(path);
  477. SkPath::Verb verb;
  478. // MOVE, LINE, QUAD, CONIC, CUBIC, CLOSE
  479. const int verbConst[] = { 0, 1, 2, 3, 4, 5 };
  480. const int pointIndex[] = { 0, 1, 1, 1, 1, 0 };
  481. const int pointCount[] = { 1, 2, 3, 3, 4, 0 };
  482. fprintf(PathOpsDebug::gOut, " \"%s\": [", pathName);
  483. bool first = true;
  484. do {
  485. SkPoint points[4];
  486. verb = iter.next(points);
  487. if (SkPath::kDone_Verb == verb) {
  488. break;
  489. }
  490. if (first) {
  491. first = false;
  492. } else {
  493. fprintf(PathOpsDebug::gOut, ",\n ");
  494. }
  495. int verbIndex = (int) verb;
  496. fprintf(PathOpsDebug::gOut, "[%d", verbConst[verbIndex]);
  497. for (int i = pointIndex[verbIndex]; i < pointCount[verbIndex]; ++i) {
  498. fprintf(PathOpsDebug::gOut, ", \"0x%08x\", \"0x%08x\"",
  499. SkFloat2Bits(points[i].fX), SkFloat2Bits(points[i].fY));
  500. }
  501. if (SkPath::kConic_Verb == verb) {
  502. fprintf(PathOpsDebug::gOut, ", \"0x%08x\"", SkFloat2Bits(iter.conicWeight()));
  503. }
  504. fprintf(PathOpsDebug::gOut, "]");
  505. } while (SkPath::kDone_Verb != verb);
  506. fprintf(PathOpsDebug::gOut, "],\n");
  507. }
  508. fprintf(PathOpsDebug::gOut, " \"fillType%s\": \"k%s_FillType\"%s", fillTypeName,
  509. gFillTypeStrs[(int) path.getFillType()], lastField ? "\n}" : ",\n");
  510. }
  511. static bool check_for_duplicate_names(const char* testName) {
  512. if (PathOpsDebug::gCheckForDuplicateNames) {
  513. if (gUniqueNames.end() != std::find(gUniqueNames.begin(), gUniqueNames.end(),
  514. std::string(testName))) {
  515. SkDebugf(""); // convenience for setting breakpoints
  516. }
  517. gUniqueNames.push_back(std::string(testName));
  518. return true;
  519. }
  520. return false;
  521. }
  522. static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
  523. ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
  524. #if 0 && DEBUG_SHOW_TEST_NAME
  525. showPathData(path);
  526. #endif
  527. if (PathOpsDebug::gJson) {
  528. if (check_for_duplicate_names(filename)) {
  529. return true;
  530. }
  531. if (!PathOpsDebug::gOutFirst) {
  532. fprintf(PathOpsDebug::gOut, ",\n");
  533. }
  534. PathOpsDebug::gOutFirst = false;
  535. fprintf(PathOpsDebug::gOut, "\"%s\": {\n", filename);
  536. json_path_out(path, "path", "", false);
  537. }
  538. SkPath out;
  539. if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
  540. SkDEBUGPARAMS(testName))) {
  541. if (ExpectSuccess::kYes == expectSuccess) {
  542. SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
  543. REPORTER_ASSERT(reporter, 0);
  544. }
  545. if (PathOpsDebug::gJson) {
  546. json_status(expectSuccess, expectMatch, false);
  547. fprintf(PathOpsDebug::gOut, " \"out\": \"\"\n}");
  548. }
  549. return false;
  550. } else {
  551. if (ExpectSuccess::kNo == expectSuccess) {
  552. SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
  553. REPORTER_ASSERT(reporter, 0);
  554. }
  555. if (PathOpsDebug::gJson) {
  556. json_status(expectSuccess, expectMatch, true);
  557. json_path_out(out, "out", "Out", true);
  558. }
  559. }
  560. SkBitmap bitmap;
  561. int errors = comparePaths(reporter, filename, path, out, bitmap);
  562. if (ExpectMatch::kNo == expectMatch) {
  563. if (!errors) {
  564. SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
  565. REPORTER_ASSERT(reporter, 0);
  566. return false;
  567. }
  568. } else if (ExpectMatch::kYes == expectMatch && errors) {
  569. REPORTER_ASSERT(reporter, 0);
  570. }
  571. reporter->bumpTestCount();
  572. return errors == 0;
  573. }
  574. bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
  575. return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
  576. ExpectMatch::kYes);
  577. }
  578. bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
  579. return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
  580. ExpectMatch::kFlaky);
  581. }
  582. bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
  583. bool checkFail) {
  584. return inner_simplify(reporter, path, filename, checkFail ?
  585. ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
  586. }
  587. bool testSimplifyFail(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
  588. return inner_simplify(reporter, path, filename,
  589. ExpectSuccess::kNo, SkipAssert::kYes, ExpectMatch::kNo);
  590. }
  591. #if DEBUG_SHOW_TEST_NAME
  592. static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
  593. SkDebugf("\n");
  594. showPathData(a);
  595. showOp(shapeOp);
  596. showPathData(b);
  597. }
  598. #endif
  599. static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
  600. const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
  601. SkipAssert skipAssert, ExpectMatch expectMatch) {
  602. #if 0 && DEBUG_SHOW_TEST_NAME
  603. showName(a, b, shapeOp);
  604. #endif
  605. if (PathOpsDebug::gJson) {
  606. if (check_for_duplicate_names(testName)) {
  607. return true;
  608. }
  609. if (!PathOpsDebug::gOutFirst) {
  610. fprintf(PathOpsDebug::gOut, ",\n");
  611. }
  612. PathOpsDebug::gOutFirst = false;
  613. fprintf(PathOpsDebug::gOut, "\"%s\": {\n", testName);
  614. json_path_out(a, "p1", "1", false);
  615. json_path_out(b, "p2", "2", false);
  616. fprintf(PathOpsDebug::gOut, " \"op\": \"%s\",\n", opStrs[shapeOp]);
  617. }
  618. SkPath out;
  619. if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
  620. SkDEBUGPARAMS(testName))) {
  621. if (ExpectSuccess::kYes == expectSuccess) {
  622. SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
  623. REPORTER_ASSERT(reporter, 0);
  624. }
  625. if (PathOpsDebug::gJson) {
  626. json_status(expectSuccess, expectMatch, false);
  627. fprintf(PathOpsDebug::gOut, " \"out\": \"\"\n}");
  628. }
  629. return false;
  630. } else {
  631. if (ExpectSuccess::kNo == expectSuccess) {
  632. SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
  633. REPORTER_ASSERT(reporter, 0);
  634. }
  635. if (PathOpsDebug::gJson) {
  636. json_status(expectSuccess, expectMatch, true);
  637. json_path_out(out, "out", "Out", true);
  638. }
  639. }
  640. if (!reporter->verbose()) {
  641. return true;
  642. }
  643. SkPath pathOut, scaledPathOut;
  644. SkRegion rgnA, rgnB, openClip, rgnOut;
  645. openClip.setRect(-16000, -16000, 16000, 16000);
  646. rgnA.setPath(a, openClip);
  647. rgnB.setPath(b, openClip);
  648. rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
  649. rgnOut.getBoundaryPath(&pathOut);
  650. SkMatrix scale;
  651. scaleMatrix(a, b, scale);
  652. SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
  653. SkPath scaledA, scaledB;
  654. scaledA.addPath(a, scale);
  655. scaledA.setFillType(a.getFillType());
  656. scaledB.addPath(b, scale);
  657. scaledB.setFillType(b.getFillType());
  658. scaledRgnA.setPath(scaledA, openClip);
  659. scaledRgnB.setPath(scaledB, openClip);
  660. scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
  661. scaledRgnOut.getBoundaryPath(&scaledPathOut);
  662. SkBitmap bitmap;
  663. SkPath scaledOut;
  664. scaledOut.addPath(out, scale);
  665. scaledOut.setFillType(out.getFillType());
  666. int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
  667. a, b, shapeOp, scale, expectMatch);
  668. reporter->bumpTestCount();
  669. return result == 0;
  670. }
  671. bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
  672. const SkPathOp shapeOp, const char* testName) {
  673. return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
  674. ExpectMatch::kYes);
  675. }
  676. bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
  677. const SkPathOp shapeOp, const char* testName, bool checkFail) {
  678. return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
  679. ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
  680. }
  681. bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
  682. const SkPathOp shapeOp, const char* testName) {
  683. return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
  684. ExpectMatch::kFlaky);
  685. }
  686. bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
  687. const SkPathOp shapeOp, const char* testName) {
  688. #if DEBUG_SHOW_TEST_NAME
  689. showName(a, b, shapeOp);
  690. #endif
  691. SkPath orig;
  692. orig.lineTo(54, 43);
  693. SkPath out = orig;
  694. if (Op(a, b, shapeOp, &out) ) {
  695. SkDebugf("%s test is expected to fail\n", __FUNCTION__);
  696. REPORTER_ASSERT(reporter, 0);
  697. return false;
  698. }
  699. SkASSERT(out == orig);
  700. return true;
  701. }
  702. void initializeTests(skiatest::Reporter* reporter, const char* test) {
  703. static SkMutex& mu = *(new SkMutex);
  704. if (reporter->verbose()) {
  705. SkAutoMutexExclusive lock(mu);
  706. testName = test;
  707. size_t testNameSize = strlen(test);
  708. SkFILEStream inFile("../../experimental/Intersection/op.htm");
  709. if (inFile.isValid()) {
  710. SkTDArray<char> inData;
  711. inData.setCount((int) inFile.getLength());
  712. size_t inLen = inData.count();
  713. inFile.read(inData.begin(), inLen);
  714. inFile.close();
  715. char* insert = strstr(inData.begin(), marker);
  716. if (insert) {
  717. insert += sizeof(marker) - 1;
  718. const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
  719. testNumber = atoi(numLoc) + 1;
  720. }
  721. }
  722. }
  723. }
  724. void PathOpsThreadState::outputProgress(const char* pathStr, SkPath::FillType pathFillType) {
  725. const char testFunction[] = "testSimplify(path);";
  726. const char* pathPrefix = nullptr;
  727. const char* nameSuffix = nullptr;
  728. if (pathFillType == SkPath::kEvenOdd_FillType) {
  729. pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
  730. nameSuffix = "x";
  731. }
  732. appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr);
  733. }
  734. void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) {
  735. const char testFunction[] = "testOp(path);";
  736. SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
  737. const char* nameSuffix = opSuffixes[op];
  738. appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr);
  739. }
  740. void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
  741. void (*firstTest)(skiatest::Reporter* , const char* filename),
  742. void (*skipTest)(skiatest::Reporter* , const char* filename),
  743. void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
  744. size_t index;
  745. if (firstTest) {
  746. index = count - 1;
  747. while (index > 0 && tests[index].fun != firstTest) {
  748. --index;
  749. }
  750. #if DEBUG_SHOW_TEST_NAME
  751. SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
  752. #endif
  753. (*tests[index].fun)(reporter, tests[index].str);
  754. if (tests[index].fun == stopTest) {
  755. return;
  756. }
  757. }
  758. index = reverse ? count - 1 : 0;
  759. size_t last = reverse ? 0 : count - 1;
  760. bool foundSkip = !skipTest;
  761. do {
  762. if (tests[index].fun == skipTest) {
  763. foundSkip = true;
  764. }
  765. if (foundSkip && tests[index].fun != firstTest) {
  766. #if DEBUG_SHOW_TEST_NAME
  767. SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
  768. #endif
  769. (*tests[index].fun)(reporter, tests[index].str);
  770. }
  771. if (tests[index].fun == stopTest || index == last) {
  772. break;
  773. }
  774. index += reverse ? -1 : 1;
  775. } while (true);
  776. #if DEBUG_SHOW_TEST_NAME
  777. SkDebugf(
  778. "\n"
  779. "</div>\n"
  780. "\n"
  781. "<script type=\"text/javascript\">\n"
  782. "\n"
  783. "var testDivs = [\n"
  784. );
  785. index = reverse ? count - 1 : 0;
  786. last = reverse ? 0 : count - 1;
  787. foundSkip = !skipTest;
  788. do {
  789. if (tests[index].fun == skipTest) {
  790. foundSkip = true;
  791. }
  792. if (foundSkip && tests[index].fun != firstTest) {
  793. SkDebugf(" %s,\n", tests[index].str);
  794. }
  795. if (tests[index].fun == stopTest || index == last) {
  796. break;
  797. }
  798. index += reverse ? -1 : 1;
  799. } while (true);
  800. #endif
  801. }