scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
halcanary | c79a391 | 2015-04-01 13:31:34 -0700 | [diff] [blame] | 10 | #include "ProcStats.h" |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 12 | #include "SkJSON.h" |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 13 | #include "SkJSONWriter.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 14 | #include "SkMutex.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 15 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 16 | #include "SkOSPath.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 17 | #include "SkStream.h" |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 18 | #include "SkTArray.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 19 | |
| 20 | namespace DM { |
| 21 | |
| 22 | SkTArray<JsonWriter::BitmapResult> gBitmapResults; |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 23 | SK_DECLARE_STATIC_MUTEX(gBitmapResultLock); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 24 | |
| 25 | void JsonWriter::AddBitmapResult(const BitmapResult& result) { |
| 26 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
| 27 | gBitmapResults.push_back(result); |
| 28 | } |
| 29 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 30 | void JsonWriter::DumpJson(const char* dir, |
| 31 | CommandLineFlags::StringArray key, |
| 32 | CommandLineFlags::StringArray properties) { |
| 33 | if (0 == strcmp(dir, "")) { |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 37 | SkString path = SkOSPath::Join(dir, "dm.json"); |
| 38 | sk_mkdir(dir); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 39 | SkFILEWStream stream(path.c_str()); |
| 40 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 41 | |
| 42 | writer.beginObject(); // root |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 43 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 44 | for (int i = 1; i < properties.count(); i += 2) { |
| 45 | writer.appendString(properties[i-1], properties[i]); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 46 | } |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 47 | |
| 48 | writer.beginObject("key"); |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 49 | for (int i = 1; i < key.count(); i += 2) { |
| 50 | writer.appendString(key[i-1], key[i]); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 51 | } |
| 52 | writer.endObject(); |
| 53 | |
| 54 | int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB(); |
| 55 | if (maxResidentSetSizeMB != -1) { |
| 56 | writer.appendS32("max_rss_MB", maxResidentSetSizeMB); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | { |
| 60 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 61 | writer.beginArray("results"); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 62 | for (int i = 0; i < gBitmapResults.count(); i++) { |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 63 | 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()); |
mtklein | 20c1c04 | 2015-04-06 07:22:05 -0700 | [diff] [blame] | 69 | |
| 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 Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 73 | writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str()); |
mtklein | 20c1c04 | 2015-04-06 07:22:05 -0700 | [diff] [blame] | 74 | } |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 75 | writer.endObject(); // key |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 76 | |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 77 | writer.beginObject("options"); |
Mike Klein | 66f09a7 | 2019-02-12 13:03:54 -0500 | [diff] [blame] | 78 | 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 Klein | 0abbaca | 2019-03-06 09:57:49 -0600 | [diff] [blame] | 81 | writer.appendString("color_type", gBitmapResults[i].colorType.c_str()); |
| 82 | writer.appendString("alpha_type", gBitmapResults[i].alphaType.c_str()); |
Mike Klein | 82020c2 | 2019-03-07 14:11:16 -0600 | [diff] [blame] | 83 | writer.appendString("color_depth", gBitmapResults[i].colorDepth.c_str()); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 84 | writer.endObject(); // options |
| 85 | |
| 86 | writer.appendString("md5", gBitmapResults[i].md5.c_str()); |
| 87 | |
| 88 | writer.endObject(); // 1 result |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 89 | } |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 90 | writer.endArray(); // results |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 93 | writer.endObject(); // root |
| 94 | writer.flush(); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 95 | stream.flush(); |
| 96 | } |
| 97 | |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 98 | using namespace skjson; |
| 99 | |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 100 | bool 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 Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 106 | DOM dom((const char*)json->data(), json->size()); |
| 107 | const ObjectValue* root = dom.root(); |
| 108 | if (!root) { |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 112 | const ArrayValue* results = (*root)["results"]; |
| 113 | if (!results) { |
| 114 | return false; |
| 115 | } |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 116 | |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 117 | 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 Klein | 66f09a7 | 2019-02-12 13:03:54 -0500 | [diff] [blame] | 126 | br.gamut = options["gamut"].as<StringValue>().begin(); |
| 127 | br.transferFn = options["transfer_fn"].as<StringValue>().begin(); |
Mike Klein | 0abbaca | 2019-03-06 09:57:49 -0600 | [diff] [blame] | 128 | br.colorType = options["color_type"].as<StringValue>().begin(); |
| 129 | br.alphaType = options["alpha_type"].as<StringValue>().begin(); |
Mike Klein | 82020c2 | 2019-03-07 14:11:16 -0600 | [diff] [blame] | 130 | br.colorDepth = options["color_depth"].as<StringValue>().begin(); |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 131 | br.md5 = (*r)["md5"].as<StringValue>().begin(); |
| 132 | |
| 133 | if (const StringValue* so = key["source_options"]) { |
| 134 | br.sourceOptions = so->begin(); |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 135 | } |
| 136 | callback(br); |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 141 | } // namespace DM |