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 | */ |
skia.committer@gmail.com | 6433435 | 2013-03-06 07:01:46 +0000 | [diff] [blame] | 7 | |
caryclark@google.com | d4c8e1e | 2013-03-05 14:13:13 +0000 | [diff] [blame] | 8 | #include "DataTypes.h" |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 9 | #include "EdgeWalker_Test.h" |
| 10 | #include "Intersection_Tests.h" |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkCanvas.h" |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 17 | #include <algorithm> |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <pthread.h> |
| 20 | #include <unistd.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/sysctl.h> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 23 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 24 | #undef SkASSERT |
| 25 | #define SkASSERT(cond) while (!(cond)) { sk_throw(); } |
| 26 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 27 | static const char marker[] = |
| 28 | "</div>\n" |
| 29 | "\n" |
| 30 | "<script type=\"text/javascript\">\n" |
| 31 | "\n" |
| 32 | "var testDivs = [\n"; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 33 | |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 34 | static const char* opStrs[] = { |
| 35 | "kDifference_Op", |
| 36 | "kIntersect_Op", |
| 37 | "kUnion_Op", |
| 38 | "kXor_Op", |
| 39 | }; |
| 40 | |
| 41 | static const char* opSuffixes[] = { |
| 42 | "d", |
| 43 | "i", |
| 44 | "u", |
| 45 | "x", |
| 46 | }; |
| 47 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 48 | static const char preferredFilename[] = "/flash/debug/XX.txt"; |
| 49 | static const char backupFilename[] = "../../experimental/Intersection/debugXX.txt"; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 50 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 51 | static bool gShowPath = false; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 52 | static bool gComparePaths = true; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 53 | static bool gShowOutputProgress = false; |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 54 | static bool gComparePathsAssert = true; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 55 | static bool gPathStrAssert = true; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 56 | static bool gUsePhysicalFiles = false; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 57 | |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 58 | static void showPathContour(SkPath::Iter& iter) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 59 | uint8_t verb; |
| 60 | SkPoint pts[4]; |
| 61 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 62 | switch (verb) { |
| 63 | case SkPath::kMove_Verb: |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 64 | 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] | 65 | continue; |
| 66 | case SkPath::kLine_Verb: |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 67 | 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] | 68 | break; |
| 69 | case SkPath::kQuad_Verb: |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 70 | 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] | 71 | pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY); |
| 72 | break; |
| 73 | case SkPath::kCubic_Verb: |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 74 | SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n", |
| 75 | pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 76 | break; |
| 77 | case SkPath::kClose_Verb: |
| 78 | SkDebugf("path.close();\n"); |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 79 | break; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 80 | default: |
| 81 | SkDEBUGFAIL("bad verb"); |
| 82 | return; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 87 | void showPath(const SkPath& path, const char* str) { |
| 88 | SkDebugf("%s\n", !str ? "original:" : str); |
caryclark@google.com | d4c8e1e | 2013-03-05 14:13:13 +0000 | [diff] [blame] | 89 | showPath(path); |
| 90 | } |
| 91 | |
| 92 | void showPath(const SkPath& path) { |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 93 | SkPath::Iter iter(path, true); |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 94 | int rectCount = path.isRectContours() ? path.rectContours(NULL, NULL) : 0; |
| 95 | if (rectCount > 0) { |
| 96 | SkTDArray<SkRect> rects; |
| 97 | SkTDArray<SkPath::Direction> directions; |
| 98 | rects.setCount(rectCount); |
| 99 | directions.setCount(rectCount); |
| 100 | path.rectContours(rects.begin(), directions.begin()); |
| 101 | for (int contour = 0; contour < rectCount; ++contour) { |
| 102 | const SkRect& rect = rects[contour]; |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 103 | SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop, |
| 104 | rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction |
| 105 | ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction"); |
| 106 | } |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 107 | return; |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 108 | } |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 109 | iter.setPath(path, true); |
| 110 | showPathContour(iter); |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 111 | } |
| 112 | |
caryclark@google.com | d4c8e1e | 2013-03-05 14:13:13 +0000 | [diff] [blame] | 113 | void showPathData(const SkPath& path) { |
| 114 | SkPath::Iter iter(path, true); |
| 115 | uint8_t verb; |
| 116 | SkPoint pts[4]; |
| 117 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 118 | switch (verb) { |
| 119 | case SkPath::kMove_Verb: |
| 120 | continue; |
| 121 | case SkPath::kLine_Verb: |
| 122 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY); |
| 123 | break; |
| 124 | case SkPath::kQuad_Verb: |
| 125 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 126 | pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY); |
| 127 | break; |
| 128 | case SkPath::kCubic_Verb: |
| 129 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 130 | pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY); |
| 131 | break; |
| 132 | case SkPath::kClose_Verb: |
| 133 | break; |
| 134 | default: |
| 135 | SkDEBUGFAIL("bad verb"); |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void showOp(const ShapeOp op) { |
| 142 | switch (op) { |
| 143 | case kDifference_Op: |
| 144 | SkDebugf("op difference\n"); |
| 145 | break; |
| 146 | case kIntersect_Op: |
| 147 | SkDebugf("op intersect\n"); |
| 148 | break; |
| 149 | case kUnion_Op: |
| 150 | SkDebugf("op union\n"); |
| 151 | break; |
| 152 | case kXor_Op: |
| 153 | SkDebugf("op xor\n"); |
| 154 | break; |
| 155 | default: |
| 156 | SkASSERT(0); |
| 157 | } |
| 158 | } |
| 159 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 160 | static void showPath(const SkPath& path, const char* str, const SkMatrix& scale) { |
| 161 | SkPath scaled; |
| 162 | SkMatrix inverse; |
| 163 | bool success = scale.invert(&inverse); |
| 164 | if (!success) SkASSERT(0); |
| 165 | path.transform(inverse, &scaled); |
| 166 | showPath(scaled, str); |
| 167 | } |
| 168 | |
| 169 | const int bitWidth = 64; |
| 170 | const int bitHeight = 64; |
skia.committer@gmail.com | 22b460c | 2012-09-29 02:01:02 +0000 | [diff] [blame] | 171 | |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 172 | static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 173 | SkRect larger = one.getBounds(); |
| 174 | larger.join(two.getBounds()); |
| 175 | SkScalar largerWidth = larger.width(); |
| 176 | if (largerWidth < 4) { |
| 177 | largerWidth = 4; |
| 178 | } |
| 179 | SkScalar largerHeight = larger.height(); |
| 180 | if (largerHeight < 4) { |
| 181 | largerHeight = 4; |
| 182 | } |
| 183 | SkScalar hScale = (bitWidth - 2) / largerWidth; |
| 184 | SkScalar vScale = (bitHeight - 2) / largerHeight; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 185 | scale.reset(); |
| 186 | scale.preScale(hScale, vScale); |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 187 | } |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 188 | |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 189 | static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo, |
| 190 | int& error2x2) { |
| 191 | if (bits.width() == 0) { |
| 192 | bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight); |
| 193 | bits.allocPixels(); |
| 194 | } |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 195 | SkCanvas canvas(bits); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 196 | canvas.drawColor(SK_ColorWHITE); |
| 197 | SkPaint paint; |
| 198 | canvas.save(); |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 199 | const SkRect& bounds1 = scaledOne.getBounds(); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 200 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 201 | canvas.drawPath(scaledOne, paint); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 202 | canvas.restore(); |
| 203 | canvas.save(); |
| 204 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 205 | canvas.drawPath(scaledTwo, paint); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 206 | canvas.restore(); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 207 | int errors2 = 0; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 208 | int errors = 0; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 209 | for (int y = 0; y < bitHeight - 1; ++y) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 210 | uint32_t* addr1 = bits.getAddr32(0, y); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 211 | uint32_t* addr2 = bits.getAddr32(0, y + 1); |
| 212 | uint32_t* addr3 = bits.getAddr32(bitWidth, y); |
| 213 | uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1); |
| 214 | for (int x = 0; x < bitWidth - 1; ++x) { |
| 215 | // count 2x2 blocks |
| 216 | bool err = addr1[x] != addr3[x]; |
| 217 | if (err) { |
| 218 | errors2 += addr1[x + 1] != addr3[x + 1] |
| 219 | && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1]; |
| 220 | errors++; |
| 221 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 224 | if (errors2 >= 6 || errors > 160) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 225 | SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors); |
| 226 | } |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 227 | error2x2 = errors2; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 228 | return errors; |
| 229 | } |
| 230 | |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 231 | static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne, |
| 232 | SkPath& scaledTwo, int& error2x2) { |
| 233 | SkMatrix scale; |
| 234 | scaleMatrix(one, two, scale); |
| 235 | one.transform(scale, &scaledOne); |
| 236 | two.transform(scale, &scaledTwo); |
| 237 | return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2); |
| 238 | } |
| 239 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 240 | bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 241 | if (!drawPaths) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 242 | return true; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 243 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 244 | const SkRect& bounds1 = one.getBounds(); |
| 245 | const SkRect& bounds2 = two.getBounds(); |
| 246 | SkRect larger = bounds1; |
| 247 | larger.join(bounds2); |
| 248 | SkBitmap bits; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 249 | char out[256]; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 250 | int bitWidth = SkScalarCeil(larger.width()) + 2; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 251 | if (bitWidth * 2 + 1 >= (int) sizeof(out)) { |
| 252 | return false; |
| 253 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 254 | int bitHeight = SkScalarCeil(larger.height()) + 2; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 255 | if (bitHeight >= (int) sizeof(out)) { |
| 256 | return false; |
| 257 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 258 | bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight); |
| 259 | bits.allocPixels(); |
| 260 | SkCanvas canvas(bits); |
| 261 | canvas.drawColor(SK_ColorWHITE); |
| 262 | SkPaint paint; |
| 263 | canvas.save(); |
| 264 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 265 | canvas.drawPath(one, paint); |
| 266 | canvas.restore(); |
| 267 | canvas.save(); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 268 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 269 | canvas.drawPath(two, paint); |
| 270 | canvas.restore(); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 271 | for (int y = 0; y < bitHeight; ++y) { |
| 272 | uint32_t* addr1 = bits.getAddr32(0, y); |
| 273 | int x; |
| 274 | char* outPtr = out; |
| 275 | for (x = 0; x < bitWidth; ++x) { |
| 276 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 277 | } |
| 278 | *outPtr++ = '|'; |
| 279 | for (x = bitWidth; x < bitWidth * 2; ++x) { |
| 280 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 281 | } |
| 282 | *outPtr++ = '\0'; |
| 283 | SkDebugf("%s\n", out); |
| 284 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 285 | return true; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 286 | } |
| 287 | |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 288 | static void showSimplifiedPath(const SkPath& one, const SkPath& two, |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 289 | const SkPath& scaledOne, const SkPath& scaledTwo) { |
| 290 | showPath(one, "original:"); |
| 291 | showPath(two, "simplified:"); |
| 292 | drawAsciiPaths(scaledOne, scaledTwo, true); |
| 293 | } |
| 294 | |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 295 | int comparePaths(const SkPath& one, const SkPath& two, SkBitmap& bitmap) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 296 | int errors2x2; |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 297 | SkPath scaledOne, scaledTwo; |
| 298 | int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 299 | if (errors2x2 == 0) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 300 | return 0; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 301 | } |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 302 | const int MAX_ERRORS = 9; |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 303 | if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) { |
| 304 | showSimplifiedPath(one, two, scaledOne, scaledTwo); |
| 305 | } |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 306 | if (errors2x2 > MAX_ERRORS && gComparePathsAssert) { |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 307 | SkDebugf("%s errors=%d\n", __FUNCTION__, errors); |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 308 | showSimplifiedPath(one, two, scaledOne, scaledTwo); |
| 309 | SkASSERT(0); |
| 310 | } |
| 311 | return errors2x2 > MAX_ERRORS ? errors2x2 : 0; |
| 312 | } |
| 313 | |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 314 | static void showShapeOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b, |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 315 | const SkPath& scaledOne, const SkPath& scaledTwo, const ShapeOp shapeOp, |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 316 | const SkMatrix& scale) { |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 317 | SkASSERT((unsigned) shapeOp < sizeof(opStrs) / sizeof(opStrs[0])); |
| 318 | showPath(a, "minuend:"); |
| 319 | SkDebugf("op: %s\n", opStrs[shapeOp]); |
| 320 | showPath(b, "subtrahend:"); |
caryclark@google.com | 4aaaaea | 2013-02-28 16:12:39 +0000 | [diff] [blame] | 321 | // the region often isn't very helpful since it approximates curves with a lot of line-tos |
| 322 | if (0) showPath(scaledOne, "region:", scale); |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 323 | showPath(two, "op result:"); |
| 324 | drawAsciiPaths(scaledOne, scaledTwo, true); |
| 325 | } |
| 326 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 327 | static int comparePaths(const SkPath& one, const SkPath& scaledOne, const SkPath& two, |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 328 | const SkPath& scaledTwo, |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 329 | SkBitmap& bitmap, const SkPath& a, const SkPath& b, const ShapeOp shapeOp, |
| 330 | const SkMatrix& scale) { |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 331 | int errors2x2; |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 332 | int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2); |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 333 | if (errors2x2 == 0) { |
| 334 | return 0; |
| 335 | } |
| 336 | const int MAX_ERRORS = 8; |
| 337 | if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 338 | showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale); |
caryclark@google.com | 4eeda37 | 2012-12-06 21:47:48 +0000 | [diff] [blame] | 339 | } |
| 340 | if (errors2x2 > MAX_ERRORS && gComparePathsAssert) { |
| 341 | SkDebugf("%s errors=%d\n", __FUNCTION__, errors); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 342 | showShapeOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 343 | SkASSERT(0); |
| 344 | } |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 345 | return errors2x2 > MAX_ERRORS ? errors2x2 : 0; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // doesn't work yet |
| 349 | void comparePathsTiny(const SkPath& one, const SkPath& two) { |
| 350 | const SkRect& bounds1 = one.getBounds(); |
| 351 | const SkRect& bounds2 = two.getBounds(); |
| 352 | SkRect larger = bounds1; |
| 353 | larger.join(bounds2); |
| 354 | SkBitmap bits; |
| 355 | int bitWidth = SkScalarCeil(larger.width()) + 2; |
| 356 | int bitHeight = SkScalarCeil(larger.height()) + 2; |
| 357 | bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight); |
| 358 | bits.allocPixels(); |
| 359 | SkCanvas canvas(bits); |
| 360 | canvas.drawColor(SK_ColorWHITE); |
| 361 | SkPaint paint; |
| 362 | canvas.save(); |
| 363 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 364 | canvas.drawPath(one, paint); |
| 365 | canvas.restore(); |
| 366 | canvas.save(); |
| 367 | canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1); |
| 368 | canvas.drawPath(two, paint); |
| 369 | canvas.restore(); |
| 370 | for (int y = 0; y < bitHeight; ++y) { |
| 371 | uint8_t* addr1 = bits.getAddr1(0, y); |
| 372 | uint8_t* addr2 = bits.getAddr1(bitWidth, y); |
caryclark@google.com | 1ab0aac | 2013-03-13 20:41:48 +0000 | [diff] [blame] | 373 | for (unsigned x = 0; x < bits.rowBytes(); ++x) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 374 | SkASSERT(addr1[x] == addr2[x]); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 379 | bool testSimplify(const SkPath& path, bool fill, SkPath& out, SkBitmap& bitmap) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 380 | if (gShowPath) { |
| 381 | showPath(path); |
| 382 | } |
| 383 | simplify(path, fill, out); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 384 | if (!gComparePaths) { |
| 385 | return true; |
| 386 | } |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 387 | return comparePaths(path, out, bitmap) == 0; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 388 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 389 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 390 | bool testSimplifyx(SkPath& path, bool useXor, SkPath& out, State4& state, |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 391 | const char* pathStr) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 392 | SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType; |
| 393 | path.setFillType(fillType); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 394 | if (gShowPath) { |
| 395 | showPath(path); |
| 396 | } |
| 397 | simplifyx(path, out); |
| 398 | if (!gComparePaths) { |
| 399 | return true; |
| 400 | } |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 401 | int result = comparePaths(path, out, state.bitmap); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 402 | if (result && gPathStrAssert) { |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 403 | SkDebugf("addTest %s\n", state.filename); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 404 | char temp[8192]; |
| 405 | bzero(temp, sizeof(temp)); |
| 406 | SkMemoryWStream stream(temp, sizeof(temp)); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 407 | const char* pathPrefix = NULL; |
| 408 | const char* nameSuffix = NULL; |
| 409 | if (fillType == SkPath::kEvenOdd_FillType) { |
| 410 | pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n"; |
| 411 | nameSuffix = "x"; |
| 412 | } |
| 413 | const char testFunction[] = "testSimplifyx(path);"; |
| 414 | outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, stream); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 415 | SkDebugf(temp); |
| 416 | SkASSERT(0); |
| 417 | } |
| 418 | return result == 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | bool testSimplifyx(const SkPath& path) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 422 | SkPath out; |
| 423 | simplifyx(path, out); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 424 | SkBitmap bitmap; |
reed@google.com | be584d7 | 2012-09-28 19:48:09 +0000 | [diff] [blame] | 425 | int result = comparePaths(path, out, bitmap); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 426 | if (result && gPathStrAssert) { |
| 427 | SkASSERT(0); |
| 428 | } |
| 429 | return result == 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 430 | } |
| 431 | |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 432 | bool testShapeOp(const SkPath& a, const SkPath& b, const ShapeOp shapeOp) { |
caryclark@google.com | d4c8e1e | 2013-03-05 14:13:13 +0000 | [diff] [blame] | 433 | #if FORCE_RELEASE == 0 |
| 434 | showPathData(a); |
| 435 | showOp(shapeOp); |
| 436 | showPathData(b); |
| 437 | #endif |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 438 | SkPath out; |
| 439 | operate(a, b, shapeOp, out); |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 440 | SkPath pathOut, scaledPathOut; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 441 | SkRegion rgnA, rgnB, openClip, rgnOut; |
| 442 | openClip.setRect(-16000, -16000, 16000, 16000); |
| 443 | rgnA.setPath(a, openClip); |
| 444 | rgnB.setPath(b, openClip); |
| 445 | rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp); |
| 446 | rgnOut.getBoundaryPath(&pathOut); |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 447 | |
| 448 | SkMatrix scale; |
| 449 | scaleMatrix(a, b, scale); |
| 450 | SkRegion scaledRgnA, scaledRgnB, scaledRgnOut; |
| 451 | SkPath scaledA, scaledB; |
| 452 | scaledA.addPath(a, scale); |
| 453 | scaledA.setFillType(a.getFillType()); |
| 454 | scaledB.addPath(b, scale); |
| 455 | scaledB.setFillType(b.getFillType()); |
| 456 | scaledRgnA.setPath(scaledA, openClip); |
| 457 | scaledRgnB.setPath(scaledB, openClip); |
| 458 | scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp); |
| 459 | scaledRgnOut.getBoundaryPath(&scaledPathOut); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 460 | SkBitmap bitmap; |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 461 | SkPath scaledOut; |
| 462 | scaledOut.addPath(out, scale); |
| 463 | scaledOut.setFillType(out.getFillType()); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 464 | int result = comparePaths(pathOut, scaledPathOut, out, scaledOut, bitmap, a, b, shapeOp, scale); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 465 | if (result && gPathStrAssert) { |
| 466 | SkASSERT(0); |
| 467 | } |
| 468 | return result == 0; |
| 469 | } |
| 470 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 471 | const int maxThreadsAllocated = 64; |
| 472 | static int maxThreads = 1; |
| 473 | static int threadIndex; |
| 474 | State4 threadState[maxThreadsAllocated]; |
| 475 | static int testNumber; |
| 476 | static const char* testName; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 477 | static bool debugThreads = false; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 478 | |
| 479 | State4* State4::queue = NULL; |
| 480 | pthread_mutex_t State4::addQueue = PTHREAD_MUTEX_INITIALIZER; |
| 481 | pthread_cond_t State4::checkQueue = PTHREAD_COND_INITIALIZER; |
| 482 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 483 | State4::State4() { |
| 484 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150 * 2, 100); |
| 485 | bitmap.allocPixels(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 486 | } |
| 487 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 488 | void createThread(State4* statePtr, void* (*testFun)(void* )) { |
| 489 | int threadError = pthread_create(&statePtr->threadID, NULL, testFun, |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 490 | (void*) statePtr); |
| 491 | SkASSERT(!threadError); |
| 492 | } |
| 493 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 494 | int dispatchTest4(void* (*testFun)(void* ), int a, int b, int c, int d) { |
| 495 | int testsRun = 0; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 496 | State4* statePtr; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 497 | if (!gRunTestsInOneThread) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 498 | pthread_mutex_lock(&State4::addQueue); |
| 499 | if (threadIndex < maxThreads) { |
| 500 | statePtr = &threadState[threadIndex]; |
| 501 | statePtr->testsRun = 0; |
| 502 | statePtr->a = a; |
| 503 | statePtr->b = b; |
| 504 | statePtr->c = c; |
| 505 | statePtr->d = d; |
| 506 | statePtr->done = false; |
| 507 | statePtr->index = threadIndex; |
| 508 | statePtr->last = false; |
| 509 | if (debugThreads) SkDebugf("%s %d create done=%d last=%d\n", __FUNCTION__, |
| 510 | statePtr->index, statePtr->done, statePtr->last); |
| 511 | pthread_cond_init(&statePtr->initialized, NULL); |
| 512 | ++threadIndex; |
| 513 | createThread(statePtr, testFun); |
| 514 | } else { |
| 515 | while (!State4::queue) { |
| 516 | if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__); |
| 517 | pthread_cond_wait(&State4::checkQueue, &State4::addQueue); |
| 518 | } |
| 519 | statePtr = State4::queue; |
| 520 | testsRun += statePtr->testsRun; |
| 521 | statePtr->testsRun = 0; |
| 522 | statePtr->a = a; |
| 523 | statePtr->b = b; |
| 524 | statePtr->c = c; |
| 525 | statePtr->d = d; |
| 526 | statePtr->done = false; |
| 527 | State4::queue = NULL; |
| 528 | for (int index = 0; index < maxThreads; ++index) { |
| 529 | if (threadState[index].done) { |
| 530 | State4::queue = &threadState[index]; |
| 531 | } |
| 532 | } |
| 533 | if (debugThreads) SkDebugf("%s %d init done=%d last=%d queued=%d\n", __FUNCTION__, |
| 534 | statePtr->index, statePtr->done, statePtr->last, |
| 535 | State4::queue ? State4::queue->index : -1); |
| 536 | pthread_cond_signal(&statePtr->initialized); |
| 537 | } |
| 538 | pthread_mutex_unlock(&State4::addQueue); |
| 539 | } else { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 540 | statePtr = &threadState[0]; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 541 | testsRun += statePtr->testsRun; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 542 | statePtr->testsRun = 0; |
| 543 | statePtr->a = a; |
| 544 | statePtr->b = b; |
| 545 | statePtr->c = c; |
| 546 | statePtr->d = d; |
| 547 | statePtr->done = false; |
| 548 | statePtr->index = threadIndex; |
| 549 | statePtr->last = false; |
| 550 | (*testFun)(statePtr); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 551 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 552 | return testsRun; |
| 553 | } |
| 554 | |
| 555 | void initializeTests(const char* test, size_t testNameSize) { |
| 556 | testName = test; |
| 557 | if (!gRunTestsInOneThread) { |
| 558 | int threads = -1; |
| 559 | size_t size = sizeof(threads); |
| 560 | sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0); |
| 561 | if (threads > 0) { |
| 562 | maxThreads = threads; |
| 563 | } else { |
| 564 | maxThreads = 8; |
| 565 | } |
| 566 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 567 | SkFILEStream inFile("../../experimental/Intersection/op.htm"); |
| 568 | if (inFile.isValid()) { |
| 569 | SkTDArray<char> inData; |
| 570 | inData.setCount(inFile.getLength()); |
| 571 | size_t inLen = inData.count(); |
| 572 | inFile.read(inData.begin(), inLen); |
| 573 | inFile.setPath(NULL); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 574 | char* insert = strstr(inData.begin(), marker); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 575 | if (insert) { |
| 576 | insert += sizeof(marker) - 1; |
| 577 | const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1; |
| 578 | testNumber = atoi(numLoc) + 1; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 581 | const char* filename = preferredFilename; |
| 582 | SkFILEWStream preferredTest(filename); |
| 583 | if (!preferredTest.isValid()) { |
| 584 | filename = backupFilename; |
| 585 | SkFILEWStream backupTest(filename); |
| 586 | SkASSERT(backupTest.isValid()); |
| 587 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 588 | for (int index = 0; index < maxThreads; ++index) { |
| 589 | State4* statePtr = &threadState[index]; |
| 590 | strcpy(statePtr->filename, filename); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 591 | size_t len = strlen(filename); |
| 592 | SkASSERT(statePtr->filename[len - 6] == 'X'); |
| 593 | SkASSERT(statePtr->filename[len - 5] == 'X'); |
| 594 | statePtr->filename[len - 6] = '0' + index / 10; |
| 595 | statePtr->filename[len - 5] = '0' + index % 10; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 596 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 597 | threadIndex = 0; |
| 598 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 599 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 600 | void outputProgress(const State4& state, const char* pathStr, SkPath::FillType pathFillType) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 601 | if (gRunTestsInOneThread && gShowOutputProgress) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 602 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 603 | SkDebugf(" path.setFillType(SkPath::kEvenOdd_FillType);\n", pathStr); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 604 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 605 | SkDebugf("%s\n", pathStr); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 606 | } |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 607 | const char testFunction[] = "testSimplifyx(path);"; |
| 608 | const char* pathPrefix = NULL; |
| 609 | const char* nameSuffix = NULL; |
| 610 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 611 | pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n"; |
| 612 | nameSuffix = "x"; |
| 613 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 614 | if (gUsePhysicalFiles) { |
| 615 | SkFILEWStream outFile(state.filename); |
| 616 | if (!outFile.isValid()) { |
| 617 | SkASSERT(0); |
| 618 | return; |
| 619 | } |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 620 | outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outFile); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 621 | return; |
| 622 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 623 | SkFILEWStream outRam(state.filename); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 624 | outputToStream(state, pathStr, pathPrefix, nameSuffix, testFunction, outRam); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 625 | } |
| 626 | |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 627 | void outputProgress(const State4& state, const char* pathStr, ShapeOp op) { |
| 628 | SkString testFunc("testShapeOp(path, pathB, "); |
| 629 | testFunc += opStrs[op]; |
| 630 | testFunc += ");"; |
| 631 | const char* testFunction = testFunc.c_str(); |
| 632 | if (gRunTestsInOneThread && gShowOutputProgress) { |
| 633 | SkDebugf("%s\n", pathStr); |
| 634 | SkDebugf(" %s\n", testFunction); |
| 635 | } |
| 636 | const char* nameSuffix = opSuffixes[op]; |
| 637 | if (gUsePhysicalFiles) { |
| 638 | SkFILEWStream outFile(state.filename); |
| 639 | if (!outFile.isValid()) { |
| 640 | SkASSERT(0); |
| 641 | return; |
| 642 | } |
| 643 | outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outFile); |
| 644 | return; |
| 645 | } |
| 646 | SkFILEWStream outRam(state.filename); |
| 647 | outputToStream(state, pathStr, NULL, nameSuffix, testFunction, outRam); |
| 648 | } |
| 649 | |
| 650 | static void writeTestName(const char* nameSuffix, SkWStream& outFile) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 651 | outFile.writeText(testName); |
| 652 | outFile.writeDecAsText(testNumber); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 653 | if (nameSuffix) { |
| 654 | outFile.writeText(nameSuffix); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 658 | void outputToStream(const State4& state, const char* pathStr, const char* pathPrefix, |
| 659 | const char* nameSuffix, |
| 660 | const char* testFunction, SkWStream& outFile) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 661 | outFile.writeText("<div id=\""); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 662 | writeTestName(nameSuffix, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 663 | outFile.writeText("\">\n"); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 664 | if (pathPrefix) { |
| 665 | outFile.writeText(pathPrefix); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 666 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 667 | outFile.writeText(pathStr); |
| 668 | outFile.writeText("</div>\n\n"); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 669 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 670 | outFile.writeText(marker); |
| 671 | outFile.writeText(" "); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 672 | writeTestName(nameSuffix, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 673 | outFile.writeText(",\n\n\n"); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 674 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 675 | outFile.writeText("static void "); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 676 | writeTestName(nameSuffix, outFile); |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 677 | outFile.writeText("() {\n SkPath path"); |
| 678 | if (!pathPrefix) { |
| 679 | outFile.writeText(", pathB"); |
| 680 | } |
| 681 | outFile.writeText(";\n"); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 682 | if (pathPrefix) { |
| 683 | outFile.writeText(pathPrefix); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 684 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 685 | outFile.writeText(pathStr); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 686 | outFile.writeText(" "); |
| 687 | outFile.writeText(testFunction); |
| 688 | outFile.writeText("\n}\n\n"); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 689 | outFile.writeText("static void (*firstTest)() = "); |
caryclark@google.com | b1c42bb | 2012-11-16 13:16:41 +0000 | [diff] [blame] | 690 | writeTestName(nameSuffix, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 691 | outFile.writeText(";\n\n"); |
| 692 | |
| 693 | outFile.writeText("static struct {\n"); |
| 694 | outFile.writeText(" void (*fun)();\n"); |
| 695 | outFile.writeText(" const char* str;\n"); |
| 696 | outFile.writeText("} tests[] = {\n"); |
| 697 | outFile.writeText(" TEST("); |
caryclark@google.com | 8f9f468 | 2013-01-03 21:18:16 +0000 | [diff] [blame] | 698 | writeTestName(nameSuffix, outFile); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 699 | outFile.writeText("),\n"); |
| 700 | outFile.flush(); |
| 701 | } |
| 702 | |
| 703 | bool runNextTestSet(State4& state) { |
| 704 | if (gRunTestsInOneThread) { |
| 705 | return false; |
| 706 | } |
| 707 | pthread_mutex_lock(&State4::addQueue); |
| 708 | state.done = true; |
| 709 | State4::queue = &state; |
| 710 | if (debugThreads) SkDebugf("%s %d checkQueue done=%d last=%d\n", __FUNCTION__, state.index, |
| 711 | state.done, state.last); |
| 712 | pthread_cond_signal(&State4::checkQueue); |
| 713 | while (state.done && !state.last) { |
| 714 | if (debugThreads) SkDebugf("%s %d done=%d last=%d\n", __FUNCTION__, state.index, state.done, state.last); |
| 715 | pthread_cond_wait(&state.initialized, &State4::addQueue); |
| 716 | } |
| 717 | pthread_mutex_unlock(&State4::addQueue); |
| 718 | return !state.last; |
| 719 | } |
| 720 | |
| 721 | int waitForCompletion() { |
| 722 | int testsRun = 0; |
| 723 | if (!gRunTestsInOneThread) { |
| 724 | pthread_mutex_lock(&State4::addQueue); |
| 725 | int runningThreads = maxThreads; |
| 726 | int index; |
| 727 | while (runningThreads > 0) { |
| 728 | while (!State4::queue) { |
| 729 | if (debugThreads) SkDebugf("%s checkQueue\n", __FUNCTION__); |
| 730 | pthread_cond_wait(&State4::checkQueue, &State4::addQueue); |
| 731 | } |
| 732 | while (State4::queue) { |
| 733 | --runningThreads; |
| 734 | SkDebugf("•"); |
| 735 | State4::queue->last = true; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 736 | State4* next = NULL; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 737 | for (index = 0; index < maxThreads; ++index) { |
| 738 | State4& test = threadState[index]; |
| 739 | if (test.done && !test.last) { |
| 740 | next = &test; |
| 741 | } |
| 742 | } |
| 743 | if (debugThreads) SkDebugf("%s %d next=%d deQueue\n", __FUNCTION__, |
| 744 | State4::queue->index, next ? next->index : -1); |
| 745 | pthread_cond_signal(&State4::queue->initialized); |
| 746 | State4::queue = next; |
| 747 | } |
| 748 | } |
| 749 | pthread_mutex_unlock(&State4::addQueue); |
| 750 | for (index = 0; index < maxThreads; ++index) { |
| 751 | pthread_join(threadState[index].threadID, NULL); |
| 752 | testsRun += threadState[index].testsRun; |
| 753 | } |
| 754 | SkDebugf("\n"); |
| 755 | } |
| 756 | #ifdef SK_DEBUG |
| 757 | gDebugMaxWindSum = SK_MaxS32; |
| 758 | gDebugMaxWindValue = SK_MaxS32; |
| 759 | #endif |
| 760 | return testsRun; |
| 761 | } |