Change file selector to work with callbacks.

This commit is contained in:
Bartosz Taudul 2022-10-06 17:35:07 +02:00
parent a4e39f3d5f
commit ec0757c03a
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
6 changed files with 82 additions and 94 deletions

View File

@ -575,12 +575,10 @@ static void DrawContents()
#ifndef TRACY_NO_FILESELECTOR
if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open saved trace" ) && !loadThread.joinable() )
{
auto fn = tracy::Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" );
if( !fn.empty() )
{
tracy::Fileselector::OpenFile( "tracy", "Tracy Profiler trace file", []( const char* fn ) {
try
{
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn.c_str() ) );
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) );
if( f )
{
loadThread = std::thread( [f] {
@ -609,7 +607,7 @@ static void DrawContents()
{
badVer.state = tracy::BadVersionState::ReadError;
}
}
} );
}
if( badVer.state != tracy::BadVersionState::Ok )

View File

@ -21,34 +21,30 @@ void Shutdown()
#endif
}
std::string OpenFile( const char* ext, const char* desc )
void OpenFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
{
std::string ret;
#ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_OpenDialogU8( &fn, &filter, 1, nullptr ) == NFD_OKAY )
{
ret.assign( fn );
callback( (const char*)fn );
NFD_FreePathU8( fn );
}
#endif
return ret;
}
std::string SaveFile( const char* ext, const char* desc )
void SaveFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
{
std::string ret;
#ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ) == NFD_OKAY )
{
ret.assign( fn );
callback( (const char*)fn );
NFD_FreePathU8( fn );
}
#endif
return ret;
}
}

View File

@ -1,7 +1,7 @@
#ifndef __TRACYFILESELECTOR_HPP__
#define __TRACYFILESELECTOR_HPP__
#include <string>
#include <functional>
namespace tracy::Fileselector
{
@ -9,8 +9,8 @@ namespace tracy::Fileselector
void Init();
void Shutdown();
std::string OpenFile( const char* ext, const char* desc );
std::string SaveFile( const char* ext, const char* desc );
void OpenFile( const char* ext, const char* desc, std::function<void(const char*)> callback );
void SaveFile( const char* ext, const char* desc, std::function<void(const char*)> callback );
}

View File

@ -5356,81 +5356,77 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop )
assert( start < m_asm.size() );
assert( start < stop );
auto fn = Fileselector::SaveFile( "asm", "Assembly file" );
if( !fn.empty() )
{
Fileselector::SaveFile( "asm", "Assembly file", [this, &worker, start, stop]( const char* fn ) {
FILE* f = nullptr;
const auto sz = fn.size();
if( sz < 5 || memcmp( fn.c_str() + sz - 4, ".asm", 4 ) != 0 )
const auto sz = strlen( fn );
if( sz < 5 || memcmp( fn + sz - 4, ".asm", 4 ) != 0 )
{
char tmp[1024];
sprintf( tmp, "%s.asm", fn.c_str() );
sprintf( tmp, "%s.asm", fn );
f = fopen( tmp, "wb" );
}
else
{
f = fopen( fn.c_str(), "wb" );
f = fopen( fn, "wb" );
}
if( f )
if( !f ) return;
char tmp[16];
auto sym = worker.GetSymbolData( m_symAddr );
assert( sym );
const char* symName;
if( sym->isInline )
{
char tmp[16];
auto sym = worker.GetSymbolData( m_symAddr );
assert( sym );
const char* symName;
if( sym->isInline )
auto parent = worker.GetSymbolData( m_baseAddr );
if( parent )
{
auto parent = worker.GetSymbolData( m_baseAddr );
if( parent )
{
symName = worker.GetString( parent->name );
}
else
{
sprintf( tmp, "0x%" PRIx64, m_baseAddr );
symName = tmp;
}
symName = worker.GetString( parent->name );
}
else
{
symName = worker.GetString( sym->name );
sprintf( tmp, "0x%" PRIx64, m_baseAddr );
symName = tmp;
}
fprintf( f, "; Tracy Profiler disassembly of symbol %s [%s]\n\n", symName, worker.GetCaptureProgram().c_str() );
if( !m_atnt ) fprintf( f, ".intel_syntax\n\n" );
const auto end = m_asm.size() < stop ? m_asm.size() : stop;
for( size_t i=start; i<end; i++ )
{
const auto& v = m_asm[i];
auto it = m_locMap.find( v.addr );
if( it != m_locMap.end() )
{
fprintf( f, ".L%" PRIu32 ":\n", it->second );
}
bool hasJump = false;
if( v.jumpAddr != 0 )
{
auto lit = m_locMap.find( v.jumpAddr );
if( lit != m_locMap.end() )
{
fprintf( f, "\t%-*s.L%" PRIu32 "\n", m_maxMnemonicLen, v.mnemonic.c_str(), lit->second );
hasJump = true;
}
}
if( !hasJump )
{
if( v.operands.empty() )
{
fprintf( f, "\t%s\n", v.mnemonic.c_str() );
}
else
{
fprintf( f, "\t%-*s%s\n", m_maxMnemonicLen, v.mnemonic.c_str(), v.operands.c_str() );
}
}
}
fclose( f );
}
}
else
{
symName = worker.GetString( sym->name );
}
fprintf( f, "; Tracy Profiler disassembly of symbol %s [%s]\n\n", symName, worker.GetCaptureProgram().c_str() );
if( !m_atnt ) fprintf( f, ".intel_syntax\n\n" );
const auto end = m_asm.size() < stop ? m_asm.size() : stop;
for( size_t i=start; i<end; i++ )
{
const auto& v = m_asm[i];
auto it = m_locMap.find( v.addr );
if( it != m_locMap.end() )
{
fprintf( f, ".L%" PRIu32 ":\n", it->second );
}
bool hasJump = false;
if( v.jumpAddr != 0 )
{
auto lit = m_locMap.find( v.jumpAddr );
if( lit != m_locMap.end() )
{
fprintf( f, "\t%-*s.L%" PRIu32 "\n", m_maxMnemonicLen, v.mnemonic.c_str(), lit->second );
hasJump = true;
}
}
if( !hasJump )
{
if( v.operands.empty() )
{
fprintf( f, "\t%s\n", v.mnemonic.c_str() );
}
else
{
fprintf( f, "\t%-*s%s\n", m_maxMnemonicLen, v.mnemonic.c_str(), v.operands.c_str() );
}
}
}
fclose( f );
} );
}
#endif

View File

@ -149,12 +149,10 @@ void View::DrawCompare()
ImGui::TextWrapped( "Please load a second trace to compare results." );
if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open second trace" ) && !m_compare.loadThread.joinable() )
{
auto fn = Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" );
if( !fn.empty() )
{
Fileselector::OpenFile( "tracy", "Tracy Profiler trace file", [this]( const char* fn ) {
try
{
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn.c_str() ) );
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) );
if( f )
{
m_compare.loadThread = std::thread( [this, f] {
@ -168,7 +166,7 @@ void View::DrawCompare()
m_compare.badVer.state = BadVersionState::UnsupportedVersion;
m_compare.badVer.version = e.version;
}
} );
} );
}
}
catch( const tracy::NotTracyDump& )
@ -179,7 +177,7 @@ void View::DrawCompare()
{
m_compare.badVer.state = BadVersionState::ReadError;
}
}
} );
}
tracy::BadVersion( m_compare.badVer, m_bigFont );
ImGui::End();

View File

@ -121,25 +121,25 @@ bool View::DrawConnection()
ImGui::Separator();
if( ImGui::Button( ICON_FA_FLOPPY_DISK " Save trace" ) && m_saveThreadState.load( std::memory_order_relaxed ) == SaveThreadState::Inert )
{
#ifndef TRACY_NO_FILESELECTOR
auto fn = Fileselector::SaveFile( "tracy", "Tracy Profiler trace file" );
if( !fn.empty() )
#else
std::string fn = "trace.tracy";
#endif
{
const auto sz = fn.size();
if( sz < 7 || memcmp( fn.c_str() + sz - 6, ".tracy", 6 ) != 0 )
auto cb = [this]( const char* fn ) {
const auto sz = strlen( fn );
if( sz < 7 || memcmp( fn + sz - 6, ".tracy", 6 ) != 0 )
{
char tmp[1024];
sprintf( tmp, "%s.tracy", fn.c_str() );
sprintf( tmp, "%s.tracy", fn );
m_filenameStaging = tmp;
}
else
{
m_filenameStaging = std::move( fn );
m_filenameStaging = fn;
}
}
};
#ifndef TRACY_NO_FILESELECTOR
Fileselector::SaveFile( "tracy", "Tracy Profiler trace file", cb );
#else
cb( "trace.tracy" );
#endif
}
ImGui::SameLine( 0, 2 * ty );