1 //
2 // Copyright 2014 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
7 #include "common/angleutils.h"
8 #include "common/debug.h"
9
10 #include <stdio.h>
11
12 #include <limits>
13 #include <vector>
14
15 namespace angle
16 {
17 // dirtyPointer is a special value that will make the comparison with any valid pointer fail and
18 // force the renderer to re-apply the state.
19 const uintptr_t DirtyPointer = std::numeric_limits<uintptr_t>::max();
20
SaveFileHelper(const std::string & filePathIn)21 SaveFileHelper::SaveFileHelper(const std::string &filePathIn)
22 : mOfs(filePathIn, std::ios::binary | std::ios::out), mFilePath(filePathIn)
23 {
24 if (!mOfs.is_open())
25 {
26 FATAL() << "Could not open " << filePathIn;
27 }
28 }
29
~SaveFileHelper()30 SaveFileHelper::~SaveFileHelper()
31 {
32 printf("Saved '%s'.\n", mFilePath.c_str());
33 }
34
checkError()35 void SaveFileHelper::checkError()
36 {
37 if (mOfs.bad())
38 {
39 FATAL() << "Error writing to " << mFilePath;
40 }
41 }
42
write(const uint8_t * data,size_t size)43 void SaveFileHelper::write(const uint8_t *data, size_t size)
44 {
45 mOfs.write(reinterpret_cast<const char *>(data), size);
46 }
47
48 // AMD_performance_monitor helpers.
49
50 PerfMonitorCounter::PerfMonitorCounter() = default;
51
52 PerfMonitorCounter::~PerfMonitorCounter() = default;
53
54 PerfMonitorCounterGroup::PerfMonitorCounterGroup() = default;
55
56 PerfMonitorCounterGroup::~PerfMonitorCounterGroup() = default;
57
GetPerfMonitorCounterIndex(const PerfMonitorCounters & counters,const std::string & name)58 uint32_t GetPerfMonitorCounterIndex(const PerfMonitorCounters &counters, const std::string &name)
59 {
60 for (uint32_t counterIndex = 0; counterIndex < static_cast<uint32_t>(counters.size());
61 ++counterIndex)
62 {
63 if (counters[counterIndex].name == name)
64 {
65 return counterIndex;
66 }
67 }
68
69 return std::numeric_limits<uint32_t>::max();
70 }
71
GetPerfMonitorCounterGroupIndex(const PerfMonitorCounterGroups & groups,const std::string & name)72 uint32_t GetPerfMonitorCounterGroupIndex(const PerfMonitorCounterGroups &groups,
73 const std::string &name)
74 {
75 for (uint32_t groupIndex = 0; groupIndex < static_cast<uint32_t>(groups.size()); ++groupIndex)
76 {
77 if (groups[groupIndex].name == name)
78 {
79 return groupIndex;
80 }
81 }
82
83 return std::numeric_limits<uint32_t>::max();
84 }
85
GetPerfMonitorCounter(const PerfMonitorCounters & counters,const std::string & name)86 const PerfMonitorCounter &GetPerfMonitorCounter(const PerfMonitorCounters &counters,
87 const std::string &name)
88 {
89 return GetPerfMonitorCounter(const_cast<PerfMonitorCounters &>(counters), name);
90 }
91
GetPerfMonitorCounter(PerfMonitorCounters & counters,const std::string & name)92 PerfMonitorCounter &GetPerfMonitorCounter(PerfMonitorCounters &counters, const std::string &name)
93 {
94 uint32_t counterIndex = GetPerfMonitorCounterIndex(counters, name);
95 ASSERT(counterIndex < static_cast<uint32_t>(counters.size()));
96 return counters[counterIndex];
97 }
98
GetPerfMonitorCounterGroup(const PerfMonitorCounterGroups & groups,const std::string & name)99 const PerfMonitorCounterGroup &GetPerfMonitorCounterGroup(const PerfMonitorCounterGroups &groups,
100 const std::string &name)
101 {
102 return GetPerfMonitorCounterGroup(const_cast<PerfMonitorCounterGroups &>(groups), name);
103 }
104
GetPerfMonitorCounterGroup(PerfMonitorCounterGroups & groups,const std::string & name)105 PerfMonitorCounterGroup &GetPerfMonitorCounterGroup(PerfMonitorCounterGroups &groups,
106 const std::string &name)
107 {
108 uint32_t groupIndex = GetPerfMonitorCounterGroupIndex(groups, name);
109 ASSERT(groupIndex < static_cast<uint32_t>(groups.size()));
110 return groups[groupIndex];
111 }
112 } // namespace angle
113
ArrayString(unsigned int i)114 std::string ArrayString(unsigned int i)
115 {
116 // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
117 ASSERT(i != UINT_MAX);
118
119 std::stringstream strstr;
120 strstr << "[";
121 strstr << i;
122 strstr << "]";
123 return strstr.str();
124 }
125
ArrayIndexString(const std::vector<unsigned int> & indices)126 std::string ArrayIndexString(const std::vector<unsigned int> &indices)
127 {
128 std::stringstream strstr;
129
130 for (auto indicesIt = indices.rbegin(); indicesIt != indices.rend(); ++indicesIt)
131 {
132 // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
133 ASSERT(*indicesIt != UINT_MAX);
134 strstr << "[";
135 strstr << (*indicesIt);
136 strstr << "]";
137 }
138
139 return strstr.str();
140 }
141
FormatStringIntoVector(const char * fmt,va_list vararg,std::vector<char> & outBuffer)142 size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char> &outBuffer)
143 {
144 va_list varargCopy;
145 va_copy(varargCopy, vararg);
146
147 int len = vsnprintf(nullptr, 0, fmt, vararg);
148 ASSERT(len >= 0);
149
150 outBuffer.resize(len + 1, 0);
151
152 len = vsnprintf(outBuffer.data(), outBuffer.size(), fmt, varargCopy);
153 va_end(varargCopy);
154 ASSERT(len >= 0);
155 return static_cast<size_t>(len);
156 }
157