blob: 6f0b09f0822e70c7d8d78626b945e0d9d45248ce [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
19SkDiffContext::SkDiffContext() {
zachr@google.com945708a2013-07-02 19:55:32 +000020 fDiffers = NULL;
21 fDifferCount = 0;
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000022 fThreadCount = SkThreadPool::kThreadPerCore;
zachr@google.com945708a2013-07-02 19:55:32 +000023}
24
25SkDiffContext::~SkDiffContext() {
zachr@google.com945708a2013-07-02 19:55:32 +000026 if (NULL != fDiffers) {
27 SkDELETE_ARRAY(fDiffers);
28 }
29}
30
djsollen@google.com513a7bf2013-11-07 19:24:06 +000031void SkDiffContext::setDifferenceDir(const SkString& path) {
32 if (!path.isEmpty() && sk_mkdir(path.c_str())) {
33 fDifferenceDir = path;
34 }
35}
36
zachr@google.com945708a2013-07-02 19:55:32 +000037void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) {
38 // Delete whatever the last array of differs was
39 if (NULL != fDiffers) {
40 SkDELETE_ARRAY(fDiffers);
41 fDiffers = NULL;
42 fDifferCount = 0;
43 }
44
45 // Copy over the new differs
46 fDifferCount = differs.count();
47 fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount);
48 differs.copy(fDiffers);
49}
50
djsollen@google.com513a7bf2013-11-07 19:24:06 +000051static SkString get_common_prefix(const SkString& a, const SkString& b) {
52 const size_t maxPrefixLength = SkTMin(a.size(), b.size());
53 SkASSERT(maxPrefixLength > 0);
54 for (size_t x = 0; x < maxPrefixLength; ++x) {
55 if (a[x] != b[x]) {
56 SkString result;
57 result.set(a.c_str(), x);
58 return result;
59 }
60 }
61 if (a.size() > b.size()) {
62 return b;
63 } else {
64 return a;
65 }
66}
67
zachr@google.com945708a2013-07-02 19:55:32 +000068void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) {
69 // Load the images at the paths
70 SkBitmap baselineBitmap;
71 SkBitmap testBitmap;
72 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) {
73 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath);
74 return;
75 }
76 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) {
77 SkDebugf("Failed to load bitmap \"%s\"\n", testPath);
78 return;
79 }
80
81 // Setup a record for this diff
djsollen@google.comefc51b72013-11-12 18:29:17 +000082 fRecordMutex.acquire();
83 DiffRecord* newRecord = fRecords.addToHead(DiffRecord());
84 fRecordMutex.release();
zachr@google.com945708a2013-07-02 19:55:32 +000085
djsollen@google.com513a7bf2013-11-07 19:24:06 +000086 // compute the common name
87 SkString baseName = SkOSPath::SkBasename(baselinePath);
88 SkString testName = SkOSPath::SkBasename(testPath);
89 newRecord->fCommonName = get_common_prefix(baseName, testName);
90
djsollen@google.comefc51b72013-11-12 18:29:17 +000091 newRecord->fBaselinePath = baselinePath;
92 newRecord->fTestPath = testPath;
93
djsollen@google.com513a7bf2013-11-07 19:24:06 +000094 bool alphaMaskPending = false;
djsollen@google.comefc51b72013-11-12 18:29:17 +000095
96 // only enable alpha masks if a difference dir has been provided
97 if (!fDifferenceDir.isEmpty()) {
98 alphaMaskPending = true;
99 }
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000100
zachr@google.com945708a2013-07-02 19:55:32 +0000101 // Perform each diff
102 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) {
103 SkImageDiffer* differ = fDiffers[differIndex];
djsollen@google.comefc51b72013-11-12 18:29:17 +0000104
105 // Copy the results into data for this record
106 DiffData& diffData = newRecord->fDiffs.push_back();
107 diffData.fDiffName = differ->getName();
108
109 if (!differ->diff(&baselineBitmap, &testBitmap, alphaMaskPending, &diffData.fResult)) {
djsollen@google.com60bce6c2014-02-11 16:22:58 +0000110 // if the diff failed record -1 as the result
djsollen@google.com27d7ede2014-02-11 16:29:39 +0000111 diffData.fResult.result = -1;
djsollen@google.comefc51b72013-11-12 18:29:17 +0000112 continue;
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000113 }
zachr@google.com945708a2013-07-02 19:55:32 +0000114
djsollen@google.comefc51b72013-11-12 18:29:17 +0000115 if (alphaMaskPending
116 && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result
117 && !diffData.fResult.poiAlphaMask.empty()
118 && !newRecord->fCommonName.isEmpty()) {
zachr@google.com945708a2013-07-02 19:55:32 +0000119
djsollen@google.comefc51b72013-11-12 18:29:17 +0000120 newRecord->fDifferencePath = SkOSPath::SkPathJoin(fDifferenceDir.c_str(),
121 newRecord->fCommonName.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000122
djsollen@google.comefc51b72013-11-12 18:29:17 +0000123 // compute the image diff and output it
124 SkBitmap copy;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000125 diffData.fResult.poiAlphaMask.copyTo(&copy, kN32_SkColorType);
djsollen@google.comefc51b72013-11-12 18:29:17 +0000126 SkImageEncoder::EncodeFile(newRecord->fDifferencePath.c_str(), copy,
127 SkImageEncoder::kPNG_Type, 100);
zachr@google.com945708a2013-07-02 19:55:32 +0000128
djsollen@google.comefc51b72013-11-12 18:29:17 +0000129 // cleanup the existing bitmap to free up resources;
130 diffData.fResult.poiAlphaMask.reset();
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000131
djsollen@google.comefc51b72013-11-12 18:29:17 +0000132 alphaMaskPending = false;
zachr@google.com945708a2013-07-02 19:55:32 +0000133 }
134 }
135}
136
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000137class SkThreadedDiff : public SkRunnable {
138public:
139 SkThreadedDiff() : fDiffContext(NULL) { }
140
141 void setup(SkDiffContext* diffContext, const SkString& baselinePath, const SkString& testPath) {
142 fDiffContext = diffContext;
143 fBaselinePath = baselinePath;
144 fTestPath = testPath;
145 }
146
147 virtual void run() SK_OVERRIDE {
148 fDiffContext->addDiff(fBaselinePath.c_str(), fTestPath.c_str());
149 }
150
151private:
152 SkDiffContext* fDiffContext;
153 SkString fBaselinePath;
154 SkString fTestPath;
155};
zachr@google.com945708a2013-07-02 19:55:32 +0000156
157void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) {
158 // Get the files in the baseline, we will then look for those inside the test path
159 SkTArray<SkString> baselineEntries;
160 if (!get_directory(baselinePath, &baselineEntries)) {
161 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
162 return;
163 }
164
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000165 SkThreadPool threadPool(fThreadCount);
166 SkTArray<SkThreadedDiff> runnableDiffs;
167 runnableDiffs.reset(baselineEntries.count());
168
169 for (int x = 0; x < baselineEntries.count(); x++) {
170 const char* baseFilename = baselineEntries[x].c_str();
zachr@google.com945708a2013-07-02 19:55:32 +0000171
172 // Find the real location of each file to compare
173 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
174 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
175
176 // Check that the test file exists and is a file
177 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
178 // Queue up the comparison with the differ
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000179 runnableDiffs[x].setup(this, baselineFile, testFile);
180 threadPool.add(&runnableDiffs[x]);
zachr@google.com945708a2013-07-02 19:55:32 +0000181 } else {
182 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
183 }
184 }
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000185
186 threadPool.wait();
zachr@google.com945708a2013-07-02 19:55:32 +0000187}
188
189
190void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPattern[]) {
191 // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy
192 // to find corresponding images by matching entry indices.
193
194 SkTArray<SkString> baselineEntries;
195 if (!glob_files(baselinePattern, &baselineEntries)) {
196 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
197 return;
198 }
199
200 SkTArray<SkString> testEntries;
201 if (!glob_files(testPattern, &testEntries)) {
202 SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
203 return;
204 }
205
206 if (baselineEntries.count() != testEntries.count()) {
207 SkDebugf("Baseline and test patterns do not yield corresponding number of files\n");
208 return;
209 }
210
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000211 SkThreadPool threadPool(fThreadCount);
212 SkTArray<SkThreadedDiff> runnableDiffs;
213 runnableDiffs.reset(baselineEntries.count());
zachr@google.com945708a2013-07-02 19:55:32 +0000214
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000215 for (int x = 0; x < baselineEntries.count(); x++) {
216 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]);
217 threadPool.add(&runnableDiffs[x]);
zachr@google.com945708a2013-07-02 19:55:32 +0000218 }
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000219
220 threadPool.wait();
zachr@google.com945708a2013-07-02 19:55:32 +0000221}
222
zachr@google.coma95959c2013-07-08 15:04:45 +0000223void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) {
djsollen@google.comefc51b72013-11-12 18:29:17 +0000224 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart);
225 DiffRecord* currentRecord = iter.get();
226
zachr@google.coma95959c2013-07-08 15:04:45 +0000227 if (useJSONP) {
228 stream.writeText("var SkPDiffRecords = {\n");
zachr@google.com55173f22013-07-25 17:22:58 +0000229 } else {
zachr@google.coma95959c2013-07-08 15:04:45 +0000230 stream.writeText("{\n");
231 }
zachr@google.com945708a2013-07-02 19:55:32 +0000232 stream.writeText(" \"records\": [\n");
233 while (NULL != currentRecord) {
234 stream.writeText(" {\n");
235
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000236 SkString differenceAbsPath = get_absolute_path(currentRecord->fDifferencePath);
zachr@google.coma479aa12013-08-02 15:54:30 +0000237 SkString baselineAbsPath = get_absolute_path(currentRecord->fBaselinePath);
238 SkString testAbsPath = get_absolute_path(currentRecord->fTestPath);
239
djsollen@google.com1e391b52013-10-16 15:00:11 +0000240 stream.writeText(" \"commonName\": \"");
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000241 stream.writeText(currentRecord->fCommonName.c_str());
djsollen@google.com1e391b52013-10-16 15:00:11 +0000242 stream.writeText("\",\n");
243
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000244 stream.writeText(" \"differencePath\": \"");
245 stream.writeText(differenceAbsPath.c_str());
246 stream.writeText("\",\n");
247
zachr@google.com945708a2013-07-02 19:55:32 +0000248 stream.writeText(" \"baselinePath\": \"");
zachr@google.coma479aa12013-08-02 15:54:30 +0000249 stream.writeText(baselineAbsPath.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000250 stream.writeText("\",\n");
251
252 stream.writeText(" \"testPath\": \"");
zachr@google.coma479aa12013-08-02 15:54:30 +0000253 stream.writeText(testAbsPath.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000254 stream.writeText("\",\n");
255
256 stream.writeText(" \"diffs\": [\n");
257 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
258 DiffData& data = currentRecord->fDiffs[diffIndex];
259 stream.writeText(" {\n");
260
261 stream.writeText(" \"differName\": \"");
262 stream.writeText(data.fDiffName);
263 stream.writeText("\",\n");
264
265 stream.writeText(" \"result\": ");
djsollen@google.comefc51b72013-11-12 18:29:17 +0000266 stream.writeScalarAsText((SkScalar)data.fResult.result);
zachr@google.com945708a2013-07-02 19:55:32 +0000267 stream.writeText(",\n");
268
djsollen@google.comefc51b72013-11-12 18:29:17 +0000269 stream.writeText(" \"pointsOfInterest\": ");
270 stream.writeDecAsText(data.fResult.poiCount);
271 stream.writeText("\n");
zachr@google.com945708a2013-07-02 19:55:32 +0000272
zachr@google.com945708a2013-07-02 19:55:32 +0000273 stream.writeText(" }");
274
275 // JSON does not allow trailing commas
zachr@google.com55173f22013-07-25 17:22:58 +0000276 if (diffIndex + 1 < currentRecord->fDiffs.count()) {
zachr@google.com945708a2013-07-02 19:55:32 +0000277 stream.writeText(",");
278 }
279 stream.writeText(" \n");
280 }
281 stream.writeText(" ]\n");
282
283 stream.writeText(" }");
284
djsollen@google.comefc51b72013-11-12 18:29:17 +0000285 currentRecord = iter.next();
286
zachr@google.com945708a2013-07-02 19:55:32 +0000287 // JSON does not allow trailing commas
djsollen@google.comefc51b72013-11-12 18:29:17 +0000288 if (NULL != currentRecord) {
zachr@google.com945708a2013-07-02 19:55:32 +0000289 stream.writeText(",");
290 }
291 stream.writeText("\n");
zachr@google.com945708a2013-07-02 19:55:32 +0000292 }
293 stream.writeText(" ]\n");
zachr@google.coma95959c2013-07-08 15:04:45 +0000294 if (useJSONP) {
295 stream.writeText("};\n");
zachr@google.com55173f22013-07-25 17:22:58 +0000296 } else {
zachr@google.coma95959c2013-07-08 15:04:45 +0000297 stream.writeText("}\n");
298 }
zachr@google.com945708a2013-07-02 19:55:32 +0000299}
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000300
301void SkDiffContext::outputCsv(SkWStream& stream) {
302 SkTDict<int> columns(2);
303 int cntColumns = 0;
304
305 stream.writeText("key");
306
djsollen@google.comefc51b72013-11-12 18:29:17 +0000307 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart);
308 DiffRecord* currentRecord = iter.get();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000309
310 // Write CSV header and create a dictionary of all columns.
311 while (NULL != currentRecord) {
312 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
313 DiffData& data = currentRecord->fDiffs[diffIndex];
314 if (!columns.find(data.fDiffName)) {
315 columns.set(data.fDiffName, cntColumns);
316 stream.writeText(", ");
317 stream.writeText(data.fDiffName);
318 cntColumns++;
319 }
320 }
djsollen@google.comefc51b72013-11-12 18:29:17 +0000321 currentRecord = iter.next();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000322 }
323 stream.writeText("\n");
324
325 double values[100];
326 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so many diff types.
327
djsollen@google.comefc51b72013-11-12 18:29:17 +0000328 SkTLList<DiffRecord>::Iter iter2(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart);
329 currentRecord = iter2.get();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000330 while (NULL != currentRecord) {
331 for (int i = 0; i < cntColumns; i++) {
332 values[i] = -1;
333 }
334
335 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
336 DiffData& data = currentRecord->fDiffs[diffIndex];
337 int index = -1;
338 SkAssertResult(columns.find(data.fDiffName, &index));
339 SkASSERT(index >= 0 && index < cntColumns);
djsollen@google.comefc51b72013-11-12 18:29:17 +0000340 values[index] = data.fResult.result;
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000341 }
342
343 const char* filename = currentRecord->fBaselinePath.c_str() +
344 strlen(currentRecord->fBaselinePath.c_str()) - 1;
345 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1) != '/') {
346 filename--;
347 }
348
349 stream.writeText(filename);
350
351 for (int i = 0; i < cntColumns; i++) {
352 SkString str;
353 str.printf(", %f", values[i]);
354 stream.writeText(str.c_str());
355 }
356 stream.writeText("\n");
357
djsollen@google.comefc51b72013-11-12 18:29:17 +0000358 currentRecord = iter2.next();
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000359 }
360}