DMJsonWriter.cpp 4.8 KB

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