From e9be28d94e462dd5ec476912f0234e296123717c Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 18 Jul 2018 17:12:32 +0800 Subject: [PATCH] Add CLEANUP_IMAGE_CACHE macro to clear image src after loading (#2993) * Add CLEANUP_IMAGE_CACHE macro to clear image src after loading * Fix API doc --- cocos2d/core/CCGame.js | 4 +++- cocos2d/core/assets/CCTexture2D.js | 13 ++++++------- cocos2d/core/platform/CCMacro.js | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index a829a52eb5..7c524e82fe 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -744,7 +744,9 @@ var game = { this._renderContext = renderer.device._gl; // Enable dynamic atlas manager by default - cc.dynamicAtlasManager.enabled = true; + if (!cc.macro.CLEANUP_IMAGE_CACHE) { + cc.dynamicAtlasManager.enabled = true; + } } if (!this._renderContext) { this.renderType = this.RENDER_TYPE_CANVAS; diff --git a/cocos2d/core/assets/CCTexture2D.js b/cocos2d/core/assets/CCTexture2D.js index 22a2149620..2963c3ae24 100644 --- a/cocos2d/core/assets/CCTexture2D.js +++ b/cocos2d/core/assets/CCTexture2D.js @@ -533,16 +533,15 @@ var Texture2D = cc.Class({ this._texture.update(opts); } - if (CC_WECHATGAME) { - // wechat game platform will cache image parsed data, - // so image will consume much more memory than web, releasing it - this._image.src = ""; - this._image = null; - } - //dispatch load event to listener. this.loaded = true; this.emit("load"); + + if (cc.macro.CLEANUP_IMAGE_CACHE) { + // wechat game platform will cache image parsed data, + // so image will consume much more memory than web, releasing it + this._image.src = ""; + } }, /** diff --git a/cocos2d/core/platform/CCMacro.js b/cocos2d/core/platform/CCMacro.js index 90fcbe2623..b5c32dd794 100644 --- a/cocos2d/core/platform/CCMacro.js +++ b/cocos2d/core/platform/CCMacro.js @@ -325,6 +325,24 @@ cc.macro = { * @default false */ ENABLE_CULLING: false, + + /** + * !#en + * Whether or not clear dom Image object cache after uploading to gl texture. + * Concretely, we are setting image.src to empty string to release the cache. + * Normally you don't need to enable this option, because on web the Image object doesn't consume too much memory. + * But on Wechat Game platform, the current version cache decoded data in Image object, which has high memory usage. + * So we enabled this option by default on Wechat, so that we can release Image cache immediately after uploaded to GPU. + * !#zh + * 是否在将贴图上传至 GPU 之后删除 DOM Image 缓存。 + * 具体来说,我们通过设置 image.src 为空字符串来释放这部分内存。 + * 正常情况下,你不需要开启这个选项,因为在 web 平台,Image 对象所占用的内存很小。 + * 但是在微信小游戏平台的当前版本,Image 对象会缓存解码后的图片数据,它所占用的内存空间很大。 + * 所以我们在微信平台默认开启了这个选项,这样我们就可以在上传 GL 贴图之后立即释放 Image 对象的内存,避免过高的内存占用。 + * @property {Boolean} CLEANUP_IMAGE_CACHE + * @default false + */ + CLEANUP_IMAGE_CACHE: false, }; js.getset(cc.macro, 'ENABLE_3D', function () {