Optimize drawing zone frames.

This commit is contained in:
Bartosz Taudul 2017-10-22 17:48:01 +02:00
parent 73d23320ee
commit 8f814b2537

View File

@ -1704,6 +1704,20 @@ void View::HandleZoneViewMouse( int64_t timespan, const ImVec2& wpos, float w, d
}
}
static const char* GetFrameText( int i, uint64_t ftime )
{
static char buf[128];
if( i == 0 )
{
sprintf( buf, "Tracy init (%s)", TimeToString( ftime ) );
}
else
{
sprintf( buf, "Frame %i (%s)", i, TimeToString( ftime ) );
}
return buf;
}
bool View::DrawZoneFrames()
{
auto& io = ImGui::GetIO();
@ -1713,6 +1727,7 @@ bool View::DrawZoneFrames()
const auto h = ImGui::GetFontSize();
const auto wh = ImGui::GetContentRegionAvail().y;
auto draw = ImGui::GetWindowDrawList();
const auto ty = ImGui::GetFontSize();
ImGui::InvisibleButton( "##zoneFrames", ImVec2( w, h ) );
bool hover = ImGui::IsItemHovered();
@ -1724,11 +1739,8 @@ bool View::DrawZoneFrames()
m_zvStartNext = 0;
// frames
do
{
const auto zitbegin = std::lower_bound( m_frames.begin(), m_frames.end(), m_zvStart );
if( zitbegin == m_frames.end() ) break;
if( zitbegin == m_frames.end() ) return hover;
const auto zitend = std::lower_bound( m_frames.begin(), m_frames.end(), m_zvEnd );
auto zbegin = (int)std::distance( m_frames.begin(), zitbegin );
@ -1740,26 +1752,12 @@ bool View::DrawZoneFrames()
const auto ftime = GetFrameTime( i );
const auto fbegin = (int64_t)GetFrameBegin( i );
const auto fend = (int64_t)GetFrameEnd( i );
uint32_t color;
char buf[128];
if( i == 0 )
{
sprintf( buf, "Tracy init (%s)", TimeToString( ftime ) );
color = 0xFF4444FF;
}
else
{
sprintf( buf, "Frame %i (%s)", i, TimeToString( ftime ) );
color = 0xFFFFFFFF;
}
const auto tsz = ImGui::CalcTextSize( buf );
const auto fsz = pxns * ftime;
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns, tsz.y ) ) )
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns, ty ) ) )
{
ImGui::BeginTooltip();
ImGui::Text( "%s", buf );
ImGui::Text( "%s", GetFrameText( i, ftime ) );
ImGui::Text( "Time from start of program: %s", TimeToString( m_frames[i] - m_frames[0] ) );
ImGui::EndTooltip();
@ -1771,31 +1769,37 @@ bool View::DrawZoneFrames()
}
}
if( fbegin >= m_zvStart && fsz > 4 )
if( fsz <= 4 ) continue;
if( fbegin >= m_zvStart )
{
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, wh ), 0x22FFFFFF );
}
if( fsz >= 5 )
{
auto buf = GetFrameText( i, ftime );
const auto tx = ImGui::CalcTextSize( buf ).x;
uint32_t color = i == 0 ? 0xFF4444FF : 0xFFFFFFFF;
if( fbegin >= m_zvStart )
{
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, 1 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, tsz.y - 1 ), color );
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, 1 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, ty - 1 ), color );
}
if( fend <= m_zvEnd )
{
draw->AddLine( wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, 1 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, tsz.y - 1 ), color );
draw->AddLine( wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, 1 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, ty - 1 ), color );
}
if( fsz - 5 > tsz.x )
if( fsz - 5 > tx )
{
const auto part = ( fsz - 5 - tsz.x ) / 2;
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, tsz.y / 2 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + part, tsz.y / 2 ), color );
const auto part = ( fsz - 5 - tx ) / 2;
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, ty / 2 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + part, ty / 2 ), color );
draw->AddText( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2 + part, 0 ), color, buf );
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2 + part + tsz.x, tsz.y / 2 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, tsz.y / 2 ), color );
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2 + part + tx, ty / 2 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, ty / 2 ), color );
}
else
{
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, tsz.y / 2 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, tsz.y / 2 ), color );
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, ty / 2 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, ty / 2 ), color );
}
}
}
@ -1805,8 +1809,6 @@ bool View::DrawZoneFrames()
{
draw->AddLine( wpos + ImVec2( ( fend - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns, wh ), 0x22FFFFFF );
}
}
while( false );
return hover;
}