2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-05-13 21:06:17 +08:00
|
|
|
Copyright (c) 2020 C4games Ltd.
|
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stack>
|
|
|
|
#include <cctype>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
#include "base/CCNinePatchImageParser.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string TextureCache::s_etc1AlphaFileSuffix = "@alpha";
|
|
|
|
|
|
|
|
// implementation TextureCache
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCache::setETC1AlphaFileSuffix(std::string_view suffix)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
s_etc1AlphaFileSuffix = suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextureCache::getETC1AlphaFileSuffix()
|
|
|
|
{
|
|
|
|
return s_etc1AlphaFileSuffix;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureCache::TextureCache() : _loadingThread(nullptr), _needQuit(false), _asyncRefCount(0) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TextureCache::~TextureCache()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("deallocing TextureCache: %p", this);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& texture : _textures)
|
2019-11-23 20:27:39 +08:00
|
|
|
texture.second->release();
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_loadingThread);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextureCache::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<TextureCache | Number of textures = %d>", static_cast<int>(_textures.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TextureCache::AsyncStruct
|
|
|
|
{
|
|
|
|
public:
|
2021-12-31 12:12:40 +08:00
|
|
|
AsyncStruct(std::string_view fn, const std::function<void(Texture2D*)>& f, std::string_view key)
|
2021-12-25 10:04:45 +08:00
|
|
|
: filename(fn)
|
|
|
|
, callback(f)
|
|
|
|
, callbackKey(key)
|
|
|
|
, pixelFormat(Texture2D::getDefaultAlphaPixelFormat())
|
|
|
|
, loadSuccess(false)
|
2019-11-23 20:27:39 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
std::function<void(Texture2D*)> callback;
|
|
|
|
std::string callbackKey;
|
|
|
|
Image image;
|
|
|
|
Image imageAlpha;
|
|
|
|
backend::PixelFormat pixelFormat;
|
|
|
|
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)
|
2021-12-25 10:04:45 +08:00
|
|
|
- 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)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
the Critical Area include these members:
|
|
|
|
- _requestQueue: locked by _requestMutex
|
|
|
|
- _responseQueue: locked by _responseMutex
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
Note:
|
|
|
|
- all AsyncStruct referenced in _asyncStructQueue, for unbind function use.
|
|
|
|
|
|
|
|
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.
|
|
|
|
- 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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Call unbindImageAsync(path) to prevent the call to the callback when the
|
|
|
|
texture is loaded.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCache::addImageAsync(std::string_view path, const std::function<void(Texture2D*)>& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
addImageAsync(path, callback, path);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The addImageAsync logic follow the steps:
|
|
|
|
- find the image has been add or not, if not add an AsyncStruct to _requestQueue (GL thread)
|
2021-12-25 10:04:45 +08:00
|
|
|
- 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)
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
the Critical Area include these members:
|
|
|
|
- _requestQueue: locked by _requestMutex
|
|
|
|
- _responseQueue: locked by _responseMutex
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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)
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Note:
|
|
|
|
- all AsyncStruct referenced in _asyncStructQueue, for unbind function use.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
- 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.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
|
|
|
|
The callbackKey allows to unbind the callback in cases where the loading of
|
|
|
|
path is requested by several sources simultaneously. Each source can then
|
|
|
|
unbind the callback independently as needed whilst a call to
|
|
|
|
unbindImageAsync(path) would be ambiguous.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCache::addImageAsync(std::string_view path,
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::function<void(Texture2D*)>& callback,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view callbackKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
|
|
|
|
|
|
|
auto it = _textures.find(fullpath);
|
|
|
|
if (it != _textures.end())
|
|
|
|
texture = it->second;
|
|
|
|
|
|
|
|
if (texture != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (callback)
|
|
|
|
callback(texture);
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if file exists
|
2021-12-25 10:04:45 +08:00
|
|
|
if (fullpath.empty() || !FileUtils::getInstance()->isFileExist(fullpath))
|
|
|
|
{
|
|
|
|
if (callback)
|
|
|
|
callback(nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazy init
|
|
|
|
if (_loadingThread == nullptr)
|
|
|
|
{
|
|
|
|
// create a new thread to load images
|
2021-12-25 10:04:45 +08:00
|
|
|
_needQuit = false;
|
2021-12-08 00:11:53 +08:00
|
|
|
_loadingThread = new std::thread(&TextureCache::loadImage, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == _asyncRefCount)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
Director::getInstance()->getScheduler()->schedule(AX_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack),
|
2021-12-25 10:04:45 +08:00
|
|
|
this, 0, false);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
++_asyncRefCount;
|
|
|
|
|
|
|
|
// generate async struct
|
2021-12-25 10:04:45 +08:00
|
|
|
AsyncStruct* data = new AsyncStruct(fullpath, callback, callbackKey);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// add async struct into queue
|
2022-08-08 13:18:33 +08:00
|
|
|
_asyncStructQueue.emplace_back(data);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::unique_lock<std::mutex> ul(_requestMutex);
|
2022-08-08 13:18:33 +08:00
|
|
|
_requestQueue.emplace_back(data);
|
2019-11-23 20:27:39 +08:00
|
|
|
_sleepCondition.notify_one();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCache::unbindImageAsync(std::string_view callbackKey)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_asyncStructQueue.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& asyncStruct : _asyncStructQueue)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (asyncStruct->callbackKey == callbackKey)
|
|
|
|
{
|
|
|
|
asyncStruct->callback = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::unbindAllImageAsync()
|
|
|
|
{
|
|
|
|
if (_asyncStructQueue.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& asyncStruct : _asyncStructQueue)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
asyncStruct->callback = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::loadImage()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
AsyncStruct* asyncStruct = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
while (!_needQuit)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> ul(_requestMutex);
|
|
|
|
// pop an AsyncStruct from request queue
|
|
|
|
if (_requestQueue.empty())
|
|
|
|
{
|
|
|
|
asyncStruct = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asyncStruct = _requestQueue.front();
|
|
|
|
_requestQueue.pop_front();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == asyncStruct)
|
|
|
|
{
|
|
|
|
if (_needQuit)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_sleepCondition.wait(ul);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ul.unlock();
|
|
|
|
|
|
|
|
// load image
|
|
|
|
asyncStruct->loadSuccess = asyncStruct->image.initWithImageFileThreadSafe(asyncStruct->filename);
|
|
|
|
|
|
|
|
// ETC1 ALPHA supports.
|
2021-12-25 10:04:45 +08:00
|
|
|
if (asyncStruct->loadSuccess && asyncStruct->image.getFileType() == Image::Format::ETC1 &&
|
|
|
|
!s_etc1AlphaFileSuffix.empty())
|
|
|
|
{ // check whether alpha texture exists & load it
|
2019-11-23 20:27:39 +08:00
|
|
|
auto alphaFile = asyncStruct->filename + s_etc1AlphaFileSuffix;
|
|
|
|
if (FileUtils::getInstance()->isFileExist(alphaFile))
|
|
|
|
asyncStruct->imageAlpha.initWithImageFileThreadSafe(alphaFile);
|
|
|
|
}
|
|
|
|
// push the asyncStruct to response queue
|
|
|
|
_responseMutex.lock();
|
2022-08-08 13:18:33 +08:00
|
|
|
_responseQueue.emplace_back(asyncStruct);
|
2019-11-23 20:27:39 +08:00
|
|
|
_responseMutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::addImageAsyncCallBack(float /*dt*/)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = nullptr;
|
|
|
|
AsyncStruct* asyncStruct = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// pop an AsyncStruct from response queue
|
|
|
|
_responseMutex.lock();
|
|
|
|
if (_responseQueue.empty())
|
|
|
|
{
|
|
|
|
asyncStruct = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asyncStruct = _responseQueue.front();
|
|
|
|
_responseQueue.pop_front();
|
|
|
|
|
|
|
|
// the asyncStruct's sequence order in _asyncStructQueue must equal to the order in _responseQueue
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_ASSERT(asyncStruct == _asyncStructQueue.front());
|
2019-11-23 20:27:39 +08:00
|
|
|
_asyncStructQueue.pop_front();
|
|
|
|
}
|
|
|
|
_responseMutex.unlock();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == asyncStruct)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the image has been convert to texture or not
|
|
|
|
auto it = _textures.find(asyncStruct->filename);
|
|
|
|
if (it != _textures.end())
|
|
|
|
{
|
|
|
|
texture = it->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert image to texture
|
|
|
|
if (asyncStruct->loadSuccess)
|
|
|
|
{
|
|
|
|
Image* image = &(asyncStruct->image);
|
|
|
|
// generate texture in render thread
|
2021-12-08 00:11:53 +08:00
|
|
|
texture = new Texture2D();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
texture->initWithImage(image, asyncStruct->pixelFormat);
|
2021-12-25 10:04:45 +08:00
|
|
|
// parse 9-patch info
|
2019-11-23 20:27:39 +08:00
|
|
|
this->parseNinePatchImage(image, texture, asyncStruct->filename);
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
// cache the texture file name
|
|
|
|
VolatileTextureMgr::addImageTexture(texture, asyncStruct->filename);
|
|
|
|
#endif
|
|
|
|
// cache the texture. retain it, since it is added in the map
|
|
|
|
_textures.emplace(asyncStruct->filename, texture);
|
|
|
|
texture->retain();
|
|
|
|
|
|
|
|
texture->autorelease();
|
|
|
|
// ETC1 ALPHA supports.
|
2021-12-25 10:04:45 +08:00
|
|
|
if (asyncStruct->imageAlpha.getFileType() == Image::Format::ETC1)
|
|
|
|
{
|
2022-05-13 21:06:17 +08:00
|
|
|
texture->updateWithImage(&asyncStruct->imageAlpha, asyncStruct->pixelFormat, 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
texture = nullptr;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cocos2d: failed to call TextureCache::addImageAsync(%s)", asyncStruct->filename.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// call callback function
|
|
|
|
if (asyncStruct->callback)
|
|
|
|
{
|
|
|
|
(asyncStruct->callback)(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
// release the asyncStruct
|
|
|
|
delete asyncStruct;
|
|
|
|
--_asyncRefCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == _asyncRefCount)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
Director::getInstance()->getScheduler()->unschedule(AX_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack),
|
2021-12-25 10:04:45 +08:00
|
|
|
this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Texture2D* TextureCache::addImage(std::string_view path)
|
2022-05-13 21:06:17 +08:00
|
|
|
{
|
|
|
|
return addImage(path, Texture2D::getDefaultAlphaPixelFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* TextureCache::addImage(std::string_view path, PixelFormat format)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = nullptr;
|
|
|
|
Image* image = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
// Split up directory and filename
|
|
|
|
// MUTEX:
|
|
|
|
// Needed since addImageAsync calls this method from a different thread
|
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
2020-08-18 14:29:09 +08:00
|
|
|
if (fullpath.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto it = _textures.find(fullpath);
|
|
|
|
if (it != _textures.end())
|
|
|
|
texture = it->second;
|
|
|
|
|
|
|
|
if (!texture)
|
|
|
|
{
|
|
|
|
// all images are handled by UIImage except PVR extension that is handled by our own handler
|
|
|
|
do
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
image = new Image();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool bRet = image->initWithImageFile(fullpath);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(!bRet);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
texture = new Texture2D();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-05-13 21:06:17 +08:00
|
|
|
if (texture->initWithImage(image, format))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
// cache the texture file name
|
|
|
|
VolatileTextureMgr::addImageTexture(texture, fullpath);
|
|
|
|
#endif
|
|
|
|
// texture already retained, no need to re-retain it
|
|
|
|
_textures.emplace(fullpath, texture);
|
|
|
|
|
|
|
|
//-- ANDROID ETC1 ALPHA SUPPORTS.
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string alphaFullPath{path};
|
|
|
|
alphaFullPath += s_etc1AlphaFileSuffix;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (image->getFileType() == Image::Format::ETC1 && !s_etc1AlphaFileSuffix.empty() &&
|
|
|
|
FileUtils::getInstance()->isFileExist(alphaFullPath))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Image imageAlpha;
|
|
|
|
if (imageAlpha.initWithImageFile(alphaFullPath))
|
|
|
|
{
|
2022-05-13 21:06:17 +08:00
|
|
|
texture->updateWithImage(&imageAlpha, format, 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// parse 9-patch info
|
2019-11-23 20:27:39 +08:00
|
|
|
this->parseNinePatchImage(image, texture, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cocos2d: Couldn't create texture for file:%s in TextureCache", path.data());
|
|
|
|
AX_SAFE_RELEASE(texture);
|
2019-11-23 20:27:39 +08:00
|
|
|
texture = nullptr;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(image);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void TextureCache::parseNinePatchImage(ax::Image* image, ax::Texture2D* texture, std::string_view path)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (NinePatchImageParser::isNinePatchImage(path))
|
|
|
|
{
|
|
|
|
Rect frameRect = Rect(0, 0, image->getWidth(), image->getHeight());
|
|
|
|
NinePatchImageParser parser(image, frameRect, false);
|
|
|
|
texture->addSpriteFrameCapInset(nullptr, parser.parseCapInset());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Texture2D* TextureCache::addImage(Image* image, std::string_view key)
|
2022-05-13 21:06:17 +08:00
|
|
|
{
|
|
|
|
return addImage(image, key, Texture2D::getDefaultAlphaPixelFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* TextureCache::addImage(Image* image, std::string_view key, PixelFormat format)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(image != nullptr, "TextureCache: image MUST not be nil");
|
|
|
|
AXASSERT(image->getData() != nullptr, "TextureCache: image MUST not be nil");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto it = _textures.find(key);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _textures.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
texture = it->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
texture = new Texture2D();
|
2022-05-13 21:06:17 +08:00
|
|
|
if (texture->initWithImage(image, format))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_textures.emplace(key, texture);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(texture);
|
2021-12-08 00:11:53 +08:00
|
|
|
texture = nullptr;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cocos2d: initWithImage failed!");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
VolatileTextureMgr::addImage(texture, image);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool TextureCache::reloadTexture(std::string_view fileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
2020-08-18 14:29:09 +08:00
|
|
|
if (fullpath.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = _textures.find(fullpath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _textures.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
texture = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!texture)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
texture = this->addImage(fullpath);
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = (texture != nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Image image;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
bool bRet = image.initWithImageFile(fullpath);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(!bRet);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
ret = texture->initWithImage(&image);
|
2019-11-23 20:27:39 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextureCache - Remove
|
|
|
|
|
|
|
|
void TextureCache::removeAllTextures()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& texture : _textures)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
texture.second->release();
|
|
|
|
}
|
|
|
|
_textures.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::removeUnusedTextures()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto it = _textures.cbegin(); it != _textures.cend(); /* nothing */)
|
|
|
|
{
|
|
|
|
Texture2D* tex = it->second;
|
|
|
|
if (tex->getReferenceCount() == 1)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cocos2d: TextureCache: removing unused texture: %s", it->first.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
tex->release();
|
|
|
|
it = _textures.erase(it);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::removeTexture(Texture2D* texture)
|
|
|
|
{
|
|
|
|
if (!texture)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto it = _textures.cbegin(); it != _textures.cend(); /* nothing */)
|
|
|
|
{
|
|
|
|
if (it->second == texture)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
it->second->release();
|
|
|
|
it = _textures.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCache::removeTextureForKey(std::string_view textureKeyName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto it = _textures.find(textureKeyName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _textures.end())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto key = FileUtils::getInstance()->fullPathForFilename(textureKeyName);
|
|
|
|
it = _textures.find(key);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _textures.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
it->second->release();
|
|
|
|
_textures.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Texture2D* TextureCache::getTextureForKey(std::string_view textureKeyName) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto it = _textures.find(textureKeyName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _textures.end())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto key = FileUtils::getInstance()->fullPathForFilename(textureKeyName);
|
|
|
|
it = _textures.find(key);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (it != _textures.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
std::string TextureCache::getTextureFilePath(ax::Texture2D* texture) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& item : _textures)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (item.second == texture)
|
|
|
|
{
|
|
|
|
return item.first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::waitForQuit()
|
|
|
|
{
|
|
|
|
// notify sub thread to quick
|
|
|
|
std::unique_lock<std::mutex> ul(_requestMutex);
|
|
|
|
_needQuit = true;
|
|
|
|
_sleepCondition.notify_one();
|
|
|
|
ul.unlock();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_loadingThread)
|
|
|
|
_loadingThread->join();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextureCache::getCachedTextureInfo() const
|
|
|
|
{
|
|
|
|
std::string buffer;
|
|
|
|
char buftmp[4096];
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int count = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int totalBytes = 0;
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& texture : _textures)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
memset(buftmp, 0, sizeof(buftmp));
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* tex = texture.second;
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int bpp = tex->getBitsPerPixelForFormat();
|
|
|
|
// Each texture takes up width * height * bytesPerPixel bytes.
|
|
|
|
auto bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8;
|
|
|
|
totalBytes += bytes;
|
|
|
|
count++;
|
2021-12-25 10:04:45 +08:00
|
|
|
snprintf(buftmp, sizeof(buftmp) - 1, "\"%s\" rc=%d id=%p %d x %d @ %d bpp => %d KB\n", texture.first.c_str(),
|
|
|
|
(int32_t)tex->getReferenceCount(), tex->getBackendTexture(), (int32_t)tex->getPixelsWide(),
|
|
|
|
(int32_t)tex->getPixelsHigh(), (int32_t)bpp, (int32_t)bytes / 1024);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
buffer += buftmp;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +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));
|
2019-11-23 20:27:39 +08:00
|
|
|
buffer += buftmp;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCache::renameTextureWithKey(std::string_view srcName, std::string_view dstName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto it = _textures.find(srcName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _textures.end())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto key = FileUtils::getInstance()->fullPathForFilename(srcName);
|
|
|
|
it = _textures.find(key);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _textures.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(dstName);
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* tex = it->second;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
Image image;
|
|
|
|
bool ret = image.initWithImageFile(dstName);
|
|
|
|
if (ret)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
tex->initWithImage(&image);
|
|
|
|
_textures.emplace(fullpath, tex);
|
|
|
|
_textures.erase(it);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::list<VolatileTexture*> VolatileTextureMgr::_textures;
|
|
|
|
bool VolatileTextureMgr::_isReloading = false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture::VolatileTexture(Texture2D* t)
|
|
|
|
: _texture(t)
|
|
|
|
, _uiImage(nullptr)
|
|
|
|
, _cashedImageType(kInvalid)
|
|
|
|
, _textureData(nullptr)
|
|
|
|
, _pixelFormat(backend::PixelFormat::RGBA8)
|
|
|
|
, _fileName("")
|
|
|
|
, _text("")
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
VolatileTexture::~VolatileTexture()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_uiImage);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void VolatileTextureMgr::addImageTexture(Texture2D* tt, std::string_view imageFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_isReloading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* vt = findVolotileTexture(tt);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
vt->_cashedImageType = VolatileTexture::kImageFile;
|
2021-12-25 10:04:45 +08:00
|
|
|
vt->_fileName = imageFileName;
|
|
|
|
vt->_pixelFormat = tt->getPixelFormat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void VolatileTextureMgr::addImage(Texture2D* tt, Image* image)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (tt == nullptr || image == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
VolatileTexture* vt = findVolotileTexture(tt);
|
2019-11-23 20:27:39 +08:00
|
|
|
image->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
vt->_uiImage = image;
|
2019-11-23 20:27:39 +08:00
|
|
|
vt->_cashedImageType = VolatileTexture::kImage;
|
2021-12-25 10:04:45 +08:00
|
|
|
vt->_pixelFormat = tt->getPixelFormat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* VolatileTextureMgr::findVolotileTexture(Texture2D* tt)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* vt = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& texture : _textures)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* v = texture;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (v->_texture == tt)
|
|
|
|
{
|
|
|
|
vt = v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vt)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
vt = new VolatileTexture(tt);
|
2022-08-08 13:18:33 +08:00
|
|
|
_textures.emplace_back(vt);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return vt;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void VolatileTextureMgr::addDataTexture(Texture2D* tt,
|
|
|
|
void* data,
|
|
|
|
int dataLen,
|
|
|
|
backend::PixelFormat pixelFormat,
|
|
|
|
const Vec2& contentSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_isReloading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* vt = findVolotileTexture(tt);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
vt->_cashedImageType = VolatileTexture::kImageData;
|
2021-12-25 10:04:45 +08:00
|
|
|
vt->_textureData = data;
|
|
|
|
vt->_dataLen = dataLen;
|
|
|
|
vt->_pixelFormat = pixelFormat;
|
|
|
|
vt->_textureSize = contentSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void VolatileTextureMgr::addStringTexture(Texture2D* tt, const char* text, const FontDefinition& fontDefinition)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_isReloading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* vt = findVolotileTexture(tt);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
vt->_cashedImageType = VolatileTexture::kString;
|
2021-12-25 10:04:45 +08:00
|
|
|
vt->_text = text;
|
|
|
|
vt->_fontDefinition = fontDefinition;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void VolatileTextureMgr::removeTexture(Texture2D* t)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& item : _textures)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* vt = item;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (vt->_texture == t)
|
|
|
|
{
|
|
|
|
_textures.remove(vt);
|
|
|
|
delete vt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolatileTextureMgr::reloadAllTextures()
|
|
|
|
{
|
|
|
|
_isReloading = true;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("reload all texture");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& texture : _textures)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture* vt = texture;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
switch (vt->_cashedImageType)
|
|
|
|
{
|
|
|
|
case VolatileTexture::kImageFile:
|
|
|
|
{
|
|
|
|
reloadTexture(vt->_texture, vt->_fileName, vt->_pixelFormat);
|
|
|
|
|
|
|
|
// etc1 support check whether alpha texture exists & load it
|
|
|
|
Image image;
|
|
|
|
if (image.initWithImageFile(vt->_fileName + TextureCache::getETC1AlphaFileSuffix()))
|
|
|
|
{
|
2021-06-27 20:58:50 +08:00
|
|
|
vt->_texture->updateWithImage(&image, vt->_pixelFormat, 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VolatileTexture::kImageData:
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
vt->_texture->initWithData(vt->_textureData, vt->_dataLen, vt->_pixelFormat, vt->_textureSize.width,
|
2022-02-06 13:18:27 +08:00
|
|
|
vt->_textureSize.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VolatileTexture::kString:
|
|
|
|
{
|
|
|
|
vt->_texture->initWithString(vt->_text.c_str(), vt->_fontDefinition);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VolatileTexture::kImage:
|
|
|
|
{
|
|
|
|
vt->_texture->initWithImage(vt->_uiImage, vt->_pixelFormat);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_isReloading = false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void VolatileTextureMgr::reloadTexture(Texture2D* texture, std::string_view filename, backend::PixelFormat pixelFormat)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!texture)
|
|
|
|
return;
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
Image* image = new Image();
|
2021-12-25 10:04:45 +08:00
|
|
|
Data data = FileUtils::getInstance()->getDataFromFile(filename);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (image->initWithImageData(data.getBytes(), data.getSize()))
|
2019-11-23 20:27:39 +08:00
|
|
|
texture->initWithImage(image, pixelFormat);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(image);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|