OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkOSFile.h" | 10 #include "SkOSFile.h" |
11 #include "SkRunnable.h" | 11 #include "SkRunnable.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkTDict.h" | 13 #include "SkTDict.h" |
14 #include "SkThreadPool.h" | 14 #include "SkThreadPool.h" |
15 | 15 |
16 #include "SkDiffContext.h" | 16 #include "SkDiffContext.h" |
17 #include "SkImageDiffer.h" | |
18 #include "skpdiff_util.h" | 17 #include "skpdiff_util.h" |
19 | 18 |
20 // Truncates the number of points of interests in JSON output to not freeze the
parser | 19 // Truncates the number of points of interests in JSON output to not freeze the
parser |
21 static const int kMaxPOI = 100; | 20 static const int kMaxPOI = 100; |
22 | 21 |
23 SkDiffContext::SkDiffContext() { | 22 SkDiffContext::SkDiffContext() { |
24 fRecords = NULL; | |
25 fDiffers = NULL; | 23 fDiffers = NULL; |
26 fDifferCount = 0; | 24 fDifferCount = 0; |
27 fThreadCount = SkThreadPool::kThreadPerCore; | 25 fThreadCount = SkThreadPool::kThreadPerCore; |
28 } | 26 } |
29 | 27 |
30 SkDiffContext::~SkDiffContext() { | 28 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) { | 29 if (NULL != fDiffers) { |
40 SkDELETE_ARRAY(fDiffers); | 30 SkDELETE_ARRAY(fDiffers); |
41 } | 31 } |
42 } | 32 } |
43 | 33 |
44 void SkDiffContext::setDifferenceDir(const SkString& path) { | 34 void SkDiffContext::setDifferenceDir(const SkString& path) { |
45 if (!path.isEmpty() && sk_mkdir(path.c_str())) { | 35 if (!path.isEmpty() && sk_mkdir(path.c_str())) { |
46 fDifferenceDir = path; | 36 fDifferenceDir = path; |
47 } | 37 } |
48 } | 38 } |
(...skipping 26 matching lines...) Expand all Loading... |
75 return b; | 65 return b; |
76 } else { | 66 } else { |
77 return a; | 67 return a; |
78 } | 68 } |
79 } | 69 } |
80 | 70 |
81 void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) { | 71 void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) { |
82 // Load the images at the paths | 72 // Load the images at the paths |
83 SkBitmap baselineBitmap; | 73 SkBitmap baselineBitmap; |
84 SkBitmap testBitmap; | 74 SkBitmap testBitmap; |
| 75 SkAutoMutexAcquire imageLock(fImageMutex); |
85 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) { | 76 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) { |
86 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath); | 77 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath); |
87 return; | 78 return; |
88 } | 79 } |
89 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) { | 80 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) { |
90 SkDebugf("Failed to load bitmap \"%s\"\n", testPath); | 81 SkDebugf("Failed to load bitmap \"%s\"\n", testPath); |
91 return; | 82 return; |
92 } | 83 } |
| 84 imageLock.release(); |
93 | 85 |
94 // Setup a record for this diff | 86 // Setup a record for this diff |
95 DiffRecord* newRecord = SkNEW(DiffRecord); | 87 fRecordMutex.acquire(); |
96 newRecord->fBaselinePath = baselinePath; | 88 DiffRecord* newRecord = fRecords.addToHead(DiffRecord()); |
97 newRecord->fTestPath = testPath; | 89 fRecordMutex.release(); |
98 newRecord->fNext = fRecords; | |
99 fRecords = newRecord; | |
100 | 90 |
101 // compute the common name | 91 // compute the common name |
102 SkString baseName = SkOSPath::SkBasename(baselinePath); | 92 SkString baseName = SkOSPath::SkBasename(baselinePath); |
103 SkString testName = SkOSPath::SkBasename(testPath); | 93 SkString testName = SkOSPath::SkBasename(testPath); |
104 newRecord->fCommonName = get_common_prefix(baseName, testName); | 94 newRecord->fCommonName = get_common_prefix(baseName, testName); |
105 | 95 |
| 96 newRecord->fBaselinePath = baselinePath; |
| 97 newRecord->fTestPath = testPath; |
| 98 |
106 bool alphaMaskPending = false; | 99 bool alphaMaskPending = false; |
107 bool alphaMaskCreated = false; | 100 |
| 101 // only enable alpha masks if a difference dir has been provided |
| 102 if (!fDifferenceDir.isEmpty()) { |
| 103 alphaMaskPending = true; |
| 104 } |
108 | 105 |
109 // Perform each diff | 106 // Perform each diff |
110 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) { | 107 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) { |
111 SkImageDiffer* differ = fDiffers[differIndex]; | 108 SkImageDiffer* differ = fDiffers[differIndex]; |
112 // TODO only enable for one differ | 109 |
113 if (!alphaMaskCreated && !fDifferenceDir.isEmpty()) { | 110 // Copy the results into data for this record |
114 alphaMaskPending = differ->enablePOIAlphaMask(); | 111 DiffData& diffData = newRecord->fDiffs.push_back(); |
| 112 diffData.fDiffName = differ->getName(); |
| 113 |
| 114 if (!differ->diff(&baselineBitmap, &testBitmap, alphaMaskPending, &diffD
ata.fResult)) { |
| 115 // if the diff failed the remove its entry from the list |
| 116 newRecord->fDiffs.pop_back(); |
| 117 continue; |
115 } | 118 } |
116 int diffID = differ->queueDiff(&baselineBitmap, &testBitmap); | |
117 if (diffID >= 0) { | |
118 | 119 |
119 // Copy the results into data for this record | 120 if (alphaMaskPending |
120 DiffData& diffData = newRecord->fDiffs.push_back(); | 121 && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result |
| 122 && !diffData.fResult.poiAlphaMask.empty() |
| 123 && !newRecord->fCommonName.isEmpty()) { |
121 | 124 |
122 diffData.fDiffName = differ->getName(); | 125 newRecord->fDifferencePath = SkOSPath::SkPathJoin(fDifferenceDir.c_s
tr(), |
123 diffData.fResult = differ->getResult(diffID); | 126 newRecord->fCommon
Name.c_str()); |
124 | 127 |
125 int poiCount = differ->getPointsOfInterestCount(diffID); | 128 // compute the image diff and output it |
126 SkIPoint* poi = differ->getPointsOfInterest(diffID); | 129 SkBitmap copy; |
127 diffData.fPointsOfInterest.append(poiCount, poi); | 130 diffData.fResult.poiAlphaMask.copyTo(©, SkBitmap::kARGB_8888_Con
fig); |
| 131 SkImageEncoder::EncodeFile(newRecord->fDifferencePath.c_str(), copy, |
| 132 SkImageEncoder::kPNG_Type, 100); |
128 | 133 |
129 if (alphaMaskPending | 134 // cleanup the existing bitmap to free up resources; |
130 && SkImageDiffer::RESULT_CORRECT != diffData.fResult | 135 diffData.fResult.poiAlphaMask.reset(); |
131 && newRecord->fDifferencePath.isEmpty()) { | |
132 newRecord->fDifferencePath = SkOSPath::SkPathJoin(fDifferenceDir
.c_str(), | |
133 newRecord->fCo
mmonName.c_str()); | |
134 | 136 |
135 // compute the image diff and output it | 137 alphaMaskPending = false; |
136 SkBitmap* alphaMask = differ->getPointsOfInterestAlphaMask(diffI
D); | |
137 SkBitmap copy; | |
138 alphaMask->copyTo(©, SkBitmap::kARGB_8888_Config); | |
139 SkImageEncoder::EncodeFile(newRecord->fDifferencePath.c_str(), c
opy, | |
140 SkImageEncoder::kPNG_Type, 100); | |
141 } | |
142 | |
143 if (alphaMaskPending) { | |
144 alphaMaskPending = false; | |
145 alphaMaskCreated = true; | |
146 } | |
147 | |
148 // Because we are doing everything synchronously for now, we are don
e with the diff | |
149 // after reading it. | |
150 differ->deleteDiff(diffID); | |
151 } | 138 } |
152 } | 139 } |
153 | |
154 // if we get a difference and we want the alpha mask then compute it here. | |
155 } | 140 } |
156 | 141 |
157 class SkThreadedDiff : public SkRunnable { | 142 class SkThreadedDiff : public SkRunnable { |
158 public: | 143 public: |
159 SkThreadedDiff() : fDiffContext(NULL) { } | 144 SkThreadedDiff() : fDiffContext(NULL) { } |
160 | 145 |
161 void setup(SkDiffContext* diffContext, const SkString& baselinePath, const S
kString& testPath) { | 146 void setup(SkDiffContext* diffContext, const SkString& baselinePath, const S
kString& testPath) { |
162 fDiffContext = diffContext; | 147 fDiffContext = diffContext; |
163 fBaselinePath = baselinePath; | 148 fBaselinePath = baselinePath; |
164 fTestPath = testPath; | 149 fTestPath = testPath; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 219 |
235 for (int x = 0; x < baselineEntries.count(); x++) { | 220 for (int x = 0; x < baselineEntries.count(); x++) { |
236 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); | 221 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); |
237 threadPool.add(&runnableDiffs[x]); | 222 threadPool.add(&runnableDiffs[x]); |
238 } | 223 } |
239 | 224 |
240 threadPool.wait(); | 225 threadPool.wait(); |
241 } | 226 } |
242 | 227 |
243 void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { | 228 void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { |
244 DiffRecord* currentRecord = fRecords; | 229 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_
IterStart); |
| 230 DiffRecord* currentRecord = iter.get(); |
| 231 |
245 if (useJSONP) { | 232 if (useJSONP) { |
246 stream.writeText("var SkPDiffRecords = {\n"); | 233 stream.writeText("var SkPDiffRecords = {\n"); |
247 } else { | 234 } else { |
248 stream.writeText("{\n"); | 235 stream.writeText("{\n"); |
249 } | 236 } |
250 stream.writeText(" \"records\": [\n"); | 237 stream.writeText(" \"records\": [\n"); |
251 while (NULL != currentRecord) { | 238 while (NULL != currentRecord) { |
252 stream.writeText(" {\n"); | 239 stream.writeText(" {\n"); |
253 | 240 |
254 SkString differenceAbsPath = get_absolute_path(currentRecord->fDiffe
rencePath); | 241 SkString differenceAbsPath = get_absolute_path(currentRecord->fDiffe
rencePath); |
(...skipping 19 matching lines...) Expand all Loading... |
274 stream.writeText(" \"diffs\": [\n"); | 261 stream.writeText(" \"diffs\": [\n"); |
275 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d
iffIndex++) { | 262 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d
iffIndex++) { |
276 DiffData& data = currentRecord->fDiffs[diffIndex]; | 263 DiffData& data = currentRecord->fDiffs[diffIndex]; |
277 stream.writeText(" {\n"); | 264 stream.writeText(" {\n"); |
278 | 265 |
279 stream.writeText(" \"differName\": \""); | 266 stream.writeText(" \"differName\": \""); |
280 stream.writeText(data.fDiffName); | 267 stream.writeText(data.fDiffName); |
281 stream.writeText("\",\n"); | 268 stream.writeText("\",\n"); |
282 | 269 |
283 stream.writeText(" \"result\": "); | 270 stream.writeText(" \"result\": "); |
284 stream.writeScalarAsText((SkScalar)data.fResult); | 271 stream.writeScalarAsText((SkScalar)data.fResult.result); |
285 stream.writeText(",\n"); | 272 stream.writeText(",\n"); |
286 | 273 |
287 stream.writeText(" \"pointsOfInterest\":
[\n"); | 274 stream.writeText(" \"pointsOfInterest\":
"); |
288 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.cou
nt() && | 275 stream.writeDecAsText(data.fResult.poiCount); |
289 poiIndex < kMaxPOI; poiIndex++) { | 276 stream.writeText("\n"); |
290 SkIPoint poi = data.fPointsOfInterest[poiIndex]; | |
291 stream.writeText(" ["); | |
292 stream.writeDecAsText(poi.x()); | |
293 stream.writeText(","); | |
294 stream.writeDecAsText(poi.y()); | |
295 stream.writeText("]"); | |
296 | 277 |
297 // JSON does not allow trailing commas | |
298 if (poiIndex + 1 < data.fPointsOfInterest.count() && | |
299 poiIndex + 1 < kMaxPOI) { | |
300 stream.writeText(","); | |
301 } | |
302 stream.writeText("\n"); | |
303 } | |
304 stream.writeText(" ]\n"); | |
305 stream.writeText(" }"); | 278 stream.writeText(" }"); |
306 | 279 |
307 // JSON does not allow trailing commas | 280 // JSON does not allow trailing commas |
308 if (diffIndex + 1 < currentRecord->fDiffs.count()) { | 281 if (diffIndex + 1 < currentRecord->fDiffs.count()) { |
309 stream.writeText(","); | 282 stream.writeText(","); |
310 } | 283 } |
311 stream.writeText(" \n"); | 284 stream.writeText(" \n"); |
312 } | 285 } |
313 stream.writeText(" ]\n"); | 286 stream.writeText(" ]\n"); |
314 | 287 |
315 stream.writeText(" }"); | 288 stream.writeText(" }"); |
316 | 289 |
| 290 currentRecord = iter.next(); |
| 291 |
317 // JSON does not allow trailing commas | 292 // JSON does not allow trailing commas |
318 if (NULL != currentRecord->fNext) { | 293 if (NULL != currentRecord) { |
319 stream.writeText(","); | 294 stream.writeText(","); |
320 } | 295 } |
321 stream.writeText("\n"); | 296 stream.writeText("\n"); |
322 currentRecord = currentRecord->fNext; | |
323 } | 297 } |
324 stream.writeText(" ]\n"); | 298 stream.writeText(" ]\n"); |
325 if (useJSONP) { | 299 if (useJSONP) { |
326 stream.writeText("};\n"); | 300 stream.writeText("};\n"); |
327 } else { | 301 } else { |
328 stream.writeText("}\n"); | 302 stream.writeText("}\n"); |
329 } | 303 } |
330 } | 304 } |
331 | 305 |
332 void SkDiffContext::outputCsv(SkWStream& stream) { | 306 void SkDiffContext::outputCsv(SkWStream& stream) { |
333 SkTDict<int> columns(2); | 307 SkTDict<int> columns(2); |
334 int cntColumns = 0; | 308 int cntColumns = 0; |
335 | 309 |
336 stream.writeText("key"); | 310 stream.writeText("key"); |
337 | 311 |
338 DiffRecord* currentRecord = fRecords; | 312 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_
IterStart); |
| 313 DiffRecord* currentRecord = iter.get(); |
339 | 314 |
340 // Write CSV header and create a dictionary of all columns. | 315 // Write CSV header and create a dictionary of all columns. |
341 while (NULL != currentRecord) { | 316 while (NULL != currentRecord) { |
342 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI
ndex++) { | 317 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI
ndex++) { |
343 DiffData& data = currentRecord->fDiffs[diffIndex]; | 318 DiffData& data = currentRecord->fDiffs[diffIndex]; |
344 if (!columns.find(data.fDiffName)) { | 319 if (!columns.find(data.fDiffName)) { |
345 columns.set(data.fDiffName, cntColumns); | 320 columns.set(data.fDiffName, cntColumns); |
346 stream.writeText(", "); | 321 stream.writeText(", "); |
347 stream.writeText(data.fDiffName); | 322 stream.writeText(data.fDiffName); |
348 cntColumns++; | 323 cntColumns++; |
349 } | 324 } |
350 } | 325 } |
351 currentRecord = currentRecord->fNext; | 326 currentRecord = iter.next(); |
352 } | 327 } |
353 stream.writeText("\n"); | 328 stream.writeText("\n"); |
354 | 329 |
355 double values[100]; | 330 double values[100]; |
356 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so ma
ny diff types. | 331 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so ma
ny diff types. |
357 | 332 |
358 currentRecord = fRecords; | 333 SkTLList<DiffRecord>::Iter iter2(fRecords, SkTLList<DiffRecord>::Iter::kHead
_IterStart); |
| 334 currentRecord = iter2.get(); |
359 while (NULL != currentRecord) { | 335 while (NULL != currentRecord) { |
360 for (int i = 0; i < cntColumns; i++) { | 336 for (int i = 0; i < cntColumns; i++) { |
361 values[i] = -1; | 337 values[i] = -1; |
362 } | 338 } |
363 | 339 |
364 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI
ndex++) { | 340 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI
ndex++) { |
365 DiffData& data = currentRecord->fDiffs[diffIndex]; | 341 DiffData& data = currentRecord->fDiffs[diffIndex]; |
366 int index = -1; | 342 int index = -1; |
367 SkAssertResult(columns.find(data.fDiffName, &index)); | 343 SkAssertResult(columns.find(data.fDiffName, &index)); |
368 SkASSERT(index >= 0 && index < cntColumns); | 344 SkASSERT(index >= 0 && index < cntColumns); |
369 values[index] = data.fResult; | 345 values[index] = data.fResult.result; |
370 } | 346 } |
371 | 347 |
372 const char* filename = currentRecord->fBaselinePath.c_str() + | 348 const char* filename = currentRecord->fBaselinePath.c_str() + |
373 strlen(currentRecord->fBaselinePath.c_str()) - 1; | 349 strlen(currentRecord->fBaselinePath.c_str()) - 1; |
374 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1
) != '/') { | 350 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1
) != '/') { |
375 filename--; | 351 filename--; |
376 } | 352 } |
377 | 353 |
378 stream.writeText(filename); | 354 stream.writeText(filename); |
379 | 355 |
380 for (int i = 0; i < cntColumns; i++) { | 356 for (int i = 0; i < cntColumns; i++) { |
381 SkString str; | 357 SkString str; |
382 str.printf(", %f", values[i]); | 358 str.printf(", %f", values[i]); |
383 stream.writeText(str.c_str()); | 359 stream.writeText(str.c_str()); |
384 } | 360 } |
385 stream.writeText("\n"); | 361 stream.writeText("\n"); |
386 | 362 |
387 currentRecord = currentRecord->fNext; | 363 currentRecord = iter2.next(); |
388 } | 364 } |
389 } | 365 } |
OLD | NEW |