2011-03-19 10:59:01 +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.
|
2011-03-19 10:59:01 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCTEXTURE_CACHE_H__
|
|
|
|
#define __CCTEXTURE_CACHE_H__
|
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
#include <string>
|
|
|
|
#include <mutex>
|
2013-06-26 14:48:19 +08:00
|
|
|
#include <thread>
|
2013-06-21 15:29:21 +08:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
2013-09-07 13:55:11 +08:00
|
|
|
#include <unordered_map>
|
2014-01-02 17:23:00 +08:00
|
|
|
#include <functional>
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "platform/CCImage.h"
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCImage.h"
|
2011-04-06 16:29:58 +08:00
|
|
|
#include <list>
|
|
|
|
#endif
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-26 15:20:25 +08:00
|
|
|
* @addtogroup _2d
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
2013-11-11 15:40:12 +08:00
|
|
|
/*
|
2015-03-23 17:50:22 +08:00
|
|
|
* From version 3.0, TextureCache will never to treated as a singleton, it will be owned by director.
|
|
|
|
* All call by TextureCache::getInstance() should be replaced by Director::getInstance()->getTextureCache().
|
2013-11-11 15:40:12 +08:00
|
|
|
*/
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** @brief Singleton that handles the loading of textures.
|
|
|
|
* Once the texture is loaded, the next time it will return.
|
|
|
|
* A reference of the previously loaded texture reducing GPU & CPU memory.
|
2010-07-16 14:15:06 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
class CC_DLL TextureCache : public Ref
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
public:
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Returns the shared instance of the cache. */
|
2013-11-07 19:10:14 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static TextureCache * getInstance();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** @deprecated Use getInstance() instead. */
|
2013-11-11 15:40:12 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static TextureCache * sharedTextureCache();
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Purges the cache. It releases the retained instance.
|
2013-07-12 06:24:23 +08:00
|
|
|
@since v0.99.0
|
|
|
|
*/
|
2013-11-07 19:10:14 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void destroyInstance();
|
2013-07-12 06:24:23 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** @deprecated Use destroyInstance() instead. */
|
2013-11-11 15:40:12 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeSharedTextureCache();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Reload all textures.
|
|
|
|
Should not call it, called by frame work.
|
|
|
|
Now the function do nothing, use VolatileTextureMgr::reloadAllTextures.
|
2013-07-16 03:43:22 +08:00
|
|
|
*/
|
2013-11-11 11:38:02 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void reloadAllTextures();
|
2013-07-16 03:43:22 +08:00
|
|
|
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-16 03:43:22 +08:00
|
|
|
TextureCache();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual ~TextureCache();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-13 06:38:12 +08:00
|
|
|
virtual std::string getDescription() const;
|
2013-07-16 03:43:22 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
// Dictionary* snapshotTextures();
|
2013-07-16 03:43:22 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
/** Returns a Texture2D object given an filename.
|
2015-03-23 17:50:22 +08:00
|
|
|
* 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.
|
2012-09-17 15:02:24 +08:00
|
|
|
* Otherwise it will return a reference of a previously loaded image.
|
2015-03-23 17:50:22 +08:00
|
|
|
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr.
|
|
|
|
@param filepath A null terminated string.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* addImage(const std::string &filepath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Returns a Texture2D object given a file image.
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the file image was not previously loaded, it will create a new Texture2D object and it will return it.
|
2012-04-19 14:35:52 +08:00
|
|
|
* Otherwise it will load a texture in a new thread, and when the image is loaded, the callback will be called with the Texture2D as a parameter.
|
|
|
|
* 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
|
2015-03-23 17:50:22 +08:00
|
|
|
@param filepath A null terminated string.
|
2015-09-22 16:08:23 +08:00
|
|
|
@param callback A callback function would be invoked after the image is loaded.
|
2015-03-23 17:50:22 +08:00
|
|
|
@since v0.8
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2014-04-14 10:01:17 +08:00
|
|
|
virtual void addImageAsync(const std::string &filepath, const std::function<void(Texture2D*)>& callback);
|
2014-05-23 17:08:22 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Unbind a specified bound image asynchronous callback.
|
2014-05-23 17:08:22 +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.
|
2015-03-23 17:50:22 +08:00
|
|
|
* @param filename It's the related/absolute path of the file image.
|
2014-05-23 17:08:22 +08:00
|
|
|
* @since v3.1
|
|
|
|
*/
|
|
|
|
virtual void unbindImageAsync(const std::string &filename);
|
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Unbind all bound image asynchronous load callbacks.
|
2014-05-23 17:08:22 +08:00
|
|
|
* @since v3.1
|
|
|
|
*/
|
|
|
|
virtual void unbindAllImageAsync();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
/** Returns a Texture2D object given an Image.
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the image was not previously loaded, it will create a new Texture2D object and it will return it.
|
2013-09-07 06:33:28 +08:00
|
|
|
* Otherwise it will return a reference of a previously loaded image.
|
2015-03-23 17:50:22 +08:00
|
|
|
* @param key The "key" parameter will be used as the "key" for the cache.
|
2012-04-19 14:35:52 +08:00
|
|
|
* If "key" is nil, then a new texture will be created each time.
|
|
|
|
*/
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* addImage(Image *image, const std::string &key);
|
2013-12-24 15:49:58 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE Texture2D* addUIImage(Image *image, const std::string& key) { return addImage(image,key); }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Returns an already created texture. Returns nil if the texture doesn't exist.
|
2015-03-23 17:50:22 +08:00
|
|
|
@param key It's the related/absolute path of the file image.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.99.5
|
|
|
|
*/
|
2013-09-07 13:55:11 +08:00
|
|
|
Texture2D* getTextureForKey(const std::string& key) const;
|
2013-12-24 15:49:58 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE Texture2D* textureForKey(const std::string& key) const { return getTextureForKey(key); }
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Reload texture from the image file.
|
2014-02-26 11:44:54 +08:00
|
|
|
* If the file image hasn't loaded before, load it.
|
|
|
|
* Otherwise the texture will be reloaded from the file image.
|
2015-03-23 17:50:22 +08:00
|
|
|
* @param fileName It's the related/absolute path of the file image.
|
|
|
|
* @return True if the reloading is succeed, otherwise return false.
|
2014-02-26 11:44:54 +08:00
|
|
|
*/
|
|
|
|
bool reloadTexture(const std::string& fileName);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Purges the dictionary of loaded textures.
|
2015-03-23 17:50:22 +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.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
void removeAllTextures();
|
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Removes unused textures.
|
|
|
|
* Textures that have a retain count of 1 will be deleted.
|
|
|
|
* It is convenient to call this method after when starting a new Scene.
|
2012-04-19 14:35:52 +08:00
|
|
|
* @since v0.8
|
|
|
|
*/
|
|
|
|
void removeUnusedTextures();
|
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Deletes a texture from the cache given a texture.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeTexture(Texture2D* texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Deletes a texture from the cache given a its key name.
|
|
|
|
@param key It's the related/absolute path of the file image.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2013-09-07 13:55:11 +08:00
|
|
|
void removeTextureForKey(const std::string &key);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-23 17:50:22 +08:00
|
|
|
/** Output to CCLOG the current contents of this TextureCache.
|
|
|
|
* This will attempt to calculate the size of each texture, and the total texture memory in use.
|
2012-04-19 14:35:52 +08:00
|
|
|
*
|
|
|
|
* @since v1.0
|
|
|
|
*/
|
2014-01-15 09:22:45 +08:00
|
|
|
std::string getCachedTextureInfo() const;
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2015-09-22 16:08:23 +08:00
|
|
|
//Wait for texture cache to quit before destroy instance.
|
2015-03-23 17:50:22 +08:00
|
|
|
/**Called by director, please do not called outside.*/
|
2013-11-07 18:52:36 +08:00
|
|
|
void waitForQuit();
|
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
/**
|
|
|
|
* Get the file path of the texture
|
|
|
|
*
|
|
|
|
* @param texture A Texture2D object pointer.
|
|
|
|
*
|
|
|
|
* @return The full path of the file.
|
|
|
|
*/
|
|
|
|
const std::string getTextureFilePath(Texture2D* texture)const;
|
|
|
|
|
2015-11-13 17:44:23 +08:00
|
|
|
void setDirty(bool dirty) { _dirty = dirty; }
|
|
|
|
bool isDirty() const { return _dirty; }
|
2015-11-24 18:29:58 +08:00
|
|
|
|
2015-11-25 11:33:24 +08:00
|
|
|
/** Reload texuture from a new file.
|
|
|
|
* 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
|
|
|
|
*/
|
2015-11-25 11:15:17 +08:00
|
|
|
void renameTextureWithKey(const std::string srcName, const std::string dstName);
|
2015-11-24 18:29:58 +08:00
|
|
|
|
2015-11-13 17:44:23 +08:00
|
|
|
|
2013-06-21 15:29:21 +08:00
|
|
|
private:
|
|
|
|
void addImageAsyncCallBack(float dt);
|
2013-06-21 15:49:45 +08:00
|
|
|
void loadImage();
|
2015-05-21 16:04:37 +08:00
|
|
|
void parseNinePatchImage(Image* image, Texture2D* texture, const std::string& path);
|
2013-06-24 10:21:36 +08:00
|
|
|
public:
|
|
|
|
protected:
|
2015-07-08 15:13:48 +08:00
|
|
|
struct AsyncStruct;
|
2013-06-26 14:48:19 +08:00
|
|
|
|
|
|
|
std::thread* _loadingThread;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
std::deque<AsyncStruct*> _asyncStructQueue;
|
|
|
|
std::deque<AsyncStruct*> _requestQueue;
|
|
|
|
std::deque<AsyncStruct*> _responseQueue;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2015-07-08 15:13:48 +08:00
|
|
|
std::mutex _requestMutex;
|
|
|
|
std::mutex _responseMutex;
|
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
std::condition_variable _sleepCondition;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
bool _needQuit;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
int _asyncRefCount;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
std::unordered_map<std::string, Texture2D*> _textures;
|
2015-11-13 17:44:23 +08:00
|
|
|
|
|
|
|
bool _dirty;
|
2011-04-06 16:29:58 +08:00
|
|
|
};
|
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
class VolatileTexture
|
|
|
|
{
|
2013-07-16 03:43:22 +08:00
|
|
|
typedef enum {
|
|
|
|
kInvalid = 0,
|
|
|
|
kImageFile,
|
|
|
|
kImageData,
|
|
|
|
kString,
|
|
|
|
kImage,
|
|
|
|
}ccCachedImageType;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-08 16:47:33 +08:00
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
VolatileTexture(Texture2D *t);
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
~VolatileTexture();
|
|
|
|
|
|
|
|
protected:
|
2013-11-08 16:47:33 +08:00
|
|
|
friend class VolatileTextureMgr;
|
2013-07-16 16:33:02 +08:00
|
|
|
Texture2D *_texture;
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-07-16 16:33:02 +08:00
|
|
|
Image *_uiImage;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCachedImageType _cashedImageType;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
void *_textureData;
|
2013-07-26 17:34:44 +08:00
|
|
|
int _dataLen;
|
2013-06-20 14:13:12 +08:00
|
|
|
Size _textureSize;
|
2013-07-25 19:52:44 +08:00
|
|
|
Texture2D::PixelFormat _pixelFormat;
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _fileName;
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2014-03-25 16:19:34 +08:00
|
|
|
bool _hasMipmaps;
|
2013-08-06 11:34:48 +08:00
|
|
|
Texture2D::TexParams _texParams;
|
|
|
|
std::string _text;
|
|
|
|
FontDefinition _fontDefinition;
|
2011-03-19 10:59:01 +08:00
|
|
|
};
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2014-10-15 05:21:34 +08:00
|
|
|
class CC_DLL VolatileTextureMgr
|
2013-11-08 16:47:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-12-25 13:57:17 +08:00
|
|
|
static void addImageTexture(Texture2D *tt, const std::string& imageFileName);
|
2013-11-08 16:47:33 +08:00
|
|
|
static void addStringTexture(Texture2D *tt, const char* text, const FontDefinition& fontDefinition);
|
|
|
|
static void addDataTexture(Texture2D *tt, void* data, int dataLen, Texture2D::PixelFormat pixelFormat, const Size& contentSize);
|
|
|
|
static void addImage(Texture2D *tt, Image *image);
|
|
|
|
|
2014-03-25 16:19:34 +08:00
|
|
|
static void setHasMipmaps(Texture2D *t, bool hasMipmaps);
|
2013-11-08 16:47:33 +08:00
|
|
|
static void setTexParameters(Texture2D *t, const Texture2D::TexParams &texParams);
|
|
|
|
static void removeTexture(Texture2D *t);
|
|
|
|
static void reloadAllTextures();
|
|
|
|
public:
|
|
|
|
static std::list<VolatileTexture*> _textures;
|
|
|
|
static bool _isReloading;
|
|
|
|
private:
|
|
|
|
// find VolatileTexture by Texture2D*
|
|
|
|
// if not found, create a new one
|
|
|
|
static VolatileTexture* findVolotileTexture(Texture2D *tt);
|
|
|
|
};
|
|
|
|
|
2011-04-06 16:29:58 +08:00
|
|
|
#endif
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of textures group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
#endif //__CCTEXTURE_CACHE_H__
|
|
|
|
|