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