1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2014 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "dm/DMJsonWriter.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/core/SkData.h"
11cb93a386Sopenharmony_ci#include "include/core/SkStream.h"
12cb93a386Sopenharmony_ci#include "include/private/SkMutex.h"
13cb93a386Sopenharmony_ci#include "include/private/SkTArray.h"
14cb93a386Sopenharmony_ci#include "src/core/SkOSFile.h"
15cb93a386Sopenharmony_ci#include "src/utils/SkJSON.h"
16cb93a386Sopenharmony_ci#include "src/utils/SkJSONWriter.h"
17cb93a386Sopenharmony_ci#include "src/utils/SkOSPath.h"
18cb93a386Sopenharmony_ci#include "tools/ProcStats.h"
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_cinamespace DM {
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ciSkTArray<JsonWriter::BitmapResult> gBitmapResults;
23cb93a386Sopenharmony_cistatic SkMutex& bitmap_result_mutex() {
24cb93a386Sopenharmony_ci    static SkMutex& mutex = *(new SkMutex);
25cb93a386Sopenharmony_ci    return mutex;
26cb93a386Sopenharmony_ci}
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_civoid JsonWriter::AddBitmapResult(const BitmapResult& result) {
30cb93a386Sopenharmony_ci    SkAutoMutexExclusive lock(bitmap_result_mutex());
31cb93a386Sopenharmony_ci    gBitmapResults.push_back(result);
32cb93a386Sopenharmony_ci}
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_civoid JsonWriter::DumpJson(const char* dir,
35cb93a386Sopenharmony_ci                          CommandLineFlags::StringArray key,
36cb93a386Sopenharmony_ci                          CommandLineFlags::StringArray properties) {
37cb93a386Sopenharmony_ci    if (0 == strcmp(dir, "")) {
38cb93a386Sopenharmony_ci        return;
39cb93a386Sopenharmony_ci    }
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    SkString path = SkOSPath::Join(dir, "dm.json");
42cb93a386Sopenharmony_ci    sk_mkdir(dir);
43cb93a386Sopenharmony_ci    SkFILEWStream stream(path.c_str());
44cb93a386Sopenharmony_ci    SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    writer.beginObject(); // root
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    for (int i = 1; i < properties.count(); i += 2) {
49cb93a386Sopenharmony_ci        writer.appendString(properties[i-1], properties[i]);
50cb93a386Sopenharmony_ci    }
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    writer.beginObject("key");
53cb93a386Sopenharmony_ci    for (int i = 1; i < key.count(); i += 2) {
54cb93a386Sopenharmony_ci        writer.appendString(key[i-1], key[i]);
55cb93a386Sopenharmony_ci    }
56cb93a386Sopenharmony_ci    writer.endObject();
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
59cb93a386Sopenharmony_ci    if (maxResidentSetSizeMB != -1) {
60cb93a386Sopenharmony_ci        writer.appendS32("max_rss_MB", maxResidentSetSizeMB);
61cb93a386Sopenharmony_ci    }
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    {
64cb93a386Sopenharmony_ci        SkAutoMutexExclusive lock(bitmap_result_mutex());
65cb93a386Sopenharmony_ci        writer.beginArray("results");
66cb93a386Sopenharmony_ci        for (int i = 0; i < gBitmapResults.count(); i++) {
67cb93a386Sopenharmony_ci            writer.beginObject();
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci            writer.beginObject("key");
70cb93a386Sopenharmony_ci            writer.appendString("name"       , gBitmapResults[i].name.c_str());
71cb93a386Sopenharmony_ci            writer.appendString("config"     , gBitmapResults[i].config.c_str());
72cb93a386Sopenharmony_ci            writer.appendString("source_type", gBitmapResults[i].sourceType.c_str());
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ci            // Source options only need to be part of the key if they exist.
75cb93a386Sopenharmony_ci            // Source type by source type, we either always set options or never set options.
76cb93a386Sopenharmony_ci            if (!gBitmapResults[i].sourceOptions.isEmpty()) {
77cb93a386Sopenharmony_ci                writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str());
78cb93a386Sopenharmony_ci            }
79cb93a386Sopenharmony_ci            writer.endObject(); // key
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci            writer.beginObject("options");
82cb93a386Sopenharmony_ci            writer.appendString("ext"  ,       gBitmapResults[i].ext.c_str());
83cb93a386Sopenharmony_ci            writer.appendString("gamut",       gBitmapResults[i].gamut.c_str());
84cb93a386Sopenharmony_ci            writer.appendString("transfer_fn", gBitmapResults[i].transferFn.c_str());
85cb93a386Sopenharmony_ci            writer.appendString("color_type",  gBitmapResults[i].colorType.c_str());
86cb93a386Sopenharmony_ci            writer.appendString("alpha_type",  gBitmapResults[i].alphaType.c_str());
87cb93a386Sopenharmony_ci            writer.appendString("color_depth", gBitmapResults[i].colorDepth.c_str());
88cb93a386Sopenharmony_ci            writer.endObject(); // options
89cb93a386Sopenharmony_ci
90cb93a386Sopenharmony_ci            writer.appendString("md5", gBitmapResults[i].md5.c_str());
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_ci            writer.endObject(); // 1 result
93cb93a386Sopenharmony_ci        }
94cb93a386Sopenharmony_ci        writer.endArray(); // results
95cb93a386Sopenharmony_ci    }
96cb93a386Sopenharmony_ci
97cb93a386Sopenharmony_ci    writer.endObject(); // root
98cb93a386Sopenharmony_ci    writer.flush();
99cb93a386Sopenharmony_ci    stream.flush();
100cb93a386Sopenharmony_ci}
101cb93a386Sopenharmony_ci
102cb93a386Sopenharmony_ciusing namespace skjson;
103cb93a386Sopenharmony_ci
104cb93a386Sopenharmony_cibool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
105cb93a386Sopenharmony_ci    sk_sp<SkData> json(SkData::MakeFromFileName(path));
106cb93a386Sopenharmony_ci    if (!json) {
107cb93a386Sopenharmony_ci        return false;
108cb93a386Sopenharmony_ci    }
109cb93a386Sopenharmony_ci
110cb93a386Sopenharmony_ci    DOM dom((const char*)json->data(), json->size());
111cb93a386Sopenharmony_ci    const ObjectValue* root = dom.root();
112cb93a386Sopenharmony_ci    if (!root) {
113cb93a386Sopenharmony_ci        return false;
114cb93a386Sopenharmony_ci    }
115cb93a386Sopenharmony_ci
116cb93a386Sopenharmony_ci    const ArrayValue* results = (*root)["results"];
117cb93a386Sopenharmony_ci    if (!results) {
118cb93a386Sopenharmony_ci        return false;
119cb93a386Sopenharmony_ci    }
120cb93a386Sopenharmony_ci
121cb93a386Sopenharmony_ci    BitmapResult br;
122cb93a386Sopenharmony_ci    for (const ObjectValue* r : *results) {
123cb93a386Sopenharmony_ci        const ObjectValue& key = (*r)["key"].as<ObjectValue>();
124cb93a386Sopenharmony_ci        const ObjectValue& options = (*r)["options"].as<ObjectValue>();
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci        br.name         = key["name"].as<StringValue>().begin();
127cb93a386Sopenharmony_ci        br.config       = key["config"].as<StringValue>().begin();
128cb93a386Sopenharmony_ci        br.sourceType   = key["source_type"].as<StringValue>().begin();
129cb93a386Sopenharmony_ci        br.ext          = options["ext"].as<StringValue>().begin();
130cb93a386Sopenharmony_ci        br.gamut        = options["gamut"].as<StringValue>().begin();
131cb93a386Sopenharmony_ci        br.transferFn   = options["transfer_fn"].as<StringValue>().begin();
132cb93a386Sopenharmony_ci        br.colorType    = options["color_type"].as<StringValue>().begin();
133cb93a386Sopenharmony_ci        br.alphaType    = options["alpha_type"].as<StringValue>().begin();
134cb93a386Sopenharmony_ci        br.colorDepth   = options["color_depth"].as<StringValue>().begin();
135cb93a386Sopenharmony_ci        br.md5          = (*r)["md5"].as<StringValue>().begin();
136cb93a386Sopenharmony_ci
137cb93a386Sopenharmony_ci        if (const StringValue* so = key["source_options"]) {
138cb93a386Sopenharmony_ci            br.sourceOptions = so->begin();
139cb93a386Sopenharmony_ci        }
140cb93a386Sopenharmony_ci        callback(br);
141cb93a386Sopenharmony_ci    }
142cb93a386Sopenharmony_ci    return true;
143cb93a386Sopenharmony_ci}
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ci} // namespace DM
146