]> git.sesse.net Git - vlc/commitdiff
skins2: implement a ArtManager singleton
authorErwan Tulou <erwan10@videolan.org>
Tue, 20 Jul 2010 09:12:01 +0000 (11:12 +0200)
committerErwan Tulou <erwan10@videolan.org>
Tue, 20 Jul 2010 10:42:09 +0000 (12:42 +0200)
This singleton is based on the same pattern as VlcManager or VoutManager.
It is intended to load and cache art for the whole skin.
Caching makes sense because 1/ several controls within a skin can display
the current art, and 2/ art is likely to be reused by successive inputs
(music album, repeat). Yet, caching is limited to two art for memory reason.

modules/gui/skins2/Modules.am
modules/gui/skins2/controls/ctrl_image.cpp
modules/gui/skins2/src/art_manager.cpp [moved from modules/gui/skins2/src/art_bitmap.cpp with 62% similarity]
modules/gui/skins2/src/art_manager.hpp [moved from modules/gui/skins2/src/art_bitmap.hpp with 61% similarity]
modules/gui/skins2/src/skin_common.hpp
modules/gui/skins2/src/skin_main.cpp

index 259bc78edc52856c2e9586fa655b14ac04673bc9..be5e4e2e5bfd2caac4f585ffd5577e96486decc3 100644 (file)
@@ -107,8 +107,8 @@ SOURCES_skins2 = \
        src/dialogs.hpp \
        src/file_bitmap.cpp \
        src/file_bitmap.hpp \
-       src/art_bitmap.cpp \
-       src/art_bitmap.hpp \
+       src/art_manager.cpp \
+       src/art_manager.hpp \
        src/ft2_bitmap.cpp \
        src/ft2_bitmap.hpp \
        src/ft2_font.cpp \
index 3de4fc97ee4d515dead6df3dc69774e40249717a..c9c35ca6883203baeaede832471950eea7e4c4e3 100644 (file)
@@ -29,7 +29,7 @@
 #include "../src/os_graphics.hpp"
 #include "../src/vlcproc.hpp"
 #include "../src/scaled_bitmap.hpp"
-#include "../src/art_bitmap.hpp"
+#include "../src/art_manager.hpp"
 #include "../utils/position.hpp"
 
 
@@ -51,8 +51,6 @@ CtrlImage::CtrlImage( intf_thread_t *pIntf, GenericBitmap &rBitmap,
     {
         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
         pVlcProc->getStreamArtVar().addObserver( this );
-
-        ArtBitmap::initArtBitmap( getIntf() );
     }
 
 }
@@ -66,8 +64,6 @@ CtrlImage::~CtrlImage()
     {
         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
         pVlcProc->getStreamArtVar().delObserver( this );
-
-        ArtBitmap::freeArtBitmap( );
     }
 }
 
