From fdd46f27189742e2f24dbd079efb66da83eca4c8 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 26 Oct 2017 12:44:06 +0800 Subject: [PATCH] Auto binds RenderTexture::saveToFile and fix js memory leak while saveToFile. The callback _saveFileCallback has to be set to nullptr to release se::Value the std::function captures. --- native/cocos/2d/CCRenderTexture.cpp | 5 + native/cocos/2d/CCRenderTexture.h | 1 + .../auto/api/jsb_cocos2dx_auto_api.js | 18 ++ .../js-bindings/auto/jsb_cocos2dx_auto.cpp | 168 ++++++++++++++++++ .../js-bindings/auto/jsb_cocos2dx_auto.hpp | 1 + native/tools/tojs/cocos2dx.ini | 2 +- 6 files changed, 194 insertions(+), 1 deletion(-) diff --git a/native/cocos/2d/CCRenderTexture.cpp b/native/cocos/2d/CCRenderTexture.cpp index 212da76fd5..6a17cf838e 100644 --- a/native/cocos/2d/CCRenderTexture.cpp +++ b/native/cocos/2d/CCRenderTexture.cpp @@ -744,6 +744,11 @@ void RenderTexture::draw(Renderer *renderer, const Mat4 &transform, uint32_t fla } } +void RenderTexture::cleanup() +{ + _saveFileCallback = nullptr; +} + void RenderTexture::begin() { Director* director = Director::getInstance(); diff --git a/native/cocos/2d/CCRenderTexture.h b/native/cocos/2d/CCRenderTexture.h index 9a3776e079..b507e0e7c0 100644 --- a/native/cocos/2d/CCRenderTexture.h +++ b/native/cocos/2d/CCRenderTexture.h @@ -262,6 +262,7 @@ public: // Overrides virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override; virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; + virtual void cleanup() override; /** Flag: Use stack matrix computed from scene hierarchy or generate new modelView and projection matrix. * diff --git a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js index e31b2805e0..3cc9e290b0 100644 --- a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js +++ b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js @@ -13741,6 +13741,24 @@ begin : function ( { }, +/** + * @method saveToFile +* @param {String|String} str +* @param {cc.Image::Format|bool} format +* @param {bool|function} bool +* @param {function} func +* @return {bool|bool} +*/ +saveToFile : function( +str, +format, +bool, +func +) +{ + return false; +}, + /** * @method setAutoDraw * @param {bool} arg0 diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp index 52b742df67..794e9931eb 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp @@ -31224,6 +31224,173 @@ static bool js_cocos2dx_RenderTexture_begin(se::State& s) } SE_BIND_FUNC(js_cocos2dx_RenderTexture_begin) +static bool js_cocos2dx_RenderTexture_saveToFile(se::State& s) +{ + CC_UNUSED bool ok = true; + cocos2d::RenderTexture* cobj = (cocos2d::RenderTexture*)s.nativeThisObject(); + SE_PRECONDITION2( cobj, false, "js_cocos2dx_RenderTexture_saveToFile : Invalid Native Object"); + const auto& args = s.args(); + size_t argc = args.size(); + do { + if (argc == 2) { + std::string arg0; + ok &= seval_to_std_string(args[0], &arg0); + if (!ok) { ok = true; break; } + cocos2d::Image::Format arg1; + ok &= seval_to_int32(args[1], (int32_t*)&arg1); + if (!ok) { ok = true; break; } + bool result = cobj->saveToFile(arg0, arg1); + ok &= boolean_to_seval(result, &s.rval()); + SE_PRECONDITION2(ok, false, "js_cocos2dx_RenderTexture_saveToFile : Error processing arguments"); + return true; + } + } while(false); + + do { + if (argc == 3) { + std::string arg0; + ok &= seval_to_std_string(args[0], &arg0); + if (!ok) { ok = true; break; } + cocos2d::Image::Format arg1; + ok &= seval_to_int32(args[1], (int32_t*)&arg1); + if (!ok) { ok = true; break; } + bool arg2; + ok &= seval_to_boolean(args[2], &arg2); + bool result = cobj->saveToFile(arg0, arg1, arg2); + ok &= boolean_to_seval(result, &s.rval()); + SE_PRECONDITION2(ok, false, "js_cocos2dx_RenderTexture_saveToFile : Error processing arguments"); + return true; + } + } while(false); + + do { + if (argc == 4) { + std::string arg0; + ok &= seval_to_std_string(args[0], &arg0); + if (!ok) { ok = true; break; } + cocos2d::Image::Format arg1; + ok &= seval_to_int32(args[1], (int32_t*)&arg1); + if (!ok) { ok = true; break; } + bool arg2; + ok &= seval_to_boolean(args[2], &arg2); + std::function &)> arg3; + do { + if (args[3].isObject() && args[3].toObject()->isFunction()) + { + se::Value jsThis(s.thisObject()); + se::Value jsFunc(args[3]); + jsThis.toObject()->attachObject(jsFunc.toObject()); + auto lambda = [=](cocos2d::RenderTexture* larg0, const std::basic_string & larg1) -> void { + se::ScriptEngine::getInstance()->clearException(); + se::AutoHandleScope hs; + + CC_UNUSED bool ok = true; + se::ValueArray args; + args.resize(2); + ok &= native_ptr_to_seval((cocos2d::RenderTexture*)larg0, &args[0]); + ok &= std_string_to_seval(larg1, &args[1]); + se::Value rval; + se::Object* thisObj = jsThis.isObject() ? jsThis.toObject() : nullptr; + se::Object* funcObj = jsFunc.toObject(); + bool succeed = funcObj->call(args, thisObj, &rval); + if (!succeed) { + se::ScriptEngine::getInstance()->clearException(); + } + }; + arg3 = lambda; + } + else + { + arg3 = nullptr; + } + } while(false) + ; + if (!ok) { ok = true; break; } + bool result = cobj->saveToFile(arg0, arg1, arg2, arg3); + ok &= boolean_to_seval(result, &s.rval()); + SE_PRECONDITION2(ok, false, "js_cocos2dx_RenderTexture_saveToFile : Error processing arguments"); + return true; + } + } while(false); + + do { + if (argc == 1) { + std::string arg0; + ok &= seval_to_std_string(args[0], &arg0); + if (!ok) { ok = true; break; } + bool result = cobj->saveToFile(arg0); + ok &= boolean_to_seval(result, &s.rval()); + SE_PRECONDITION2(ok, false, "js_cocos2dx_RenderTexture_saveToFile : Error processing arguments"); + return true; + } + } while(false); + + do { + if (argc == 2) { + std::string arg0; + ok &= seval_to_std_string(args[0], &arg0); + if (!ok) { ok = true; break; } + bool arg1; + ok &= seval_to_boolean(args[1], &arg1); + bool result = cobj->saveToFile(arg0, arg1); + ok &= boolean_to_seval(result, &s.rval()); + SE_PRECONDITION2(ok, false, "js_cocos2dx_RenderTexture_saveToFile : Error processing arguments"); + return true; + } + } while(false); + + do { + if (argc == 3) { + std::string arg0; + ok &= seval_to_std_string(args[0], &arg0); + if (!ok) { ok = true; break; } + bool arg1; + ok &= seval_to_boolean(args[1], &arg1); + std::function &)> arg2; + do { + if (args[2].isObject() && args[2].toObject()->isFunction()) + { + se::Value jsThis(s.thisObject()); + se::Value jsFunc(args[2]); + jsThis.toObject()->attachObject(jsFunc.toObject()); + auto lambda = [=](cocos2d::RenderTexture* larg0, const std::basic_string & larg1) -> void { + se::ScriptEngine::getInstance()->clearException(); + se::AutoHandleScope hs; + + CC_UNUSED bool ok = true; + se::ValueArray args; + args.resize(2); + ok &= native_ptr_to_seval((cocos2d::RenderTexture*)larg0, &args[0]); + ok &= std_string_to_seval(larg1, &args[1]); + se::Value rval; + se::Object* thisObj = jsThis.isObject() ? jsThis.toObject() : nullptr; + se::Object* funcObj = jsFunc.toObject(); + bool succeed = funcObj->call(args, thisObj, &rval); + if (!succeed) { + se::ScriptEngine::getInstance()->clearException(); + } + }; + arg2 = lambda; + } + else + { + arg2 = nullptr; + } + } while(false) + ; + if (!ok) { ok = true; break; } + bool result = cobj->saveToFile(arg0, arg1, arg2); + ok &= boolean_to_seval(result, &s.rval()); + SE_PRECONDITION2(ok, false, "js_cocos2dx_RenderTexture_saveToFile : Error processing arguments"); + return true; + } + } while(false); + + SE_REPORT_ERROR("wrong number of arguments: %d", (int)argc); + return false; +} +SE_BIND_FUNC(js_cocos2dx_RenderTexture_saveToFile) + static bool js_cocos2dx_RenderTexture_setAutoDraw(se::State& s) { cocos2d::RenderTexture* cobj = (cocos2d::RenderTexture*)s.nativeThisObject(); @@ -31629,6 +31796,7 @@ bool js_register_cocos2dx_RenderTexture(se::Object* obj) cls->defineFunction("setKeepMatrix", _SE(js_cocos2dx_RenderTexture_setKeepMatrix)); cls->defineFunction("setClearFlags", _SE(js_cocos2dx_RenderTexture_setClearFlags)); cls->defineFunction("begin", _SE(js_cocos2dx_RenderTexture_begin)); + cls->defineFunction("saveToFile", _SE(js_cocos2dx_RenderTexture_saveToFile)); cls->defineFunction("setAutoDraw", _SE(js_cocos2dx_RenderTexture_setAutoDraw)); cls->defineFunction("setClearColor", _SE(js_cocos2dx_RenderTexture_setClearColor)); cls->defineFunction("beginWithClear", _SE(js_cocos2dx_RenderTexture_beginWithClear)); diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.hpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.hpp index 96342eb271..75b5375f76 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.hpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.hpp @@ -2077,6 +2077,7 @@ SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_isAutoDraw); SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_setKeepMatrix); SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_setClearFlags); SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_begin); +SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_saveToFile); SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_setAutoDraw); SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_setClearColor); SE_DECLARE_FUNC(js_cocos2dx_RenderTexture_beginWithClear); diff --git a/native/tools/tojs/cocos2dx.ini b/native/tools/tojs/cocos2dx.ini index 42fa72d3f2..cf52a6e843 100644 --- a/native/tools/tojs/cocos2dx.ini +++ b/native/tools/tojs/cocos2dx.ini @@ -111,7 +111,7 @@ skip = Node::[update ^setPosition$ setGLServerState description getUserObject .* Application::[^application.* ^run$ getCurrentLanguageCode setAnimationInterval], ccFontDefinition::[*], NewTextureAtlas::[*], - RenderTexture::[listenToBackground listenToForeground saveToFile], + RenderTexture::[listenToBackground listenToForeground], TextFieldTTF::[(g|s)etDelegate], EventListenerVector::[*], EventListener(Touch|Keyboard|Mouse|Acceleration|Focus|Custom).*::[create],