blob: 397bfcacbc41705fab2a2202592c42d55fd18633 [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
32static const char preferredFilename[] = "/flash/debug/XX.txt";
33static const char backupFilename[] = "../../experimental/Intersection/debugXX.txt";
caryclark@google.com59823f72012-08-09 18:17:47 +000034
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000035static bool gShowPath = false;
caryclark@google.com198e0542012-03-30 18:47:02 +000036static bool gComparePaths = true;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000037//static bool gDrawLastAsciiPaths = true;
caryclark@google.comc899ad92012-08-23 15:24:42 +000038//static bool gDrawAllAsciiPaths = false;
39static bool gShowOutputProgress = false;
40static bool gShowAsciiPaths = true;
caryclark@google.com59823f72012-08-09 18:17:47 +000041static bool gComparePathsAssert = false;
42static bool gPathStrAssert = true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +000043
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000044void showPath(const SkPath& path, const char* str) {
caryclark@google.com752b60e2012-03-22 21:11:17 +000045 SkDebugf("%s\n", !str ? "original:" : str);
caryclark@google.comcd4421d2012-03-01 19:16:31 +000046 SkPath::Iter iter(path, true);
47 uint8_t verb;
48 SkPoint pts[4];
49 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
50 switch (verb) {
51 case SkPath::kMove_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +000052 SkDebugf("path.moveTo(%1.9g, %1.9g);\n", pts[0].fX, pts[0].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +000053 continue;
54 case SkPath::kLine_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +000055 SkDebugf("path.lineTo(%1.9g, %1.9g);\n", pts[1].fX, pts[1].fY);
caryclark@google.comcd4421d2012-03-01 19:16:31 +000056 break;
57 case SkPath::kQuad_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +000058 SkDebugf("path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n",
caryclark@google.comcd4421d2012-03-01 19:16:31 +000059 pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
60 break;
61 case SkPath::kCubic_Verb:
caryclark@google.comd88e0892012-03-27 13:23:51 +000062 SkDebugf("path.cubicTo(%1.9g, %1.9g, %1.9g, %1.9g);\n",
caryclark@google.comcd4421d2012-03-01 19:16:31 +000063 pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
64 pts[3].fX, pts[3].fY);
65 break;
66 case SkPath::kClose_Verb:
67 SkDebugf("path.close();\n");
68 continue;
69 default:
70 SkDEBUGFAIL("bad verb");
71 return;
72 }
73 }
74}
75
caryclark@google.com198e0542012-03-30 18:47:02 +000076static int pathsDrawTheSame(const SkPath& one, const SkPath& two,
reed@google.combe584d72012-09-28 19:48:09 +000077 SkBitmap& bits, int& error2x2) {
caryclark@google.comc899ad92012-08-23 15:24:42 +000078 const int bitWidth = 64;
79 const int bitHeight = 64;
80 if (bits.width() == 0) {
caryclark@google.com198e0542012-03-30 18:47:02 +000081 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
82 bits.allocPixels();
caryclark@google.com198e0542012-03-30 18:47:02 +000083 }
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +000084
caryclark@google.comc899ad92012-08-23 15:24:42 +000085 SkRect larger = one.getBounds();
86 larger.join(two.getBounds());
87 SkScalar largerWidth = larger.width();
88 if (largerWidth < 4) {
89 largerWidth = 4;
90 }
91 SkScalar largerHeight = larger.height();
92 if (largerHeight < 4) {
93 largerHeight = 4;
94 }
95 SkScalar hScale = (bitWidth - 2) / largerWidth;
96 SkScalar vScale = (bitHeight - 2) / largerHeight;
97 SkMatrix scale;
98 scale.reset();
99 scale.preScale(hScale, vScale);
100 SkPath scaledOne, scaledTwo;
101 one.transform(scale, &scaledOne);
102 two.transform(scale, &scaledTwo);
103 const SkRect& bounds1 = scaledOne.getBounds();
reed@google.combe584d72012-09-28 19:48:09 +0000104
105 SkCanvas canvas(bits);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000106 canvas.drawColor(SK_ColorWHITE);
107 SkPaint paint;
108 canvas.save();
109 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000110 canvas.drawPath(scaledOne, paint);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000111 canvas.restore();
112 canvas.save();
113 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000114 canvas.drawPath(scaledTwo, paint);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000115 canvas.restore();
caryclark@google.comc899ad92012-08-23 15:24:42 +0000116 int errors2 = 0;
caryclark@google.com198e0542012-03-30 18:47:02 +0000117 int errors = 0;
caryclark@google.comc899ad92012-08-23 15:24:42 +0000118 for (int y = 0; y < bitHeight - 1; ++y) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000119 uint32_t* addr1 = bits.getAddr32(0, y);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000120 uint32_t* addr2 = bits.getAddr32(0, y + 1);
121 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
122 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
123 for (int x = 0; x < bitWidth - 1; ++x) {
124 // count 2x2 blocks
125 bool err = addr1[x] != addr3[x];
126 if (err) {
127 errors2 += addr1[x + 1] != addr3[x + 1]
128 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
129 errors++;
130 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000131 }
132 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000133 if (errors2 >= 6 || errors > 160) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000134 SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors);
135 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000136 if (errors2 >= 7) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000137 drawAsciiPaths(scaledOne, scaledTwo, true);
138 }
139 error2x2 = errors2;
caryclark@google.com198e0542012-03-30 18:47:02 +0000140 return errors;
141}
142
caryclark@google.comc899ad92012-08-23 15:24:42 +0000143bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000144 if (!drawPaths) {
caryclark@google.com752b60e2012-03-22 21:11:17 +0000145 return true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000146 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000147 if (gShowAsciiPaths) {
148 showPath(one, "one:");
149 showPath(two, "two:");
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000150 }
151 const SkRect& bounds1 = one.getBounds();
152 const SkRect& bounds2 = two.getBounds();
153 SkRect larger = bounds1;
154 larger.join(bounds2);
155 SkBitmap bits;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000156 char out[256];
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000157 int bitWidth = SkScalarCeil(larger.width()) + 2;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000158 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
159 return false;
160 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000161 int bitHeight = SkScalarCeil(larger.height()) + 2;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000162 if (bitHeight >= (int) sizeof(out)) {
163 return false;
164 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000165 bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
166 bits.allocPixels();
167 SkCanvas canvas(bits);
168 canvas.drawColor(SK_ColorWHITE);
169 SkPaint paint;
170 canvas.save();
171 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
172 canvas.drawPath(one, paint);
173 canvas.restore();
174 canvas.save();
caryclark@google.comfb173422012-04-10 18:28:55 +0000175 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000176 canvas.drawPath(two, paint);
177 canvas.restore();
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000178 for (int y = 0; y < bitHeight; ++y) {
179 uint32_t* addr1 = bits.getAddr32(0, y);
180 int x;
181 char* outPtr = out;
182 for (x = 0; x < bitWidth; ++x) {
183 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
184 }
185 *outPtr++ = '|';
186 for (x = bitWidth; x < bitWidth * 2; ++x) {
187 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
188 }
189 *outPtr++ = '\0';
190 SkDebugf("%s\n", out);
191 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000192 return true;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000193}
194
reed@google.combe584d72012-09-28 19:48:09 +0000195int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000196 int errors2x2;
reed@google.combe584d72012-09-28 19:48:09 +0000197 int errors = pathsDrawTheSame(one, two, bitmap, errors2x2);
caryclark@google.comc899ad92012-08-23 15:24:42 +0000198 if (errors2x2 == 0) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000199 return 0;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000200 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000201 const int MAX_ERRORS = 8;
caryclark@google.comc899ad92012-08-23 15:24:42 +0000202 if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000203 SkDebugf("%s errors=%d\n", __FUNCTION__, errors);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000204 showPath(one);
205 showPath(two, "simplified:");
206 SkASSERT(0);
207 }
caryclark@google.comc899ad92012-08-23 15:24:42 +0000208 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000209}
210
211// doesn't work yet
212void comparePathsTiny(const SkPath& one, const SkPath& two) {
213 const SkRect& bounds1 = one.getBounds();
214 const SkRect& bounds2 = two.getBounds();
215 SkRect larger = bounds1;
216 larger.join(bounds2);
217 SkBitmap bits;
218 int bitWidth = SkScalarCeil(larger.width()) + 2;
219 int bitHeight = SkScalarCeil(larger.height()) + 2;
220 bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight);
221 bits.allocPixels();
222 SkCanvas canvas(bits);
223 canvas.drawColor(SK_ColorWHITE);
224 SkPaint paint;
225 canvas.save();
226 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
227 canvas.drawPath(one, paint);
228 canvas.restore();
229 canvas.save();
230 canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1);
231 canvas.drawPath(two, paint);
232 canvas.restore();
233 for (int y = 0; y < bitHeight; ++y) {
234 uint8_t* addr1 = bits.getAddr1(0, y);
235 uint8_t* addr2 = bits.getAddr1(bitWidth, y);
236 for (int x = 0; x < bits.rowBytes(); ++x) {
237 SkASSERT(addr1[x] == addr2[x]);
238 }
239 }
240}
241
reed@google.combe584d72012-09-28 19:48:09 +0000242bool testSimplify(const SkPath& path, bool fill, SkPath& out, SkBitmap& bitmap) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000243 if (gShowPath) {
244 showPath(path);
245 }
246 simplify(path, fill, out);
caryclark@google.com752b60e2012-03-22 21:11:17 +0000247 if (!gComparePaths) {
248 return true;
249 }
reed@google.combe584d72012-09-28 19:48:09 +0000250 return comparePaths(path, out, bitmap) == 0;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000251}
caryclark@google.com78e17132012-04-17 11:40:34 +0000252
caryclark@google.com24bec792012-08-20 12:43:57 +0000253bool testSimplifyx(SkPath& path, bool useXor, SkPath& out, State4& state,
caryclark@google.com59823f72012-08-09 18:17:47 +0000254 const char* pathStr) {
caryclark@google.com24bec792012-08-20 12:43:57 +0000255 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
256 path.setFillType(fillType);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000257 if (gShowPath) {
258 showPath(path);
259 }
260 simplifyx(path, out);
261 if (!gComparePaths) {
262 return true;
263 }
reed@google.combe584d72012-09-28 19:48:09 +0000264 int result = comparePaths(path, out, state.bitmap);
caryclark@google.com59823f72012-08-09 18:17:47 +0000265 if (result && gPathStrAssert) {
caryclark@google.comd1688742012-09-18 20:08:37 +0000266 SkDebugf("addTest %s\n", state.filename);
caryclark@google.com59823f72012-08-09 18:17:47 +0000267 char temp[8192];
268 bzero(temp, sizeof(temp));
269 SkMemoryWStream stream(temp, sizeof(temp));
caryclark@google.com24bec792012-08-20 12:43:57 +0000270 outputToStream(state, pathStr, fillType, stream);
caryclark@google.com59823f72012-08-09 18:17:47 +0000271 SkDebugf(temp);
272 SkASSERT(0);
273 }
274 return result == 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000275}
276
277bool testSimplifyx(const SkPath& path) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000278 SkPath out;
279 simplifyx(path, out);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000280 SkBitmap bitmap;
reed@google.combe584d72012-09-28 19:48:09 +0000281 int result = comparePaths(path, out, bitmap);
caryclark@google.com24bec792012-08-20 12:43:57 +0000282 if (result && gPathStrAssert) {
283 SkASSERT(0);
284 }
285 return result == 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000286}
287
caryclark@google.com59823f72012-08-09 18:17:47 +0000288const int maxThreadsAllocated = 64;
289static int maxThreads = 1;
290static int threadIndex;
291State4 threadState[maxThreadsAllocated];
292static int testNumber;
293static const char* testName;
caryclark@google.comd1688742012-09-18 20:08:37 +0000294static bool debugThreads = true;
caryclark@google.com59823f72012-08-09 18:17:47 +0000295
296State4* State4::queue = NULL;
297pthread_mutex_t State4::addQueue = PTHREAD_MUTEX_INITIALIZER;
298pthread_cond_t State4::checkQueue = PTHREAD_COND_INITIALIZER;
299
caryclark@google.com78e17132012-04-17 11:40:34 +0000300State4::State4() {
301 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150 * 2, 100);
302 bitmap.allocPixels();
caryclark@google.com78e17132012-04-17 11:40:34 +0000303}
304
caryclark@google.com59823f72012-08-09 18:17:47 +0000305void createThread(State4* statePtr, void* (*testFun)(void* )) {
306 int threadError = pthread_create(&statePtr->threadID, NULL, testFun,
caryclark@google.com78e17132012-04-17 11:40:34 +0000307 (void*) statePtr);
308 SkASSERT(!threadError);
309}
310
caryclark@google.com59823f72012-08-09 18:17:47 +0000311int dispatchTest4(void* (*testFun)(void* ), int a, int b, int c, int d) {
312 int testsRun = 0;
caryclark@google.com03f97062012-08-21 13:13:52 +0000313 State4* statePtr;
caryclark@google.com59823f72012-08-09 18:17:47 +0000314 if (!gRunTestsInOneThread) {
caryclark@google.com59823f72012-08-09 18:17:47 +0000315 pthread_mutex_lock(&State4::addQueue);
316 if (threadIndex < maxThreads) {
317 statePtr = &threadState[threadIndex];
318 statePtr->testsRun = 0;
319 statePtr->a = a;
320 statePtr->b = b;
321 statePtr->c = c;
322 statePtr->d = d;
323 statePtr->done = false;
324 statePtr->index = threadIndex;
325 statePtr->last = false;
326 if (debugThreads) SkDebugf("%s %d create done=%d last=%d\n", __FUNCTION__,
327 statePtr->index, statePtr->done, statePtr->last);
328 pthread_cond_init(&statePtr->initialized, NULL);
329 ++threadIndex;
330 createThread(statePtr, testFun);
331 } else {
332 while (!State4::queue) {
333 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
334 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
335 }
336 statePtr = State4::queue;
337 testsRun += statePtr->testsRun;
338 statePtr->testsRun = 0;
339 statePtr->a = a;
340 statePtr->b = b;
341 statePtr->c = c;
342 statePtr->d = d;
343 statePtr->done = false;
344 State4::queue = NULL;
345 for (int index = 0; index < maxThreads; ++index) {
346 if (threadState[index].done) {
347 State4::queue = &threadState[index];
348 }
349 }
350 if (debugThreads) SkDebugf("%s %d init done=%d last=%d queued=%d\n", __FUNCTION__,
351 statePtr->index, statePtr->done, statePtr->last,
352 State4::queue ? State4::queue->index : -1);
353 pthread_cond_signal(&statePtr->initialized);
354 }
355 pthread_mutex_unlock(&State4::addQueue);
356 } else {
caryclark@google.com03f97062012-08-21 13:13:52 +0000357 statePtr = &threadState[0];
caryclark@google.comc899ad92012-08-23 15:24:42 +0000358 testsRun += statePtr->testsRun;
caryclark@google.com03f97062012-08-21 13:13:52 +0000359 statePtr->testsRun = 0;
360 statePtr->a = a;
361 statePtr->b = b;
362 statePtr->c = c;
363 statePtr->d = d;
364 statePtr->done = false;
365 statePtr->index = threadIndex;
366 statePtr->last = false;
367 (*testFun)(statePtr);
caryclark@google.com78e17132012-04-17 11:40:34 +0000368 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000369 return testsRun;
370}
371
372void initializeTests(const char* test, size_t testNameSize) {
373 testName = test;
374 if (!gRunTestsInOneThread) {
375 int threads = -1;
376 size_t size = sizeof(threads);
377 sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0);
378 if (threads > 0) {
379 maxThreads = threads;
380 } else {
381 maxThreads = 8;
382 }
383 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000384 SkFILEStream inFile("../../experimental/Intersection/op.htm");
385 if (inFile.isValid()) {
386 SkTDArray<char> inData;
387 inData.setCount(inFile.getLength());
388 size_t inLen = inData.count();
389 inFile.read(inData.begin(), inLen);
390 inFile.setPath(NULL);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000391 char* insert = strstr(inData.begin(), marker);
caryclark@google.com03f97062012-08-21 13:13:52 +0000392 if (insert) {
393 insert += sizeof(marker) - 1;
394 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
395 testNumber = atoi(numLoc) + 1;
caryclark@google.com59823f72012-08-09 18:17:47 +0000396 }
397 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000398 const char* filename = preferredFilename;
399 SkFILEWStream preferredTest(filename);
400 if (!preferredTest.isValid()) {
401 filename = backupFilename;
402 SkFILEWStream backupTest(filename);
403 SkASSERT(backupTest.isValid());
404 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000405 for (int index = 0; index < maxThreads; ++index) {
406 State4* statePtr = &threadState[index];
407 strcpy(statePtr->filename, filename);
caryclark@google.com24bec792012-08-20 12:43:57 +0000408 size_t len = strlen(filename);
409 SkASSERT(statePtr->filename[len - 6] == 'X');
410 SkASSERT(statePtr->filename[len - 5] == 'X');
411 statePtr->filename[len - 6] = '0' + index / 10;
412 statePtr->filename[len - 5] = '0' + index % 10;
caryclark@google.com59823f72012-08-09 18:17:47 +0000413 }
caryclark@google.com78e17132012-04-17 11:40:34 +0000414 threadIndex = 0;
415}
caryclark@google.com59823f72012-08-09 18:17:47 +0000416
caryclark@google.com24bec792012-08-20 12:43:57 +0000417void outputProgress(const State4& state, const char* pathStr, SkPath::FillType pathFillType) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000418 if (gRunTestsInOneThread && gShowOutputProgress) {
caryclark@google.com03f97062012-08-21 13:13:52 +0000419 if (pathFillType == SkPath::kEvenOdd_FillType) {
420 SkDebugf(" path.setFillType(SkPath::kEvenOdd_FillType);\n", pathStr);
caryclark@google.com59823f72012-08-09 18:17:47 +0000421 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000422 SkDebugf("%s\n", pathStr);
caryclark@google.com59823f72012-08-09 18:17:47 +0000423 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000424 SkFILEWStream outFile(state.filename);
425 if (!outFile.isValid()) {
426 SkASSERT(0);
427 return;
428 }
429 outputToStream(state, pathStr, pathFillType, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000430}
431
caryclark@google.com24bec792012-08-20 12:43:57 +0000432static void writeTestName(SkPath::FillType pathFillType, SkWStream& outFile) {
caryclark@google.com59823f72012-08-09 18:17:47 +0000433 outFile.writeText(testName);
434 outFile.writeDecAsText(testNumber);
caryclark@google.com24bec792012-08-20 12:43:57 +0000435 if (pathFillType == SkPath::kEvenOdd_FillType) {
436 outFile.writeText("x");
437 }
438}
439
440void outputToStream(const State4& state, const char* pathStr, SkPath::FillType pathFillType, SkWStream& outFile) {
441 outFile.writeText("<div id=\"");
442 writeTestName(pathFillType, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000443 outFile.writeText("\">\n");
caryclark@google.com24bec792012-08-20 12:43:57 +0000444 if (pathFillType == SkPath::kEvenOdd_FillType) {
445 outFile.writeText(" path.setFillType(SkPath::kEvenOdd_FillType);\n");
446 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000447 outFile.writeText(pathStr);
448 outFile.writeText("</div>\n\n");
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000449
caryclark@google.com59823f72012-08-09 18:17:47 +0000450 outFile.writeText(marker);
451 outFile.writeText(" ");
caryclark@google.com24bec792012-08-20 12:43:57 +0000452 writeTestName(pathFillType, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000453 outFile.writeText(",\n\n\n");
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000454
caryclark@google.com59823f72012-08-09 18:17:47 +0000455 outFile.writeText("static void ");
caryclark@google.com24bec792012-08-20 12:43:57 +0000456 writeTestName(pathFillType, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000457 outFile.writeText("() {\n SkPath path;\n");
caryclark@google.com24bec792012-08-20 12:43:57 +0000458 if (pathFillType == SkPath::kEvenOdd_FillType) {
459 outFile.writeText(" path.setFillType(SkPath::kEvenOdd_FillType);\n");
460 }
caryclark@google.com59823f72012-08-09 18:17:47 +0000461 outFile.writeText(pathStr);
462 outFile.writeText(" testSimplifyx(path);\n}\n\n");
463 outFile.writeText("static void (*firstTest)() = ");
caryclark@google.com24bec792012-08-20 12:43:57 +0000464 writeTestName(pathFillType, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000465 outFile.writeText(";\n\n");
466
467 outFile.writeText("static struct {\n");
468 outFile.writeText(" void (*fun)();\n");
469 outFile.writeText(" const char* str;\n");
470 outFile.writeText("} tests[] = {\n");
471 outFile.writeText(" TEST(");
caryclark@google.com24bec792012-08-20 12:43:57 +0000472 writeTestName(pathFillType, outFile);
caryclark@google.com59823f72012-08-09 18:17:47 +0000473 outFile.writeText("),\n");
474 outFile.flush();
475}
476
477bool runNextTestSet(State4& state) {
478 if (gRunTestsInOneThread) {
479 return false;
480 }
481 pthread_mutex_lock(&State4::addQueue);
482 state.done = true;
483 State4::queue = &state;
484 if (debugThreads) SkDebugf("%s %d checkQueue done=%d last=%d\n", __FUNCTION__, state.index,
485 state.done, state.last);
486 pthread_cond_signal(&State4::checkQueue);
487 while (state.done && !state.last) {
488 if (debugThreads) SkDebugf("%s %d done=%d last=%d\n", __FUNCTION__, state.index, state.done, state.last);
489 pthread_cond_wait(&state.initialized, &State4::addQueue);
490 }
491 pthread_mutex_unlock(&State4::addQueue);
492 return !state.last;
493}
494
495int waitForCompletion() {
496 int testsRun = 0;
497 if (!gRunTestsInOneThread) {
498 pthread_mutex_lock(&State4::addQueue);
499 int runningThreads = maxThreads;
500 int index;
501 while (runningThreads > 0) {
502 while (!State4::queue) {
503 if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__);
504 pthread_cond_wait(&State4::checkQueue, &State4::addQueue);
505 }
506 while (State4::queue) {
507 --runningThreads;
508 SkDebugf("•");
509 State4::queue->last = true;
caryclark@google.com24bec792012-08-20 12:43:57 +0000510 State4* next = NULL;
caryclark@google.com59823f72012-08-09 18:17:47 +0000511 for (index = 0; index < maxThreads; ++index) {
512 State4& test = threadState[index];
513 if (test.done && !test.last) {
514 next = &test;
515 }
516 }
517 if (debugThreads) SkDebugf("%s %d next=%d deQueue\n", __FUNCTION__,
518 State4::queue->index, next ? next->index : -1);
519 pthread_cond_signal(&State4::queue->initialized);
520 State4::queue = next;
521 }
522 }
523 pthread_mutex_unlock(&State4::addQueue);
524 for (index = 0; index < maxThreads; ++index) {
525 pthread_join(threadState[index].threadID, NULL);
526 testsRun += threadState[index].testsRun;
527 }
528 SkDebugf("\n");
529 }
530#ifdef SK_DEBUG
531 gDebugMaxWindSum = SK_MaxS32;
532 gDebugMaxWindValue = SK_MaxS32;
533#endif
534 return testsRun;
535}