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(); |
88 DiffRecord* newRecord = fRecords.addToHead(DiffRecord()); | |
mtklein
2013/11/08 15:06:13
If you're feeling precise, I think the release can
djsollen
2013/11/12 16:40:43
Done.
| |
96 newRecord->fBaselinePath = baselinePath; | 89 newRecord->fBaselinePath = baselinePath; |
97 newRecord->fTestPath = testPath; | 90 newRecord->fTestPath = testPath; |
98 newRecord->fNext = fRecords; | 91 fRecordMutex.release(); |
99 fRecords = newRecord; | |
100 | 92 |
101 // compute the common name | 93 // compute the common name |
102 SkString baseName = SkOSPath::SkBasename(baselinePath); | 94 SkString baseName = SkOSPath::SkBasename(baselinePath); |
103 SkString testName = SkOSPath::SkBasename(testPath); | 95 SkString testName = SkOSPath::SkBasename(testPath); |
104 newRecord->fCommonName = get_common_prefix(baseName, testName); | 96 newRecord->fCommonName = get_common_prefix(baseName, testName); |
105 | 97 |
106 bool alphaMaskPending = false; | 98 bool alphaMaskPending = false; |
107 bool alphaMaskCreated = false; | 99 bool alphaMaskCreated = false; |
108 | 100 |
109 // Perform each diff | 101 // Perform each diff |
110 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) { | 102 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) { |
111 SkImageDiffer* differ = fDiffers[differIndex]; | 103 SkImageDiffer* differ = fDiffers[differIndex]; |
112 // TODO only enable for one differ | 104 // only enable for one differ |
113 if (!alphaMaskCreated && !fDifferenceDir.isEmpty()) { | 105 if (!alphaMaskCreated && !fDifferenceDir.isEmpty()) { |
114 alphaMaskPending = differ->enablePOIAlphaMask(); | 106 alphaMaskPending = differ->enablePOIAlphaMask(); |
mtklein
2013/11/08 15:06:13
Can you eliminate enablePOIAlphaMask? Make it par
djsollen
2013/11/12 16:40:43
Done.
| |
115 } | 107 } |
116 int diffID = differ->queueDiff(&baselineBitmap, &testBitmap); | |
117 if (diffID >= 0) { | |
118 | 108 |
119 // Copy the results into data for this record | 109 // Copy the results into data for this record |
120 DiffData& diffData = newRecord->fDiffs.push_back(); | 110 DiffData& diffData = newRecord->fDiffs.push_back(); |
111 | |
112 if (differ->diff(&baselineBitmap, &testBitmap, &diffData.fResult)) { | |
121 | 113 |
122 diffData.fDiffName = differ->getName(); | 114 diffData.fDiffName = differ->getName(); |
123 diffData.fResult = differ->getResult(diffID); | |
124 | |
125 int poiCount = differ->getPointsOfInterestCount(diffID); | |
126 SkIPoint* poi = differ->getPointsOfInterest(diffID); | |
127 diffData.fPointsOfInterest.append(poiCount, poi); | |
128 | 115 |
129 if (alphaMaskPending | 116 if (alphaMaskPending |
130 && SkImageDiffer::RESULT_CORRECT != diffData.fResult | 117 && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result |
131 && newRecord->fDifferencePath.isEmpty()) { | 118 && !newRecord->fCommonName.isEmpty()) { |
132 newRecord->fDifferencePath = SkOSPath::SkPathJoin(fDifferenceDir .c_str(), | 119 newRecord->fDifferencePath = SkOSPath::SkPathJoin(fDifferenceDir .c_str(), |
133 newRecord->fCo mmonName.c_str()); | 120 newRecord->fCo mmonName.c_str()); |
134 | 121 |
135 // compute the image diff and output it | 122 // compute the image diff and output it |
136 SkBitmap* alphaMask = differ->getPointsOfInterestAlphaMask(diffI D); | 123 SkASSERT(!diffData.fResult.poiAlphaMask.empty()); |
137 SkBitmap copy; | 124 SkBitmap copy; |
138 alphaMask->copyTo(©, SkBitmap::kARGB_8888_Config); | 125 diffData.fResult.poiAlphaMask.copyTo(©, SkBitmap::kARGB_8888 _Config); |
139 SkImageEncoder::EncodeFile(newRecord->fDifferencePath.c_str(), c opy, | 126 SkImageEncoder::EncodeFile(newRecord->fDifferencePath.c_str(), c opy, |
140 SkImageEncoder::kPNG_Type, 100); | 127 SkImageEncoder::kPNG_Type, 100); |
128 | |
129 // cleanup the existing bitmap to free up resources; | |
130 diffData.fResult.poiAlphaMask.reset(); | |
141 } | 131 } |
142 | 132 |
143 if (alphaMaskPending) { | 133 if (alphaMaskPending) { |
144 alphaMaskPending = false; | 134 alphaMaskPending = false; |
145 alphaMaskCreated = true; | 135 alphaMaskCreated = true; |
146 } | 136 } |
147 | 137 } else { |
148 // Because we are doing everything synchronously for now, we are don e with the diff | 138 // if the diff failed the remove its entry from the list |
149 // after reading it. | 139 newRecord->fDiffs.pop_back(); |
150 differ->deleteDiff(diffID); | |
151 } | 140 } |
152 } | 141 } |
153 | |
154 // if we get a difference and we want the alpha mask then compute it here. | |
155 } | 142 } |
156 | 143 |
157 class SkThreadedDiff : public SkRunnable { | 144 class SkThreadedDiff : public SkRunnable { |
158 public: | 145 public: |
159 SkThreadedDiff() : fDiffContext(NULL) { } | 146 SkThreadedDiff() : fDiffContext(NULL) { } |
160 | 147 |
161 void setup(SkDiffContext* diffContext, const SkString& baselinePath, const S kString& testPath) { | 148 void setup(SkDiffContext* diffContext, const SkString& baselinePath, const S kString& testPath) { |
162 fDiffContext = diffContext; | 149 fDiffContext = diffContext; |
163 fBaselinePath = baselinePath; | 150 fBaselinePath = baselinePath; |
164 fTestPath = testPath; | 151 fTestPath = testPath; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 | 221 |
235 for (int x = 0; x < baselineEntries.count(); x++) { | 222 for (int x = 0; x < baselineEntries.count(); x++) { |
236 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); | 223 runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); |
237 threadPool.add(&runnableDiffs[x]); | 224 threadPool.add(&runnableDiffs[x]); |
238 } | 225 } |
239 | 226 |
240 threadPool.wait(); | 227 threadPool.wait(); |
241 } | 228 } |
242 | 229 |
243 void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { | 230 void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { |
244 DiffRecord* currentRecord = fRecords; | 231 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_ IterStart); |
232 DiffRecord* currentRecord = iter.get(); | |
233 | |
245 if (useJSONP) { | 234 if (useJSONP) { |
246 stream.writeText("var SkPDiffRecords = {\n"); | 235 stream.writeText("var SkPDiffRecords = {\n"); |
247 } else { | 236 } else { |
248 stream.writeText("{\n"); | 237 stream.writeText("{\n"); |
249 } | 238 } |
250 stream.writeText(" \"records\": [\n"); | 239 stream.writeText(" \"records\": [\n"); |
251 while (NULL != currentRecord) { | 240 while (NULL != currentRecord) { |
252 stream.writeText(" {\n"); | 241 stream.writeText(" {\n"); |
253 | 242 |
254 SkString differenceAbsPath = get_absolute_path(currentRecord->fDiffe rencePath); | 243 SkString differenceAbsPath = get_absolute_path(currentRecord->fDiffe rencePath); |
(...skipping 19 matching lines...) Expand all Loading... | |
274 stream.writeText(" \"diffs\": [\n"); | 263 stream.writeText(" \"diffs\": [\n"); |
275 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d iffIndex++) { | 264 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d iffIndex++) { |
276 DiffData& data = currentRecord->fDiffs[diffIndex]; | 265 DiffData& data = currentRecord->fDiffs[diffIndex]; |
277 stream.writeText(" {\n"); | 266 stream.writeText(" {\n"); |
278 | 267 |
279 stream.writeText(" \"differName\": \""); | 268 stream.writeText(" \"differName\": \""); |
280 stream.writeText(data.fDiffName); | 269 stream.writeText(data.fDiffName); |
281 stream.writeText("\",\n"); | 270 stream.writeText("\",\n"); |
282 | 271 |
283 stream.writeText(" \"result\": "); | 272 stream.writeText(" \"result\": "); |
284 stream.writeScalarAsText((SkScalar)data.fResult); | 273 stream.writeScalarAsText((SkScalar)data.fResult.result); |
285 stream.writeText(",\n"); | 274 stream.writeText(",\n"); |
286 | 275 |
287 stream.writeText(" \"pointsOfInterest\": [\n"); | 276 stream.writeText(" \"pointsOfInterest\": "); |
288 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.cou nt() && | 277 stream.writeDecAsText(data.fResult.poiCount); |
289 poiIndex < kMaxPOI; poiIndex++) { | 278 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 | 279 |
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(" }"); | 280 stream.writeText(" }"); |
306 | 281 |
307 // JSON does not allow trailing commas | 282 // JSON does not allow trailing commas |
308 if (diffIndex + 1 < currentRecord->fDiffs.count()) { | 283 if (diffIndex + 1 < currentRecord->fDiffs.count()) { |
309 stream.writeText(","); | 284 stream.writeText(","); |
310 } | 285 } |
311 stream.writeText(" \n"); | 286 stream.writeText(" \n"); |
312 } | 287 } |
313 stream.writeText(" ]\n"); | 288 stream.writeText(" ]\n"); |
314 | 289 |
315 stream.writeText(" }"); | 290 stream.writeText(" }"); |
316 | 291 |
292 currentRecord = iter.next(); | |
293 | |
317 // JSON does not allow trailing commas | 294 // JSON does not allow trailing commas |
318 if (NULL != currentRecord->fNext) { | 295 if (NULL != currentRecord) { |
319 stream.writeText(","); | 296 stream.writeText(","); |
320 } | 297 } |
321 stream.writeText("\n"); | 298 stream.writeText("\n"); |
322 currentRecord = currentRecord->fNext; | |
323 } | 299 } |
324 stream.writeText(" ]\n"); | 300 stream.writeText(" ]\n"); |
325 if (useJSONP) { | 301 if (useJSONP) { |
326 stream.writeText("};\n"); | 302 stream.writeText("};\n"); |
327 } else { | 303 } else { |
328 stream.writeText("}\n"); | 304 stream.writeText("}\n"); |
329 } | 305 } |
330 } | 306 } |
331 | 307 |
332 void SkDiffContext::outputCsv(SkWStream& stream) { | 308 void SkDiffContext::outputCsv(SkWStream& stream) { |
333 SkTDict<int> columns(2); | 309 SkTDict<int> columns(2); |
334 int cntColumns = 0; | 310 int cntColumns = 0; |
335 | 311 |
336 stream.writeText("key"); | 312 stream.writeText("key"); |
337 | 313 |
338 DiffRecord* currentRecord = fRecords; | 314 SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_ IterStart); |
315 DiffRecord* currentRecord = iter.get(); | |
339 | 316 |
340 // Write CSV header and create a dictionary of all columns. | 317 // Write CSV header and create a dictionary of all columns. |
341 while (NULL != currentRecord) { | 318 while (NULL != currentRecord) { |
342 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI ndex++) { | 319 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI ndex++) { |
343 DiffData& data = currentRecord->fDiffs[diffIndex]; | 320 DiffData& data = currentRecord->fDiffs[diffIndex]; |
344 if (!columns.find(data.fDiffName)) { | 321 if (!columns.find(data.fDiffName)) { |
345 columns.set(data.fDiffName, cntColumns); | 322 columns.set(data.fDiffName, cntColumns); |
346 stream.writeText(", "); | 323 stream.writeText(", "); |
347 stream.writeText(data.fDiffName); | 324 stream.writeText(data.fDiffName); |
348 cntColumns++; | 325 cntColumns++; |
349 } | 326 } |
350 } | 327 } |
351 currentRecord = currentRecord->fNext; | 328 currentRecord = iter.next(); |
352 } | 329 } |
353 stream.writeText("\n"); | 330 stream.writeText("\n"); |
354 | 331 |
355 double values[100]; | 332 double values[100]; |
356 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so ma ny diff types. | 333 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so ma ny diff types. |
357 | 334 |
358 currentRecord = fRecords; | 335 SkTLList<DiffRecord>::Iter iter2(fRecords, SkTLList<DiffRecord>::Iter::kHead _IterStart); |
336 currentRecord = iter2.get(); | |
359 while (NULL != currentRecord) { | 337 while (NULL != currentRecord) { |
360 for (int i = 0; i < cntColumns; i++) { | 338 for (int i = 0; i < cntColumns; i++) { |
361 values[i] = -1; | 339 values[i] = -1; |
362 } | 340 } |
363 | 341 |
364 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI ndex++) { | 342 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI ndex++) { |
365 DiffData& data = currentRecord->fDiffs[diffIndex]; | 343 DiffData& data = currentRecord->fDiffs[diffIndex]; |
366 int index = -1; | 344 int index = -1; |
367 SkAssertResult(columns.find(data.fDiffName, &index)); | 345 SkAssertResult(columns.find(data.fDiffName, &index)); |
368 SkASSERT(index >= 0 && index < cntColumns); | 346 SkASSERT(index >= 0 && index < cntColumns); |
369 values[index] = data.fResult; | 347 values[index] = data.fResult.result; |
370 } | 348 } |
371 | 349 |
372 const char* filename = currentRecord->fBaselinePath.c_str() + | 350 const char* filename = currentRecord->fBaselinePath.c_str() + |
373 strlen(currentRecord->fBaselinePath.c_str()) - 1; | 351 strlen(currentRecord->fBaselinePath.c_str()) - 1; |
374 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1 ) != '/') { | 352 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1 ) != '/') { |
375 filename--; | 353 filename--; |
376 } | 354 } |
377 | 355 |
378 stream.writeText(filename); | 356 stream.writeText(filename); |
379 | 357 |
380 for (int i = 0; i < cntColumns; i++) { | 358 for (int i = 0; i < cntColumns; i++) { |
381 SkString str; | 359 SkString str; |
382 str.printf(", %f", values[i]); | 360 str.printf(", %f", values[i]); |
383 stream.writeText(str.c_str()); | 361 stream.writeText(str.c_str()); |
384 } | 362 } |
385 stream.writeText("\n"); | 363 stream.writeText("\n"); |
386 | 364 |
387 currentRecord = currentRecord->fNext; | 365 currentRecord = iter2.next(); |
388 } | 366 } |
389 } | 367 } |
OLD | NEW |