dfc2638a9e28406c70994111c8a09b7a17f95c14
[platform/framework/web/crosswalk.git] / src / third_party / skia / tools / image_expectations.cpp
1 /*
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"
10 #include "SkData.h"
11 #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  */
30 const static char kJsonKey_ActualResults[] = "actual-results";
31 const static char kJsonKey_Descriptions[] = "descriptions";
32 const static char kJsonKey_ExpectedResults[] = "expected-results";
33 const static char kJsonKey_Header[] = "header";
34 const static char kJsonKey_Header_Type[] = "type";
35 const static char kJsonKey_Header_Revision[] = "revision";
36 const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
37 const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
38 const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
39 const static char kJsonKey_Image_Filepath[] = "filepath";
40 const static char kJsonKey_Image_IgnoreFailure[] = "ignoreFailure";
41 const static char kJsonKey_Source_TiledImages[] = "tiled-images";
42 const static char kJsonKey_Source_WholeImage[] = "whole-image";
43 // Values (not keys) that are written out by this JSON generator
44 const static char kJsonValue_Header_Type[] = "ChecksummedImages";
45 const static int kJsonValue_Header_Revision = 1;
46 const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
47 const static char kJsonValue_Image_ComparisonResult_Failed[] = "failed";
48 const static char kJsonValue_Image_ComparisonResult_FailureIgnored[] = "failure-ignored";
49 const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
50 const static char kJsonValue_Image_ComparisonResult_Succeeded[] = "succeeded";
51
52 namespace sk_tools {
53
54     // ImageDigest class...
55
56     ImageDigest::ImageDigest(const SkBitmap &bitmap) {
57         if (!SkBitmapHasher::ComputeDigest(bitmap, &fHashValue)) {
58             SkFAIL("unable to compute image digest");
59         }
60     }
61
62     ImageDigest::ImageDigest(const SkString &hashType, uint64_t hashValue) {
63         if (!hashType.equals(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5)) {
64             SkFAIL((SkString("unsupported hashType ")+=hashType).c_str());
65         } else {
66             fHashValue = hashValue;
67         }
68     }
69
70     SkString ImageDigest::getHashType() const {
71         // TODO(epoger): The current implementation assumes that the
72         // result digest is always of type kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5 .
73         return SkString(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5);
74     }
75
76     uint64_t ImageDigest::getHashValue() const {
77         return fHashValue;
78     }
79
80     // BitmapAndDigest class...
81
82     BitmapAndDigest::BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap) {
83     }
84
85     const ImageDigest *BitmapAndDigest::getImageDigestPtr() {
86         if (NULL == fImageDigestRef.get()) {
87             fImageDigestRef.reset(SkNEW_ARGS(ImageDigest, (fBitmap)));
88         }
89         return fImageDigestRef.get();
90     }
91
92     const SkBitmap *BitmapAndDigest::getBitmapPtr() const {
93         return &fBitmap;
94     }
95
96     // ImageResultsAndExpectations class...
97
98     bool ImageResultsAndExpectations::readExpectationsFile(const char *jsonPath) {
99         if (NULL == jsonPath) {
100             SkDebugf("JSON expectations filename not specified\n");
101             return false;
102         }
103         SkFILE* filePtr = sk_fopen(jsonPath, kRead_SkFILE_Flag);
104         if (NULL == filePtr) {
105             SkDebugf("JSON expectations file '%s' does not exist\n", jsonPath);
106             return false;
107         }
108         size_t size = sk_fgetsize(filePtr);
109         if (0 == size) {
110             SkDebugf("JSON expectations file '%s' is empty, so no expectations\n", jsonPath);
111             sk_fclose(filePtr);
112             fExpectedResults.clear();
113             return true;
114         }
115         bool parsedJson = Parse(filePtr, &fExpectedJsonRoot);
116         sk_fclose(filePtr);
117         if (!parsedJson) {
118             SkDebugf("Failed to parse JSON expectations file '%s'\n", jsonPath);
119             return false;
120         }
121         Json::Value header = fExpectedJsonRoot[kJsonKey_Header];
122         Json::Value headerType = header[kJsonKey_Header_Type];
123         Json::Value headerRevision = header[kJsonKey_Header_Revision];
124         if (strcmp(headerType.asCString(), kJsonValue_Header_Type)) {
125             SkDebugf("JSON expectations file '%s': expected headerType '%s', found '%s'\n",
126                      jsonPath, kJsonValue_Header_Type, headerType.asCString());
127             return false;
128         }
129         if (headerRevision.asInt() != kJsonValue_Header_Revision) {
130             SkDebugf("JSON expectations file '%s': expected headerRevision %d, found %d\n",
131                      jsonPath, kJsonValue_Header_Revision, headerRevision.asInt());
132             return false;
133         }
134         fExpectedResults = fExpectedJsonRoot[kJsonKey_ExpectedResults];
135         return true;
136     }
137
138     void ImageResultsAndExpectations::add(const char *sourceName, const char *fileName,
139                                           const ImageDigest &digest, const int *tileNumber) {
140         // Get expectation, if any.
141         Json::Value expectedImage;
142         if (!fExpectedResults.isNull()) {
143             if (NULL == tileNumber) {
144                 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
145             } else {
146                 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages]
147                                                 [*tileNumber];
148             }
149         }
150
151         // Fill in info about the actual result itself.
152         Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
153         Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
154         Json::Value actualImage;
155         actualImage[kJsonKey_Image_ChecksumAlgorithm] = actualChecksumAlgorithm;
156         actualImage[kJsonKey_Image_ChecksumValue] = actualChecksumValue;
157         actualImage[kJsonKey_Image_Filepath] = fileName;
158
159         // Compare against expectedImage to fill in comparisonResult.
160         Json::Value comparisonResult = kJsonValue_Image_ComparisonResult_NoComparison;
161         if (!expectedImage.isNull()) {
162             if ((actualChecksumAlgorithm == expectedImage[kJsonKey_Image_ChecksumAlgorithm]) &&
163                 (actualChecksumValue == expectedImage[kJsonKey_Image_ChecksumValue])) {
164                 comparisonResult = kJsonValue_Image_ComparisonResult_Succeeded;
165             } else if (expectedImage[kJsonKey_Image_IgnoreFailure] == true) {
166                 comparisonResult = kJsonValue_Image_ComparisonResult_FailureIgnored;
167             } else {
168                 comparisonResult = kJsonValue_Image_ComparisonResult_Failed;
169             }
170         }
171         actualImage[kJsonKey_Image_ComparisonResult] = comparisonResult;
172
173         // Add this actual result to our collection.
174         if (NULL == tileNumber) {
175             fActualResults[sourceName][kJsonKey_Source_WholeImage] = actualImage;
176         } else {
177             fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = actualImage;
178         }
179     }
180
181     void ImageResultsAndExpectations::addDescription(const char *key, const char *value) {
182         fDescriptions[key] = value;
183     }
184
185     bool ImageResultsAndExpectations::matchesExpectation(const char *sourceName,
186                                                          const ImageDigest &digest,
187                                                          const int *tileNumber) {
188         if (fExpectedResults.isNull()) {
189             return false;
190         }
191
192         Json::Value expectedImage;
193         if (NULL == tileNumber) {
194             expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
195         } else {
196             expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber];
197         }
198         if (expectedImage.isNull()) {
199             return false;
200         }
201
202         Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
203         Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
204         return ((actualChecksumAlgorithm == expectedImage[kJsonKey_Image_ChecksumAlgorithm]) &&
205                 (actualChecksumValue == expectedImage[kJsonKey_Image_ChecksumValue]));
206     }
207
208     void ImageResultsAndExpectations::writeToFile(const char *filename) const {
209         Json::Value header;
210         header[kJsonKey_Header_Type] = kJsonValue_Header_Type;
211         header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision;
212         Json::Value root;
213         root[kJsonKey_ActualResults] = fActualResults;
214         root[kJsonKey_Descriptions] = fDescriptions;
215         root[kJsonKey_Header] = header;
216         std::string jsonStdString = root.toStyledString();
217         SkFILEWStream stream(filename);
218         stream.write(jsonStdString.c_str(), jsonStdString.length());
219     }
220
221     /*static*/ bool ImageResultsAndExpectations::Parse(SkFILE *filePtr,
222                                                        Json::Value *jsonRoot) {
223         SkAutoDataUnref dataRef(SkData::NewFromFILE(filePtr));
224         if (NULL == dataRef.get()) {
225             return false;
226         }
227
228         const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data());
229         size_t size = dataRef.get()->size();
230         Json::Reader reader;
231         if (!reader.parse(bytes, bytes+size, *jsonRoot)) {
232             return false;
233         }
234
235         return true;
236     }
237
238 } // namespace sk_tools