blob: 28830ed702280ea585a3d72a749b1923fcd801ab [file] [log] [blame]
caryclark@google.com818b0cc2013-04-08 11:50:46 +00001/*
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
8#include "PathOpsExtendedTest.h"
caryclark@google.com66089e42013-04-10 15:55:37 +00009#include "PathOpsThreadedCommon.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000012#include "SkForceLinking.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000013#include "SkMatrix.h"
14#include "SkPaint.h"
15#include "SkStream.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +000016#include "SkThreadPool.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000017
18#ifdef SK_BUILD_FOR_MAC
19#include <sys/sysctl.h>
20#endif
21
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000022__SK_FORCE_IMAGE_DECODER_LINKING;
23
caryclark@google.com818b0cc2013-04-08 11:50:46 +000024static const char marker[] =
25 "</div>\n"
26 "\n"
27 "<script type=\"text/javascript\">\n"
28 "\n"
29 "var testDivs = [\n";
30
31static const char* opStrs[] = {
32 "kDifference_PathOp",
33 "kIntersect_PathOp",
34 "kUnion_PathOp",
35 "kXor_PathOp",
caryclark@google.com6dc7df62013-04-25 11:51:54 +000036 "kReverseDifference_PathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000037};
38
39static const char* opSuffixes[] = {
40 "d",
41 "i",
42 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000043 "o",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000044};
45
46static bool gShowPath = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000047static bool gComparePathsAssert = true;
48static bool gPathStrAssert = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000049
caryclark@google.com07e97fc2013-07-08 17:17:02 +000050static const char* gFillTypeStr[] = {
51 "kWinding_FillType",
52 "kEvenOdd_FillType",
53 "kInverseWinding_FillType",
54 "kInverseEvenOdd_FillType"
55};
56
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000057static void output_scalar(SkScalar num) {
58 if (num == (int) num) {
59 SkDebugf("%d", (int) num);
60 } else {
61 SkString str;
62 str.printf("%1.9g", num);
63 int width = str.size();
64 const char* cStr = str.c_str();
65 while (cStr[width - 1] == '0') {
66 --width;
67 }
68 str.resize(width);
69 SkDebugf("%sf", str.c_str());
70 }
71}
72
73static void output_points(const SkPoint* pts, int count) {
74 for (int index = 0; index < count; ++index) {
75 output_scalar(pts[index].fX);
76 SkDebugf(", ");
77 output_scalar(pts[index].fY);
78 if (index + 1 < count) {
79 SkDebugf(", ");
80 }
81 }
82 SkDebugf(");\n");
83}
84
caryclark@google.com07e97fc2013-07-08 17:17:02 +000085static void showPathContours(SkPath::RawIter& iter, const char* pathName) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +000086 uint8_t verb;
87 SkPoint pts[4];
88 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
89 switch (verb) {
90 case SkPath::kMove_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000091 SkDebugf(" %s.moveTo(", pathName);
92 output_points(&pts[0], 1);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000093 continue;
94 case SkPath::kLine_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000095 SkDebugf(" %s.lineTo(", pathName);
96 output_points(&pts[1], 1);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000097 break;
98 case SkPath::kQuad_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000099 SkDebugf(" %s.quadTo(", pathName);
100 output_points(&pts[1], 2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000101 break;
102 case SkPath::kCubic_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000103 SkDebugf(" %s.cubicTo(", pathName);
104 output_points(&pts[1], 3);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000105 break;
106 case SkPath::kClose_Verb:
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000107 SkDebugf(" %s.close();\n", pathName);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000108 break;
109 default:
110 SkDEBUGFAIL("bad verb");
111 return;
112 }
113 }
114}
115
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000116static void showPath(const SkPath& path, const char* pathName, bool includeDeclaration) {
117 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000118#define SUPPORT_RECT_CONTOUR_DETECTION 0
119#if SUPPORT_RECT_CONTOUR_DETECTION
120 int rectCount = path.isRectContours() ? path.rectContours(NULL, NULL) : 0;
121 if (rectCount > 0) {
122 SkTDArray<SkRect> rects;
123 SkTDArray<SkPath::Direction> directions;
124 rects.setCount(rectCount);
125 directions.setCount(rectCount);
126 path.rectContours(rects.begin(), directions.begin());
127 for (int contour = 0; contour < rectCount; ++contour) {
128 const SkRect& rect = rects[contour];
129 SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
130 rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction
131 ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction");
132 }
133 return;
134 }
135#endif
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000136 SkPath::FillType fillType = path.getFillType();
137 SkASSERT(fillType >= SkPath::kWinding_FillType && fillType <= SkPath::kInverseEvenOdd_FillType);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000138 if (includeDeclaration) {
139 SkDebugf(" SkPath %s;\n", pathName);
140 }
141 SkDebugf(" %s.setFillType(SkPath::%s);\n", pathName, gFillTypeStr[fillType]);
142 iter.setPath(path);
143 showPathContours(iter, pathName);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000144}
145
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000146#if DEBUG_SHOW_TEST_NAME
147static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000148 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000149 uint8_t verb;
150 SkPoint pts[4];
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000151 SkPoint firstPt, lastPt;
152 bool firstPtSet = false;
153 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000154 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
155 switch (verb) {
156 case SkPath::kMove_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000157 firstPt = pts[0];
158 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000159 continue;
160 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +0000161 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
162 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000163 lastPt = pts[1];
164 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000165 break;
166 case SkPath::kQuad_Verb:
167 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000168 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000169 lastPt = pts[2];
170 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000171 break;
172 case SkPath::kCubic_Verb:
173 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000174 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
175 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000176 lastPt = pts[3];
177 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000178 break;
179 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000180 if (firstPtSet && lastPtSet && firstPt != lastPt) {
181 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
182 firstPt.fX, firstPt.fY);
183 }
184 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000185 break;
186 default:
187 SkDEBUGFAIL("bad verb");
188 return;
189 }
190 }
191}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000192#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000193
194void showOp(const SkPathOp op) {
195 switch (op) {
196 case kDifference_PathOp:
197 SkDebugf("op difference\n");
198 break;
199 case kIntersect_PathOp:
200 SkDebugf("op intersect\n");
201 break;
202 case kUnion_PathOp:
203 SkDebugf("op union\n");
204 break;
205 case kXOR_PathOp:
206 SkDebugf("op xor\n");
207 break;
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000208 case kReverseDifference_PathOp:
209 SkDebugf("op reverse difference\n");
210 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000211 default:
212 SkASSERT(0);
213 }
214}
215
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000216#if DEBUG_SHOW_TEST_NAME
217
218void ShowFunctionHeader(const char* functionName) {
219 SkDebugf("\nstatic void %s(skiatest::Reporter* reporter) {\n", functionName);
220 if (strcmp("skphealth_com76", functionName) == 0) {
221 SkDebugf("found it\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000222 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000223}
224
225static const char* gOpStrs[] = {
226 "kDifference_PathOp",
227 "kIntersect_PathOp",
228 "kUnion_PathOp",
229 "kXor_PathOp",
230 "kReverseDifference_PathOp",
231};
232
233void ShowOp(SkPathOp op, const char* pathOne, const char* pathTwo) {
234 SkDebugf(" testPathOp(reporter, %s, %s, %s);\n", pathOne, pathTwo, gOpStrs[op]);
235 SkDebugf("}\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000236}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000237#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000238
caryclark@google.com03610322013-04-18 15:58:21 +0000239#if DEBUG_SHOW_TEST_NAME
240static char hexorator(int x) {
241 if (x < 10) {
242 return x + '0';
243 }
244 x -= 10;
245 SkASSERT(x < 26);
246 return x + 'A';
247}
248#endif
249
250void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
251#if DEBUG_SHOW_TEST_NAME
252 state->fSerialNo[0] = hexorator(state->fA);
253 state->fSerialNo[1] = hexorator(state->fB);
254 state->fSerialNo[2] = hexorator(state->fC);
255 state->fSerialNo[3] = hexorator(state->fD);
256 state->fSerialNo[4] = hexorator(a);
257 state->fSerialNo[5] = hexorator(b);
258 state->fSerialNo[6] = hexorator(c);
259 state->fSerialNo[7] = hexorator(d);
260 state->fSerialNo[8] = '\0';
261 SkDebugf("%s\n", state->fSerialNo);
262 if (strcmp(state->fSerialNo, state->fKey) == 0) {
263 SkDebugf("%s\n", state->fPathStr);
264 }
265#endif
266}
267
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000268const int bitWidth = 64;
269const int bitHeight = 64;
270
271static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
272 SkRect larger = one.getBounds();
273 larger.join(two.getBounds());
274 SkScalar largerWidth = larger.width();
275 if (largerWidth < 4) {
276 largerWidth = 4;
277 }
278 SkScalar largerHeight = larger.height();
279 if (largerHeight < 4) {
280 largerHeight = 4;
281 }
282 SkScalar hScale = (bitWidth - 2) / largerWidth;
283 SkScalar vScale = (bitHeight - 2) / largerHeight;
284 scale.reset();
285 scale.preScale(hScale, vScale);
286}
287
288static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
289 int& error2x2) {
290 if (bits.width() == 0) {
291 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
292 bits.allocPixels();
293 }
294 SkCanvas canvas(bits);
295 canvas.drawColor(SK_ColorWHITE);
296 SkPaint paint;
297 canvas.save();
298 const SkRect& bounds1 = scaledOne.getBounds();
299 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
300 canvas.drawPath(scaledOne, paint);
301 canvas.restore();
302 canvas.save();
303 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
304 canvas.drawPath(scaledTwo, paint);
305 canvas.restore();
306 int errors2 = 0;
307 int errors = 0;
308 for (int y = 0; y < bitHeight - 1; ++y) {
309 uint32_t* addr1 = bits.getAddr32(0, y);
310 uint32_t* addr2 = bits.getAddr32(0, y + 1);
311 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
312 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
313 for (int x = 0; x < bitWidth - 1; ++x) {
314 // count 2x2 blocks
315 bool err = addr1[x] != addr3[x];
316 if (err) {
317 errors2 += addr1[x + 1] != addr3[x + 1]
318 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
319 errors++;
320 }
321 }
322 }
323 if (errors2 >= 6 || errors > 160) {
324 SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors);
325 }
326 error2x2 = errors2;
327 return errors;
328}
329
330static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
331 SkPath& scaledTwo, int& error2x2) {
332 SkMatrix scale;
333 scaleMatrix(one, two, scale);
334 one.transform(scale, &scaledOne);
335 two.transform(scale, &scaledTwo);
336 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
337}
338
339bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
340 if (!drawPaths) {
341 return true;
342 }
343 const SkRect& bounds1 = one.getBounds();
344 const SkRect& bounds2 = two.getBounds();
345 SkRect larger = bounds1;
346 larger.join(bounds2);
347 SkBitmap bits;
348 char out[256];
349 int bitWidth = SkScalarCeil(larger.width()) + 2;
350 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
351 return false;
352 }
353 int bitHeight = SkScalarCeil(larger.height()) + 2;
354 if (bitHeight >= (int) sizeof(out)) {
355 return false;
356 }
357 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
358 bits.allocPixels();
359 SkCanvas canvas(bits);
360 canvas.drawColor(SK_ColorWHITE);
361 SkPaint paint;
362 canvas.save();
363 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
364 canvas.drawPath(one, paint);
365 canvas.restore();
366 canvas.save();
367 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
368 canvas.drawPath(two, paint);
369 canvas.restore();
370 for (int y = 0; y < bitHeight; ++y) {
371 uint32_t* addr1 = bits.getAddr32(0, y);
372 int x;
373 char* outPtr = out;
374 for (x = 0; x < bitWidth; ++x) {
375 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
376 }
377 *outPtr++ = '|';
378 for (x = bitWidth; x < bitWidth * 2; ++x) {
379 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
380 }
381 *outPtr++ = '\0';
382 SkDebugf("%s\n", out);
383 }
384 return true;
385}
386
387static void showSimplifiedPath(const SkPath& one, const SkPath& two,
388 const SkPath& scaledOne, const SkPath& scaledTwo) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000389 showPath(one, "path", false);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000390 drawAsciiPaths(scaledOne, scaledTwo, true);
391}
392
393static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& two,
394 SkBitmap& bitmap) {
395 int errors2x2;
396 SkPath scaledOne, scaledTwo;
397 int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
398 if (errors2x2 == 0) {
399 return 0;
400 }
401 const int MAX_ERRORS = 9;
402 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
403 showSimplifiedPath(one, two, scaledOne, scaledTwo);
404 }
405 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
406 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
407 showSimplifiedPath(one, two, scaledOne, scaledTwo);
408 REPORTER_ASSERT(reporter, 0);
409 }
410 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
411}
412
413static void showPathOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b,
414 const SkPath& scaledOne, const SkPath& scaledTwo, const SkPathOp shapeOp,
415 const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000416 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000417 SkDebugf("static void xOp#%s(skiatest::Reporter* reporter) {\n", opSuffixes[shapeOp]);
418 SkDebugf(" SkPath path, pathB;\n");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000419 showPath(a, "path", false);
420 showPath(b, "pathB", false);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000421 SkDebugf(" testPathOp(reporter, path, pathB, %s);\n", opStrs[shapeOp]);
422 SkDebugf("}\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000423 drawAsciiPaths(scaledOne, scaledTwo, true);
424}
425
426static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& scaledOne,
427 const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000428 const SkPath& a, const SkPath& b, const SkPathOp shapeOp,
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000429 const SkMatrix& scale) {
430 int errors2x2;
431 int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
432 if (errors2x2 == 0) {
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000433 if (gShowPath) {
434 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
435 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000436 return 0;
437 }
438 const int MAX_ERRORS = 8;
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000439 if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000440 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
441 }
442 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
443 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
444 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
445 REPORTER_ASSERT(reporter, 0);
446 }
447 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
448}
449
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000450// Default values for when reporter->verbose() is false.
451static int testNumber = 1;
452static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000453
454static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
455 outFile.writeText(testName);
456 outFile.writeDecAsText(testNumber);
457 if (nameSuffix) {
458 outFile.writeText(nameSuffix);
459 }
460}
461
462static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
463 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
464 outFile.writeText("<div id=\"");
465 writeTestName(nameSuffix, outFile);
466 outFile.writeText("\">\n");
467 if (pathPrefix) {
468 outFile.writeText(pathPrefix);
469 }
470 outFile.writeText(pathStr);
471 outFile.writeText("</div>\n\n");
472
473 outFile.writeText(marker);
474 outFile.writeText(" ");
475 writeTestName(nameSuffix, outFile);
476 outFile.writeText(",\n\n\n");
477
478 outFile.writeText("static void ");
479 writeTestName(nameSuffix, outFile);
480 outFile.writeText("() {\n SkPath path");
481 if (twoPaths) {
482 outFile.writeText(", pathB");
483 }
484 outFile.writeText(";\n");
485 if (pathPrefix) {
486 outFile.writeText(pathPrefix);
487 }
488 outFile.writeText(pathStr);
489 outFile.writeText(" ");
490 outFile.writeText(testFunction);
491 outFile.writeText("\n}\n\n");
492 outFile.writeText("static void (*firstTest)() = ");
493 writeTestName(nameSuffix, outFile);
494 outFile.writeText(";\n\n");
495
496 outFile.writeText("static struct {\n");
497 outFile.writeText(" void (*fun)();\n");
498 outFile.writeText(" const char* str;\n");
499 outFile.writeText("} tests[] = {\n");
500 outFile.writeText(" TEST(");
501 writeTestName(nameSuffix, outFile);
502 outFile.writeText("),\n");
503 outFile.flush();
504}
505
506bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
507 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000508 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
509 path.setFillType(fillType);
510 if (gShowPath) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000511 showPath(path, "path", false);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000512 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000513 if (!Simplify(path, &out)) {
514 SkDebugf("%s did not expect failure\n", __FUNCTION__);
515 REPORTER_ASSERT(state.fReporter, 0);
516 return false;
517 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000518 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000519 return true;
520 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000521 int result = comparePaths(state.fReporter, path, out, *state.fBitmap);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000522 if (result && gPathStrAssert) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000523 char temp[8192];
524 sk_bzero(temp, sizeof(temp));
525 SkMemoryWStream stream(temp, sizeof(temp));
526 const char* pathPrefix = NULL;
527 const char* nameSuffix = NULL;
528 if (fillType == SkPath::kEvenOdd_FillType) {
529 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
530 nameSuffix = "x";
531 }
532 const char testFunction[] = "testSimplifyx(path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000533 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000534 SkDebugf(temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000535 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000536 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000537 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000538 return result == 0;
539}
540
541bool testSimplify(skiatest::Reporter* reporter, const SkPath& path) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000542#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000543 showPathData(path);
544#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000545 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000546 if (!Simplify(path, &out)) {
547 SkDebugf("%s did not expect failure\n", __FUNCTION__);
548 REPORTER_ASSERT(reporter, 0);
549 return false;
550 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000551 SkBitmap bitmap;
552 int result = comparePaths(reporter, path, out, bitmap);
553 if (result && gPathStrAssert) {
554 REPORTER_ASSERT(reporter, 0);
555 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000556 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000557 return result == 0;
558}
559
caryclark@google.coma5e55922013-05-07 18:51:31 +0000560#if DEBUG_SHOW_TEST_NAME
caryclark@google.com570863f2013-09-16 15:55:01 +0000561void SkPathOpsDebug::ShowPath(const SkPath& a, const SkPath& b, SkPathOp shapeOp,
562 const char* testName) {
563 ShowFunctionHeader(testName);
564 showPath(a, "path", true);
565 showPath(b, "pathB", true);
566 ShowOp(shapeOp, "path", "pathB");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000567}
568#endif
569
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000570static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
571 const SkPathOp shapeOp, const char* testName, bool threaded) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000572#if DEBUG_SHOW_TEST_NAME
573 if (testName == NULL) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000574 SkDebugf("\n");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000575 showPathData(a);
576 showOp(shapeOp);
577 showPathData(b);
578 } else {
caryclark@google.com570863f2013-09-16 15:55:01 +0000579 SkPathOpsDebug::ShowPath(a, b, shapeOp, testName);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000580 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000581#endif
582 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000583 if (!Op(a, b, shapeOp, &out) ) {
584 SkDebugf("%s did not expect failure\n", __FUNCTION__);
585 REPORTER_ASSERT(reporter, 0);
586 return false;
587 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000588 if (threaded && !reporter->verbose()) {
589 return true;
590 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000591 SkPath pathOut, scaledPathOut;
592 SkRegion rgnA, rgnB, openClip, rgnOut;
593 openClip.setRect(-16000, -16000, 16000, 16000);
594 rgnA.setPath(a, openClip);
595 rgnB.setPath(b, openClip);
596 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
597 rgnOut.getBoundaryPath(&pathOut);
598
599 SkMatrix scale;
600 scaleMatrix(a, b, scale);
601 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
602 SkPath scaledA, scaledB;
603 scaledA.addPath(a, scale);
604 scaledA.setFillType(a.getFillType());
605 scaledB.addPath(b, scale);
606 scaledB.setFillType(b.getFillType());
607 scaledRgnA.setPath(scaledA, openClip);
608 scaledRgnB.setPath(scaledB, openClip);
609 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
610 scaledRgnOut.getBoundaryPath(&scaledPathOut);
611 SkBitmap bitmap;
612 SkPath scaledOut;
613 scaledOut.addPath(out, scale);
614 scaledOut.setFillType(out.getFillType());
615 int result = comparePaths(reporter, pathOut, scaledPathOut, out, scaledOut, bitmap, a, b,
616 shapeOp, scale);
617 if (result && gPathStrAssert) {
618 REPORTER_ASSERT(reporter, 0);
619 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000620 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000621 return result == 0;
622}
623
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000624bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
625 const SkPathOp shapeOp, const char* testName) {
626 return innerPathOp(reporter, a, b, shapeOp, testName, false);
627}
628
629bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
630 const SkPathOp shapeOp, const char* testName) {
631 return innerPathOp(reporter, a, b, shapeOp, testName, true);
632}
633
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000634SK_DECLARE_STATIC_MUTEX(gMutex);
635
caryclark@google.com16cfe402013-04-18 18:47:37 +0000636int initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000637#ifdef SK_DEBUG
caryclark@google.com570863f2013-09-16 15:55:01 +0000638 SkPathOpsDebug::gMaxWindSum = 4;
639 SkPathOpsDebug::gMaxWindValue = 4;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000640#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000641 if (reporter->verbose()) {
642 SkAutoMutexAcquire lock(gMutex);
643 testName = test;
644 size_t testNameSize = strlen(test);
645 SkFILEStream inFile("../../experimental/Intersection/op.htm");
646 if (inFile.isValid()) {
647 SkTDArray<char> inData;
648 inData.setCount(inFile.getLength());
649 size_t inLen = inData.count();
650 inFile.read(inData.begin(), inLen);
651 inFile.setPath(NULL);
652 char* insert = strstr(inData.begin(), marker);
653 if (insert) {
654 insert += sizeof(marker) - 1;
655 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
656 testNumber = atoi(numLoc) + 1;
657 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000658 }
659 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000660 return reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 1;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000661}
662
caryclark@google.com66089e42013-04-10 15:55:37 +0000663void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
664 const char testFunction[] = "testSimplify(path);";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000665 const char* pathPrefix = NULL;
666 const char* nameSuffix = NULL;
667 if (pathFillType == SkPath::kEvenOdd_FillType) {
668 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
669 nameSuffix = "x";
670 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000671 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
672 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000673}
674
caryclark@google.com66089e42013-04-10 15:55:37 +0000675void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
676 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000677 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000678 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000679 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
680 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000681}
682
683void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
684 void (*firstTest)(skiatest::Reporter* ),
685 void (*stopTest)(skiatest::Reporter* ), bool reverse) {
686 size_t index;
687 if (firstTest) {
688 index = count - 1;
689 while (index > 0 && tests[index].fun != firstTest) {
690 --index;
691 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000692#if DEBUG_SHOW_TEST_NAME
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000693 SkDebugf("<div id=\"%s\">\n", tests[index].str);
694 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
695#endif
696 (*tests[index].fun)(reporter);
697 }
698 index = reverse ? count - 1 : 0;
699 size_t last = reverse ? 0 : count - 1;
700 do {
701 if (tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000702 #if DEBUG_SHOW_TEST_NAME
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000703 SkDebugf("<div id=\"%s\">\n", tests[index].str);
704 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
705 #endif
706 (*tests[index].fun)(reporter);
707 }
708 if (tests[index].fun == stopTest) {
709 SkDebugf("lastTest\n");
710 }
711 if (index == last) {
712 break;
713 }
714 index += reverse ? -1 : 1;
715 } while (true);
716}