blob: cee27e458899ed296c9fb375ccfc53d5bbd8146b [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"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000015#include "SkRTConf.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000016#include "SkStream.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +000017#include "SkThreadPool.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000018
19#ifdef SK_BUILD_FOR_MAC
20#include <sys/sysctl.h>
21#endif
22
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000023__SK_FORCE_IMAGE_DECODER_LINKING;
24
caryclark@google.com818b0cc2013-04-08 11:50:46 +000025static const char marker[] =
26 "</div>\n"
27 "\n"
28 "<script type=\"text/javascript\">\n"
29 "\n"
30 "var testDivs = [\n";
31
32static const char* opStrs[] = {
33 "kDifference_PathOp",
34 "kIntersect_PathOp",
35 "kUnion_PathOp",
36 "kXor_PathOp",
caryclark@google.com6dc7df62013-04-25 11:51:54 +000037 "kReverseDifference_PathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000038};
39
40static const char* opSuffixes[] = {
41 "d",
42 "i",
43 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000044 "o",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000045};
46
47static bool gShowPath = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000048static bool gComparePathsAssert = true;
49static bool gPathStrAssert = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000050
caryclark@google.com07e97fc2013-07-08 17:17:02 +000051static const char* gFillTypeStr[] = {
52 "kWinding_FillType",
53 "kEvenOdd_FillType",
54 "kInverseWinding_FillType",
55 "kInverseEvenOdd_FillType"
56};
57
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000058static void output_scalar(SkScalar num) {
59 if (num == (int) num) {
60 SkDebugf("%d", (int) num);
61 } else {
62 SkString str;
63 str.printf("%1.9g", num);
64 int width = str.size();
65 const char* cStr = str.c_str();
66 while (cStr[width - 1] == '0') {
67 --width;
68 }
69 str.resize(width);
70 SkDebugf("%sf", str.c_str());
71 }
72}
73
74static void output_points(const SkPoint* pts, int count) {
75 for (int index = 0; index < count; ++index) {
76 output_scalar(pts[index].fX);
77 SkDebugf(", ");
78 output_scalar(pts[index].fY);
79 if (index + 1 < count) {
80 SkDebugf(", ");
81 }
82 }
83 SkDebugf(");\n");
84}
85
caryclark@google.com07e97fc2013-07-08 17:17:02 +000086static void showPathContours(SkPath::RawIter& iter, const char* pathName) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +000087 uint8_t verb;
88 SkPoint pts[4];
89 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
90 switch (verb) {
91 case SkPath::kMove_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000092 SkDebugf(" %s.moveTo(", pathName);
93 output_points(&pts[0], 1);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000094 continue;
95 case SkPath::kLine_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000096 SkDebugf(" %s.lineTo(", pathName);
97 output_points(&pts[1], 1);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000098 break;
99 case SkPath::kQuad_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000100 SkDebugf(" %s.quadTo(", pathName);
101 output_points(&pts[1], 2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000102 break;
103 case SkPath::kCubic_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000104 SkDebugf(" %s.cubicTo(", pathName);
105 output_points(&pts[1], 3);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000106 break;
107 case SkPath::kClose_Verb:
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000108 SkDebugf(" %s.close();\n", pathName);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000109 break;
110 default:
111 SkDEBUGFAIL("bad verb");
112 return;
113 }
114 }
115}
116
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000117static void showPath(const SkPath& path, const char* pathName, bool includeDeclaration) {
118 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000119#define SUPPORT_RECT_CONTOUR_DETECTION 0
120#if SUPPORT_RECT_CONTOUR_DETECTION
121 int rectCount = path.isRectContours() ? path.rectContours(NULL, NULL) : 0;
122 if (rectCount > 0) {
123 SkTDArray<SkRect> rects;
124 SkTDArray<SkPath::Direction> directions;
125 rects.setCount(rectCount);
126 directions.setCount(rectCount);
127 path.rectContours(rects.begin(), directions.begin());
128 for (int contour = 0; contour < rectCount; ++contour) {
129 const SkRect& rect = rects[contour];
130 SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
131 rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction
132 ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction");
133 }
134 return;
135 }
136#endif
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000137 SkPath::FillType fillType = path.getFillType();
138 SkASSERT(fillType >= SkPath::kWinding_FillType && fillType <= SkPath::kInverseEvenOdd_FillType);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000139 if (includeDeclaration) {
140 SkDebugf(" SkPath %s;\n", pathName);
141 }
142 SkDebugf(" %s.setFillType(SkPath::%s);\n", pathName, gFillTypeStr[fillType]);
143 iter.setPath(path);
144 showPathContours(iter, pathName);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000145}
146
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000147#if DEBUG_SHOW_TEST_NAME
148static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000149 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000150 uint8_t verb;
151 SkPoint pts[4];
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000152 SkPoint firstPt, lastPt;
153 bool firstPtSet = false;
154 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000155 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
156 switch (verb) {
157 case SkPath::kMove_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000158 firstPt = pts[0];
159 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000160 continue;
161 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +0000162 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
163 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000164 lastPt = pts[1];
165 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000166 break;
167 case SkPath::kQuad_Verb:
168 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000169 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 +0000170 lastPt = pts[2];
171 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000172 break;
173 case SkPath::kCubic_Verb:
174 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 +0000175 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
176 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000177 lastPt = pts[3];
178 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000179 break;
180 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000181 if (firstPtSet && lastPtSet && firstPt != lastPt) {
182 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
183 firstPt.fX, firstPt.fY);
184 }
185 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000186 break;
187 default:
188 SkDEBUGFAIL("bad verb");
189 return;
190 }
191 }
192}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000193#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000194
195void showOp(const SkPathOp op) {
196 switch (op) {
197 case kDifference_PathOp:
198 SkDebugf("op difference\n");
199 break;
200 case kIntersect_PathOp:
201 SkDebugf("op intersect\n");
202 break;
203 case kUnion_PathOp:
204 SkDebugf("op union\n");
205 break;
206 case kXOR_PathOp:
207 SkDebugf("op xor\n");
208 break;
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000209 case kReverseDifference_PathOp:
210 SkDebugf("op reverse difference\n");
211 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000212 default:
213 SkASSERT(0);
214 }
215}
216
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000217#if DEBUG_SHOW_TEST_NAME
218
219void ShowFunctionHeader(const char* functionName) {
220 SkDebugf("\nstatic void %s(skiatest::Reporter* reporter) {\n", functionName);
221 if (strcmp("skphealth_com76", functionName) == 0) {
222 SkDebugf("found it\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000223 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000224}
225
226static const char* gOpStrs[] = {
227 "kDifference_PathOp",
228 "kIntersect_PathOp",
229 "kUnion_PathOp",
230 "kXor_PathOp",
231 "kReverseDifference_PathOp",
232};
233
234void ShowOp(SkPathOp op, const char* pathOne, const char* pathTwo) {
235 SkDebugf(" testPathOp(reporter, %s, %s, %s);\n", pathOne, pathTwo, gOpStrs[op]);
236 SkDebugf("}\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000237}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000238#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000239
caryclark@google.com03610322013-04-18 15:58:21 +0000240#if DEBUG_SHOW_TEST_NAME
241static char hexorator(int x) {
242 if (x < 10) {
243 return x + '0';
244 }
245 x -= 10;
246 SkASSERT(x < 26);
247 return x + 'A';
248}
249#endif
250
251void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
252#if DEBUG_SHOW_TEST_NAME
253 state->fSerialNo[0] = hexorator(state->fA);
254 state->fSerialNo[1] = hexorator(state->fB);
255 state->fSerialNo[2] = hexorator(state->fC);
256 state->fSerialNo[3] = hexorator(state->fD);
257 state->fSerialNo[4] = hexorator(a);
258 state->fSerialNo[5] = hexorator(b);
259 state->fSerialNo[6] = hexorator(c);
260 state->fSerialNo[7] = hexorator(d);
261 state->fSerialNo[8] = '\0';
262 SkDebugf("%s\n", state->fSerialNo);
263 if (strcmp(state->fSerialNo, state->fKey) == 0) {
264 SkDebugf("%s\n", state->fPathStr);
265 }
266#endif
267}
268
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000269const int bitWidth = 64;
270const int bitHeight = 64;
271
272static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
273 SkRect larger = one.getBounds();
274 larger.join(two.getBounds());
275 SkScalar largerWidth = larger.width();
276 if (largerWidth < 4) {
277 largerWidth = 4;
278 }
279 SkScalar largerHeight = larger.height();
280 if (largerHeight < 4) {
281 largerHeight = 4;
282 }
283 SkScalar hScale = (bitWidth - 2) / largerWidth;
284 SkScalar vScale = (bitHeight - 2) / largerHeight;
285 scale.reset();
286 scale.preScale(hScale, vScale);
287}
288
289static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
290 int& error2x2) {
291 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000292 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000293 }
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];
reed@google.come1ca7052013-12-17 19:22:07 +0000349 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000350 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
351 return false;
352 }
reed@google.come1ca7052013-12-17 19:22:07 +0000353 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000354 if (bitHeight >= (int) sizeof(out)) {
355 return false;
356 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000357 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000358 SkCanvas canvas(bits);
359 canvas.drawColor(SK_ColorWHITE);
360 SkPaint paint;
361 canvas.save();
362 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
363 canvas.drawPath(one, paint);
364 canvas.restore();
365 canvas.save();
366 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
367 canvas.drawPath(two, paint);
368 canvas.restore();
369 for (int y = 0; y < bitHeight; ++y) {
370 uint32_t* addr1 = bits.getAddr32(0, y);
371 int x;
372 char* outPtr = out;
373 for (x = 0; x < bitWidth; ++x) {
374 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
375 }
376 *outPtr++ = '|';
377 for (x = bitWidth; x < bitWidth * 2; ++x) {
378 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
379 }
380 *outPtr++ = '\0';
381 SkDebugf("%s\n", out);
382 }
383 return true;
384}
385
386static void showSimplifiedPath(const SkPath& one, const SkPath& two,
387 const SkPath& scaledOne, const SkPath& scaledTwo) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000388 showPath(one, "path", false);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000389 drawAsciiPaths(scaledOne, scaledTwo, true);
390}
391
392static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& two,
393 SkBitmap& bitmap) {
394 int errors2x2;
395 SkPath scaledOne, scaledTwo;
396 int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
397 if (errors2x2 == 0) {
398 return 0;
399 }
400 const int MAX_ERRORS = 9;
401 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
402 showSimplifiedPath(one, two, scaledOne, scaledTwo);
403 }
404 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
405 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
406 showSimplifiedPath(one, two, scaledOne, scaledTwo);
407 REPORTER_ASSERT(reporter, 0);
408 }
409 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
410}
411
412static void showPathOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b,
413 const SkPath& scaledOne, const SkPath& scaledTwo, const SkPathOp shapeOp,
414 const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000415 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000416 SkDebugf("static void xOp#%s(skiatest::Reporter* reporter) {\n", opSuffixes[shapeOp]);
417 SkDebugf(" SkPath path, pathB;\n");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000418 showPath(a, "path", false);
419 showPath(b, "pathB", false);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000420 SkDebugf(" testPathOp(reporter, path, pathB, %s);\n", opStrs[shapeOp]);
421 SkDebugf("}\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000422 drawAsciiPaths(scaledOne, scaledTwo, true);
423}
424
425static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& scaledOne,
426 const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000427 const SkPath& a, const SkPath& b, const SkPathOp shapeOp,
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000428 const SkMatrix& scale) {
429 int errors2x2;
430 int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
431 if (errors2x2 == 0) {
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000432 if (gShowPath) {
433 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
434 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000435 return 0;
436 }
437 const int MAX_ERRORS = 8;
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000438 if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000439 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
440 }
441 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
442 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
443 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
444 REPORTER_ASSERT(reporter, 0);
445 }
446 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
447}
448
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000449// Default values for when reporter->verbose() is false.
450static int testNumber = 1;
451static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000452
453static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
454 outFile.writeText(testName);
455 outFile.writeDecAsText(testNumber);
456 if (nameSuffix) {
457 outFile.writeText(nameSuffix);
458 }
459}
460
461static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
462 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
463 outFile.writeText("<div id=\"");
464 writeTestName(nameSuffix, outFile);
465 outFile.writeText("\">\n");
466 if (pathPrefix) {
467 outFile.writeText(pathPrefix);
468 }
469 outFile.writeText(pathStr);
470 outFile.writeText("</div>\n\n");
471
472 outFile.writeText(marker);
473 outFile.writeText(" ");
474 writeTestName(nameSuffix, outFile);
475 outFile.writeText(",\n\n\n");
476
477 outFile.writeText("static void ");
478 writeTestName(nameSuffix, outFile);
479 outFile.writeText("() {\n SkPath path");
480 if (twoPaths) {
481 outFile.writeText(", pathB");
482 }
483 outFile.writeText(";\n");
484 if (pathPrefix) {
485 outFile.writeText(pathPrefix);
486 }
487 outFile.writeText(pathStr);
488 outFile.writeText(" ");
489 outFile.writeText(testFunction);
490 outFile.writeText("\n}\n\n");
491 outFile.writeText("static void (*firstTest)() = ");
492 writeTestName(nameSuffix, outFile);
493 outFile.writeText(";\n\n");
494
495 outFile.writeText("static struct {\n");
496 outFile.writeText(" void (*fun)();\n");
497 outFile.writeText(" const char* str;\n");
498 outFile.writeText("} tests[] = {\n");
499 outFile.writeText(" TEST(");
500 writeTestName(nameSuffix, outFile);
501 outFile.writeText("),\n");
502 outFile.flush();
503}
504
505bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
506 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000507 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
508 path.setFillType(fillType);
509 if (gShowPath) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000510 showPath(path, "path", false);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000511 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000512 if (!Simplify(path, &out)) {
513 SkDebugf("%s did not expect failure\n", __FUNCTION__);
514 REPORTER_ASSERT(state.fReporter, 0);
515 return false;
516 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000517 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000518 return true;
519 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000520 int result = comparePaths(state.fReporter, path, out, *state.fBitmap);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000521 if (result && gPathStrAssert) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000522 char temp[8192];
523 sk_bzero(temp, sizeof(temp));
524 SkMemoryWStream stream(temp, sizeof(temp));
525 const char* pathPrefix = NULL;
526 const char* nameSuffix = NULL;
527 if (fillType == SkPath::kEvenOdd_FillType) {
528 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
529 nameSuffix = "x";
530 }
531 const char testFunction[] = "testSimplifyx(path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000532 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000533 SkDebugf(temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000534 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000535 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000536 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000537 return result == 0;
538}
539
540bool testSimplify(skiatest::Reporter* reporter, const SkPath& path) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000541#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000542 showPathData(path);
543#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000544 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000545 if (!Simplify(path, &out)) {
546 SkDebugf("%s did not expect failure\n", __FUNCTION__);
547 REPORTER_ASSERT(reporter, 0);
548 return false;
549 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000550 SkBitmap bitmap;
551 int result = comparePaths(reporter, path, out, bitmap);
552 if (result && gPathStrAssert) {
553 REPORTER_ASSERT(reporter, 0);
554 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000555 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000556 return result == 0;
557}
558
caryclark@google.coma5e55922013-05-07 18:51:31 +0000559#if DEBUG_SHOW_TEST_NAME
caryclark@google.com570863f2013-09-16 15:55:01 +0000560void SkPathOpsDebug::ShowPath(const SkPath& a, const SkPath& b, SkPathOp shapeOp,
561 const char* testName) {
562 ShowFunctionHeader(testName);
563 showPath(a, "path", true);
564 showPath(b, "pathB", true);
565 ShowOp(shapeOp, "path", "pathB");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000566}
567#endif
568
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000569static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
570 const SkPathOp shapeOp, const char* testName, bool threaded) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000571#if DEBUG_SHOW_TEST_NAME
572 if (testName == NULL) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000573 SkDebugf("\n");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000574 showPathData(a);
575 showOp(shapeOp);
576 showPathData(b);
577 } else {
caryclark@google.com570863f2013-09-16 15:55:01 +0000578 SkPathOpsDebug::ShowPath(a, b, shapeOp, testName);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000579 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000580#endif
581 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000582 if (!Op(a, b, shapeOp, &out) ) {
583 SkDebugf("%s did not expect failure\n", __FUNCTION__);
584 REPORTER_ASSERT(reporter, 0);
585 return false;
586 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000587 if (threaded && !reporter->verbose()) {
588 return true;
589 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000590 SkPath pathOut, scaledPathOut;
591 SkRegion rgnA, rgnB, openClip, rgnOut;
592 openClip.setRect(-16000, -16000, 16000, 16000);
593 rgnA.setPath(a, openClip);
594 rgnB.setPath(b, openClip);
595 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
596 rgnOut.getBoundaryPath(&pathOut);
597
598 SkMatrix scale;
599 scaleMatrix(a, b, scale);
600 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
601 SkPath scaledA, scaledB;
602 scaledA.addPath(a, scale);
603 scaledA.setFillType(a.getFillType());
604 scaledB.addPath(b, scale);
605 scaledB.setFillType(b.getFillType());
606 scaledRgnA.setPath(scaledA, openClip);
607 scaledRgnB.setPath(scaledB, openClip);
608 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
609 scaledRgnOut.getBoundaryPath(&scaledPathOut);
610 SkBitmap bitmap;
611 SkPath scaledOut;
612 scaledOut.addPath(out, scale);
613 scaledOut.setFillType(out.getFillType());
614 int result = comparePaths(reporter, pathOut, scaledPathOut, out, scaledOut, bitmap, a, b,
615 shapeOp, scale);
616 if (result && gPathStrAssert) {
617 REPORTER_ASSERT(reporter, 0);
618 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000619 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000620 return result == 0;
621}
622
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000623bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
624 const SkPathOp shapeOp, const char* testName) {
625 return innerPathOp(reporter, a, b, shapeOp, testName, false);
626}
627
628bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
629 const SkPathOp shapeOp, const char* testName) {
630 return innerPathOp(reporter, a, b, shapeOp, testName, true);
631}
632
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000633SK_DECLARE_STATIC_MUTEX(gMutex);
634
caryclark@google.com16cfe402013-04-18 18:47:37 +0000635int initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000636#if 0 // doesn't work yet
637 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
638 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
639#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000640#ifdef SK_DEBUG
caryclark@google.com570863f2013-09-16 15:55:01 +0000641 SkPathOpsDebug::gMaxWindSum = 4;
642 SkPathOpsDebug::gMaxWindValue = 4;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000643#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000644 if (reporter->verbose()) {
645 SkAutoMutexAcquire lock(gMutex);
646 testName = test;
647 size_t testNameSize = strlen(test);
648 SkFILEStream inFile("../../experimental/Intersection/op.htm");
649 if (inFile.isValid()) {
650 SkTDArray<char> inData;
651 inData.setCount(inFile.getLength());
652 size_t inLen = inData.count();
653 inFile.read(inData.begin(), inLen);
654 inFile.setPath(NULL);
655 char* insert = strstr(inData.begin(), marker);
656 if (insert) {
657 insert += sizeof(marker) - 1;
658 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
659 testNumber = atoi(numLoc) + 1;
660 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000661 }
662 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000663 return reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 1;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000664}
665
caryclark@google.com66089e42013-04-10 15:55:37 +0000666void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
667 const char testFunction[] = "testSimplify(path);";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000668 const char* pathPrefix = NULL;
669 const char* nameSuffix = NULL;
670 if (pathFillType == SkPath::kEvenOdd_FillType) {
671 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
672 nameSuffix = "x";
673 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000674 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
675 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000676}
677
caryclark@google.com66089e42013-04-10 15:55:37 +0000678void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
679 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000680 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000681 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000682 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
683 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000684}
685
686void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
687 void (*firstTest)(skiatest::Reporter* ),
688 void (*stopTest)(skiatest::Reporter* ), bool reverse) {
689 size_t index;
690 if (firstTest) {
691 index = count - 1;
692 while (index > 0 && tests[index].fun != firstTest) {
693 --index;
694 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000695#if DEBUG_SHOW_TEST_NAME
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000696 SkDebugf("<div id=\"%s\">\n", tests[index].str);
697 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
698#endif
699 (*tests[index].fun)(reporter);
700 }
701 index = reverse ? count - 1 : 0;
702 size_t last = reverse ? 0 : count - 1;
703 do {
704 if (tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000705 #if DEBUG_SHOW_TEST_NAME
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000706 SkDebugf("<div id=\"%s\">\n", tests[index].str);
707 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
708 #endif
709 (*tests[index].fun)(reporter);
710 }
711 if (tests[index].fun == stopTest) {
712 SkDebugf("lastTest\n");
713 }
714 if (index == last) {
715 break;
716 }
717 index += reverse ? -1 : 1;
718 } while (true);
719}