blob: 1083a897e15657c35bb6b7b3a36c728377712477 [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) {
292 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
293 bits.allocPixels();
294 }
295 SkCanvas canvas(bits);
296 canvas.drawColor(SK_ColorWHITE);
297 SkPaint paint;
298 canvas.save();
299 const SkRect& bounds1 = scaledOne.getBounds();
300 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
301 canvas.drawPath(scaledOne, paint);
302 canvas.restore();
303 canvas.save();
304 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
305 canvas.drawPath(scaledTwo, paint);
306 canvas.restore();
307 int errors2 = 0;
308 int errors = 0;
309 for (int y = 0; y < bitHeight - 1; ++y) {
310 uint32_t* addr1 = bits.getAddr32(0, y);
311 uint32_t* addr2 = bits.getAddr32(0, y + 1);
312 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
313 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
314 for (int x = 0; x < bitWidth - 1; ++x) {
315 // count 2x2 blocks
316 bool err = addr1[x] != addr3[x];
317 if (err) {
318 errors2 += addr1[x + 1] != addr3[x + 1]
319 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
320 errors++;
321 }
322 }
323 }
324 if (errors2 >= 6 || errors > 160) {
325 SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors);
326 }
327 error2x2 = errors2;
328 return errors;
329}
330
331static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
332 SkPath& scaledTwo, int& error2x2) {
333 SkMatrix scale;
334 scaleMatrix(one, two, scale);
335 one.transform(scale, &scaledOne);
336 two.transform(scale, &scaledTwo);
337 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
338}
339
340bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
341 if (!drawPaths) {
342 return true;
343 }
344 const SkRect& bounds1 = one.getBounds();
345 const SkRect& bounds2 = two.getBounds();
346 SkRect larger = bounds1;
347 larger.join(bounds2);
348 SkBitmap bits;
349 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000350 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000351 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
352 return false;
353 }
reed@google.come1ca7052013-12-17 19:22:07 +0000354 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000355 if (bitHeight >= (int) sizeof(out)) {
356 return false;
357 }
358 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
359 bits.allocPixels();
360 SkCanvas canvas(bits);
361 canvas.drawColor(SK_ColorWHITE);
362 SkPaint paint;
363 canvas.save();
364 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
365 canvas.drawPath(one, paint);
366 canvas.restore();
367 canvas.save();
368 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
369 canvas.drawPath(two, paint);
370 canvas.restore();
371 for (int y = 0; y < bitHeight; ++y) {
372 uint32_t* addr1 = bits.getAddr32(0, y);
373 int x;
374 char* outPtr = out;
375 for (x = 0; x < bitWidth; ++x) {
376 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
377 }
378 *outPtr++ = '|';
379 for (x = bitWidth; x < bitWidth * 2; ++x) {
380 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
381 }
382 *outPtr++ = '\0';
383 SkDebugf("%s\n", out);
384 }
385 return true;
386}
387
388static void showSimplifiedPath(const SkPath& one, const SkPath& two,
389 const SkPath& scaledOne, const SkPath& scaledTwo) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000390 showPath(one, "path", false);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000391 drawAsciiPaths(scaledOne, scaledTwo, true);
392}
393
394static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& two,
395 SkBitmap& bitmap) {
396 int errors2x2;
397 SkPath scaledOne, scaledTwo;
398 int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
399 if (errors2x2 == 0) {
400 return 0;
401 }
402 const int MAX_ERRORS = 9;
403 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
404 showSimplifiedPath(one, two, scaledOne, scaledTwo);
405 }
406 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
407 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
408 showSimplifiedPath(one, two, scaledOne, scaledTwo);
409 REPORTER_ASSERT(reporter, 0);
410 }
411 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
412}
413
414static void showPathOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b,
415 const SkPath& scaledOne, const SkPath& scaledTwo, const SkPathOp shapeOp,
416 const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000417 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000418 SkDebugf("static void xOp#%s(skiatest::Reporter* reporter) {\n", opSuffixes[shapeOp]);
419 SkDebugf(" SkPath path, pathB;\n");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000420 showPath(a, "path", false);
421 showPath(b, "pathB", false);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000422 SkDebugf(" testPathOp(reporter, path, pathB, %s);\n", opStrs[shapeOp]);
423 SkDebugf("}\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000424 drawAsciiPaths(scaledOne, scaledTwo, true);
425}
426
427static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& scaledOne,
428 const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000429 const SkPath& a, const SkPath& b, const SkPathOp shapeOp,
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000430 const SkMatrix& scale) {
431 int errors2x2;
432 int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
433 if (errors2x2 == 0) {
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000434 if (gShowPath) {
435 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
436 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000437 return 0;
438 }
439 const int MAX_ERRORS = 8;
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000440 if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000441 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
442 }
443 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
444 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
445 showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
446 REPORTER_ASSERT(reporter, 0);
447 }
448 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
449}
450
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000451// Default values for when reporter->verbose() is false.
452static int testNumber = 1;
453static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000454
455static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
456 outFile.writeText(testName);
457 outFile.writeDecAsText(testNumber);
458 if (nameSuffix) {
459 outFile.writeText(nameSuffix);
460 }
461}
462
463static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
464 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
465 outFile.writeText("<div id=\"");
466 writeTestName(nameSuffix, outFile);
467 outFile.writeText("\">\n");
468 if (pathPrefix) {
469 outFile.writeText(pathPrefix);
470 }
471 outFile.writeText(pathStr);
472 outFile.writeText("</div>\n\n");
473
474 outFile.writeText(marker);
475 outFile.writeText(" ");
476 writeTestName(nameSuffix, outFile);
477 outFile.writeText(",\n\n\n");
478
479 outFile.writeText("static void ");
480 writeTestName(nameSuffix, outFile);
481 outFile.writeText("() {\n SkPath path");
482 if (twoPaths) {
483 outFile.writeText(", pathB");
484 }
485 outFile.writeText(";\n");
486 if (pathPrefix) {
487 outFile.writeText(pathPrefix);
488 }
489 outFile.writeText(pathStr);
490 outFile.writeText(" ");
491 outFile.writeText(testFunction);
492 outFile.writeText("\n}\n\n");
493 outFile.writeText("static void (*firstTest)() = ");
494 writeTestName(nameSuffix, outFile);
495 outFile.writeText(";\n\n");
496
497 outFile.writeText("static struct {\n");
498 outFile.writeText(" void (*fun)();\n");
499 outFile.writeText(" const char* str;\n");
500 outFile.writeText("} tests[] = {\n");
501 outFile.writeText(" TEST(");
502 writeTestName(nameSuffix, outFile);
503 outFile.writeText("),\n");
504 outFile.flush();
505}
506
507bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
508 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000509 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
510 path.setFillType(fillType);
511 if (gShowPath) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000512 showPath(path, "path", false);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000513 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000514 if (!Simplify(path, &out)) {
515 SkDebugf("%s did not expect failure\n", __FUNCTION__);
516 REPORTER_ASSERT(state.fReporter, 0);
517 return false;
518 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000519 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000520 return true;
521 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000522 int result = comparePaths(state.fReporter, path, out, *state.fBitmap);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000523 if (result && gPathStrAssert) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000524 char temp[8192];
525 sk_bzero(temp, sizeof(temp));
526 SkMemoryWStream stream(temp, sizeof(temp));
527 const char* pathPrefix = NULL;
528 const char* nameSuffix = NULL;
529 if (fillType == SkPath::kEvenOdd_FillType) {
530 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
531 nameSuffix = "x";
532 }
533 const char testFunction[] = "testSimplifyx(path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000534 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000535 SkDebugf(temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000536 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000537 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000538 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000539 return result == 0;
540}
541
542bool testSimplify(skiatest::Reporter* reporter, const SkPath& path) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000543#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000544 showPathData(path);
545#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000546 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000547 if (!Simplify(path, &out)) {
548 SkDebugf("%s did not expect failure\n", __FUNCTION__);
549 REPORTER_ASSERT(reporter, 0);
550 return false;
551 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000552 SkBitmap bitmap;
553 int result = comparePaths(reporter, path, out, bitmap);
554 if (result && gPathStrAssert) {
555 REPORTER_ASSERT(reporter, 0);
556 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000557 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000558 return result == 0;
559}
560
caryclark@google.coma5e55922013-05-07 18:51:31 +0000561#if DEBUG_SHOW_TEST_NAME
caryclark@google.com570863f2013-09-16 15:55:01 +0000562void SkPathOpsDebug::ShowPath(const SkPath& a, const SkPath& b, SkPathOp shapeOp,
563 const char* testName) {
564 ShowFunctionHeader(testName);
565 showPath(a, "path", true);
566 showPath(b, "pathB", true);
567 ShowOp(shapeOp, "path", "pathB");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000568}
569#endif
570
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000571static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
572 const SkPathOp shapeOp, const char* testName, bool threaded) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000573#if DEBUG_SHOW_TEST_NAME
574 if (testName == NULL) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000575 SkDebugf("\n");
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000576 showPathData(a);
577 showOp(shapeOp);
578 showPathData(b);
579 } else {
caryclark@google.com570863f2013-09-16 15:55:01 +0000580 SkPathOpsDebug::ShowPath(a, b, shapeOp, testName);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000581 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000582#endif
583 SkPath out;
caryclark@google.com66560ca2013-04-26 19:51:16 +0000584 if (!Op(a, b, shapeOp, &out) ) {
585 SkDebugf("%s did not expect failure\n", __FUNCTION__);
586 REPORTER_ASSERT(reporter, 0);
587 return false;
588 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000589 if (threaded && !reporter->verbose()) {
590 return true;
591 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000592 SkPath pathOut, scaledPathOut;
593 SkRegion rgnA, rgnB, openClip, rgnOut;
594 openClip.setRect(-16000, -16000, 16000, 16000);
595 rgnA.setPath(a, openClip);
596 rgnB.setPath(b, openClip);
597 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
598 rgnOut.getBoundaryPath(&pathOut);
599
600 SkMatrix scale;
601 scaleMatrix(a, b, scale);
602 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
603 SkPath scaledA, scaledB;
604 scaledA.addPath(a, scale);
605 scaledA.setFillType(a.getFillType());
606 scaledB.addPath(b, scale);
607 scaledB.setFillType(b.getFillType());
608 scaledRgnA.setPath(scaledA, openClip);
609 scaledRgnB.setPath(scaledB, openClip);
610 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
611 scaledRgnOut.getBoundaryPath(&scaledPathOut);
612 SkBitmap bitmap;
613 SkPath scaledOut;
614 scaledOut.addPath(out, scale);
615 scaledOut.setFillType(out.getFillType());
616 int result = comparePaths(reporter, pathOut, scaledPathOut, out, scaledOut, bitmap, a, b,
617 shapeOp, scale);
618 if (result && gPathStrAssert) {
619 REPORTER_ASSERT(reporter, 0);
620 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000621 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000622 return result == 0;
623}
624
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000625bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
626 const SkPathOp shapeOp, const char* testName) {
627 return innerPathOp(reporter, a, b, shapeOp, testName, false);
628}
629
630bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
631 const SkPathOp shapeOp, const char* testName) {
632 return innerPathOp(reporter, a, b, shapeOp, testName, true);
633}
634
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000635SK_DECLARE_STATIC_MUTEX(gMutex);
636
caryclark@google.com16cfe402013-04-18 18:47:37 +0000637int initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000638#if 0 // doesn't work yet
639 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
640 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
641#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000642#ifdef SK_DEBUG
caryclark@google.com570863f2013-09-16 15:55:01 +0000643 SkPathOpsDebug::gMaxWindSum = 4;
644 SkPathOpsDebug::gMaxWindValue = 4;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000645#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000646 if (reporter->verbose()) {
647 SkAutoMutexAcquire lock(gMutex);
648 testName = test;
649 size_t testNameSize = strlen(test);
650 SkFILEStream inFile("../../experimental/Intersection/op.htm");
651 if (inFile.isValid()) {
652 SkTDArray<char> inData;
653 inData.setCount(inFile.getLength());
654 size_t inLen = inData.count();
655 inFile.read(inData.begin(), inLen);
656 inFile.setPath(NULL);
657 char* insert = strstr(inData.begin(), marker);
658 if (insert) {
659 insert += sizeof(marker) - 1;
660 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
661 testNumber = atoi(numLoc) + 1;
662 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000663 }
664 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000665 return reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 1;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000666}
667
caryclark@google.com66089e42013-04-10 15:55:37 +0000668void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
669 const char testFunction[] = "testSimplify(path);";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000670 const char* pathPrefix = NULL;
671 const char* nameSuffix = NULL;
672 if (pathFillType == SkPath::kEvenOdd_FillType) {
673 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
674 nameSuffix = "x";
675 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000676 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
677 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000678}
679
caryclark@google.com66089e42013-04-10 15:55:37 +0000680void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
681 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000682 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000683 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000684 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
685 outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000686}
687
688void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
689 void (*firstTest)(skiatest::Reporter* ),
690 void (*stopTest)(skiatest::Reporter* ), bool reverse) {
691 size_t index;
692 if (firstTest) {
693 index = count - 1;
694 while (index > 0 && tests[index].fun != firstTest) {
695 --index;
696 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000697#if DEBUG_SHOW_TEST_NAME
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000698 SkDebugf("<div id=\"%s\">\n", tests[index].str);
699 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
700#endif
701 (*tests[index].fun)(reporter);
702 }
703 index = reverse ? count - 1 : 0;
704 size_t last = reverse ? 0 : count - 1;
705 do {
706 if (tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000707 #if DEBUG_SHOW_TEST_NAME
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000708 SkDebugf("<div id=\"%s\">\n", tests[index].str);
709 SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str);
710 #endif
711 (*tests[index].fun)(reporter);
712 }
713 if (tests[index].fun == stopTest) {
714 SkDebugf("lastTest\n");
715 }
716 if (index == last) {
717 break;
718 }
719 index += reverse ? -1 : 1;
720 } while (true);
721}