blob: ac232e9f30daf4eba7f8f04c59db01e3f3b80998 [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";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000031const static char kJsonKey_ExpectedResults[] = "expected-results";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000032const static char kJsonKey_Header[] = "header";
33const static char kJsonKey_Header_Type[] = "type";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000034const static char kJsonKey_Header_Revision[] = "revision";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000035const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
36const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
37const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
38const static char kJsonKey_Image_Filepath[] = "filepath";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000039const static char kJsonKey_Image_IgnoreFailure[] = "ignoreFailure";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000040const static char kJsonKey_Source_TiledImages[] = "tiled-images";
41const static char kJsonKey_Source_WholeImage[] = "whole-image";
42// Values (not keys) that are written out by this JSON generator
43const static char kJsonValue_Header_Type[] = "ChecksummedImages";
44const static int kJsonValue_Header_Revision = 1;
45const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000046const static char kJsonValue_Image_ComparisonResult_Failed[] = "failed";
47const static char kJsonValue_Image_ComparisonResult_FailureIgnored[] = "failure-ignored";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000048const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000049const static char kJsonValue_Image_ComparisonResult_Succeeded[] = "succeeded";
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000050
51namespace sk_tools {
52
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000053 // ImageDigest class...
54
55 ImageDigest::ImageDigest(const SkBitmap &bitmap) {
56 if (!SkBitmapHasher::ComputeDigest(bitmap, &fHashValue)) {
57 SkFAIL("unable to compute image digest");
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000058 }
59 }
60
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000061 ImageDigest::ImageDigest(const SkString &hashType, uint64_t hashValue) {
62 if (!hashType.equals(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5)) {
63 SkFAIL((SkString("unsupported hashType ")+=hashType).c_str());
64 } else {
65 fHashValue = hashValue;
66 }
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000067 }
68
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000069 SkString ImageDigest::getHashType() const {
70 // TODO(epoger): The current implementation assumes that the
71 // result digest is always of type kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5 .
72 return SkString(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5);
73 }
74
75 uint64_t ImageDigest::getHashValue() const {
76 return fHashValue;
77 }
78
79 // BitmapAndDigest class...
80
81 BitmapAndDigest::BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap) {
82 }
83
84 const ImageDigest *BitmapAndDigest::getImageDigestPtr() {
85 if (NULL == fImageDigestRef.get()) {
86 fImageDigestRef.reset(SkNEW_ARGS(ImageDigest, (fBitmap)));
87 }
88 return fImageDigestRef.get();
89 }
90
91 const SkBitmap *BitmapAndDigest::getBitmapPtr() const {
92 return &fBitmap;
93 }
94
95 // ImageResultsAndExpectations class...
96
97 bool ImageResultsAndExpectations::readExpectationsFile(const char *jsonPath) {
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +000098 if (NULL == jsonPath) {
99 SkDebugf("JSON expectations filename not specified\n");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000100 return false;
101 }
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000102 SkFILE* filePtr = sk_fopen(jsonPath, kRead_SkFILE_Flag);
103 if (NULL == filePtr) {
104 SkDebugf("JSON expectations file '%s' does not exist\n", jsonPath);
105 return false;
106 }
107 size_t size = sk_fgetsize(filePtr);
108 if (0 == size) {
109 SkDebugf("JSON expectations file '%s' is empty, so no expectations\n", jsonPath);
110 sk_fclose(filePtr);
111 fExpectedResults.clear();
112 return true;
113 }
114 bool parsedJson = Parse(filePtr, &fExpectedJsonRoot);
115 sk_fclose(filePtr);
116 if (!parsedJson) {
117 SkDebugf("Failed to parse JSON expectations file '%s'\n", jsonPath);
118 return false;
119 }
120 Json::Value header = fExpectedJsonRoot[kJsonKey_Header];
121 Json::Value headerType = header[kJsonKey_Header_Type];
122 Json::Value headerRevision = header[kJsonKey_Header_Revision];
123 if (strcmp(headerType.asCString(), kJsonValue_Header_Type)) {
124 SkDebugf("JSON expectations file '%s': expected headerType '%s', found '%s'\n",
125 jsonPath, kJsonValue_Header_Type, headerType.asCString());
126 return false;
127 }
128 if (headerRevision.asInt() != kJsonValue_Header_Revision) {
129 SkDebugf("JSON expectations file '%s': expected headerRevision %d, found %d\n",
130 jsonPath, kJsonValue_Header_Revision, headerRevision.asInt());
131 return false;
132 }
133 fExpectedResults = fExpectedJsonRoot[kJsonKey_ExpectedResults];
134 return true;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000135 }
136
137 void ImageResultsAndExpectations::add(const char *sourceName, const char *fileName,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000138 const ImageDigest &digest, const int *tileNumber) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000139 // Get expectation, if any.
140 Json::Value expectedImage;
141 if (!fExpectedResults.isNull()) {
142 if (NULL == tileNumber) {
143 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
144 } else {
145 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages]
146 [*tileNumber];
147 }
148 }
149
150 // Fill in info about the actual result itself.
151 Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
152 Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
153 Json::Value actualImage;
154 actualImage[kJsonKey_Image_ChecksumAlgorithm] = actualChecksumAlgorithm;
155 actualImage[kJsonKey_Image_ChecksumValue] = actualChecksumValue;
156 actualImage[kJsonKey_Image_Filepath] = fileName;
157
158 // Compare against expectedImage to fill in comparisonResult.
159 Json::Value comparisonResult = kJsonValue_Image_ComparisonResult_NoComparison;
160 if (!expectedImage.isNull()) {
161 if ((actualChecksumAlgorithm == expectedImage[kJsonKey_Image_ChecksumAlgorithm]) &&
162 (actualChecksumValue == expectedImage[kJsonKey_Image_ChecksumValue])) {
163 comparisonResult = kJsonValue_Image_ComparisonResult_Succeeded;
164 } else if (expectedImage[kJsonKey_Image_IgnoreFailure] == true) {
165 comparisonResult = kJsonValue_Image_ComparisonResult_FailureIgnored;
166 } else {
167 comparisonResult = kJsonValue_Image_ComparisonResult_Failed;
168 }
169 }
170 actualImage[kJsonKey_Image_ComparisonResult] = comparisonResult;
171
172 // Add this actual result to our collection.
173 if (NULL == tileNumber) {
174 fActualResults[sourceName][kJsonKey_Source_WholeImage] = actualImage;
175 } else {
176 fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = actualImage;
177 }
178 }
179
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000180 bool ImageResultsAndExpectations::matchesExpectation(const char *sourceName,
181 const ImageDigest &digest,
182 const int *tileNumber) {
183 if (fExpectedResults.isNull()) {
184 return false;
185 }
186
187 Json::Value expectedImage;
188 if (NULL == tileNumber) {
189 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
190 } else {
191 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber];
192 }
193 if (expectedImage.isNull()) {
194 return false;
195 }
196
197 Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
198 Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
199 return ((actualChecksumAlgorithm == expectedImage[kJsonKey_Image_ChecksumAlgorithm]) &&
200 (actualChecksumValue == expectedImage[kJsonKey_Image_ChecksumValue]));
201 }
202
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000203 void ImageResultsAndExpectations::writeToFile(const char *filename) const {
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000204 Json::Value header;
205 header[kJsonKey_Header_Type] = kJsonValue_Header_Type;
206 header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision;
207 Json::Value root;
208 root[kJsonKey_Header] = header;
209 root[kJsonKey_ActualResults] = fActualResults;
210 std::string jsonStdString = root.toStyledString();
211 SkFILEWStream stream(filename);
212 stream.write(jsonStdString.c_str(), jsonStdString.length());
213 }
214
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000215 /*static*/ bool ImageResultsAndExpectations::Parse(SkFILE *filePtr,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000216 Json::Value *jsonRoot) {
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000217 SkAutoDataUnref dataRef(SkData::NewFromFILE(filePtr));
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000218 if (NULL == dataRef.get()) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000219 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)) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000226 return false;
227 }
228
229 return true;
230 }
231
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000232} // namespace sk_tools