blob: 18796668bc3e894e147c856ec0dd541a1821e89c [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"
Brian Osman57796b32019-01-25 18:02:59 +000011#include "SkData.h"
Brian Osmanfad5c772019-01-25 16:34:20 -050012#include "SkJSON.h"
Brian Osmanaae63882019-01-25 10:02:27 -050013#include "SkJSONWriter.h"
mtklein1b249332015-07-07 12:21:21 -070014#include "SkMutex.h"
scroggo7a10fb62014-11-04 07:21:10 -080015#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050016#include "SkOSPath.h"
scroggo7a10fb62014-11-04 07:21:10 -080017#include "SkStream.h"
Brian Osman57796b32019-01-25 18:02:59 +000018#include "SkTArray.h"
scroggo7a10fb62014-11-04 07:21:10 -080019
20namespace DM {
21
22SkTArray<JsonWriter::BitmapResult> gBitmapResults;
reed086eea92016-05-04 17:12:46 -070023SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
scroggo7a10fb62014-11-04 07:21:10 -080024
25void JsonWriter::AddBitmapResult(const BitmapResult& result) {
26 SkAutoMutexAcquire lock(&gBitmapResultLock);
27 gBitmapResults.push_back(result);
28}
29
Mike Kleinc6142d82019-03-25 10:54:59 -050030void JsonWriter::DumpJson(const char* dir,
31 CommandLineFlags::StringArray key,
32 CommandLineFlags::StringArray properties) {
33 if (0 == strcmp(dir, "")) {
scroggo7a10fb62014-11-04 07:21:10 -080034 return;
35 }
36
Mike Kleinc6142d82019-03-25 10:54:59 -050037 SkString path = SkOSPath::Join(dir, "dm.json");
38 sk_mkdir(dir);
Brian Osmanaae63882019-01-25 10:02:27 -050039 SkFILEWStream stream(path.c_str());
40 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
41
42 writer.beginObject(); // root
scroggo7a10fb62014-11-04 07:21:10 -080043
Mike Kleinc6142d82019-03-25 10:54:59 -050044 for (int i = 1; i < properties.count(); i += 2) {
45 writer.appendString(properties[i-1], properties[i]);
scroggo7a10fb62014-11-04 07:21:10 -080046 }
Brian Osmanaae63882019-01-25 10:02:27 -050047
48 writer.beginObject("key");
Mike Kleinc6142d82019-03-25 10:54:59 -050049 for (int i = 1; i < key.count(); i += 2) {
50 writer.appendString(key[i-1], key[i]);
Brian Osmanaae63882019-01-25 10:02:27 -050051 }
52 writer.endObject();
53
54 int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
55 if (maxResidentSetSizeMB != -1) {
56 writer.appendS32("max_rss_MB", maxResidentSetSizeMB);
scroggo7a10fb62014-11-04 07:21:10 -080057 }
58
59 {
60 SkAutoMutexAcquire lock(&gBitmapResultLock);
Brian Osmanaae63882019-01-25 10:02:27 -050061 writer.beginArray("results");
scroggo7a10fb62014-11-04 07:21:10 -080062 for (int i = 0; i < gBitmapResults.count(); i++) {
Brian Osmanaae63882019-01-25 10:02:27 -050063 writer.beginObject();
64
65 writer.beginObject("key");
66 writer.appendString("name" , gBitmapResults[i].name.c_str());
67 writer.appendString("config" , gBitmapResults[i].config.c_str());
68 writer.appendString("source_type", gBitmapResults[i].sourceType.c_str());
mtklein20c1c042015-04-06 07:22:05 -070069
70 // Source options only need to be part of the key if they exist.
71 // Source type by source type, we either always set options or never set options.
72 if (!gBitmapResults[i].sourceOptions.isEmpty()) {
Brian Osmanaae63882019-01-25 10:02:27 -050073 writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str());
mtklein20c1c042015-04-06 07:22:05 -070074 }
Brian Osmanaae63882019-01-25 10:02:27 -050075 writer.endObject(); // key
scroggo7a10fb62014-11-04 07:21:10 -080076
Brian Osmanaae63882019-01-25 10:02:27 -050077 writer.beginObject("options");
Mike Klein66f09a72019-02-12 13:03:54 -050078 writer.appendString("ext" , gBitmapResults[i].ext.c_str());
79 writer.appendString("gamut", gBitmapResults[i].gamut.c_str());
80 writer.appendString("transfer_fn", gBitmapResults[i].transferFn.c_str());
Mike Klein0abbaca2019-03-06 09:57:49 -060081 writer.appendString("color_type", gBitmapResults[i].colorType.c_str());
82 writer.appendString("alpha_type", gBitmapResults[i].alphaType.c_str());
Mike Klein82020c22019-03-07 14:11:16 -060083 writer.appendString("color_depth", gBitmapResults[i].colorDepth.c_str());
Brian Osmanaae63882019-01-25 10:02:27 -050084 writer.endObject(); // options
85
86 writer.appendString("md5", gBitmapResults[i].md5.c_str());
87
88 writer.endObject(); // 1 result
scroggo7a10fb62014-11-04 07:21:10 -080089 }
Brian Osmanaae63882019-01-25 10:02:27 -050090 writer.endArray(); // results
scroggo7a10fb62014-11-04 07:21:10 -080091 }
92
Brian Osmanaae63882019-01-25 10:02:27 -050093 writer.endObject(); // root
94 writer.flush();
scroggo7a10fb62014-11-04 07:21:10 -080095 stream.flush();
96}
97
Brian Osmanfad5c772019-01-25 16:34:20 -050098using namespace skjson;
99
Brian Osman57796b32019-01-25 18:02:59 +0000100bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
101 sk_sp<SkData> json(SkData::MakeFromFileName(path));
102 if (!json) {
103 return false;
104 }
105
Brian Osmanfad5c772019-01-25 16:34:20 -0500106 DOM dom((const char*)json->data(), json->size());
107 const ObjectValue* root = dom.root();
108 if (!root) {
Brian Osman57796b32019-01-25 18:02:59 +0000109 return false;
110 }
111
Brian Osmanfad5c772019-01-25 16:34:20 -0500112 const ArrayValue* results = (*root)["results"];
113 if (!results) {
114 return false;
115 }
Brian Osman57796b32019-01-25 18:02:59 +0000116
Brian Osmanfad5c772019-01-25 16:34:20 -0500117 BitmapResult br;
118 for (const ObjectValue* r : *results) {
119 const ObjectValue& key = (*r)["key"].as<ObjectValue>();
120 const ObjectValue& options = (*r)["options"].as<ObjectValue>();
121
122 br.name = key["name"].as<StringValue>().begin();
123 br.config = key["config"].as<StringValue>().begin();
124 br.sourceType = key["source_type"].as<StringValue>().begin();
125 br.ext = options["ext"].as<StringValue>().begin();
Mike Klein66f09a72019-02-12 13:03:54 -0500126 br.gamut = options["gamut"].as<StringValue>().begin();
127 br.transferFn = options["transfer_fn"].as<StringValue>().begin();
Mike Klein0abbaca2019-03-06 09:57:49 -0600128 br.colorType = options["color_type"].as<StringValue>().begin();
129 br.alphaType = options["alpha_type"].as<StringValue>().begin();
Mike Klein82020c22019-03-07 14:11:16 -0600130 br.colorDepth = options["color_depth"].as<StringValue>().begin();
Brian Osmanfad5c772019-01-25 16:34:20 -0500131 br.md5 = (*r)["md5"].as<StringValue>().begin();
132
133 if (const StringValue* so = key["source_options"]) {
134 br.sourceOptions = so->begin();
Brian Osman57796b32019-01-25 18:02:59 +0000135 }
136 callback(br);
137 }
138 return true;
139}
140
scroggo7a10fb62014-11-04 07:21:10 -0800141} // namespace DM