blob: 56c1301df166ad1a70c49f54dec7b4c74e51db38 [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();
mtklein748ca3b2015-01-15 10:56:12 -080057 result["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");
78 SkFILEWStream stream(path.c_str());
79 stream.writeText(Json::StyledWriter().write(root).c_str());
80 stream.flush();
81}
82
mtklein62bd1a62015-01-27 14:46:26 -080083bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
84 SkAutoTUnref<SkData> json(SkData::NewFromFileName(path));
85 if (!json) {
86 return false;
87 }
88
89 Json::Reader reader;
90 Json::Value root;
91 const char* data = (const char*)json->data();
92 if (!reader.parse(data, data+json->size(), root)) {
93 return false;
94 }
95
96 const Json::Value& results = root["results"];
97 BitmapResult br;
98 for (unsigned i = 0; i < results.size(); i++) {
99 const Json::Value& r = results[i];
100 br.name = r["key"]["name"].asCString();
101 br.config = r["key"]["config"].asCString();
102 br.sourceType = r["key"]["source_type"].asCString();
103 br.ext = r["ext"].asCString();
104 br.md5 = r["md5"].asCString();
105 callback(br);
106 }
107 return true;
108}
109
scroggo7a10fb62014-11-04 07:21:10 -0800110} // namespace DM