Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 910099f

Browse files
committed
add new interfaces for TextureCache to unbind the bound image asynchronous load callbacks.
1 parent 99bfa9f commit 910099f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

cocos/renderer/CCTextureCache.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,28 @@ void TextureCache::addImageAsync(const std::string &path, const std::function<vo
141141
_sleepCondition.notify_one();
142142
}
143143

144+
void TextureCache::unbindImageAsync(const std::string& filename)
145+
{
146+
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filename);
147+
auto found = std::find_if(_imageInfoQueue->begin(), _imageInfoQueue->end(), [&](ImageInfo* ptr)->bool{ return ptr->asyncStruct->filename == fullpath; });
148+
if (found != _imageInfoQueue->end())
149+
{
150+
_imageInfoMutex.lock();
151+
(*found)->asyncStruct->callback = nullptr;
152+
_imageInfoMutex.unlock();
153+
}
154+
}
155+
156+
void TextureCache::unbindAllImageAsync()
157+
{
158+
if (_imageInfoQueue && !_imageInfoQueue->empty())
159+
{
160+
_imageInfoMutex.lock();
161+
std::for_each(_imageInfoQueue->begin(), _imageInfoQueue->end(), [](ImageInfo* ptr) { ptr->asyncStruct->callback = nullptr; });
162+
_imageInfoMutex.unlock();
163+
}
164+
}
165+
144166
void TextureCache::loadImage()
145167
{
146168
AsyncStruct *asyncStruct = nullptr;

cocos/renderer/CCTextureCache.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ class CC_DLL TextureCache : public Ref
118118
* @since v0.8
119119
*/
120120
virtual void addImageAsync(const std::string &filepath, const std::function<void(Texture2D*)>& callback);
121+
122+
/* Unbind a specified bound image asynchronous callback
123+
* In the case an object who was bound to an image asynchronous callback was destroyed before the callback is invoked,
124+
* the object always need to unbind this callback manually.
125+
* @since v3.1
126+
*/
127+
virtual void unbindImageAsync(const std::string &filename);
128+
129+
/* Unbind all bound image asynchronous load callbacks
130+
* @since v3.1
131+
*/
132+
virtual void unbindAllImageAsync();
121133

122134
/** Returns a Texture2D object given an Image.
123135
* If the image was not previously loaded, it will create a new Texture2D object and it will return it.

tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,7 @@ void TextureAsync::onEnter()
15331533

15341534
TextureAsync::~TextureAsync()
15351535
{
1536+
Director::getInstance()->getTextureCache()->unbindAllImageAsync();
15361537
Director::getInstance()->getTextureCache()->removeAllTextures();
15371538
}
15381539

0 commit comments

Comments
 (0)