blob: 71311d5e116d6fa7c923352b9e3f3576f5fb0124 [file] [log] [blame]
epoger@google.com76c913d2013-04-26 15:06:44 +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#include "gm_expectations.h"
9#include "SkBitmapHasher.h"
10#include "SkImageDecoder.h"
11
12#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
13
epoger@google.com26515ba2013-05-14 18:58:12 +000014// These constants must be kept in sync with the JSONKEY_ constants in
epoger@google.coma55e48d2013-05-21 16:06:40 +000015// display_json_results.py !
epoger@google.com76c913d2013-04-26 15:06:44 +000016const static char kJsonKey_ActualResults[] = "actual-results";
17const static char kJsonKey_ActualResults_Failed[] = "failed";
18const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored";
19const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison";
20const static char kJsonKey_ActualResults_Succeeded[] = "succeeded";
epoger@google.com366a7702013-05-07 15:51:54 +000021const static char kJsonKey_ActualResults_AnyStatus_BitmapHash[] = "bitmap-64bitMD5";
epoger@google.com76c913d2013-04-26 15:06:44 +000022
23const static char kJsonKey_ExpectedResults[] = "expected-results";
epoger@google.com366a7702013-05-07 15:51:54 +000024const static char kJsonKey_ExpectedResults_AllowedBitmapHashes[] = "allowed-bitmap-64bitMD5s";
epoger@google.com76c913d2013-04-26 15:06:44 +000025const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-failure";
26
27namespace skiagm {
28
scroggo@google.com6843bdb2013-05-08 19:14:23 +000029 void gm_fprintf(FILE *stream, const char format[], ...) {
30 va_list args;
31 va_start(args, format);
32 fprintf(stream, "GM: ");
33 vfprintf(stream, format, args);
34 va_end(args);
35 }
36
epoger@google.comce057fe2013-05-14 15:17:46 +000037 SkString SkPathJoin(const char *rootPath, const char *relativePath) {
38 SkString result(rootPath);
39 if (!result.endsWith(SkPATH_SEPARATOR)) {
40 result.appendUnichar(SkPATH_SEPARATOR);
41 }
42 result.append(relativePath);
43 return result;
44 }
45
epoger@google.com76c913d2013-04-26 15:06:44 +000046 // TODO(epoger): This currently assumes that the result SkHashDigest was
epoger@google.com366a7702013-05-07 15:51:54 +000047 // generated as an SkHashDigest of an SkBitmap. We'll need to allow for other
48 // hash types to cover non-bitmaps.
epoger@google.com76c913d2013-04-26 15:06:44 +000049 Json::Value ActualResultAsJsonValue(const SkHashDigest& result) {
50 Json::Value jsonValue;
epoger@google.com366a7702013-05-07 15:51:54 +000051 jsonValue[kJsonKey_ActualResults_AnyStatus_BitmapHash] = asJsonValue(result);
epoger@google.com76c913d2013-04-26 15:06:44 +000052 return jsonValue;
53 }
54
55 Json::Value CreateJsonTree(Json::Value expectedResults,
56 Json::Value actualResultsFailed,
57 Json::Value actualResultsFailureIgnored,
58 Json::Value actualResultsNoComparison,
59 Json::Value actualResultsSucceeded) {
60 Json::Value actualResults;
61 actualResults[kJsonKey_ActualResults_Failed] = actualResultsFailed;
62 actualResults[kJsonKey_ActualResults_FailureIgnored] = actualResultsFailureIgnored;
63 actualResults[kJsonKey_ActualResults_NoComparison] = actualResultsNoComparison;
64 actualResults[kJsonKey_ActualResults_Succeeded] = actualResultsSucceeded;
65 Json::Value root;
66 root[kJsonKey_ActualResults] = actualResults;
67 root[kJsonKey_ExpectedResults] = expectedResults;
68 return root;
69 }
70
71
72 // Expectations class...
73
74 Expectations::Expectations(bool ignoreFailure) {
75 fIgnoreFailure = ignoreFailure;
76 }
77
78 Expectations::Expectations(const SkBitmap& bitmap, bool ignoreFailure) {
79 fBitmap = bitmap;
80 fIgnoreFailure = ignoreFailure;
81 SkHashDigest digest;
82 // TODO(epoger): Better handling for error returned by ComputeDigest()?
83 // For now, we just report a digest of 0 in error cases, like before.
84 if (!SkBitmapHasher::ComputeDigest(bitmap, &digest)) {
85 digest = 0;
86 }
epoger@google.com366a7702013-05-07 15:51:54 +000087 fAllowedBitmapChecksums.push_back() = digest;
epoger@google.com76c913d2013-04-26 15:06:44 +000088 }
89
90 Expectations::Expectations(Json::Value jsonElement) {
91 if (jsonElement.empty()) {
92 fIgnoreFailure = kDefaultIgnoreFailure;
93 } else {
94 Json::Value ignoreFailure = jsonElement[kJsonKey_ExpectedResults_IgnoreFailure];
95 if (ignoreFailure.isNull()) {
96 fIgnoreFailure = kDefaultIgnoreFailure;
97 } else if (!ignoreFailure.isBool()) {
98 gm_fprintf(stderr, "found non-boolean json value"
99 " for key '%s' in element '%s'\n",
100 kJsonKey_ExpectedResults_IgnoreFailure,
101 jsonElement.toStyledString().c_str());
102 DEBUGFAIL_SEE_STDERR;
103 fIgnoreFailure = kDefaultIgnoreFailure;
104 } else {
105 fIgnoreFailure = ignoreFailure.asBool();
106 }
107
108 Json::Value allowedChecksums =
epoger@google.com366a7702013-05-07 15:51:54 +0000109 jsonElement[kJsonKey_ExpectedResults_AllowedBitmapHashes];
epoger@google.com76c913d2013-04-26 15:06:44 +0000110 if (allowedChecksums.isNull()) {
111 // ok, we'll just assume there aren't any expected checksums to compare against
112 } else if (!allowedChecksums.isArray()) {
113 gm_fprintf(stderr, "found non-array json value"
114 " for key '%s' in element '%s'\n",
epoger@google.com366a7702013-05-07 15:51:54 +0000115 kJsonKey_ExpectedResults_AllowedBitmapHashes,
epoger@google.com76c913d2013-04-26 15:06:44 +0000116 jsonElement.toStyledString().c_str());
117 DEBUGFAIL_SEE_STDERR;
118 } else {
119 for (Json::ArrayIndex i=0; i<allowedChecksums.size(); i++) {
120 Json::Value checksumElement = allowedChecksums[i];
121 if (!checksumElement.isIntegral()) {
122 gm_fprintf(stderr, "found non-integer checksum"
123 " in json element '%s'\n",
124 jsonElement.toStyledString().c_str());
125 DEBUGFAIL_SEE_STDERR;
126 } else {
epoger@google.com366a7702013-05-07 15:51:54 +0000127 fAllowedBitmapChecksums.push_back() = asChecksum(checksumElement);
epoger@google.com76c913d2013-04-26 15:06:44 +0000128 }
129 }
130 }
131 }
132 }
133
134 bool Expectations::match(Checksum actualChecksum) const {
epoger@google.com366a7702013-05-07 15:51:54 +0000135 for (int i=0; i < this->fAllowedBitmapChecksums.count(); i++) {
136 Checksum allowedChecksum = this->fAllowedBitmapChecksums[i];
epoger@google.com76c913d2013-04-26 15:06:44 +0000137 if (allowedChecksum == actualChecksum) {
138 return true;
139 }
140 }
141 return false;
142 }
143
144 Json::Value Expectations::asJsonValue() const {
145 Json::Value allowedChecksumArray;
epoger@google.com366a7702013-05-07 15:51:54 +0000146 if (!this->fAllowedBitmapChecksums.empty()) {
147 for (int i=0; i < this->fAllowedBitmapChecksums.count(); i++) {
148 Checksum allowedChecksum = this->fAllowedBitmapChecksums[i];
epoger@google.com76c913d2013-04-26 15:06:44 +0000149 allowedChecksumArray.append(Json::UInt64(allowedChecksum));
150 }
151 }
152
153 Json::Value jsonValue;
epoger@google.com366a7702013-05-07 15:51:54 +0000154 jsonValue[kJsonKey_ExpectedResults_AllowedBitmapHashes] = allowedChecksumArray;
epoger@google.com76c913d2013-04-26 15:06:44 +0000155 jsonValue[kJsonKey_ExpectedResults_IgnoreFailure] = this->ignoreFailure();
156 return jsonValue;
157 }
158
159
160 // IndividualImageExpectationsSource class...
161
162 Expectations IndividualImageExpectationsSource::get(const char *testName) {
epoger@google.comce057fe2013-05-14 15:17:46 +0000163 SkString path = SkPathJoin(fRootDir.c_str(), testName);
epoger@google.com76c913d2013-04-26 15:06:44 +0000164 SkBitmap referenceBitmap;
165 bool decodedReferenceBitmap =
166 SkImageDecoder::DecodeFile(path.c_str(), &referenceBitmap,
167 SkBitmap::kARGB_8888_Config,
168 SkImageDecoder::kDecodePixels_Mode,
169 NULL);
170 if (decodedReferenceBitmap) {
171 return Expectations(referenceBitmap);
172 } else {
173 return Expectations();
174 }
175 }
176
177
178 // JsonExpectationsSource class...
179
180 JsonExpectationsSource::JsonExpectationsSource(const char *jsonPath) {
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000181 Parse(jsonPath, &fJsonRoot);
epoger@google.com76c913d2013-04-26 15:06:44 +0000182 fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults];
183 }
184
185 Expectations JsonExpectationsSource::get(const char *testName) {
186 return Expectations(fJsonExpectedResults[testName]);
187 }
188
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000189 /*static*/ SkData* JsonExpectationsSource::ReadIntoSkData(SkStream &stream, size_t maxBytes) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000190 if (0 == maxBytes) {
191 return SkData::NewEmpty();
192 }
193 char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes));
194 char* bufPtr = bufStart;
195 size_t bytesRemaining = maxBytes;
196 while (bytesRemaining > 0) {
197 size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining);
198 if (0 == bytesReadThisTime) {
199 break;
200 }
201 bytesRemaining -= bytesReadThisTime;
202 bufPtr += bytesReadThisTime;
203 }
204 return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining);
205 }
206
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000207 /*static*/ bool JsonExpectationsSource::Parse(const char *jsonPath, Json::Value *jsonRoot) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000208 SkFILEStream inFile(jsonPath);
209 if (!inFile.isValid()) {
210 gm_fprintf(stderr, "unable to read JSON file %s\n", jsonPath);
211 DEBUGFAIL_SEE_STDERR;
212 return false;
213 }
214
scroggo@google.com6843bdb2013-05-08 19:14:23 +0000215 SkAutoDataUnref dataRef(ReadFileIntoSkData(inFile));
epoger@google.com76c913d2013-04-26 15:06:44 +0000216 if (NULL == dataRef.get()) {
217 gm_fprintf(stderr, "error reading JSON file %s\n", jsonPath);
218 DEBUGFAIL_SEE_STDERR;
219 return false;
220 }
221
222 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data());
223 size_t size = dataRef.get()->size();
224 Json::Reader reader;
225 if (!reader.parse(bytes, bytes+size, *jsonRoot)) {
226 gm_fprintf(stderr, "error parsing JSON file %s\n", jsonPath);
227 DEBUGFAIL_SEE_STDERR;
228 return false;
229 }
230 return true;
231 }
232
233}