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-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCTEXTURE_CACHE_H__
|
|
|
|
#define __CCTEXTURE_CACHE_H__
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <functional>
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Ref.h"
|
|
|
|
#include "renderer/Texture2D.h"
|
|
|
|
#include "platform/Image.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <list>
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup _2d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* From version 3.0, TextureCache will never be treated as a singleton, it will be owned by director.
|
|
|
|
* All call by TextureCache::getInstance() should be replaced by Director::getInstance()->getTextureCache().
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** @brief Singleton that handles the loading of textures.
|
2021-12-25 10:04:45 +08:00
|
|
|
* Once the texture is loaded, the next time it will return.
|
|
|
|
* A reference of the previously loaded texture reducing GPU & CPU memory.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL TextureCache : public Ref
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ETC1 ALPHA supports.
|
2021-12-31 12:12:40 +08:00
|
|
|
static void setETC1AlphaFileSuffix(std::string_view suffix);
|
2019-11-23 20:27:39 +08:00
|
|
|
static std::string getETC1AlphaFileSuffix();
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
|
|
|
TextureCache();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~TextureCache();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual std::string getDescription() const;
|
2023-08-09 14:37:43 +08:00
|
|
|
|
|
|
|
/** Gets a 2x2 white texture */
|
|
|
|
Texture2D* getWhiteTexture();
|
|
|
|
|
|
|
|
/** Gets 1x1 dummy texture with alpha=0 */
|
|
|
|
Texture2D* getDummyTexture();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Dictionary* snapshotTextures();
|
|
|
|
|
|
|
|
/** Returns a Texture2D object given an filename.
|
|
|
|
* If the filename was not previously loaded, it will create a new Texture2D.
|
|
|
|
* Object and it will return it. It will use the filename as a key.
|
|
|
|
* Otherwise it will return a reference of a previously loaded image.
|
|
|
|
* Supported image extensions: .png, .bmp, .jpeg, .pvr.
|
|
|
|
@param filepath The file path.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
Texture2D* addImage(std::string_view filepath);
|
2022-05-13 21:06:17 +08:00
|
|
|
Texture2D* addImage(std::string_view filepath, PixelFormat format);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Returns a Texture2D object given a file image.
|
|
|
|
* If the file image was not previously loaded, it will create a new Texture2D object and it will return it.
|
2022-05-13 21:06:17 +08:00
|
|
|
* Otherwise it will load a texture in a offthread, and when the image is loaded, the callback will be called with
|
2021-12-25 10:04:45 +08:00
|
|
|
the Texture2D as a parameter.
|
2019-11-23 20:27:39 +08:00
|
|
|
* The callback will be called from the main thread, so it is safe to create any cocos2d object from the callback.
|
|
|
|
* Supported image extensions: .png, .jpg
|
|
|
|
@param filepath The file path.
|
|
|
|
@param callback A callback function would be invoked after the image is loaded.
|
|
|
|
@since v0.8
|
2022-05-13 21:06:17 +08:00
|
|
|
|
|
|
|
@remark Please don't invoke Texture2D::setDefaultAlphaPixelFormat in main GL thread before invoke this API.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void addImageAsync(std::string_view filepath, const std::function<void(Texture2D*)>& callback);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void 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
|
|
|
|
|
|
|
/** Unbind a specified bound image asynchronous callback.
|
2021-12-25 10:04:45 +08:00
|
|
|
* In the case an object who was bound to an image asynchronous callback was destroyed before the callback is
|
|
|
|
* invoked, the object always need to unbind this callback manually.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @param filename It's the related/absolute path of the file image.
|
|
|
|
* @since v3.1
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void unbindImageAsync(std::string_view filename);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Unbind all bound image asynchronous load callbacks.
|
|
|
|
* @since v3.1
|
|
|
|
*/
|
|
|
|
virtual void unbindAllImageAsync();
|
|
|
|
|
|
|
|
/** Returns a Texture2D object given an Image.
|
2021-12-25 10:04:45 +08:00
|
|
|
* If the image was not previously loaded, it will create a new Texture2D object and it will return it.
|
|
|
|
* Otherwise it will return a reference of a previously loaded image.
|
|
|
|
* @param key The "key" parameter will be used as the "key" for the cache.
|
|
|
|
* If "key" is nil, then a new texture will be created each time.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
Texture2D* addImage(Image* image, std::string_view key);
|
2022-05-13 21:06:17 +08:00
|
|
|
Texture2D* addImage(Image* image, std::string_view key, PixelFormat format);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Returns an already created texture. Returns nil if the texture doesn't exist.
|
|
|
|
@param key It's the related/absolute path of the file image.
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
Texture2D* getTextureForKey(std::string_view key) const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Reload texture from the image file.
|
2021-12-25 10:04:45 +08:00
|
|
|
* If the file image hasn't loaded before, load it.
|
|
|
|
* Otherwise the texture will be reloaded from the file image.
|
|
|
|
* @param fileName It's the related/absolute path of the file image.
|
|
|
|
* @return True if the reloading is succeed, otherwise return false.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool reloadTexture(std::string_view fileName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Purges the dictionary of loaded textures.
|
2021-12-25 10:04:45 +08:00
|
|
|
* Call this method if you receive the "Memory Warning".
|
|
|
|
* In the short term: it will free some resources preventing your app from being killed.
|
|
|
|
* In the medium term: it will allocate more resources.
|
|
|
|
* In the long term: it will be the same.
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
void removeAllTextures();
|
|
|
|
|
|
|
|
/** Removes unused textures.
|
2021-12-25 10:04:45 +08:00
|
|
|
* Textures that have a retain count of 1 will be deleted.
|
|
|
|
* It is convenient to call this method after when starting a new Scene.
|
|
|
|
* @since v0.8
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
void removeUnusedTextures();
|
|
|
|
|
|
|
|
/** Deletes a texture from the cache given a texture.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
void removeTexture(Texture2D* texture);
|
|
|
|
|
|
|
|
/** Deletes a texture from the cache given a its key name.
|
|
|
|
@param key It's the related/absolute path of the file image.
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
void removeTextureForKey(std::string_view key);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
/** Output to AXLOG the current contents of this TextureCache.
|
2021-12-25 10:04:45 +08:00
|
|
|
* This will attempt to calculate the size of each texture, and the total texture memory in use.
|
|
|
|
*
|
|
|
|
* @since v1.0
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string getCachedTextureInfo() const;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// Wait for texture cache to quit before destroy instance.
|
2019-11-23 20:27:39 +08:00
|
|
|
/**Called by director, please do not called outside.*/
|
|
|
|
void waitForQuit();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file path of the texture
|
|
|
|
*
|
|
|
|
* @param texture A Texture2D object pointer.
|
|
|
|
*
|
|
|
|
* @return The full path of the file.
|
|
|
|
*/
|
|
|
|
std::string getTextureFilePath(Texture2D* texture) const;
|
|
|
|
|
|
|
|
/** Reload texture from a new file.
|
2021-12-25 10:04:45 +08:00
|
|
|
* This function is mainly for editor, won't suggest use it in game for performance reason.
|
|
|
|
*
|
|
|
|
* @param srcName Original texture file name.
|
|
|
|
* @param dstName New texture file name.
|
|
|
|
*
|
|
|
|
* @since v3.10
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
void renameTextureWithKey(std::string_view srcName, std::string_view dstName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void addImageAsyncCallBack(float dt);
|
|
|
|
void loadImage();
|
2021-12-31 12:12:40 +08:00
|
|
|
void parseNinePatchImage(Image* image, Texture2D* texture, std::string_view path);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
public:
|
|
|
|
protected:
|
|
|
|
struct AsyncStruct;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::thread* _loadingThread;
|
|
|
|
|
|
|
|
std::deque<AsyncStruct*> _asyncStructQueue;
|
|
|
|
std::deque<AsyncStruct*> _requestQueue;
|
|
|
|
std::deque<AsyncStruct*> _responseQueue;
|
|
|
|
|
|
|
|
std::mutex _requestMutex;
|
|
|
|
std::mutex _responseMutex;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::condition_variable _sleepCondition;
|
|
|
|
|
|
|
|
bool _needQuit;
|
|
|
|
|
|
|
|
int _asyncRefCount;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
hlookup::string_map<Texture2D*> _textures;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static std::string s_etc1AlphaFileSuffix;
|
|
|
|
};
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
class VolatileTexture
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
kInvalid = 0,
|
|
|
|
kImageFile,
|
|
|
|
kImageData,
|
|
|
|
kString,
|
|
|
|
kImage,
|
2021-12-25 10:04:45 +08:00
|
|
|
} ccCachedImageType;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
VolatileTexture(Texture2D* t);
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
~VolatileTexture();
|
|
|
|
|
|
|
|
protected:
|
2021-12-25 10:04:45 +08:00
|
|
|
friend class VolatileTextureMgr;
|
|
|
|
Texture2D* _texture;
|
|
|
|
|
|
|
|
Image* _uiImage;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ccCachedImageType _cashedImageType;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void* _textureData;
|
|
|
|
int _dataLen;
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 _textureSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
backend::PixelFormat _pixelFormat;
|
|
|
|
|
|
|
|
std::string _fileName;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string _text;
|
|
|
|
FontDefinition _fontDefinition;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL VolatileTextureMgr
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-12-31 12:12:40 +08:00
|
|
|
static void addImageTexture(Texture2D* tt, std::string_view imageFileName);
|
2023-03-11 22:10:18 +08:00
|
|
|
static void addStringTexture(Texture2D* tt, std::string_view text, const FontDefinition& fontDefinition);
|
2021-12-25 10:04:45 +08:00
|
|
|
static void addDataTexture(Texture2D* tt,
|
|
|
|
void* data,
|
|
|
|
int dataLen,
|
|
|
|
backend::PixelFormat pixelFormat,
|
|
|
|
const Vec2& contentSize);
|
|
|
|
static void addImage(Texture2D* tt, Image* image);
|
|
|
|
static void removeTexture(Texture2D* t);
|
2019-11-23 20:27:39 +08:00
|
|
|
static void reloadAllTextures();
|
|
|
|
|
|
|
|
public:
|
|
|
|
static std::list<VolatileTexture*> _textures;
|
|
|
|
static bool _isReloading;
|
|
|
|
|
|
|
|
// find VolatileTexture by Texture2D*
|
|
|
|
// if not found, create a new one
|
2021-12-25 10:04:45 +08:00
|
|
|
static VolatileTexture* findVolotileTexture(Texture2D* tt);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
private:
|
2021-12-31 12:12:40 +08:00
|
|
|
static void reloadTexture(Texture2D* texture, std::string_view filename, backend::PixelFormat pixelFormat);
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// end of textures group
|
|
|
|
/// @}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif //__CCTEXTURE_CACHE_H__
|