@@ -218,7 +214,8 @@ void CtrlImage::onUpdate( Subject<VarString> &rVariable, void* arg )
     if( &rVariable == &pVlcProc->getStreamArtVar() )
     {
         string str = ((VarString&)rVariable).get();
-        GenericBitmap* pArt = (GenericBitmap*) ArtBitmap::getArtBitmap( str );
+        ArtManager* pArtManager = ArtManager::instance( getIntf() );
+        GenericBitmap* pArt = (GenericBitmap*) pArtManager->getArtBitmap( str );
 
         m_pBitmap = pArt ? pArt : m_pOriginalBitmap;
 
similarity index 62%
rename from modules/gui/skins2/src/art_bitmap.cpp
rename to modules/gui/skins2/src/art_manager.cpp
index d24784dc2cfc52ced4c68103484a8f0b493bb2a9..f9e3041032041b1105c0d6a5515867a6ac11c400 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * art_bitmap.cpp
+ * art_manager.cpp
  *****************************************************************************
  * Copyright (C) 2010 the VideoLAN team
  * $Id$
 # include "config.h"
 #endif
 
-#include "art_bitmap.hpp"
+#include "art_manager.hpp"
 #include <vlc_image.h>
 
+#define MAX_ART_CACHED    2
 
-// static variables
-intf_thread_t*    ArtBitmap::m_pIntf = NULL;
-image_handler_t*  ArtBitmap::m_pImageHandler = NULL;
-list<ArtBitmap*>  ArtBitmap::m_listBitmap;
 
-ArtBitmap::ArtBitmap( string uriName ):
-    FileBitmap( m_pIntf, m_pImageHandler, uriName, -1 ), m_uriName( uriName )
+ArtManager *ArtManager::instance( intf_thread_t *pIntf )
 {
+    if( pIntf->p_sys->p_artManager == NULL )
+    {
+        pIntf->p_sys->p_artManager = new ArtManager( pIntf );
+    }
+
+    return pIntf->p_sys->p_artManager;
 }
 
-void ArtBitmap::initArtBitmap( intf_thread_t* pIntf )
+
+void ArtManager::destroy( intf_thread_t *pIntf )
 {
-    if( m_pIntf )
-        return;
+    delete pIntf->p_sys->p_artManager;
+    pIntf->p_sys->p_artManager = NULL;
+}
 
-    // retain reference to skins interface
-    m_pIntf = pIntf;
 
+ArtManager::ArtManager( intf_thread_t* pIntf ) : SkinObject( pIntf )
+{
     // initialize handler
     m_pImageHandler = image_HandlerCreate( pIntf );
 
     if( !m_pImageHandler )
-        msg_Err( m_pIntf, "initialization of art bitmaps failed" );
+        msg_Err( getIntf(), "initialization of art manager failed" );
+}
+
+
+ArtManager::~ArtManager( )
+{
+    if( m_pImageHandler )
+    {
+        image_HandlerDelete( m_pImageHandler );
+        m_pImageHandler = NULL;
+    }
+
+    list<ArtBitmap*>::const_iterator it;
+    for( it = m_listBitmap.begin(); it != m_listBitmap.end(); ++it )
+        delete *it;
+    m_listBitmap.clear();
 }
 
 
-ArtBitmap* ArtBitmap::getArtBitmap( string uriName )
+ArtBitmap* ArtManager::getArtBitmap( string uriName )
 {
+    if( !uriName.size() )
+        return NULL;
+
     if( !m_pImageHandler )
         return NULL;
 
@@ -68,10 +90,16 @@ ArtBitmap* ArtBitmap::getArtBitmap( string uriName )
             return *it;
     }
 
-    // create and retain a new ArtBitmap since uri if not yet known
-    ArtBitmap* pArt = new ArtBitmap( uriName );
+    // create and retain a new ArtBitmap since uri is not yet known
+    ArtBitmap* pArt = new ArtBitmap( getIntf(), m_pImageHandler, uriName );
     if( pArt && pArt->getWidth() && pArt->getHeight() )
     {
+        if( m_listBitmap.size() == MAX_ART_CACHED )
+        {
+            ArtBitmap* pOldest = *(m_listBitmap.begin());
+            delete pOldest;
+            m_listBitmap.pop_front();
+        }
         m_listBitmap.push_back( pArt );
         return pArt;
     }
@@ -81,16 +109,3 @@ ArtBitmap* ArtBitmap::getArtBitmap( string uriName )
         return NULL;
     }
 }
-
-void ArtBitmap::freeArtBitmap( )
-{
-    m_pIntf = NULL;
-
-    if( m_pImageHandler )
-    {
-        image_HandlerDelete( m_pImageHandler );
-        m_pImageHandler = NULL;
-    }
-
-    m_listBitmap.clear();
-}
similarity index 61%
rename from modules/gui/skins2/src/art_bitmap.hpp
rename to modules/gui/skins2/src/art_manager.hpp
index a45f30142a005a8014aa31a765d8bdcb4c1fe0e1..6cd80eb793a332e857f50b1953186721d404aedf 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * art_bitmap.hpp
+ * art_manager.hpp
  *****************************************************************************
  * Copyright (C) 2010 the VideoLAN team
  * $Id$
@@ -21,8 +21,8 @@
  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#ifndef ART_BITMAP_HPP
-#define ART_BITMAP_HPP
+#ifndef ART_MANAGER_HPP
+#define ART_MANAGER_HPP
 
 #include "file_bitmap.hpp"
 #include <string>
@@ -34,33 +34,46 @@ class ArtBitmap: public FileBitmap
 {
 public:
 
-    static ArtBitmap* getArtBitmap( string uriName );
-    static void initArtBitmap( intf_thread_t* pIntf );
-    static void freeArtBitmap( );
-
     string getUriName() { return m_uriName; }
 
-protected:
-
     /// Constructor/destructor
-    ArtBitmap( string uriName );
+    ArtBitmap( intf_thread_t *pIntf, image_handler_t *pImageHandler,
+               string uriName ) :
+        FileBitmap( pIntf, pImageHandler, uriName, -1 ),
+        m_uriName( uriName ) {}
     virtual ~ArtBitmap() {}
 
-    /// skins2 interface
-    static intf_thread_t *m_pIntf;
+private:
+    /// uriName
+    string m_uriName;
+};
 
