xref: /aosp_15_r20/external/angle/src/common/angleutils.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 
21 // AMD_performance_monitor helpers.
22 
23 PerfMonitorCounter::PerfMonitorCounter() = default;
24 
25 PerfMonitorCounter::~PerfMonitorCounter() = default;
26 
27 PerfMonitorCounterGroup::PerfMonitorCounterGroup() = default;
28 
29 PerfMonitorCounterGroup::~PerfMonitorCounterGroup() = default;
30 
GetPerfMonitorCounterIndex(const PerfMonitorCounters & counters,const std::string & name)31 uint32_t GetPerfMonitorCounterIndex(const PerfMonitorCounters &counters, const std::string &name)
32 {
33     for (uint32_t counterIndex = 0; counterIndex < static_cast<uint32_t>(counters.size());
34          ++counterIndex)
35     {
36         if (counters[counterIndex].name == name)
37         {
38             return counterIndex;
39         }
40     }
41 
42     return std::numeric_limits<uint32_t>::max();
43 }
44 
GetPerfMonitorCounterGroupIndex(const PerfMonitorCounterGroups & groups,const std::string & name)45 uint32_t GetPerfMonitorCounterGroupIndex(const PerfMonitorCounterGroups &groups,
46                                          const std::string &name)
47 {
48     for (uint32_t groupIndex = 0; groupIndex < static_cast<uint32_t>(groups.size()); ++groupIndex)
49     {
50         if (groups[groupIndex].name == name)
51         {
52             return groupIndex;
53         }
54     }
55 
56     return std::numeric_limits<uint32_t>::max();
57 }
58 
GetPerfMonitorCounter(const PerfMonitorCounters & counters,const std::string & name)59 const PerfMonitorCounter &GetPerfMonitorCounter(const PerfMonitorCounters &counters,
60                                                 const std::string &name)
61 {
62     return GetPerfMonitorCounter(const_cast<PerfMonitorCounters &>(counters), name);
63 }
64 
GetPerfMonitorCounter(PerfMonitorCounters & counters,const std::string & name)65 PerfMonitorCounter &GetPerfMonitorCounter(PerfMonitorCounters &counters, const std::string &name)
66 {
67     uint32_t counterIndex = GetPerfMonitorCounterIndex(counters, name);
68     ASSERT(counterIndex < static_cast<uint32_t>(counters.size()));
69     return counters[counterIndex];
70 }
71 
GetPerfMonitorCounterGroup(const PerfMonitorCounterGroups & groups,const std::string & name)72 const PerfMonitorCounterGroup &GetPerfMonitorCounterGroup(const PerfMonitorCounterGroups &groups,
73                                                           const std::string &name)
74 {
75     return GetPerfMonitorCounterGroup(const_cast<PerfMonitorCounterGroups &>(groups), name);
76 }
77 
GetPerfMonitorCounterGroup(PerfMonitorCounterGroups & groups,const std::string & name)78 PerfMonitorCounterGroup &GetPerfMonitorCounterGroup(PerfMonitorCounterGroups &groups,
79                                                     const std::string &name)
80 {
81     uint32_t groupIndex = GetPerfMonitorCounterGroupIndex(groups, name);
82     ASSERT(groupIndex < static_cast<uint32_t>(groups.size()));
83     return groups[groupIndex];
84 }
85 }  // namespace angle
86 
ArrayString(unsigned int i)87 std::string ArrayString(unsigned int i)
88 {
89     // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
90     ASSERT(i != UINT_MAX);
91 
92     std::stringstream strstr;
93     strstr << "[";
94     strstr << i;
95     strstr << "]";
96     return strstr.str();
97 }
98 
ArrayIndexString(const std::vector<unsigned int> & indices)99 std::string ArrayIndexString(const std::vector<unsigned int> &indices)
100 {
101     std::stringstream strstr;
102 
103     for (auto indicesIt = indices.rbegin(); indicesIt != indices.rend(); ++indicesIt)
104     {
105         // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
106         ASSERT(*indicesIt != UINT_MAX);
107         strstr << "[";
108         strstr << (*indicesIt);
109         strstr << "]";
110     }
111 
112     return strstr.str();
113 }
114 
FormatStringIntoVector(const char * fmt,va_list vararg,std::vector<char> & outBuffer)115 size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char> &outBuffer)
116 {
117     va_list varargCopy;
118     va_copy(varargCopy, vararg);
119 
120     int len = vsnprintf(nullptr, 0, fmt, vararg);
121     ASSERT(len >= 0);
122 
123     outBuffer.resize(len + 1, 0);
124 
125     len = vsnprintf(outBuffer.data(), outBuffer.size(), fmt, varargCopy);
126     va_end(varargCopy);
127     ASSERT(len >= 0);
128     return static_cast<size_t>(len);
129 }
130