Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TracyUtility.cpp « server - github.com/wolfpld/tracy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32f9df40c9ed5c5fa0e17d52d034c41a8782d622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <assert.h>

#include "TracyColor.hpp"
#include "TracyPrint.hpp"
#include "TracyUtility.hpp"
#include "TracyWorker.hpp"

namespace tracy
{

// Short list based on GetTypes() in TracySourceTokenizer.cpp
constexpr const char* TypesList[] = {
    "bool ", "char ", "double ", "float ", "int ", "long ", "short ",
    "signed ", "unsigned ", "void ", "wchar_t ", "size_t ", "int8_t ",
    "int16_t ", "int32_t ", "int64_t ", "intptr_t ", "uint8_t ", "uint16_t ",
    "uint32_t ", "uint64_t ", "ptrdiff_t ", nullptr
};

const char* ShortenZoneName( ShortenName type, const char* name, ImVec2& tsz, float zsz )
{
    assert( type != ShortenName::Never );
    if( name[0] == '<' ) return name;
    if( type == ShortenName::Always ) zsz = 0;

    static char buf[64*1024];
    char tmp[64*1024];

    auto end = name;
    while( *end ) end++;

    auto ptr = name;
    auto dst = tmp;
    int cnt = 0;
    for(;;)
    {
        auto start = ptr;
        while( ptr < end && *ptr != '<' ) ptr++;
        memcpy( dst, start, ptr - start + 1 );
        dst += ptr - start + 1;
        if( ptr == end ) break;
        cnt++;
        ptr++;
        while( cnt > 0 )
        {
            if( ptr == end ) break;
            if( *ptr == '<' ) cnt++;
            else if( *ptr == '>' ) cnt--;
            ptr++;
        }
        *dst++ = '>';
    }

    end = dst-1;
    ptr = tmp;
    dst = buf;
    cnt = 0;
    for(;;)
    {
        auto start = ptr;
        while( ptr < end && *ptr != '(' ) ptr++;
        memcpy( dst, start, ptr - start + 1 );
        dst += ptr - start + 1;
        if( ptr == end ) break;
        cnt++;
        ptr++;
        while( cnt > 0 )
        {
            if( ptr == end ) break;
            if( *ptr == '(' ) cnt++;
            else if( *ptr == ')' ) cnt--;
            ptr++;
        }
        *dst++ = ')';
    }

    end = dst-1;
    if( end - buf > 6 && memcmp( end-6, " const", 6 ) == 0 )
    {
        dst[-7] = '\0';
        end -= 6;
    }

    ptr = buf;
    for(;;)
    {
        auto match = TypesList;
        while( *match )
        {
            auto m = *match;
            auto p = ptr;
            while( *m )
            {
                if( *m != *p ) break;
                m++;
                p++;
            }
            if( !*m )
            {
                ptr = p;
                break;
            }
            match++;
        }
        if( !*match ) break;
    }

    tsz = ImGui::CalcTextSize( ptr, end );
    if( type == ShortenName::OnlyNormalize || tsz.x < zsz ) return ptr;

    for(;;)
    {
        auto p = ptr;
        while( p < end && *p != ':' ) p++;
        if( p == end ) return ptr;
        p++;
        while( p < end && *p == ':' ) p++;
        ptr = p;
        tsz = ImGui::CalcTextSize( ptr, end );
        if( tsz.x < zsz ) return ptr;
    }
}

void TooltipNormalizedName( const char* name, const char* normalized )
{
    if( ImGui::IsItemHovered() && normalized != name && strcmp( normalized, name ) != 0 )
    {
        const auto scale = ImGui::GetTextLineHeight() / 15.f;
        if( ImGui::CalcTextSize( name ).x > 1400 * scale )
        {
            ImGui::SetNextWindowSize( ImVec2( 1400 * scale, 0 ) );
            ImGui::BeginTooltip();
            ImGui::TextWrapped( "%s", name );
        }
        else
        {
            ImGui::BeginTooltip();
            ImGui::TextUnformatted( name );
        }
        ImGui::EndTooltip();
    }
}

uint32_t GetThreadColor( uint64_t thread, int depth, bool dynamic )
{
    if( !dynamic ) return 0xFFCC5555;
    return GetHsvColor( thread, depth );
}

uint32_t GetPlotColor( const PlotData& plot, const Worker& worker )
{
    switch( plot.type )
    {
    case PlotType::User:
        if( plot.color != 0 ) return plot.color | 0xFF000000;
        return GetHsvColor( charutil::hash( worker.GetString( plot.name ) ), -10 );
    case PlotType::Memory:
        return 0xFF2266CC;
    case PlotType::SysTime:
        return 0xFFBAB220;
    default:
        assert( false );
        return 0;
    }
}

const char* FormatPlotValue( double val, PlotValueFormatting format )
{
    static char buf[64];
    switch( format )
    {
    case PlotValueFormatting::Number:
        return RealToString( val );
        break;
    case PlotValueFormatting::Memory:
        return MemSizeToString( val );
        break;
    case PlotValueFormatting::Percentage:
        sprintf( buf, "%.2f%%", val );
        break;
    default:
        assert( false );
        break;
    }
    return buf;
}

}