blob: 27ec5be90fa344fa2c6f3aed1adf189b57368704 [file] [log] [blame]
caryclark@google.comd88e0892012-03-27 13:23:51 +00001#include "EdgeWalker_Test.h"
2#include "Intersection_Tests.h"
3#include <assert.h>
4#include <pthread.h>
5
6struct State {
7 int a;
8 int b;
9 int c;
10 int d;
11 pthread_t threadID;
12 bool abcIsATriangle;
13};
14
15void createThread(State* statePtr, void* (*test)(void* )) {
16 int threadError = pthread_create(&statePtr->threadID, NULL, test,
17 (void*) statePtr);
18 SkASSERT(!threadError);
19}
20
21void waitForCompletion(State threadState[], int& threadIndex) {
22 for (int index = 0; index < threadIndex; ++index) {
23 pthread_join(threadState[index].threadID, NULL);
24 }
25 SkDebugf(".");
26 threadIndex = 0;
27}
28
29static void* testSimplify4x4QuadralateralsMain(void* data)
30{
31 char pathStr[1024];
32 bzero(pathStr, sizeof(pathStr));
33 SkASSERT(data);
34 State& state = *(State*) data;
35 int ax = state.a & 0x03;
36 int ay = state.a >> 2;
37 int bx = state.b & 0x03;
38 int by = state.b >> 2;
39 int cx = state.c & 0x03;
40 int cy = state.c >> 2;
41 int dx = state.d & 0x03;
42 int dy = state.d >> 2;
43 for (int e = 0 ; e < 16; ++e) {
44 int ex = e & 0x03;
45 int ey = e >> 2;
46 for (int f = e ; f < 16; ++f) {
47 int fx = f & 0x03;
48 int fy = f >> 2;
49 for (int g = f ; g < 16; ++g) {
50 int gx = g & 0x03;
51 int gy = g >> 2;
52 for (int h = g ; h < 16; ++h) {
53 int hx = h & 0x03;
54 int hy = h >> 2;
55 SkPath path, out;
56 path.setFillType(SkPath::kWinding_FillType);
57 path.moveTo(ax, ay);
58 path.lineTo(bx, by);
59 path.lineTo(cx, cy);
60 path.lineTo(dx, dy);
61 path.close();
62 path.moveTo(ex, ey);
63 path.lineTo(fx, fy);
64 path.lineTo(gx, gy);
65 path.lineTo(hx, hy);
66 path.close();
67 if (1) { // gdb: set print elements 400
68 char* str = pathStr;
69 str += sprintf(str, " path.moveTo(%d, %d);\n", ax, ay);
70 str += sprintf(str, " path.lineTo(%d, %d);\n", bx, by);
71 str += sprintf(str, " path.lineTo(%d, %d);\n", cx, cy);
72 str += sprintf(str, " path.lineTo(%d, %d);\n", dx, dy);
73 str += sprintf(str, " path.close();\n");
74 str += sprintf(str, " path.moveTo(%d, %d);\n", ex, ey);
75 str += sprintf(str, " path.lineTo(%d, %d);\n", fx, fy);
76 str += sprintf(str, " path.lineTo(%d, %d);\n", gx, gy);
77 str += sprintf(str, " path.lineTo(%d, %d);\n", hx, hy);
78 str += sprintf(str, " path.close();");
79 }
80 if (!testSimplify(path, true, out)) {
81 SkDebugf("*/\n{ SkPath::kWinding_FillType, %d, %d, %d, %d,"
82 " %d, %d, %d, %d },\n/*\n", state.a, state.b, state.c, state.d,
83 e, f, g, h);
84 }
85 path.setFillType(SkPath::kEvenOdd_FillType);
86 if (!testSimplify(path, true, out)) {
87 SkDebugf("*/\n{ SkPath::kEvenOdd_FillType, %d, %d, %d, %d,"
88 " %d, %d, %d, %d },\n/*\n", state.a, state.b, state.c, state.d,
89 e, f, g, h);
90 }
91 }
92 }
93 }
94 }
95 return NULL;
96}
97
98const int maxThreads = gShowDebugf ? 1 : 24;
99
100void Simplify4x4QuadralateralsThreaded_Test()
101{
102 State threadState[maxThreads];
103 int threadIndex = 0;
104 for (int a = 0; a < 16; ++a) {
105 for (int b = a ; b < 16; ++b) {
106 for (int c = b ; c < 16; ++c) {
107 for (int d = c; d < 16; ++d) {
108 State* statePtr = &threadState[threadIndex];
109 statePtr->a = a;
110 statePtr->b = b;
111 statePtr->c = c;
112 statePtr->d = d;
113 if (maxThreads > 1) {
114 createThread(statePtr, testSimplify4x4QuadralateralsMain);
115 if (++threadIndex >= maxThreads) {
116 waitForCompletion(threadState, threadIndex);
117 }
118 } else {
119 testSimplify4x4QuadralateralsMain(statePtr);
120 }
121 }
122 }
123 }
124 }
125 waitForCompletion(threadState, threadIndex);
126}
127
128
129static void* testSimplify4x4NondegeneratesMain(void* data) {
130 char pathStr[1024];
131 bzero(pathStr, sizeof(pathStr));
132 SkASSERT(data);
133 State& state = *(State*) data;
134 int ax = state.a & 0x03;
135 int ay = state.a >> 2;
136 int bx = state.b & 0x03;
137 int by = state.b >> 2;
138 int cx = state.c & 0x03;
139 int cy = state.c >> 2;
140 for (int d = 0; d < 15; ++d) {
141 int dx = d & 0x03;
142 int dy = d >> 2;
143 for (int e = d + 1; e < 16; ++e) {
144 int ex = e & 0x03;
145 int ey = e >> 2;
146 for (int f = d + 1; f < 16; ++f) {
147 if (e == f) {
148 continue;
149 }
150 int fx = f & 0x03;
151 int fy = f >> 2;
152 if ((ex - dx) * (fy - dy) == (ey - dy) * (fx - dx)) {
153 continue;
154 }
155 SkPath path, out;
156 path.setFillType(SkPath::kWinding_FillType);
157 path.moveTo(ax, ay);
158 path.lineTo(bx, by);
159 path.lineTo(cx, cy);
160 path.close();
161 path.moveTo(dx, dy);
162 path.lineTo(ex, ey);
163 path.lineTo(fx, fy);
164 path.close();
165 if (1) {
166 char* str = pathStr;
167 str += sprintf(str, " path.moveTo(%d, %d);\n", ax, ay);
168 str += sprintf(str, " path.lineTo(%d, %d);\n", bx, by);
169 str += sprintf(str, " path.lineTo(%d, %d);\n", cx, cy);
170 str += sprintf(str, " path.close();\n");
171 str += sprintf(str, " path.moveTo(%d, %d);\n", dx, dy);
172 str += sprintf(str, " path.lineTo(%d, %d);\n", ex, ey);
173 str += sprintf(str, " path.lineTo(%d, %d);\n", fx, fy);
174 str += sprintf(str, " path.close();");
175 }
176 testSimplify(path, true, out);
177 path.setFillType(SkPath::kEvenOdd_FillType);
178 testSimplify(path, true, out);
179 }
180 }
181 }
182 return NULL;
183}
184
185void SimplifyNondegenerate4x4TrianglesThreaded_Test() {
186 State threadState[maxThreads];
187 int threadIndex = 0;
188 for (int a = 0; a < 15; ++a) {
189 int ax = a & 0x03;
190 int ay = a >> 2;
191 for (int b = a + 1; b < 16; ++b) {
192 int bx = b & 0x03;
193 int by = b >> 2;
194 for (int c = a + 1; c < 16; ++c) {
195 if (b == c) {
196 continue;
197 }
198 int cx = c & 0x03;
199 int cy = c >> 2;
200 if ((bx - ax) * (cy - ay) == (by - ay) * (cx - ax)) {
201 continue;
202 }
203 State* statePtr = &threadState[threadIndex];
204 statePtr->a = a;
205 statePtr->b = b;
206 statePtr->c = c;
207 if (maxThreads > 1) {
208 createThread(statePtr, testSimplify4x4NondegeneratesMain);
209 if (++threadIndex >= maxThreads) {
210 waitForCompletion(threadState, threadIndex);
211 }
212 } else {
213 testSimplify4x4NondegeneratesMain(statePtr);
214 }
215 }
216 }
217 }
218 waitForCompletion(threadState, threadIndex);
219}
220
221static void* testSimplify4x4DegeneratesMain(void* data) {
222 char pathStr[1024];
223 bzero(pathStr, sizeof(pathStr));
224 SkASSERT(data);
225 State& state = *(State*) data;
226 int ax = state.a & 0x03;
227 int ay = state.a >> 2;
228 int bx = state.b & 0x03;
229 int by = state.b >> 2;
230 int cx = state.c & 0x03;
231 int cy = state.c >> 2;
232 for (int d = 0; d < 16; ++d) {
233 int dx = d & 0x03;
234 int dy = d >> 2;
235 for (int e = d ; e < 16; ++e) {
236 int ex = e & 0x03;
237 int ey = e >> 2;
238 for (int f = d ; f < 16; ++f) {
239 int fx = f & 0x03;
240 int fy = f >> 2;
241 if (state.abcIsATriangle && (ex - dx) * (fy - dy)
242 != (ey - dy) * (fx - dx)) {
243 continue;
244 }
245 SkPath path, out;
246 path.setFillType(SkPath::kWinding_FillType);
247 path.moveTo(ax, ay);
248 path.lineTo(bx, by);
249 path.lineTo(cx, cy);
250 path.close();
251 path.moveTo(dx, dy);
252 path.lineTo(ex, ey);
253 path.lineTo(fx, fy);
254 path.close();
255 if (1) {
256 char* str = pathStr;
257 str += sprintf(str, " path.moveTo(%d, %d);\n", ax, ay);
258 str += sprintf(str, " path.lineTo(%d, %d);\n", bx, by);
259 str += sprintf(str, " path.lineTo(%d, %d);\n", cx, cy);
260 str += sprintf(str, " path.close();\n");
261 str += sprintf(str, " path.moveTo(%d, %d);\n", dx, dy);
262 str += sprintf(str, " path.lineTo(%d, %d);\n", ex, ey);
263 str += sprintf(str, " path.lineTo(%d, %d);\n", fx, fy);
264 str += sprintf(str, " path.close();");
265 }
266 testSimplify(path, true, out);
267 path.setFillType(SkPath::kEvenOdd_FillType);
268 testSimplify(path, true, out);
269 }
270 }
271 }
272 return NULL;
273}
274
275void SimplifyDegenerate4x4TrianglesThreaded_Test() {
276 State threadState[maxThreads];
277 int threadIndex = 0;
278 for (int a = 0; a < 16; ++a) {
279 int ax = a & 0x03;
280 int ay = a >> 2;
281 for (int b = a ; b < 16; ++b) {
282 int bx = b & 0x03;
283 int by = b >> 2;
284 for (int c = a ; c < 16; ++c) {
285 int cx = c & 0x03;
286 int cy = c >> 2;
287 State* statePtr = &threadState[threadIndex];
288 statePtr->abcIsATriangle = (bx - ax) * (cy - ay)
289 != (by - ay) * (cx - ax);
290 statePtr->a = a;
291 statePtr->b = b;
292 statePtr->c = c;
293 if (maxThreads > 1) {
294 createThread(statePtr, testSimplify4x4DegeneratesMain);
295 if (++threadIndex >= maxThreads) {
296 waitForCompletion(threadState, threadIndex);
297 }
298 } else {
299 testSimplify4x4DegeneratesMain(statePtr);
300 }
301 }
302 }
303 }
304 waitForCompletion(threadState, threadIndex);
305}
306