use RageTextureLock to avoid a texture copy

remove threaded textures for now at least; makes RageTextureLock
much harder, causes a lot of nondeterministically-timed competing
CPU use, and we can probably optimize more easily this way
This commit is contained in:
Glenn Maynard
2007-03-22 19:11:48 +00:00
parent 5d0d26164c
commit 4139261ac7
2 changed files with 54 additions and 129 deletions
@@ -16,8 +16,7 @@
MovieTexture_Generic::MovieTexture_Generic( RageTextureID ID, MovieDecoder *pDecoder ): MovieTexture_Generic::MovieTexture_Generic( RageTextureID ID, MovieDecoder *pDecoder ):
RageMovieTexture( ID ), RageMovieTexture( ID )
m_BufferFinished( "BufferFinished", 0 )
{ {
LOG->Trace( "MovieTexture_Generic::MovieTexture_Generic(%s)", ID.filename.c_str() ); LOG->Trace( "MovieTexture_Generic::MovieTexture_Generic(%s)", ID.filename.c_str() );
@@ -27,14 +26,13 @@ MovieTexture_Generic::MovieTexture_Generic( RageTextureID ID, MovieDecoder *pDec
m_pRenderTarget = NULL; m_pRenderTarget = NULL;
m_pTextureIntermediate = NULL; m_pTextureIntermediate = NULL;
m_bLoop = true; m_bLoop = true;
m_State = DECODER_QUIT; /* it's quit until we call StartThread */
m_pSurface = NULL; m_pSurface = NULL;
m_pTextureLock = NULL;
m_ImageWaiting = FRAME_NONE; m_ImageWaiting = FRAME_NONE;
m_fRate = 1; m_fRate = 1;
m_bWantRewind = false; m_bWantRewind = false;
m_fClock = 0; m_fClock = 0;
m_bFrameSkipMode = false; m_bFrameSkipMode = false;
m_bThreaded = PREFSMAN->m_bThreadedMovieDecode.Get();
m_pSprite = new Sprite; m_pSprite = new Sprite;
} }
@@ -48,7 +46,7 @@ RString MovieTexture_Generic::Init()
CreateFrameRects(); CreateFrameRects();
/* Decode one frame, to guarantee that the texture is drawn when this function returns. */ /* Decode one frame, to guarantee that the texture is drawn when this function returns. */
int ret = m_pDecoder->GetFrame( m_pSurface, -1 ); int ret = GetFrame( -1 );
if( ret == -1 ) if( ret == -1 )
return ssprintf( "%s: error getting first frame", GetID().filename.c_str() ); return ssprintf( "%s: error getting first frame", GetID().filename.c_str() );
if( ret == 0 ) if( ret == 0 )
@@ -67,14 +65,11 @@ RString MovieTexture_Generic::Init()
CHECKPOINT; CHECKPOINT;
StartThread();
return RString(); return RString();
} }
MovieTexture_Generic::~MovieTexture_Generic() MovieTexture_Generic::~MovieTexture_Generic()
{ {
StopThread();
if( m_pDecoder ) if( m_pDecoder )
m_pDecoder->Close(); m_pDecoder->Close();
@@ -93,6 +88,9 @@ void MovieTexture_Generic::DestroyTexture()
delete m_pSurface; delete m_pSurface;
m_pSurface = NULL; m_pSurface = NULL;
delete m_pTextureLock;
m_pTextureLock = NULL;
if( m_uTexHandle ) if( m_uTexHandle )
{ {
DISPLAY->DeleteTexture( m_uTexHandle ); DISPLAY->DeleteTexture( m_uTexHandle );
@@ -207,7 +205,19 @@ void MovieTexture_Generic::CreateTexture()
m_iTextureHeight = power_of_two( m_iImageHeight ); m_iTextureHeight = power_of_two( m_iImageHeight );
MovieDecoderPixelFormatYCbCr fmt = PixelFormatYCbCr_Invalid; MovieDecoderPixelFormatYCbCr fmt = PixelFormatYCbCr_Invalid;
if( m_pSurface == NULL ) if( m_pSurface == NULL )
m_pSurface = m_pDecoder->CreateCompatibleSurface( m_iImageWidth, m_iImageHeight, TEXTUREMAN->GetPrefs().m_iMovieColorDepth == 32, fmt ); {
ASSERT( m_pTextureLock == NULL );
m_pTextureLock = DISPLAY->CreateTextureLock();
m_pSurface = m_pDecoder->CreateCompatibleSurface( m_iImageWidth, m_iImageHeight,
TEXTUREMAN->GetPrefs().m_iMovieColorDepth == 32, fmt );
if( m_pTextureLock != NULL )
{
delete [] m_pSurface->pixels;
m_pSurface->pixels = NULL;
}
}
PixelFormat pixfmt = DISPLAY->FindPixelFormat( m_pSurface->format->BitsPerPixel, PixelFormat pixfmt = DISPLAY->FindPixelFormat( m_pSurface->format->BitsPerPixel,
m_pSurface->format->Mask[0], m_pSurface->format->Mask[0],
@@ -294,9 +304,6 @@ bool MovieTexture_Generic::DecodeFrame()
bool bTriedRewind = false; bool bTriedRewind = false;
do do
{ {
if( m_State == DECODER_QUIT )
return false;
if( m_bWantRewind ) if( m_bWantRewind )
{ {
if( bTriedRewind ) if( bTriedRewind )
@@ -324,7 +331,7 @@ bool MovieTexture_Generic::DecodeFrame()
if( m_bFrameSkipMode && m_fClock > m_pDecoder->GetTimestamp() ) if( m_bFrameSkipMode && m_fClock > m_pDecoder->GetTimestamp() )
fTargetTime = m_fClock; fTargetTime = m_fClock;
int ret = m_pDecoder->GetFrame( m_pSurface, fTargetTime ); int ret = GetFrame( fTargetTime );
if( ret == -1 ) if( ret == -1 )
return false; return false;
@@ -411,61 +418,6 @@ void MovieTexture_Generic::DiscardFrame()
m_ImageWaiting = FRAME_NONE; m_ImageWaiting = FRAME_NONE;
} }
void MovieTexture_Generic::DecoderThread()
{
#if defined(_WINDOWS)
/* Windows likes to boost priority when processes come out of a wait state. We don't
* want that, since it'll result in us having a small priority boost after each movie
* frame, resulting in skips in the gameplay thread. */
if( !SetThreadPriorityBoost(GetCurrentThread(), TRUE) && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED )
LOG->Warn( werr_ssprintf(GetLastError(), "SetThreadPriorityBoost failed") );
#endif
CHECKPOINT;
while( m_State != DECODER_QUIT )
{
if( m_ImageWaiting == FRAME_NONE )
DecodeFrame();
/* If we still have no frame, we're at EOF and we didn't loop. */
if( m_ImageWaiting != FRAME_DECODED )
{
usleep( 10000 );
continue;
}
const float fTime = CheckFrameTime();
if( fTime > 0 ) // not time to decode a new frame yet
{
/* This needs to be relatively short so that we wake up quickly
* from being paused or for changes in m_fRate. */
usleep( 10000 );
}
else // fTime == 0
{
{
/* The only reason m_BufferFinished might be non-zero right now (before
* ConvertFrame()) is if we're quitting. */
int n = m_BufferFinished.GetValue();
ASSERT_M( n == 0 || m_State == DECODER_QUIT, ssprintf("%i, %i", n, m_State) );
}
m_ImageWaiting = FRAME_WAITING;
/* We just went into FRAME_WAITING. Don't actually check; the main thread
* will change us back to FRAME_NONE without locking, and poke m_BufferFinished.
* Don't time out on this; if a new screen has started loading, this might not
* return for a while. */
m_BufferFinished.Wait( false );
/* If the frame wasn't used, then we must be shutting down. */
ASSERT_M( m_ImageWaiting == FRAME_NONE || m_State == DECODER_QUIT, ssprintf("%i, %i", m_ImageWaiting, m_State) );
}
}
CHECKPOINT;
}
void MovieTexture_Generic::Update(float fDeltaTime) void MovieTexture_Generic::Update(float fDeltaTime)
{ {
/* We might need to decode more than one frame per update. However, there /* We might need to decode more than one frame per update. However, there
@@ -474,7 +426,6 @@ void MovieTexture_Generic::Update(float fDeltaTime)
int iMax = 4; int iMax = 4;
while( --iMax ) while( --iMax )
{ {
if( !m_bThreaded )
{ {
/* If we don't have a frame decoded, decode one. */ /* If we don't have a frame decoded, decode one. */
if( m_ImageWaiting == FRAME_NONE ) if( m_ImageWaiting == FRAME_NONE )
@@ -499,17 +450,29 @@ void MovieTexture_Generic::Update(float fDeltaTime)
CHECKPOINT; CHECKPOINT;
UpdateFrame(); UpdateFrame();
if( m_bThreaded )
m_BufferFinished.Post();
} }
LOG->MapLog( "movie_looping", "MovieTexture_Generic::Update looping" ); LOG->MapLog( "movie_looping", "MovieTexture_Generic::Update looping" );
} }
int MovieTexture_Generic::GetFrame( float fTargetTime )
{
/* Just in case we were invalidated: */
CreateTexture();
/* Call from the main thread when m_ImageWaiting == FRAME_WAITING to update the if( m_pTextureLock != NULL )
* texture. Sets FRAME_NONE. Does not signal m_BufferFinished. */ {
int iHandle = m_pTextureIntermediate != NULL? m_pTextureIntermediate->GetTexHandle(): this->GetTexHandle();
m_pTextureLock->Lock( iHandle, m_pSurface );
}
int ret = m_pDecoder->GetFrame( m_pSurface, fTargetTime );
if( m_pTextureLock != NULL )
m_pTextureLock->Unlock( m_pSurface, ret != -1 );
return ret;
}
/* Call when m_ImageWaiting == FRAME_WAITING to update the texture. Sets FRAME_NONE. */
void MovieTexture_Generic::UpdateFrame() void MovieTexture_Generic::UpdateFrame()
{ {
ASSERT_M( m_ImageWaiting == FRAME_WAITING, ssprintf("%i", m_ImageWaiting) ); ASSERT_M( m_ImageWaiting == FRAME_WAITING, ssprintf("%i", m_ImageWaiting) );
@@ -521,11 +484,13 @@ void MovieTexture_Generic::UpdateFrame()
{ {
CHECKPOINT; CHECKPOINT;
DISPLAY->UpdateTexture( /* If we have no m_pTextureLock, we still have to upload the texture. */
m_pTextureIntermediate->GetTexHandle(), if( m_pTextureLock == NULL )
m_pSurface, DISPLAY->UpdateTexture(
0, 0, m_pTextureIntermediate->GetTexHandle(),
m_pSurface->w, m_pSurface->h ); m_pSurface,
0, 0,
m_pSurface->w, m_pSurface->h );
CHECKPOINT; CHECKPOINT;
m_pRenderTarget->BeginRenderingTo( false ); m_pRenderTarget->BeginRenderingTo( false );
@@ -535,11 +500,12 @@ void MovieTexture_Generic::UpdateFrame()
else else
{ {
CHECKPOINT; CHECKPOINT;
DISPLAY->UpdateTexture( if( m_pTextureLock == NULL )
m_uTexHandle, DISPLAY->UpdateTexture(
m_pSurface, m_uTexHandle,
0, 0, m_pSurface,
m_iImageWidth, m_iImageHeight ); 0, 0,
m_iImageWidth, m_iImageHeight );
CHECKPOINT; CHECKPOINT;
} }
@@ -562,43 +528,8 @@ void MovieTexture_Generic::Reload()
{ {
} }
void MovieTexture_Generic::StartThread()
{
ASSERT( m_State == DECODER_QUIT );
m_State = DECODER_RUNNING;
m_DecoderThread.SetName( ssprintf("MovieTexture_Generic(%s)", GetID().filename.c_str()) );
if( m_bThreaded )
m_DecoderThread.Create( DecoderThread_start, this );
}
void MovieTexture_Generic::StopThread()
{
if( !m_DecoderThread.IsCreated() )
return;
LOG->Trace("Shutting down decoder thread ...");
m_State = DECODER_QUIT;
/* Make sure we don't deadlock waiting for m_BufferFinished. */
m_BufferFinished.Post();
CHECKPOINT;
m_DecoderThread.Wait();
CHECKPOINT;
m_ImageWaiting = FRAME_NONE;
/* Clear the above post, if the thread didn't. */
m_BufferFinished.TryWait();
LOG->Trace("Decoder thread shut down.");
}
void MovieTexture_Generic::SetPosition( float fSeconds ) void MovieTexture_Generic::SetPosition( float fSeconds )
{ {
ASSERT( m_State != DECODER_QUIT );
/* We can reset to 0, but I don't think this API supports fast seeking /* We can reset to 0, but I don't think this API supports fast seeking
* yet. I don't think we ever actually seek except to 0 right now, * yet. I don't think we ever actually seek except to 0 right now,
* anyway. XXX */ * anyway. XXX */
@@ -2,10 +2,10 @@
#define RAGE_MOVIE_TEXTURE_GENERIC_H #define RAGE_MOVIE_TEXTURE_GENERIC_H
#include "MovieTexture.h" #include "MovieTexture.h"
#include "RageThreads.h"
class FFMpeg_Helper; class FFMpeg_Helper;
struct RageSurface; struct RageSurface;
struct RageTextureLock;
class RageTextureRenderTarget; class RageTextureRenderTarget;
class Sprite; class Sprite;
@@ -94,6 +94,8 @@ public:
static EffectMode GetEffectMode( MovieDecoderPixelFormatYCbCr fmt ); static EffectMode GetEffectMode( MovieDecoderPixelFormatYCbCr fmt );
private: private:
int GetFrame( float fTargetTime );
MovieDecoder *m_pDecoder; MovieDecoder *m_pDecoder;
float m_fRate; float m_fRate;
@@ -104,7 +106,6 @@ private:
} m_ImageWaiting; } m_ImageWaiting;
bool m_bLoop; bool m_bLoop;
bool m_bWantRewind; bool m_bWantRewind;
bool m_bThreaded;
/* /*
* Only the main thread can change m_State. * Only the main thread can change m_State.
@@ -126,26 +127,19 @@ private:
RageSurface *m_pSurface; RageSurface *m_pSurface;
RageSemaphore m_BufferFinished; RageTextureLock *m_pTextureLock;
/* The time the movie is actually at: */ /* The time the movie is actually at: */
float m_fClock; float m_fClock;
bool m_bFrameSkipMode; bool m_bFrameSkipMode;
static int DecoderThread_start(void *p) { ((MovieTexture_Generic *)(p))->DecoderThread(); return 0; }
void DecoderThread();
RageThread m_DecoderThread;
void UpdateFrame(); void UpdateFrame();
void CreateTexture(); void CreateTexture();
void DestroyTexture(); void DestroyTexture();
void StartThread();
void StopThread();
bool DecodeFrame(); bool DecodeFrame();
float CheckFrameTime(); float CheckFrameTime();
void DiscardFrame();
}; };
#endif #endif