Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: gm/gm_expectations.h

Issue 14284018: GM: specify that currently used checksums are CityHashes of SkBitmaps (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: move_some_of_gm_expectations_h_into_cpp_file Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | gm/gm_expectations.cpp » ('j') | gm/gm_expectations.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifndef gm_expectations_DEFINED 7 #ifndef gm_expectations_DEFINED
8 #define gm_expectations_DEFINED 8 #define gm_expectations_DEFINED
9 9
10 #include <stdarg.h>
11 #include "gm.h" 10 #include "gm.h"
12 #include "SkBitmap.h" 11 #include "SkBitmap.h"
13 #include "SkBitmapHasher.h"
14 #include "SkData.h" 12 #include "SkData.h"
15 #include "SkImageDecoder.h"
16 #include "SkOSFile.h" 13 #include "SkOSFile.h"
17 #include "SkRefCnt.h" 14 #include "SkRefCnt.h"
18 #include "SkStream.h" 15 #include "SkStream.h"
19 #include "SkTArray.h" 16 #include "SkTArray.h"
20 17
21 #ifdef SK_BUILD_FOR_WIN 18 #ifdef SK_BUILD_FOR_WIN
22 // json includes xlocale which generates warning 4530 because we're compilin g without 19 // json includes xlocale which generates warning 4530 because we're compilin g without
23 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067 20 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
24 #pragma warning(push) 21 #pragma warning(push)
25 #pragma warning(disable : 4530) 22 #pragma warning(disable : 4530)
26 #endif 23 #endif
27 #include "json/reader.h" 24 #include "json/reader.h"
28 #include "json/value.h" 25 #include "json/value.h"
29 #ifdef SK_BUILD_FOR_WIN 26 #ifdef SK_BUILD_FOR_WIN
30 #pragma warning(pop) 27 #pragma warning(pop)
31 #endif 28 #endif
32 29
33 #define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") 30 // EPOGER: move these into a .cpp file so we know nobody else uses them?
34
35 const static char kJsonKey_ActualResults[] = "actual-results"; 31 const static char kJsonKey_ActualResults[] = "actual-results";
36 const static char kJsonKey_ActualResults_Failed[] = "failed"; 32 const static char kJsonKey_ActualResults_Failed[] = "failed";
37 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; 33 const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored";
38 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; 34 const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison";
39 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; 35 const static char kJsonKey_ActualResults_Succeeded[] = "succeeded";
40 const static char kJsonKey_ActualResults_AnyStatus_Checksum[] = "checksum"; 36 const static char kJsonKey_ActualResults_AnyStatus_BitmapCityhash[] = "bitmap-c ityhash";
41 37
42 const static char kJsonKey_ExpectedResults[] = "expected-results"; 38 const static char kJsonKey_ExpectedResults[] = "expected-results";
43 const static char kJsonKey_ExpectedResults_Checksums[] = "checksums"; 39 const static char kJsonKey_ExpectedResults_AllowedBitmapCityhashes[] = "allowed- bitmap-cityhashes";
44 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-failure"; 40 const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-f ailure";
45 41
46 namespace skiagm { 42 namespace skiagm {
47 43
48 // The actual type we use to represent a checksum is hidden in here. 44 // The actual type we use to represent a checksum is hidden in here.
49 typedef Json::UInt64 Checksum; 45 typedef Json::UInt64 Checksum;
50 static inline Json::Value asJsonValue(Checksum checksum) { 46 static inline Json::Value asJsonValue(Checksum checksum) {
51 return checksum; 47 return checksum;
52 } 48 }
53 static inline Checksum asChecksum(Json::Value jsonValue) { 49 static inline Checksum asChecksum(Json::Value jsonValue) {
54 return jsonValue.asUInt64(); 50 return jsonValue.asUInt64();
(...skipping 21 matching lines...) Expand all
76 } 72 }
77 73
78 /** 74 /**
79 * Test expectations (allowed image checksums, etc.) 75 * Test expectations (allowed image checksums, etc.)
80 */ 76 */
81 class Expectations { 77 class Expectations {
82 public: 78 public:
83 /** 79 /**
84 * No expectations at all. 80 * No expectations at all.
85 */ 81 */
86 Expectations(bool ignoreFailure=kDefaultIgnoreFailure) { 82 Expectations(bool ignoreFailure=kDefaultIgnoreFailure);
87 fIgnoreFailure = ignoreFailure;
88 }
89 83
90 /** 84 /**
91 * Expect exactly one image (appropriate for the case when we 85 * Expect exactly one image (appropriate for the case when we
92 * are comparing against a single PNG file). 86 * are comparing against a single PNG file).
93 */ 87 */
94 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFa ilure) { 88 Expectations(const SkBitmap& bitmap, bool ignoreFailure=kDefaultIgnoreFa ilure);
95 fBitmap = bitmap;
96 fIgnoreFailure = ignoreFailure;
97 SkHashDigest digest;
98 // TODO(epoger): Better handling for error returned by ComputeDigest ()?
99 // For now, we just report a digest of 0 in error cases, like before .
100 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) {
101 digest = 0;
102 }
103 fAllowedChecksums.push_back() = digest;
104 }
105 89
106 /** 90 /**
107 * Create Expectations from a JSON element as found within the 91 * Create Expectations from a JSON element as found within the
108 * kJsonKey_ExpectedResults section. 92 * kJsonKey_ExpectedResults section.
109 * 93 *
110 * It's fine if the jsonElement is null or empty; in that case, we just 94 * It's fine if the jsonElement is null or empty; in that case, we just
111 * don't have any expectations. 95 * don't have any expectations.
112 */ 96 */
113 Expectations(Json::Value jsonElement) { 97 Expectations(Json::Value jsonElement);
114 if (jsonElement.empty()) {
115 fIgnoreFailure = kDefaultIgnoreFailure;
116 } else {
117 Json::Value ignoreFailure = jsonElement[kJsonKey_ExpectedResults _IgnoreFailure];
118 if (ignoreFailure.isNull()) {
119 fIgnoreFailure = kDefaultIgnoreFailure;
120 } else if (!ignoreFailure.isBool()) {
121 gm_fprintf(stderr, "found non-boolean json value"
122 " for key '%s' in element '%s'\n",
123 kJsonKey_ExpectedResults_IgnoreFailure,
124 jsonElement.toStyledString().c_str());
125 DEBUGFAIL_SEE_STDERR;
126 fIgnoreFailure = kDefaultIgnoreFailure;
127 } else {
128 fIgnoreFailure = ignoreFailure.asBool();
129 }
130
131 Json::Value allowedChecksums = jsonElement[kJsonKey_ExpectedResu lts_Checksums];
132 if (allowedChecksums.isNull()) {
133 // ok, we'll just assume there aren't any expected checksums to compare against
134 } else if (!allowedChecksums.isArray()) {
135 gm_fprintf(stderr, "found non-array json value"
136 " for key '%s' in element '%s'\n",
137 kJsonKey_ExpectedResults_Checksums,
138 jsonElement.toStyledString().c_str());
139 DEBUGFAIL_SEE_STDERR;
140 } else {
141 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) {
142 Json::Value checksumElement = allowedChecksums[i];
143 if (!checksumElement.isIntegral()) {
144 gm_fprintf(stderr, "found non-integer checksum"
145 " in json element '%s'\n",
146 jsonElement.toStyledString().c_str());
147 DEBUGFAIL_SEE_STDERR;
148 } else {
149 fAllowedChecksums.push_back() = asChecksum(checksumE lement);
150 }
151 }
152 }
153 }
154 }
155 98
156 /** 99 /**
157 * Returns true iff we want to ignore failed expectations. 100 * Returns true iff we want to ignore failed expectations.
158 */ 101 */
159 bool ignoreFailure() const { return this->fIgnoreFailure; } 102 bool ignoreFailure() const { return this->fIgnoreFailure; }
160 103
161 /** 104 /**
162 * Returns true iff there are no allowed checksums. 105 * Returns true iff there are no allowed checksums.
163 */ 106 */
164 bool empty() const { return this->fAllowedChecksums.empty(); } 107 bool empty() const { return this->fAllowedBitmapCityhashes.empty(); }
165 108
166 /** 109 /**
167 * Returns true iff actualChecksum matches any allowedChecksum, 110 * Returns true iff actualChecksum matches any allowedChecksum,
168 * regardless of fIgnoreFailure. (The caller can check 111 * regardless of fIgnoreFailure. (The caller can check
169 * that separately.) 112 * that separately.)
170 */ 113 */
171 bool match(Checksum actualChecksum) const { 114 bool match(Checksum actualChecksum) const;
172 for (int i=0; i < this->fAllowedChecksums.count(); i++) {
173 Checksum allowedChecksum = this->fAllowedChecksums[i];
174 if (allowedChecksum == actualChecksum) {
175 return true;
176 }
177 }
178 return false;
179 }
180 115
181 /** 116 /**
182 * If this Expectation is based on a single SkBitmap, return a 117 * If this Expectation is based on a single SkBitmap, return a
183 * pointer to that SkBitmap. Otherwise (if the Expectation is 118 * pointer to that SkBitmap. Otherwise (if the Expectation is
184 * empty, or if it was based on a list of checksums rather 119 * empty, or if it was based on a list of checksums rather
185 * than a single bitmap), returns NULL. 120 * than a single bitmap), returns NULL.
186 */ 121 */
187 const SkBitmap *asBitmap() const { 122 const SkBitmap *asBitmap() const {
188 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap; 123 return (SkBitmap::kNo_Config == fBitmap.config()) ? NULL : &fBitmap;
189 } 124 }
190 125
191 /** 126 /**
192 * Return a JSON representation of the allowed checksums. 127 * Return a JSON representation of the allowed checksums.
193 * This does NOT include any information about whether to 128 * This does NOT include any information about whether to
194 * ignore failures. 129 * ignore failures.
195 */ 130 */
196 Json::Value allowedChecksumsAsJson() const { 131 Json::Value allowedChecksumsAsJson() const;
197 Json::Value allowedChecksumArray;
198 if (!this->fAllowedChecksums.empty()) {
199 for (int i=0; i < this->fAllowedChecksums.count(); i++) {
200 Checksum allowedChecksum = this->fAllowedChecksums[i];
201 allowedChecksumArray.append(asJsonValue(allowedChecksum));
202 }
203 }
204 return allowedChecksumArray;
205 }
206 132
207 private: 133 private:
208 const static bool kDefaultIgnoreFailure = false; 134 const static bool kDefaultIgnoreFailure = false;
209 135
210 SkTArray<Checksum> fAllowedChecksums; 136 SkTArray<Checksum> fAllowedBitmapCityhashes;
211 bool fIgnoreFailure; 137 bool fIgnoreFailure;
212 SkBitmap fBitmap; 138 SkBitmap fBitmap;
213 }; 139 };
214 140
215 /** 141 /**
216 * Abstract source of Expectations objects for individual tests. 142 * Abstract source of Expectations objects for individual tests.
217 */ 143 */
218 class ExpectationsSource : public SkRefCnt { 144 class ExpectationsSource : public SkRefCnt {
219 public: 145 public:
220 virtual Expectations get(const char *testName) = 0; 146 virtual Expectations get(const char *testName) = 0;
221 }; 147 };
222 148
223 /** 149 /**
224 * Return Expectations based on individual image files on disk. 150 * Return Expectations based on individual image files on disk.
225 */ 151 */
226 class IndividualImageExpectationsSource : public ExpectationsSource { 152 class IndividualImageExpectationsSource : public ExpectationsSource {
227 public: 153 public:
228 /** 154 /**
229 * Create an ExpectationsSource that will return Expectations based on 155 * Create an ExpectationsSource that will return Expectations based on
230 * image files found within rootDir. 156 * image files found within rootDir.
231 * 157 *
232 * rootDir: directory under which to look for image files 158 * rootDir: directory under which to look for image files
233 * (this string will be copied to storage within this object) 159 * (this string will be copied to storage within this object)
234 */ 160 */
235 IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDi r) {} 161 IndividualImageExpectationsSource(const char *rootDir) : fRootDir(rootDi r) {}
236 162
237 Expectations get(const char *testName) SK_OVERRIDE { 163 Expectations get(const char *testName) SK_OVERRIDE ;
238 SkString path = make_filename(fRootDir.c_str(), "", testName,
239 "png");
240 SkBitmap referenceBitmap;
241 bool decodedReferenceBitmap =
242 SkImageDecoder::DecodeFile(path.c_str(), &referenceBitmap,
243 SkBitmap::kARGB_8888_Config,
244 SkImageDecoder::kDecodePixels_Mode,
245 NULL);
246 if (decodedReferenceBitmap) {
247 return Expectations(referenceBitmap);
248 } else {
249 return Expectations();
250 }
251 }
252 164
253 private: 165 private:
254 const SkString fRootDir; 166 const SkString fRootDir;
255 }; 167 };
256 168
257 /** 169 /**
258 * Return Expectations based on JSON summary file. 170 * Return Expectations based on JSON summary file.
259 */ 171 */
260 class JsonExpectationsSource : public ExpectationsSource { 172 class JsonExpectationsSource : public ExpectationsSource {
261 public: 173 public:
262 /** 174 /**
263 * Create an ExpectationsSource that will return Expectations based on 175 * Create an ExpectationsSource that will return Expectations based on
264 * a JSON file. 176 * a JSON file.
265 * 177 *
266 * jsonPath: path to JSON file to read 178 * jsonPath: path to JSON file to read
267 */ 179 */
268 JsonExpectationsSource(const char *jsonPath) { 180 JsonExpectationsSource(const char *jsonPath);
269 parse(jsonPath, &fJsonRoot);
270 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults];
271 }
272 181
273 Expectations get(const char *testName) SK_OVERRIDE { 182 Expectations get(const char *testName) SK_OVERRIDE;
274 return Expectations(fJsonExpectedResults[testName]);
275 }
276 183
277 private: 184 private:
278 185
279 /** 186 /**
280 * Read as many bytes as possible (up to maxBytes) from the stream into 187 * Read as many bytes as possible (up to maxBytes) from the stream into
281 * an SkData object. 188 * an SkData object.
282 * 189 *
283 * If the returned SkData contains fewer than maxBytes, then EOF has bee n 190 * If the returned SkData contains fewer than maxBytes, then EOF has bee n
284 * reached and no more data would be available from subsequent calls. 191 * reached and no more data would be available from subsequent calls.
285 * (If EOF has already been reached, then this call will return an empty 192 * (If EOF has already been reached, then this call will return an empty
(...skipping 14 matching lines...) Expand all
300 * size_t bytesActuallyRead = dataRef.get()->size(); 207 * size_t bytesActuallyRead = dataRef.get()->size();
301 * // use the data... 208 * // use the data...
302 * } 209 * }
303 * } 210 * }
304 * // underlying buffer has been freed, thanks to auto unref 211 * // underlying buffer has been freed, thanks to auto unref
305 * 212 *
306 */ 213 */
307 // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in 214 // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in
308 // https://codereview.appspot.com/7300071 ? 215 // https://codereview.appspot.com/7300071 ?
309 // And maybe readFileIntoSkData() also? 216 // And maybe readFileIntoSkData() also?
310 static SkData* readIntoSkData(SkStream &stream, size_t maxBytes) { 217 static SkData* readIntoSkData(SkStream &stream, size_t maxBytes);
311 if (0 == maxBytes) {
312 return SkData::NewEmpty();
313 }
314 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes)) ;
315 char* bufPtr = bufStart;
316 size_t bytesRemaining = maxBytes;
317 while (bytesRemaining > 0) {
318 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining);
319 if (0 == bytesReadThisTime) {
320 break;
321 }
322 bytesRemaining -= bytesReadThisTime;
323 bufPtr += bytesReadThisTime;
324 }
325 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining);
326 }
327 218
328 /** 219 /**
329 * Wrapper around readIntoSkData for files: reads the entire file into 220 * Wrapper around readIntoSkData for files: reads the entire file into
330 * an SkData object. 221 * an SkData object.
331 */ 222 */
332 static SkData* readFileIntoSkData(SkFILEStream &stream) { 223 static SkData* readFileIntoSkData(SkFILEStream &stream) {
333 return readIntoSkData(stream, stream.getLength()); 224 return readIntoSkData(stream, stream.getLength());
334 } 225 }
335 226
336 /** 227 /**
337 * Read the file contents from jsonPath and parse them into jsonRoot. 228 * Read the file contents from jsonPath and parse them into jsonRoot.
338 * 229 *
339 * Returns true if successful. 230 * Returns true if successful.
340 */ 231 */
341 static bool parse(const char *jsonPath, Json::Value *jsonRoot) { 232 static bool parse(const char *jsonPath, Json::Value *jsonRoot);
342 SkFILEStream inFile(jsonPath);
343 if (!inFile.isValid()) {
344 gm_fprintf(stderr, "unable to read JSON file %s\n", jsonPath);
345 DEBUGFAIL_SEE_STDERR;
346 return false;
347 }
348
349 SkAutoDataUnref dataRef(readFileIntoSkData(inFile));
350 if (NULL == dataRef.get()) {
351 gm_fprintf(stderr, "error reading JSON file %s\n", jsonPath);
352 DEBUGFAIL_SEE_STDERR;
353 return false;
354 }
355
356 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->da ta());
357 size_t size = dataRef.get()->size();
358 Json::Reader reader;
359 if (!reader.parse(bytes, bytes+size, *jsonRoot)) {
360 gm_fprintf(stderr, "error parsing JSON file %s\n", jsonPath);
361 DEBUGFAIL_SEE_STDERR;
362 return false;
363 }
364 return true;
365 }
366 233
367 Json::Value fJsonRoot; 234 Json::Value fJsonRoot;
368 Json::Value fJsonExpectedResults; 235 Json::Value fJsonExpectedResults;
369 }; 236 };
370 237
371 } 238 }
372 #endif 239 #endif
OLDNEW
« no previous file with comments | « no previous file | gm/gm_expectations.cpp » ('j') | gm/gm_expectations.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698