blob: 5489a8b5334e83bc5fae7b9ae18a5a0b08c5778d [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +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 */
caryclark@google.comcd4421d2012-03-01 19:16:31 +00007#include "EdgeWalker_Test.h"
8#include "Intersection_Tests.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkPaint.h"
caryclark@google.com59823f72012-08-09 18:17:47 +000012#include "SkStream.h"
13
caryclark@google.comfb173422012-04-10 18:28:55 +000014#include <algorithm>
caryclark@google.com59823f72012-08-09 18:17:47 +000015#include <assert.h>
16#include <errno.h>
17#include <pthread.h>
18#include <unistd.h>
19#include <sys/types.h>
20#include <sys/sysctl.h>
caryclark@google.comcd4421d2012-03-01 19:16:31 +000021
caryclark@google.com78e17132012-04-17 11:40:34 +000022#undef SkASSERT
23#define SkASSERT(cond) while (!(cond)) { sk_throw(); }
24
caryclark@google.com59823f72012-08-09 18:17:47 +000025static const char marker[] =
26 "</div>\n"
27 "\n"
28 "<script type=\"text/javascript\">\n"
29 "\n"
30 "var testDivs = [\n";
caryclark@google.com24bec792012-08-20 12:43:57 +000031
caryclark@google.com4eeda372012-12-06 21:47:48 +000032static const char* opStrs[] = {
33 "kDifference_Op",
34 "kIntersect_Op",
35 "kUnion_Op",
36 "kXor_Op",
37};
38
39static const char* opSuffixes[] = {
40 "d",
41 "i",
42 "u",
43 "x",
44};
45
caryclark@google.com24bec792012-08-20 12:43:57 +000046static const char preferredFilename[] = "/flash/debug/XX.txt";
47static const char backupFilename[] = "../../experimental/Intersection/debugXX.txt";
caryclark@google.com59823f72012-08-09 18:17:47 +000048
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000049static bool gShowPath = false;
caryclark@google.com198e0542012-03-30 18:47:02 +000050static bool gComparePaths = true;
caryclark@google.comc899ad92012-08-23 15:24:42 +000051static bool gShowOutputProgress = false;
caryclark@google.com4eeda372012-12-06 21:47:48 +000052static bool gComparePathsAssert = true;
caryclark@google.com59823f72012-08-09 18:17:47 +000053static bool gPathStrAssert = true;
caryclark@google.com6aea33f2012-10-09 14:11:58 +000054static bool gUsePhysicalFiles = false;
caryclark@google.comcd4421d2012-03-01 19:16:31 +000055
caryclark@google.com4eeda372012-12-06 21:47:48 +000056static bool isRectContour(SkPath::Iter& iter, SkRect& rect, SkPath::Direction& direction) {
57 int corners = 0;
58 SkPoint first, last;
59 first.set(0, 0);
60 last.set(0, 0);
61 int firstDirection = 0;
62 int lastDirection = 0;
63 int nextDirection = 0;
64 bool closedOrMoved = false;
65 bool autoClose = false;
66 rect.setEmpty();
67 uint8_t verb;
68 SkPoint data[4];
69 SkTDArray<SkPoint> sides;
70 bool empty = true;
71 while ((verb = iter.next(data)) != SkPath::kDone_Verb && !autoClose) {
72 empty = false;
73 SkPoint* pts = &data[1];
74 switch (verb) {
75 case SkPath::kClose_Verb:
76 pts = &last;
77 autoClose = true;
78 case SkPath::kLine_Verb: {
79 SkScalar left = last.fX;
80 SkScalar top = last.fY;
81 SkScalar right = pts->fX;
82 SkScalar bottom = pts->fY;
83 *sides.append() = *pts;
84 ++pts;
85 if (left != right && top != bottom) {
86 return false; // diagonal
87 }
88 if (left == right && top == bottom) {
89 break; // single point on side OK
90 }
91 nextDirection = (left != right) << 0 |
92 (left < right || top < bottom) << 1;
93 if (0 == corners) {
94 firstDirection = nextDirection;
95 first = last;
96 last = pts[-1];
97 corners = 1;
98 closedOrMoved = false;
99 break;
100 }
101 if (closedOrMoved) {
102 return false; // closed followed by a line
103 }
104 if (autoClose && nextDirection == firstDirection) {
105 break; // colinear with first
106 }
107 closedOrMoved = autoClose;
108 if (lastDirection != nextDirection) {
109 if (++corners > 4) {
110 return false; // too many direction changes
111 }
112 }
113 last = pts[-1];
114 if (lastDirection == nextDirection) {
115 break; // colinear segment
116 }
117 // Possible values for corners are 2, 3, and 4.
118 // When corners == 3, nextDirection opposes firstDirection.
119 // Otherwise, nextDirection at corner 2 opposes corner 4.
120 int turn = firstDirection ^ (corners - 1);
121 int directionCycle = 3 == corners ? 0 : nextDirection ^ turn;
122 if ((directionCycle ^ turn) != nextDirection) {
123 return false; // direction didn't follow cycle
124 }
125 break;
126 }
127 case SkPath::kQuad_Verb:
128 case SkPath::kCubic_Verb:
129 return false; // quadratic, cubic not allowed
130 case SkPath::kMove_Verb:
131 last = *pts++;
132 *sides.append() = last;
133 closedOrMoved = true;
134 break;
135 }
136 lastDirection = nextDirection;
137 }
138 // Success if 4 corners and first point equals last
139 bool result = 4 == corners && (first == last || autoClose);
140 if (result) {
141 direction = firstDirection == (lastDirection + 1 & 3) ? SkPath::kCCW_Direction
142 : SkPath::kCW_Direction;
143 rect.set(&sides[0], sides.count());
144 } else {
145 rect.setEmpty();
146 }
147 return !empty;
148}
149
150static void showPathContour(SkPath::Iter& iter, bool skip) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000151 uint8_t verb;
152 SkPoint pts[4];
153 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
caryclark@google.com4eeda372012-12-06 21:47:48 +0000154 if (skip) {
155 if (verb == SkPath::kClose_Verb) {
156 return;
157 }
158 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000159 switch (verb) {
160 case SkPath::kMove_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +0000161 SkDebugf("path.moveTo(%1.9g, %1.9g);\n", pts[0].fX, pts[0].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000162 continue;
163 case SkPath::kLine_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +0000164 SkDebugf("path.lineTo(%1.9g, %1.9g);\n", pts[1].fX, pts[1].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000165 break;
166 case SkPath::kQuad_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +0000167 SkDebugf("path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n",
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000168 pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
169 break;
170 case SkPath::kCubic_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +0000171 SkDebugf("path.cubicTo(%1.9g, %1.9g, %1.9g, %1.9g);\n",
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000172 pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
173 pts[3].fX, pts[3].fY);
174 break;
175 case SkPath::kClose_Verb:
176 SkDebugf("path.close();\n");
caryclark@google.com4eeda372012-12-06 21:47:48 +0000177 return;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000178 default:
179 SkDEBUGFAIL("bad verb");
180 return;
181 }
182 }
183}
184
caryclark@google.com4eeda372012-12-06 21:47:48 +0000185void showPath(const SkPath& path, const char* str) {
186 SkDebugf("%s\n", !str ? "original:" : str);
187 SkPath::Iter iter(path, true);
188 SkTDArray<SkRect> rects;
189 SkTDArray<SkPath::Direction> directions;
190 SkRect rect;
191 SkPath::Direction direction;
192 while (isRectContour(iter, rect, direction)) {
193 *rects.append() = rect;
194 *directions.append() = direction;
195 }
196 iter.setPath(path, true);
197 for (int contour = 0; contour < rects.count(); ++contour) {
198 const SkRect& rect = rects[contour];
199 bool useRect = !rect.isEmpty();
200 showPathContour(iter, useRect);
201 if (useRect) {
202 SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
203 rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction
204 ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction");
205 }
206 }
207}
208
209static int pathsDrawTheSame(const SkPath& one, const SkPath& two,
210 SkBitmap& bits, SkPath& scaledOne, SkPath& scaledTwo, int& error2x2) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000211 const int bitWidth = 64;
212 const int bitHeight = 64;
213 if (bits.width() == 0) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000214 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
215 bits.allocPixels();
caryclark@google.com198e0542012-03-30 18:47:02 +0000216 }
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +0000217
caryclark@google.comc899ad92012-08-23 15:24:42 +0000218 SkRect larger = one.getBounds();
219 larger.join(two.getBounds());
220 SkScalar largerWidth = larger.width();
221 if (largerWidth < 4) {
222 largerWidth = 4;
223 }
224 SkScalar largerHeight = larger.height();
225 if (largerHeight < 4) {
226 largerHeight = 4;
227 }
228 SkScalar hScale = (bitWidth - 2) / largerWidth;
229 SkScalar vScale = (bitHeight - 2) / largerHeight;
230 SkMatrix scale;
231 scale.reset();
232 scale.preScale(hScale, vScale);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000233 one.transform(scale, &scaledOne);
234 two.transform(scale, &scaledTwo);
235 const SkRect& bounds1 = scaledOne.getBounds();
reed@google.combe584d72012-09-28 19:48:09 +0000236
237 SkCanvas canvas(bits);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000238 canvas.drawColor(SK_ColorWHITE);
239 SkPaint paint;
240 canvas.save();
241 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000242 canvas.drawPath(scaledOne, paint);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000243 canvas.restore();
244 canvas.save();
245 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000246 canvas.drawPath(scaledTwo, paint);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000247 canvas.restore();
caryclark@google.comc899ad92012-08-23 15:24:42 +0000248 int errors2 = 0;
caryclark@google.com198e0542012-03-30 18:47:02 +0000249 int errors = 0;
caryclark@google.comc899ad92012-08-23 15:24:42 +0000250 for (int y = 0; y < bitHeight - 1; ++y) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000251 uint32_t* addr1 = bits.getAddr32(0, y);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000252 uint32_t* addr2 = bits.getAddr32(0, y + 1);
253 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
254 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
255 for (int x = 0; x < bitWidth - 1; ++x) {
256 // count 2x2 blocks
257 bool err = addr1[x] != addr3[x];
258 if (err) {
259 errors2 += addr1[x + 1] != addr3[x + 1]
260 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
261 errors++;
262 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000263 }
264 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000265 if (errors2 >= 6 || errors > 160) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000266 SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors);
267 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000268 error2x2 = errors2;
caryclark@google.com198e0542012-03-30 18:47:02 +0000269 return errors;
270}
271
caryclark@google.comc899ad92012-08-23 15:24:42 +0000272bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000273 if (!drawPaths) {
caryclark@google.com752b60e2012-03-22 21:11:17 +0000274 return true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000275 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000276 const SkRect& bounds1 = one.getBounds();
277 const SkRect& bounds2 = two.getBounds();
278 SkRect larger = bounds1;
279 larger.join(bounds2);
280 SkBitmap bits;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000281 char out[256];
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000282 int bitWidth = SkScalarCeil(larger.width()) + 2;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000283 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
284 return false;
285 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000286 int bitHeight = SkScalarCeil(larger.height()) + 2;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000287 if (bitHeight >= (int) sizeof(out)) {
288 return false;
289 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000290 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
291 bits.allocPixels();
292 SkCanvas canvas(bits);
293 canvas.drawColor(SK_ColorWHITE);
294 SkPaint paint;
295 canvas.save();
296 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
297 canvas.drawPath(one, paint);
298 canvas.restore();
299 canvas.save();
caryclark@google.comfb173422012-04-10 18:28:55 +0000300 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000301 canvas.drawPath(two, paint);
302 canvas.restore();
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000303 for (int y = 0; y < bitHeight; ++y) {
304 uint32_t* addr1 = bits.getAddr32(0, y);
305 int x;
306 char* outPtr = out;
307 for (x = 0; x < bitWidth; ++x) {
308 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
309 }
310 *outPtr++ = '|';
311 for (x = bitWidth; x < bitWidth * 2; ++x) {
312 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
313 }
314 *outPtr++ = '\0';
315 SkDebugf("%s\n", out);
316 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000317 return true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000318}
319
caryclark@google.com4eeda372012-12-06 21:47:48 +0000320static void showSimplifiedPath(const SkPath& one, const SkPath& two,
321 const SkPath& scaledOne, const SkPath& scaledTwo) {
322 showPath(one, "original:");
323 showPath(two, "simplified:");
324 drawAsciiPaths(scaledOne, scaledTwo, true);
325}
326
reed@google.combe584d72012-09-28 19:48:09 +0000327int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000328 int errors2x2;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000329 SkPath scaledOne, scaledTwo;
330 int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000331 if (errors2x2 == 0) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000332 return 0;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000333 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000334 const int MAX_ERRORS = 8;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000335 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
336 showSimplifiedPath(one, two, scaledOne, scaledTwo);
337 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000338 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000339 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
caryclark@google.com4eeda372012-12-06 21:47:48 +0000340 showSimplifiedPath(one, two, scaledOne, scaledTwo);
341 SkASSERT(0);
342 }
343 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
344}
345
346static void showShapeOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b,
347 const SkPath& scaledOne, const SkPath& scaledTwo, const ShapeOp shapeOp) {
348 SkASSERT((unsigned) shapeOp < sizeof(opStrs) / sizeof(opStrs[0]));
349 showPath(a, "minuend:");
350 SkDebugf("op: %s\n", opStrs[shapeOp]);
351 showPath(b, "subtrahend:");
352 showPath(one, "region:");
353 showPath(two, "op result:");
354 drawAsciiPaths(scaledOne, scaledTwo, true);
355}
356
357int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap,
358 const SkPath& a, const SkPath& b, const ShapeOp shapeOp) {
359 int errors2x2;
360 SkPath scaledOne, scaledTwo;
361 int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
362 if (errors2x2 == 0) {
363 return 0;
364 }
365 const int MAX_ERRORS = 8;
366 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
367 showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp);
368 }
369 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
370 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
371 showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000372 SkASSERT(0);
373 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000374 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000375}
376
377// doesn't work yet
378void comparePathsTiny(const SkPath& one, const SkPath& two) {
379 const SkRect& bounds1 = one.getBounds();
380 const SkRect& bounds2 = two.getBounds();
381 SkRect larger = bounds1;
382 larger.join(bounds2);
383 SkBitmap bits;
384 int bitWidth = SkScalarCeil(larger.width()) + 2;
385 int bitHeight = SkScalarCeil(larger.height()) + 2;
386 bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight);
387 bits.allocPixels();
388 SkCanvas canvas(bits);
389 canvas.drawColor(SK_ColorWHITE);
390 SkPaint paint;
391 canvas.save();
392 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
393 canvas.drawPath(one, paint);
394 canvas.restore();
395 canvas.save();
396 canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1);
397 canvas.drawPath(two, paint);
398 canvas.restore();
399 for (int y = 0; y < bitHeight; ++y) {
400 uint8_t* addr1 = bits.getAddr1(0, y);
401 uint8_t* addr2 = bits.getAddr1(bitWidth, y);
402 for (int x = 0; x < bits.rowBytes(); ++x) {
403 SkASSERT(addr1[x] == addr2[x]);
404 }
405 }
406}
407
reed@google.combe584d72012-09-28 19:48:09 +0000408bool testSimplify(const SkPath& path, bool fill, SkPath& out, SkBitmap& bitmap) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000409 if (gShowPath) {
410 showPath(path);
411 }
412 simplify(path, fill, out);
caryclark@google.com752b60e2012-03-22 21:11:17 +0000413 if (!gComparePaths) {
414 return true;
415 }
reed@google.combe584d72012-09-28 19:48:09 +0000416 return comparePaths(path, out, bitmap) == 0;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000417}
caryclark@google.com78e17132012-04-17 11:40:34 +0000418
caryclark@google.com24bec792012-08-20 12:43:57 +0000419bool testSimplifyx(SkPath& path, bool useXor, SkPath& out, State4& state,
caryclark@google.com59823f72012-08-09 18:17:47 +0000420 const char* pathStr) {
caryclark@google.com24bec792012-08-20 12:43:57 +0000421 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
422 path.setFillType(fillType);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000423 if (gShowPath) {
424 showPath(path);
425 }
426 simplifyx(path, out);
427 if (!gComparePaths) {
428 return true;
429 }
reed@google.combe584d72012-09-28 19:48:09 +0000430 int result = comparePaths(path, out, state.bitmap);
caryclark@google.com59823f72012-08-09 18:17:47 +0000431 if (result && gPathStrAssert) {
caryclark@google.comd1688742012-09-18 20:08:37 +0000432 SkDebugf("addTest %s\n", state.filename);
caryclark@google.com59823f72012-08-09 18:17:47 +0000433 char temp[8192];
434 bzero(temp, sizeof(temp));
435 SkMemoryWStream stream(temp, sizeof(temp));
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000436 const char* pathPrefix = NULL;
437 const char* nameSuffix = NULL;
438 if (fillType == SkPath::kEvenOdd_FillType) {
439 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
440 nameSuffix = "x";
441 }
442 const char testFunction[] = "testSimplifyx(path);";
443 outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, stream);
caryclark@google.com59823f72012-08-09 18:17:47 +0000444 SkDebugf(temp);
445 SkASSERT(0);
446 }
447 return result == 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000448}
449
450bool testSimplifyx(const SkPath& path) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000451 SkPath out;
452 simplifyx(path, out);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000453 SkBitmap bitmap;
reed@google.combe584d72012-09-28 19:48:09 +0000454 int result = comparePaths(path, out, bitmap);
caryclark@google.com24bec792012-08-20 12:43:57 +0000455 if (result && gPathStrAssert) {
456 SkASSERT(0);
457 }
458 return result == 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000459}
460
caryclark@google.com31143cf2012-11-09 22:14:19 +0000461bool testShapeOp(const SkPath& a, const SkPath& b, const ShapeOp shapeOp) {
462 SkPath out;
463 operate(a, b, shapeOp, out);
464 SkPath pathOut;
465 SkRegion rgnA, rgnB, openClip, rgnOut;
466 openClip.setRect(-16000, -16000, 16000, 16000);
467 rgnA.setPath(a, openClip);
468 rgnB.setPath(b, openClip);
469 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
470 rgnOut.getBoundaryPath(&pathOut);
471 SkBitmap bitmap;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000472 int result = comparePaths(pathOut, out, bitmap, a, b, shapeOp);
caryclark@google.com31143cf2012-11-09 22:14:19 +0000473 if (result && gPathStrAssert) {
474 SkASSERT(0);
475 }
476 return result == 0;
477}
478
caryclark@google.com59823f72012-08-09 18:17:47 +0000479const int maxThreadsAllocated = 64;
480static int maxThreads = 1;
481static int threadIndex;
482State4 threadState[maxThreadsAllocated];
483static int testNumber;
484static const char* testName;
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000485static bool debugThreads = false;
caryclark@google.com59823f72012-08-09 18:17:47 +0000486
487State4* State4::queue = NULL;
488pthread_mutex_t State4::addQueue = PTHREAD_MUTEX_INITIALIZER;
489pthread_cond_t State4::checkQueue = PTHREAD_COND_INITIALIZER;
490
caryclark@google.com78e17132012-04-17 11:40:34 +0000491State4::State4() {
492 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150 * 2, 100);
493 bitmap.allocPixels();
caryclark@google.com78e17132012-04-17 11:40:34 +0000494}
495
caryclark@google.com59823f72012-08-09 18:17:47 +0000496void createThread(State4* statePtr, void* (*testFun)(void* )) {
497 int threadError = pthread_create(&statePtr->threadID, NULL, testFun,
caryclark@google.com78e17132012-04-17 11:40:34 +0000498 (void*) statePtr);
499 SkASSERT(!threadError);
500}
501
caryclark@google.com59823f72012-08-09 18:17:47 +0000502int dispatchTest4(void* (*testFun)(void* ), int a, int b, int c, int d) {
503 int testsRun = 0;
caryclark@google.com03f97062012-08-21 13:13:52 +0000504 State4* statePtr;
caryclark@google.com59823f72012-08-09 18:17:47 +0000505 if (!gRunTestsInOneThread) {
caryclark@google.com59823f72012-08-09 18:17:47 +0000506 pthread_mutex_lock(&State4::addQueue);
507 if (threadIndex < maxThreads) {
508 statePtr = &threadState[threadIndex];
509 statePtr->testsRun = 0;
510 statePtr->a = a;
511 statePtr->b = b;
512 statePtr->c = c;
513 statePtr->d = d;
514 statePtr->done = false;
515 statePtr->index = threadIndex;
516 statePtr->last = false;
517 if (debugThreads) SkDebugf("%s %d create done=%d last=%d\n", __FUNCTION__,
518 statePtr->index, statePtr->done, statePtr->last);
519 pthread_cond_init(&statePtr->initialized, NULL);
520 ++threadIndex;
521 createThread(statePtr, testFun);
522 } else {
523 while (!State4::queue) {
524 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
525 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
526 }
527 statePtr = State4::queue;
528 testsRun += statePtr->testsRun;
529 statePtr->testsRun = 0;
530 statePtr->a = a;
531 statePtr->b = b;
532 statePtr->c = c;
533 statePtr->d = d;
534 statePtr->done = false;
535 State4::queue = NULL;
536 for (int index = 0; index < maxThreads; ++index) {
537 if (threadState[index].done) {
538 State4::queue = &threadState[index];
539 }
540 }
541 if (debugThreads) SkDebugf("%s %d init done=%d last=%d queued=%d\n", __FUNCTION__,
542 statePtr->index, statePtr->done, statePtr->last,
543 State4::queue ? State4::queue->index : -1);
544 pthread_cond_signal(&statePtr->initialized);
545 }
546 pthread_mutex_unlock(&State4::addQueue);
547 } else {
caryclark@google.com03f97062012-08-21 13:13:52 +0000548 statePtr = &threadState[0];
caryclark@google.comc899ad92012-08-23 15:24:42 +0000549 testsRun += statePtr->testsRun;
caryclark@google.com03f97062012-08-21 13:13:52 +0000550 statePtr->testsRun = 0;
551 statePtr->a = a;
552 statePtr->b = b;
553 statePtr->c = c;
554 statePtr->d = d;
555 statePtr->done = false;
556 statePtr->index = threadIndex;
557 statePtr->last = false;
558 (*testFun)(statePtr);
caryclark@google.com78e17132012-04-17 11:40:34 +0000559 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000560 return testsRun;
561}
562
563void initializeTests(const char* test, size_t testNameSize) {
564 testName = test;
565 if (!gRunTestsInOneThread) {
566 int threads = -1;
567 size_t size = sizeof(threads);
568 sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0);
569 if (threads > 0) {
570 maxThreads = threads;
571 } else {
572 maxThreads = 8;
573 }
574 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000575 SkFILEStream inFile("../../experimental/Intersection/op.htm");
576 if (inFile.isValid()) {
577 SkTDArray<char> inData;
578 inData.setCount(inFile.getLength());
579 size_t inLen = inData.count();
580 inFile.read(inData.begin(), inLen);
581 inFile.setPath(NULL);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000582 char* insert = strstr(inData.begin(), marker);
caryclark@google.com03f97062012-08-21 13:13:52 +0000583 if (insert) {
584 insert += sizeof(marker) - 1;
585 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
586 testNumber = atoi(numLoc) + 1;
caryclark@google.com59823f72012-08-09 18:17:47 +0000587 }
588 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000589 const char* filename = preferredFilename;
590 SkFILEWStream preferredTest(filename);
591 if (!preferredTest.isValid()) {
592 filename = backupFilename;
593 SkFILEWStream backupTest(filename);
594 SkASSERT(backupTest.isValid());
595 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000596 for (int index = 0; index < maxThreads; ++index) {
597 State4* statePtr = &threadState[index];
598 strcpy(statePtr->filename, filename);
caryclark@google.com24bec792012-08-20 12:43:57 +0000599 size_t len = strlen(filename);
600 SkASSERT(statePtr->filename[len - 6] == 'X');
601 SkASSERT(statePtr->filename[len - 5] == 'X');
602 statePtr->filename[len - 6] = '0' + index / 10;
603 statePtr->filename[len - 5] = '0' + index % 10;
caryclark@google.com59823f72012-08-09 18:17:47 +0000604 }
caryclark@google.com78e17132012-04-17 11:40:34 +0000605 threadIndex = 0;
606}
caryclark@google.com59823f72012-08-09 18:17:47 +0000607
caryclark@google.com24bec792012-08-20 12:43:57 +0000608void outputProgress(const State4& state, const char* pathStr, SkPath::FillType pathFillType) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000609 if (gRunTestsInOneThread && gShowOutputProgress) {
caryclark@google.com03f97062012-08-21 13:13:52 +0000610 if (pathFillType == SkPath::kEvenOdd_FillType) {
611 SkDebugf(" path.setFillType(SkPath::kEvenOdd_FillType);\n", pathStr);
caryclark@google.com59823f72012-08-09 18:17:47 +0000612 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000613 SkDebugf("%s\n", pathStr);
caryclark@google.com59823f72012-08-09 18:17:47 +0000614 }
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000615 const char testFunction[] = "testSimplifyx(path);";
616 const char* pathPrefix = NULL;
617 const char* nameSuffix = NULL;
618 if (pathFillType == SkPath::kEvenOdd_FillType) {
619 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
620 nameSuffix = "x";
621 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000622 if (gUsePhysicalFiles) {
623 SkFILEWStream outFile(state.filename);
624 if (!outFile.isValid()) {
625 SkASSERT(0);
626 return;
627 }
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000628 outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outFile);
caryclark@google.com03f97062012-08-21 13:13:52 +0000629 return;
630 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000631 SkFILEWStream outRam(state.filename);
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000632 outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outRam);
caryclark@google.com59823f72012-08-09 18:17:47 +0000633}
634
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000635void outputProgress(const State4& state, const char* pathStr, ShapeOp op) {
636 SkString testFunc("testShapeOp(path, pathB, ");
637 testFunc += opStrs[op];
638 testFunc += ");";
639 const char* testFunction = testFunc.c_str();
640 if (gRunTestsInOneThread && gShowOutputProgress) {
641 SkDebugf("%s\n", pathStr);
642 SkDebugf(" %s\n", testFunction);
643 }
644 const char* nameSuffix = opSuffixes[op];
645 if (gUsePhysicalFiles) {
646 SkFILEWStream outFile(state.filename);
647 if (!outFile.isValid()) {
648 SkASSERT(0);
649 return;
650 }
651 outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outFile);
652 return;
653 }
654 SkFILEWStream outRam(state.filename);
655 outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outRam);
656}
657
658static void writeTestName(const char* nameSuffix, SkWStream& outFile) {
caryclark@google.com59823f72012-08-09 18:17:47 +0000659 outFile.writeText(testName);
660 outFile.writeDecAsText(testNumber);
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000661 if (nameSuffix) {
662 outFile.writeText(nameSuffix);
caryclark@google.com24bec792012-08-20 12:43:57 +0000663 }
664}
665
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000666void outputToStream(const State4& state, const char* pathStr, const char* pathPrefix,
667 const char* nameSuffix,
668 const char* testFunction, SkWStream& outFile) {
caryclark@google.com24bec792012-08-20 12:43:57 +0000669 outFile.writeText("<div id=\"");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000670 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000671 outFile.writeText("\">\n");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000672 if (pathPrefix) {
673 outFile.writeText(pathPrefix);
caryclark@google.com24bec792012-08-20 12:43:57 +0000674 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000675 outFile.writeText(pathStr);
676 outFile.writeText("</div>\n\n");
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000677
caryclark@google.com59823f72012-08-09 18:17:47 +0000678 outFile.writeText(marker);
679 outFile.writeText(" ");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000680 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000681 outFile.writeText(",\n\n\n");
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000682
caryclark@google.com59823f72012-08-09 18:17:47 +0000683 outFile.writeText("static void ");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000684 writeTestName(nameSuffix, outFile);
685 outFile.writeText("() {\n SkPath path, pathB;\n");
686 if (pathPrefix) {
687 outFile.writeText(pathPrefix);
caryclark@google.com24bec792012-08-20 12:43:57 +0000688 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000689 outFile.writeText(pathStr);
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000690 outFile.writeText(" ");
691 outFile.writeText(testFunction);
692 outFile.writeText("\n}\n\n");
caryclark@google.com59823f72012-08-09 18:17:47 +0000693 outFile.writeText("static void (*firstTest)() = ");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000694 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000695 outFile.writeText(";\n\n");
696
697 outFile.writeText("static struct {\n");
698 outFile.writeText(" void (*fun)();\n");
699 outFile.writeText(" const char* str;\n");
700 outFile.writeText("} tests[] = {\n");
701 outFile.writeText(" TEST(");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000702 writeTestName(pathPrefix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000703 outFile.writeText("),\n");
704 outFile.flush();
705}
706
707bool runNextTestSet(State4& state) {
708 if (gRunTestsInOneThread) {
709 return false;
710 }
711 pthread_mutex_lock(&State4::addQueue);
712 state.done = true;
713 State4::queue = &state;
714 if (debugThreads) SkDebugf("%s %d checkQueue done=%d last=%d\n", __FUNCTION__, state.index,
715 state.done, state.last);
716 pthread_cond_signal(&State4::checkQueue);
717 while (state.done && !state.last) {
718 if (debugThreads) SkDebugf("%s %d done=%d last=%d\n", __FUNCTION__, state.index, state.done, state.last);
719 pthread_cond_wait(&state.initialized, &State4::addQueue);
720 }
721 pthread_mutex_unlock(&State4::addQueue);
722 return !state.last;
723}
724
725int waitForCompletion() {
726 int testsRun = 0;
727 if (!gRunTestsInOneThread) {
728 pthread_mutex_lock(&State4::addQueue);
729 int runningThreads = maxThreads;
730 int index;
731 while (runningThreads > 0) {
732 while (!State4::queue) {
733 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
734 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
735 }
736 while (State4::queue) {
737 --runningThreads;
738 SkDebugf("•");
739 State4::queue->last = true;
caryclark@google.com24bec792012-08-20 12:43:57 +0000740 State4* next = NULL;
caryclark@google.com59823f72012-08-09 18:17:47 +0000741 for (index = 0; index < maxThreads; ++index) {
742 State4& test = threadState[index];
743 if (test.done && !test.last) {
744 next = &test;
745 }
746 }
747 if (debugThreads) SkDebugf("%s %d next=%d deQueue\n", __FUNCTION__,
748 State4::queue->index, next ? next->index : -1);
749 pthread_cond_signal(&State4::queue->initialized);
750 State4::queue = next;
751 }
752 }
753 pthread_mutex_unlock(&State4::addQueue);
754 for (index = 0; index < maxThreads; ++index) {
755 pthread_join(threadState[index].threadID, NULL);
756 testsRun += threadState[index].testsRun;
757 }
758 SkDebugf("\n");
759 }
760#ifdef SK_DEBUG
761 gDebugMaxWindSum = SK_MaxS32;
762 gDebugMaxWindValue = SK_MaxS32;
763#endif
764 return testsRun;
765}