2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga 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>
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
#include "cocoa/CCDictionary.h"
|
|
|
|
#include "textures/CCTexture2D.h"
|
2013-06-21 15:29:21 +08:00
|
|
|
#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
|
2012-06-19 17:22:55 +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
|
|
|
/**
|
|
|
|
* @addtogroup textures
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief Singleton that handles the loading of textures
|
2010-07-16 14:15:06 +08:00
|
|
|
* Once the texture is loaded, the next time it will return
|
|
|
|
* a reference of the previously loaded texture reducing GPU & CPU memory
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TextureCache : public Object
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
public:
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Returns the shared instance of the cache */
|
2013-07-12 06:24:23 +08:00
|
|
|
static TextureCache * getInstance();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** purges the cache. It releases the retained instance.
|
2013-07-12 06:24:23 +08:00
|
|
|
@since v0.99.0
|
|
|
|
*/
|
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/** @deprecated Use getInstance() instead */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static TextureCache * sharedTextureCache();
|
|
|
|
|
|
|
|
/** @deprecated Use destroyInstance() instead */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeSharedTextureCache();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
/** Reload all textures
|
|
|
|
It's only useful when the value of CC_ENABLE_CACHE_TEXTURE_DATA is 1
|
|
|
|
*/
|
|
|
|
static void reloadAllTextures();
|
|
|
|
|
|
|
|
public:
|
|
|
|
TextureCache();
|
|
|
|
virtual ~TextureCache();
|
|
|
|
|
|
|
|
const char* description(void) const;
|
|
|
|
|
|
|
|
Dictionary* snapshotTextures();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns a Texture2D object given an file image
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the file image was not previously loaded, it will create a new Texture2D
|
2012-04-19 14:35:52 +08:00
|
|
|
* 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.
|
2012-04-19 14:35:52 +08:00
|
|
|
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* addImage(const char* fileimage);
|
2012-04-19 14:35:52 +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
|
|
|
|
* @since v0.8
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void addImageAsync(const char *path, Object *target, SEL_CallFuncO selector);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Returns a Texture2D object given an UIImage 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.
|
2012-04-19 14:35:52 +08:00
|
|
|
* Otherwise it will return a reference of a previously loaded image
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* addUIImage(Image *image, const char *key);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Returns an already created texture. Returns nil if the texture doesn't exist.
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* textureForKey(const char* key);
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Purges the dictionary of loaded textures.
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
void removeAllTextures();
|
|
|
|
|
|
|
|
/** Removes unused textures
|
|
|
|
* Textures that have a retain count of 1 will be deleted
|
2012-09-17 15:02:24 +08:00
|
|
|
* 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();
|
|
|
|
|
|
|
|
/** Deletes a texture from the cache given a texture
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeTexture(Texture2D* texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Deletes a texture from the cache given a its key name
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
|
|
|
void removeTextureForKey(const char *textureKeyName);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** Output to CCLOG the current contents of this TextureCache
|
2012-04-19 14:35:52 +08:00
|
|
|
* This will attempt to calculate the size of each texture, and the total texture memory in use
|
|
|
|
*
|
|
|
|
* @since v1.0
|
|
|
|
*/
|
|
|
|
void dumpCachedTextureInfo();
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns a Texture2D object given an PVR filename
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the file image was not previously loaded, it will create a new Texture2D
|
2012-09-17 15:02:24 +08:00
|
|
|
* object and it will return it. Otherwise it will return a reference of a previously loaded image
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* addPVRImage(const char* filename);
|
2013-05-27 14:42:22 +08:00
|
|
|
|
|
|
|
/** Returns a Texture2D object given an ETC filename
|
2013-06-20 14:13:12 +08:00
|
|
|
* If the file image was not previously loaded, it will create a new Texture2D
|
2013-05-27 14:42:22 +08:00
|
|
|
* object and it will return it. Otherwise it will return a reference of a previously loaded image
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* addETCImage(const char* filename);
|
2011-04-06 16:29:58 +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();
|
|
|
|
Image::EImageFormat computeImageFormatType(std::string& filename);
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-24 10:21:36 +08:00
|
|
|
public:
|
2013-06-21 15:49:45 +08:00
|
|
|
struct AsyncStruct
|
|
|
|
{
|
2013-06-21 15:29:21 +08:00
|
|
|
public:
|
|
|
|
AsyncStruct(const std::string& fn, Object *t, SEL_CallFuncO s) : filename(fn), target(t), selector(s) {}
|
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
Object *target;
|
|
|
|
SEL_CallFuncO selector;
|
|
|
|
};
|
|
|
|
|
2013-06-24 10:21:36 +08:00
|
|
|
protected:
|
2013-06-21 15:49:45 +08:00
|
|
|
typedef struct _ImageInfo
|
2013-06-21 15:29:21 +08:00
|
|
|
{
|
|
|
|
AsyncStruct *asyncStruct;
|
|
|
|
Image *image;
|
|
|
|
Image::EImageFormat imageType;
|
|
|
|
} ImageInfo;
|
2013-06-26 14:48:19 +08:00
|
|
|
|
|
|
|
std::thread* _loadingThread;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-21 15:43:17 +08:00
|
|
|
std::queue<AsyncStruct*>* _asyncStructQueue;
|
2013-06-21 15:29:21 +08:00
|
|
|
std::queue<ImageInfo*>* _imageInfoQueue;
|
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
std::mutex _asyncStructQueueMutex;
|
|
|
|
std::mutex _imageInfoMutex;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
std::mutex _sleepMutex;
|
|
|
|
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-06-21 15:49:45 +08:00
|
|
|
Dictionary* _textures;
|
2013-06-21 15:29:21 +08:00
|
|
|
|
2013-06-21 15:49:45 +08:00
|
|
|
static TextureCache *_sharedTextureCache;
|
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
|
|
|
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
VolatileTexture(Texture2D *t);
|
2012-04-19 14:35:52 +08:00
|
|
|
~VolatileTexture();
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static void addImageTexture(Texture2D *tt, const char* imageFileName, Image::EImageFormat format);
|
|
|
|
static void addStringTexture(Texture2D *tt, const char* text, const Size& dimensions, TextAlignment alignment,
|
|
|
|
VerticalTextAlignment vAlignment, const char *fontName, float fontSize);
|
|
|
|
static void addDataTexture(Texture2D *tt, void* data, Texture2DPixelFormat pixelFormat, const Size& contentSize);
|
|
|
|
static void addImage(Texture2D *tt, Image *image);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-04 08:59:22 +08:00
|
|
|
static void setTexParameters(Texture2D *t, const ccTexParams &texParams);
|
2013-06-20 14:13:12 +08:00
|
|
|
static void removeTexture(Texture2D *t);
|
2012-04-19 14:35:52 +08:00
|
|
|
static void reloadAllTextures();
|
|
|
|
|
|
|
|
public:
|
|
|
|
static std::list<VolatileTexture*> textures;
|
|
|
|
static bool isReloading;
|
2012-04-24 15:02:18 +08:00
|
|
|
|
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
// find VolatileTexture by Texture2D*
|
2012-04-24 15:02:18 +08:00
|
|
|
// if not found, create a new one
|
2013-06-20 14:13:12 +08:00
|
|
|
static VolatileTexture* findVolotileTexture(Texture2D *tt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D *texture;
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-06-20 14:13:12 +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-06-20 14:13:12 +08:00
|
|
|
Size _textureSize;
|
|
|
|
Texture2DPixelFormat _pixelFormat;
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _fileName;
|
2013-06-20 14:13:12 +08:00
|
|
|
Image::EImageFormat _fmtImage;
|
2011-04-06 16:29:58 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccTexParams _texParams;
|
2013-06-20 14:13:12 +08:00
|
|
|
Size _size;
|
|
|
|
TextAlignment _alignment;
|
|
|
|
VerticalTextAlignment _vAlignment;
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _fontName;
|
|
|
|
std::string _text;
|
|
|
|
float _fontSize;
|
2011-03-19 10:59:01 +08:00
|
|
|
};
|
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__
|
|
|
|
|