Move profiler render to a separate function.

This commit is contained in:
Bartosz Taudul 2020-01-10 02:10:07 +01:00
parent 2f718b57c1
commit 5f48b08215

View File

@ -76,6 +76,8 @@ static void SetWindowTitleCallback( const char* title )
s_customTitle = true;
}
static void DrawContents();
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> RebuildConnectionHistory( const std::unordered_map<std::string, uint64_t>& connHistMap )
{
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> ret;
@ -97,16 +99,31 @@ struct ClientData
std::string address;
};
enum class ViewShutdown { False, True, Join };
static tracy::flat_hash_map<uint32_t, ClientData> clients;
static std::unique_ptr<tracy::View> view;
static tracy::BadVersionState badVer;
static int port = 8086;
static const char* connectTo = nullptr;
static char title[128];
static std::thread loadThread;
static std::unique_ptr<tracy::UdpListen> broadcastListen;
static std::mutex resolvLock;
static tracy::flat_hash_map<std::string, std::string> resolvMap;
static ResolvService resolv( port );
static ImFont* bigFont;
static ImFont* smallFont;
static ImFont* fixedWidth;
static char addr[1024] = { "127.0.0.1" };
static std::unordered_map<std::string, uint64_t> connHistMap;
static std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> connHistVec;
static ViewShutdown viewShutdown = ViewShutdown::False;
static double animTime = 0;
static float dpiScale = 1.f;
int main( int argc, char** argv )
{
tracy::flat_hash_map<uint32_t, ClientData> clients;
std::unique_ptr<tracy::View> view;
tracy::BadVersionState badVer;
int port = 8086;
const char* connectTo = nullptr;
if( argc == 2 )
{
auto f = std::unique_ptr<tracy::FileRead>( tracy::FileRead::Open( argv[1] ) );
@ -141,7 +158,6 @@ int main( int argc, char** argv )
view = std::make_unique<tracy::View>( connectTo, port );
}
char title[128];
sprintf( title, "Tracy Profiler %i.%i.%i", tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch );
std::string winPosFile = tracy::GetSavePath( "window.position" );
@ -170,8 +186,6 @@ int main( int argc, char** argv )
}
std::string connHistFile = tracy::GetSavePath( "connection.history" );
std::unordered_map<std::string, uint64_t> connHistMap;
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> connHistVec;
{
FILE* f = fopen( connHistFile.c_str(), "rb" );
if( f )
@ -222,7 +236,6 @@ int main( int argc, char** argv )
glfwSwapInterval(1); // Enable vsync
gl3wInit();
float dpiScale = 1.f;
#ifdef _WIN32
typedef UINT(*GDFS)(void);
GDFS getDpiForSystem = nullptr;
@ -259,9 +272,9 @@ int main( int argc, char** argv )
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Arimo_compressed_data, tracy::Arimo_compressed_size, 15.0f * dpiScale, nullptr, rangesBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, 14.0f * dpiScale, &configMerge, rangesIcons );
auto fixedWidth = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Cousine_compressed_data, tracy::Cousine_compressed_size, 15.0f * dpiScale );
auto bigFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Arimo_compressed_data, tracy::Cousine_compressed_size, 20.0f * dpiScale );
auto smallFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Arimo_compressed_data, tracy::Cousine_compressed_size, 10.0f * dpiScale );
fixedWidth = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Cousine_compressed_data, tracy::Cousine_compressed_size, 15.0f * dpiScale );
bigFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Arimo_compressed_data, tracy::Cousine_compressed_size, 20.0f * dpiScale );
smallFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::Arimo_compressed_data, tracy::Cousine_compressed_size, 10.0f * dpiScale );
ImGuiFreeType::BuildFontAtlas( io.Fonts, ImGuiFreeType::LightHinting );
@ -273,36 +286,78 @@ int main( int argc, char** argv )
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4( 1, 1, 1, 0.03f );
style.ScaleAllSizes( dpiScale );
ImVec4 clear_color = ImColor(114, 144, 154);
char addr[1024] = { "127.0.0.1" };
std::thread loadThread;
std::unique_ptr<tracy::UdpListen> broadcastListen;
enum class ViewShutdown { False, True, Join };
ViewShutdown viewShutdown = ViewShutdown::False;
std::mutex resolvLock;
tracy::flat_hash_map<std::string, std::string> resolvMap;
ResolvService resolv( port );
glfwShowWindow( window );
double time = 0;
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
if( glfwGetWindowAttrib( window, GLFW_ICONIFIED ) )
{
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
continue;
}
DrawContents();
if( !glfwGetWindowAttrib( window, GLFW_FOCUSED ) )
{
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
}
}
{
FILE* f = fopen( winPosFile.c_str(), "wb" );
if( f )
{
#ifdef GLFW_MAXIMIZED
uint32_t maximized = glfwGetWindowAttrib( window, GLFW_MAXIMIZED );
if( maximized ) glfwRestoreWindow( window );
#else
uint32_t maximized = 0;
#endif
glfwGetWindowPos( window, &x, &y );
glfwGetWindowSize( window, &w, &h );
uint32_t data[5] = { uint32_t( x ), uint32_t( y ), uint32_t( w ), uint32_t( h ), maximized };
fwrite( data, 1, sizeof( data ), f );
fclose( f );
}
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
{
FILE* f = fopen( connHistFile.c_str(), "wb" );
if( f )
{
uint64_t sz = uint64_t( connHistMap.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
for( auto& v : connHistMap )
{
sz = uint64_t( v.first.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
fwrite( v.first.c_str(), 1, sz, f );
fwrite( &v.second, 1, sizeof( v.second ), f );
}
fclose( f );
}
}
return 0;
}
static void DrawContents()
{
const ImVec4 clear_color = ImColor( 114, 144, 154 );
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glfwGetFramebufferSize(s_glfwWindow, &display_w, &display_h);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
@ -313,7 +368,7 @@ int main( int argc, char** argv )
if( s_customTitle )
{
s_customTitle = false;
glfwSetWindowTitle( window, title );
glfwSetWindowTitle( s_glfwWindow, title );
}
const auto time = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch() ).count();
@ -352,7 +407,7 @@ int main( int argc, char** argv )
if( resolvMap.find( ip ) == resolvMap.end() )
{
resolvMap.emplace( ip, ip );
resolv.Query( ipNumerical, [&resolvMap, &resolvLock, ip] ( std::string&& name ) {
resolv.Query( ipNumerical, [ip] ( std::string&& name ) {
std::lock_guard<std::mutex> lock( resolvLock );
auto it = resolvMap.find( ip );
assert( it != resolvMap.end() );
@ -387,6 +442,7 @@ int main( int argc, char** argv )
}
setlocale( LC_NUMERIC, "C" );
auto& style = ImGui::GetStyle();
style.Colors[ImGuiCol_WindowBg] = ImVec4( 0.129f, 0.137f, 0.11f, 1.f );
ImGui::Begin( "Get started", nullptr, ImGuiWindowFlags_AlwaysAutoResize );
char buf[128];
@ -498,7 +554,7 @@ int main( int argc, char** argv )
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) );
if( f )
{
loadThread = std::thread( [&view, f, &badVer, fixedWidth, smallFont, bigFont] {
loadThread = std::thread( [f] {
try
{
view = std::make_unique<tracy::View>( *f, fixedWidth, smallFont, bigFont, SetWindowTitleCallback );
@ -595,7 +651,7 @@ int main( int argc, char** argv )
if( !view->Draw() )
{
viewShutdown = ViewShutdown::True;
loadThread = std::thread( [&viewShutdown, view = std::move( view )] () mutable {
loadThread = std::thread( [view = std::move( view )] () mutable {
view.reset();
viewShutdown = ViewShutdown::Join;
} );
@ -611,8 +667,8 @@ int main( int argc, char** argv )
{
tracy::TextCentered( ICON_FA_HOURGLASS_HALF );
time += io.DeltaTime;
tracy::DrawWaitingDots( time );
animTime += ImGui::GetIO().DeltaTime;
tracy::DrawWaitingDots( animTime );
auto currProgress = progress.progress.load( std::memory_order_relaxed );
if( totalProgress == 0 )
@ -690,73 +746,20 @@ int main( int argc, char** argv )
{
if( viewShutdown != ViewShutdown::True ) ImGui::CloseCurrentPopup();
tracy::TextCentered( ICON_FA_BROOM );
time += io.DeltaTime;
tracy::DrawWaitingDots( time );
animTime += ImGui::GetIO().DeltaTime;
tracy::DrawWaitingDots( animTime );
ImGui::Text( "Please wait, cleanup is in progress" );
ImGui::EndPopup();
}
// Rendering
ImGui::Render();
glfwMakeContextCurrent(window);
glfwMakeContextCurrent(s_glfwWindow);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwMakeContextCurrent(window);
glfwSwapBuffers(window);
if( !glfwGetWindowAttrib( window, GLFW_FOCUSED ) )
{
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
}
}
{
FILE* f = fopen( winPosFile.c_str(), "wb" );
if( f )
{
#ifdef GLFW_MAXIMIZED
uint32_t maximized = glfwGetWindowAttrib( window, GLFW_MAXIMIZED );
if( maximized ) glfwRestoreWindow( window );
#else
uint32_t maximized = 0;
#endif
glfwGetWindowPos( window, &x, &y );
glfwGetWindowSize( window, &w, &h );
uint32_t data[5] = { uint32_t( x ), uint32_t( y ), uint32_t( w ), uint32_t( h ), maximized };
fwrite( data, 1, sizeof( data ), f );
fclose( f );
}
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
{
FILE* f = fopen( connHistFile.c_str(), "wb" );
if( f )
{
uint64_t sz = uint64_t( connHistMap.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
for( auto& v : connHistMap )
{
sz = uint64_t( v.first.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
fwrite( v.first.c_str(), 1, sz, f );
fwrite( &v.second, 1, sizeof( v.second ), f );
}
fclose( f );
}
}
return 0;
glfwMakeContextCurrent(s_glfwWindow);
glfwSwapBuffers(s_glfwWindow);
}