blob: f0d6a8d1cc431e757178547f5e78ae075bc60a13 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "dm/DMJsonWriter.h"
scroggo7a10fb62014-11-04 07:21:10 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkData.h"
11#include "include/core/SkStream.h"
12#include "include/private/SkMutex.h"
13#include "include/private/SkTArray.h"
14#include "src/core/SkOSFile.h"
15#include "src/utils/SkJSON.h"
16#include "src/utils/SkJSONWriter.h"
17#include "src/utils/SkOSPath.h"
18#include "tools/ProcStats.h"
scroggo7a10fb62014-11-04 07:21:10 -080019
20namespace DM {
21
22SkTArray<JsonWriter::BitmapResult> gBitmapResults;
Herb Derby9c71e7b2019-06-17 14:40:42 -040023static SkMutex& bitmap_result_mutex() {
24 static SkMutex& mutex = *(new SkMutex);
25 return mutex;
26}
27
scroggo7a10fb62014-11-04 07:21:10 -080028
29void JsonWriter::AddBitmapResult(const BitmapResult& result) {
Herb Derby9c71e7b2019-06-17 14:40:42 -040030 SkAutoMutexExclusive lock(bitmap_result_mutex());
scroggo7a10fb62014-11-04 07:21:10 -080031 gBitmapResults.push_back(result);
32}
33
Mike Kleinc6142d82019-03-25 10:54:59 -050034void JsonWriter::DumpJson(const char* dir,
35 CommandLineFlags::StringArray key,
36 CommandLineFlags::StringArray properties) {
37 if (0 == strcmp(dir, "")) {
scroggo7a10fb62014-11-04 07:21:10 -080038 return;
39 }
40
Mike Kleinc6142d82019-03-25 10:54:59 -050041 SkString path = SkOSPath::Join(dir, "dm.json");
42 sk_mkdir(dir);
Brian Osmanaae63882019-01-25 10:02:27 -050043 SkFILEWStream stream(path.c_str());
44 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
45
46 writer.beginObject(); // root
scroggo7a10fb62014-11-04 07:21:10 -080047
Mike Kleinc6142d82019-03-25 10:54:59 -050048 for (int i = 1; i < properties.count(); i += 2) {
49 writer.appendString(properties[i-1], properties[i]);
scroggo7a10fb62014-11-04 07:21:10 -080050 }
Brian Osmanaae63882019-01-25 10:02:27 -050051
52 writer.beginObject("key");
Mike Kleinc6142d82019-03-25 10:54:59 -050053 for (int i = 1; i < key.count(); i += 2) {
54 writer.appendString(key[i-1], key[i]);
Brian Osmanaae63882019-01-25 10:02:27 -050055 }
56 writer.endObject();
57
58 int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
59 if (maxResidentSetSizeMB != -1) {
60 writer.appendS32("max_rss_MB", maxResidentSetSizeMB);
scroggo7a10fb62014-11-04 07:21:10 -080061 }
62
63 {
Herb Derby9c71e7b2019-06-17 14:40:42 -040064 SkAutoMutexExclusive lock(bitmap_result_mutex());
Brian Osmanaae63882019-01-25 10:02:27 -050065 writer.beginArray("results");
scroggo7a10fb62014-11-04 07:21:10 -080066 for (int i = 0; i < gBitmapResults.count(); i++) {
Brian Osmanaae63882019-01-25 10:02:27 -050067 writer.beginObject();
68
69 writer.beginObject("key");
70 writer.appendString("name" , gBitmapResults[i].name.c_str());
71 writer.appendString("config" , gBitmapResults[i].config.c_str());
72 writer.appendString("source_type", gBitmapResults[i].sourceType.c_str());
mtklein20c1c042015-04-06 07:22:05 -070073
74 // Source options only need to be part of the key if they exist.
75 // Source type by source type, we either always set options or never set options.
76 if (!gBitmapResults[i].sourceOptions.isEmpty()) {
Brian Osmanaae63882019-01-25 10:02:27 -050077 writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str());
mtklein20c1c042015-04-06 07:22:05 -070078 }
Brian Osmanaae63882019-01-25 10:02:27 -050079 writer.endObject(); // key
scroggo7a10fb62014-11-04 07:21:10 -080080
Brian Osmanaae63882019-01-25 10:02:27 -050081 writer.beginObject("options");
Mike Klein66f09a72019-02-12 13:03:54 -050082 writer.appendString("ext" , gBitmapResults[i].ext.c_str());
83 writer.appendString("gamut", gBitmapResults[i].gamut.c_str());
84 writer.appendString("transfer_fn", gBitmapResults[i].transferFn.c_str());
Mike Klein0abbaca2019-03-06 09:57:49 -060085 writer.appendString("color_type", gBitmapResults[i].colorType.c_str());
86 writer.appendString("alpha_type", gBitmapResults[i].alphaType.c_str());
Mike Klein82020c22019-03-07 14:11:16 -060087 writer.appendString("color_depth", gBitmapResults[i].colorDepth.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
Brian Osmanaae63882019-01-25 10:02:27 -050097 writer.endObject(); // root
98 writer.flush();
scroggo7a10fb62014-11-04 07:21:10 -080099 stream.flush();
100}
101
Brian Osmanfad5c772019-01-25 16:34:20 -0500102using namespace skjson;
103
Brian Osman57796b32019-01-25 18:02:59 +0000104bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
105 sk_sp<SkData> json(SkData::MakeFromFileName(path));
106 if (!json) {
107 return false;
108 }
109
Brian Osmanfad5c772019-01-25 16:34:20 -0500110 DOM dom((const char*)json->data(), json->size());
111 const ObjectValue* root = dom.root();
112 if (!root) {
Brian Osman57796b32019-01-25 18:02:59 +0000113 return false;
114 }
115
Brian Osmanfad5c772019-01-25 16:34:20 -0500116 const ArrayValue* results = (*root)["results"];
117 if (!results) {
118 return false;
119 }
Brian Osman57796b32019-01-25 18:02:59 +0000120
Brian Osmanfad5c772019-01-25 16:34:20 -0500121 BitmapResult br;
122 for (const ObjectValue* r : *results) {
123 const ObjectValue& key = (*r)["key"].as<ObjectValue>();
124 const ObjectValue& options = (*r)["options"].as<ObjectValue>();
125
126 br.name = key["name"].as<StringValue>().begin();
127 br.config = key["config"].as<StringValue>().begin();
128 br.sourceType = key["source_type"].as<StringValue>().begin();
129 br.ext = options["ext"].as<StringValue>().begin();
Mike Klein66f09a72019-02-12 13:03:54 -0500130 br.gamut = options["gamut"].as<StringValue>().begin();
131 br.transferFn = options["transfer_fn"].as<StringValue>().begin();
Mike Klein0abbaca2019-03-06 09:57:49 -0600132 br.colorType = options["color_type"].as<StringValue>().begin();
133 br.alphaType = options["alpha_type"].as<StringValue>().begin();
Mike Klein82020c22019-03-07 14:11:16 -0600134 br.colorDepth = options["color_depth"].as<StringValue>().begin();
Brian Osmanfad5c772019-01-25 16:34:20 -0500135 br.md5 = (*r)["md5"].as<StringValue>().begin();
136
137 if (const StringValue* so = key["source_options"]) {
138 br.sourceOptions = so->begin();
Brian Osman57796b32019-01-25 18:02:59 +0000139 }
140 callback(br);
141 }
142 return true;
143}
144
scroggo7a10fb62014-11-04 07:21:10 -0800145} // namespace DM