1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // frame_capture_test_utils:
7 // Helper functions for capture and replay of traces.
8 //
9
10 #include "frame_capture_test_utils.h"
11
12 #include "common/string_utils.h"
13
14 #include <rapidjson/document.h>
15 #include <rapidjson/istreamwrapper.h>
16 #include <fstream>
17
18 namespace angle
19 {
20 namespace
21 {
LoadJSONFromFile(const std::string & fileName,rapidjson::Document * doc)22 bool LoadJSONFromFile(const std::string &fileName, rapidjson::Document *doc)
23 {
24 std::ifstream ifs(fileName);
25 if (!ifs.is_open())
26 {
27 return false;
28 }
29
30 rapidjson::IStreamWrapper inWrapper(ifs);
31 doc->ParseStream(inWrapper);
32 return !doc->HasParseError();
33 }
34 } // namespace
35
LoadTraceNamesFromJSON(const std::string jsonFilePath,std::vector<std::string> * namesOut)36 bool LoadTraceNamesFromJSON(const std::string jsonFilePath, std::vector<std::string> *namesOut)
37 {
38 rapidjson::Document doc;
39 if (!LoadJSONFromFile(jsonFilePath, &doc))
40 {
41 return false;
42 }
43
44 if (!doc.IsObject() || !doc.HasMember("traces") || !doc["traces"].IsArray())
45 {
46 return false;
47 }
48
49 // Read trace json into a list of trace names.
50 std::vector<std::string> traces;
51
52 rapidjson::Document::Array traceArray = doc["traces"].GetArray();
53 for (rapidjson::SizeType arrayIndex = 0; arrayIndex < traceArray.Size(); ++arrayIndex)
54 {
55 const rapidjson::Document::ValueType &arrayElement = traceArray[arrayIndex];
56
57 if (!arrayElement.IsString())
58 {
59 return false;
60 }
61
62 std::vector<std::string> traceAndVersion;
63 angle::SplitStringAlongWhitespace(arrayElement.GetString(), &traceAndVersion);
64 traces.push_back(traceAndVersion[0]);
65 }
66
67 *namesOut = std::move(traces);
68 return true;
69 }
70
LoadTraceInfoFromJSON(const std::string & traceName,const std::string & traceJsonPath,TraceInfo * traceInfoOut)71 bool LoadTraceInfoFromJSON(const std::string &traceName,
72 const std::string &traceJsonPath,
73 TraceInfo *traceInfoOut)
74 {
75 rapidjson::Document doc;
76 if (!LoadJSONFromFile(traceJsonPath, &doc))
77 {
78 return false;
79 }
80
81 if (!doc.IsObject() || !doc.HasMember("TraceMetadata"))
82 {
83 return false;
84 }
85
86 const rapidjson::Document::Object &meta = doc["TraceMetadata"].GetObject();
87
88 strncpy(traceInfoOut->name, traceName.c_str(), kTraceInfoMaxNameLen);
89 traceInfoOut->contextClientMajorVersion = meta["ContextClientMajorVersion"].GetInt();
90 traceInfoOut->contextClientMinorVersion = meta["ContextClientMinorVersion"].GetInt();
91 traceInfoOut->frameEnd = meta["FrameEnd"].GetInt();
92 traceInfoOut->frameStart = meta["FrameStart"].GetInt();
93 traceInfoOut->drawSurfaceHeight = meta["DrawSurfaceHeight"].GetInt();
94 traceInfoOut->drawSurfaceWidth = meta["DrawSurfaceWidth"].GetInt();
95
96 angle::HexStringToUInt(meta["DrawSurfaceColorSpace"].GetString(),
97 &traceInfoOut->drawSurfaceColorSpace);
98 angle::HexStringToUInt(meta["DisplayPlatformType"].GetString(),
99 &traceInfoOut->displayPlatformType);
100 angle::HexStringToUInt(meta["DisplayDeviceType"].GetString(), &traceInfoOut->displayDeviceType);
101
102 traceInfoOut->configRedBits = meta["ConfigRedBits"].GetInt();
103 traceInfoOut->configGreenBits = meta["ConfigGreenBits"].GetInt();
104 traceInfoOut->configBlueBits = meta["ConfigBlueBits"].GetInt();
105 traceInfoOut->configAlphaBits = meta["ConfigAlphaBits"].GetInt();
106 traceInfoOut->configDepthBits = meta["ConfigDepthBits"].GetInt();
107 traceInfoOut->configStencilBits = meta["ConfigStencilBits"].GetInt();
108
109 traceInfoOut->isBinaryDataCompressed = meta["IsBinaryDataCompressed"].GetBool();
110 traceInfoOut->areClientArraysEnabled = meta["AreClientArraysEnabled"].GetBool();
111 traceInfoOut->isBindGeneratesResourcesEnabled =
112 meta["IsBindGeneratesResourcesEnabled"].GetBool();
113 traceInfoOut->isWebGLCompatibilityEnabled = meta["IsWebGLCompatibilityEnabled"].GetBool();
114 traceInfoOut->isRobustResourceInitEnabled = meta["IsRobustResourceInitEnabled"].GetBool();
115
116 return true;
117 }
118 } // namespace angle
119