blob: f86264715bcd65367eb9c1430e798fa33f495a1a [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
10#include "SkCommonFlags.h"
11#include "SkJSONCPP.h"
12#include "SkOSFile.h"
13#include "SkStream.h"
14#include "SkTArray.h"
15#include "SkThread.h"
16
17namespace DM {
18
19SkTArray<JsonWriter::BitmapResult> gBitmapResults;
20SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
21
22void JsonWriter::AddBitmapResult(const BitmapResult& result) {
23 SkAutoMutexAcquire lock(&gBitmapResultLock);
24 gBitmapResults.push_back(result);
25}
26
27void JsonWriter::DumpJson() {
28 if (FLAGS_writePath.isEmpty()) {
29 return;
30 }
31
32 Json::Value root;
33
34 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
35 root[FLAGS_properties[i-1]] = FLAGS_properties[i];
36 }
37 for (int i = 1; i < FLAGS_key.count(); i += 2) {
38 root["key"][FLAGS_key[i-1]] = FLAGS_key[i];
39 }
40
41 {
42 SkAutoMutexAcquire lock(&gBitmapResultLock);
43 for (int i = 0; i < gBitmapResults.count(); i++) {
44 Json::Value result;
45 result["key"]["name"] = gBitmapResults[i].name.c_str();
46 result["key"]["config"] = gBitmapResults[i].config.c_str();
47 result["key"]["mode"] = gBitmapResults[i].mode.c_str();
48 result["options"]["source_type"] = gBitmapResults[i].sourceType.c_str();
49 result["md5"] = gBitmapResults[i].md5.c_str();
50
51 root["results"].append(result);
52 }
53 }
54
55 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json");
56 SkFILEWStream stream(path.c_str());
57 stream.writeText(Json::StyledWriter().write(root).c_str());
58 stream.flush();
59}
60
61} // namespace DM