diff --git a/src/graphic/QskGraphicProvider.cpp b/src/graphic/QskGraphicProvider.cpp index 04436d97..40afd672 100644 --- a/src/graphic/QskGraphicProvider.cpp +++ b/src/graphic/QskGraphicProvider.cpp @@ -7,6 +7,7 @@ #include "QskGraphic.h" #include "QskSetup.h" +#include #include #include #include @@ -16,6 +17,7 @@ class QskGraphicProvider::PrivateData public: // caching of graphics QCache< QString, const QskGraphic > cache; + QMutex mutex; }; QskGraphicProvider::QskGraphicProvider( QObject* parent ) @@ -33,34 +35,55 @@ void QskGraphicProvider::setCacheSize( int size ) if ( size < 0 ) size = 0; + QMutexLocker locker( &m_data->mutex ); m_data->cache.setMaxCost( size ); } int QskGraphicProvider::cacheSize() const { + QMutexLocker locker( &m_data->mutex ); return m_data->cache.maxCost(); } void QskGraphicProvider::clearCache() { + QMutexLocker locker( &m_data->mutex ); m_data->cache.clear(); } const QskGraphic* QskGraphicProvider::requestGraphic( const QString& id ) const { - const QskGraphic* graphic = m_data->cache.object( id ); + const QskGraphic* graphic = nullptr; + + { + QMutexLocker locker( &m_data->mutex ); + graphic = m_data->cache.object( id ); + } if ( graphic == nullptr ) { graphic = loadGraphic( id ); + if ( graphic == nullptr ) { qWarning() << "QskGraphicProvider: can't load" << id; return nullptr; - } + } - const int cost = 1; // TODO ... - m_data->cache.insert( id, graphic, cost ); + { + QMutexLocker locker( &m_data->mutex ); + + if( auto cached = m_data->cache.object( id ) ) + { + delete graphic; + graphic = cached; + } + else + { + const int cost = 1; // TODO ... + m_data->cache.insert( id, graphic, cost ); + } + } } return graphic;