Merge pull request #852 from dumganhar/jsb2.0

Image refactor
This commit is contained in:
James Chen
2017-09-25 21:51:42 -05:00
committed by GitHub
8 changed files with 50 additions and 47 deletions
+1 -1
View File
@@ -169,7 +169,7 @@ AutoPolygon::AutoPolygon(const std::string &filename)
AutoPolygon::~AutoPolygon()
{
CC_SAFE_DELETE(_image);
CC_SAFE_RELEASE(_image);
}
std::vector<Vec2> AutoPolygon::trace(const Rect& rect, const float& threshold)
+3 -3
View File
@@ -85,7 +85,7 @@ RenderTexture::~RenderTexture()
{
glDeleteRenderbuffers(1, &_stencilRenderBufffer);
}
CC_SAFE_DELETE(_UITextureImage);
CC_SAFE_RELEASE_NULL(_UITextureImage);
}
void RenderTexture::listenToBackground(EventCustom *event)
@@ -93,7 +93,7 @@ void RenderTexture::listenToBackground(EventCustom *event)
// We have not found a way to dispatch the enter background message before the texture data are destroyed.
// So we disable this pair of message handler at present.
#if CC_ENABLE_CACHE_TEXTURE_DATA
CC_SAFE_DELETE(_UITextureImage);
CC_SAFE_RELEASE(_UITextureImage);
// to get the rendered texture data
_UITextureImage = newImage(false);
@@ -510,7 +510,7 @@ void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA)
{
_saveFileCallback(this, filename);
}
CC_SAFE_DELETE(image);
CC_SAFE_RELEASE_NULL(image);
}
/* get buffer as Image */
+1 -1
View File
@@ -293,7 +293,7 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu
// add sprite frame
_spriteFrames.insert(spriteFrameName, spriteFrame);
}
CC_SAFE_DELETE(image);
CC_SAFE_RELEASE(image);
}
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dict, const std::string &texturePath)
+1 -1
View File
@@ -140,7 +140,7 @@ void onCaptureScreen(const std::function<void(bool, const std::string&)>& afterC
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, mainThread, nullptr, [image, outputFile]()
{
succeedSaveToFile = image->saveToFile(outputFile);
delete image;
image->release();
});
}
else
-15
View File
@@ -374,21 +374,6 @@ bool Image::initWithImageFile(const std::string& path)
return ret;
}
bool Image::initWithImageFileThreadSafe(const std::string& fullpath)
{
bool ret = false;
_filePath = fullpath;
Data data = FileUtils::getInstance()->getDataFromFile(fullpath);
if (!data.isNull())
{
ret = initWithImageData(data.getBytes(), data.getSize());
}
return ret;
}
bool Image::initWithImageData(const unsigned char * data, ssize_t dataLen)
{
bool ret = false;
+10 -15
View File
@@ -58,16 +58,10 @@ typedef struct _MipmapInfo
class CC_DLL Image : public Ref
{
public:
friend class TextureCache;
/**
* @js ctor
*/
Image();
/**
* @js NA
* @lua NA
*/
virtual ~Image();
/** Supported formats for Image */
enum class Format
@@ -197,17 +191,18 @@ protected:
protected:
// noncopyable
Image(const Image& rImg);
Image& operator=(const Image&);
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
/*
@brief The same result as with initWithImageFile, but thread safe. It is caused by
loadImage() in TextureCache.cpp.
@param fullpath full path of the file.
@param imageType the type of image, currently only supporting two types.
@return true if loaded correctly.
// nonmoveable
Image(Image&&) = delete;
Image& operator=(Image&&) = delete;
/**
* @js NA
* @lua NA
*/
bool initWithImageFileThreadSafe(const std::string& fullpath);
virtual ~Image();
Format detectFormat(const unsigned char * data, ssize_t dataLen);
bool isPng(const unsigned char * data, ssize_t dataLen);
+15 -5
View File
@@ -186,11 +186,7 @@ public:
* @js ctor
*/
Texture2D();
/**
* @js NA
* @lua NA
*/
virtual ~Texture2D();
/**
Get texture name, dimensions and coordinates message by a string.
* @js NA
@@ -394,6 +390,20 @@ public:
static const PixelFormatInfoMap& getPixelFormatInfoMap();
private:
// noncopyable
Texture2D(const Texture2D&) = delete;
Texture2D& operator=(const Texture2D&) = delete;
// nonmoveable
Texture2D(Texture2D&&) = delete;
Texture2D& operator=(Texture2D&&) = delete;
/**
* @js NA
* @lua NA
*/
virtual ~Texture2D();
/**
* A struct for storing 9-patch image capInsets.
*/
+19 -6
View File
@@ -83,12 +83,25 @@ std::string TextureCache::getDescription() const
struct TextureCache::AsyncStruct
{
public:
AsyncStruct(const std::string& fn, std::function<void(Texture2D*)> f) : filename(fn), callback(f), pixelFormat(Texture2D::getDefaultAlphaPixelFormat()), loadSuccess(false) {}
AsyncStruct(const std::string& fn, std::function<void(Texture2D*)> f)
: filename(fn)
, callback(f)
, image(new (std::nothrow) Image())
, imageAlpha(new (std::nothrow) Image())
, pixelFormat(Texture2D::getDefaultAlphaPixelFormat())
, loadSuccess(false)
{}
~AsyncStruct()
{
CC_SAFE_RELEASE(image);
CC_SAFE_RELEASE(imageAlpha);
}
std::string filename;
std::function<void(Texture2D*)> callback;
Image image;
Image imageAlpha;
Image* image;
Image* imageAlpha;
Texture2D::PixelFormat pixelFormat;
bool loadSuccess;
};
@@ -223,7 +236,7 @@ void TextureCache::loadImage()
}
// load image
asyncStruct->loadSuccess = asyncStruct->image.initWithImageFileThreadSafe(asyncStruct->filename);
asyncStruct->loadSuccess = asyncStruct->image->initWithImageFile(asyncStruct->filename);
// push the asyncStruct to response queue
_responseMutex.lock();
@@ -270,7 +283,7 @@ void TextureCache::addImageAsyncCallBack(float dt)
// convert image to texture
if (asyncStruct->loadSuccess)
{
Image* image = &(asyncStruct->image);
Image* image = asyncStruct->image;
// generate texture in render thread
texture = new (std::nothrow) Texture2D();
@@ -638,7 +651,7 @@ void TextureCache::renameTextureWithKey(const std::string& srcName, const std::s
_textures.insert(std::make_pair(fullpath, tex));
_textures.erase(it);
}
CC_SAFE_DELETE(image);
CC_SAFE_RELEASE(image);
}
}
}