2014-11-04 15:21:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "dm/DMJsonWriter.h"
|
2014-11-04 15:21:10 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/private/SkMutex.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/utils/SkJSON.h"
|
|
|
|
#include "src/utils/SkJSONWriter.h"
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
#include "tools/ProcStats.h"
|
2014-11-04 15:21:10 +00:00
|
|
|
|
|
|
|
namespace DM {
|
|
|
|
|
|
|
|
SkTArray<JsonWriter::BitmapResult> gBitmapResults;
|
2019-06-17 18:40:42 +00:00
|
|
|
static SkMutex& bitmap_result_mutex() {
|
|
|
|
static SkMutex& mutex = *(new SkMutex);
|
|
|
|
return mutex;
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:21:10 +00:00
|
|
|
|
|
|
|
void JsonWriter::AddBitmapResult(const BitmapResult& result) {
|
2019-06-17 18:40:42 +00:00
|
|
|
SkAutoMutexExclusive lock(bitmap_result_mutex());
|
2014-11-04 15:21:10 +00:00
|
|
|
gBitmapResults.push_back(result);
|
|
|
|
}
|
|
|
|
|
2019-03-25 15:54:59 +00:00
|
|
|
void JsonWriter::DumpJson(const char* dir,
|
|
|
|
CommandLineFlags::StringArray key,
|
|
|
|
CommandLineFlags::StringArray properties) {
|
|
|
|
if (0 == strcmp(dir, "")) {
|
2014-11-04 15:21:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-25 15:54:59 +00:00
|
|
|
SkString path = SkOSPath::Join(dir, "dm.json");
|
|
|
|
sk_mkdir(dir);
|
2019-01-25 15:02:27 +00:00
|
|
|
SkFILEWStream stream(path.c_str());
|
|
|
|
SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
|
|
|
|
|
|
|
|
writer.beginObject(); // root
|
2014-11-04 15:21:10 +00:00
|
|
|
|
2019-03-25 15:54:59 +00:00
|
|
|
for (int i = 1; i < properties.count(); i += 2) {
|
|
|
|
writer.appendString(properties[i-1], properties[i]);
|
2014-11-04 15:21:10 +00:00
|
|
|
}
|
2019-01-25 15:02:27 +00:00
|
|
|
|
|
|
|
writer.beginObject("key");
|
2019-03-25 15:54:59 +00:00
|
|
|
for (int i = 1; i < key.count(); i += 2) {
|
|
|
|
writer.appendString(key[i-1], key[i]);
|
2019-01-25 15:02:27 +00:00
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
|
|
|
|
int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
|
|
|
|
if (maxResidentSetSizeMB != -1) {
|
|
|
|
writer.appendS32("max_rss_MB", maxResidentSetSizeMB);
|
2014-11-04 15:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-06-17 18:40:42 +00:00
|
|
|
SkAutoMutexExclusive lock(bitmap_result_mutex());
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.beginArray("results");
|
2014-11-04 15:21:10 +00:00
|
|
|
for (int i = 0; i < gBitmapResults.count(); i++) {
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.beginObject();
|
|
|
|
|
|
|
|
writer.beginObject("key");
|
|
|
|
writer.appendString("name" , gBitmapResults[i].name.c_str());
|
|
|
|
writer.appendString("config" , gBitmapResults[i].config.c_str());
|
|
|
|
writer.appendString("source_type", gBitmapResults[i].sourceType.c_str());
|
2015-04-06 14:22:05 +00:00
|
|
|
|
|
|
|
// Source options only need to be part of the key if they exist.
|
|
|
|
// Source type by source type, we either always set options or never set options.
|
|
|
|
if (!gBitmapResults[i].sourceOptions.isEmpty()) {
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str());
|
2015-04-06 14:22:05 +00:00
|
|
|
}
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.endObject(); // key
|
2014-11-04 15:21:10 +00:00
|
|
|
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.beginObject("options");
|
2019-02-12 18:03:54 +00:00
|
|
|
writer.appendString("ext" , gBitmapResults[i].ext.c_str());
|
|
|
|
writer.appendString("gamut", gBitmapResults[i].gamut.c_str());
|
|
|
|
writer.appendString("transfer_fn", gBitmapResults[i].transferFn.c_str());
|
2019-03-06 15:57:49 +00:00
|
|
|
writer.appendString("color_type", gBitmapResults[i].colorType.c_str());
|
|
|
|
writer.appendString("alpha_type", gBitmapResults[i].alphaType.c_str());
|
2019-03-07 20:11:16 +00:00
|
|
|
writer.appendString("color_depth", gBitmapResults[i].colorDepth.c_str());
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.endObject(); // options
|
|
|
|
|
|
|
|
writer.appendString("md5", gBitmapResults[i].md5.c_str());
|
|
|
|
|
|
|
|
writer.endObject(); // 1 result
|
2014-11-04 15:21:10 +00:00
|
|
|
}
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.endArray(); // results
|
2014-11-04 15:21:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 15:02:27 +00:00
|
|
|
writer.endObject(); // root
|
|
|
|
writer.flush();
|
2014-11-04 15:21:10 +00:00
|
|
|
stream.flush();
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:34:20 +00:00
|
|
|
using namespace skjson;
|
|
|
|
|
2019-01-25 18:02:59 +00:00
|
|
|
bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
|
|
|
|
sk_sp<SkData> json(SkData::MakeFromFileName(path));
|
|
|
|
if (!json) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:34:20 +00:00
|
|
|
DOM dom((const char*)json->data(), json->size());
|
|
|
|
const ObjectValue* root = dom.root();
|
|
|
|
if (!root) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ArrayValue* results = (*root)["results"];
|
|
|
|
if (!results) {
|
2019-01-25 18:02:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitmapResult br;
|
2019-01-25 21:34:20 +00:00
|
|
|
for (const ObjectValue* r : *results) {
|
|
|
|
const ObjectValue& key = (*r)["key"].as<ObjectValue>();
|
|
|
|
const ObjectValue& options = (*r)["options"].as<ObjectValue>();
|
|
|
|
|
|
|
|
br.name = key["name"].as<StringValue>().begin();
|
|
|
|
br.config = key["config"].as<StringValue>().begin();
|
|
|
|
br.sourceType = key["source_type"].as<StringValue>().begin();
|
|
|
|
br.ext = options["ext"].as<StringValue>().begin();
|
2019-02-12 18:03:54 +00:00
|
|
|
br.gamut = options["gamut"].as<StringValue>().begin();
|
|
|
|
br.transferFn = options["transfer_fn"].as<StringValue>().begin();
|
2019-03-06 15:57:49 +00:00
|
|
|
br.colorType = options["color_type"].as<StringValue>().begin();
|
|
|
|
br.alphaType = options["alpha_type"].as<StringValue>().begin();
|
2019-03-07 20:11:16 +00:00
|
|
|
br.colorDepth = options["color_depth"].as<StringValue>().begin();
|
2019-01-25 21:34:20 +00:00
|
|
|
br.md5 = (*r)["md5"].as<StringValue>().begin();
|
|
|
|
|
|
|
|
if (const StringValue* so = key["source_options"]) {
|
|
|
|
br.sourceOptions = so->begin();
|
2019-01-25 18:02:59 +00:00
|
|
|
}
|
|
|
|
callback(br);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:21:10 +00:00
|
|
|
} // namespace DM
|