caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +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 | */ |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 7 | #include "EdgeWalker_Test.h" |
| 8 | #include "Intersection_Tests.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 12 | #include "SkStream.h" |
| 13 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 14 | #include <algorithm> |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 15 | #include <assert.h> |
| 16 | #include <errno.h> |
| 17 | #include <pthread.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/sysctl.h> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 21 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 22 | #undef SkASSERT |
| 23 | #define SkASSERT(cond) while (!(cond)) { sk_throw(); } |
| 24 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +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"; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 31 | |
| 32 | static const char preferredFilename[] = "/flash/debug/XX.txt"; |
| 33 | static const char backupFilename[] = "../../experimental/Intersection/debugXX.txt"; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 34 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 35 | static bool gShowPath = false; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 36 | static bool gComparePaths = true; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 37 | //static bool gDrawLastAsciiPaths = true; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 38 | //static bool gDrawAllAsciiPaths = false; |
| 39 | static bool gShowOutputProgress = false; |
| 40 | static bool gShowAsciiPaths = true; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 41 | static bool gComparePathsAssert = false; |
| 42 | static bool gPathStrAssert = true; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 43 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 44 | void showPath(const SkPath& path, const char* str) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 45 | SkDebugf("%s\n", !str ? "original:" : str); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 46 | SkPath::Iter iter(path, true); |
| 47 | uint8_t verb; |
| 48 | SkPoint pts[4]; |
| 49 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 50 | switch (verb) { |
| 51 | case SkPath::kMove_Verb: |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 52 | SkDebugf("path.moveTo(%1.9g, %1.9g);\n", pts[0].fX, pts[0].fY); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 53 | continue; |
| 54 | case SkPath::kLine_Verb: |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 55 | SkDebugf("path.lineTo(%1.9g, %1.9g);\n", pts[1].fX, pts[1].fY); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 56 | break; |
| 57 | case SkPath::kQuad_Verb: |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 58 | SkDebugf("path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 59 | pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY); |
| 60 | break; |
| 61 | case SkPath::kCubic_Verb: |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 62 | SkDebugf("path.cubicTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 63 | pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, |
| 64 | pts[3].fX, pts[3].fY); |
| 65 | break; |
| 66 | case SkPath::kClose_Verb: |
| 67 | SkDebugf("path.close();\n"); |
| 68 | continue; |
| 69 | default: |
| 70 | SkDEBUGFAIL("bad verb"); |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 76 | static int pathsDrawTheSame(const SkPath& one, const SkPath& two, |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 77 | SkBitmap& bits, int& error2x2) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 78 | const int bitWidth = 64; |
| 79 | const int bitHeight = 64; |
| 80 | if (bits.width() == 0) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 81 | bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight); |
| 82 | bits.allocPixels(); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 83 | } |
skia.committer@gmail.com | 22b460c | 2012-09-29 02:01:02 +0000 | [diff] [blame^] | 84 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 85 | SkRect larger = one.getBounds(); |
| 86 | larger.join(two.getBounds()); |
| 87 | SkScalar largerWidth = larger.width(); |
| 88 | if (largerWidth < 4) { |
| 89 | largerWidth = 4; |
| 90 | } |
| 91 | SkScalar largerHeight = larger.height(); |
| 92 | if (largerHeight < 4) { |
| 93 | largerHeight = 4; |
| 94 | } |
| 95 | SkScalar hScale = (bitWidth - 2) / largerWidth; |
| 96 | SkScalar vScale = (bitHeight - 2) / largerHeight; |
| 97 | SkMatrix scale; |
| 98 | scale.reset(); |
| 99 | scale.preScale(hScale, vScale); |
| 100 | SkPath scaledOne, scaledTwo; |
| 101 | one.transform(scale, &scaledOne); |
| 102 | two.transform(scale, &scaledTwo); |
| 103 | const SkRect& bounds1 = scaledOne.getBounds(); |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 104 | |
| 105 | SkCanvas canvas(bits); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 106 | canvas.drawColor(SK_ColorWHITE); |
| 107 | SkPaint paint; |
| 108 | canvas.save(); |
| 109 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 110 | canvas.drawPath(scaledOne, paint); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 111 | canvas.restore(); |
| 112 | canvas.save(); |
| 113 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 114 | canvas.drawPath(scaledTwo, paint); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 115 | canvas.restore(); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 116 | int errors2 = 0; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 117 | int errors = 0; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 118 | for (int y = 0; y < bitHeight - 1; ++y) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 119 | uint32_t* addr1 = bits.getAddr32(0, y); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 120 | uint32_t* addr2 = bits.getAddr32(0, y + 1); |
| 121 | uint32_t* addr3 = bits.getAddr32(bitWidth, y); |
| 122 | uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1); |
| 123 | for (int x = 0; x < bitWidth - 1; ++x) { |
| 124 | // count 2x2 blocks |
| 125 | bool err = addr1[x] != addr3[x]; |
| 126 | if (err) { |
| 127 | errors2 += addr1[x + 1] != addr3[x + 1] |
| 128 | && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1]; |
| 129 | errors++; |
| 130 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 133 | if (errors2 >= 6 || errors > 160) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 134 | SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors); |
| 135 | } |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 136 | if (errors2 >= 7) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 137 | drawAsciiPaths(scaledOne, scaledTwo, true); |
| 138 | } |
| 139 | error2x2 = errors2; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 140 | return errors; |
| 141 | } |
| 142 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 143 | bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 144 | if (!drawPaths) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 145 | return true; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 146 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 147 | if (gShowAsciiPaths) { |
| 148 | showPath(one, "one:"); |
| 149 | showPath(two, "two:"); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 150 | } |
| 151 | const SkRect& bounds1 = one.getBounds(); |
| 152 | const SkRect& bounds2 = two.getBounds(); |
| 153 | SkRect larger = bounds1; |
| 154 | larger.join(bounds2); |
| 155 | SkBitmap bits; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 156 | char out[256]; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 157 | int bitWidth = SkScalarCeil(larger.width()) + 2; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 158 | if (bitWidth * 2 + 1 >= (int) sizeof(out)) { |
| 159 | return false; |
| 160 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 161 | int bitHeight = SkScalarCeil(larger.height()) + 2; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 162 | if (bitHeight >= (int) sizeof(out)) { |
| 163 | return false; |
| 164 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 165 | bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight); |
| 166 | bits.allocPixels(); |
| 167 | SkCanvas canvas(bits); |
| 168 | canvas.drawColor(SK_ColorWHITE); |
| 169 | SkPaint paint; |
| 170 | canvas.save(); |
| 171 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 172 | canvas.drawPath(one, paint); |
| 173 | canvas.restore(); |
| 174 | canvas.save(); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 175 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 176 | canvas.drawPath(two, paint); |
| 177 | canvas.restore(); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 178 | for (int y = 0; y < bitHeight; ++y) { |
| 179 | uint32_t* addr1 = bits.getAddr32(0, y); |
| 180 | int x; |
| 181 | char* outPtr = out; |
| 182 | for (x = 0; x < bitWidth; ++x) { |
| 183 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 184 | } |
| 185 | *outPtr++ = '|'; |
| 186 | for (x = bitWidth; x < bitWidth * 2; ++x) { |
| 187 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 188 | } |
| 189 | *outPtr++ = '\0'; |
| 190 | SkDebugf("%s\n", out); |
| 191 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 192 | return true; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 193 | } |
| 194 | |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 195 | int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 196 | int errors2x2; |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 197 | int errors = pathsDrawTheSame(one, two, bitmap, errors2x2); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 198 | if (errors2x2 == 0) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 199 | return 0; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 200 | } |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 201 | const int MAX_ERRORS = 8; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 202 | if (errors2x2 > MAX_ERRORS && gComparePathsAssert) { |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 203 | SkDebugf("%s errors=%d\n", __FUNCTION__, errors); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 204 | showPath(one); |
| 205 | showPath(two, "simplified:"); |
| 206 | SkASSERT(0); |
| 207 | } |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 208 | return errors2x2 > MAX_ERRORS ? errors2x2 : 0; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // doesn't work yet |
| 212 | void comparePathsTiny(const SkPath& one, const SkPath& two) { |
| 213 | const SkRect& bounds1 = one.getBounds(); |
| 214 | const SkRect& bounds2 = two.getBounds(); |
| 215 | SkRect larger = bounds1; |
| 216 | larger.join(bounds2); |
| 217 | SkBitmap bits; |
| 218 | int bitWidth = SkScalarCeil(larger.width()) + 2; |
| 219 | int bitHeight = SkScalarCeil(larger.height()) + 2; |
| 220 | bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight); |
| 221 | bits.allocPixels(); |
| 222 | SkCanvas canvas(bits); |
| 223 | canvas.drawColor(SK_ColorWHITE); |
| 224 | SkPaint paint; |
| 225 | canvas.save(); |
| 226 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 227 | canvas.drawPath(one, paint); |
| 228 | canvas.restore(); |
| 229 | canvas.save(); |
| 230 | canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1); |
| 231 | canvas.drawPath(two, paint); |
| 232 | canvas.restore(); |
| 233 | for (int y = 0; y < bitHeight; ++y) { |
| 234 | uint8_t* addr1 = bits.getAddr1(0, y); |
| 235 | uint8_t* addr2 = bits.getAddr1(bitWidth, y); |
| 236 | for (int x = 0; x < bits.rowBytes(); ++x) { |
| 237 | SkASSERT(addr1[x] == addr2[x]); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 242 | bool testSimplify(const SkPath& path, bool fill, SkPath& out, SkBitmap& bitmap) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 243 | if (gShowPath) { |
| 244 | showPath(path); |
| 245 | } |
| 246 | simplify(path, fill, out); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 247 | if (!gComparePaths) { |
| 248 | return true; |
| 249 | } |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 250 | return comparePaths(path, out, bitmap) == 0; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 251 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 252 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 253 | bool testSimplifyx(SkPath& path, bool useXor, SkPath& out, State4& state, |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 254 | const char* pathStr) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 255 | SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType; |
| 256 | path.setFillType(fillType); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 257 | if (gShowPath) { |
| 258 | showPath(path); |
| 259 | } |
| 260 | simplifyx(path, out); |
| 261 | if (!gComparePaths) { |
| 262 | return true; |
| 263 | } |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 264 | int result = comparePaths(path, out, state.bitmap); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 265 | if (result && gPathStrAssert) { |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 266 | SkDebugf("addTest %s\n", state.filename); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 267 | char temp[8192]; |
| 268 | bzero(temp, sizeof(temp)); |
| 269 | SkMemoryWStream stream(temp, sizeof(temp)); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 270 | outputToStream(state, pathStr, fillType, stream); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 271 | SkDebugf(temp); |
| 272 | SkASSERT(0); |
| 273 | } |
| 274 | return result == 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool testSimplifyx(const SkPath& path) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 278 | SkPath out; |
| 279 | simplifyx(path, out); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 280 | SkBitmap bitmap; |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 281 | int result = comparePaths(path, out, bitmap); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 282 | if (result && gPathStrAssert) { |
| 283 | SkASSERT(0); |
| 284 | } |
| 285 | return result == 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 286 | } |
| 287 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 288 | const int maxThreadsAllocated = 64; |
| 289 | static int maxThreads = 1; |
| 290 | static int threadIndex; |
| 291 | State4 threadState[maxThreadsAllocated]; |
| 292 | static int testNumber; |
| 293 | static const char* testName; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 294 | static bool debugThreads = true; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 295 | |
| 296 | State4* State4::queue = NULL; |
| 297 | pthread_mutex_t State4::addQueue = PTHREAD_MUTEX_INITIALIZER; |
| 298 | pthread_cond_t State4::checkQueue = PTHREAD_COND_INITIALIZER; |
| 299 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 300 | State4::State4() { |
| 301 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150 * 2, 100); |
| 302 | bitmap.allocPixels(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 303 | } |
| 304 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 305 | void createThread(State4* statePtr, void* (*testFun)(void* )) { |
| 306 | int threadError = pthread_create(&statePtr->threadID, NULL, testFun, |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 307 | (void*) statePtr); |
| 308 | SkASSERT(!threadError); |
| 309 | } |
| 310 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 311 | int dispatchTest4(void* (*testFun)(void* ), int a, int b, int c, int d) { |
| 312 | int testsRun = 0; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 313 | State4* statePtr; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 314 | if (!gRunTestsInOneThread) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 315 | pthread_mutex_lock(&State4::addQueue); |
| 316 | if (threadIndex < maxThreads) { |
| 317 | statePtr = &threadState[threadIndex]; |
| 318 | statePtr->testsRun = 0; |
| 319 | statePtr->a = a; |
| 320 | statePtr->b = b; |
| 321 | statePtr->c = c; |
| 322 | statePtr->d = d; |
| 323 | statePtr->done = false; |
| 324 | statePtr->index = threadIndex; |
| 325 | statePtr->last = false; |
| 326 | if (debugThreads) SkDebugf("%s %d create done=%d last=%d\n", __FUNCTION__, |
| 327 | statePtr->index, statePtr->done, statePtr->last); |
| 328 | pthread_cond_init(&statePtr->initialized, NULL); |
| 329 | ++threadIndex; |
| 330 | createThread(statePtr, testFun); |
| 331 | } else { |
| 332 | while (!State4::queue) { |
| 333 | if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__); |
| 334 | pthread_cond_wait(&State4::checkQueue, &State4::addQueue); |
| 335 | } |
| 336 | statePtr = State4::queue; |
| 337 | testsRun += statePtr->testsRun; |
| 338 | statePtr->testsRun = 0; |
| 339 | statePtr->a = a; |
| 340 | statePtr->b = b; |
| 341 | statePtr->c = c; |
| 342 | statePtr->d = d; |
| 343 | statePtr->done = false; |
| 344 | State4::queue = NULL; |
| 345 | for (int index = 0; index < maxThreads; ++index) { |
| 346 | if (threadState[index].done) { |
| 347 | State4::queue = &threadState[index]; |
| 348 | } |
| 349 | } |
| 350 | if (debugThreads) SkDebugf("%s %d init done=%d last=%d queued=%d\n", __FUNCTION__, |
| 351 | statePtr->index, statePtr->done, statePtr->last, |
| 352 | State4::queue ? State4::queue->index : -1); |
| 353 | pthread_cond_signal(&statePtr->initialized); |
| 354 | } |
| 355 | pthread_mutex_unlock(&State4::addQueue); |
| 356 | } else { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 357 | statePtr = &threadState[0]; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 358 | testsRun += statePtr->testsRun; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 359 | statePtr->testsRun = 0; |
| 360 | statePtr->a = a; |
| 361 | statePtr->b = b; |
| 362 | statePtr->c = c; |
| 363 | statePtr->d = d; |
| 364 | statePtr->done = false; |
| 365 | statePtr->index = threadIndex; |
| 366 | statePtr->last = false; |
| 367 | (*testFun)(statePtr); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 368 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 369 | return testsRun; |
| 370 | } |
| 371 | |
| 372 | void initializeTests(const char* test, size_t testNameSize) { |
| 373 | testName = test; |
| 374 | if (!gRunTestsInOneThread) { |
| 375 | int threads = -1; |
| 376 | size_t size = sizeof(threads); |
| 377 | sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0); |
| 378 | if (threads > 0) { |
| 379 | maxThreads = threads; |
| 380 | } else { |
| 381 | maxThreads = 8; |
| 382 | } |
| 383 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 384 | SkFILEStream inFile("../../experimental/Intersection/op.htm"); |
| 385 | if (inFile.isValid()) { |
| 386 | SkTDArray<char> inData; |
| 387 | inData.setCount(inFile.getLength()); |
| 388 | size_t inLen = inData.count(); |
| 389 | inFile.read(inData.begin(), inLen); |
| 390 | inFile.setPath(NULL); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 391 | char* insert = strstr(inData.begin(), marker); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 392 | if (insert) { |
| 393 | insert += sizeof(marker) - 1; |
| 394 | const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1; |
| 395 | testNumber = atoi(numLoc) + 1; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 398 | const char* filename = preferredFilename; |
| 399 | SkFILEWStream preferredTest(filename); |
| 400 | if (!preferredTest.isValid()) { |
| 401 | filename = backupFilename; |
| 402 | SkFILEWStream backupTest(filename); |
| 403 | SkASSERT(backupTest.isValid()); |
| 404 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 405 | for (int index = 0; index < maxThreads; ++index) { |
| 406 | State4* statePtr = &threadState[index]; |
| 407 | strcpy(statePtr->filename, filename); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 408 | size_t len = strlen(filename); |
| 409 | SkASSERT(statePtr->filename[len - 6] == 'X'); |
| 410 | SkASSERT(statePtr->filename[len - 5] == 'X'); |
| 411 | statePtr->filename[len - 6] = '0' + index / 10; |
| 412 | statePtr->filename[len - 5] = '0' + index % 10; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 413 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 414 | threadIndex = 0; |
| 415 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 416 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 417 | void outputProgress(const State4& state, const char* pathStr, SkPath::FillType pathFillType) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 418 | if (gRunTestsInOneThread && gShowOutputProgress) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 419 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 420 | SkDebugf(" path.setFillType(SkPath::kEvenOdd_FillType);\n", pathStr); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 421 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 422 | SkDebugf("%s\n", pathStr); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 423 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 424 | SkFILEWStream outFile(state.filename); |
| 425 | if (!outFile.isValid()) { |
| 426 | SkASSERT(0); |
| 427 | return; |
| 428 | } |
| 429 | outputToStream(state, pathStr, pathFillType, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 430 | } |
| 431 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 432 | static void writeTestName(SkPath::FillType pathFillType, SkWStream& outFile) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 433 | outFile.writeText(testName); |
| 434 | outFile.writeDecAsText(testNumber); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 435 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 436 | outFile.writeText("x"); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void outputToStream(const State4& state, const char* pathStr, SkPath::FillType pathFillType, SkWStream& outFile) { |
| 441 | outFile.writeText("<div id=\""); |
| 442 | writeTestName(pathFillType, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 443 | outFile.writeText("\">\n"); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 444 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 445 | outFile.writeText(" path.setFillType(SkPath::kEvenOdd_FillType);\n"); |
| 446 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 447 | outFile.writeText(pathStr); |
| 448 | outFile.writeText("</div>\n\n"); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 449 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 450 | outFile.writeText(marker); |
| 451 | outFile.writeText(" "); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 452 | writeTestName(pathFillType, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 453 | outFile.writeText(",\n\n\n"); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 454 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 455 | outFile.writeText("static void "); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 456 | writeTestName(pathFillType, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 457 | outFile.writeText("() {\n SkPath path;\n"); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 458 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 459 | outFile.writeText(" path.setFillType(SkPath::kEvenOdd_FillType);\n"); |
| 460 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 461 | outFile.writeText(pathStr); |
| 462 | outFile.writeText(" testSimplifyx(path);\n}\n\n"); |
| 463 | outFile.writeText("static void (*firstTest)() = "); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 464 | writeTestName(pathFillType, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 465 | outFile.writeText(";\n\n"); |
| 466 | |
| 467 | outFile.writeText("static struct {\n"); |
| 468 | outFile.writeText(" void (*fun)();\n"); |
| 469 | outFile.writeText(" const char* str;\n"); |
| 470 | outFile.writeText("} tests[] = {\n"); |
| 471 | outFile.writeText(" TEST("); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 472 | writeTestName(pathFillType, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 473 | outFile.writeText("),\n"); |
| 474 | outFile.flush(); |
| 475 | } |
| 476 | |
| 477 | bool runNextTestSet(State4& state) { |
| 478 | if (gRunTestsInOneThread) { |
| 479 | return false; |
| 480 | } |
| 481 | pthread_mutex_lock(&State4::addQueue); |
| 482 | state.done = true; |
| 483 | State4::queue = &state; |
| 484 | if (debugThreads) SkDebugf("%s %d checkQueue done=%d last=%d\n", __FUNCTION__, state.index, |
| 485 | state.done, state.last); |
| 486 | pthread_cond_signal(&State4::checkQueue); |
| 487 | while (state.done && !state.last) { |
| 488 | if (debugThreads) SkDebugf("%s %d done=%d last=%d\n", __FUNCTION__, state.index, state.done, state.last); |
| 489 | pthread_cond_wait(&state.initialized, &State4::addQueue); |
| 490 | } |
| 491 | pthread_mutex_unlock(&State4::addQueue); |
| 492 | return !state.last; |
| 493 | } |
| 494 | |
| 495 | int waitForCompletion() { |
| 496 | int testsRun = 0; |
| 497 | if (!gRunTestsInOneThread) { |
| 498 | pthread_mutex_lock(&State4::addQueue); |
| 499 | int runningThreads = maxThreads; |
| 500 | int index; |
| 501 | while (runningThreads > 0) { |
| 502 | while (!State4::queue) { |
| 503 | if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__); |
| 504 | pthread_cond_wait(&State4::checkQueue, &State4::addQueue); |
| 505 | } |
| 506 | while (State4::queue) { |
| 507 | --runningThreads; |
| 508 | SkDebugf("•"); |
| 509 | State4::queue->last = true; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 510 | State4* next = NULL; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 511 | for (index = 0; index < maxThreads; ++index) { |
| 512 | State4& test = threadState[index]; |
| 513 | if (test.done && !test.last) { |
| 514 | next = &test; |
| 515 | } |
| 516 | } |
| 517 | if (debugThreads) SkDebugf("%s %d next=%d deQueue\n", __FUNCTION__, |
| 518 | State4::queue->index, next ? next->index : -1); |
| 519 | pthread_cond_signal(&State4::queue->initialized); |
| 520 | State4::queue = next; |
| 521 | } |
| 522 | } |
| 523 | pthread_mutex_unlock(&State4::addQueue); |
| 524 | for (index = 0; index < maxThreads; ++index) { |
| 525 | pthread_join(threadState[index].threadID, NULL); |
| 526 | testsRun += threadState[index].testsRun; |
| 527 | } |
| 528 | SkDebugf("\n"); |
| 529 | } |
| 530 | #ifdef SK_DEBUG |
| 531 | gDebugMaxWindSum = SK_MaxS32; |
| 532 | gDebugMaxWindValue = SK_MaxS32; |
| 533 | #endif |
| 534 | return testsRun; |
| 535 | } |