diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 130d170d30..b55bec2446 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -145,6 +145,8 @@ bool Director::init(void) _scheduler->scheduleUpdateForTarget(_actionManager, Scheduler::PRIORITY_SYSTEM, false); _eventDispatcher = new EventDispatcher(); + //init TextureCache + initTextureCache(); // create autorelease pool PoolManager::sharedPoolManager()->push(); @@ -359,6 +361,29 @@ void Director::setOpenGLView(EGLView *pobOpenGLView) } } +TextureCache* Director::getTextureCache() const +{ + return _textureCache; +} + +void Director::initTextureCache() +{ +#ifdef EMSCRIPTEN + _textureCache = new TextureCacheEmscripten(); +#else + _textureCache = new TextureCache(); +#endif // EMSCRIPTEN +} + +void Director::destroyTextureCache() +{ + if (_textureCache) + { + _textureCache->waitForQuit(); + CC_SAFE_RELEASE_NULL(_textureCache); + } +} + void Director::setViewport() { if (_openGLView) @@ -693,7 +718,6 @@ void Director::purgeDirector() DrawPrimitives::free(); AnimationCache::destroyInstance(); SpriteFrameCache::destroyInstance(); - TextureCache::destroyInstance(); ShaderCache::destroyInstance(); FileUtils::destroyInstance(); Configuration::destroyInstance(); @@ -704,6 +728,8 @@ void Director::purgeDirector() GL::invalidateStateCache(); + destroyTextureCache(); + CHECK_GL_ERROR_DEBUG(); // OpenGL view diff --git a/cocos/2d/CCDirector.h b/cocos/2d/CCDirector.h index b01b0fe954..0eb4b5d390 100644 --- a/cocos/2d/CCDirector.h +++ b/cocos/2d/CCDirector.h @@ -54,6 +54,7 @@ class Node; class Scheduler; class ActionManager; class EventDispatcher; +class TextureCache; /** @brief Class that creates and handles the main Window and manages how @@ -137,6 +138,8 @@ public: inline EGLView* getOpenGLView() { return _openGLView; } void setOpenGLView(EGLView *pobOpenGLView); + TextureCache* getTextureCache() const; + inline bool isNextDeltaTimeZero() { return _nextDeltaTimeZero; } void setNextDeltaTimeZero(bool nextDeltaTimeZero); @@ -381,6 +384,10 @@ protected: /** calculates delta time since last time it was called */ void calculateDeltaTime(); + //textureCache creation or release + void initTextureCache(); + void destroyTextureCache(); + protected: /** Scheduler associated with this director @since v2.0 @@ -403,6 +410,9 @@ protected: /* The EGLView, where everything is rendered */ EGLView *_openGLView; + //texture cache belongs to this director + TextureCache *_textureCache; + double _animationInterval; double _oldAnimationInterval; diff --git a/cocos/2d/CCTextureCache.cpp b/cocos/2d/CCTextureCache.cpp index 8f3fc6fe3a..cf1e8887f9 100644 --- a/cocos/2d/CCTextureCache.cpp +++ b/cocos/2d/CCTextureCache.cpp @@ -55,15 +55,7 @@ TextureCache* TextureCache::_sharedTextureCache = nullptr; TextureCache * TextureCache::getInstance() { - if (!_sharedTextureCache) - { -#ifdef EMSCRIPTEN - _sharedTextureCache = new TextureCacheEmscripten(); -#else - _sharedTextureCache = new TextureCache(); -#endif // EMSCRIPTEN - } - return _sharedTextureCache; + return Director::getInstance()->getTextureCache(); } TextureCache::TextureCache() @@ -89,15 +81,6 @@ TextureCache::~TextureCache() void TextureCache::destroyInstance() { - if (_sharedTextureCache) - { - // notify sub thread to quick - _sharedTextureCache->_needQuit = true; - _sharedTextureCache->_sleepCondition.notify_one(); - if (_sharedTextureCache->_loadingThread) _sharedTextureCache->_loadingThread->join(); - - CC_SAFE_RELEASE_NULL(_sharedTextureCache); - } } const char* TextureCache::description() const @@ -443,6 +426,14 @@ void TextureCache::reloadAllTextures() #endif } +void TextureCache::waitForQuit() +{ + // notify sub thread to quick + _needQuit = true; + _sleepCondition.notify_one(); + if (_loadingThread) _loadingThread->join(); +} + void TextureCache::dumpCachedTextureInfo() const { unsigned int count = 0; diff --git a/cocos/2d/CCTextureCache.h b/cocos/2d/CCTextureCache.h index ec87d891d8..1dcc96eb3e 100644 --- a/cocos/2d/CCTextureCache.h +++ b/cocos/2d/CCTextureCache.h @@ -158,6 +158,10 @@ public: */ void dumpCachedTextureInfo() const; + //wait for texture cahe to quit befor destroy instance + //called by director, please do not called outside + void waitForQuit(); + private: void addImageAsyncCallBack(float dt); void loadImage();