Skip to content

Commit

Permalink
Do image processing to reverse premultiplied alpha in separate thread (
Browse files Browse the repository at this point in the history
…#1929)

* Add missing parameter names

* Move processing intensive operation to a separate thread

* FileUtils instance may not be thread safe
  • Loading branch information
rh101 authored May 26, 2024
1 parent 49ba0dd commit 7414723
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
44 changes: 32 additions & 12 deletions core/2d/RenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,20 @@ bool RenderTexture::saveToFileAsNonPMA(std::string_view filename, bool isRGBA, S

if (basename.find(".png") != std::string::npos)
{
return saveToFileAsNonPMA(filename, Image::Format::PNG, isRGBA, callback);
return saveToFileAsNonPMA(filename, Image::Format::PNG, isRGBA, std::move(callback));
}
else if (basename.find(".jpg") != std::string::npos)
{
if (isRGBA)
AXLOG("RGBA is not supported for JPG format.");
return saveToFileAsNonPMA(filename, Image::Format::JPG, false, callback);
return saveToFileAsNonPMA(filename, Image::Format::JPG, false, std::move(callback));
}
else
{
AXLOG("Only PNG and JPG format are supported now!");
}

return saveToFileAsNonPMA(filename, Image::Format::JPG, false, callback);
return saveToFileAsNonPMA(filename, Image::Format::JPG, false, std::move(callback));
}

bool RenderTexture::saveToFile(std::string_view filename, bool isRGBA, SaveFileCallbackType callback)
Expand All @@ -405,20 +405,20 @@ bool RenderTexture::saveToFile(std::string_view filename, bool isRGBA, SaveFileC

if (basename.find(".png") != std::string::npos)
{
return saveToFile(filename, Image::Format::PNG, isRGBA, callback);
return saveToFile(filename, Image::Format::PNG, isRGBA, std::move(callback));
}
else if (basename.find(".jpg") != std::string::npos)
{
if (isRGBA)
AXLOG("RGBA is not supported for JPG format.");
return saveToFile(filename, Image::Format::JPG, false, callback);
return saveToFile(filename, Image::Format::JPG, false, std::move(callback));
}
else
{
AXLOG("Only PNG and JPG format are supported now!");
}

return saveToFile(filename, Image::Format::JPG, false, callback);
return saveToFile(filename, Image::Format::JPG, false, std::move(callback));
}

bool RenderTexture::saveToFileAsNonPMA(std::string_view fileName,
Expand All @@ -431,7 +431,7 @@ bool RenderTexture::saveToFileAsNonPMA(std::string_view fileName,
if (isRGBA && format == Image::Format::JPG)
AXLOG("RGBA is not supported for JPG format");

_saveFileCallback = callback;
_saveFileCallback = std::move(callback);

std::string fullpath = FileUtils::getInstance()->getWritablePath().append(fileName);

Expand All @@ -454,7 +454,7 @@ bool RenderTexture::saveToFile(std::string_view fileName,
if (isRGBA && format == Image::Format::JPG)
AXLOG("RGBA is not supported for JPG format");

_saveFileCallback = callback;
_saveFileCallback = std::move(callback);

std::string fullpath = FileUtils::getInstance()->getWritablePath().append(fileName);

Expand All @@ -474,13 +474,33 @@ void RenderTexture::onSaveToFile(std::string filename, bool isRGBA, bool forceNo
{
if (forceNonPMA && image->hasPremultipliedAlpha())
{
image->reversePremultipliedAlpha();
std::thread([this, image, _filename, isRGBA, forceNonPMA]() {
image->reversePremultipliedAlpha();

Director::getInstance()->getScheduler()->runOnAxmolThread([this, image, _filename, isRGBA] {
image->saveToFile(_filename, !isRGBA);
if (_saveFileCallback)
{
_saveFileCallback(this, _filename);
}
});
}).detach();
}
else
{
image->saveToFile(_filename, !isRGBA);
if (_saveFileCallback)
{
_saveFileCallback(this, _filename);
}
}
image->saveToFile(_filename, !isRGBA);
}
if (_saveFileCallback)
else
{
_saveFileCallback(this, _filename);
if (_saveFileCallback)
{
_saveFileCallback(this, _filename);
}
}
};
newImage(callbackFunc);
Expand Down
4 changes: 2 additions & 2 deletions core/2d/RenderTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class AX_DLL RenderTexture : public Node
* @param callback When the file is save finished,it will callback this function.
* @return Returns true if the operation is successful.
*/
bool saveToFileAsNonPMA(std::string_view filename, bool isRGBA = true, SaveFileCallbackType = nullptr);
bool saveToFileAsNonPMA(std::string_view filename, bool isRGBA = true, SaveFileCallbackType callback = nullptr);

/** Saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
* Returns true if the operation is successful.
Expand All @@ -186,7 +186,7 @@ class AX_DLL RenderTexture : public Node
* @param callback When the file is save finished,it will callback this function.
* @return Returns true if the operation is successful.
*/
bool saveToFile(std::string_view filename, bool isRGBA = true, SaveFileCallbackType = nullptr);
bool saveToFile(std::string_view filename, bool isRGBA = true, SaveFileCallbackType callback = nullptr);

/** saves the texture into a file in non-PMA. The format could be JPG or PNG. The file will be saved in the
Documents folder. Returns true if the operation is successful.
Expand Down

0 comments on commit 7414723

Please sign in to comment.