caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 7 | #include "EdgeWalker_Test.h" |
| 8 | #include "Intersection_Tests.h" |
| 9 | #include "ShapeOps.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 12 | #include "SkStream.h" |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 13 | #include <assert.h> |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 14 | #include <errno.h> |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 15 | #include <pthread.h> |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/sysctl.h> |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 19 | |
| 20 | // four rects, of four sizes |
| 21 | // for 3 smaller sizes, tall, wide |
| 22 | // top upper mid lower bottom aligned (3 bits, 5 values) |
| 23 | // same with x (3 bits, 5 values) |
| 24 | // not included, square, tall, wide (2 bits) |
| 25 | // cw or ccw (1 bit) |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 26 | static const char marker[] = |
| 27 | "</div>\n" |
| 28 | "\n" |
| 29 | "<script type=\"text/javascript\">\n" |
| 30 | "\n" |
| 31 | "var testDivs = [\n"; |
| 32 | static const char testLineStr[] = " testLine"; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 33 | #if 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 34 | static const char filename[] = "../../experimental/Intersection/debugXX.txt"; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 35 | #else |
| 36 | static const char filename[] = "/flash/debug/XX.txt"; |
| 37 | #endif |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 38 | static int testNumber; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 39 | |
| 40 | static void* testSimplify4x4RectsMain(void* data) |
| 41 | { |
| 42 | char pathStr[1024]; // gdb: set print elements 400 |
| 43 | bzero(pathStr, sizeof(pathStr)); |
| 44 | SkASSERT(data); |
| 45 | State4& state = *(State4*) data; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 46 | state.testsRun = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 47 | int aShape = state.a & 0x03; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 48 | int aCW = state.a >> 2; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 49 | int bShape = state.b & 0x03; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 50 | int bCW = state.b >> 2; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 51 | int cShape = state.c & 0x03; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 52 | int cCW = state.c >> 2; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 53 | int dShape = state.d & 0x03; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 54 | int dCW = state.d >> 2; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 55 | for (int aXAlign = 0 ; aXAlign < 5; ++aXAlign) { |
| 56 | for (int aYAlign = 0 ; aYAlign < 5; ++aYAlign) { |
| 57 | for (int bXAlign = 0 ; bXAlign < 5; ++bXAlign) { |
| 58 | for (int bYAlign = 0 ; bYAlign < 5; ++bYAlign) { |
| 59 | for (int cXAlign = 0 ; cXAlign < 5; ++cXAlign) { |
| 60 | for (int cYAlign = 0 ; cYAlign < 5; ++cYAlign) { |
| 61 | for (int dXAlign = 0 ; dXAlign < 5; ++dXAlign) { |
| 62 | for (int dYAlign = 0 ; dYAlign < 5; ++dYAlign) { |
| 63 | SkPath path, out; |
| 64 | char* str = pathStr; |
| 65 | path.setFillType(SkPath::kWinding_FillType); |
| 66 | int l, t, r, b; |
| 67 | if (aShape) { |
| 68 | switch (aShape) { |
| 69 | case 1: // square |
| 70 | l = 0; r = 60; |
| 71 | t = 0; b = 60; |
| 72 | aXAlign = 5; |
| 73 | aYAlign = 5; |
| 74 | break; |
| 75 | case 2: |
| 76 | l = aXAlign * 12; |
| 77 | r = l + 30; |
| 78 | t = 0; b = 60; |
| 79 | aYAlign = 5; |
| 80 | break; |
| 81 | case 3: |
| 82 | l = 0; r = 60; |
| 83 | t = aYAlign * 12; |
| 84 | b = l + 30; |
| 85 | aXAlign = 5; |
| 86 | break; |
| 87 | } |
| 88 | path.addRect(l, t, r, b, (SkPath::Direction) aCW); |
| 89 | str += sprintf(str, " path.addRect(%d, %d, %d, %d," |
| 90 | " (SkPath::Direction) %d);\n", l, t, r, b, aCW); |
| 91 | } else { |
| 92 | aXAlign = 5; |
| 93 | aYAlign = 5; |
| 94 | } |
| 95 | if (bShape) { |
| 96 | switch (bShape) { |
| 97 | case 1: // square |
| 98 | l = bXAlign * 10; |
| 99 | r = l + 20; |
| 100 | t = bYAlign * 10; |
| 101 | b = l + 20; |
| 102 | break; |
| 103 | case 2: |
| 104 | l = bXAlign * 10; |
| 105 | r = l + 20; |
| 106 | t = 10; b = 40; |
| 107 | bYAlign = 5; |
| 108 | break; |
| 109 | case 3: |
| 110 | l = 10; r = 40; |
| 111 | t = bYAlign * 10; |
| 112 | b = l + 20; |
| 113 | bXAlign = 5; |
| 114 | break; |
| 115 | } |
| 116 | path.addRect(l, t, r, b, (SkPath::Direction) bCW); |
| 117 | str += sprintf(str, " path.addRect(%d, %d, %d, %d," |
| 118 | " (SkPath::Direction) %d);\n", l, t, r, b, bCW); |
| 119 | } else { |
| 120 | bXAlign = 5; |
| 121 | bYAlign = 5; |
| 122 | } |
| 123 | if (cShape) { |
| 124 | switch (cShape) { |
| 125 | case 1: // square |
| 126 | l = cXAlign * 6; |
| 127 | r = l + 12; |
| 128 | t = cYAlign * 6; |
| 129 | b = l + 12; |
| 130 | break; |
| 131 | case 2: |
| 132 | l = cXAlign * 6; |
| 133 | r = l + 12; |
| 134 | t = 20; b = 30; |
| 135 | cYAlign = 5; |
| 136 | break; |
| 137 | case 3: |
| 138 | l = 20; r = 30; |
| 139 | t = cYAlign * 6; |
| 140 | b = l + 20; |
| 141 | cXAlign = 5; |
| 142 | break; |
| 143 | } |
| 144 | path.addRect(l, t, r, b, (SkPath::Direction) cCW); |
| 145 | str += sprintf(str, " path.addRect(%d, %d, %d, %d," |
| 146 | " (SkPath::Direction) %d);\n", l, t, r, b, cCW); |
| 147 | } else { |
| 148 | cXAlign = 5; |
| 149 | cYAlign = 5; |
| 150 | } |
| 151 | if (dShape) { |
| 152 | switch (dShape) { |
| 153 | case 1: // square |
| 154 | l = dXAlign * 4; |
| 155 | r = l + 9; |
| 156 | t = dYAlign * 4; |
| 157 | b = l + 9; |
| 158 | break; |
| 159 | case 2: |
| 160 | l = dXAlign * 6; |
| 161 | r = l + 9; |
| 162 | t = 32; b = 36; |
| 163 | dYAlign = 5; |
| 164 | break; |
| 165 | case 3: |
| 166 | l = 32; r = 36; |
| 167 | t = dYAlign * 6; |
| 168 | b = l + 9; |
| 169 | dXAlign = 5; |
| 170 | break; |
| 171 | } |
| 172 | path.addRect(l, t, r, b, (SkPath::Direction) dCW); |
| 173 | str += sprintf(str, " path.addRect(%d, %d, %d, %d," |
| 174 | " (SkPath::Direction) %d);\n", l, t, r, b, dCW); |
| 175 | } else { |
| 176 | dXAlign = 5; |
| 177 | dYAlign = 5; |
| 178 | } |
| 179 | path.close(); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 180 | if (gRunTestsInOneThread) { |
| 181 | SkDebugf("%s\n", pathStr); |
| 182 | } else { |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 183 | #if 0 |
| 184 | char pwd[1024]; |
| 185 | getcwd(pwd, sizeof(pwd)); |
| 186 | SkDebugf("%s\n", pwd); |
| 187 | #endif |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 188 | #if 1 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 189 | SkFILEWStream outFile(state.filename); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 190 | if (!outFile.isValid()) { |
| 191 | continue; |
| 192 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 193 | outFile.writeText("<div id=\"testLine"); |
| 194 | outFile.writeDecAsText(testNumber); |
| 195 | outFile.writeText("\">\n"); |
| 196 | outFile.writeText(pathStr); |
| 197 | outFile.writeText("</div>\n\n"); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 198 | |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 199 | outFile.writeText(marker); |
| 200 | outFile.writeText(testLineStr); |
| 201 | outFile.writeDecAsText(testNumber); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 202 | outFile.writeText(",\n\n\n"); |
| 203 | |
| 204 | outFile.writeText("static void testLine"); |
| 205 | outFile.writeDecAsText(testNumber); |
| 206 | outFile.writeText("() {\n SkPath path, simple;\n"); |
| 207 | outFile.writeText(pathStr); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 208 | outFile.writeText(" testSimplifyx(path);\n}\n\n"); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 209 | outFile.writeText("static void (*firstTest)() = testLine"); |
| 210 | outFile.writeDecAsText(testNumber); |
| 211 | outFile.writeText(";\n\n"); |
| 212 | |
| 213 | outFile.writeText("static struct {\n"); |
| 214 | outFile.writeText(" void (*fun)();\n"); |
| 215 | outFile.writeText(" const char* str;\n"); |
| 216 | outFile.writeText("} tests[] = {\n"); |
| 217 | outFile.writeText(" TEST(testLine"); |
| 218 | outFile.writeDecAsText(testNumber); |
| 219 | outFile.writeText("),\n"); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 220 | outFile.flush(); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 221 | #endif |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 222 | } |
| 223 | testSimplifyx(path, out, state.bitmap, state.canvas); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 224 | state.testsRun++; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | return NULL; |
| 234 | } |
| 235 | |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 236 | const int maxThreadsAllocated = 32; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 237 | |
| 238 | void Simplify4x4RectsThreaded_Test() |
| 239 | { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 240 | #ifdef SK_DEBUG |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 241 | gDebugMaxWindSum = 4; |
| 242 | gDebugMaxWindValue = 4; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 243 | #endif |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 244 | int maxThreads = 1; |
| 245 | if (!gRunTestsInOneThread) { |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 246 | int threads = -1; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 247 | size_t size = sizeof(threads); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 248 | sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 249 | SkDebugf("%s errno=%d size=%d processors=%d\n", __FUNCTION__, |
| 250 | errno, size, threads); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 251 | if (threads > 0) { |
| 252 | maxThreads = threads; |
| 253 | } else { |
| 254 | maxThreads = 8; |
| 255 | } |
| 256 | SkDebugf("%s maxThreads=%d\n", __FUNCTION__, maxThreads); |
| 257 | } |
| 258 | if (!gRunTestsInOneThread) { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 259 | SkFILEStream inFile("../../experimental/Intersection/op.htm"); |
| 260 | if (inFile.isValid()) { |
| 261 | SkTDArray<char> inData; |
| 262 | inData.setCount(inFile.getLength()); |
| 263 | size_t inLen = inData.count(); |
| 264 | inFile.read(inData.begin(), inLen); |
| 265 | inFile.setPath(NULL); |
| 266 | char* insert = strstr(inData.begin(), marker); |
| 267 | if (insert) { |
| 268 | insert += sizeof(marker) - 1; |
| 269 | const char* numLoc = insert + sizeof(testLineStr) - 1; |
| 270 | testNumber = atoi(numLoc) + 1; |
| 271 | } |
| 272 | } |
| 273 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 274 | State4 threadState[maxThreadsAllocated]; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 275 | int threadIndex; |
| 276 | for (threadIndex = 0; threadIndex < maxThreads; ++threadIndex) { |
| 277 | State4* statePtr = &threadState[threadIndex]; |
| 278 | strcpy(statePtr->filename, filename); |
| 279 | SkASSERT(statePtr->filename[sizeof(filename) - 7] == 'X'); |
| 280 | SkASSERT(statePtr->filename[sizeof(filename) - 6] == 'X'); |
| 281 | statePtr->filename[sizeof(filename) - 7] = '0' + threadIndex / 10; |
| 282 | statePtr->filename[sizeof(filename) - 6] = '0' + threadIndex % 10; |
| 283 | } |
| 284 | threadIndex = 0; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 285 | int testsRun = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 286 | for (int a = 0; a < 8; ++a) { // outermost |
| 287 | for (int b = a ; b < 8; ++b) { |
| 288 | for (int c = b ; c < 8; ++c) { |
| 289 | for (int d = c; d < 8; ++d) { |
| 290 | State4* statePtr = &threadState[threadIndex]; |
| 291 | statePtr->a = a; |
| 292 | statePtr->b = b; |
| 293 | statePtr->c = c; |
| 294 | statePtr->d = d; |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 295 | if (!gRunTestsInOneThread) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 296 | createThread(statePtr, testSimplify4x4RectsMain); |
| 297 | if (++threadIndex >= maxThreads) { |
| 298 | waitForCompletion(threadState, threadIndex); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 299 | for (int index = 0; index < maxThreads; ++index) { |
| 300 | testsRun += threadState[index].testsRun; |
| 301 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 302 | } |
| 303 | } else { |
| 304 | testSimplify4x4RectsMain(statePtr); |
| 305 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 306 | if (!gRunTestsInOneThread) SkDebugf("."); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 307 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 308 | if (!gRunTestsInOneThread) SkDebugf("%d", c); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 309 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 310 | if (!gRunTestsInOneThread) SkDebugf("\n%d", b); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 311 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 312 | if (!gRunTestsInOneThread) SkDebugf("\n\n%d", a); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 313 | } |
| 314 | waitForCompletion(threadState, threadIndex); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 315 | for (int index = 0; index < maxThreads; ++index) { |
| 316 | testsRun += threadState[index].testsRun; |
| 317 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 318 | #ifdef SK_DEBUG |
| 319 | gDebugMaxWindSum = SK_MaxS32; |
| 320 | gDebugMaxWindValue = SK_MaxS32; |
| 321 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 322 | } |
| 323 | |