scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 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 "DMJsonWriter.h" |
| 9 | |
| 10 | #include "SkCommonFlags.h" |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame] | 11 | #include "SkData.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 12 | #include "SkJSONCPP.h" |
| 13 | #include "SkOSFile.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTArray.h" |
| 16 | #include "SkThread.h" |
| 17 | |
| 18 | namespace DM { |
| 19 | |
| 20 | SkTArray<JsonWriter::BitmapResult> gBitmapResults; |
| 21 | SK_DECLARE_STATIC_MUTEX(gBitmapResultLock); |
| 22 | |
| 23 | void JsonWriter::AddBitmapResult(const BitmapResult& result) { |
| 24 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
| 25 | gBitmapResults.push_back(result); |
| 26 | } |
| 27 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 28 | SkTArray<skiatest::Failure> gFailures; |
| 29 | SK_DECLARE_STATIC_MUTEX(gFailureLock); |
| 30 | |
| 31 | void JsonWriter::AddTestFailure(const skiatest::Failure& failure) { |
| 32 | SkAutoMutexAcquire lock(gFailureLock); |
| 33 | gFailures.push_back(failure); |
| 34 | } |
| 35 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 36 | void JsonWriter::DumpJson() { |
| 37 | if (FLAGS_writePath.isEmpty()) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | Json::Value root; |
| 42 | |
| 43 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 44 | root[FLAGS_properties[i-1]] = FLAGS_properties[i]; |
| 45 | } |
| 46 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 47 | root["key"][FLAGS_key[i-1]] = FLAGS_key[i]; |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
| 52 | for (int i = 0; i < gBitmapResults.count(); i++) { |
| 53 | Json::Value result; |
mtklein | 3d8878c | 2015-01-05 06:47:41 -0800 | [diff] [blame] | 54 | result["key"]["name"] = gBitmapResults[i].name.c_str(); |
| 55 | result["key"]["config"] = gBitmapResults[i].config.c_str(); |
mtklein | 3d8878c | 2015-01-05 06:47:41 -0800 | [diff] [blame] | 56 | result["key"]["source_type"] = gBitmapResults[i].sourceType.c_str(); |
mtklein | 82079fb | 2015-02-17 13:18:04 -0800 | [diff] [blame] | 57 | result["options"]["ext"] = gBitmapResults[i].ext.c_str(); |
mtklein | 3d8878c | 2015-01-05 06:47:41 -0800 | [diff] [blame] | 58 | result["md5"] = gBitmapResults[i].md5.c_str(); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 59 | |
| 60 | root["results"].append(result); |
| 61 | } |
| 62 | } |
| 63 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 64 | { |
| 65 | SkAutoMutexAcquire lock(gFailureLock); |
| 66 | for (int i = 0; i < gFailures.count(); i++) { |
| 67 | Json::Value result; |
| 68 | result["file_name"] = gFailures[i].fileName; |
| 69 | result["line_no"] = gFailures[i].lineNo; |
| 70 | result["condition"] = gFailures[i].condition; |
| 71 | result["message"] = gFailures[i].message.c_str(); |
| 72 | |
| 73 | root["test_results"]["failures"].append(result); |
| 74 | } |
| 75 | } |
| 76 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 77 | SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); |
halcanary | 022afb8 | 2015-01-30 11:00:12 -0800 | [diff] [blame] | 78 | sk_mkdir(FLAGS_writePath[0]); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 79 | SkFILEWStream stream(path.c_str()); |
| 80 | stream.writeText(Json::StyledWriter().write(root).c_str()); |
| 81 | stream.flush(); |
| 82 | } |
| 83 | |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame] | 84 | bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) { |
| 85 | SkAutoTUnref<SkData> json(SkData::NewFromFileName(path)); |
| 86 | if (!json) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | Json::Reader reader; |
| 91 | Json::Value root; |
| 92 | const char* data = (const char*)json->data(); |
| 93 | if (!reader.parse(data, data+json->size(), root)) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | const Json::Value& results = root["results"]; |
| 98 | BitmapResult br; |
| 99 | for (unsigned i = 0; i < results.size(); i++) { |
| 100 | const Json::Value& r = results[i]; |
| 101 | br.name = r["key"]["name"].asCString(); |
| 102 | br.config = r["key"]["config"].asCString(); |
| 103 | br.sourceType = r["key"]["source_type"].asCString(); |
mtklein | 82079fb | 2015-02-17 13:18:04 -0800 | [diff] [blame] | 104 | br.ext = r["options"]["ext"].asCString(); |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame] | 105 | br.md5 = r["md5"].asCString(); |
| 106 | callback(br); |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 111 | } // namespace DM |