blob: 79f308f2ba1dc105f3942d310cea670b3d5383ae [file] [log] [blame]
scroggo7a10fb62014-11-04 07:21:10 -08001/*
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
halcanaryc79a3912015-04-01 13:31:34 -070010#include "ProcStats.h"
scroggo7a10fb62014-11-04 07:21:10 -080011#include "SkCommonFlags.h"
mtklein62bd1a62015-01-27 14:46:26 -080012#include "SkData.h"
scroggo7a10fb62014-11-04 07:21:10 -080013#include "SkJSONCPP.h"
mtklein1b249332015-07-07 12:21:21 -070014#include "SkMutex.h"
scroggo7a10fb62014-11-04 07:21:10 -080015#include "SkOSFile.h"
16#include "SkStream.h"
17#include "SkTArray.h"
scroggo7a10fb62014-11-04 07:21:10 -080018
19namespace DM {
20
21SkTArray<JsonWriter::BitmapResult> gBitmapResults;
reed086eea92016-05-04 17:12:46 -070022SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
scroggo7a10fb62014-11-04 07:21:10 -080023
24void JsonWriter::AddBitmapResult(const BitmapResult& result) {
25 SkAutoMutexAcquire lock(&gBitmapResultLock);
26 gBitmapResults.push_back(result);
27}
28
scroggo0ee26272014-11-07 06:07:32 -080029SkTArray<skiatest::Failure> gFailures;
reed086eea92016-05-04 17:12:46 -070030SK_DECLARE_STATIC_MUTEX(gFailureLock);
scroggo0ee26272014-11-07 06:07:32 -080031
32void JsonWriter::AddTestFailure(const skiatest::Failure& failure) {
33 SkAutoMutexAcquire lock(gFailureLock);
34 gFailures.push_back(failure);
35}
36
scroggo7a10fb62014-11-04 07:21:10 -080037void JsonWriter::DumpJson() {
38 if (FLAGS_writePath.isEmpty()) {
39 return;
40 }
41
42 Json::Value root;
43
44 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
45 root[FLAGS_properties[i-1]] = FLAGS_properties[i];
46 }
47 for (int i = 1; i < FLAGS_key.count(); i += 2) {
48 root["key"][FLAGS_key[i-1]] = FLAGS_key[i];
49 }
50
51 {
52 SkAutoMutexAcquire lock(&gBitmapResultLock);
53 for (int i = 0; i < gBitmapResults.count(); i++) {
54 Json::Value result;
mtklein409d4702016-02-29 07:38:01 -080055 result["key"]["name"] = gBitmapResults[i].name.c_str();
56 result["key"]["config"] = gBitmapResults[i].config.c_str();
57 result["key"]["source_type"] = gBitmapResults[i].sourceType.c_str();
58 result["options"]["ext"] = gBitmapResults[i].ext.c_str();
mtkleinb3f31482016-03-01 10:31:42 -080059 result["options"]["gamma_correct"] = gBitmapResults[i].gammaCorrect ? "yes" : "no";
mtklein409d4702016-02-29 07:38:01 -080060 result["md5"] = gBitmapResults[i].md5.c_str();
mtklein20c1c042015-04-06 07:22:05 -070061
62 // Source options only need to be part of the key if they exist.
63 // Source type by source type, we either always set options or never set options.
64 if (!gBitmapResults[i].sourceOptions.isEmpty()) {
65 result["key"]["source_options"] = gBitmapResults[i].sourceOptions.c_str();
66 }
scroggo7a10fb62014-11-04 07:21:10 -080067
68 root["results"].append(result);
69 }
70 }
71
scroggo0ee26272014-11-07 06:07:32 -080072 {
73 SkAutoMutexAcquire lock(gFailureLock);
74 for (int i = 0; i < gFailures.count(); i++) {
75 Json::Value result;
76 result["file_name"] = gFailures[i].fileName;
77 result["line_no"] = gFailures[i].lineNo;
78 result["condition"] = gFailures[i].condition;
79 result["message"] = gFailures[i].message.c_str();
80
81 root["test_results"]["failures"].append(result);
82 }
83 }
84
halcanaryc79a3912015-04-01 13:31:34 -070085 int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
86 if (maxResidentSetSizeMB != -1) {
87 root["max_rss_MB"] = sk_tools::getMaxResidentSetSizeMB();
88 }
89
scroggo7a10fb62014-11-04 07:21:10 -080090 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json");
halcanary022afb82015-01-30 11:00:12 -080091 sk_mkdir(FLAGS_writePath[0]);
scroggo7a10fb62014-11-04 07:21:10 -080092 SkFILEWStream stream(path.c_str());
93 stream.writeText(Json::StyledWriter().write(root).c_str());
94 stream.flush();
95}
96
mtklein62bd1a62015-01-27 14:46:26 -080097bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
bungeman38d909e2016-08-02 14:40:46 -070098 sk_sp<SkData> json(SkData::MakeFromFileName(path));
mtklein62bd1a62015-01-27 14:46:26 -080099 if (!json) {
100 return false;
101 }
102
103 Json::Reader reader;
104 Json::Value root;
105 const char* data = (const char*)json->data();
106 if (!reader.parse(data, data+json->size(), root)) {
107 return false;
108 }
109
110 const Json::Value& results = root["results"];
111 BitmapResult br;
112 for (unsigned i = 0; i < results.size(); i++) {
113 const Json::Value& r = results[i];
mtklein409d4702016-02-29 07:38:01 -0800114 br.name = r["key"]["name"].asCString();
115 br.config = r["key"]["config"].asCString();
116 br.sourceType = r["key"]["source_type"].asCString();
117 br.ext = r["options"]["ext"].asCString();
mtkleinb3f31482016-03-01 10:31:42 -0800118 br.gammaCorrect = 0 == strcmp("yes", r["options"]["gamma_correct"].asCString());
mtklein409d4702016-02-29 07:38:01 -0800119 br.md5 = r["md5"].asCString();
mtklein0ea6d712015-04-07 08:48:38 -0700120
121 if (!r["key"]["source_options"].isNull()) {
122 br.sourceOptions = r["key"]["source_options"].asCString();
123 }
mtklein62bd1a62015-01-27 14:46:26 -0800124 callback(br);
125 }
126 return true;
127}
128
scroggo7a10fb62014-11-04 07:21:10 -0800129} // namespace DM