blob: e4a3d12d5bf84195527bc79e11fc2e27c442ae91 [file] [log] [blame]
scroggo7a10fb62014-11-04 07:21:10 -08001/*
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"
mtklein62bd1a62015-01-27 14:46:26 -080011#include "SkData.h"
scroggo7a10fb62014-11-04 07:21:10 -080012#include "SkJSONCPP.h"
13#include "SkOSFile.h"
14#include "SkStream.h"
15#include "SkTArray.h"
16#include "SkThread.h"
17
18namespace DM {
19
20SkTArray<JsonWriter::BitmapResult> gBitmapResults;
21SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
22
23void JsonWriter::AddBitmapResult(const BitmapResult& result) {
24 SkAutoMutexAcquire lock(&gBitmapResultLock);
25 gBitmapResults.push_back(result);
26}
27
scroggo0ee26272014-11-07 06:07:32 -080028SkTArray<skiatest::Failure> gFailures;
29SK_DECLARE_STATIC_MUTEX(gFailureLock);
30
31void JsonWriter::AddTestFailure(const skiatest::Failure& failure) {
32 SkAutoMutexAcquire lock(gFailureLock);
33 gFailures.push_back(failure);
34}
35
scroggo7a10fb62014-11-04 07:21:10 -080036void 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;
mtklein3d8878c2015-01-05 06:47:41 -080054 result["key"]["name"] = gBitmapResults[i].name.c_str();
55 result["key"]["config"] = gBitmapResults[i].config.c_str();
mtklein3d8878c2015-01-05 06:47:41 -080056 result["key"]["source_type"] = gBitmapResults[i].sourceType.c_str();
mtklein82079fb2015-02-17 13:18:04 -080057 result["options"]["ext"] = gBitmapResults[i].ext.c_str();
mtklein3d8878c2015-01-05 06:47:41 -080058 result["md5"] = gBitmapResults[i].md5.c_str();
scroggo7a10fb62014-11-04 07:21:10 -080059
60 root["results"].append(result);
61 }
62 }
63
scroggo0ee26272014-11-07 06:07:32 -080064 {
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
scroggo7a10fb62014-11-04 07:21:10 -080077 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json");
halcanary022afb82015-01-30 11:00:12 -080078 sk_mkdir(FLAGS_writePath[0]);
scroggo7a10fb62014-11-04 07:21:10 -080079 SkFILEWStream stream(path.c_str());
80 stream.writeText(Json::StyledWriter().write(root).c_str());
81 stream.flush();
82}
83
mtklein62bd1a62015-01-27 14:46:26 -080084bool 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();
mtklein82079fb2015-02-17 13:18:04 -0800104 br.ext = r["options"]["ext"].asCString();
mtklein62bd1a62015-01-27 14:46:26 -0800105 br.md5 = r["md5"].asCString();
106 callback(br);
107 }
108 return true;
109}
110
scroggo7a10fb62014-11-04 07:21:10 -0800111} // namespace DM