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