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