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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "dm/DMJsonWriter.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #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" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 19 | |
| 20 | namespace DM { |
| 21 | |
| 22 | SkTArray<JsonWriter::BitmapResult> gBitmapResults; |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 23 | static SkMutex& bitmap_result_mutex() { |
| 24 | static SkMutex& mutex = *(new SkMutex); |
| 25 | return mutex; |
| 26 | } |
| 27 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 28 | |
| 29 | void JsonWriter::AddBitmapResult(const BitmapResult& result) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 30 | SkAutoMutexExclusive lock(bitmap_result_mutex()); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 31 | gBitmapResults.push_back(result); |
| 32 | } |
| 33 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 34 | void JsonWriter::DumpJson(const char* dir, |
| 35 | CommandLineFlags::StringArray key, |
| 36 | CommandLineFlags::StringArray properties) { |
| 37 | if (0 == strcmp(dir, "")) { |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 41 | SkString path = SkOSPath::Join(dir, "dm.json"); |
| 42 | sk_mkdir(dir); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 43 | SkFILEWStream stream(path.c_str()); |
| 44 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 45 | |
| 46 | writer.beginObject(); // root |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 47 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 48 | for (int i = 1; i < properties.count(); i += 2) { |
| 49 | writer.appendString(properties[i-1], properties[i]); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 50 | } |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 51 | |
| 52 | writer.beginObject("key"); |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 53 | for (int i = 1; i < key.count(); i += 2) { |
| 54 | writer.appendString(key[i-1], key[i]); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 55 | } |
| 56 | writer.endObject(); |
| 57 | |
| 58 | int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB(); |
| 59 | if (maxResidentSetSizeMB != -1) { |
| 60 | writer.appendS32("max_rss_MB", maxResidentSetSizeMB); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 64 | SkAutoMutexExclusive lock(bitmap_result_mutex()); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 65 | writer.beginArray("results"); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 66 | for (int i = 0; i < gBitmapResults.count(); i++) { |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 67 | 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()); |
mtklein | 20c1c04 | 2015-04-06 07:22:05 -0700 | [diff] [blame] | 73 | |
| 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 Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 77 | writer.appendString("source_options", gBitmapResults[i].sourceOptions.c_str()); |
mtklein | 20c1c04 | 2015-04-06 07:22:05 -0700 | [diff] [blame] | 78 | } |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 79 | writer.endObject(); // key |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 80 | |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 81 | writer.beginObject("options"); |
Mike Klein | 66f09a7 | 2019-02-12 13:03:54 -0500 | [diff] [blame] | 82 | 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 Klein | 0abbaca | 2019-03-06 09:57:49 -0600 | [diff] [blame] | 85 | writer.appendString("color_type", gBitmapResults[i].colorType.c_str()); |
| 86 | writer.appendString("alpha_type", gBitmapResults[i].alphaType.c_str()); |
Mike Klein | 82020c2 | 2019-03-07 14:11:16 -0600 | [diff] [blame] | 87 | writer.appendString("color_depth", gBitmapResults[i].colorDepth.c_str()); |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 88 | writer.endObject(); // options |
| 89 | |
| 90 | writer.appendString("md5", gBitmapResults[i].md5.c_str()); |
| 91 | |
| 92 | writer.endObject(); // 1 result |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 93 | } |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 94 | writer.endArray(); // results |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Brian Osman | aae6388 | 2019-01-25 10:02:27 -0500 | [diff] [blame] | 97 | writer.endObject(); // root |
| 98 | writer.flush(); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 99 | stream.flush(); |
| 100 | } |
| 101 | |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 102 | using namespace skjson; |
| 103 | |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 104 | bool 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 Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 110 | DOM dom((const char*)json->data(), json->size()); |
| 111 | const ObjectValue* root = dom.root(); |
| 112 | if (!root) { |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 116 | const ArrayValue* results = (*root)["results"]; |
| 117 | if (!results) { |
| 118 | return false; |
| 119 | } |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 120 | |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 121 | 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 Klein | 66f09a7 | 2019-02-12 13:03:54 -0500 | [diff] [blame] | 130 | br.gamut = options["gamut"].as<StringValue>().begin(); |
| 131 | br.transferFn = options["transfer_fn"].as<StringValue>().begin(); |
Mike Klein | 0abbaca | 2019-03-06 09:57:49 -0600 | [diff] [blame] | 132 | br.colorType = options["color_type"].as<StringValue>().begin(); |
| 133 | br.alphaType = options["alpha_type"].as<StringValue>().begin(); |
Mike Klein | 82020c2 | 2019-03-07 14:11:16 -0600 | [diff] [blame] | 134 | br.colorDepth = options["color_depth"].as<StringValue>().begin(); |
Brian Osman | fad5c77 | 2019-01-25 16:34:20 -0500 | [diff] [blame] | 135 | br.md5 = (*r)["md5"].as<StringValue>().begin(); |
| 136 | |
| 137 | if (const StringValue* so = key["source_options"]) { |
| 138 | br.sourceOptions = so->begin(); |
Brian Osman | 57796b3 | 2019-01-25 18:02:59 +0000 | [diff] [blame] | 139 | } |
| 140 | callback(br); |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 145 | } // namespace DM |