| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 26515ba | 2013-05-14 18:58:12 +0000 | [diff] [blame] | 14 | // These constants must be kept in sync with the JSONKEY_ constants in |
| epoger@google.com | a55e48d | 2013-05-21 16:06:40 +0000 | [diff] [blame^] | 15 | // display_json_results.py ! |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 16 | const static char kJsonKey_ActualResults[] = "actual-results"; |
| 17 | const static char kJsonKey_ActualResults_Failed[] = "failed"; |
| 18 | const static char kJsonKey_ActualResults_FailureIgnored[]= "failure-ignored"; |
| 19 | const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; |
| 20 | const static char kJsonKey_ActualResults_Succeeded[] = "succeeded"; |
| epoger@google.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 21 | const static char kJsonKey_ActualResults_AnyStatus_BitmapHash[] = "bitmap-64bitMD5"; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 22 | |
| 23 | const static char kJsonKey_ExpectedResults[] = "expected-results"; |
| epoger@google.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 24 | const static char kJsonKey_ExpectedResults_AllowedBitmapHashes[] = "allowed-bitmap-64bitMD5s"; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 25 | const static char kJsonKey_ExpectedResults_IgnoreFailure[] = "ignore-failure"; |
| 26 | |
| 27 | namespace skiagm { |
| 28 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 29 | 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.com | ce057fe | 2013-05-14 15:17:46 +0000 | [diff] [blame] | 37 | 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.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 46 | // TODO(epoger): This currently assumes that the result SkHashDigest was |
| epoger@google.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 47 | // generated as an SkHashDigest of an SkBitmap. We'll need to allow for other |
| 48 | // hash types to cover non-bitmaps. |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 49 | Json::Value ActualResultAsJsonValue(const SkHashDigest& result) { |
| 50 | Json::Value jsonValue; |
| epoger@google.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 51 | jsonValue[kJsonKey_ActualResults_AnyStatus_BitmapHash] = asJsonValue(result); |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 52 | 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.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 87 | fAllowedBitmapChecksums.push_back() = digest; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 88 | } |
| 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.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 109 | jsonElement[kJsonKey_ExpectedResults_AllowedBitmapHashes]; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 110 | 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.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 115 | kJsonKey_ExpectedResults_AllowedBitmapHashes, |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 116 | 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.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 127 | fAllowedBitmapChecksums.push_back() = asChecksum(checksumElement); |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | bool Expectations::match(Checksum actualChecksum) const { |
| epoger@google.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 135 | for (int i=0; i < this->fAllowedBitmapChecksums.count(); i++) { |
| 136 | Checksum allowedChecksum = this->fAllowedBitmapChecksums[i]; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 137 | 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.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 146 | if (!this->fAllowedBitmapChecksums.empty()) { |
| 147 | for (int i=0; i < this->fAllowedBitmapChecksums.count(); i++) { |
| 148 | Checksum allowedChecksum = this->fAllowedBitmapChecksums[i]; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 149 | allowedChecksumArray.append(Json::UInt64(allowedChecksum)); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | Json::Value jsonValue; |
| epoger@google.com | 366a770 | 2013-05-07 15:51:54 +0000 | [diff] [blame] | 154 | jsonValue[kJsonKey_ExpectedResults_AllowedBitmapHashes] = allowedChecksumArray; |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 155 | 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.com | ce057fe | 2013-05-14 15:17:46 +0000 | [diff] [blame] | 163 | SkString path = SkPathJoin(fRootDir.c_str(), testName); |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 164 | 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.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 181 | Parse(jsonPath, &fJsonRoot); |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 182 | fJsonExpectedResults = fJsonRoot[kJsonKey_ExpectedResults]; |
| 183 | } |
| 184 | |
| 185 | Expectations JsonExpectationsSource::get(const char *testName) { |
| 186 | return Expectations(fJsonExpectedResults[testName]); |
| 187 | } |
| 188 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 189 | /*static*/ SkData* JsonExpectationsSource::ReadIntoSkData(SkStream &stream, size_t maxBytes) { |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 190 | 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.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 207 | /*static*/ bool JsonExpectationsSource::Parse(const char *jsonPath, Json::Value *jsonRoot) { |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 208 | 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.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 215 | SkAutoDataUnref dataRef(ReadFileIntoSkData(inFile)); |
| epoger@google.com | 76c913d | 2013-04-26 15:06:44 +0000 | [diff] [blame] | 216 | 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 | } |