blob: 9ec83f8f45d5ce1ee748fcdd08a9de94e999ff34 [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"
Ben Wagnerbf111d72016-11-07 18:05:29 -050016#include "SkOSPath.h"
scroggo7a10fb62014-11-04 07:21:10 -080017#include "SkStream.h"
18#include "SkTArray.h"
scroggo7a10fb62014-11-04 07:21:10 -080019
20namespace DM {
21
22SkTArray<JsonWriter::BitmapResult> gBitmapResults;
reed086eea92016-05-04 17:12:46 -070023SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
scroggo7a10fb62014-11-04 07:21:10 -080024
25void JsonWriter::AddBitmapResult(const BitmapResult& result) {
26 SkAutoMutexAcquire lock(&gBitmapResultLock);
27 gBitmapResults.push_back(result);
28}
29
scroggo0ee26272014-11-07 06:07:32 -080030SkTArray<skiatest::Failure> gFailures;
reed086eea92016-05-04 17:12:46 -070031SK_DECLARE_STATIC_MUTEX(gFailureLock);
scroggo0ee26272014-11-07 06:07:32 -080032
33void JsonWriter::AddTestFailure(const skiatest::Failure& failure) {
34 SkAutoMutexAcquire lock(gFailureLock);
35 gFailures.push_back(failure);
36}
37
scroggo7a10fb62014-11-04 07:21:10 -080038void JsonWriter::DumpJson() {
39 if (FLAGS_writePath.isEmpty()) {
40 return;
41 }
42
43 Json::Value root;
44
45 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
46 root[FLAGS_properties[i-1]] = FLAGS_properties[i];
47 }
48 for (int i = 1; i < FLAGS_key.count(); i += 2) {
49 root["key"][FLAGS_key[i-1]] = FLAGS_key[i];
50 }
51
52 {
53 SkAutoMutexAcquire lock(&gBitmapResultLock);
54 for (int i = 0; i < gBitmapResults.count(); i++) {
55 Json::Value result;
mtklein409d4702016-02-29 07:38:01 -080056 result["key"]["name"] = gBitmapResults[i].name.c_str();
57 result["key"]["config"] = gBitmapResults[i].config.c_str();
58 result["key"]["source_type"] = gBitmapResults[i].sourceType.c_str();
59 result["options"]["ext"] = gBitmapResults[i].ext.c_str();
mtkleinb3f31482016-03-01 10:31:42 -080060 result["options"]["gamma_correct"] = gBitmapResults[i].gammaCorrect ? "yes" : "no";
mtklein409d4702016-02-29 07:38:01 -080061 result["md5"] = gBitmapResults[i].md5.c_str();
mtklein20c1c042015-04-06 07:22:05 -070062
63 // Source options only need to be part of the key if they exist.
64 // Source type by source type, we either always set options or never set options.
65 if (!gBitmapResults[i].sourceOptions.isEmpty()) {
66 result["key"]["source_options"] = gBitmapResults[i].sourceOptions.c_str();
67 }
scroggo7a10fb62014-11-04 07:21:10 -080068
69 root["results"].append(result);
70 }
71 }
72
scroggo0ee26272014-11-07 06:07:32 -080073 {
74 SkAutoMutexAcquire lock(gFailureLock);
75 for (int i = 0; i < gFailures.count(); i++) {
76 Json::Value result;
77 result["file_name"] = gFailures[i].fileName;
78 result["line_no"] = gFailures[i].lineNo;
79 result["condition"] = gFailures[i].condition;
80 result["message"] = gFailures[i].message.c_str();
81
82 root["test_results"]["failures"].append(result);
83 }
84 }
85
halcanaryc79a3912015-04-01 13:31:34 -070086 int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
87 if (maxResidentSetSizeMB != -1) {
88 root["max_rss_MB"] = sk_tools::getMaxResidentSetSizeMB();
89 }
90
scroggo7a10fb62014-11-04 07:21:10 -080091 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json");
halcanary022afb82015-01-30 11:00:12 -080092 sk_mkdir(FLAGS_writePath[0]);
scroggo7a10fb62014-11-04 07:21:10 -080093 SkFILEWStream stream(path.c_str());
94 stream.writeText(Json::StyledWriter().write(root).c_str());
95 stream.flush();
96}
97
mtklein62bd1a62015-01-27 14:46:26 -080098bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
bungeman38d909e2016-08-02 14:40:46 -070099 sk_sp<SkData> json(SkData::MakeFromFileName(path));
mtklein62bd1a62015-01-27 14:46:26 -0800100 if (!json) {
101 return false;
102 }
103
104 Json::Reader reader;
105 Json::Value root;
106 const char* data = (const char*)json->data();
107 if (!reader.parse(data, data+json->size(), root)) {
108 return false;
109 }
110
111 const Json::Value& results = root["results"];
112 BitmapResult br;
113 for (unsigned i = 0; i < results.size(); i++) {
114 const Json::Value& r = results[i];
mtklein409d4702016-02-29 07:38:01 -0800115 br.name = r["key"]["name"].asCString();
116 br.config = r["key"]["config"].asCString();
117 br.sourceType = r["key"]["source_type"].asCString();
118 br.ext = r["options"]["ext"].asCString();
mtkleinb3f31482016-03-01 10:31:42 -0800119 br.gammaCorrect = 0 == strcmp("yes", r["options"]["gamma_correct"].asCString());
mtklein409d4702016-02-29 07:38:01 -0800120 br.md5 = r["md5"].asCString();
mtklein0ea6d712015-04-07 08:48:38 -0700121
122 if (!r["key"]["source_options"].isNull()) {
123 br.sourceOptions = r["key"]["source_options"].asCString();
124 }
mtklein62bd1a62015-01-27 14:46:26 -0800125 callback(br);
126 }
127 return true;
128}
129
scroggo7a10fb62014-11-04 07:21:10 -0800130} // namespace DM