Skip to content

Use imagePool to reduce image memory usage in WebGL mode #3482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions CCBoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,33 @@ cc.path = /** @lends cc.path# */{
* @see cc.loader
*/

var imagePool = {
_pool: new Array(10),
_MAX: 10,
_smallImg: "data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=",

count: 0,
get: function () {
if (this.count > 0) {
this.count--;
var result = this._pool[this.count];
this._pool[this.count] = null;
return result;
}
else {
return new Image();
}
},
put: function (img) {
var pool = this._pool;
if (img instanceof HTMLImageElement && this.count < this._MAX) {
img.src = this._smallImg;
pool[this.count] = img;
this.count++;
}
}
};

/**
* Singleton instance of cc.Loader.
* @name cc.loader
Expand Down Expand Up @@ -867,7 +894,7 @@ cc.loader = (function () {
* @param {function} callback
* @returns {Image}
*/
loadImg: function (url, option, callback) {
loadImg: function (url, option, callback, img) {
var opt = {
isCrossOrigin: true
};
Expand All @@ -876,30 +903,22 @@ cc.loader = (function () {
else if (option !== undefined)
callback = option;

var img = this.getRes(url);
if (img) {
callback && callback(null, img);
return img;
}

var queue = _queue[url];
if (queue) {
queue.callbacks.push(callback);
return queue.img;
}

img = new Image();
img = img || imagePool.get();
if (opt.isCrossOrigin && location.origin !== "file://")
img.crossOrigin = "Anonymous";
else
img.crossOrigin = null;

var loadCallback = function () {
this.removeEventListener('load', loadCallback, false);
this.removeEventListener('error', errorCallback, false);

if (!_urlRegExp.test(url)) {
cc.loader.cache[url] = img;
}

var queue = _queue[url];
if (queue) {
var callbacks = queue.callbacks;
Expand All @@ -912,17 +931,21 @@ cc.loader = (function () {
queue.img = null;
delete _queue[url];
}

if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) {
imagePool.put(img);
}
};

var self = this;
var errorCallback = function () {
this.removeEventListener('load', loadCallback, false);
this.removeEventListener('error', errorCallback, false);

if (img.crossOrigin && img.crossOrigin.toLowerCase() === "anonymous") {
if (window.location.protocol !== 'https:' && img.crossOrigin && img.crossOrigin.toLowerCase() === "anonymous") {
opt.isCrossOrigin = false;
self.release(url);
cc.loader.loadImg(url, opt, callback);
cc.loader.loadImg(url, opt, callback, img);
} else {
var queue = _queue[url];
if (queue) {
Expand All @@ -936,6 +959,10 @@ cc.loader = (function () {
queue.img = null;
delete _queue[url];
}

if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) {
imagePool.put(img);
}
}
};

Expand Down
10 changes: 8 additions & 2 deletions cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
if (node._string.length === 0) {
locLabelCanvas.width = 1;
locLabelCanvas.height = locContentSize.height || 1;
node._texture && node._texture.handleLoadedTexture();
if (node._texture) {
node._texture._htmlElementObj = this._labelCanvas;
node._texture.handleLoadedTexture();
}
node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height));
return true;
}
Expand All @@ -432,7 +435,10 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
if (flag) locContext.clearRect(0, 0, width, height);
this._saveStatus();
this._drawTTFInCanvas(locContext);
node._texture && node._texture.handleLoadedTexture();
if (node._texture) {
node._texture._htmlElementObj = this._labelCanvas;
node._texture.handleLoadedTexture();
}
node.setTextureRect(cc.rect(0, 0, width, height));
return true;
};
Expand Down
10 changes: 7 additions & 3 deletions cocos2d/core/platform/CCLoaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ cc._imgLoader = {
callback = function (err, img) {
if (err)
return cb(err);
cc.loader.cache[url] = img;
cc.textureCache.handleLoadedTexture(url);
cb(null, img);

var tex = cc.textureCache.getTextureForKey(url) || new cc.Texture2D();
tex.url = url;
tex.initWithElement(img);
tex.handleLoadedTexture();
cc.textureCache.cacheImage(url, tex);
cb(null, tex);
};
}
cc.loader.loadImg(realUrl, callback);
Expand Down
4 changes: 1 addition & 3 deletions cocos2d/core/textures/CCTexture2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
var self = this;
if (self._textureLoaded) return;
if (!self._htmlElementObj) {
var img = cc.loader.getRes(self.url);
if (!img) return;
self.initWithElement(img);
return;
}

var locElement = self._htmlElementObj;
Expand Down
9 changes: 5 additions & 4 deletions cocos2d/core/textures/CCTextureCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,15 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {

var _p = cc.textureCache;

_p.handleLoadedTexture = function (url) {
_p.handleLoadedTexture = function (url, img) {
var locTexs = this._textures;
//remove judge
var tex = locTexs[url];
if (!tex) {
tex = locTexs[url] = new cc.Texture2D();
tex.url = url;
}
tex.initWithElement(img);
tex.handleLoadedTexture();
};

Expand Down Expand Up @@ -365,12 +366,12 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
if (err)
return cb && cb.call(target, err);

cc.textureCache.handleLoadedTexture(url, img);
var texResult = locTexs[url];
if (!cc.loader.cache[url]) {
cc.loader.cache[url] = img;
cc.loader.cache[url] = texResult;
}
cc.textureCache.handleLoadedTexture(url);

var texResult = locTexs[url];
cb && cb.call(target, texResult);
});

Expand Down
11 changes: 5 additions & 6 deletions cocos2d/core/textures/TexturesWebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,8 @@ cc._tmp.WebGLTexture2D = function () {
// Not sure about this ! Some texture need to be updated even after loaded
if (!cc.game._rendererInitialized)
return;
if (!self._htmlElementObj) {
var img = cc.loader.getRes(self.url);
if (!img) return;
self.initWithElement(img);
}
if (!self._htmlElementObj)
return;
if (!self._htmlElementObj.width || !self._htmlElementObj.height)
return;

Expand Down Expand Up @@ -498,6 +495,7 @@ cc._tmp.WebGLTexture2D = function () {

self._hasPremultipliedAlpha = premultiplied;
self._hasMipmaps = false;
self._htmlElementObj = null;

//dispatch load event to listener.
self.dispatchEvent("load");
Expand Down Expand Up @@ -854,7 +852,7 @@ cc._tmp.WebGLTextureAtlas = function () {
cc._tmp.WebGLTextureCache = function () {
var _p = cc.textureCache;

_p.handleLoadedTexture = function (url) {
_p.handleLoadedTexture = function (url, img) {
var locTexs = this._textures, tex, ext;
//remove judge(webgl)
if (!cc.game._rendererInitialized) {
Expand All @@ -865,6 +863,7 @@ cc._tmp.WebGLTextureCache = function () {
tex = locTexs[url] = new cc.Texture2D();
tex.url = url;
}
tex.initWithElement(img);
ext = cc.path.extname(url);
if (ext === ".png") {
tex.handleLoadedTexture(true);
Expand Down