zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 1 | /* |
| 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 | #ifndef SkDiffContext_DEFINED |
| 9 | #define SkDiffContext_DEFINED |
| 10 | |
| 11 | #include "SkString.h" |
| 12 | #include "SkTArray.h" |
| 13 | #include "SkTDArray.h" |
| 14 | |
| 15 | class SkWStream; |
| 16 | class SkImageDiffer; |
| 17 | |
| 18 | /** |
| 19 | * Collects records of diffs and outputs them as JSON. |
| 20 | */ |
| 21 | class SkDiffContext { |
| 22 | public: |
| 23 | SkDiffContext(); |
| 24 | ~SkDiffContext(); |
| 25 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 26 | void setThreadCount(int threadCount) { fThreadCount = threadCount; } |
| 27 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 28 | /** |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame^] | 29 | * Creates the directory if it does not exist and uses it to store differences |
| 30 | * between images. |
| 31 | */ |
| 32 | void setDifferenceDir(const SkString& directory); |
| 33 | |
| 34 | /** |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 35 | * Sets the differs to be used in each diff. Already started diffs will not retroactively use |
| 36 | * these. |
| 37 | * @param differs An array of differs to use. The array is copied, but not the differs |
| 38 | * themselves. |
| 39 | */ |
| 40 | void setDiffers(const SkTDArray<SkImageDiffer*>& differs); |
| 41 | |
| 42 | /** |
| 43 | * Compares two directories of images with the given differ |
| 44 | * @param baselinePath The baseline directory's path |
| 45 | * @param testPath The test directory's path |
| 46 | */ |
| 47 | void diffDirectories(const char baselinePath[], const char testPath[]); |
| 48 | |
| 49 | /** |
| 50 | * Compares two sets of images identified by glob style patterns with the given differ |
| 51 | * @param baselinePattern A pattern for baseline files |
| 52 | * @param testPattern A pattern for test files that matches each file of the baseline file |
| 53 | */ |
| 54 | void diffPatterns(const char baselinePattern[], const char testPattern[]); |
| 55 | |
| 56 | /** |
| 57 | * Compares the images at the given paths |
| 58 | * @param baselinePath The baseline file path |
| 59 | * @param testPath The matching test file path |
| 60 | */ |
| 61 | void addDiff(const char* baselinePath, const char* testPath); |
| 62 | |
| 63 | /** |
| 64 | * Output the records of each diff in JSON. |
| 65 | * |
| 66 | * The format of the JSON document is one top level array named "records". |
| 67 | * Each record in the array is an object with both a "baselinePath" and "testPath" string field. |
| 68 | * They also have an array named "diffs" with each element being one diff record for the two |
| 69 | * images indicated in the above field. |
| 70 | * A diff record includes: |
| 71 | * "differName" : string name of the diff metric used |
| 72 | * "result" : numerical result of the diff |
| 73 | * "pointsOfInterest" : an array of coordinates (stored as a 2-array of ints) of interesting |
| 74 | * points |
| 75 | * |
| 76 | * Here is an example: |
| 77 | * |
| 78 | * { |
| 79 | * "records": [ |
| 80 | * { |
| 81 | * "baselinePath": "queue.png", |
| 82 | * "testPath": "queue.png", |
| 83 | * "diffs": [ |
| 84 | * { |
| 85 | * "differName": "different_pixels", |
| 86 | * "result": 1, |
| 87 | * "pointsOfInterest": [ |
| 88 | * [285,279], |
| 89 | * ] |
| 90 | * } |
| 91 | * ] |
| 92 | * } |
| 93 | * ] |
| 94 | * } |
| 95 | * |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 96 | * @param stream The stream to output the diff to |
| 97 | * @param useJSONP True to adding padding to the JSON output to make it cross-site requestable. |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 98 | */ |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 99 | void outputRecords(SkWStream& stream, bool useJSONP); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 100 | |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 101 | /** |
| 102 | * Output the records score in csv format. |
| 103 | */ |
| 104 | void outputCsv(SkWStream& stream); |
| 105 | |
| 106 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 107 | private: |
| 108 | struct DiffData { |
| 109 | const char* fDiffName; |
| 110 | double fResult; |
| 111 | SkTDArray<SkIPoint> fPointsOfInterest; |
| 112 | }; |
| 113 | |
| 114 | struct DiffRecord { |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame^] | 115 | SkString fCommonName; |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 116 | SkString fDifferencePath; |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 117 | SkString fBaselinePath; |
| 118 | SkString fTestPath; |
| 119 | SkTArray<DiffData> fDiffs; |
| 120 | DiffRecord* fNext; |
| 121 | }; |
| 122 | |
| 123 | // We use linked list for the records so that their pointers remain stable. A resizable array |
| 124 | // might change its pointers, which would make it harder for async diffs to record their |
| 125 | // results. |
| 126 | DiffRecord * fRecords; |
| 127 | |
| 128 | SkImageDiffer** fDiffers; |
| 129 | int fDifferCount; |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 130 | int fThreadCount; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame^] | 131 | |
| 132 | SkString fDifferenceDir; |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif |