diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 2e27e6b6..bdd1ccee 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -956,11 +956,9 @@ Profiler::Profiler() , m_serialDequeue( 1024*1024 ) , m_fiQueue( 16 ) , m_fiDequeue( 16 ) - , m_etc1Buf( nullptr ) - , m_etc1BufSize( 0 ) + , m_frameCount( 0 ) #ifdef TRACY_ON_DEMAND , m_isConnected( false ) - , m_frameCount( 0 ) , m_connectionId( 0 ) , m_deferredQueue( 64*1024 ) #endif @@ -1040,7 +1038,6 @@ Profiler::~Profiler() tracy_free( m_lz4Buf ); tracy_free( m_itemBuf ); tracy_free( m_buffer ); - tracy_free( m_etc1Buf ); LZ4_freeStream( (LZ4_stream_t*)m_stream ); if( m_sock ) @@ -1403,7 +1400,68 @@ void Profiler::Worker() void Profiler::CompressWorker() { + for(;;) + { + const auto shouldExit = ShouldExit(); + { + bool lockHeld = true; + while( !m_fiLock.try_lock() ) + { + if( m_shutdownManual.load( std::memory_order_relaxed ) ) + { + lockHeld = false; + break; + } + } + m_fiQueue.swap( m_fiDequeue ); + if( lockHeld ) + { + m_fiLock.unlock(); + } + } + + const auto sz = m_fiDequeue.size(); + if( sz > 0 ) + { + auto fi = m_fiDequeue.data(); + auto end = fi + sz; + while( fi != end ) + { + const auto w = fi->w; + const auto h = fi->h; + const auto csz = size_t( w * h / 2 ); + auto etc1buf = (char*)tracy_malloc( csz ); + CompressImageEtc1( (const char*)fi->image, etc1buf, w, h ); + tracy_free( fi->image ); + + Magic magic; + auto token = GetToken(); + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + MemWrite( &item->hdr.type, QueueType::FrameImage ); + MemWrite( &item->frameImage.image, (uint64_t)etc1buf ); + MemWrite( &item->frameImage.frame, fi->frame ); + MemWrite( &item->frameImage.w, w ); + MemWrite( &item->frameImage.h, h ); + uint8_t flip = fi->flip; + MemWrite( &item->frameImage.flip, flip ); + tail.store( magic + 1, std::memory_order_release ); + + fi++; + } + m_fiDequeue.clear(); + } + else + { + std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) ); + } + + if( shouldExit ) + { + return; + } + } } static void FreeAssociatedMemory( const QueueItem& item ) @@ -1536,14 +1594,8 @@ Profiler::DequeueStatus Profiler::Dequeue( moodycamel::ConsumerToken& token ) const auto w = MemRead( &item->frameImage.w ); const auto h = MemRead( &item->frameImage.h ); const auto csz = size_t( w * h / 2 ); - if( csz > m_etc1BufSize ) - { - tracy_free( m_etc1Buf ); - m_etc1Buf = (char*)tracy_malloc( csz ); - } - CompressImageEtc1( (const char*)ptr, m_etc1Buf, w, h ); + SendLongString( ptr, (const char*)ptr, csz, QueueType::FrameImageData ); tracy_free( (void*)ptr ); - SendLongString( ptr, m_etc1Buf, csz, QueueType::FrameImageData ); break; } default: diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 2b6a633d..2a8f8aa3 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -92,7 +92,7 @@ class Profiler struct FrameImageQueueItem { void* image; - int64_t time; + uint64_t frame; uint16_t w; uint16_t h; uint8_t offset; @@ -154,8 +154,8 @@ public: static tracy_force_inline void SendFrameMark( const char* name ) { -#ifdef TRACY_ON_DEMAND if( !name ) GetProfiler().m_frameCount.fetch_add( 1, std::memory_order_relaxed ); +#ifdef TRACY_ON_DEMAND if( !GetProfiler().IsConnected() ) return; #endif Magic magic; @@ -189,7 +189,6 @@ public: #ifdef TRACY_ON_DEMAND if( !profiler.IsConnected() ) return; #endif - const auto time = GetTime(); const auto sz = size_t( w ) * size_t( h ) * 4; auto ptr = (char*)tracy_malloc( sz ); memcpy( ptr, image, sz ); @@ -197,10 +196,9 @@ public: profiler.m_fiLock.lock(); auto fi = profiler.m_fiQueue.prepare_next(); fi->image = ptr; - fi->time = time; + fi->frame = profiler.m_frameCount.load( std::memory_order_relaxed ) - offset; fi->w = w; fi->h = h; - fi->offset = offset; fi->flip = flip; profiler.m_fiQueue.commit_next(); profiler.m_fiLock.unlock(); @@ -554,12 +552,9 @@ private: FastVector m_fiQueue, m_fiDequeue; TracyMutex m_fiLock; - char* m_etc1Buf; - size_t m_etc1BufSize; - + std::atomic m_frameCount; #ifdef TRACY_ON_DEMAND std::atomic m_isConnected; - std::atomic m_frameCount; std::atomic m_connectionId; TracyMutex m_deferredLock; diff --git a/common/TracyProtocol.hpp b/common/TracyProtocol.hpp index f816ca70..4a188b03 100644 --- a/common/TracyProtocol.hpp +++ b/common/TracyProtocol.hpp @@ -9,7 +9,7 @@ namespace tracy { -enum : uint32_t { ProtocolVersion = 9 }; +enum : uint32_t { ProtocolVersion = 10 }; enum : uint32_t { BroadcastVersion = 0 }; using lz4sz_t = uint32_t; diff --git a/common/TracyQueue.hpp b/common/TracyQueue.hpp index 24537d4f..42225f40 100644 --- a/common/TracyQueue.hpp +++ b/common/TracyQueue.hpp @@ -103,9 +103,9 @@ struct QueueFrameMark struct QueueFrameImage { uint64_t image; // ptr + uint64_t frame; uint16_t w; uint16_t h; - uint8_t offset; uint8_t flip; }; diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index a21f7e26..56ed92c9 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -3005,7 +3005,8 @@ void Worker::ProcessFrameImage( const QueueFrameImage& ev ) assert( it != m_pendingFrameImageData.end() ); auto& frames = m_data.framesBase->frames; - const auto fidx = (int64_t)frames.size() - 1 - ev.offset; + const auto fidx = ev.frame - m_data.frameOffset + 1; + assert( fidx < frames.size() ); if( m_onDemand && fidx <= 1 ) { m_pendingFrameImageData.erase( it );