blob: 9669ae0ad39e79250f6007c30a3bf47707e10f37 [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
11#include "SkString.h"
12#include "SkTArray.h"
13#include "SkTDArray.h"
14
15class SkWStream;
16class SkImageDiffer;
17
18/**
19 * Collects records of diffs and outputs them as JSON.
20 */
21class SkDiffContext {
22public:
23 SkDiffContext();
24 ~SkDiffContext();
25
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000026 void setThreadCount(int threadCount) { fThreadCount = threadCount; }
27
zachr@google.com945708a2013-07-02 19:55:32 +000028 /**
djsollen@google.com513a7bf2013-11-07 19:24:06 +000029 * 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.com945708a2013-07-02 19:55:32 +000035 * 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.coma95959c2013-07-08 15:04:45 +000096 * @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.com945708a2013-07-02 19:55:32 +000098 */
zachr@google.coma95959c2013-07-08 15:04:45 +000099 void outputRecords(SkWStream& stream, bool useJSONP);
zachr@google.com945708a2013-07-02 19:55:32 +0000100
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000101 /**
102 * Output the records score in csv format.
103 */
104 void outputCsv(SkWStream& stream);
105
106
zachr@google.com945708a2013-07-02 19:55:32 +0000107private:
108 struct DiffData {
109 const char* fDiffName;
110 double fResult;
111 SkTDArray<SkIPoint> fPointsOfInterest;
112 };
113
114 struct DiffRecord {
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000115 SkString fCommonName;
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000116 SkString fDifferencePath;
zachr@google.com945708a2013-07-02 19:55:32 +0000117 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.comcbbf1ca2013-10-16 18:36:49 +0000130 int fThreadCount;
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000131
132 SkString fDifferenceDir;
zachr@google.com945708a2013-07-02 19:55:32 +0000133};
134
135#endif