blob: 2d971059c2f725dce47b7c064c188b27a269db0c [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#ifndef SkDiffContext_DEFINED
9#define SkDiffContext_DEFINED
10
djsollen@google.comefc51b72013-11-12 18:29:17 +000011#include "SkImageDiffer.h"
zachr@google.com945708a2013-07-02 19:55:32 +000012#include "SkString.h"
13#include "SkTArray.h"
14#include "SkTDArray.h"
djsollen@google.comefc51b72013-11-12 18:29:17 +000015#include "SkTLList.h"
16#include "SkThread.h"
zachr@google.com945708a2013-07-02 19:55:32 +000017
18class SkWStream;
zachr@google.com945708a2013-07-02 19:55:32 +000019
20/**
21 * Collects records of diffs and outputs them as JSON.
22 */
23class SkDiffContext {
24public:
25 SkDiffContext();
26 ~SkDiffContext();
27
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000028 void setThreadCount(int threadCount) { fThreadCount = threadCount; }
29
zachr@google.com945708a2013-07-02 19:55:32 +000030 /**
djsollen@google.com513a7bf2013-11-07 19:24:06 +000031 * Creates the directory if it does not exist and uses it to store differences
32 * between images.
33 */
34 void setDifferenceDir(const SkString& directory);
35
36 /**
zachr@google.com945708a2013-07-02 19:55:32 +000037 * Sets the differs to be used in each diff. Already started diffs will not retroactively use
38 * these.
39 * @param differs An array of differs to use. The array is copied, but not the differs
40 * themselves.
41 */
42 void setDiffers(const SkTDArray<SkImageDiffer*>& differs);
43
44 /**
45 * Compares two directories of images with the given differ
46 * @param baselinePath The baseline directory's path
47 * @param testPath The test directory's path
48 */
49 void diffDirectories(const char baselinePath[], const char testPath[]);
50
51 /**
52 * Compares two sets of images identified by glob style patterns with the given differ
53 * @param baselinePattern A pattern for baseline files
54 * @param testPattern A pattern for test files that matches each file of the baseline file
55 */
56 void diffPatterns(const char baselinePattern[], const char testPattern[]);
57
58 /**
59 * Compares the images at the given paths
60 * @param baselinePath The baseline file path
61 * @param testPath The matching test file path
62 */
63 void addDiff(const char* baselinePath, const char* testPath);
64
65 /**
66 * Output the records of each diff in JSON.
67 *
68 * The format of the JSON document is one top level array named "records".
djsollen@google.comefc51b72013-11-12 18:29:17 +000069 * Each record in the array is an object with the following values:
70 * "commonName" : string containing the common prefix of the baselinePath
71 * and testPath filenames
72 * "baselinePath" : string containing the path to the baseline image
73 * "testPath" : string containing the path to the test image
74 * "differencePath" : (optional) string containing the path to an alpha
75 * mask of the pixel difference between the baseline
76 * and test images
77 *
zachr@google.com945708a2013-07-02 19:55:32 +000078 * They also have an array named "diffs" with each element being one diff record for the two
79 * images indicated in the above field.
80 * A diff record includes:
81 * "differName" : string name of the diff metric used
82 * "result" : numerical result of the diff
zachr@google.com945708a2013-07-02 19:55:32 +000083 *
84 * Here is an example:
85 *
86 * {
87 * "records": [
88 * {
djsollen@google.comefc51b72013-11-12 18:29:17 +000089 * "commonName": "queue.png",
90 * "baselinePath": "/a/queue.png",
91 * "testPath": "/b/queue.png",
zachr@google.com945708a2013-07-02 19:55:32 +000092 * "diffs": [
93 * {
94 * "differName": "different_pixels",
95 * "result": 1,
zachr@google.com945708a2013-07-02 19:55:32 +000096 * }
97 * ]
98 * }
99 * ]
100 * }
101 *
zachr@google.coma95959c2013-07-08 15:04:45 +0000102 * @param stream The stream to output the diff to
103 * @param useJSONP True to adding padding to the JSON output to make it cross-site requestable.
zachr@google.com945708a2013-07-02 19:55:32 +0000104 */
zachr@google.coma95959c2013-07-08 15:04:45 +0000105 void outputRecords(SkWStream& stream, bool useJSONP);
zachr@google.com945708a2013-07-02 19:55:32 +0000106
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000107 /**
108 * Output the records score in csv format.
109 */
110 void outputCsv(SkWStream& stream);
111
112
zachr@google.com945708a2013-07-02 19:55:32 +0000113private:
114 struct DiffData {
115 const char* fDiffName;
djsollen@google.comefc51b72013-11-12 18:29:17 +0000116 SkImageDiffer::Result fResult;
zachr@google.com945708a2013-07-02 19:55:32 +0000117 };
118
119 struct DiffRecord {
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000120 SkString fCommonName;
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000121 SkString fDifferencePath;
zachr@google.com945708a2013-07-02 19:55:32 +0000122 SkString fBaselinePath;
123 SkString fTestPath;
124 SkTArray<DiffData> fDiffs;
zachr@google.com945708a2013-07-02 19:55:32 +0000125 };
126
djsollen@google.comefc51b72013-11-12 18:29:17 +0000127 // This is needed to work around a bug in the multithreaded case where the
128 // image decoders are crashing when large numbers of threads are invoking
129 // the decoder at the same time.
130 // see https://code.google.com/p/skia/issues/detail?id=1803
131 SkMutex fImageMutex;
132
133 // Used to protect access to fRecords and ensure only one thread is
134 // adding new entries at a time.
135 SkMutex fRecordMutex;
136
zachr@google.com945708a2013-07-02 19:55:32 +0000137 // We use linked list for the records so that their pointers remain stable. A resizable array
138 // might change its pointers, which would make it harder for async diffs to record their
139 // results.
djsollen@google.comefc51b72013-11-12 18:29:17 +0000140 SkTLList<DiffRecord> fRecords;
zachr@google.com945708a2013-07-02 19:55:32 +0000141
142 SkImageDiffer** fDiffers;
143 int fDifferCount;
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000144 int fThreadCount;
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000145
146 SkString fDifferenceDir;
zachr@google.com945708a2013-07-02 19:55:32 +0000147};
148
149#endif