blob: f64aeac2acd8fb974c36ba37a11d9bd95636a7ef [file] [log] [blame]
zachr@google.com945708a2013-07-02 19:55:32 +00001/*
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.comcbbf1ca2013-10-16 18:36:49 +000011#include "SkRunnable.h"
zachr@google.com945708a2013-07-02 19:55:32 +000012#include "SkStream.h"
edisonn@google.comc93c8ac2013-07-22 15:24:26 +000013#include "SkTDict.h"
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000014#include "SkThreadPool.h"
zachr@google.com945708a2013-07-02 19:55:32 +000015
16#include "SkDiffContext.h"
zachr@google.com945708a2013-07-02 19:55:32 +000017#include "skpdiff_util.h"
18
zachr@google.comee0f46d2013-07-23 12:57:52 +000019// Truncates the number of points of interests in JSON output to not freeze the parser
20static const int kMaxPOI = 100;
21
zachr@google.com945708a2013-07-02 19:55:32 +000022SkDiffContext::SkDiffContext() {
zachr@google.com945708a2013-07-02 19:55:32 +000023 fDiffers = NULL;
24 fDifferCount = 0;
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000025 fThreadCount = SkThreadPool::kThreadPerCore;
zachr@google.com945708a2013-07-02 19:55:32 +000026}
27
28SkDiffContext::~SkDiffContext() {
zachr@google.com945708a2013-07-02 19:55:32 +000029 if (NULL != fDiffers) {
30 SkDELETE_ARRAY(fDiffers);
31 }
32}
33
djsollen@google.com513a7bf2013-11-07 19:24:06 +000034void SkDiffContext::setDifferenceDir(const SkString& path) {
35 if (!path.isEmpty() && sk_mkdir(path.c_str())) {
36 fDifferenceDir = path;
37 }
38}
39
zachr@google.com945708a2013-07-02 19:55:32 +000040void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) {
41 // Delete whatever the last array of differs was
42 if (NULL != fDiffers) {
43 SkDELETE_ARRAY(fDiffers);
44 fDiffers = NULL;
45 fDifferCount = 0;
46 }
47
48 // Copy over the new differs
49 fDifferCount = differs.count();
50 fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount);
51 differs.copy(fDiffers);
52}
53
djsollen@google.com513a7bf2013-11-07 19:24:06 +000054static SkString get_common_prefix(const SkString& a, const SkString& b) {
55 const size_t maxPrefixLength = SkTMin(a.size(), b.size());
56 SkASSERT(maxPrefixLength > 0);
57 for (size_t x = 0; x < maxPrefixLength; ++x) {
58 if (a[x] != b[x]) {
59 SkString result;
60 result.set(a.c_str(), x);
61 return result;
62 }
63 }
64 if (a.size() > b.size()) {
65 return b;
66 } else {
67 return a;
68 }
69}
70
zachr@google.com945708a2013-07-02 19:55:32 +000071void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) {
72 // Load the images at the paths
73 SkBitmap baselineBitmap;
74 SkBitmap testBitmap;
djsollen@google.comefc51b72013-11-12 18:29:17 +000075 SkAutoMutexAcquire imageLock(fImageMutex);
zachr@google.com945708a2013-07-02 19:55:32 +000076 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) {
77 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath);
78 return;
79 }
80 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) {
81 SkDebugf("Failed to load bitmap \"%s\"\n", testPath);
82 return;
83 }
djsollen@google.comefc51b72013-11-12 18:29:17 +000084 imageLock.release();
zachr@google.com945708a2013-07-02 19:55:32 +000085
86 // Setup a record for this diff
djsollen@google.comefc51b72013-11-12 18:29:17 +000087 fRecordMutex.acquire();
88 DiffRecord* newRecord = fRecords.addToHead(DiffRecord());
89 fRecordMutex.release();
zachr@google.com945708a2013-07-02 19:55:32 +000090
djsollen@google.com513a7bf2013-11-07 19:24:06 +000091 // compute the common name
92 SkString baseName = SkOSPath::SkBasename(baselinePath);
93 SkString testName = SkOSPath::SkBasename(testPath);
94 newRecord->fCommonName = get_common_prefix(baseName, testName);
95
djsollen@google.comefc51b72013-11-12 18:29:17 +000096 newRecord->fBaselinePath = baselinePath;
97 newRecord->fTestPath = testPath;
98
djsollen@google.com513a7bf2013-11-07 19:24:06 +000099 bool alphaMaskPending = false;
djsollen@google.comefc51b72013-11-12 18:29:17 +0000100
101 // only enable alpha masks if a difference dir has been provided
102 if (!fDifferenceDir.isEmpty()) {
103 alphaMaskPending = true;
104 }
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000105
zachr@google.com945708a2013-07-02 19:55:32 +0000106 // Perform each diff
107 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) {
108 SkImageDiffer* differ = fDiffers[differIndex];
djsollen@google.comefc51b72013-11-12 18:29:17 +0000109
110 // Copy the results into data for this record
111 DiffData& diffData = newRecord->fDiffs.push_back();
112 diffData.fDiffName = differ->getName();
113
114 if (!differ->diff(&baselineBitmap, &testBitmap, alphaMaskPending, &diffData.fResult)) {
115 // if the diff failed the remove its entry from the list
116 newRecord->fDiffs.pop_back();
117 continue;
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000118 }
zachr@google.com945708a2013-07-02 19:55:32 +0000119
djsollen@google.comefc51b72013-11-12 18:29:17 +0000120 if (alphaMaskPending
121 && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result
122 && !diffData.fResult.poiAlphaMask.empty()
123 && !newRecord->fCommonName.isEmpty()) {
zachr@google.com945708a2013-07-02 19:55:32 +0000124
djsollen@google.comefc51b72013-11-12 18:29:17 +0000125 newRecord->fDifferencePath = SkOSPath::SkPathJoin(fDifferenceDir.c_str(),
126 newRecord->fCommonName.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000127
djsollen@google.comefc51b72013-11-12 18:29:17 +0000128 // compute the image diff and output it
129 SkBitmap copy;
130 diffData.fResult.poiAlphaMask.copyTo(&copy, SkBitmap::kARGB_8888_Config);
131 SkImageEncoder::EncodeFile(newRecord->fDifferencePath.c_str(), copy,
132 SkImageEncoder::kPNG_Type, 100);
zachr@google.com945708a2013-07-02 19:55:32 +0000133
djsollen@google.comefc51b72013-11-12 18:29:17 +0000134 // cleanup the existing bitmap to free up resources;
135 diffData.fResult.poiAlphaMask.reset();
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000136
djsollen@google.comefc51b72013-11-12 18:29:17 +0000137 alphaMaskPending = false;
zachr@google.com945708a2013-07-02 19:55:32 +0000138 }
139 }
140}
141
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000142class SkThreadedDiff : public SkRunnable {
143public:
144 SkThreadedDiff() : fDiffContext(NULL) { }
145
146 void setup(SkDiffContext* diffContext, const SkString& baselinePath, const SkString& testPath) {
147 fDiffContext = diffContext;
148 fBaselinePath = baselinePath;
149 fTestPath = testPath;
150 }
151
152 virtual void run() SK_OVERRIDE {
153 fDiffContext->addDiff(fBaselinePath.c_str(), fTestPath.c_str());
154 }
155
156private:
157 SkDiffContext* fDiffContext;
158 SkString fBaselinePath;
159 SkString fTestPath;
160};
zachr@google.com945708a2013-07-02 19:55:32 +0000161
162void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) {
163 // Get the files in the baseline, we will then look for those inside the test path
164 SkTArray<SkString> baselineEntries;
165 if (!get_directory(baselinePath, &baselineEntries)) {
166 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
167 return;
168 }
169
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000170 SkThreadPool threadPool(fThreadCount);
171 SkTArray<SkThreadedDiff> runnableDiffs;
172 runnableDiffs.reset(baselineEntries.count());
173
174 for (int x = 0; x < baselineEntries.count(); x++) {
175 const char* baseFilename = baselineEntries[x].c_str();
zachr@google.com945708a2013-07-02 19:55:32 +0000176
177 // Find the real location of each file to compare
178 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
179 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
180
181 // Check that the test file exists and is a file
182 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
183 // Queue up the comparison with the differ
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000184 runnableDiffs[x].setup(this, baselineFile, testFile);
185 threadPool.add(&runnableDiffs[x]);
zachr@google.com945708a2013-07-02 19:55:32 +0000186 } else {
187 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
188 }
189 }
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000190
191 threadPool.wait();
zachr@google.com945708a2013-07-02 19:55:32 +0000192}
193
194
195void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPattern[]) {
196 // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy
197 // to find corresponding images by matching entry indices.
198
199 SkTArray<SkString> baselineEntries;
200 if (!glob_files(baselinePattern, &baselineEntries)) {
201 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
202 return;
203 }
204
205 SkTArray<SkString> testEntries;
206 if (!glob_files(testPattern, &testEntries)) {
207 SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
208 return;
209 }
210
211 if (baselineEntries.count() != testEntries.count()) {
212 SkDebugf("Baseline and test patterns do not yield corresponding number of files\n");
213 return;
214 }
215
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000216 SkThreadPool threadPool(fThreadCount);
217 SkTArray<SkThreadedDiff> runnableDiffs;
218 runnableDiffs.reset(baselineEntries.count());
zachr@google.com945708a2013-07-02 19:55:32 +0000219
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000220 for (int x = 0; x < baselineEntries.count(); x++) {
221 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]);
222 threadPool.add(&runnableDiffs[x]);
zachr@google.com945708a2013-07-02 19:55:32 +0000223 }
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000224
225 threadPool.wait();
zachr@google.com945708a2013-07-02 19:55:32 +0000226}
227
zachr@google.coma95959c2013-07-08 15:04:45 +0000228void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) {
djsollen@google.comefc51b72013-11-12 18:29:17 +0000229 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart);
230 DiffRecord* currentRecord = iter.get();
231
zachr@google.coma95959c2013-07-08 15:04:45 +0000232 if (useJSONP) {
233 stream.writeText("var SkPDiffRecords = {\n");
zachr@google.com55173f22013-07-25 17:22:58 +0000234 } else {
zachr@google.coma95959c2013-07-08 15:04:45 +0000235 stream.writeText("{\n");
236 }
zachr@google.com945708a2013-07-02 19:55:32 +0000237 stream.writeText(" \"records\": [\n");
238 while (NULL != currentRecord) {
239 stream.writeText(" {\n");
240
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000241 SkString differenceAbsPath = get_absolute_path(currentRecord->fDifferencePath);
zachr@google.coma479aa12013-08-02 15:54:30 +0000242 SkString baselineAbsPath = get_absolute_path(currentRecord->fBaselinePath);
243 SkString testAbsPath = get_absolute_path(currentRecord->fTestPath);
244
djsollen@google.com1e391b52013-10-16 15:00:11 +0000245 stream.writeText(" \"commonName\": \"");
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000246 stream.writeText(currentRecord->fCommonName.c_str());
djsollen@google.com1e391b52013-10-16 15:00:11 +0000247 stream.writeText("\",\n");
248
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000249 stream.writeText(" \"differencePath\": \"");
250 stream.writeText(differenceAbsPath.c_str());
251 stream.writeText("\",\n");
252
zachr@google.com945708a2013-07-02 19:55:32 +0000253 stream.writeText(" \"baselinePath\": \"");
zachr@google.coma479aa12013-08-02 15:54:30 +0000254 stream.writeText(baselineAbsPath.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000255 stream.writeText("\",\n");
256
257 stream.writeText(" \"testPath\": \"");
zachr@google.coma479aa12013-08-02 15:54:30 +0000258 stream.writeText(testAbsPath.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000259 stream.writeText("\",\n");
260
261 stream.writeText(" \"diffs\": [\n");
262 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
263 DiffData& data = currentRecord->fDiffs[diffIndex];
264 stream.writeText(" {\n");
265
266 stream.writeText(" \"differName\": \"");
267 stream.writeText(data.fDiffName);
268 stream.writeText("\",\n");
269
270 stream.writeText(" \"result\": ");
djsollen@google.comefc51b72013-11-12 18:29:17 +0000271 stream.writeScalarAsText((SkScalar)data.fResult.result);
zachr@google.com945708a2013-07-02 19:55:32 +0000272 stream.writeText(",\n");
273
djsollen@google.comefc51b72013-11-12 18:29:17 +0000274 stream.writeText(" \"pointsOfInterest\": ");
275 stream.writeDecAsText(data.fResult.poiCount);
276 stream.writeText("\n");
zachr@google.com945708a2013-07-02 19:55:32 +0000277
zachr@google.com945708a2013-07-02 19:55:32 +0000278 stream.writeText(" }");
279
280 // JSON does not allow trailing commas
zachr@google.com55173f22013-07-25 17:22:58 +0000281 if (diffIndex + 1 < currentRecord->fDiffs.count()) {
zachr@google.com945708a2013-07-02 19:55:32 +0000282 stream.writeText(",");
283 }
284 stream.writeText(" \n");
285 }
286 stream.writeText(" ]\n");
287
288 stream.writeText(" }");
289
djsollen@google.comefc51b72013-11-12 18:29:17 +0000290 currentRecord = iter.next();
291
zachr@google.com945708a2013-07-02 19:55:32 +0000292 // JSON does not allow trailing commas
djsollen@google.comefc51b72013-11-12 18:29:17 +0000293 if (NULL != currentRecord) {
zachr@google.com945708a2013-07-02 19:55:32 +0000294 stream.writeText(",");
295 }
296 stream.writeText("\n");
zachr@google.com945708a2013-07-02 19:55:32 +0000297 }
298 stream.writeText(" ]\n");
zachr@google.coma95959c2013-07-08 15:04:45 +0000299 if (useJSONP) {
300 stream.writeText("};\n");
zachr@google.com55173f22013-07-25 17:22:58 +0000301 } else {
zachr@google.coma95959c2013-07-08 15:04:45 +0000302 stream.writeText("}\n");
303 }
zachr@google.com945708a2013-07-02 19:55:32 +0000304}
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000305
306void SkDiffContext::outputCsv(SkWStream& stream) {
307 SkTDict<int> columns(2);
308 int cntColumns = 0;
309
310 stream.writeText("key");
311
djsollen@google.comefc51b72013-11-12 18:29:17 +0000312 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart);
313 DiffRecord* currentRecord = iter.get();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000314
315 // Write CSV header and create a dictionary of all columns.
316 while (NULL != currentRecord) {
317 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
318 DiffData& data = currentRecord->fDiffs[diffIndex];
319 if (!columns.find(data.fDiffName)) {
320 columns.set(data.fDiffName, cntColumns);
321 stream.writeText(", ");
322 stream.writeText(data.fDiffName);
323 cntColumns++;
324 }
325 }
djsollen@google.comefc51b72013-11-12 18:29:17 +0000326 currentRecord = iter.next();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000327 }
328 stream.writeText("\n");
329
330 double values[100];
331 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so many diff types.
332
djsollen@google.comefc51b72013-11-12 18:29:17 +0000333 SkTLList<DiffRecord>::Iter iter2(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart);
334 currentRecord = iter2.get();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000335 while (NULL != currentRecord) {
336 for (int i = 0; i < cntColumns; i++) {
337 values[i] = -1;
338 }
339
340 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
341 DiffData& data = currentRecord->fDiffs[diffIndex];
342 int index = -1;
343 SkAssertResult(columns.find(data.fDiffName, &index));
344 SkASSERT(index >= 0 && index < cntColumns);
djsollen@google.comefc51b72013-11-12 18:29:17 +0000345 values[index] = data.fResult.result;
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000346 }
347
348 const char* filename = currentRecord->fBaselinePath.c_str() +
349 strlen(currentRecord->fBaselinePath.c_str()) - 1;
350 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1) != '/') {
351 filename--;
352 }
353
354 stream.writeText(filename);
355
356 for (int i = 0; i < cntColumns; i++) {
357 SkString str;
358 str.printf(", %f", values[i]);
359 stream.writeText(str.c_str());
360 }
361 stream.writeText("\n");
362
djsollen@google.comefc51b72013-11-12 18:29:17 +0000363 currentRecord = iter2.next();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000364 }
365}