blob: d3b9aa0f0a8c4ccdb2ecf721330b95133dd19068 [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
halcanaryc79a3912015-04-01 13:31:34 -070010#include "ProcStats.h"
scroggo7a10fb62014-11-04 07:21:10 -080011#include "SkCommonFlags.h"
Brian Osman57796b32019-01-25 18:02:59 +000012#include "SkData.h"
Brian Osmanfad5c772019-01-25 16:34:20 -050013#include "SkJSON.h"
Brian Osmanaae63882019-01-25 10:02:27 -050014#include "SkJSONWriter.h"
mtklein1b249332015-07-07 12:21:21 -070015#include "SkMutex.h"
scroggo7a10fb62014-11-04 07:21:10 -080016#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050017#include "SkOSPath.h"
scroggo7a10fb62014-11-04 07:21:10 -080018#include "SkStream.h"
Brian Osman57796b32019-01-25 18:02:59 +000019#include "SkTArray.h"
scroggo7a10fb62014-11-04 07:21:10 -080020
21namespace DM {
22
23SkTArray<JsonWriter::BitmapResult> gBitmapResults;
reed086eea92016-05-04 17:12:46 -070024SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
scroggo7a10fb62014-11-04 07:21:10 -080025
26void JsonWriter::AddBitmapResult(const BitmapResult& result) {
27 SkAutoMutexAcquire lock(&gBitmapResultLock);
28 gBitmapResults.push_back(result);
29}
30
scroggo0ee26272014-11-07 06:07:32 -080031SkTArray<skiatest::Failure> gFailures;
reed086eea92016-05-04 17:12:46 -070032SK_DECLARE_STATIC_MUTEX(gFailureLock);
scroggo0ee26272014-11-07 06:07:32 -080033
34void JsonWriter::AddTestFailure(const skiatest::Failure& failure) {
35 SkAutoMutexAcquire lock(gFailureLock);
36 gFailures.push_back(failure);
37}
38
scroggo7a10fb62014-11-04 07:21:10 -080039void JsonWriter::DumpJson() {
40 if (FLAGS_writePath.isEmpty()) {
41 return;
42 }
43
Brian Osmanaae63882019-01-25 10:02:27 -050044 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json");
45 sk_mkdir(FLAGS_writePath[0]);
46 SkFILEWStream stream(path.c_str());
47 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
48
49 writer.beginObject(); // root
scroggo7a10fb62014-11-04 07:21:10 -080050
51 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osmanaae63882019-01-25 10:02:27 -050052 writer.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
scroggo7a10fb62014-11-04 07:21:10 -080053 }
Brian Osmanaae63882019-01-25 10:02:27 -050054
55 writer.beginObject("key");
scroggo7a10fb62014-11-04 07:21:10 -080056 for (int i = 1; i < FLAGS_key.count(); i += 2) {
Brian Osmanaae63882019-01-25 10:02:27 -050057 writer.appendString(FLAGS_key[i-1], FLAGS_key[i]);
58 }
59 writer.endObject();
60
61 int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
62 if (maxResidentSetSizeMB != -1) {
63 writer.appendS32("max_rss_MB", maxResidentSetSizeMB);
scroggo7a10fb62014-11-04 07:21:10 -080064 }
65
66 {
67 SkAutoMutexAcquire lock(&gBitmapResultLock);
Brian Osmanaae63882019-01-25 10:02:27 -050068 writer.beginArray("results");
scroggo7a10fb62014-11-04 07:21:10 -080069 for (int i = 0; i < gBitmapResults.count(); i++) {
Brian Osmanaae63882019-01-25 10:02:27 -050070 writer.beginObject();
71
72 writer.beginObject("key");
73 writer.appendString("name" , gBitmapResults[i].name.c_str());
74 writer.appendString("config" , gBitmapResults[i].config.c_str());
75 writer.appendString("source_type", gBitmapResults[i].sourceType.c_str());
mtklein20c1c042015-04-06 07:22:05 -070076
77 // Source options only need to be part of the key if they exist.
78 // Source type by source type, we either always set options or never set options.
79 if (!gBitmapResults[i].sourceOptions.isEmpty()) {
Brian Osmanaae63882019-01-25 10:02:27 -050080 writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str());
mtklein20c1c042015-04-06 07:22:05 -070081 }
Brian Osmanaae63882019-01-25 10:02:27 -050082 writer.endObject(); // key
scroggo7a10fb62014-11-04 07:21:10 -080083
Brian Osmanaae63882019-01-25 10:02:27 -050084 writer.beginObject("options");
85 writer.appendString("ext" , gBitmapResults[i].ext.c_str());
86 writer.appendString("gamma_correct", gBitmapResults[i].gammaCorrect ? "yes" : "no");
87 writer.endObject(); // options
88
89 writer.appendString("md5", gBitmapResults[i].md5.c_str());
90
91 writer.endObject(); // 1 result
scroggo7a10fb62014-11-04 07:21:10 -080092 }
Brian Osmanaae63882019-01-25 10:02:27 -050093 writer.endArray(); // results
scroggo7a10fb62014-11-04 07:21:10 -080094 }
95
scroggo0ee26272014-11-07 06:07:32 -080096 {
97 SkAutoMutexAcquire lock(gFailureLock);
Brian Osmanaae63882019-01-25 10:02:27 -050098 if (gFailures.count() > 0) {
99 writer.beginObject("test_results");
100 writer.beginArray("failures");
101 for (int i = 0; i < gFailures.count(); i++) {
102 writer.beginObject();
103 writer.appendString("file_name", gFailures[i].fileName);
104 writer.appendS32 ("line_no" , gFailures[i].lineNo);
105 writer.appendString("condition", gFailures[i].condition);
106 writer.appendString("message" , gFailures[i].message.c_str());
107 writer.endObject(); // 1 failure
108 }
109 writer.endArray(); // failures
110 writer.endObject(); // test_results
scroggo0ee26272014-11-07 06:07:32 -0800111 }
112 }
113
Brian Osmanaae63882019-01-25 10:02:27 -0500114 writer.endObject(); // root
115 writer.flush();
scroggo7a10fb62014-11-04 07:21:10 -0800116 stream.flush();
117}
118
Brian Osmanfad5c772019-01-25 16:34:20 -0500119using namespace skjson;
120
Brian Osman57796b32019-01-25 18:02:59 +0000121bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
122 sk_sp<SkData> json(SkData::MakeFromFileName(path));
123 if (!json) {
124 return false;
125 }
126
Brian Osmanfad5c772019-01-25 16:34:20 -0500127 DOM dom((const char*)json->data(), json->size());
128 const ObjectValue* root = dom.root();
129 if (!root) {
Brian Osman57796b32019-01-25 18:02:59 +0000130 return false;
131 }
132
Brian Osmanfad5c772019-01-25 16:34:20 -0500133 const ArrayValue* results = (*root)["results"];
134 if (!results) {
135 return false;
136 }
Brian Osman57796b32019-01-25 18:02:59 +0000137
Brian Osmanfad5c772019-01-25 16:34:20 -0500138 BitmapResult br;
139 for (const ObjectValue* r : *results) {
140 const ObjectValue& key = (*r)["key"].as<ObjectValue>();
141 const ObjectValue& options = (*r)["options"].as<ObjectValue>();
142
143 br.name = key["name"].as<StringValue>().begin();
144 br.config = key["config"].as<StringValue>().begin();
145 br.sourceType = key["source_type"].as<StringValue>().begin();
146 br.ext = options["ext"].as<StringValue>().begin();
147 br.gammaCorrect = 0 == strcmp("yes", options["gamma_correct"].as<StringValue>().begin());
148 br.md5 = (*r)["md5"].as<StringValue>().begin();
149
150 if (const StringValue* so = key["source_options"]) {
151 br.sourceOptions = so->begin();
Brian Osman57796b32019-01-25 18:02:59 +0000152 }
153 callback(br);
154 }
155 return true;
156}
157
scroggo7a10fb62014-11-04 07:21:10 -0800158} // namespace DM