summaryrefslogtreecommitdiff
path: root/noncore/multimedia
authorsimon <simon>2002-12-11 17:18:17 (UTC)
committer simon <simon>2002-12-11 17:18:17 (UTC)
commit75f0ed4978579eb4b27cdece64c597741ed24b79 (patch) (unidiff)
tree6443323e08a9639ad0db7b5b1852d3804172e70a /noncore/multimedia
parent94461696cfdcf8cdbaabec1400300e546edc447e (diff)
downloadopie-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 :(
Diffstat (limited to 'noncore/multimedia') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/multimedia/opieplayer2/singleton.h49
-rw-r--r--noncore/multimedia/opieplayer2/skin.cpp58
-rw-r--r--noncore/multimedia/opieplayer2/skin.h52
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 @@
1#ifndef SINGLETON_H
2#define SINGLETON_H
3
4template <class Product>
5struct DefaultSingletonCreator
6{
7 static Product *create() { return new Product; }
8};
9
10template <class Product>
11struct NullSingletonCreator
12{
13 static Product *create() { return 0; }
14};
15
16template
17<
18 class T,
19 template <class> class Creator = DefaultSingletonCreator
20>
21class Singleton
22{
23public:
24 static T &self()
25 {
26 if ( !s_self )
27 s_self = Creator<T>::create();
28 return *s_self;
29 }
30
31protected:
32 Singleton()
33 { s_self = static_cast<T *>( this ); }
34 ~Singleton()
35 { s_self = 0; }
36
37private:
38 Singleton( const Singleton<T, Creator> &rhs );
39 Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs );
40
41 static T *s_self;
42};
43
44template <class T, template <class> class Creator>
45T *Singleton<T, Creator>::s_self = 0;
46
47#endif // SINGLETON_H
48/* vim: et sw=4 ts=4
49 */
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
@@ -4,6 +4,8 @@
4#include <qpe/resource.h> 4#include <qpe/resource.h>
5#include <qpe/config.h> 5#include <qpe/config.h>
6 6
7#include <assert.h>
8
7Skin::Skin( const QString &name, const QString &fileNameInfix ) 9Skin::Skin( const QString &name, const QString &fileNameInfix )
8 : m_fileNameInfix( fileNameInfix ) 10 : m_fileNameInfix( fileNameInfix )
9{ 11{
@@ -32,21 +34,21 @@ void Skin::preload( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint butt
32QImage Skin::backgroundImage() const 34QImage Skin::backgroundImage() const
33{ 35{
34 if ( m_backgroundImage.isNull() ) 36 if ( m_backgroundImage.isNull() )
35 m_backgroundImage = QImage( Resource::findPixmap( QString( "%1/background" ).arg( m_skinPath ) ) ); 37 m_backgroundImage = SkinCache::self().loadImage( QString( "%1/background" ).arg( m_skinPath ) );
36 return m_backgroundImage; 38 return m_backgroundImage;
37} 39}
38 40
39QImage Skin::buttonUpImage() const 41QImage Skin::buttonUpImage() const
40{ 42{
41 if ( m_buttonUpImage.isNull() ) 43 if ( m_buttonUpImage.isNull() )
42 m_buttonUpImage = QImage( Resource::findPixmap( QString( "%1/skin%2_up" ).arg( m_skinPath ).arg( m_fileNameInfix ) ) ); 44 m_buttonUpImage = SkinCache::self().loadImage( QString( "%1/skin%2_up" ).arg( m_skinPath ).arg( m_fileNameInfix ) );
43 return m_buttonUpImage; 45 return m_buttonUpImage;
44} 46}
45 47
46QImage Skin::buttonDownImage() const 48QImage Skin::buttonDownImage() const
47{ 49{
48 if ( m_buttonDownImage.isNull() ) 50 if ( m_buttonDownImage.isNull() )
49 m_buttonDownImage = QImage( Resource::findPixmap( QString( "%1/skin%2_down" ).arg( m_skinPath ).arg( m_fileNameInfix ) ) ); 51 m_buttonDownImage = SkinCache::self().loadImage( QString( "%1/skin%2_down" ).arg( m_skinPath ).arg( m_fileNameInfix ) );
50 return m_buttonDownImage; 52 return m_buttonDownImage;
51} 53}
52 54
@@ -86,7 +88,7 @@ QImage Skin::buttonMaskImage( const QString &fileName ) const
86 if ( it == m_buttonMasks.end() ) { 88 if ( it == m_buttonMasks.end() ) {
87 QString prefix = m_skinPath + QString::fromLatin1( "/skin%1_mask_" ).arg( m_fileNameInfix ); 89 QString prefix = m_skinPath + QString::fromLatin1( "/skin%1_mask_" ).arg( m_fileNameInfix );
88 QString path = prefix + fileName + ".png"; 90 QString path = prefix + fileName + ".png";
89 it = m_buttonMasks.insert( fileName, QImage( Resource::findPixmap( path ) ) ); 91 it = m_buttonMasks.insert( fileName, SkinCache::self().loadImage( path ) );
90 } 92 }
91 return *it; 93 return *it;
92} 94}
@@ -98,5 +100,53 @@ QString Skin::defaultSkinName()
98 return cfg.readEntry( "Skin", "default" ); 100 return cfg.readEntry( "Skin", "default" );
99} 101}
100 102
103SkinCache::SkinCache()
104{
105 m_cache.setAutoDelete( true );
106}
107
108QImage SkinCache::loadImage( const QString &name )
109{
110 ThreadUtil::AutoLock lock( m_cacheGuard );
111
112 QImage *image = m_cache.find( name );
113 if ( image ) {
114 qDebug( "cache hit for %s", name.ascii() );
115 return *image;
116 }
117
118 image = new QImage( Resource::findPixmap( name ) );
119 m_cache.insert( name, image );
120 return *image;
121}
122
123SkinLoader::SkinLoader()
124{
125}
126
127void SkinLoader::schedule( const QString &skinName, const QString &fileNameInfix,
128 const MediaWidget::SkinButtonInfo *skinButtonInfo, const uint buttonCount )
129{
130 assert( isRunning() == false );
131
132 pendingSkins << Info( skinName, fileNameInfix, skinButtonInfo, buttonCount );
133}
134
135void SkinLoader::run()
136{
137 qDebug( "SkinLoader::run()" );
138 for ( InfoList::ConstIterator it = pendingSkins.begin(); it != pendingSkins.end(); ++it )
139 load( *it );
140 qDebug( "SkinLoader is done." );
141}
142
143void SkinLoader::load( const Info &nfo )
144{
145 qDebug( "preloading %s with infix %s", nfo.skinName.ascii(), nfo.fileNameInfix.ascii() );
146
147 Skin skin( nfo.skinName, nfo.fileNameInfix );
148 skin.preload( nfo.skinButtonInfo, nfo.buttonCount );
149}
150
101/* vim: et sw=4 ts=4 151/* vim: et sw=4 ts=4
102 */ 152 */
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
@@ -4,8 +4,11 @@
4#include <qstring.h> 4#include <qstring.h>
5#include <qimage.h> 5#include <qimage.h>
6#include <qmap.h> 6#include <qmap.h>
7#include <qdict.h>
7 8
8#include "mediawidget.h" 9#include "mediawidget.h"
10#include "threadutil.h"
11#include "singleton.h"
9 12
10class Skin 13class Skin
11{ 14{
@@ -45,6 +48,55 @@ private:
45 Skin &operator=( const Skin & ); 48 Skin &operator=( const Skin & );
46}; 49};
47 50
51class SkinCache : public Singleton<SkinCache>
52{
53public:
54 SkinCache();
55
56 QImage loadImage( const QString &name );
57
58private:
59 typedef QDict<QImage> ImageCache;
60
61 ImageCache m_cache;
62
63 ThreadUtil::Mutex m_cacheGuard;
64};
65
66class SkinLoader : public ThreadUtil::Thread
67{
68public:
69 SkinLoader();
70
71 void schedule( const QString &skinName, const QString &fileNameInfix,
72 const MediaWidget::SkinButtonInfo *skinButtonInfo, const uint buttonCount );
73
74protected:
75 virtual void run();
76
77private:
78 struct Info
79 {
80 Info() : skinButtonInfo( 0 ), buttonCount( 0 ) {}
81 Info( const QString &_skinName, const QString &_fileNameInfix,
82 const MediaWidget::SkinButtonInfo *_skinButtonInfo, const uint _buttonCount )
83 : skinName( _skinName ), fileNameInfix( _fileNameInfix ),
84 skinButtonInfo( _skinButtonInfo ), buttonCount( _buttonCount )
85 {}
86
87 const QString skinName;
88 const QString fileNameInfix;
89 const MediaWidget::SkinButtonInfo *skinButtonInfo;
90 const uint buttonCount;
91 };
92 typedef QValueList<Info> InfoList;
93
94 void load( const Info &nfo );
95
96 InfoList pendingSkins;
97 ThreadUtil::Mutex guard;
98};
99
48#endif // SKIN_H 100#endif // SKIN_H
49/* vim: et sw=4 ts=4 101/* vim: et sw=4 ts=4
50 */ 102 */