2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stack>
|
|
|
|
#include <cctype>
|
|
|
|
#include <list>
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/ccUtils.h"
|
2015-05-21 16:04:37 +08:00
|
|
|
#include "base/CCNinePatchImageParser.h"
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
std::string TextureCache::s_etc1AlphaFileSuffix = "@alpha";
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation TextureCache
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
void TextureCache::setETC1AlphaFileSuffix(const std::string& suffix)
|
|
|
|
{
|
|
|
|
s_etc1AlphaFileSuffix = suffix;
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
TextureCache * TextureCache::getInstance()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-11-07 18:52:36 +08:00
|
|
|
return Director::getInstance()->getTextureCache();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureCache::TextureCache()
|
2013-06-26 14:48:19 +08:00
|
|
|
: _loadingThread(nullptr)
|
2013-06-21 15:29:21 +08:00
|
|
|
, _needQuit(false)
|
|
|
|
, _asyncRefCount(0)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureCache::~TextureCache()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-08-22 11:12:09 +08:00
|
|
|
CCLOGINFO("deallocing TextureCache: %p", this);
|
2013-04-27 00:55:36 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto it = _textures.begin(); it != _textures.end(); ++it)
|
2013-09-07 13:55:11 +08:00
|
|
|
(it->second)->release();
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-26 14:48:19 +08:00
|
|
|
CC_SAFE_DELETE(_loadingThread);
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
void TextureCache::destroyInstance()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
TextureCache * TextureCache::sharedTextureCache()
|
2013-11-11 15:40:12 +08:00
|
|
|
{
|
2013-11-11 16:04:34 +08:00
|
|
|
return Director::getInstance()->getTextureCache();
|
2013-11-11 15:40:12 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
void TextureCache::purgeSharedTextureCache()
|
|
|
|
{
|
2013-11-11 15:40:12 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 06:38:12 +08:00
|
|
|
std::string TextureCache::getDescription() const
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2014-01-05 11:59:32 +08:00
|
|
|
return StringUtils::format("<TextureCache | Number of textures = %d>", static_cast<int>(_textures.size()));
|
2012-04-27 18:47:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
struct TextureCache::AsyncStruct
|
|
|
|
{
|
|
|
|
public:
|
2016-06-03 09:38:01 +08:00
|
|
|
AsyncStruct(const std::string& fn, std::function<void(Texture2D*)> f) : filename(fn), callback(f), pixelFormat(Texture2D::getDefaultAlphaPixelFormat()), loadSuccess(false) {}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
std::string filename;
|
|
|
|
std::function<void(Texture2D*)> callback;
|
|
|
|
Image image;
|
2016-07-25 17:31:54 +08:00
|
|
|
Image imageAlpha;
|
2016-06-03 09:38:01 +08:00
|
|
|
Texture2D::PixelFormat pixelFormat;
|
2015-07-08 15:13:48 +08:00
|
|
|
bool loadSuccess;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
The addImageAsync logic follow the steps:
|
|
|
|
- find the image has been add or not, if not add an AsyncStruct to _requestQueue (GL thread)
|
|
|
|
- get AsyncStruct from _requestQueue, load res and fill image data to AsyncStruct.image, then add AsyncStruct to _responseQueue (Load thread)
|
|
|
|
- on schedule callback, get AsyncStruct from _responseQueue, convert image to texture, then delete AsyncStruct (GL thread)
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
the Critical Area include these members:
|
|
|
|
- _requestQueue: locked by _requestMutex
|
|
|
|
- _responseQueue: locked by _responseMutex
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
the object's life time:
|
|
|
|
- AsyncStruct: construct and destruct in GL thread
|
|
|
|
- image data: new in Load thread, delete in GL thread(by Image instance)
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
Note:
|
|
|
|
- all AsyncStruct referenced in _asyncStructQueue, for unbind function use.
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
How to deal add image many times?
|
|
|
|
- At first, this situation is abnormal, we only ensure the logic is correct.
|
|
|
|
- If the image has been loaded, the after load image call will return immediately.
|
2015-09-22 16:08:23 +08:00
|
|
|
- If the image request is in queue already, there will be more than one request in queue,
|
|
|
|
- In addImageAsyncCallback, will deduplicate the request to ensure only create one texture.
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
Does process all response in addImageAsyncCallback consume more time?
|
|
|
|
- Convert image to texture faster than load image from disk, so this isn't a problem.
|
|
|
|
*/
|
2014-04-14 10:01:17 +08:00
|
|
|
void TextureCache::addImageAsync(const std::string &path, const std::function<void(Texture2D*)>& callback)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D *texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(fullpath);
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end())
|
2013-09-07 13:55:11 +08:00
|
|
|
texture = it->second;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-01-02 17:23:00 +08:00
|
|
|
if (texture != nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-06-16 14:36:04 +08:00
|
|
|
if (callback) callback(texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:31:52 +08:00
|
|
|
// check if file exists
|
2016-07-25 17:31:54 +08:00
|
|
|
if (fullpath.empty() || !FileUtils::getInstance()->isFileExist(fullpath)) {
|
2015-06-16 14:36:04 +08:00
|
|
|
if (callback) callback(nullptr);
|
2015-06-16 14:31:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-06-09 08:14:15 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// lazy init
|
2015-07-08 15:13:48 +08:00
|
|
|
if (_loadingThread == nullptr)
|
|
|
|
{
|
2013-06-21 15:49:45 +08:00
|
|
|
// create a new thread to load images
|
2015-12-16 14:02:55 +08:00
|
|
|
_loadingThread = new (std::nothrow) std::thread(&TextureCache::loadImage, this);
|
2013-06-21 15:29:21 +08:00
|
|
|
_needQuit = false;
|
2012-05-28 02:41:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
if (0 == _asyncRefCount)
|
2012-05-30 14:24:59 +08:00
|
|
|
{
|
2014-10-04 00:38:36 +08:00
|
|
|
Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack), this, 0, false);
|
2012-05-30 14:24:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
++_asyncRefCount;
|
2012-05-30 14:24:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// generate async struct
|
2014-08-28 07:31:57 +08:00
|
|
|
AsyncStruct *data = new (std::nothrow) AsyncStruct(fullpath, callback);
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// add async struct into queue
|
2015-07-08 15:13:48 +08:00
|
|
|
_asyncStructQueue.push_back(data);
|
|
|
|
_requestMutex.lock();
|
|
|
|
_requestQueue.push_back(data);
|
|
|
|
_requestMutex.unlock();
|
2011-11-25 16:14:06 +08:00
|
|
|
|
2013-06-21 15:43:17 +08:00
|
|
|
_sleepCondition.notify_one();
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 17:08:22 +08:00
|
|
|
void TextureCache::unbindImageAsync(const std::string& filename)
|
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
if (_asyncStructQueue.empty())
|
2015-05-25 11:47:37 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
return;
|
2015-05-25 11:47:37 +08:00
|
|
|
}
|
2015-07-08 15:13:48 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filename);
|
|
|
|
for (auto it = _asyncStructQueue.begin(); it != _asyncStructQueue.end(); ++it)
|
2014-05-23 17:08:22 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
if ((*it)->filename == fullpath)
|
2014-05-23 17:27:59 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
(*it)->callback = nullptr;
|
2014-05-23 17:27:59 +08:00
|
|
|
}
|
2014-05-23 17:08:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::unbindAllImageAsync()
|
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
if (_asyncStructQueue.empty())
|
2015-05-25 11:47:37 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
return;
|
|
|
|
|
2015-05-25 11:47:37 +08:00
|
|
|
}
|
2015-07-08 15:13:48 +08:00
|
|
|
for (auto it = _asyncStructQueue.begin(); it != _asyncStructQueue.end(); ++it)
|
2014-05-23 17:08:22 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
(*it)->callback = nullptr;
|
2014-05-23 17:08:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
void TextureCache::loadImage()
|
|
|
|
{
|
2013-09-07 06:33:28 +08:00
|
|
|
AsyncStruct *asyncStruct = nullptr;
|
2015-07-09 17:06:55 +08:00
|
|
|
std::mutex signalMutex;
|
|
|
|
std::unique_lock<std::mutex> signal(signalMutex);
|
2015-07-08 15:13:48 +08:00
|
|
|
while (!_needQuit)
|
2013-06-21 15:29:21 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
// pop an AsyncStruct from request queue
|
|
|
|
_requestMutex.lock();
|
2016-07-25 17:31:54 +08:00
|
|
|
if (_requestQueue.empty())
|
2013-06-21 15:29:21 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
asyncStruct = nullptr;
|
2016-07-25 17:31:54 +08:00
|
|
|
}
|
|
|
|
else
|
2013-06-21 15:29:21 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
asyncStruct = _requestQueue.front();
|
|
|
|
_requestQueue.pop_front();
|
|
|
|
}
|
|
|
|
_requestMutex.unlock();
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
if (nullptr == asyncStruct) {
|
|
|
|
_sleepCondition.wait(signal);
|
|
|
|
continue;
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
// load image
|
|
|
|
asyncStruct->loadSuccess = asyncStruct->image.initWithImageFileThreadSafe(asyncStruct->filename);
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
// ETC1 ALPHA supports.
|
|
|
|
if (asyncStruct->loadSuccess && asyncStruct->image.getFileType() == Image::Format::ETC && !s_etc1AlphaFileSuffix.empty())
|
|
|
|
{ // check whether alpha texture exists & load it
|
|
|
|
auto alphaFile = asyncStruct->filename + s_etc1AlphaFileSuffix;
|
|
|
|
if (FileUtils::getInstance()->isFileExist(alphaFile))
|
|
|
|
asyncStruct->imageAlpha.initWithImageFileThreadSafe(alphaFile);
|
|
|
|
}
|
2015-07-08 15:13:48 +08:00
|
|
|
// push the asyncStruct to response queue
|
|
|
|
_responseMutex.lock();
|
|
|
|
_responseQueue.push_back(asyncStruct);
|
|
|
|
_responseMutex.unlock();
|
2013-06-21 15:29:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::addImageAsyncCallBack(float dt)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
Texture2D *texture = nullptr;
|
|
|
|
AsyncStruct *asyncStruct = nullptr;
|
|
|
|
while (true)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
// pop an AsyncStruct from response queue
|
|
|
|
_responseMutex.lock();
|
2016-07-25 17:31:54 +08:00
|
|
|
if (_responseQueue.empty())
|
2013-12-16 17:12:53 +08:00
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
asyncStruct = nullptr;
|
2016-07-25 17:31:54 +08:00
|
|
|
}
|
|
|
|
else
|
2015-07-08 15:13:48 +08:00
|
|
|
{
|
|
|
|
asyncStruct = _responseQueue.front();
|
|
|
|
_responseQueue.pop_front();
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
// the asyncStruct's sequence order in _asyncStructQueue must equal to the order in _responseQueue
|
|
|
|
CC_ASSERT(asyncStruct == _asyncStructQueue.front());
|
|
|
|
_asyncStructQueue.pop_front();
|
|
|
|
}
|
|
|
|
_responseMutex.unlock();
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
if (nullptr == asyncStruct) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
// check the image has been convert to texture or not
|
|
|
|
auto it = _textures.find(asyncStruct->filename);
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end())
|
2015-07-08 15:13:48 +08:00
|
|
|
{
|
|
|
|
texture = it->second;
|
2013-12-16 17:12:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
// convert image to texture
|
|
|
|
if (asyncStruct->loadSuccess)
|
|
|
|
{
|
|
|
|
Image* image = &(asyncStruct->image);
|
|
|
|
// generate texture in render thread
|
|
|
|
texture = new (std::nothrow) Texture2D();
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2016-06-03 09:38:01 +08:00
|
|
|
texture->initWithImage(image, asyncStruct->pixelFormat);
|
2015-07-08 15:13:48 +08:00
|
|
|
//parse 9-patch info
|
|
|
|
this->parseNinePatchImage(image, texture, asyncStruct->filename);
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// cache the texture file name
|
|
|
|
VolatileTextureMgr::addImageTexture(texture, asyncStruct->filename);
|
|
|
|
#endif
|
|
|
|
// cache the texture. retain it, since it is added in the map
|
2016-07-25 17:31:54 +08:00
|
|
|
_textures.insert(std::make_pair(asyncStruct->filename, texture));
|
2015-07-08 15:13:48 +08:00
|
|
|
texture->retain();
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
texture->autorelease();
|
2016-07-25 17:31:54 +08:00
|
|
|
// ETC1 ALPHA supports.
|
|
|
|
if (asyncStruct->imageAlpha.getFileType() == Image::Format::ETC) {
|
|
|
|
auto alphaTexture = new(std::nothrow) Texture2D();
|
|
|
|
if(alphaTexture != nullptr && alphaTexture->initWithImage(&asyncStruct->imageAlpha, asyncStruct->pixelFormat)) {
|
|
|
|
texture->setAlphaTexture(alphaTexture);
|
|
|
|
}
|
|
|
|
CC_SAFE_RELEASE(alphaTexture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-07-08 15:13:48 +08:00
|
|
|
texture = nullptr;
|
|
|
|
CCLOG("cocos2d: failed to call TextureCache::addImageAsync(%s)", asyncStruct->filename.c_str());
|
|
|
|
}
|
2013-12-16 17:12:53 +08:00
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
// call callback function
|
2014-05-22 12:42:52 +08:00
|
|
|
if (asyncStruct->callback)
|
|
|
|
{
|
2015-07-08 15:13:48 +08:00
|
|
|
(asyncStruct->callback)(texture);
|
2014-05-22 12:42:52 +08:00
|
|
|
}
|
2015-07-08 15:13:48 +08:00
|
|
|
|
|
|
|
// release the asyncStruct
|
2013-09-07 06:33:28 +08:00
|
|
|
delete asyncStruct;
|
2015-07-09 17:06:55 +08:00
|
|
|
--_asyncRefCount;
|
2015-07-08 15:13:48 +08:00
|
|
|
}
|
2012-05-30 14:24:59 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
if (0 == _asyncRefCount)
|
|
|
|
{
|
|
|
|
Director::getInstance()->getScheduler()->unschedule(CC_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack), this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-11-16 11:04:29 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D * TextureCache::addImage(const std::string &path)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D * texture = nullptr;
|
|
|
|
Image* image = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
// Split up directory and filename
|
|
|
|
// MUTEX:
|
|
|
|
// Needed since addImageAsync calls this method from a different thread
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
2013-09-07 13:55:11 +08:00
|
|
|
if (fullpath.size() == 0)
|
2013-02-27 11:35:38 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2013-02-27 11:35:38 +08:00
|
|
|
}
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(fullpath);
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end())
|
2013-09-07 13:55:11 +08:00
|
|
|
texture = it->second;
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (!texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// all images are handled by UIImage except PVR extension that is handled by our own handler
|
2016-07-25 17:31:54 +08:00
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
image = new (std::nothrow) Image();
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(nullptr == image);
|
2012-12-27 16:07:48 +08:00
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
bool bRet = image->initWithImageFile(fullpath);
|
2013-07-26 17:34:44 +08:00
|
|
|
CC_BREAK_IF(!bRet);
|
2013-05-18 08:09:28 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
texture = new (std::nothrow) Texture2D();
|
2013-07-26 17:34:44 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (texture && texture->initWithImage(image))
|
2013-07-26 17:34:44 +08:00
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-07-26 17:34:44 +08:00
|
|
|
// cache the texture file name
|
2013-12-25 13:57:17 +08:00
|
|
|
VolatileTextureMgr::addImageTexture(texture, fullpath);
|
2011-04-06 16:29:58 +08:00
|
|
|
#endif
|
2013-09-07 13:55:11 +08:00
|
|
|
// texture already retained, no need to re-retain it
|
2016-07-25 17:31:54 +08:00
|
|
|
_textures.insert(std::make_pair(fullpath, texture));
|
|
|
|
|
|
|
|
//-- ANDROID ETC1 ALPHA SUPPORTS.
|
|
|
|
std::string alphaFullPath = path + s_etc1AlphaFileSuffix;
|
|
|
|
if (image->getFileType() == Image::Format::ETC && !s_etc1AlphaFileSuffix.empty() && FileUtils::getInstance()->isFileExist(alphaFullPath))
|
|
|
|
{
|
|
|
|
Image alphaImage;
|
|
|
|
if (alphaImage.initWithImageFile(alphaFullPath))
|
|
|
|
{
|
|
|
|
Texture2D *pAlphaTexture = new(std::nothrow) Texture2D;
|
|
|
|
if(pAlphaTexture != nullptr && pAlphaTexture->initWithImage(&alphaImage)) {
|
|
|
|
texture->setAlphaTexture(pAlphaTexture);
|
|
|
|
}
|
|
|
|
CC_SAFE_RELEASE(pAlphaTexture);
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
|
|
|
|
//parse 9-patch info
|
|
|
|
this->parseNinePatchImage(image, texture, path);
|
2013-07-26 17:34:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
CCLOG("cocos2d: Couldn't create texture for file:%s in TextureCache", path.c_str());
|
2015-11-30 01:09:34 +08:00
|
|
|
CC_SAFE_RELEASE(texture);
|
|
|
|
texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
CC_SAFE_RELEASE(image);
|
2012-12-27 15:42:55 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return texture;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
void TextureCache::parseNinePatchImage(cocos2d::Image *image, cocos2d::Texture2D *texture, const std::string& path)
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
if (NinePatchImageParser::isNinePatchImage(path))
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
Rect frameRect = Rect(0, 0, image->getWidth(), image->getHeight());
|
2015-05-21 16:04:37 +08:00
|
|
|
NinePatchImageParser parser(image, frameRect, false);
|
|
|
|
texture->addSpriteFrameCapInset(nullptr, parser.parseCapInset());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* TextureCache::addImage(Image *image, const std::string &key)
|
2010-08-26 14:06:41 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT(image != nullptr, "TextureCache: image MUST not be nil");
|
2016-06-18 07:07:33 +08:00
|
|
|
CCASSERT(image->getData() != nullptr, "TextureCache: image MUST not be nil");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D * texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(key);
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end()) {
|
2013-09-07 13:55:11 +08:00
|
|
|
texture = it->second;
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevents overloading the autorelease pool
|
2014-08-28 07:31:57 +08:00
|
|
|
texture = new (std::nothrow) Texture2D();
|
2012-08-08 18:39:33 +08:00
|
|
|
texture->initWithImage(image);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
_textures.insert(std::make_pair(key, texture));
|
2013-09-07 13:55:11 +08:00
|
|
|
texture->retain();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: Couldn't add UIImage in TextureCache");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (0);
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-11-08 16:47:33 +08:00
|
|
|
VolatileTextureMgr::addImage(texture, image);
|
2012-04-24 15:02:18 +08:00
|
|
|
#endif
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return texture;
|
2010-08-26 14:06:41 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2014-02-26 11:44:54 +08:00
|
|
|
bool TextureCache::reloadTexture(const std::string& fileName)
|
|
|
|
{
|
|
|
|
Texture2D * texture = nullptr;
|
2014-10-20 10:43:18 +08:00
|
|
|
Image * image = nullptr;
|
2014-02-26 11:44:54 +08:00
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
|
|
|
if (fullpath.size() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = _textures.find(fullpath);
|
|
|
|
if (it != _textures.end()) {
|
|
|
|
texture = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
2016-07-25 17:31:54 +08:00
|
|
|
if (!texture) {
|
2014-02-26 11:44:54 +08:00
|
|
|
texture = this->addImage(fullpath);
|
|
|
|
ret = (texture != nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do {
|
2014-10-20 10:43:18 +08:00
|
|
|
image = new (std::nothrow) Image();
|
2014-02-26 11:44:54 +08:00
|
|
|
CC_BREAK_IF(nullptr == image);
|
|
|
|
|
|
|
|
bool bRet = image->initWithImageFile(fullpath);
|
|
|
|
CC_BREAK_IF(!bRet);
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2014-02-26 11:44:54 +08:00
|
|
|
ret = texture->initWithImage(image);
|
|
|
|
} while (0);
|
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2014-10-20 10:43:18 +08:00
|
|
|
CC_SAFE_RELEASE(image);
|
2014-02-26 11:44:54 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// TextureCache - Remove
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::removeAllTextures()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto it = _textures.begin(); it != _textures.end(); ++it) {
|
2013-09-07 13:55:11 +08:00
|
|
|
(it->second)->release();
|
|
|
|
}
|
|
|
|
_textures.clear();
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::removeUnusedTextures()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto it = _textures.cbegin(); it != _textures.cend(); /* nothing */) {
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D *tex = it->second;
|
2016-07-25 17:31:54 +08:00
|
|
|
if (tex->getReferenceCount() == 1) {
|
2013-09-07 13:55:11 +08:00
|
|
|
CCLOG("cocos2d: TextureCache: removing unused texture: %s", it->first.c_str());
|
|
|
|
|
|
|
|
tex->release();
|
2016-04-02 08:19:11 +08:00
|
|
|
it = _textures.erase(it);
|
2016-07-25 17:31:54 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-09-07 13:55:11 +08:00
|
|
|
++it;
|
2012-04-28 11:34:13 +08:00
|
|
|
}
|
2013-09-07 13:55:11 +08:00
|
|
|
|
2012-04-28 11:34:13 +08:00
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::removeTexture(Texture2D* texture)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
if (!texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto it = _textures.cbegin(); it != _textures.cend(); /* nothing */) {
|
|
|
|
if (it->second == texture) {
|
2015-12-17 16:53:38 +08:00
|
|
|
it->second->release();
|
2016-04-02 08:19:11 +08:00
|
|
|
it = _textures.erase(it);
|
2013-09-07 13:55:11 +08:00
|
|
|
break;
|
2016-07-25 17:31:54 +08:00
|
|
|
}
|
|
|
|
else
|
2013-09-07 13:55:11 +08:00
|
|
|
++it;
|
|
|
|
}
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
void TextureCache::removeTextureForKey(const std::string &textureKeyName)
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string key = textureKeyName;
|
|
|
|
auto it = _textures.find(key);
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it == _textures.end()) {
|
2014-01-15 09:22:45 +08:00
|
|
|
key = FileUtils::getInstance()->fullPathForFilename(textureKeyName);
|
|
|
|
it = _textures.find(key);
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end()) {
|
2015-12-17 16:53:38 +08:00
|
|
|
it->second->release();
|
2013-09-07 13:55:11 +08:00
|
|
|
_textures.erase(it);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
Texture2D* TextureCache::getTextureForKey(const std::string &textureKeyName) const
|
2010-12-30 17:30:11 +08:00
|
|
|
{
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string key = textureKeyName;
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _textures.find(key);
|
2014-01-15 09:22:45 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it == _textures.end()) {
|
2014-01-15 09:22:45 +08:00
|
|
|
key = FileUtils::getInstance()->fullPathForFilename(textureKeyName);
|
|
|
|
it = _textures.find(key);
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end())
|
2013-09-07 13:55:11 +08:00
|
|
|
return it->second;
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2010-07-16 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCache::reloadAllTextures()
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
//will do nothing
|
|
|
|
// #if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// VolatileTextureMgr::reloadAllTextures();
|
|
|
|
// #endif
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2016-02-29 15:48:07 +08:00
|
|
|
std::string TextureCache::getTextureFilePath(cocos2d::Texture2D* texture) const
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto& item : _textures)
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
if (item.second == texture)
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
|
|
|
return item.first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:52:36 +08:00
|
|
|
void TextureCache::waitForQuit()
|
|
|
|
{
|
|
|
|
// notify sub thread to quick
|
|
|
|
_needQuit = true;
|
|
|
|
_sleepCondition.notify_one();
|
|
|
|
if (_loadingThread) _loadingThread->join();
|
|
|
|
}
|
|
|
|
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string TextureCache::getCachedTextureInfo() const
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2014-03-15 01:09:22 +08:00
|
|
|
std::string buffer;
|
2014-01-15 09:22:45 +08:00
|
|
|
char buftmp[4096];
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int count = 0;
|
|
|
|
unsigned int totalBytes = 0;
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto it = _textures.begin(); it != _textures.end(); ++it) {
|
2013-09-07 13:55:11 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
memset(buftmp, 0, sizeof(buftmp));
|
2014-01-15 09:22:45 +08:00
|
|
|
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* tex = it->second;
|
2013-07-20 04:16:38 +08:00
|
|
|
unsigned int bpp = tex->getBitsPerPixelForFormat();
|
2011-11-25 10:48:05 +08:00
|
|
|
// Each texture takes up width * height * bytesPerPixel bytes.
|
2013-12-05 17:19:01 +08:00
|
|
|
auto bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8;
|
2012-04-19 14:35:52 +08:00
|
|
|
totalBytes += bytes;
|
|
|
|
count++;
|
2016-07-25 17:31:54 +08:00
|
|
|
snprintf(buftmp, sizeof(buftmp) - 1, "\"%s\" rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB\n",
|
|
|
|
it->first.c_str(),
|
|
|
|
(long)tex->getReferenceCount(),
|
|
|
|
(long)tex->getName(),
|
|
|
|
(long)tex->getPixelsWide(),
|
|
|
|
(long)tex->getPixelsHigh(),
|
|
|
|
(long)bpp,
|
|
|
|
(long)bytes / 1024);
|
|
|
|
|
2014-03-15 01:09:22 +08:00
|
|
|
buffer += buftmp;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
snprintf(buftmp, sizeof(buftmp) - 1, "TextureCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)\n", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f));
|
2014-03-15 01:09:22 +08:00
|
|
|
buffer += buftmp;
|
2014-01-15 09:22:45 +08:00
|
|
|
|
2014-03-15 01:09:22 +08:00
|
|
|
return buffer;
|
2011-07-05 10:47:25 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 14:53:26 +08:00
|
|
|
void TextureCache::renameTextureWithKey(const std::string& srcName, const std::string& dstName)
|
2015-11-13 17:44:23 +08:00
|
|
|
{
|
|
|
|
std::string key = srcName;
|
|
|
|
auto it = _textures.find(key);
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it == _textures.end()) {
|
2015-11-13 17:44:23 +08:00
|
|
|
key = FileUtils::getInstance()->fullPathForFilename(srcName);
|
|
|
|
it = _textures.find(key);
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
if (it != _textures.end()) {
|
2015-11-13 17:44:23 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(dstName);
|
|
|
|
Texture2D* tex = it->second;
|
2015-11-24 18:29:58 +08:00
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
Image* image = new (std::nothrow) Image();
|
2015-11-24 18:29:58 +08:00
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
bool ret = image->initWithImageFile(dstName);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
tex->initWithImage(image);
|
|
|
|
_textures.insert(std::make_pair(fullpath, tex));
|
|
|
|
_textures.erase(it);
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(image);
|
|
|
|
}
|
2015-11-13 17:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
std::list<VolatileTexture*> VolatileTextureMgr::_textures;
|
|
|
|
bool VolatileTextureMgr::_isReloading = false;
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
VolatileTexture::VolatileTexture(Texture2D *t)
|
2013-07-16 16:33:02 +08:00
|
|
|
: _texture(t)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _cashedImageType(kInvalid)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _textureData(nullptr)
|
2013-07-26 04:36:19 +08:00
|
|
|
, _pixelFormat(Texture2D::PixelFormat::RGBA8888)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _fileName("")
|
|
|
|
, _text("")
|
2013-12-18 17:47:20 +08:00
|
|
|
, _uiImage(nullptr)
|
2014-03-29 02:25:57 +08:00
|
|
|
, _hasMipmaps(false)
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_texParams.minFilter = GL_LINEAR;
|
|
|
|
_texParams.magFilter = GL_LINEAR;
|
|
|
|
_texParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
_texParams.wrapT = GL_CLAMP_TO_EDGE;
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VolatileTexture::~VolatileTexture()
|
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
CC_SAFE_RELEASE(_uiImage);
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 13:57:17 +08:00
|
|
|
void VolatileTextureMgr::addImageTexture(Texture2D *tt, const std::string& imageFileName)
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
if (_isReloading)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
return;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImageFile;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_fileName = imageFileName;
|
|
|
|
vt->_pixelFormat = tt->getPixelFormat();
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::addImage(Texture2D *tt, Image *image)
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2012-05-23 11:18:04 +08:00
|
|
|
image->retain();
|
2013-07-16 16:33:02 +08:00
|
|
|
vt->_uiImage = image;
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImage;
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
VolatileTexture* VolatileTextureMgr::findVolotileTexture(Texture2D *tt)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2016-05-25 02:09:11 +08:00
|
|
|
VolatileTexture *vt = nullptr;
|
2013-07-16 16:33:02 +08:00
|
|
|
auto i = _textures.begin();
|
|
|
|
while (i != _textures.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *v = *i++;
|
2013-07-16 16:33:02 +08:00
|
|
|
if (v->_texture == tt)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
vt = v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
|
|
|
if (!vt)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
vt = new (std::nothrow) VolatileTexture(tt);
|
2013-11-08 16:47:33 +08:00
|
|
|
_textures.push_back(vt);
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
return vt;
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::addDataTexture(Texture2D *tt, void* data, int dataLen, Texture2D::PixelFormat pixelFormat, const Size& contentSize)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
if (_isReloading)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImageData;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_textureData = data;
|
2013-07-26 17:34:44 +08:00
|
|
|
vt->_dataLen = dataLen;
|
2013-06-15 14:03:30 +08:00
|
|
|
vt->_pixelFormat = pixelFormat;
|
|
|
|
vt->_textureSize = contentSize;
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::addStringTexture(Texture2D *tt, const char* text, const FontDefinition& fontDefinition)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
if (_isReloading)
|
2011-04-06 16:29:58 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
return;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = findVolotileTexture(tt);
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kString;
|
2016-07-25 17:31:54 +08:00
|
|
|
vt->_text = text;
|
2013-07-10 04:21:43 +08:00
|
|
|
vt->_fontDefinition = fontDefinition;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
2011-11-25 10:48:05 +08:00
|
|
|
|
2014-03-25 16:19:34 +08:00
|
|
|
void VolatileTextureMgr::setHasMipmaps(Texture2D *t, bool hasMipmaps)
|
|
|
|
{
|
|
|
|
VolatileTexture *vt = findVolotileTexture(t);
|
|
|
|
vt->_hasMipmaps = hasMipmaps;
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::setTexParameters(Texture2D *t, const Texture2D::TexParams &texParams)
|
2012-08-31 03:02:05 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *vt = findVolotileTexture(t);
|
|
|
|
|
2013-07-04 08:59:22 +08:00
|
|
|
if (texParams.minFilter != GL_NONE)
|
|
|
|
vt->_texParams.minFilter = texParams.minFilter;
|
|
|
|
if (texParams.magFilter != GL_NONE)
|
|
|
|
vt->_texParams.magFilter = texParams.magFilter;
|
|
|
|
if (texParams.wrapS != GL_NONE)
|
|
|
|
vt->_texParams.wrapS = texParams.wrapS;
|
|
|
|
if (texParams.wrapT != GL_NONE)
|
|
|
|
vt->_texParams.wrapT = texParams.wrapT;
|
2012-08-31 03:02:05 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
void VolatileTextureMgr::removeTexture(Texture2D *t)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
auto i = _textures.begin();
|
|
|
|
while (i != _textures.end())
|
2011-11-25 10:48:05 +08:00
|
|
|
{
|
|
|
|
VolatileTexture *vt = *i++;
|
2016-07-25 17:31:54 +08:00
|
|
|
if (vt->_texture == t)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-11-08 16:47:33 +08:00
|
|
|
_textures.remove(vt);
|
2011-11-25 10:48:05 +08:00
|
|
|
delete vt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
void VolatileTextureMgr::reloadAllTextures()
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2013-07-16 16:33:02 +08:00
|
|
|
_isReloading = true;
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2014-06-13 07:31:46 +08:00
|
|
|
// we need to release all of the glTextures to avoid collisions of texture id's when reloading the textures onto the GPU
|
2016-07-25 17:31:54 +08:00
|
|
|
for (auto iter = _textures.begin(); iter != _textures.end(); ++iter)
|
2014-06-13 07:31:46 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
(*iter)->_texture->releaseGLTexture();
|
2014-06-13 07:31:46 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
CCLOG("reload all texture");
|
2013-07-16 16:33:02 +08:00
|
|
|
auto iter = _textures.begin();
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2013-07-16 16:33:02 +08:00
|
|
|
while (iter != _textures.end())
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
VolatileTexture *vt = *iter++;
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (vt->_cashedImageType)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kImageFile:
|
2016-07-25 17:31:54 +08:00
|
|
|
{
|
|
|
|
Image* image = new (std::nothrow) Image();
|
|
|
|
|
|
|
|
Data data = FileUtils::getInstance()->getDataFromFile(vt->_fileName);
|
|
|
|
|
|
|
|
if (image && image->initWithImageData(data.getBytes(), data.getSize()))
|
2012-04-08 14:16:29 +08:00
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
Texture2D::PixelFormat oldPixelFormat = Texture2D::getDefaultAlphaPixelFormat();
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(vt->_pixelFormat);
|
|
|
|
vt->_texture->initWithImage(image);
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(oldPixelFormat);
|
2012-04-08 14:16:29 +08:00
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
|
|
|
|
CC_SAFE_RELEASE(image);
|
|
|
|
}
|
|
|
|
break;
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kImageData:
|
2016-07-25 17:31:54 +08:00
|
|
|
{
|
|
|
|
vt->_texture->initWithData(vt->_textureData,
|
|
|
|
vt->_dataLen,
|
|
|
|
vt->_pixelFormat,
|
|
|
|
vt->_textureSize.width,
|
|
|
|
vt->_textureSize.height,
|
|
|
|
vt->_textureSize);
|
|
|
|
}
|
|
|
|
break;
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kString:
|
2016-07-25 17:31:54 +08:00
|
|
|
{
|
|
|
|
vt->_texture->initWithString(vt->_text.c_str(), vt->_fontDefinition);
|
|
|
|
}
|
|
|
|
break;
|
2013-11-08 16:47:33 +08:00
|
|
|
case VolatileTexture::kImage:
|
2016-07-25 17:31:54 +08:00
|
|
|
{
|
|
|
|
vt->_texture->initWithImage(vt->_uiImage);
|
|
|
|
}
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-25 16:19:34 +08:00
|
|
|
if (vt->_hasMipmaps) {
|
|
|
|
vt->_texture->generateMipmap();
|
|
|
|
}
|
2013-07-16 16:33:02 +08:00
|
|
|
vt->_texture->setTexParameters(vt->_texParams);
|
2011-11-25 10:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-07-16 16:33:02 +08:00
|
|
|
_isReloading = false;
|
2011-04-06 16:29:58 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#endif // CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_END
|
2010-07-16 16:28:11 +08:00
|
|
|
|