blob: 28accddf8dbff9e5b9cc6f4464fcde8e3b7e1a1b [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");
Mike Klein66f09a72019-02-12 13:03:54 -050085 writer.appendString("ext" , gBitmapResults[i].ext.c_str());
86 writer.appendString("gamut", gBitmapResults[i].gamut.c_str());
87 writer.appendString("transfer_fn", gBitmapResults[i].transferFn.c_str());
Brian Osmanaae63882019-01-25 10:02:27 -050088 writer.endObject(); // options
89
90 writer.appendString("md5", gBitmapResults[i].md5.c_str());
91
92 writer.endObject(); // 1 result
scroggo7a10fb62014-11-04 07:21:10 -080093 }
Brian Osmanaae63882019-01-25 10:02:27 -050094 writer.endArray(); // results
scroggo7a10fb62014-11-04 07:21:10 -080095 }
96
scroggo0ee26272014-11-07 06:07:32 -080097 {
98 SkAutoMutexAcquire lock(gFailureLock);
Brian Osmanaae63882019-01-25 10:02:27 -050099 if (gFailures.count() > 0) {
100 writer.beginObject("test_results");
101 writer.beginArray("failures");
102 for (int i = 0; i < gFailures.count(); i++) {
103 writer.beginObject();
104 writer.appendString("file_name", gFailures[i].fileName);
105 writer.appendS32 ("line_no" , gFailures[i].lineNo);
106 writer.appendString("condition", gFailures[i].condition);
107 writer.appendString("message" , gFailures[i].message.c_str());
108 writer.endObject(); // 1 failure
109 }
110 writer.endArray(); // failures
111 writer.endObject(); // test_results
scroggo0ee26272014-11-07 06:07:32 -0800112 }
113 }
114
Brian Osmanaae63882019-01-25 10:02:27 -0500115 writer.endObject(); // root
116 writer.flush();
scroggo7a10fb62014-11-04 07:21:10 -0800117 stream.flush();
118}
119
Brian Osmanfad5c772019-01-25 16:34:20 -0500120using namespace skjson;
121
Brian Osman57796b32019-01-25 18:02:59 +0000122bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
123 sk_sp<SkData> json(SkData::MakeFromFileName(path));
124 if (!json) {
125 return false;
126 }
127
Brian Osmanfad5c772019-01-25 16:34:20 -0500128 DOM dom((const char*)json->data(), json->size());
129 const ObjectValue* root = dom.root();
130 if (!root) {
Brian Osman57796b32019-01-25 18:02:59 +0000131 return false;
132 }
133
Brian Osmanfad5c772019-01-25 16:34:20 -0500134 const ArrayValue* results = (*root)["results"];
135 if (!results) {
136 return false;
137 }
Brian Osman57796b32019-01-25 18:02:59 +0000138
Brian Osmanfad5c772019-01-25 16:34:20 -0500139 BitmapResult br;
140 for (const ObjectValue* r : *results) {
141 const ObjectValue& key = (*r)["key"].as<ObjectValue>();
142 const ObjectValue& options = (*r)["options"].as<ObjectValue>();
143
144 br.name = key["name"].as<StringValue>().begin();
145 br.config = key["config"].as<StringValue>().begin();
146 br.sourceType = key["source_type"].as<StringValue>().begin();
147 br.ext = options["ext"].as<StringValue>().begin();
Mike Klein66f09a72019-02-12 13:03:54 -0500148 br.gamut = options["gamut"].as<StringValue>().begin();
149 br.transferFn = options["transfer_fn"].as<StringValue>().begin();
Brian Osmanfad5c772019-01-25 16:34:20 -0500150 br.md5 = (*r)["md5"].as<StringValue>().begin();
151
152 if (const StringValue* so = key["source_options"]) {
153 br.sourceOptions = so->begin();
Brian Osman57796b32019-01-25 18:02:59 +0000154 }
155 callback(br);
156 }
157 return true;
158}
159
scroggo7a10fb62014-11-04 07:21:10 -0800160} // namespace DM