2003-02-16 04:01:45 +00:00
|
|
|
#include "global.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
2002-05-19 01:59:48 +00:00
|
|
|
Class: RageTextureManager
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Desc: See header.
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
|
|
|
|
Chris Danford
|
2001-11-04 19:34:28 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Includes
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
#include "RageTextureManager.h"
|
|
|
|
|
#include "RageBitmapTexture.h"
|
2003-02-20 03:35:37 +00:00
|
|
|
#include "arch/MovieTexture/MovieTexture.h"
|
2001-11-03 10:52:42 +00:00
|
|
|
#include "RageUtil.h"
|
2002-05-01 19:14:55 +00:00
|
|
|
#include "RageLog.h"
|
2002-07-27 19:29:51 +00:00
|
|
|
#include "RageException.h"
|
2003-02-20 03:36:57 +00:00
|
|
|
#include <time.h>
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
RageTextureManager* TEXTUREMAN = NULL;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// constructor/destructor
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2003-04-18 23:55:20 +00:00
|
|
|
RageTextureManager::RageTextureManager()
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageTextureManager::~RageTextureManager()
|
|
|
|
|
{
|
2002-12-30 02:43:52 +00:00
|
|
|
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
2002-09-07 08:20:10 +00:00
|
|
|
i != m_mapPathToTexture.end(); ++i)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-09-07 08:20:10 +00:00
|
|
|
RageTexture* pTexture = i->second;
|
2002-10-12 01:15:39 +00:00
|
|
|
if( pTexture->m_iRefCount )
|
2003-04-25 00:01:35 +00:00
|
|
|
LOG->Trace( "TEXTUREMAN LEAK: '%s', RefCount = %d.", i->first.filename.c_str(), pTexture->m_iRefCount );
|
2002-03-30 20:00:13 +00:00
|
|
|
SAFE_DELETE( pTexture );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-28 20:48:00 +00:00
|
|
|
void RageTextureManager::Update( float fDeltaTime )
|
|
|
|
|
{
|
2002-12-30 02:43:52 +00:00
|
|
|
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
2002-11-28 20:48:00 +00:00
|
|
|
i != m_mapPathToTexture.end(); ++i)
|
|
|
|
|
{
|
|
|
|
|
RageTexture* pTexture = i->second;
|
|
|
|
|
pTexture->Update( fDeltaTime );
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Load/Unload textures from disk
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-12-30 02:43:52 +00:00
|
|
|
RageTexture* RageTextureManager::LoadTexture( RageTextureID ID )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2003-04-25 00:01:35 +00:00
|
|
|
Checkpoint( ssprintf( "RageTextureManager::LoadTexture(%s).", ID.filename.c_str() ) );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-05-22 05:28:37 +00:00
|
|
|
if( ID.iColorDepth == -1 )
|
|
|
|
|
ID.iColorDepth = m_iTextureColorDepth;
|
|
|
|
|
ID.iMaxSize = min( ID.iMaxSize, m_iMaxTextureResolution );
|
|
|
|
|
|
2003-04-19 22:37:00 +00:00
|
|
|
/* We could have two copies of the same bitmap if there are equivalent but
|
|
|
|
|
* different paths, e.g. "Bitmaps\me.bmp" and "..\Rage PC Edition\Bitmaps\me.bmp". */
|
2002-12-30 02:43:52 +00:00
|
|
|
std::map<RageTextureID, RageTexture*>::iterator p = m_mapPathToTexture.find(ID);
|
2003-05-22 05:28:37 +00:00
|
|
|
if(p != m_mapPathToTexture.end())
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2003-02-12 22:32:04 +00:00
|
|
|
/* Found the texture. Just increase the refcount and return it. */
|
2003-05-22 05:28:37 +00:00
|
|
|
RageTexture* pTexture = p->second;
|
|
|
|
|
pTexture->m_iRefCount++;
|
2003-04-22 05:58:29 +00:00
|
|
|
LOG->UnmapLog( "LoadTexture" );
|
2003-05-22 05:28:37 +00:00
|
|
|
return pTexture;
|
2003-01-09 02:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
2003-04-19 22:37:00 +00:00
|
|
|
// The texture is not already loaded. Load it.
|
2003-01-16 22:49:22 +00:00
|
|
|
CString sDir, sFName, sExt;
|
|
|
|
|
splitpath( ID.filename, sDir, sFName, sExt );
|
2003-03-11 19:07:03 +00:00
|
|
|
sExt.MakeLower();
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-01-09 02:58:16 +00:00
|
|
|
RageTexture* pTexture;
|
2003-01-25 08:11:03 +00:00
|
|
|
if( sExt == ".avi" || sExt == ".mpg" || sExt == ".mpeg" )
|
2003-02-20 03:35:37 +00:00
|
|
|
pTexture = MakeRageMovieTexture( ID );
|
2003-01-09 02:58:16 +00:00
|
|
|
else
|
|
|
|
|
pTexture = new RageBitmapTexture( ID );
|
2002-01-24 08:01:24 +00:00
|
|
|
|
2003-01-09 02:58:16 +00:00
|
|
|
m_mapPathToTexture[ID] = pTexture;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-04-22 05:58:29 +00:00
|
|
|
LOG->UnmapLog( "LoadTexture" );
|
2001-11-03 10:52:42 +00:00
|
|
|
return pTexture;
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-18 23:55:20 +00:00
|
|
|
void RageTextureManager::CacheTexture( RageTextureID ID )
|
|
|
|
|
{
|
|
|
|
|
RageTexture* pTexture = LoadTexture( ID );
|
|
|
|
|
pTexture->m_bCacheThis |= true;
|
|
|
|
|
UnloadTexture( pTexture );
|
|
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-04-18 23:55:20 +00:00
|
|
|
void RageTextureManager::UnloadTexture( RageTexture *t )
|
2002-10-19 19:18:40 +00:00
|
|
|
{
|
2003-04-18 23:55:20 +00:00
|
|
|
t->m_iRefCount--;
|
|
|
|
|
ASSERT( t->m_iRefCount >= 0 );
|
2002-10-19 19:18:40 +00:00
|
|
|
|
2003-04-19 22:37:00 +00:00
|
|
|
if( t->m_iRefCount )
|
2003-04-19 22:38:10 +00:00
|
|
|
return; /* Can't unload textures that are still referenced. */
|
2003-04-19 22:37:00 +00:00
|
|
|
|
|
|
|
|
bool bDeleteThis = false;
|
|
|
|
|
|
2003-04-18 23:55:20 +00:00
|
|
|
/* Always unload movies, so we don't waste time decoding.
|
|
|
|
|
*
|
|
|
|
|
* Actually, multiple refs to a movie won't work; they should play independently,
|
|
|
|
|
* but they'll actually share settings. Not worth fixing, since we don't currently
|
|
|
|
|
* using movies for anything except BGAs (though we could).
|
|
|
|
|
*/
|
2003-04-19 22:37:00 +00:00
|
|
|
if( t->IsAMovie() )
|
|
|
|
|
bDeleteThis = true;
|
2003-04-19 23:37:50 +00:00
|
|
|
|
|
|
|
|
/* If m_bDelayedDelete, keep all textures around until we GC. If m_bCacheThis,
|
|
|
|
|
* keep this individual texture, even if m_bDelayedDelete is off. (Would
|
|
|
|
|
* "m_bDelayedDelete" be clearer as "m_bCacheAll"?) */
|
|
|
|
|
if( !m_bDelayedDelete && !t->m_bCacheThis )
|
2003-04-19 22:37:00 +00:00
|
|
|
bDeleteThis = true;
|
|
|
|
|
|
|
|
|
|
if( bDeleteThis )
|
|
|
|
|
DeleteTexture( t );
|
2003-04-18 23:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageTextureManager::DeleteTexture( RageTexture *t )
|
|
|
|
|
{
|
|
|
|
|
ASSERT( t->m_iRefCount==0 );
|
2003-04-25 00:01:35 +00:00
|
|
|
LOG->Trace( "RageTextureManager: deleting '%s'.", t->GetID().filename.c_str() );
|
2003-05-22 05:28:37 +00:00
|
|
|
|
|
|
|
|
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
|
|
|
|
i != m_mapPathToTexture.end(); i++ )
|
|
|
|
|
{
|
|
|
|
|
if( i->second == t )
|
|
|
|
|
{
|
|
|
|
|
m_mapPathToTexture.erase( i ); // remove map entry
|
|
|
|
|
SAFE_DELETE( t ); // free the texture
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT(0); // we tried to delete a texture that wasn't loaded.
|
2003-04-18 23:55:20 +00:00
|
|
|
}
|
2002-10-19 19:18:40 +00:00
|
|
|
|
2003-04-18 23:55:20 +00:00
|
|
|
void RageTextureManager::GarbageCollect( GCType type )
|
|
|
|
|
{
|
2002-10-19 19:18:40 +00:00
|
|
|
// Search for old textures with refcount==0 to unload
|
2003-04-18 23:55:20 +00:00
|
|
|
LOG->Trace("Performing texture garbage collection.");
|
2002-10-19 19:18:40 +00:00
|
|
|
|
2002-12-30 02:43:52 +00:00
|
|
|
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
2002-10-19 19:18:40 +00:00
|
|
|
i != m_mapPathToTexture.end(); )
|
|
|
|
|
{
|
2002-12-30 02:43:52 +00:00
|
|
|
std::map<RageTextureID, RageTexture*>::iterator j = i;
|
2002-10-19 19:18:40 +00:00
|
|
|
i++;
|
|
|
|
|
|
2002-12-30 02:43:52 +00:00
|
|
|
CString sPath = j->first.filename;
|
2003-04-18 23:55:20 +00:00
|
|
|
RageTexture* t = j->second;
|
|
|
|
|
|
2003-04-19 22:37:00 +00:00
|
|
|
if( t->m_iRefCount )
|
|
|
|
|
continue; /* Can't unload textures that are still referenced. */
|
2002-10-19 19:18:40 +00:00
|
|
|
|
2003-04-19 22:37:00 +00:00
|
|
|
bool bDeleteThis = false;
|
|
|
|
|
if( type==cached_textures && !m_bDelayedDelete )
|
|
|
|
|
bDeleteThis = true;
|
|
|
|
|
if( type==delayed_delete )
|
|
|
|
|
bDeleteThis = true;
|
|
|
|
|
|
|
|
|
|
if( bDeleteThis )
|
|
|
|
|
DeleteTexture( t );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
2002-12-30 02:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
2002-04-28 20:42:32 +00:00
|
|
|
|
|
|
|
|
void RageTextureManager::ReloadAll()
|
|
|
|
|
{
|
2002-12-30 02:43:52 +00:00
|
|
|
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
2002-09-07 08:20:10 +00:00
|
|
|
i != m_mapPathToTexture.end(); ++i)
|
2002-04-28 20:42:32 +00:00
|
|
|
{
|
2003-01-26 00:56:13 +00:00
|
|
|
i->second->Reload();
|
2002-04-28 20:42:32 +00:00
|
|
|
}
|
2002-09-07 20:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
2002-11-12 09:21:44 +00:00
|
|
|
/* In some cases, changing the display mode will reset the rendering context,
|
|
|
|
|
* releasing all textures. We don't want to reload immediately if that happens,
|
|
|
|
|
* since we might be changing texture preferences too, which also may have to reload
|
2002-12-30 02:43:52 +00:00
|
|
|
* textures. Instead, tell all textures that their texture ID is invalid, so it
|
2002-11-12 09:21:44 +00:00
|
|
|
* doesn't try to free it later when we really do reload (since that ID might be
|
|
|
|
|
* associated with a different texture). Ack. */
|
|
|
|
|
void RageTextureManager::InvalidateTextures()
|
2002-09-07 20:32:50 +00:00
|
|
|
{
|
2003-01-21 10:32:08 +00:00
|
|
|
std::map<RageTextureID, RageTexture*>::iterator i;
|
|
|
|
|
for( i = m_mapPathToTexture.begin(); i != m_mapPathToTexture.end(); ++i)
|
|
|
|
|
{
|
|
|
|
|
RageTexture* pTexture = i->second;
|
|
|
|
|
pTexture->Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-20 22:12:43 +00:00
|
|
|
/* We're going to have to reload all loaded textures. Let's get rid
|
|
|
|
|
* of all unreferenced textures, so we don't reload a ton of cached
|
2003-01-21 10:32:08 +00:00
|
|
|
* data that we're not necessarily going to use.
|
|
|
|
|
*
|
|
|
|
|
* This must be done *after* we Invalidate above, to let the texture
|
|
|
|
|
* know its OpenGL texture number is invalid. If we don't do that,
|
|
|
|
|
* it'll try to free it. */
|
2003-01-20 22:12:43 +00:00
|
|
|
for( i = m_mapPathToTexture.begin();
|
|
|
|
|
i != m_mapPathToTexture.end(); )
|
|
|
|
|
{
|
|
|
|
|
std::map<RageTextureID, RageTexture*>::iterator j = i;
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
CString sPath = j->first.filename;
|
|
|
|
|
RageTexture* pTexture = j->second;
|
|
|
|
|
if( pTexture->m_iRefCount==0 )
|
|
|
|
|
{
|
|
|
|
|
SAFE_DELETE( pTexture ); // free the texture
|
|
|
|
|
m_mapPathToTexture.erase(j); // and remove the key in the map
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-11-12 09:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
2003-04-18 23:55:20 +00:00
|
|
|
bool RageTextureManager::SetPrefs( int iTextureColorDepth, bool bDelayedDelete, int iMaxTextureResolution )
|
2002-11-12 09:21:44 +00:00
|
|
|
{
|
|
|
|
|
bool need_reload = false;
|
2003-04-18 23:55:20 +00:00
|
|
|
if( m_bDelayedDelete != bDelayedDelete ||
|
2003-01-11 20:15:00 +00:00
|
|
|
m_iTextureColorDepth != iTextureColorDepth ||
|
|
|
|
|
m_iMaxTextureResolution != iMaxTextureResolution )
|
2002-11-12 09:21:44 +00:00
|
|
|
need_reload = true;
|
|
|
|
|
|
2003-04-18 23:55:20 +00:00
|
|
|
m_bDelayedDelete = bDelayedDelete;
|
2002-09-18 20:42:33 +00:00
|
|
|
m_iTextureColorDepth = iTextureColorDepth;
|
2003-01-11 20:15:00 +00:00
|
|
|
m_iMaxTextureResolution = iMaxTextureResolution;
|
2002-11-12 09:21:44 +00:00
|
|
|
|
2002-11-11 04:53:31 +00:00
|
|
|
ASSERT( m_iTextureColorDepth==16 || m_iTextureColorDepth==32 );
|
2002-11-12 09:21:44 +00:00
|
|
|
return need_reload;
|
2002-09-07 20:32:50 +00:00
|
|
|
}
|
2003-04-24 19:51:27 +00:00
|
|
|
|
|
|
|
|
void RageTextureManager::DiagnosticOutput() const
|
|
|
|
|
{
|
|
|
|
|
unsigned cnt = distance(m_mapPathToTexture.begin(), m_mapPathToTexture.end());
|
|
|
|
|
LOG->Trace("%u textures loaded:", cnt);
|
|
|
|
|
for( std::map<RageTextureID, RageTexture*>::const_iterator i = m_mapPathToTexture.begin();
|
|
|
|
|
i != m_mapPathToTexture.end(); ++i )
|
|
|
|
|
{
|
|
|
|
|
const RageTextureID &ID = i->first;
|
|
|
|
|
const RageTexture *tex = i->second;
|
|
|
|
|
|
|
|
|
|
/* This could output much more, but I only need resolution now and I don't
|
|
|
|
|
* want it to be more than one line. */
|
|
|
|
|
LOG->Trace(" %3ix%3i (%2i) %s",
|
|
|
|
|
tex->GetTextureHeight(), tex->GetTextureWidth(),
|
|
|
|
|
tex->m_iRefCount, Basename(ID.filename).c_str() );
|
|
|
|
|
}
|
|
|
|
|
}
|