/**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2011 Zynga Inc. 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. ****************************************************************************/ #include "CCTextureCache.h" #include "CCTexture2D.h" #include "ccMacros.h" #include "CCDirector.h" #include "platform/platform.h" #include "platform/CCFileUtils.h" #include "platform/CCThread.h" #include "platform/CCImage.h" #include "support/ccUtils.h" #include "CCScheduler.h" #include "cocoa/CCString.h" #include #include #include #include #include #include #include using namespace std; NS_CC_BEGIN typedef struct _AsyncStruct { std::string filename; CCObject *target; SEL_CallFuncO selector; } AsyncStruct; typedef struct _ImageInfo { AsyncStruct *asyncStruct; CCImage *image; CCImage::EImageFormat imageType; } ImageInfo; static pthread_t s_loadingThread; static pthread_mutex_t s_SleepMutex; static pthread_cond_t s_SleepCondition; static pthread_mutex_t s_asyncStructQueueMutex; static pthread_mutex_t s_ImageInfoMutex; static unsigned long s_nAsyncRefCount = 0; static bool need_quit = false; static std::queue* s_pAsyncStructQueue = NULL; static std::queue* s_pImageQueue = NULL; static CCImage::EImageFormat computeImageFormatType(string& filename) { CCImage::EImageFormat ret = CCImage::kFmtUnKnown; if ((std::string::npos != filename.find(".jpg")) || (std::string::npos != filename.find(".jpeg"))) { ret = CCImage::kFmtJpg; } else if ((std::string::npos != filename.find(".png")) || (std::string::npos != filename.find(".PNG"))) { ret = CCImage::kFmtPng; } else if ((std::string::npos != filename.find(".tiff")) || (std::string::npos != filename.find(".TIFF"))) { ret = CCImage::kFmtTiff; } else if ((std::string::npos != filename.find(".webp")) || (std::string::npos != filename.find(".WEBP"))) { ret = CCImage::kFmtWebp; } return ret; } static void* loadImage(void* data) { AsyncStruct *pAsyncStruct = NULL; while (true) { // create autorelease pool for iOS CCThread thread; thread.createAutoreleasePool(); std::queue *pQueue = s_pAsyncStructQueue; pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue if (pQueue->empty()) { pthread_mutex_unlock(&s_asyncStructQueueMutex); if (need_quit) { break; } else { pthread_cond_wait(&s_SleepCondition, &s_SleepMutex); continue; } } else { pAsyncStruct = pQueue->front(); pQueue->pop(); pthread_mutex_unlock(&s_asyncStructQueueMutex); } const char *filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; continue; } // generate image CCImage *pImage = new CCImage(); if (pImage && !pImage->initWithImageFileThreadSafe(filename, imageType)) { CC_SAFE_RELEASE(pImage); CCLOG("can not load %s", filename); continue; } // generate image info ImageInfo *pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->image = pImage; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); } if( s_pAsyncStructQueue != NULL ) { delete s_pAsyncStructQueue; s_pAsyncStructQueue = NULL; delete s_pImageQueue; s_pImageQueue = NULL; pthread_mutex_destroy(&s_asyncStructQueueMutex); pthread_mutex_destroy(&s_ImageInfoMutex); pthread_mutex_destroy(&s_SleepMutex); pthread_cond_destroy(&s_SleepCondition); } return 0; } // implementation CCTextureCache // TextureCache - Alloc, Init & Dealloc static CCTextureCache *g_sharedTextureCache = NULL; CCTextureCache * CCTextureCache::sharedTextureCache() { if (!g_sharedTextureCache) { g_sharedTextureCache = new CCTextureCache(); } return g_sharedTextureCache; } CCTextureCache::CCTextureCache() { CCAssert(g_sharedTextureCache == NULL, "Attempted to allocate a second instance of a singleton."); m_pTextures = new CCDictionary(); } CCTextureCache::~CCTextureCache() { CCLOGINFO("cocos2d: deallocing CCTextureCache."); need_quit = true; pthread_cond_signal(&s_SleepCondition); CC_SAFE_RELEASE(m_pTextures); } void CCTextureCache::purgeSharedTextureCache() { CC_SAFE_RELEASE_NULL(g_sharedTextureCache); } const char* CCTextureCache::description() { return CCString::createWithFormat("", m_pTextures->count())->getCString(); } CCDictionary* CCTextureCache::snapshotTextures() { CCDictionary* pRet = new CCDictionary(); CCDictElement* pElement = NULL; CCDICT_FOREACH(m_pTextures, pElement) { pRet->setObject(pElement->getObject(), pElement->getStrKey()); } return pRet; } void CCTextureCache::addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector) { CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL"); CCTexture2D *texture = NULL; // optimization std::string pathKey = path; pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str()); texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str()); std::string fullpath = pathKey; if (texture != NULL) { if (target && selector) { (target->*selector)(texture); } return; } // lazy init if (s_pAsyncStructQueue == NULL) { s_pAsyncStructQueue = new queue(); s_pImageQueue = new queue(); pthread_mutex_init(&s_asyncStructQueueMutex, NULL); pthread_mutex_init(&s_ImageInfoMutex, NULL); pthread_mutex_init(&s_SleepMutex, NULL); pthread_cond_init(&s_SleepCondition, NULL); pthread_create(&s_loadingThread, NULL, loadImage, NULL); need_quit = false; } if (0 == s_nAsyncRefCount) { CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this, 0, false); } ++s_nAsyncRefCount; if (target) { target->retain(); } // generate async struct AsyncStruct *data = new AsyncStruct(); data->filename = fullpath.c_str(); data->target = target; data->selector = selector; // add async struct into queue pthread_mutex_lock(&s_asyncStructQueueMutex); s_pAsyncStructQueue->push(data); pthread_mutex_unlock(&s_asyncStructQueueMutex); pthread_cond_signal(&s_SleepCondition); } void CCTextureCache::addImageAsyncCallBack(float dt) { // the image is generated in loading thread std::queue *imagesQueue = s_pImageQueue; pthread_mutex_lock(&s_ImageInfoMutex); if (imagesQueue->empty()) { pthread_mutex_unlock(&s_ImageInfoMutex); } else { ImageInfo *pImageInfo = imagesQueue->front(); imagesQueue->pop(); pthread_mutex_unlock(&s_ImageInfoMutex); AsyncStruct *pAsyncStruct = pImageInfo->asyncStruct; CCImage *pImage = pImageInfo->image; CCObject *target = pAsyncStruct->target; SEL_CallFuncO selector = pAsyncStruct->selector; const char* filename = pAsyncStruct->filename.c_str(); // generate texture in render thread CCTexture2D *texture = new CCTexture2D(); #if 0 //TODO: (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) texture->initWithImage(pImage, kCCResolutioniPhone); #else texture->initWithImage(pImage); #endif #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture, filename, pImageInfo->imageType); #endif // cache the texture m_pTextures->setObject(texture, filename); texture->autorelease(); if (target && selector) { (target->*selector)(texture); target->release(); } pImage->release(); delete pAsyncStruct; delete pImageInfo; --s_nAsyncRefCount; if (0 == s_nAsyncRefCount) { CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this); } } } CCTexture2D * CCTextureCache::addImage(const char * path) { CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL"); CCTexture2D * texture = NULL; CCImage* pImage = NULL; // Split up directory and filename // MUTEX: // Needed since addImageAsync calls this method from a different thread //pthread_mutex_lock(m_pDictLock); std::string pathKey = path; pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str()); if (pathKey.size() == 0) { return NULL; } texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str()); std::string fullpath = pathKey; // (CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(path)); if (! texture) { std::string lowerCase(pathKey); for (unsigned int i = 0; i < lowerCase.length(); ++i) { lowerCase[i] = tolower(lowerCase[i]); } // all images are handled by UIImage except PVR extension that is handled by our own handler do { if (std::string::npos != lowerCase.find(".pvr")) { texture = this->addPVRImage(fullpath.c_str()); } else { CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown; if (std::string::npos != lowerCase.find(".png")) { eImageFormat = CCImage::kFmtPng; } else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg")) { eImageFormat = CCImage::kFmtJpg; } else if (std::string::npos != lowerCase.find(".tif") || std::string::npos != lowerCase.find(".tiff")) { eImageFormat = CCImage::kFmtTiff; } else if (std::string::npos != lowerCase.find(".webp")) { eImageFormat = CCImage::kFmtWebp; } pImage = new CCImage(); CC_BREAK_IF(NULL == pImage); unsigned long nSize = 0; unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullpath.c_str(), "rb", &nSize); bool bRet = pImage->initWithImageData((void*)pBuffer, nSize, eImageFormat); CC_SAFE_DELETE_ARRAY(pBuffer); CC_BREAK_IF(!bRet); texture = new CCTexture2D(); if( texture && texture->initWithImage(pImage) ) { #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture, fullpath.c_str(), eImageFormat); #endif m_pTextures->setObject(texture, pathKey.c_str()); texture->release(); } else { CCLOG("cocos2d: Couldn't create texture for file:%s in CCTextureCache", path); } } } while (0); } CC_SAFE_RELEASE(pImage); //pthread_mutex_unlock(m_pDictLock); return texture; } CCTexture2D * CCTextureCache::addPVRImage(const char* path) { CCAssert(path != NULL, "TextureCache: fileimage MUST not be nil"); CCTexture2D* texture = NULL; std::string key(path); if( (texture = (CCTexture2D*)m_pTextures->objectForKey(key.c_str())) ) { return texture; } // Split up directory and filename std::string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(key.c_str()); texture = new CCTexture2D(); if(texture != NULL && texture->initWithPVRFile(fullpath.c_str()) ) { #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtRawData); #endif m_pTextures->setObject(texture, key.c_str()); texture->autorelease(); } else { CCLOG("cocos2d: Couldn't add PVRImage:%s in CCTextureCache",key.c_str()); CC_SAFE_DELETE(texture); } return texture; } CCTexture2D* CCTextureCache::addUIImage(CCImage *image, const char *key) { CCAssert(image != NULL, "TextureCache: image MUST not be nil"); CCTexture2D * texture = NULL; // textureForKey() use full path,so the key should be full path std::string forKey; if (key) { forKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(key); } // Don't have to lock here, because addImageAsync() will not // invoke opengl function in loading thread. do { // If key is nil, then create a new texture each time if(key && (texture = (CCTexture2D *)m_pTextures->objectForKey(forKey.c_str()))) { break; } // prevents overloading the autorelease pool texture = new CCTexture2D(); texture->initWithImage(image); if(key && texture) { m_pTextures->setObject(texture, forKey.c_str()); texture->autorelease(); } else { CCLOG("cocos2d: Couldn't add UIImage in CCTextureCache"); } } while (0); #if CC_ENABLE_CACHE_TEXTURE_DATA VolatileTexture::addCCImage(texture, image); #endif return texture; } // TextureCache - Remove void CCTextureCache::removeAllTextures() { m_pTextures->removeAllObjects(); } void CCTextureCache::removeUnusedTextures() { /* CCDictElement* pElement = NULL; CCDICT_FOREACH(m_pTextures, pElement) { CCLOG("cocos2d: CCTextureCache: texture: %s", pElement->getStrKey()); CCTexture2D *value = (CCTexture2D*)pElement->getObject(); if (value->retainCount() == 1) { CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", pElement->getStrKey()); m_pTextures->removeObjectForElememt(pElement); } } */ /** Inter engineer zhuoshi sun finds that this way will get better performance */ if (m_pTextures->count()) { // find elements to be removed CCDictElement* pElement = NULL; list elementToRemove; CCDICT_FOREACH(m_pTextures, pElement) { CCLOG("cocos2d: CCTextureCache: texture: %s", pElement->getStrKey()); CCTexture2D *value = (CCTexture2D*)pElement->getObject(); if (value->retainCount() == 1) { elementToRemove.push_back(pElement); } } // remove elements for (list::iterator iter = elementToRemove.begin(); iter != elementToRemove.end(); ++iter) { CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", (*iter)->getStrKey()); m_pTextures->removeObjectForElememt(*iter); } } } void CCTextureCache::removeTexture(CCTexture2D* texture) { if( ! texture ) { return; } CCArray* keys = m_pTextures->allKeysForObject(texture); m_pTextures->removeObjectsForKeys(keys); } void CCTextureCache::removeTextureForKey(const char *textureKeyName) { if (textureKeyName == NULL) { return; } string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(textureKeyName); m_pTextures->removeObjectForKey(fullPath); } CCTexture2D* CCTextureCache::textureForKey(const char* key) { return (CCTexture2D*)m_pTextures->objectForKey(CCFileUtils::sharedFileUtils()->fullPathForFilename(key)); } void CCTextureCache::reloadAllTextures() { #if CC_ENABLE_CACHE_TEXTURE_DATA VolatileTexture::reloadAllTextures(); #endif } void CCTextureCache::dumpCachedTextureInfo() { unsigned int count = 0; unsigned int totalBytes = 0; CCDictElement* pElement = NULL; CCDICT_FOREACH(m_pTextures, pElement) { CCTexture2D* tex = (CCTexture2D*)pElement->getObject(); unsigned int bpp = tex->bitsPerPixelForFormat(); // Each texture takes up width * height * bytesPerPixel bytes. unsigned int bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8; totalBytes += bytes; count++; CCLOG("cocos2d: \"%s\" rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB", pElement->getStrKey(), (long)tex->retainCount(), (long)tex->getName(), (long)tex->getPixelsWide(), (long)tex->getPixelsHigh(), (long)bpp, (long)bytes / 1024); } CCLOG("cocos2d: CCTextureCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f)); } #if CC_ENABLE_CACHE_TEXTURE_DATA std::list VolatileTexture::textures; bool VolatileTexture::isReloading = false; VolatileTexture::VolatileTexture(CCTexture2D *t) : texture(t) , m_eCashedImageType(kInvalid) , m_pTextureData(NULL) , m_PixelFormat(kTexture2DPixelFormat_RGBA8888) , m_strFileName("") , m_FmtImage(CCImage::kFmtPng) , m_alignment(kCCTextAlignmentCenter) , m_vAlignment(kCCVerticalTextAlignmentCenter) , m_strFontName("") , m_strText("") , uiImage(NULL) , m_fFontSize(0.0f) { m_size = CCSizeMake(0, 0); m_texParams.minFilter = GL_LINEAR; m_texParams.magFilter = GL_LINEAR; m_texParams.wrapS = GL_CLAMP_TO_EDGE; m_texParams.wrapT = GL_CLAMP_TO_EDGE; textures.push_back(this); } VolatileTexture::~VolatileTexture() { textures.remove(this); CC_SAFE_RELEASE(uiImage); } void VolatileTexture::addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format) { if (isReloading) { return; } VolatileTexture *vt = findVolotileTexture(tt); vt->m_eCashedImageType = kImageFile; vt->m_strFileName = imageFileName; vt->m_FmtImage = format; vt->m_PixelFormat = tt->getPixelFormat(); } void VolatileTexture::addCCImage(CCTexture2D *tt, CCImage *image) { VolatileTexture *vt = findVolotileTexture(tt); image->retain(); vt->uiImage = image; vt->m_eCashedImageType = kImage; } VolatileTexture* VolatileTexture::findVolotileTexture(CCTexture2D *tt) { VolatileTexture *vt = 0; std::list::iterator i = textures.begin(); while (i != textures.end()) { VolatileTexture *v = *i++; if (v->texture == tt) { vt = v; break; } } if (! vt) { vt = new VolatileTexture(tt); } return vt; } void VolatileTexture::addDataTexture(CCTexture2D *tt, void* data, CCTexture2DPixelFormat pixelFormat, const CCSize& contentSize) { if (isReloading) { return; } VolatileTexture *vt = findVolotileTexture(tt); vt->m_eCashedImageType = kImageData; vt->m_pTextureData = data; vt->m_PixelFormat = pixelFormat; vt->m_TextureSize = contentSize; } void VolatileTexture::addStringTexture(CCTexture2D *tt, const char* text, const CCSize& dimensions, CCTextAlignment alignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize) { if (isReloading) { return; } VolatileTexture *vt = findVolotileTexture(tt); vt->m_eCashedImageType = kString; vt->m_size = dimensions; vt->m_strFontName = fontName; vt->m_alignment = alignment; vt->m_vAlignment = vAlignment; vt->m_fFontSize = fontSize; vt->m_strText = text; } void VolatileTexture::setTexParameters(CCTexture2D *t, ccTexParams *texParams) { VolatileTexture *vt = findVolotileTexture(t); if (texParams->minFilter != GL_NONE) vt->m_texParams.minFilter = texParams->minFilter; if (texParams->magFilter != GL_NONE) vt->m_texParams.magFilter = texParams->magFilter; if (texParams->wrapS != GL_NONE) vt->m_texParams.wrapS = texParams->wrapS; if (texParams->wrapT != GL_NONE) vt->m_texParams.wrapT = texParams->wrapT; } void VolatileTexture::removeTexture(CCTexture2D *t) { std::list::iterator i = textures.begin(); while (i != textures.end()) { VolatileTexture *vt = *i++; if (vt->texture == t) { delete vt; break; } } } void VolatileTexture::reloadAllTextures() { isReloading = true; CCLOG("reload all texture"); std::list::iterator iter = textures.begin(); while (iter != textures.end()) { VolatileTexture *vt = *iter++; switch (vt->m_eCashedImageType) { case kImageFile: { std::string lowerCase(vt->m_strFileName.c_str()); for (unsigned int i = 0; i < lowerCase.length(); ++i) { lowerCase[i] = tolower(lowerCase[i]); } if (std::string::npos != lowerCase.find(".pvr")) { CCTexture2DPixelFormat oldPixelFormat = CCTexture2D::defaultAlphaPixelFormat(); CCTexture2D::setDefaultAlphaPixelFormat(vt->m_PixelFormat); vt->texture->initWithPVRFile(vt->m_strFileName.c_str()); CCTexture2D::setDefaultAlphaPixelFormat(oldPixelFormat); } else { CCImage* pImage = new CCImage(); unsigned long nSize = 0; unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(vt->m_strFileName.c_str(), "rb", &nSize); if (pImage && pImage->initWithImageData((void*)pBuffer, nSize, vt->m_FmtImage)) { CCTexture2DPixelFormat oldPixelFormat = CCTexture2D::defaultAlphaPixelFormat(); CCTexture2D::setDefaultAlphaPixelFormat(vt->m_PixelFormat); vt->texture->initWithImage(pImage); CCTexture2D::setDefaultAlphaPixelFormat(oldPixelFormat); } CC_SAFE_DELETE_ARRAY(pBuffer); CC_SAFE_RELEASE(pImage); } } break; case kImageData: { vt->texture->initWithData(vt->m_pTextureData, vt->m_PixelFormat, vt->m_TextureSize.width, vt->m_TextureSize.height, vt->m_TextureSize); } break; case kString: { vt->texture->initWithString(vt->m_strText.c_str(), vt->m_strFontName.c_str(), vt->m_fFontSize, vt->m_size, vt->m_alignment, vt->m_vAlignment ); } break; case kImage: { vt->texture->initWithImage(vt->uiImage); } break; default: break; } vt->texture->setTexParameters(&vt->m_texParams); } isReloading = false; } #endif // CC_ENABLE_CACHE_TEXTURE_DATA NS_CC_END