author | simon <simon> | 2002-12-11 17:18:17 (UTC) |
---|---|---|
committer | simon <simon> | 2002-12-11 17:18:17 (UTC) |
commit | 75f0ed4978579eb4b27cdece64c597741ed24b79 (patch) (side-by-side diff) | |
tree | 6443323e08a9639ad0db7b5b1852d3804172e70a /noncore | |
parent | 94461696cfdcf8cdbaabec1400300e546edc447e (diff) | |
download | opie-75f0ed4978579eb4b27cdece64c597741ed24b79.zip opie-75f0ed4978579eb4b27cdece64c597741ed24b79.tar.gz opie-75f0ed4978579eb4b27cdece64c597741ed24b79.tar.bz2 |
- added a skin cache and a threaded skin loader. looks like the latter
we have to disable though, because pure image loading with qt is
anything but threadsafe :(
-rw-r--r-- | noncore/multimedia/opieplayer2/singleton.h | 49 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/skin.cpp | 58 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/skin.h | 52 |
3 files changed, 155 insertions, 4 deletions
diff --git a/noncore/multimedia/opieplayer2/singleton.h b/noncore/multimedia/opieplayer2/singleton.h new file mode 100644 index 0000000..35b3dff --- a/dev/null +++ b/noncore/multimedia/opieplayer2/singleton.h @@ -0,0 +1,49 @@ +#ifndef SINGLETON_H +#define SINGLETON_H + +template <class Product> +struct DefaultSingletonCreator +{ + static Product *create() { return new Product; } +}; + +template <class Product> +struct NullSingletonCreator +{ + static Product *create() { return 0; } +}; + +template +< + class T, + template <class> class Creator = DefaultSingletonCreator +> +class Singleton +{ +public: + static T &self() + { + if ( !s_self ) + s_self = Creator<T>::create(); + return *s_self; + } + +protected: + Singleton() + { s_self = static_cast<T *>( this ); } + ~Singleton() + { s_self = 0; } + +private: + Singleton( const Singleton<T, Creator> &rhs ); + Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs ); + + static T *s_self; +}; + +template <class T, template <class> class Creator> +T *Singleton<T, Creator>::s_self = 0; + +#endif // SINGLETON_H +/* vim: et sw=4 ts=4 + */ diff --git a/noncore/multimedia/opieplayer2/skin.cpp b/noncore/multimedia/opieplayer2/skin.cpp index 8281b20..0de3023 100644 --- a/noncore/multimedia/opieplayer2/skin.cpp +++ b/noncore/multimedia/opieplayer2/skin.cpp @@ -1,102 +1,152 @@ #include "skin.h" #include <qpe/resource.h> #include <qpe/config.h> +#include <assert.h> + Skin::Skin( const QString &name, const QString &fileNameInfix ) : m_fileNameInfix( fileNameInfix ) { init( name ); } Skin::Skin( const QString &fileNameInfix ) : m_fileNameInfix( fileNameInfix ) { init( defaultSkinName() ); } void Skin::init( const QString &name ) { m_skinPath = "opieplayer2/skins/" + name; } void Skin::preload( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint buttonCount ) { backgroundImage(); buttonUpImage(); buttonDownImage(); ( void )buttonMask( skinButtonInfo, buttonCount ); } QImage Skin::backgroundImage() const { if ( m_backgroundImage.isNull() ) - m_backgroundImage = QImage( Resource::findPixmap( QString( "%1/background" ).arg( m_skinPath ) ) ); + m_backgroundImage = SkinCache::self().loadImage( QString( "%1/background" ).arg( m_skinPath ) ); return m_backgroundImage; } QImage Skin::buttonUpImage() const { if ( m_buttonUpImage.isNull() ) - m_buttonUpImage = QImage( Resource::findPixmap( QString( "%1/skin%2_up" ).arg( m_skinPath ).arg( m_fileNameInfix ) ) ); + m_buttonUpImage = SkinCache::self().loadImage( QString( "%1/skin%2_up" ).arg( m_skinPath ).arg( m_fileNameInfix ) ); return m_buttonUpImage; } QImage Skin::buttonDownImage() const { if ( m_buttonDownImage.isNull() ) - m_buttonDownImage = QImage( Resource::findPixmap( QString( "%1/skin%2_down" ).arg( m_skinPath ).arg( m_fileNameInfix ) ) ); + m_buttonDownImage = SkinCache::self().loadImage( QString( "%1/skin%2_down" ).arg( m_skinPath ).arg( m_fileNameInfix ) ); return m_buttonDownImage; } QImage Skin::buttonMask( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint buttonCount ) const { if ( !m_buttonMask.isNull() ) return m_buttonMask; QSize buttonAreaSize = buttonUpImage().size(); m_buttonMask = QImage( buttonAreaSize, 8, 255 ); m_buttonMask.fill( 0 ); for ( uint i = 0; i < buttonCount; ++i ) addButtonToMask( skinButtonInfo[ i ].command + 1, buttonMaskImage( skinButtonInfo[ i ].fileName ) ); return m_buttonMask; } void Skin::addButtonToMask( int tag, const QImage &maskImage ) const { if ( maskImage.isNull() ) return; uchar **dest = m_buttonMask.jumpTable(); for ( int y = 0; y < m_buttonMask.height(); y++ ) { uchar *line = dest[y]; for ( int x = 0; x < m_buttonMask.width(); x++ ) if ( !qRed( maskImage.pixel( x, y ) ) ) line[x] = tag; } } QImage Skin::buttonMaskImage( const QString &fileName ) const { ButtonMaskImageMap::Iterator it = m_buttonMasks.find( fileName ); if ( it == m_buttonMasks.end() ) { QString prefix = m_skinPath + QString::fromLatin1( "/skin%1_mask_" ).arg( m_fileNameInfix ); QString path = prefix + fileName + ".png"; - it = m_buttonMasks.insert( fileName, QImage( Resource::findPixmap( path ) ) ); + it = m_buttonMasks.insert( fileName, SkinCache::self().loadImage( path ) ); } return *it; } QString Skin::defaultSkinName() { Config cfg( "OpiePlayer" ); cfg.setGroup( "Options" ); return cfg.readEntry( "Skin", "default" ); } +SkinCache::SkinCache() +{ + m_cache.setAutoDelete( true ); +} + +QImage SkinCache::loadImage( const QString &name ) +{ + ThreadUtil::AutoLock lock( m_cacheGuard ); + + QImage *image = m_cache.find( name ); + if ( image ) { + qDebug( "cache hit for %s", name.ascii() ); + return *image; + } + + image = new QImage( Resource::findPixmap( name ) ); + m_cache.insert( name, image ); + return *image; +} + +SkinLoader::SkinLoader() +{ +} + +void SkinLoader::schedule( const QString &skinName, const QString &fileNameInfix, + const MediaWidget::SkinButtonInfo *skinButtonInfo, const uint buttonCount ) +{ + assert( isRunning() == false ); + + pendingSkins << Info( skinName, fileNameInfix, skinButtonInfo, buttonCount ); +} + +void SkinLoader::run() +{ + qDebug( "SkinLoader::run()" ); + for ( InfoList::ConstIterator it = pendingSkins.begin(); it != pendingSkins.end(); ++it ) + load( *it ); + qDebug( "SkinLoader is done." ); +} + +void SkinLoader::load( const Info &nfo ) +{ + qDebug( "preloading %s with infix %s", nfo.skinName.ascii(), nfo.fileNameInfix.ascii() ); + + Skin skin( nfo.skinName, nfo.fileNameInfix ); + skin.preload( nfo.skinButtonInfo, nfo.buttonCount ); +} + /* vim: et sw=4 ts=4 */ diff --git a/noncore/multimedia/opieplayer2/skin.h b/noncore/multimedia/opieplayer2/skin.h index 58f1849..c15d9dc 100644 --- a/noncore/multimedia/opieplayer2/skin.h +++ b/noncore/multimedia/opieplayer2/skin.h @@ -1,50 +1,102 @@ #ifndef SKIN_H #define SKIN_H #include <qstring.h> #include <qimage.h> #include <qmap.h> +#include <qdict.h> #include "mediawidget.h" +#include "threadutil.h" +#include "singleton.h" class Skin { public: Skin( const QString &name, const QString &fileNameInfix ); Skin( const QString &fileNameInfix ); void preload( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint buttonCount ); QImage backgroundImage() const; QImage buttonUpImage() const; QImage buttonDownImage() const; QImage buttonMask( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint buttonCount ) const; QImage buttonMaskImage( const QString &fileName ) const; static QString defaultSkinName(); private: void init( const QString &name ); void addButtonToMask( int tag, const QImage &maskImage ) const; QString m_fileNameInfix; QString m_skinPath; typedef QMap<QString, QImage> ButtonMaskImageMap; mutable QImage m_backgroundImage; mutable QImage m_buttonUpImage; mutable QImage m_buttonDownImage; mutable QImage m_buttonMask; mutable ButtonMaskImageMap m_buttonMasks; Skin( const Skin & ); Skin &operator=( const Skin & ); }; +class SkinCache : public Singleton<SkinCache> +{ +public: + SkinCache(); + + QImage loadImage( const QString &name ); + +private: + typedef QDict<QImage> ImageCache; + + ImageCache m_cache; + + ThreadUtil::Mutex m_cacheGuard; +}; + +class SkinLoader : public ThreadUtil::Thread +{ +public: + SkinLoader(); + + void schedule( const QString &skinName, const QString &fileNameInfix, + const MediaWidget::SkinButtonInfo *skinButtonInfo, const uint buttonCount ); + +protected: + virtual void run(); + +private: + struct Info + { + Info() : skinButtonInfo( 0 ), buttonCount( 0 ) {} + Info( const QString &_skinName, const QString &_fileNameInfix, + const MediaWidget::SkinButtonInfo *_skinButtonInfo, const uint _buttonCount ) + : skinName( _skinName ), fileNameInfix( _fileNameInfix ), + skinButtonInfo( _skinButtonInfo ), buttonCount( _buttonCount ) + {} + + const QString skinName; + const QString fileNameInfix; + const MediaWidget::SkinButtonInfo *skinButtonInfo; + const uint buttonCount; + }; + typedef QValueList<Info> InfoList; + + void load( const Info &nfo ); + + InfoList pendingSkins; + ThreadUtil::Mutex guard; +}; + #endif // SKIN_H /* vim: et sw=4 ts=4 */ |