move static members of ResourceManager into DependencyManager

This commit is contained in:
ZappoMan 2015-02-04 13:24:37 -08:00
parent fe3f01be7e
commit 4061e14c2d
3 changed files with 39 additions and 14 deletions

View file

@ -73,6 +73,7 @@
#include <PhysicsEngine.h> #include <PhysicsEngine.h>
#include <ProgramObject.h> #include <ProgramObject.h>
#include <ResourceCache.h> #include <ResourceCache.h>
#include <ScriptCache.h>
#include <SettingHandle.h> #include <SettingHandle.h>
#include <SoundCache.h> #include <SoundCache.h>
#include <TextRenderer.h> #include <TextRenderer.h>
@ -188,6 +189,7 @@ bool setupEssentials(int& argc, char** argv) {
auto jsConsole = DependencyManager::set<StandAloneJSConsole>(); auto jsConsole = DependencyManager::set<StandAloneJSConsole>();
auto dialogsManager = DependencyManager::set<DialogsManager>(); auto dialogsManager = DependencyManager::set<DialogsManager>();
auto bandwidthRecorder = DependencyManager::set<BandwidthRecorder>(); auto bandwidthRecorder = DependencyManager::set<BandwidthRecorder>();
auto resouceCacheSharedItems = DependencyManager::set<ResouceCacheSharedItems>();
#if defined(Q_OS_MAC) || defined(Q_OS_WIN) #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
auto speechRecognizer = DependencyManager::set<SpeechRecognizer>(); auto speechRecognizer = DependencyManager::set<SpeechRecognizer>();
#endif #endif
@ -514,6 +516,11 @@ Application::~Application() {
_myAvatar = NULL; _myAvatar = NULL;
DependencyManager::destroy<GLCanvas>(); DependencyManager::destroy<GLCanvas>();
DependencyManager::destroy<AnimationCache>();
DependencyManager::destroy<TextureCache>();
DependencyManager::destroy<GeometryCache>();
DependencyManager::destroy<ScriptCache>();
DependencyManager::destroy<SoundCache>();
} }
void Application::initializeGL() { void Application::initializeGL() {

View file

@ -112,27 +112,31 @@ void ResourceCache::reserveUnusedResource(qint64 resourceSize) {
} }
void ResourceCache::attemptRequest(Resource* resource) { void ResourceCache::attemptRequest(Resource* resource) {
auto sharedItems = DependencyManager::get<ResouceCacheSharedItems>();
if (_requestLimit <= 0) { if (_requestLimit <= 0) {
// wait until a slot becomes available // wait until a slot becomes available
_pendingRequests.append(resource); sharedItems->_pendingRequests.append(resource);
return; return;
} }
_requestLimit--; _requestLimit--;
_loadingRequests.append(resource); sharedItems->_loadingRequests.append(resource);
resource->makeRequest(); resource->makeRequest();
} }
void ResourceCache::requestCompleted(Resource* resource) { void ResourceCache::requestCompleted(Resource* resource) {
_loadingRequests.removeOne(resource);
auto sharedItems = DependencyManager::get<ResouceCacheSharedItems>();
sharedItems->_loadingRequests.removeOne(resource);
_requestLimit++; _requestLimit++;
// look for the highest priority pending request // look for the highest priority pending request
int highestIndex = -1; int highestIndex = -1;
float highestPriority = -FLT_MAX; float highestPriority = -FLT_MAX;
for (int i = 0; i < _pendingRequests.size(); ) { int pendingRequests = sharedItems->_pendingRequests.size();
Resource* resource = _pendingRequests.at(i).data(); for (int i = 0; i < pendingRequests; ) {
Resource* resource = sharedItems->_pendingRequests.at(i).data();
if (!resource) { if (!resource) {
_pendingRequests.removeAt(i); sharedItems->_pendingRequests.removeAt(i);
continue; continue;
} }
float priority = resource->getLoadPriority(); float priority = resource->getLoadPriority();
@ -143,16 +147,13 @@ void ResourceCache::requestCompleted(Resource* resource) {
i++; i++;
} }
if (highestIndex >= 0) { if (highestIndex >= 0) {
attemptRequest(_pendingRequests.takeAt(highestIndex)); attemptRequest(sharedItems->_pendingRequests.takeAt(highestIndex));
} }
} }
const int DEFAULT_REQUEST_LIMIT = 10; const int DEFAULT_REQUEST_LIMIT = 10;
int ResourceCache::_requestLimit = DEFAULT_REQUEST_LIMIT; int ResourceCache::_requestLimit = DEFAULT_REQUEST_LIMIT;
QList<QPointer<Resource> > ResourceCache::_pendingRequests;
QList<Resource*> ResourceCache::_loadingRequests;
Resource::Resource(const QUrl& url, bool delayLoad) : Resource::Resource(const QUrl& url, bool delayLoad) :
_url(url), _url(url),
_request(url) { _request(url) {

View file

@ -22,6 +22,8 @@
#include <QUrl> #include <QUrl>
#include <QWeakPointer> #include <QWeakPointer>
#include <DependencyManager.h>
class QNetworkReply; class QNetworkReply;
class QTimer; class QTimer;
@ -40,6 +42,21 @@ static const qint64 DEFAULT_UNUSED_MAX_SIZE = 1024 * BYTES_PER_MEGABYTES;
static const qint64 MIN_UNUSED_MAX_SIZE = 0; static const qint64 MIN_UNUSED_MAX_SIZE = 0;
static const qint64 MAX_UNUSED_MAX_SIZE = 10 * BYTES_PER_GIGABYTES; static const qint64 MAX_UNUSED_MAX_SIZE = 10 * BYTES_PER_GIGABYTES;
// We need to make sure that these items are available for all instances of
// ResourceCache derived classes. Since we can't count on the ordering of
// static members destruction, we need to use this Dependency manager implemented
// object instead
class ResouceCacheSharedItems : public Dependency {
SINGLETON_DEPENDENCY
public:
QList<QPointer<Resource> > _pendingRequests;
QList<Resource*> _loadingRequests;
private:
ResouceCacheSharedItems() { }
virtual ~ResouceCacheSharedItems() { }
};
/// Base class for resource caches. /// Base class for resource caches.
class ResourceCache : public QObject { class ResourceCache : public QObject {
Q_OBJECT Q_OBJECT
@ -51,9 +68,11 @@ public:
void setUnusedResourceCacheSize(qint64 unusedResourcesMaxSize); void setUnusedResourceCacheSize(qint64 unusedResourcesMaxSize);
qint64 getUnusedResourceCacheSize() const { return _unusedResourcesMaxSize; } qint64 getUnusedResourceCacheSize() const { return _unusedResourcesMaxSize; }
static const QList<Resource*>& getLoadingRequests() { return _loadingRequests; } static const QList<Resource*>& getLoadingRequests()
{ return DependencyManager::get<ResouceCacheSharedItems>()->_loadingRequests; }
static int getPendingRequestCount() { return _pendingRequests.size(); } static int getPendingRequestCount()
{ return DependencyManager::get<ResouceCacheSharedItems>()->_pendingRequests.size(); }
ResourceCache(QObject* parent = NULL); ResourceCache(QObject* parent = NULL);
virtual ~ResourceCache(); virtual ~ResourceCache();
@ -90,8 +109,6 @@ private:
int _lastLRUKey = 0; int _lastLRUKey = 0;
static int _requestLimit; static int _requestLimit;
static QList<QPointer<Resource> > _pendingRequests;
static QList<Resource*> _loadingRequests;
}; };
/// Base class for resources. /// Base class for resources.