-    /// Image handler (used to load art files)
-    static image_handler_t *m_pImageHandler;
 
-    // keep a cache of art already open
-    static list<ArtBitmap*> m_listBitmap;
+/// Singleton object for handling art
+class ArtManager: public SkinObject
+{
+public:
+    /// Get the instance of ArtManager
+    /// Returns NULL if the initialization of the object failed
+    static ArtManager *instance( intf_thread_t *pIntf );
 
-private:
+    /// Delete the instance of ArtManager
+    static void destroy( intf_thread_t *pIntf );
 
-    // uriName
-    string m_uriName;
+    /// Retrieve for the art file from uri name
+    ArtBitmap* getArtBitmap( string uriName );
 
-};
+protected:
+    // Protected because it is a singleton
+    ArtManager( intf_thread_t *pIntf );
+    virtual ~ArtManager();
 
+private:
+    /// Image handler (used to load art files)
+    image_handler_t *m_pImageHandler;
+
+    // keep a cache of art already open
+    list<ArtBitmap*> m_listBitmap;
+};
 
 #endif
index d680319d09e607f38f0f2ed2ea98b05051087c51..64b1dfd6255c1795cb43ec688d0d5c4f6d3c9125 100644 (file)
@@ -46,6 +46,7 @@ class OSLoop;
 class VarManager;
 class VlcProc;
 class VoutManager;
+class ArtManager;
 class Theme;
 class ThemeRepository;
 
@@ -124,6 +125,8 @@ struct intf_sys_t
     VlcProc *p_vlcProc;
     /// Vout manager
     VoutManager *p_voutManager;
+    /// Art manager
+    ArtManager *p_artManager;
     /// Theme repository
     ThemeRepository *p_repository;
 
index d0e6dd06ca31b606941b1ca7e9c4cbe9fd6fa912..d4e91594df1515646941ed03952e4df3d26f014b 100644 (file)
@@ -43,6 +43,7 @@
 #include "theme_repository.hpp"
 #include "vout_window.hpp"
 #include "vout_manager.hpp"
+#include "art_manager.hpp"
 #include "../parser/interpreter.hpp"
 #include "../commands/async_queue.hpp"
 #include "../commands/cmd_quit.hpp"
@@ -212,13 +213,13 @@ static void *Run( void * p_obj )
     }
     if( Interpreter::instance( p_intf ) == NULL )
     {
-        msg_Err( p_intf, "cannot instanciate Interpreter" );
+        msg_Err( p_intf, "cannot instantiate Interpreter" );
         b_error = true;
         goto end;
     }
     if( VarManager::instance( p_intf ) == NULL )
     {
-        msg_Err( p_intf, "cannot instanciate VarManager" );
+        msg_Err( p_intf, "cannot instantiate VarManager" );
         b_error = true;
         goto end;
     }
@@ -230,19 +231,25 @@ static void *Run( void * p_obj )
     }
     if( VoutManager::instance( p_intf ) == NULL )
     {
-        msg_Err( p_intf, "cannot instanciate VoutManager" );
+        msg_Err( p_intf, "cannot instantiate VoutManager" );
+        b_error = true;
+        goto end;
+    }
+    if( ArtManager::instance( p_intf ) == NULL )
+    {
+        msg_Err( p_intf, "cannot instantiate ArtManager" );
         b_error = true;
         goto end;
     }
     if( ThemeRepository::instance( p_intf ) == NULL )
     {
-        msg_Err( p_intf, "cannot instanciate ThemeRepository" );
+        msg_Err( p_intf, "cannot instantiate ThemeRepository" );
         b_error = true;
         goto end;
     }
     if( Dialogs::instance( p_intf ) == NULL )
     {
-        msg_Err( p_intf, "cannot instanciate qt4 dialogs provider" );
+        msg_Err( p_intf, "cannot instantiate qt4 dialogs provider" );
         b_error = true;
         goto end;
     }
@@ -295,6 +302,7 @@ end:
     // Destroy "singleton" objects
     Dialogs::destroy( p_intf );
     ThemeRepository::destroy( p_intf );
+    ArtManager::destroy( p_intf );
     VoutManager::destroy( p_intf );
     VlcProc::destroy( p_intf );
     VarManager::destroy( p_intf );