blob: 7926ee982202817eca691024019e626356dc08b1 [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 */
skia.committer@gmail.com64334352013-03-06 07:01:46 +00007
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +00008#include "DataTypes.h"
caryclark@google.comcd4421d2012-03-01 19:16:31 +00009#include "EdgeWalker_Test.h"
10#include "Intersection_Tests.h"
11#include "SkBitmap.h"
12#include "SkCanvas.h"
caryclark@google.com45a8fc62013-02-14 15:29:11 +000013#include "SkMatrix.h"
caryclark@google.comcd4421d2012-03-01 19:16:31 +000014#include "SkPaint.h"
caryclark@google.com59823f72012-08-09 18:17:47 +000015#include "SkStream.h"
16
caryclark@google.comfb173422012-04-10 18:28:55 +000017#include <algorithm>
caryclark@google.com59823f72012-08-09 18:17:47 +000018#include <errno.h>
19#include <pthread.h>
20#include <unistd.h>
21#include <sys/types.h>
22#include <sys/sysctl.h>
caryclark@google.comcd4421d2012-03-01 19:16:31 +000023
caryclark@google.com78e17132012-04-17 11:40:34 +000024#undef SkASSERT
25#define SkASSERT(cond) while (!(cond)) { sk_throw(); }
26
caryclark@google.com59823f72012-08-09 18:17:47 +000027static const char marker[] =
28 "</div>\n"
29 "\n"
30 "<script type=\"text/javascript\">\n"
31 "\n"
32 "var testDivs = [\n";
caryclark@google.com24bec792012-08-20 12:43:57 +000033
caryclark@google.com4eeda372012-12-06 21:47:48 +000034static const char* opStrs[] = {
35 "kDifference_Op",
36 "kIntersect_Op",
37 "kUnion_Op",
38 "kXor_Op",
39};
40
41static const char* opSuffixes[] = {
42 "d",
43 "i",
44 "u",
45 "x",
46};
47
caryclark@google.com24bec792012-08-20 12:43:57 +000048static const char preferredFilename[] = "/flash/debug/XX.txt";
49static const char backupFilename[] = "../../experimental/Intersection/debugXX.txt";
caryclark@google.com59823f72012-08-09 18:17:47 +000050
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000051static bool gShowPath = false;
caryclark@google.com198e0542012-03-30 18:47:02 +000052static bool gComparePaths = true;
caryclark@google.comc899ad92012-08-23 15:24:42 +000053static bool gShowOutputProgress = false;
caryclark@google.com4eeda372012-12-06 21:47:48 +000054static bool gComparePathsAssert = true;
caryclark@google.com59823f72012-08-09 18:17:47 +000055static bool gPathStrAssert = true;
caryclark@google.com6aea33f2012-10-09 14:11:58 +000056static bool gUsePhysicalFiles = false;
caryclark@google.comcd4421d2012-03-01 19:16:31 +000057
caryclark@google.com8f9f4682013-01-03 21:18:16 +000058static void showPathContour(SkPath::Iter& iter) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +000059 uint8_t verb;
60 SkPoint pts[4];
61 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
62 switch (verb) {
63 case SkPath::kMove_Verb:
caryclark@google.combeda3892013-02-07 13:13:41 +000064 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", pts[0].fX, pts[0].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +000065 continue;
66 case SkPath::kLine_Verb:
caryclark@google.combeda3892013-02-07 13:13:41 +000067 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", pts[1].fX, pts[1].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +000068 break;
69 case SkPath::kQuad_Verb:
caryclark@google.combeda3892013-02-07 13:13:41 +000070 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
caryclark@google.comcd4421d2012-03-01 19:16:31 +000071 pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
72 break;
73 case SkPath::kCubic_Verb:
caryclark@google.combeda3892013-02-07 13:13:41 +000074 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
75 pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +000076 break;
77 case SkPath::kClose_Verb:
78 SkDebugf("path.close();\n");
caryclark@google.com8f9f4682013-01-03 21:18:16 +000079 break;
caryclark@google.comcd4421d2012-03-01 19:16:31 +000080 default:
81 SkDEBUGFAIL("bad verb");
82 return;
83 }
84 }
85}
86
caryclark@google.com4eeda372012-12-06 21:47:48 +000087void showPath(const SkPath& path, const char* str) {
88 SkDebugf("%s\n", !str ? "original:" : str);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +000089 showPath(path);
90}
91
92void showPath(const SkPath& path) {
caryclark@google.com4eeda372012-12-06 21:47:48 +000093 SkPath::Iter iter(path, true);
caryclark@google.com8f9f4682013-01-03 21:18:16 +000094 int rectCount = path.isRectContours() ? path.rectContours(NULL, NULL) : 0;
95 if (rectCount > 0) {
96 SkTDArray<SkRect> rects;
97 SkTDArray<SkPath::Direction> directions;
98 rects.setCount(rectCount);
99 directions.setCount(rectCount);
100 path.rectContours(rects.begin(), directions.begin());
101 for (int contour = 0; contour < rectCount; ++contour) {
102 const SkRect& rect = rects[contour];
caryclark@google.com4eeda372012-12-06 21:47:48 +0000103 SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop,
104 rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction
105 ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction");
106 }
caryclark@google.com8f9f4682013-01-03 21:18:16 +0000107 return;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000108 }
caryclark@google.com8f9f4682013-01-03 21:18:16 +0000109 iter.setPath(path, true);
110 showPathContour(iter);
caryclark@google.com4eeda372012-12-06 21:47:48 +0000111}
112
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000113void showPathData(const SkPath& path) {
114 SkPath::Iter iter(path, true);
115 uint8_t verb;
116 SkPoint pts[4];
117 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
118 switch (verb) {
119 case SkPath::kMove_Verb:
120 continue;
121 case SkPath::kLine_Verb:
122 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
123 break;
124 case SkPath::kQuad_Verb:
125 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
126 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
127 break;
128 case SkPath::kCubic_Verb:
129 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
130 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
131 break;
132 case SkPath::kClose_Verb:
133 break;
134 default:
135 SkDEBUGFAIL("bad verb");
136 return;
137 }
138 }
139}
140
141void showOp(const ShapeOp op) {
142 switch (op) {
143 case kDifference_Op:
144 SkDebugf("op difference\n");
145 break;
146 case kIntersect_Op:
147 SkDebugf("op intersect\n");
148 break;
149 case kUnion_Op:
150 SkDebugf("op union\n");
151 break;
152 case kXor_Op:
153 SkDebugf("op xor\n");
154 break;
155 default:
156 SkASSERT(0);
157 }
158}
159
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000160static void showPath(const SkPath& path, const char* str, const SkMatrix& scale) {
161 SkPath scaled;
162 SkMatrix inverse;
163 bool success = scale.invert(&inverse);
164 if (!success) SkASSERT(0);
165 path.transform(inverse, &scaled);
166 showPath(scaled, str);
167}
168
169const int bitWidth = 64;
170const int bitHeight = 64;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +0000171
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000172static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000173 SkRect larger = one.getBounds();
174 larger.join(two.getBounds());
175 SkScalar largerWidth = larger.width();
176 if (largerWidth < 4) {
177 largerWidth = 4;
178 }
179 SkScalar largerHeight = larger.height();
180 if (largerHeight < 4) {
181 largerHeight = 4;
182 }
183 SkScalar hScale = (bitWidth - 2) / largerWidth;
184 SkScalar vScale = (bitHeight - 2) / largerHeight;
caryclark@google.comc899ad92012-08-23 15:24:42 +0000185 scale.reset();
186 scale.preScale(hScale, vScale);
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000187}
reed@google.combe584d72012-09-28 19:48:09 +0000188
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000189static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
190 int& error2x2) {
191 if (bits.width() == 0) {
192 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
193 bits.allocPixels();
194 }
reed@google.combe584d72012-09-28 19:48:09 +0000195 SkCanvas canvas(bits);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000196 canvas.drawColor(SK_ColorWHITE);
197 SkPaint paint;
198 canvas.save();
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000199 const SkRect& bounds1 = scaledOne.getBounds();
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000200 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000201 canvas.drawPath(scaledOne, paint);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000202 canvas.restore();
203 canvas.save();
204 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000205 canvas.drawPath(scaledTwo, paint);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000206 canvas.restore();
caryclark@google.comc899ad92012-08-23 15:24:42 +0000207 int errors2 = 0;
caryclark@google.com198e0542012-03-30 18:47:02 +0000208 int errors = 0;
caryclark@google.comc899ad92012-08-23 15:24:42 +0000209 for (int y = 0; y < bitHeight - 1; ++y) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000210 uint32_t* addr1 = bits.getAddr32(0, y);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000211 uint32_t* addr2 = bits.getAddr32(0, y + 1);
212 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
213 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
214 for (int x = 0; x < bitWidth - 1; ++x) {
215 // count 2x2 blocks
216 bool err = addr1[x] != addr3[x];
217 if (err) {
218 errors2 += addr1[x + 1] != addr3[x + 1]
219 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
220 errors++;
221 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000222 }
223 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000224 if (errors2 >= 6 || errors > 160) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000225 SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors);
226 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000227 error2x2 = errors2;
caryclark@google.com198e0542012-03-30 18:47:02 +0000228 return errors;
229}
230
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000231static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
232 SkPath& scaledTwo, int& error2x2) {
233 SkMatrix scale;
234 scaleMatrix(one, two, scale);
235 one.transform(scale, &scaledOne);
236 two.transform(scale, &scaledTwo);
237 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
238}
239
caryclark@google.comc899ad92012-08-23 15:24:42 +0000240bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000241 if (!drawPaths) {
caryclark@google.com752b60e2012-03-22 21:11:17 +0000242 return true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000243 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000244 const SkRect& bounds1 = one.getBounds();
245 const SkRect& bounds2 = two.getBounds();
246 SkRect larger = bounds1;
247 larger.join(bounds2);
248 SkBitmap bits;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000249 char out[256];
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000250 int bitWidth = SkScalarCeil(larger.width()) + 2;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000251 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
252 return false;
253 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000254 int bitHeight = SkScalarCeil(larger.height()) + 2;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000255 if (bitHeight >= (int) sizeof(out)) {
256 return false;
257 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000258 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
259 bits.allocPixels();
260 SkCanvas canvas(bits);
261 canvas.drawColor(SK_ColorWHITE);
262 SkPaint paint;
263 canvas.save();
264 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
265 canvas.drawPath(one, paint);
266 canvas.restore();
267 canvas.save();
caryclark@google.comfb173422012-04-10 18:28:55 +0000268 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000269 canvas.drawPath(two, paint);
270 canvas.restore();
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000271 for (int y = 0; y < bitHeight; ++y) {
272 uint32_t* addr1 = bits.getAddr32(0, y);
273 int x;
274 char* outPtr = out;
275 for (x = 0; x < bitWidth; ++x) {
276 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
277 }
278 *outPtr++ = '|';
279 for (x = bitWidth; x < bitWidth * 2; ++x) {
280 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
281 }
282 *outPtr++ = '\0';
283 SkDebugf("%s\n", out);
284 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000285 return true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000286}
287
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000288static void showSimplifiedPath(const SkPath& one, const SkPath& two,
caryclark@google.com4eeda372012-12-06 21:47:48 +0000289 const SkPath& scaledOne, const SkPath& scaledTwo) {
290 showPath(one, "original:");
291 showPath(two, "simplified:");
292 drawAsciiPaths(scaledOne, scaledTwo, true);
293}
294
reed@google.combe584d72012-09-28 19:48:09 +0000295int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000296 int errors2x2;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000297 SkPath scaledOne, scaledTwo;
298 int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000299 if (errors2x2 == 0) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000300 return 0;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000301 }
caryclark@google.com8f9f4682013-01-03 21:18:16 +0000302 const int MAX_ERRORS = 9;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000303 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
304 showSimplifiedPath(one, two, scaledOne, scaledTwo);
305 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000306 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000307 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
caryclark@google.com4eeda372012-12-06 21:47:48 +0000308 showSimplifiedPath(one, two, scaledOne, scaledTwo);
309 SkASSERT(0);
310 }
311 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
312}
313
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000314static void showShapeOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b,
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000315 const SkPath& scaledOne, const SkPath& scaledTwo, const ShapeOp shapeOp,
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000316 const SkMatrix& scale) {
caryclark@google.com4eeda372012-12-06 21:47:48 +0000317 SkASSERT((unsigned) shapeOp < sizeof(opStrs) / sizeof(opStrs[0]));
318 showPath(a, "minuend:");
319 SkDebugf("op: %s\n", opStrs[shapeOp]);
320 showPath(b, "subtrahend:");
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000321 // the region often isn't very helpful since it approximates curves with a lot of line-tos
322 if (0) showPath(scaledOne, "region:", scale);
caryclark@google.com4eeda372012-12-06 21:47:48 +0000323 showPath(two, "op result:");
324 drawAsciiPaths(scaledOne, scaledTwo, true);
325}
326
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000327static int comparePaths(const SkPath& one, const SkPath& scaledOne, const SkPath& two,
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000328 const SkPath& scaledTwo,
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000329 SkBitmap& bitmap, const SkPath& a, const SkPath& b, const ShapeOp shapeOp,
330 const SkMatrix& scale) {
caryclark@google.com4eeda372012-12-06 21:47:48 +0000331 int errors2x2;
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000332 int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com4eeda372012-12-06 21:47:48 +0000333 if (errors2x2 == 0) {
334 return 0;
335 }
336 const int MAX_ERRORS = 8;
337 if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000338 showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclark@google.com4eeda372012-12-06 21:47:48 +0000339 }
340 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
341 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000342 showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000343 SkASSERT(0);
344 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000345 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000346}
347
348// doesn't work yet
349void comparePathsTiny(const SkPath& one, const SkPath& two) {
350 const SkRect& bounds1 = one.getBounds();
351 const SkRect& bounds2 = two.getBounds();
352 SkRect larger = bounds1;
353 larger.join(bounds2);
354 SkBitmap bits;
355 int bitWidth = SkScalarCeil(larger.width()) + 2;
356 int bitHeight = SkScalarCeil(larger.height()) + 2;
357 bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight);
358 bits.allocPixels();
359 SkCanvas canvas(bits);
360 canvas.drawColor(SK_ColorWHITE);
361 SkPaint paint;
362 canvas.save();
363 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
364 canvas.drawPath(one, paint);
365 canvas.restore();
366 canvas.save();
367 canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1);
368 canvas.drawPath(two, paint);
369 canvas.restore();
370 for (int y = 0; y < bitHeight; ++y) {
371 uint8_t* addr1 = bits.getAddr1(0, y);
372 uint8_t* addr2 = bits.getAddr1(bitWidth, y);
caryclark@google.com1ab0aac2013-03-13 20:41:48 +0000373 for (unsigned x = 0; x < bits.rowBytes(); ++x) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000374 SkASSERT(addr1[x] == addr2[x]);
375 }
376 }
377}
378
reed@google.combe584d72012-09-28 19:48:09 +0000379bool testSimplify(const SkPath& path, bool fill, SkPath& out, SkBitmap& bitmap) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000380 if (gShowPath) {
381 showPath(path);
382 }
383 simplify(path, fill, out);
caryclark@google.com752b60e2012-03-22 21:11:17 +0000384 if (!gComparePaths) {
385 return true;
386 }
reed@google.combe584d72012-09-28 19:48:09 +0000387 return comparePaths(path, out, bitmap) == 0;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000388}
caryclark@google.com78e17132012-04-17 11:40:34 +0000389
caryclark@google.com24bec792012-08-20 12:43:57 +0000390bool testSimplifyx(SkPath& path, bool useXor, SkPath& out, State4& state,
caryclark@google.com59823f72012-08-09 18:17:47 +0000391 const char* pathStr) {
caryclark@google.com24bec792012-08-20 12:43:57 +0000392 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
393 path.setFillType(fillType);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000394 if (gShowPath) {
395 showPath(path);
396 }
397 simplifyx(path, out);
398 if (!gComparePaths) {
399 return true;
400 }
reed@google.combe584d72012-09-28 19:48:09 +0000401 int result = comparePaths(path, out, state.bitmap);
caryclark@google.com59823f72012-08-09 18:17:47 +0000402 if (result && gPathStrAssert) {
caryclark@google.comd1688742012-09-18 20:08:37 +0000403 SkDebugf("addTest %s\n", state.filename);
caryclark@google.com59823f72012-08-09 18:17:47 +0000404 char temp[8192];
405 bzero(temp, sizeof(temp));
406 SkMemoryWStream stream(temp, sizeof(temp));
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000407 const char* pathPrefix = NULL;
408 const char* nameSuffix = NULL;
409 if (fillType == SkPath::kEvenOdd_FillType) {
410 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
411 nameSuffix = "x";
412 }
413 const char testFunction[] = "testSimplifyx(path);";
414 outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, stream);
caryclark@google.com59823f72012-08-09 18:17:47 +0000415 SkDebugf(temp);
416 SkASSERT(0);
417 }
418 return result == 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000419}
420
421bool testSimplifyx(const SkPath& path) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000422 SkPath out;
423 simplifyx(path, out);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000424 SkBitmap bitmap;
reed@google.combe584d72012-09-28 19:48:09 +0000425 int result = comparePaths(path, out, bitmap);
caryclark@google.com24bec792012-08-20 12:43:57 +0000426 if (result && gPathStrAssert) {
427 SkASSERT(0);
428 }
429 return result == 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000430}
431
caryclark@google.com31143cf2012-11-09 22:14:19 +0000432bool testShapeOp(const SkPath& a, const SkPath& b, const ShapeOp shapeOp) {
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000433#if FORCE_RELEASE == 0
434 showPathData(a);
435 showOp(shapeOp);
436 showPathData(b);
437#endif
caryclark@google.com31143cf2012-11-09 22:14:19 +0000438 SkPath out;
439 operate(a, b, shapeOp, out);
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000440 SkPath pathOut, scaledPathOut;
caryclark@google.com31143cf2012-11-09 22:14:19 +0000441 SkRegion rgnA, rgnB, openClip, rgnOut;
442 openClip.setRect(-16000, -16000, 16000, 16000);
443 rgnA.setPath(a, openClip);
444 rgnB.setPath(b, openClip);
445 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
446 rgnOut.getBoundaryPath(&pathOut);
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000447
448 SkMatrix scale;
449 scaleMatrix(a, b, scale);
450 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
451 SkPath scaledA, scaledB;
452 scaledA.addPath(a, scale);
453 scaledA.setFillType(a.getFillType());
454 scaledB.addPath(b, scale);
455 scaledB.setFillType(b.getFillType());
456 scaledRgnA.setPath(scaledA, openClip);
457 scaledRgnB.setPath(scaledB, openClip);
458 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
459 scaledRgnOut.getBoundaryPath(&scaledPathOut);
caryclark@google.com31143cf2012-11-09 22:14:19 +0000460 SkBitmap bitmap;
caryclark@google.com86adc0d2013-01-30 12:50:38 +0000461 SkPath scaledOut;
462 scaledOut.addPath(out, scale);
463 scaledOut.setFillType(out.getFillType());
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000464 int result = comparePaths(pathOut, scaledPathOut, out, scaledOut, bitmap, a, b, shapeOp, scale);
caryclark@google.com31143cf2012-11-09 22:14:19 +0000465 if (result && gPathStrAssert) {
466 SkASSERT(0);
467 }
468 return result == 0;
469}
470
caryclark@google.com59823f72012-08-09 18:17:47 +0000471const int maxThreadsAllocated = 64;
472static int maxThreads = 1;
473static int threadIndex;
474State4 threadState[maxThreadsAllocated];
475static int testNumber;
476static const char* testName;
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000477static bool debugThreads = false;
caryclark@google.com59823f72012-08-09 18:17:47 +0000478
479State4* State4::queue = NULL;
480pthread_mutex_t State4::addQueue = PTHREAD_MUTEX_INITIALIZER;
481pthread_cond_t State4::checkQueue = PTHREAD_COND_INITIALIZER;
482
caryclark@google.com78e17132012-04-17 11:40:34 +0000483State4::State4() {
484 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150 * 2, 100);
485 bitmap.allocPixels();
caryclark@google.com78e17132012-04-17 11:40:34 +0000486}
487
caryclark@google.com59823f72012-08-09 18:17:47 +0000488void createThread(State4* statePtr, void* (*testFun)(void* )) {
489 int threadError = pthread_create(&statePtr->threadID, NULL, testFun,
caryclark@google.com78e17132012-04-17 11:40:34 +0000490 (void*) statePtr);
491 SkASSERT(!threadError);
492}
493
caryclark@google.com59823f72012-08-09 18:17:47 +0000494int dispatchTest4(void* (*testFun)(void* ), int a, int b, int c, int d) {
495 int testsRun = 0;
caryclark@google.com03f97062012-08-21 13:13:52 +0000496 State4* statePtr;
caryclark@google.com59823f72012-08-09 18:17:47 +0000497 if (!gRunTestsInOneThread) {
caryclark@google.com59823f72012-08-09 18:17:47 +0000498 pthread_mutex_lock(&State4::addQueue);
499 if (threadIndex < maxThreads) {
500 statePtr = &threadState[threadIndex];
501 statePtr->testsRun = 0;
502 statePtr->a = a;
503 statePtr->b = b;
504 statePtr->c = c;
505 statePtr->d = d;
506 statePtr->done = false;
507 statePtr->index = threadIndex;
508 statePtr->last = false;
509 if (debugThreads) SkDebugf("%s %d create done=%d last=%d\n", __FUNCTION__,
510 statePtr->index, statePtr->done, statePtr->last);
511 pthread_cond_init(&statePtr->initialized, NULL);
512 ++threadIndex;
513 createThread(statePtr, testFun);
514 } else {
515 while (!State4::queue) {
516 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
517 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
518 }
519 statePtr = State4::queue;
520 testsRun += statePtr->testsRun;
521 statePtr->testsRun = 0;
522 statePtr->a = a;
523 statePtr->b = b;
524 statePtr->c = c;
525 statePtr->d = d;
526 statePtr->done = false;
527 State4::queue = NULL;
528 for (int index = 0; index < maxThreads; ++index) {
529 if (threadState[index].done) {
530 State4::queue = &threadState[index];
531 }
532 }
533 if (debugThreads) SkDebugf("%s %d init done=%d last=%d queued=%d\n", __FUNCTION__,
534 statePtr->index, statePtr->done, statePtr->last,
535 State4::queue ? State4::queue->index : -1);
536 pthread_cond_signal(&statePtr->initialized);
537 }
538 pthread_mutex_unlock(&State4::addQueue);
539 } else {
caryclark@google.com03f97062012-08-21 13:13:52 +0000540 statePtr = &threadState[0];
caryclark@google.comc899ad92012-08-23 15:24:42 +0000541 testsRun += statePtr->testsRun;
caryclark@google.com03f97062012-08-21 13:13:52 +0000542 statePtr->testsRun = 0;
543 statePtr->a = a;
544 statePtr->b = b;
545 statePtr->c = c;
546 statePtr->d = d;
547 statePtr->done = false;
548 statePtr->index = threadIndex;
549 statePtr->last = false;
550 (*testFun)(statePtr);
caryclark@google.com78e17132012-04-17 11:40:34 +0000551 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000552 return testsRun;
553}
554
555void initializeTests(const char* test, size_t testNameSize) {
556 testName = test;
557 if (!gRunTestsInOneThread) {
558 int threads = -1;
559 size_t size = sizeof(threads);
560 sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0);
561 if (threads > 0) {
562 maxThreads = threads;
563 } else {
564 maxThreads = 8;
565 }
566 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000567 SkFILEStream inFile("../../experimental/Intersection/op.htm");
568 if (inFile.isValid()) {
569 SkTDArray<char> inData;
570 inData.setCount(inFile.getLength());
571 size_t inLen = inData.count();
572 inFile.read(inData.begin(), inLen);
573 inFile.setPath(NULL);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000574 char* insert = strstr(inData.begin(), marker);
caryclark@google.com03f97062012-08-21 13:13:52 +0000575 if (insert) {
576 insert += sizeof(marker) - 1;
577 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
578 testNumber = atoi(numLoc) + 1;
caryclark@google.com59823f72012-08-09 18:17:47 +0000579 }
580 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000581 const char* filename = preferredFilename;
582 SkFILEWStream preferredTest(filename);
583 if (!preferredTest.isValid()) {
584 filename = backupFilename;
585 SkFILEWStream backupTest(filename);
586 SkASSERT(backupTest.isValid());
587 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000588 for (int index = 0; index < maxThreads; ++index) {
589 State4* statePtr = &threadState[index];
590 strcpy(statePtr->filename, filename);
caryclark@google.com24bec792012-08-20 12:43:57 +0000591 size_t len = strlen(filename);
592 SkASSERT(statePtr->filename[len - 6] == 'X');
593 SkASSERT(statePtr->filename[len - 5] == 'X');
594 statePtr->filename[len - 6] = '0' + index / 10;
595 statePtr->filename[len - 5] = '0' + index % 10;
caryclark@google.com59823f72012-08-09 18:17:47 +0000596 }
caryclark@google.com78e17132012-04-17 11:40:34 +0000597 threadIndex = 0;
598}
caryclark@google.com59823f72012-08-09 18:17:47 +0000599
caryclark@google.com24bec792012-08-20 12:43:57 +0000600void outputProgress(const State4& state, const char* pathStr, SkPath::FillType pathFillType) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000601 if (gRunTestsInOneThread && gShowOutputProgress) {
caryclark@google.com03f97062012-08-21 13:13:52 +0000602 if (pathFillType == SkPath::kEvenOdd_FillType) {
603 SkDebugf(" path.setFillType(SkPath::kEvenOdd_FillType);\n", pathStr);
caryclark@google.com59823f72012-08-09 18:17:47 +0000604 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000605 SkDebugf("%s\n", pathStr);
caryclark@google.com59823f72012-08-09 18:17:47 +0000606 }
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000607 const char testFunction[] = "testSimplifyx(path);";
608 const char* pathPrefix = NULL;
609 const char* nameSuffix = NULL;
610 if (pathFillType == SkPath::kEvenOdd_FillType) {
611 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
612 nameSuffix = "x";
613 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000614 if (gUsePhysicalFiles) {
615 SkFILEWStream outFile(state.filename);
616 if (!outFile.isValid()) {
617 SkASSERT(0);
618 return;
619 }
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000620 outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outFile);
caryclark@google.com03f97062012-08-21 13:13:52 +0000621 return;
622 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000623 SkFILEWStream outRam(state.filename);
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000624 outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outRam);
caryclark@google.com59823f72012-08-09 18:17:47 +0000625}
626
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000627void outputProgress(const State4& state, const char* pathStr, ShapeOp op) {
628 SkString testFunc("testShapeOp(path, pathB, ");
629 testFunc += opStrs[op];
630 testFunc += ");";
631 const char* testFunction = testFunc.c_str();
632 if (gRunTestsInOneThread && gShowOutputProgress) {
633 SkDebugf("%s\n", pathStr);
634 SkDebugf(" %s\n", testFunction);
635 }
636 const char* nameSuffix = opSuffixes[op];
637 if (gUsePhysicalFiles) {
638 SkFILEWStream outFile(state.filename);
639 if (!outFile.isValid()) {
640 SkASSERT(0);
641 return;
642 }
643 outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outFile);
644 return;
645 }
646 SkFILEWStream outRam(state.filename);
647 outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outRam);
648}
649
650static void writeTestName(const char* nameSuffix, SkWStream& outFile) {
caryclark@google.com59823f72012-08-09 18:17:47 +0000651 outFile.writeText(testName);
652 outFile.writeDecAsText(testNumber);
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000653 if (nameSuffix) {
654 outFile.writeText(nameSuffix);
caryclark@google.com24bec792012-08-20 12:43:57 +0000655 }
656}
657
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000658void outputToStream(const State4& state, const char* pathStr, const char* pathPrefix,
659 const char* nameSuffix,
660 const char* testFunction, SkWStream& outFile) {
caryclark@google.com24bec792012-08-20 12:43:57 +0000661 outFile.writeText("<div id=\"");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000662 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000663 outFile.writeText("\">\n");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000664 if (pathPrefix) {
665 outFile.writeText(pathPrefix);
caryclark@google.com24bec792012-08-20 12:43:57 +0000666 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000667 outFile.writeText(pathStr);
668 outFile.writeText("</div>\n\n");
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000669
caryclark@google.com59823f72012-08-09 18:17:47 +0000670 outFile.writeText(marker);
671 outFile.writeText(" ");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000672 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000673 outFile.writeText(",\n\n\n");
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000674
caryclark@google.com59823f72012-08-09 18:17:47 +0000675 outFile.writeText("static void ");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000676 writeTestName(nameSuffix, outFile);
caryclark@google.comaa358312013-01-29 20:28:49 +0000677 outFile.writeText("() {\n SkPath path");
678 if (!pathPrefix) {
679 outFile.writeText(", pathB");
680 }
681 outFile.writeText(";\n");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000682 if (pathPrefix) {
683 outFile.writeText(pathPrefix);
caryclark@google.com24bec792012-08-20 12:43:57 +0000684 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000685 outFile.writeText(pathStr);
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000686 outFile.writeText(" ");
687 outFile.writeText(testFunction);
688 outFile.writeText("\n}\n\n");
caryclark@google.com59823f72012-08-09 18:17:47 +0000689 outFile.writeText("static void (*firstTest)() = ");
caryclark@google.comb1c42bb2012-11-16 13:16:41 +0000690 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000691 outFile.writeText(";\n\n");
692
693 outFile.writeText("static struct {\n");
694 outFile.writeText(" void (*fun)();\n");
695 outFile.writeText(" const char* str;\n");
696 outFile.writeText("} tests[] = {\n");
697 outFile.writeText(" TEST(");
caryclark@google.com8f9f4682013-01-03 21:18:16 +0000698 writeTestName(nameSuffix, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000699 outFile.writeText("),\n");
700 outFile.flush();
701}
702
703bool runNextTestSet(State4& state) {
704 if (gRunTestsInOneThread) {
705 return false;
706 }
707 pthread_mutex_lock(&State4::addQueue);
708 state.done = true;
709 State4::queue = &state;
710 if (debugThreads) SkDebugf("%s %d checkQueue done=%d last=%d\n", __FUNCTION__, state.index,
711 state.done, state.last);
712 pthread_cond_signal(&State4::checkQueue);
713 while (state.done && !state.last) {
714 if (debugThreads) SkDebugf("%s %d done=%d last=%d\n", __FUNCTION__, state.index, state.done, state.last);
715 pthread_cond_wait(&state.initialized, &State4::addQueue);
716 }
717 pthread_mutex_unlock(&State4::addQueue);
718 return !state.last;
719}
720
721int waitForCompletion() {
722 int testsRun = 0;
723 if (!gRunTestsInOneThread) {
724 pthread_mutex_lock(&State4::addQueue);
725 int runningThreads = maxThreads;
726 int index;
727 while (runningThreads > 0) {
728 while (!State4::queue) {
729 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
730 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
731 }
732 while (State4::queue) {
733 --runningThreads;
734 SkDebugf("•");
735 State4::queue->last = true;
caryclark@google.com24bec792012-08-20 12:43:57 +0000736 State4* next = NULL;
caryclark@google.com59823f72012-08-09 18:17:47 +0000737 for (index = 0; index < maxThreads; ++index) {
738 State4& test = threadState[index];
739 if (test.done && !test.last) {
740 next = &test;
741 }
742 }
743 if (debugThreads) SkDebugf("%s %d next=%d deQueue\n", __FUNCTION__,
744 State4::queue->index, next ? next->index : -1);
745 pthread_cond_signal(&State4::queue->initialized);
746 State4::queue = next;
747 }
748 }
749 pthread_mutex_unlock(&State4::addQueue);
750 for (index = 0; index < maxThreads; ++index) {
751 pthread_join(threadState[index].threadID, NULL);
752 testsRun += threadState[index].testsRun;
753 }
754 SkDebugf("\n");
755 }
756#ifdef SK_DEBUG
757 gDebugMaxWindSum = SK_MaxS32;
758 gDebugMaxWindValue = SK_MaxS32;
759#endif
760 return testsRun;
761}