zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 8 | #include "SkBitmap.h" |
| 9 | #include "SkImageDecoder.h" |
| 10 | #include "SkOSFile.h" |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 11 | #include "SkRunnable.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 12 | #include "SkStream.h" |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 13 | #include "SkTDict.h" |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 14 | #include "SkThreadPool.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 15 | |
| 16 | #include "SkDiffContext.h" |
| 17 | #include "SkImageDiffer.h" |
| 18 | #include "skpdiff_util.h" |
| 19 | |
zachr@google.com | ee0f46d | 2013-07-23 12:57:52 +0000 | [diff] [blame] | 20 | // Truncates the number of points of interests in JSON output to not freeze the parser |
| 21 | static const int kMaxPOI = 100; |
| 22 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 23 | SkDiffContext::SkDiffContext() { |
| 24 | fRecords = NULL; |
| 25 | fDiffers = NULL; |
| 26 | fDifferCount = 0; |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 27 | fThreadCount = SkThreadPool::kThreadPerCore; |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | SkDiffContext::~SkDiffContext() { |
| 31 | // Delete the record linked list |
| 32 | DiffRecord* currentRecord = fRecords; |
| 33 | while (NULL != currentRecord) { |
| 34 | DiffRecord* nextRecord = currentRecord->fNext; |
| 35 | SkDELETE(currentRecord); |
| 36 | currentRecord = nextRecord; |
| 37 | } |
| 38 | |
| 39 | if (NULL != fDiffers) { |
| 40 | SkDELETE_ARRAY(fDiffers); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) { |
| 45 | // Delete whatever the last array of differs was |
| 46 | if (NULL != fDiffers) { |
| 47 | SkDELETE_ARRAY(fDiffers); |
| 48 | fDiffers = NULL; |
| 49 | fDifferCount = 0; |
| 50 | } |
| 51 | |
| 52 | // Copy over the new differs |
| 53 | fDifferCount = differs.count(); |
| 54 | fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount); |
| 55 | differs.copy(fDiffers); |
| 56 | } |
| 57 | |
| 58 | void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) { |
| 59 | // Load the images at the paths |
| 60 | SkBitmap baselineBitmap; |
| 61 | SkBitmap testBitmap; |
| 62 | if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) { |
| 63 | SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath); |
| 64 | return; |
| 65 | } |
| 66 | if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) { |
| 67 | SkDebugf("Failed to load bitmap \"%s\"\n", testPath); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // Setup a record for this diff |
| 72 | DiffRecord* newRecord = SkNEW(DiffRecord); |
| 73 | newRecord->fBaselinePath = baselinePath; |
| 74 | newRecord->fTestPath = testPath; |
| 75 | newRecord->fNext = fRecords; |
| 76 | fRecords = newRecord; |
| 77 | |
| 78 | // Perform each diff |
| 79 | for (int differIndex = 0; differIndex < fDifferCount; differIndex++) { |
| 80 | SkImageDiffer* differ = fDiffers[differIndex]; |
| 81 | int diffID = differ->queueDiff(&baselineBitmap, &testBitmap); |
| 82 | if (diffID >= 0) { |
| 83 | |
| 84 | // Copy the results into data for this record |
| 85 | DiffData& diffData = newRecord->fDiffs.push_back(); |
| 86 | |
| 87 | diffData.fDiffName = differ->getName(); |
| 88 | diffData.fResult = differ->getResult(diffID); |
| 89 | |
| 90 | int poiCount = differ->getPointsOfInterestCount(diffID); |
| 91 | SkIPoint* poi = differ->getPointsOfInterest(diffID); |
| 92 | diffData.fPointsOfInterest.append(poiCount, poi); |
| 93 | |
| 94 | // Because we are doing everything synchronously for now, we are done with the diff |
| 95 | // after reading it. |
| 96 | differ->deleteDiff(diffID); |
| 97 | } |
| 98 | } |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 99 | |
| 100 | // if we get a difference and we want the alpha mask then compute it here. |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 101 | } |
| 102 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 103 | class SkThreadedDiff : public SkRunnable { |
| 104 | public: |
| 105 | SkThreadedDiff() : fDiffContext(NULL) { } |
| 106 | |
| 107 | void setup(SkDiffContext* diffContext, const SkString& baselinePath, const SkString& testPath) { |
| 108 | fDiffContext = diffContext; |
| 109 | fBaselinePath = baselinePath; |
| 110 | fTestPath = testPath; |
| 111 | } |
| 112 | |
| 113 | virtual void run() SK_OVERRIDE { |
| 114 | fDiffContext->addDiff(fBaselinePath.c_str(), fTestPath.c_str()); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | SkDiffContext* fDiffContext; |
| 119 | SkString fBaselinePath; |
| 120 | SkString fTestPath; |
| 121 | }; |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 122 | |
| 123 | void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) { |
| 124 | // Get the files in the baseline, we will then look for those inside the test path |
| 125 | SkTArray<SkString> baselineEntries; |
| 126 | if (!get_directory(baselinePath, &baselineEntries)) { |
| 127 | SkDebugf("Unable to open path \"%s\"\n", baselinePath); |
| 128 | return; |
| 129 | } |
| 130 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 131 | SkThreadPool threadPool(fThreadCount); |
| 132 | SkTArray<SkThreadedDiff> runnableDiffs; |
| 133 | runnableDiffs.reset(baselineEntries.count()); |
| 134 | |
| 135 | for (int x = 0; x < baselineEntries.count(); x++) { |
| 136 | const char* baseFilename = baselineEntries[x].c_str(); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 137 | |
| 138 | // Find the real location of each file to compare |
| 139 | SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename); |
| 140 | SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename); |
| 141 | |
| 142 | // Check that the test file exists and is a file |
| 143 | if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) { |
| 144 | // Queue up the comparison with the differ |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 145 | runnableDiffs[x].setup(this, baselineFile, testFile); |
| 146 | threadPool.add(&runnableDiffs[x]); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 147 | } else { |
| 148 | SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str()); |
| 149 | } |
| 150 | } |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 151 | |
| 152 | threadPool.wait(); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPattern[]) { |
| 157 | // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy |
| 158 | // to find corresponding images by matching entry indices. |
| 159 | |
| 160 | SkTArray<SkString> baselineEntries; |
| 161 | if (!glob_files(baselinePattern, &baselineEntries)) { |
| 162 | SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | SkTArray<SkString> testEntries; |
| 167 | if (!glob_files(testPattern, &testEntries)) { |
| 168 | SkDebugf("Unable to get pattern \"%s\"\n", testPattern); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (baselineEntries.count() != testEntries.count()) { |
| 173 | SkDebugf("Baseline and test patterns do not yield corresponding number of files\n"); |
| 174 | return; |
| 175 | } |
| 176 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 177 | SkThreadPool threadPool(fThreadCount); |
| 178 | SkTArray<SkThreadedDiff> runnableDiffs; |
| 179 | runnableDiffs.reset(baselineEntries.count()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 180 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 181 | for (int x = 0; x < baselineEntries.count(); x++) { |
| 182 | runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); |
| 183 | threadPool.add(&runnableDiffs[x]); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 184 | } |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 185 | |
| 186 | threadPool.wait(); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 187 | } |
| 188 | |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 189 | void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 190 | DiffRecord* currentRecord = fRecords; |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 191 | if (useJSONP) { |
| 192 | stream.writeText("var SkPDiffRecords = {\n"); |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 193 | } else { |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 194 | stream.writeText("{\n"); |
| 195 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 196 | stream.writeText(" \"records\": [\n"); |
| 197 | while (NULL != currentRecord) { |
| 198 | stream.writeText(" {\n"); |
| 199 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 200 | SkString differenceAbsPath = get_absolute_path(currentRecord->fDifferencePath); |
zachr@google.com | a479aa1 | 2013-08-02 15:54:30 +0000 | [diff] [blame] | 201 | SkString baselineAbsPath = get_absolute_path(currentRecord->fBaselinePath); |
| 202 | SkString testAbsPath = get_absolute_path(currentRecord->fTestPath); |
| 203 | |
djsollen@google.com | 1e391b5 | 2013-10-16 15:00:11 +0000 | [diff] [blame] | 204 | // strip off directory structure and find the common part of the filename |
| 205 | SkString baseName = SkOSPath::SkBasename(baselineAbsPath.c_str()); |
| 206 | SkString testName = SkOSPath::SkBasename(testAbsPath.c_str()); |
| 207 | for (size_t x = 0; x < baseName.size(); ++x) { |
| 208 | if (baseName[x] != testName[x]) { |
| 209 | baseName.insertUnichar(x, '\n'); |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | stream.writeText(" \"commonName\": \""); |
| 215 | stream.writeText(baseName.c_str()); |
| 216 | stream.writeText("\",\n"); |
| 217 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame^] | 218 | stream.writeText(" \"differencePath\": \""); |
| 219 | stream.writeText(differenceAbsPath.c_str()); |
| 220 | stream.writeText("\",\n"); |
| 221 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 222 | stream.writeText(" \"baselinePath\": \""); |
zachr@google.com | a479aa1 | 2013-08-02 15:54:30 +0000 | [diff] [blame] | 223 | stream.writeText(baselineAbsPath.c_str()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 224 | stream.writeText("\",\n"); |
| 225 | |
| 226 | stream.writeText(" \"testPath\": \""); |
zachr@google.com | a479aa1 | 2013-08-02 15:54:30 +0000 | [diff] [blame] | 227 | stream.writeText(testAbsPath.c_str()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 228 | stream.writeText("\",\n"); |
| 229 | |
| 230 | stream.writeText(" \"diffs\": [\n"); |
| 231 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 232 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 233 | stream.writeText(" {\n"); |
| 234 | |
| 235 | stream.writeText(" \"differName\": \""); |
| 236 | stream.writeText(data.fDiffName); |
| 237 | stream.writeText("\",\n"); |
| 238 | |
| 239 | stream.writeText(" \"result\": "); |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 240 | stream.writeScalarAsText((SkScalar)data.fResult); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 241 | stream.writeText(",\n"); |
| 242 | |
| 243 | stream.writeText(" \"pointsOfInterest\": [\n"); |
zachr@google.com | ee0f46d | 2013-07-23 12:57:52 +0000 | [diff] [blame] | 244 | for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.count() && |
| 245 | poiIndex < kMaxPOI; poiIndex++) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 246 | SkIPoint poi = data.fPointsOfInterest[poiIndex]; |
| 247 | stream.writeText(" ["); |
| 248 | stream.writeDecAsText(poi.x()); |
| 249 | stream.writeText(","); |
| 250 | stream.writeDecAsText(poi.y()); |
| 251 | stream.writeText("]"); |
| 252 | |
| 253 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 254 | if (poiIndex + 1 < data.fPointsOfInterest.count() && |
| 255 | poiIndex + 1 < kMaxPOI) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 256 | stream.writeText(","); |
| 257 | } |
| 258 | stream.writeText("\n"); |
| 259 | } |
| 260 | stream.writeText(" ]\n"); |
| 261 | stream.writeText(" }"); |
| 262 | |
| 263 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 264 | if (diffIndex + 1 < currentRecord->fDiffs.count()) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 265 | stream.writeText(","); |
| 266 | } |
| 267 | stream.writeText(" \n"); |
| 268 | } |
| 269 | stream.writeText(" ]\n"); |
| 270 | |
| 271 | stream.writeText(" }"); |
| 272 | |
| 273 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 274 | if (NULL != currentRecord->fNext) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 275 | stream.writeText(","); |
| 276 | } |
| 277 | stream.writeText("\n"); |
| 278 | currentRecord = currentRecord->fNext; |
| 279 | } |
| 280 | stream.writeText(" ]\n"); |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 281 | if (useJSONP) { |
| 282 | stream.writeText("};\n"); |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 283 | } else { |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 284 | stream.writeText("}\n"); |
| 285 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 286 | } |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 287 | |
| 288 | void SkDiffContext::outputCsv(SkWStream& stream) { |
| 289 | SkTDict<int> columns(2); |
| 290 | int cntColumns = 0; |
| 291 | |
| 292 | stream.writeText("key"); |
| 293 | |
| 294 | DiffRecord* currentRecord = fRecords; |
| 295 | |
| 296 | // Write CSV header and create a dictionary of all columns. |
| 297 | while (NULL != currentRecord) { |
| 298 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 299 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 300 | if (!columns.find(data.fDiffName)) { |
| 301 | columns.set(data.fDiffName, cntColumns); |
| 302 | stream.writeText(", "); |
| 303 | stream.writeText(data.fDiffName); |
| 304 | cntColumns++; |
| 305 | } |
| 306 | } |
| 307 | currentRecord = currentRecord->fNext; |
| 308 | } |
| 309 | stream.writeText("\n"); |
| 310 | |
| 311 | double values[100]; |
| 312 | SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so many diff types. |
| 313 | |
| 314 | currentRecord = fRecords; |
| 315 | while (NULL != currentRecord) { |
| 316 | for (int i = 0; i < cntColumns; i++) { |
| 317 | values[i] = -1; |
| 318 | } |
| 319 | |
| 320 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 321 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 322 | int index = -1; |
| 323 | SkAssertResult(columns.find(data.fDiffName, &index)); |
| 324 | SkASSERT(index >= 0 && index < cntColumns); |
| 325 | values[index] = data.fResult; |
| 326 | } |
| 327 | |
| 328 | const char* filename = currentRecord->fBaselinePath.c_str() + |
| 329 | strlen(currentRecord->fBaselinePath.c_str()) - 1; |
| 330 | while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1) != '/') { |
| 331 | filename--; |
| 332 | } |
| 333 | |
| 334 | stream.writeText(filename); |
| 335 | |
| 336 | for (int i = 0; i < cntColumns; i++) { |
| 337 | SkString str; |
| 338 | str.printf(", %f", values[i]); |
| 339 | stream.writeText(str.c_str()); |
| 340 | } |
| 341 | stream.writeText("\n"); |
| 342 | |
| 343 | currentRecord = currentRecord->fNext; |
| 344 | } |
| 345 | } |