blob: 05a905dd585dbc34aca6fb47de70b0b253bb20c9 [file] [log] [blame]
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +00001/*
2 * Copyright 2014 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 "SkBitmap.h"
9#include "SkBitmapHasher.h"
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000010#include "SkData.h"
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000011#include "SkJSONCPP.h"
12#include "SkOSFile.h"
13#include "SkStream.h"
14#include "SkTypes.h"
15
16#include "image_expectations.h"
17
18/*
19 * TODO(epoger): Make constant strings consistent instead of mixing hypenated and camel-caps.
20 *
21 * TODO(epoger): Similar constants are already maintained in 2 other places:
22 * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place.
23 * Figure out a way to share the definitions instead.
24 *
25 * Note that, as of https://codereview.chromium.org/226293002 , the JSON
26 * schema used here has started to differ from the one in gm_expectations.cpp .
27 * TODO(epoger): Consider getting GM and render_pictures to use the same JSON
28 * output module.
29 */
30const static char kJsonKey_ActualResults[] = "actual-results";
epogerb492c6f2014-08-14 07:32:49 -070031const static char kJsonKey_Descriptions[] = "descriptions";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000032const static char kJsonKey_ExpectedResults[] = "expected-results";
rmistry2529f2e2014-08-22 04:46:30 -070033const static char kJsonKey_ImageBaseGSUrl[] = "image-base-gs-url";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000034const static char kJsonKey_Header[] = "header";
35const static char kJsonKey_Header_Type[] = "type";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000036const static char kJsonKey_Header_Revision[] = "revision";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000037const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
38const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
39const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
40const static char kJsonKey_Image_Filepath[] = "filepath";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000041const static char kJsonKey_Image_IgnoreFailure[] = "ignoreFailure";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000042const static char kJsonKey_Source_TiledImages[] = "tiled-images";
43const static char kJsonKey_Source_WholeImage[] = "whole-image";
44// Values (not keys) that are written out by this JSON generator
45const static char kJsonValue_Header_Type[] = "ChecksummedImages";
46const static int kJsonValue_Header_Revision = 1;
47const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000048const static char kJsonValue_Image_ComparisonResult_Failed[] = "failed";
49const static char kJsonValue_Image_ComparisonResult_FailureIgnored[] = "failure-ignored";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000050const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000051const static char kJsonValue_Image_ComparisonResult_Succeeded[] = "succeeded";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000052
53namespace sk_tools {
54
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000055 // ImageDigest class...
56
epoger85b438d2014-08-21 23:21:32 -070057 ImageDigest::ImageDigest(const SkBitmap &bitmap) :
58 fBitmap(bitmap), fHashValue(0), fComputedHashValue(false) {}
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000059
epoger85b438d2014-08-21 23:21:32 -070060 ImageDigest::ImageDigest(const SkString &hashType, uint64_t hashValue) :
61 fBitmap(), fHashValue(hashValue), fComputedHashValue(true) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000062 if (!hashType.equals(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5)) {
epoger85b438d2014-08-21 23:21:32 -070063 SkDebugf("unsupported hashType '%s'\n", hashType.c_str());
64 SkFAIL("unsupported hashType (see above)");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000065 }
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000066 }
67
epoger85b438d2014-08-21 23:21:32 -070068 bool ImageDigest::equals(ImageDigest &other) {
69 // TODO(epoger): The current implementation assumes that this
70 // and other always have hashType kJsonKey_Hashtype_Bitmap_64bitMD5
71 return (this->getHashValue() == other.getHashValue());
72 }
73
74 SkString ImageDigest::getHashType() {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000075 // TODO(epoger): The current implementation assumes that the
76 // result digest is always of type kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5 .
77 return SkString(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5);
78 }
79
epoger85b438d2014-08-21 23:21:32 -070080 uint64_t ImageDigest::getHashValue() {
81 if (!this->fComputedHashValue) {
82 if (!SkBitmapHasher::ComputeDigest(this->fBitmap, &this->fHashValue)) {
83 SkFAIL("unable to compute image digest");
84 }
85 this->fComputedHashValue = true;
86 }
87 return this->fHashValue;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000088 }
89
90 // BitmapAndDigest class...
91
epoger85b438d2014-08-21 23:21:32 -070092 BitmapAndDigest::BitmapAndDigest(const SkBitmap &bitmap) :
93 fBitmap(bitmap), fImageDigest(bitmap) {}
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000094
epoger85b438d2014-08-21 23:21:32 -070095 const SkBitmap *BitmapAndDigest::getBitmapPtr() const {return &fBitmap;}
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000096
epoger85b438d2014-08-21 23:21:32 -070097 ImageDigest *BitmapAndDigest::getImageDigestPtr() {return &fImageDigest;}
98
99 // Expectation class...
100
101 // For when we need a valid ImageDigest, but we don't care what it is.
102 static const ImageDigest kDummyImageDigest(
103 SkString(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5), 0);
104
105 Expectation::Expectation(bool ignoreFailure) :
106 fIsEmpty(true), fIgnoreFailure(ignoreFailure), fImageDigest(kDummyImageDigest) {}
107
108 Expectation::Expectation(const SkString &hashType, uint64_t hashValue, bool ignoreFailure) :
109 fIsEmpty(false), fIgnoreFailure(ignoreFailure), fImageDigest(hashType, hashValue) {}
110
111 Expectation::Expectation(const SkBitmap& bitmap, bool ignoreFailure) :
112 fIsEmpty(false), fIgnoreFailure(ignoreFailure), fImageDigest(bitmap) {}
113
114 bool Expectation::ignoreFailure() const { return this->fIgnoreFailure; }
115
116 bool Expectation::empty() const { return this->fIsEmpty; }
117
118 bool Expectation::matches(ImageDigest &imageDigest) {
119 return !(this->fIsEmpty) && (this->fImageDigest.equals(imageDigest));
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000120 }
121
122 // ImageResultsAndExpectations class...
123
124 bool ImageResultsAndExpectations::readExpectationsFile(const char *jsonPath) {
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000125 if (NULL == jsonPath) {
126 SkDebugf("JSON expectations filename not specified\n");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000127 return false;
128 }
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000129 SkFILE* filePtr = sk_fopen(jsonPath, kRead_SkFILE_Flag);
130 if (NULL == filePtr) {
131 SkDebugf("JSON expectations file '%s' does not exist\n", jsonPath);
132 return false;
133 }
134 size_t size = sk_fgetsize(filePtr);
135 if (0 == size) {
136 SkDebugf("JSON expectations file '%s' is empty, so no expectations\n", jsonPath);
137 sk_fclose(filePtr);
138 fExpectedResults.clear();
139 return true;
140 }
141 bool parsedJson = Parse(filePtr, &fExpectedJsonRoot);
142 sk_fclose(filePtr);
143 if (!parsedJson) {
144 SkDebugf("Failed to parse JSON expectations file '%s'\n", jsonPath);
145 return false;
146 }
147 Json::Value header = fExpectedJsonRoot[kJsonKey_Header];
148 Json::Value headerType = header[kJsonKey_Header_Type];
149 Json::Value headerRevision = header[kJsonKey_Header_Revision];
150 if (strcmp(headerType.asCString(), kJsonValue_Header_Type)) {
151 SkDebugf("JSON expectations file '%s': expected headerType '%s', found '%s'\n",
152 jsonPath, kJsonValue_Header_Type, headerType.asCString());
153 return false;
154 }
155 if (headerRevision.asInt() != kJsonValue_Header_Revision) {
156 SkDebugf("JSON expectations file '%s': expected headerRevision %d, found %d\n",
157 jsonPath, kJsonValue_Header_Revision, headerRevision.asInt());
158 return false;
159 }
160 fExpectedResults = fExpectedJsonRoot[kJsonKey_ExpectedResults];
161 return true;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000162 }
163
164 void ImageResultsAndExpectations::add(const char *sourceName, const char *fileName,
epoger85b438d2014-08-21 23:21:32 -0700165 ImageDigest &digest, const int *tileNumber) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000166 // Get expectation, if any.
epoger85b438d2014-08-21 23:21:32 -0700167 Expectation expectation = this->getExpectation(sourceName, tileNumber);
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000168
epoger85b438d2014-08-21 23:21:32 -0700169 // Fill in info about the actual result.
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000170 Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
171 Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
172 Json::Value actualImage;
173 actualImage[kJsonKey_Image_ChecksumAlgorithm] = actualChecksumAlgorithm;
174 actualImage[kJsonKey_Image_ChecksumValue] = actualChecksumValue;
175 actualImage[kJsonKey_Image_Filepath] = fileName;
176
177 // Compare against expectedImage to fill in comparisonResult.
epoger85b438d2014-08-21 23:21:32 -0700178 Json::Value comparisonResult;
179 if (expectation.empty()) {
180 comparisonResult = kJsonValue_Image_ComparisonResult_NoComparison;
181 } else if (expectation.matches(digest)) {
182 comparisonResult = kJsonValue_Image_ComparisonResult_Succeeded;
183 } else if (expectation.ignoreFailure()) {
184 comparisonResult = kJsonValue_Image_ComparisonResult_FailureIgnored;
185 } else {
186 comparisonResult = kJsonValue_Image_ComparisonResult_Failed;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000187 }
188 actualImage[kJsonKey_Image_ComparisonResult] = comparisonResult;
189
190 // Add this actual result to our collection.
191 if (NULL == tileNumber) {
192 fActualResults[sourceName][kJsonKey_Source_WholeImage] = actualImage;
193 } else {
194 fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = actualImage;
195 }
196 }
197
epogerb492c6f2014-08-14 07:32:49 -0700198 void ImageResultsAndExpectations::addDescription(const char *key, const char *value) {
199 fDescriptions[key] = value;
200 }
201
rmistry2529f2e2014-08-22 04:46:30 -0700202 void ImageResultsAndExpectations::setImageBaseGSUrl(const char *imageBaseGSUrl) {
203 fImageBaseGSUrl = imageBaseGSUrl;
204 }
205
epoger85b438d2014-08-21 23:21:32 -0700206 Expectation ImageResultsAndExpectations::getExpectation(const char *sourceName,
207 const int *tileNumber) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000208 if (fExpectedResults.isNull()) {
epoger85b438d2014-08-21 23:21:32 -0700209 return Expectation();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000210 }
211
212 Json::Value expectedImage;
213 if (NULL == tileNumber) {
214 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
215 } else {
216 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber];
217 }
218 if (expectedImage.isNull()) {
epoger85b438d2014-08-21 23:21:32 -0700219 return Expectation();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000220 }
221
epoger85b438d2014-08-21 23:21:32 -0700222 bool ignoreFailure = (expectedImage[kJsonKey_Image_IgnoreFailure] == true);
223 return Expectation(SkString(expectedImage[kJsonKey_Image_ChecksumAlgorithm].asCString()),
224 expectedImage[kJsonKey_Image_ChecksumValue].asUInt64(),
225 ignoreFailure);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000226 }
227
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000228 void ImageResultsAndExpectations::writeToFile(const char *filename) const {
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000229 Json::Value header;
230 header[kJsonKey_Header_Type] = kJsonValue_Header_Type;
231 header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision;
232 Json::Value root;
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000233 root[kJsonKey_ActualResults] = fActualResults;
epogerb492c6f2014-08-14 07:32:49 -0700234 root[kJsonKey_Descriptions] = fDescriptions;
235 root[kJsonKey_Header] = header;
rmistry2529f2e2014-08-22 04:46:30 -0700236 root[kJsonKey_ImageBaseGSUrl] = fImageBaseGSUrl;
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000237 std::string jsonStdString = root.toStyledString();
238 SkFILEWStream stream(filename);
239 stream.write(jsonStdString.c_str(), jsonStdString.length());
240 }
241
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000242 /*static*/ bool ImageResultsAndExpectations::Parse(SkFILE *filePtr,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000243 Json::Value *jsonRoot) {
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000244 SkAutoDataUnref dataRef(SkData::NewFromFILE(filePtr));
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000245 if (NULL == dataRef.get()) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000246 return false;
247 }
248
249 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data());
250 size_t size = dataRef.get()->size();
251 Json::Reader reader;
252 if (!reader.parse(bytes, bytes+size, *jsonRoot)) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000253 return false;
254 }
255
256 return true;
257 }
258
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000259} // namespace sk_tools