blob: 07d304b64cb2e0fac003848c8cfa1e7d2bb80fed [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"
17#include "SkImageDiffer.h"
18#include "skpdiff_util.h"
19
zachr@google.comee0f46d2013-07-23 12:57:52 +000020// Truncates the number of points of interests in JSON output to not freeze the parser
21static const int kMaxPOI = 100;
22
zachr@google.com945708a2013-07-02 19:55:32 +000023SkDiffContext::SkDiffContext() {
24 fRecords = NULL;
25 fDiffers = NULL;
26 fDifferCount = 0;
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000027 fThreadCount = SkThreadPool::kThreadPerCore;
zachr@google.com945708a2013-07-02 19:55:32 +000028}
29
30SkDiffContext::~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
44void 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
58void 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.comcbbf1ca2013-10-16 18:36:49 +000099
100 // if we get a difference and we want the alpha mask then compute it here.
zachr@google.com945708a2013-07-02 19:55:32 +0000101}
102
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000103class SkThreadedDiff : public SkRunnable {
104public:
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
117private:
118 SkDiffContext* fDiffContext;
119 SkString fBaselinePath;
120 SkString fTestPath;
121};
zachr@google.com945708a2013-07-02 19:55:32 +0000122
123void 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.comcbbf1ca2013-10-16 18:36:49 +0000131 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.com945708a2013-07-02 19:55:32 +0000137
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.comcbbf1ca2013-10-16 18:36:49 +0000145 runnableDiffs[x].setup(this, baselineFile, testFile);
146 threadPool.add(&runnableDiffs[x]);
zachr@google.com945708a2013-07-02 19:55:32 +0000147 } else {
148 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
149 }
150 }
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000151
152 threadPool.wait();
zachr@google.com945708a2013-07-02 19:55:32 +0000153}
154
155
156void 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.comcbbf1ca2013-10-16 18:36:49 +0000177 SkThreadPool threadPool(fThreadCount);
178 SkTArray<SkThreadedDiff> runnableDiffs;
179 runnableDiffs.reset(baselineEntries.count());
zachr@google.com945708a2013-07-02 19:55:32 +0000180
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000181 for (int x = 0; x < baselineEntries.count(); x++) {
182 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]);
183 threadPool.add(&runnableDiffs[x]);
zachr@google.com945708a2013-07-02 19:55:32 +0000184 }
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000185
186 threadPool.wait();
zachr@google.com945708a2013-07-02 19:55:32 +0000187}
188
zachr@google.coma95959c2013-07-08 15:04:45 +0000189void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) {
zachr@google.com945708a2013-07-02 19:55:32 +0000190 DiffRecord* currentRecord = fRecords;
zachr@google.coma95959c2013-07-08 15:04:45 +0000191 if (useJSONP) {
192 stream.writeText("var SkPDiffRecords = {\n");
zachr@google.com55173f22013-07-25 17:22:58 +0000193 } else {
zachr@google.coma95959c2013-07-08 15:04:45 +0000194 stream.writeText("{\n");
195 }
zachr@google.com945708a2013-07-02 19:55:32 +0000196 stream.writeText(" \"records\": [\n");
197 while (NULL != currentRecord) {
198 stream.writeText(" {\n");
199
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000200 SkString differenceAbsPath = get_absolute_path(currentRecord->fDifferencePath);
zachr@google.coma479aa12013-08-02 15:54:30 +0000201 SkString baselineAbsPath = get_absolute_path(currentRecord->fBaselinePath);
202 SkString testAbsPath = get_absolute_path(currentRecord->fTestPath);
203
djsollen@google.com1e391b52013-10-16 15:00:11 +0000204 // 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.comcbbf1ca2013-10-16 18:36:49 +0000218 stream.writeText(" \"differencePath\": \"");
219 stream.writeText(differenceAbsPath.c_str());
220 stream.writeText("\",\n");
221
zachr@google.com945708a2013-07-02 19:55:32 +0000222 stream.writeText(" \"baselinePath\": \"");
zachr@google.coma479aa12013-08-02 15:54:30 +0000223 stream.writeText(baselineAbsPath.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000224 stream.writeText("\",\n");
225
226 stream.writeText(" \"testPath\": \"");
zachr@google.coma479aa12013-08-02 15:54:30 +0000227 stream.writeText(testAbsPath.c_str());
zachr@google.com945708a2013-07-02 19:55:32 +0000228 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.com35f02fb2013-07-22 17:05:24 +0000240 stream.writeScalarAsText((SkScalar)data.fResult);
zachr@google.com945708a2013-07-02 19:55:32 +0000241 stream.writeText(",\n");
242
243 stream.writeText(" \"pointsOfInterest\": [\n");
zachr@google.comee0f46d2013-07-23 12:57:52 +0000244 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.count() &&
245 poiIndex < kMaxPOI; poiIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000246 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.com55173f22013-07-25 17:22:58 +0000254 if (poiIndex + 1 < data.fPointsOfInterest.count() &&
255 poiIndex + 1 < kMaxPOI) {
zachr@google.com945708a2013-07-02 19:55:32 +0000256 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.com55173f22013-07-25 17:22:58 +0000264 if (diffIndex + 1 < currentRecord->fDiffs.count()) {
zachr@google.com945708a2013-07-02 19:55:32 +0000265 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.com55173f22013-07-25 17:22:58 +0000274 if (NULL != currentRecord->fNext) {
zachr@google.com945708a2013-07-02 19:55:32 +0000275 stream.writeText(",");
276 }
277 stream.writeText("\n");
278 currentRecord = currentRecord->fNext;
279 }
280 stream.writeText(" ]\n");
zachr@google.coma95959c2013-07-08 15:04:45 +0000281 if (useJSONP) {
282 stream.writeText("};\n");
zachr@google.com55173f22013-07-25 17:22:58 +0000283 } else {
zachr@google.coma95959c2013-07-08 15:04:45 +0000284 stream.writeText("}\n");
285 }
zachr@google.com945708a2013-07-02 19:55:32 +0000286}
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000287
288void 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}