Skip to content

Commit f5cca84

Browse files
authored
Merge pull request #3502 from pandamicro/programstate
Improve Spine rendering and fix action manager leak
2 parents 15704c3 + c44b97c commit f5cca84

File tree

11 files changed

+603
-601
lines changed

11 files changed

+603
-601
lines changed

cocos2d/core/CCActionManager.js

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,13 @@
3030
* @example
3131
* var element = new cc.HashElement();
3232
*/
33-
cc.HashElement = cc.Class.extend(/** @lends cc.HashElement# */{
34-
actions:null,
35-
target:null, //ccobject
36-
actionIndex:0,
37-
currentAction:null, //CCAction
38-
currentActionSalvaged:false,
39-
paused:false,
40-
/**
41-
* Constructor
42-
*/
43-
ctor:function () {
44-
this.actions = [];
45-
this.target = null;
46-
this.actionIndex = 0;
47-
this.currentAction = null; //CCAction
48-
this.currentActionSalvaged = false;
49-
this.paused = false;
50-
}
51-
});
33+
cc.HashElement = function () {
34+
this.actions = [];
35+
this.target = null;
36+
this.actionIndex = 0;
37+
this.currentAction = null; //CCAction
38+
this.paused = false;
39+
};
5240

5341
/**
5442
* cc.ActionManager is a class that can manage actions.<br/>
@@ -64,10 +52,6 @@ cc.HashElement = cc.Class.extend(/** @lends cc.HashElement# */{
6452
* var mng = new cc.ActionManager();
6553
*/
6654
cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
67-
_hashTargets:null,
68-
_arrayTargets:null,
69-
_currentTarget:null,
70-
_currentTargetSalvaged:false,
7155
_elementPool: [],
7256

7357
_searchElementByTarget:function (arr, target) {
@@ -82,7 +66,6 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
8266
this._hashTargets = {};
8367
this._arrayTargets = [];
8468
this._currentTarget = null;
85-
this._currentTargetSalvaged = false;
8669
},
8770

8871
_getElement: function (target, paused) {
@@ -99,8 +82,8 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
9982
element.actions.length = 0;
10083
element.actionIndex = 0;
10184
element.currentAction = null;
102-
element.currentActionSalvaged = false;
10385
element.paused = false;
86+
element.target = null;
10487
this._elementPool.push(element);
10588
},
10689

@@ -156,15 +139,8 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
156139
return;
157140
var element = this._hashTargets[target.__instanceId];
158141
if (element) {
159-
if (element.actions.indexOf(element.currentAction) !== -1 && !(element.currentActionSalvaged))
160-
element.currentActionSalvaged = true;
161-
162142
element.actions.length = 0;
163-
if (this._currentTarget === element && !forceDelete) {
164-
this._currentTargetSalvaged = true;
165-
} else {
166-
this._deleteHashElement(element);
167-
}
143+
this._deleteHashElement(element);
168144
}
169145
},
170146
/** Removes an action given an action reference.
@@ -181,6 +157,9 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
181157
for (var i = 0; i < element.actions.length; i++) {
182158
if (element.actions[i] === action) {
183159
element.actions.splice(i, 1);
160+
// update actionIndex in case we are in tick. looping over the actions
161+
if (element.actionIndex >= i)
162+
element.actionIndex--;
184163
break;
185164
}
186165
}
@@ -291,10 +270,10 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
291270
* @param {Array} targetsToResume
292271
*/
293272
resumeTargets:function(targetsToResume){
294-
if(!targetsToResume)
273+
if (!targetsToResume)
295274
return;
296275

297-
for(var i = 0 ; i< targetsToResume.length; i++){
276+
for (var i = 0; i< targetsToResume.length; i++) {
298277
if(targetsToResume[i])
299278
this.resumeTarget(targetsToResume[i]);
300279
}
@@ -311,21 +290,14 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
311290
_removeActionAtIndex:function (index, element) {
312291
var action = element.actions[index];
313292

314-
if ((action === element.currentAction) && (!element.currentActionSalvaged))
315-
element.currentActionSalvaged = true;
316-
317293
element.actions.splice(index, 1);
318294

319295
// update actionIndex in case we are in tick. looping over the actions
320296
if (element.actionIndex >= index)
321297
element.actionIndex--;
322298

323299
if (element.actions.length === 0) {
324-
if (this._currentTarget === element) {
325-
this._currentTargetSalvaged = true;
326-
} else {
327-
this._deleteHashElement(element);
328-
}
300+
this._deleteHashElement(element);
329301
}
330302
},
331303

@@ -356,25 +328,17 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
356328
for (var elt = 0; elt < locTargets.length; elt++) {
357329
this._currentTarget = locTargets[elt];
358330
locCurrTarget = this._currentTarget;
359-
//this._currentTargetSalvaged = false;
360-
if (!locCurrTarget.paused) {
331+
if (!locCurrTarget.paused && locCurrTarget.actions) {
361332
// The 'actions' CCMutableArray may change while inside this loop.
362-
for (locCurrTarget.actionIndex = 0;
363-
locCurrTarget.actionIndex < (locCurrTarget.actions ? locCurrTarget.actions.length : 0);
364-
locCurrTarget.actionIndex++) {
333+
for (locCurrTarget.actionIndex = 0; locCurrTarget.actionIndex < locCurrTarget.actions.length; locCurrTarget.actionIndex++) {
365334
locCurrTarget.currentAction = locCurrTarget.actions[locCurrTarget.actionIndex];
366335
if (!locCurrTarget.currentAction)
367336
continue;
368337

369-
locCurrTarget.currentActionSalvaged = false;
370338
//use for speed
371339
locCurrTarget.currentAction.step(dt * ( locCurrTarget.currentAction._speedMethod ? locCurrTarget.currentAction._speed : 1 ) );
372-
if (locCurrTarget.currentActionSalvaged) {
373-
// The currentAction told the node to remove it. To prevent the action from
374-
// accidentally deallocating itself before finishing its step, we retained
375-
// it. Now that step is done, it's safe to release it.
376-
locCurrTarget.currentAction = null;//release
377-
} else if (locCurrTarget.currentAction.isDone()) {
340+
341+
if (locCurrTarget.currentAction.isDone()) {
378342
locCurrTarget.currentAction.stop();
379343
var action = locCurrTarget.currentAction;
380344
// Make currentAction nil to prevent removeAction from salvaging it.
@@ -385,12 +349,8 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
385349
locCurrTarget.currentAction = null;
386350
}
387351
}
388-
389-
// elt, at this moment, is still valid
390-
// so it is safe to ask this here (issue #490)
391-
392352
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
393-
if (this._currentTargetSalvaged && locCurrTarget.actions.length === 0) {
353+
if (locCurrTarget.actions.length === 0) {
394354
this._deleteHashElement(locCurrTarget) && elt--;
395355
}
396356
}

cocos2d/core/platform/CCMacro.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ cc.ORIENTATION_AUTO = 3;
499499
*/
500500
cc.CONCURRENCY_HTTP_REQUEST_COUNT = cc.sys.isMobile ? 20 : 0;
501501

502+
/**
503+
* The maximum vertex count for a single batched draw call.
504+
* @constant
505+
* @type Number
506+
*/
507+
cc.BATCH_VERTEX_COUNT = 2000;
508+
502509

503510
// ------------------- vertex attrib flags -----------------------------
504511
/**

cocos2d/core/renderer/RendererWebGL.js

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var _batchedInfo = {
3333
blendSrc: null,
3434
// The batched blend destination, all batching element should have the same blend destination
3535
blendDst: null,
36-
// The batched glProgramState, all batching element should have the same glProgramState
36+
// The batched gl program state, all batching element should have the same state
3737
glProgramState: null
3838
},
3939

@@ -129,7 +129,7 @@ return {
129129
_cacheToBufferCmds: {}, // an array saves the renderer commands need for cache to other canvas
130130
_cacheInstanceIds: [],
131131
_currentID: 0,
132-
_clearColor: cc.color(), //background color,default BLACK
132+
_clearColor: cc.color(0, 0, 0, 255), //background color,default BLACK
133133

134134
init: function () {
135135
var gl = cc._renderContext;
@@ -138,7 +138,7 @@ return {
138138

139139
this.mat4Identity = new cc.math.Matrix4();
140140
this.mat4Identity.identity();
141-
initQuadBuffer(2000);
141+
initQuadBuffer(cc.BATCH_VERTEX_COUNT);
142142
if (cc.sys.os === cc.sys.OS_IOS) {
143143
_IS_IOS = true;
144144
}
@@ -313,18 +313,23 @@ return {
313313
_batchingSize += increment;
314314
},
315315

316-
_updateBatchedInfo: function (texture, blendFunc, shaderProgram) {
317-
if (texture) {
316+
_updateBatchedInfo: function (texture, blendFunc, glProgramState) {
317+
if (texture !== _batchedInfo.texture ||
318+
blendFunc.src !== _batchedInfo.blendSrc ||
319+
blendFunc.dst !== _batchedInfo.blendDst ||
320+
glProgramState !== _batchedInfo.glProgramState) {
321+
// Draw batched elements
322+
this._batchRendering();
323+
// Update _batchedInfo
318324
_batchedInfo.texture = texture;
319-
}
320-
321-
if (blendFunc) {
322325
_batchedInfo.blendSrc = blendFunc.src;
323326
_batchedInfo.blendDst = blendFunc.dst;
324-
}
327+
_batchedInfo.glProgramState = glProgramState;
325328

326-
if (shaderProgram) {
327-
_batchedInfo.shader = shaderProgram;
329+
return true;
330+
}
331+
else {
332+
return false;
328333
}
329334
},
330335

@@ -339,7 +344,7 @@ return {
339344

340345
// Check batching
341346
var node = cmd._node;
342-
var texture = node._texture || (node._spriteFrame ? node._spriteFrame._texture : null);
347+
var texture = node._texture || (node._spriteFrame && node._spriteFrame._texture);
343348
var blendSrc = node._blendFunc.src;
344349
var blendDst = node._blendFunc.dst;
345350
var glProgramState = cmd._glProgramState;
@@ -361,38 +366,7 @@ return {
361366
// Upload vertex data
362367
var len = cmd.uploadData(_vertexDataF32, _vertexDataUI32, _batchingSize * _sizePerVertex);
363368
if (len > 0) {
364-
var i, curr, type = cmd.vertexType || VertexType.QUAD;
365-
switch (type) {
366-
case VertexType.QUAD:
367-
for (i = 0; i < len; i += 4) {
368-
curr = _batchingSize + i;
369-
_indexData[_indexSize++] = curr + 0;
370-
_indexData[_indexSize++] = curr + 1;
371-
_indexData[_indexSize++] = curr + 2;
372-
_indexData[_indexSize++] = curr + 1;
373-
_indexData[_indexSize++] = curr + 2;
374-
_indexData[_indexSize++] = curr + 3;
375-
}
376-
break;
377-
case VertexType.TRIANGLE:
378-
_pureQuad = false;
379-
for (i = 0; i < len; i += 3) {
380-
curr = _batchingSize + i;
381-
_indexData[_indexSize++] = curr + 0;
382-
_indexData[_indexSize++] = curr + 1;
383-
_indexData[_indexSize++] = curr + 2;
384-
}
385-
break;
386-
case VertexType.CUSTOM:
387-
_pureQuad = false;
388-
if (cmd.uploadIndexData) {
389-
_indexSize += cmd.uploadIndexData(_indexData, _indexSize, _batchingSize);
390-
}
391-
break;
392-
default:
393-
return;
394-
}
395-
_batchingSize += len;
369+
this._increaseBatchingSize(len, cmd.vertexType);
396370
}
397371
},
398372

cocos2d/core/textures/CCTexture2D.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
190190
*/
191191
handleLoadedTexture: function () {
192192
var self = this;
193-
if (self._textureLoaded) return;
194193
if (!self._htmlElementObj) {
195194
return;
196195
}

0 commit comments

Comments
 (0)