Skip to content

Commit ada74f8

Browse files
committed
fixed cocos2d#2280 Fixed video memory bug for WebGL removing texture
1 parent 30ce5db commit ada74f8

File tree

10 files changed

+128
-60
lines changed

10 files changed

+128
-60
lines changed

cocos2d/CCDrawingPrimitives.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
653653
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
654654

655655
glContext.drawArrays(glContext.POINTS, 0, 1);
656+
glContext.deleteBuffer(pointBuffer);
656657

657658
cc.INCREMENT_GL_DRAWS(1);
658659
},
@@ -681,6 +682,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
681682
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
682683

683684
glContext.drawArrays(glContext.POINTS, 0, points.length);
685+
glContext.deleteBuffer(pointBuffer);
684686

685687
cc.INCREMENT_GL_DRAWS(1);
686688
},
@@ -714,6 +716,8 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
714716
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
715717

716718
glContext.drawArrays(glContext.LINES, 0, 2);
719+
glContext.deleteBuffer(pointBuffer);
720+
717721
cc.INCREMENT_GL_DRAWS(1);
718722
},
719723

@@ -770,6 +774,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
770774
glContext.drawArrays(glContext.LINE_LOOP, 0, vertices.length);
771775
else
772776
glContext.drawArrays(glContext.LINE_STRIP, 0, vertices.length);
777+
glContext.deleteBuffer(pointBuffer);
773778

774779
cc.INCREMENT_GL_DRAWS(1);
775780
},
@@ -796,6 +801,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
796801
glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(poli), glContext.STATIC_DRAW);
797802
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
798803
glContext.drawArrays(glContext.TRIANGLE_FAN, 0, poli.length);
804+
glContext.deleteBuffer(pointBuffer);
799805

800806
cc.INCREMENT_GL_DRAWS(1);
801807
},
@@ -844,6 +850,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
844850
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
845851

846852
glContext.drawArrays(glContext.LINE_STRIP, 0, segments + additionalSegment);
853+
glContext.deleteBuffer(pointBuffer);
847854

848855
cc.INCREMENT_GL_DRAWS(1);
849856
},
@@ -881,6 +888,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
881888
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
882889

883890
glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1);
891+
glContext.deleteBuffer(pointBuffer);
884892

885893
cc.INCREMENT_GL_DRAWS(1);
886894
},
@@ -918,6 +926,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
918926
glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW);
919927
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
920928
glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1);
929+
glContext.deleteBuffer(pointBuffer);
921930

922931
cc.INCREMENT_GL_DRAWS(1);
923932
},
@@ -977,6 +986,7 @@ cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
977986
glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW);
978987
glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
979988
glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1);
989+
glContext.deleteBuffer(pointBuffer);
980990

981991
cc.INCREMENT_GL_DRAWS(1);
982992
},

cocos2d/draw_nodes/CCDrawNode.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,11 @@ cc.DrawNodeWebGL = cc.Node.extend(/** @lends cc.DrawNodeWebGL# */{
448448
this._dirty = true;
449449
},
450450

451+
onExit:function(){
452+
cc.Node.prototype.onExit.call(this);
453+
cc.renderContext.deleteBuffer(this._trianglesWebBuffer);
454+
},
455+
451456
/**
452457
* Clear the geometry in the node's buffer.
453458
*/

cocos2d/label_nodes/CCLabelTTF.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,16 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
830830
cc.drawingUtil.drawPoly(verticesG2, 4, true);
831831
} // CC_SPRITE_DEBUG_DRAW
832832
cc.g_NumberOfDraws++;
833+
},
834+
835+
onExit:function(){
836+
this.releaseTexture();
837+
cc.Sprite.prototype.onExit.call(this);
838+
},
839+
840+
releaseTexture:function(){
841+
if(this._texture)
842+
this._texture.releaseTexture();
833843
}
834844
});
835845

cocos2d/layers_scenes_transitions_nodes/CCLayer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,12 @@ cc.LayerColorWebGL = cc.Layer.extend(/** @lends cc.LayerColorCanvas# */{
984984
var glContext = cc.renderContext;
985985
glContext.bindBuffer(glContext.ARRAY_BUFFER, this._colorsUint8Buffer);
986986
glContext.bufferData(glContext.ARRAY_BUFFER, this._squareColorsAB, glContext.STATIC_DRAW);
987+
},
988+
989+
onExit:function(){
990+
cc.Layer.prototype.onExit.call(this);
991+
cc.renderContext.deleteBuffer(this._verticesFloat32Buffer);
992+
cc.renderContext.deleteBuffer(this._colorsUint8Buffer);
987993
}
988994
});
989995

cocos2d/particle_nodes/CCParticleBatchNode.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
483483
*/
484484
setTextureAtlas:function (textureAtlas) {
485485
this._textureAtlas = textureAtlas;
486+
},
487+
488+
onExit:function(){
489+
cc.Node.prototype.onExit.call(this);
490+
if(this._textureAtlas)
491+
this._textureAtlas._releaseBuffer();
486492
}
487493
});
488494

cocos2d/particle_nodes/CCParticleSystemQuad.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,16 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
769769
return false;
770770
}
771771
return true;
772+
},
773+
774+
onExit: function () {
775+
cc.Node.prototype.onExit.call(this);
776+
if (this._buffersVBO) {
777+
if (this._buffersVBO[0])
778+
cc.renderContext.deleteBuffer(this._buffersVBO[0]);
779+
if (this._buffersVBO[1])
780+
cc.renderContext.deleteBuffer(this._buffersVBO[1]);
781+
}
772782
}
773783
});
774784

cocos2d/sprite_nodes/CCSprite.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,10 @@ cc.SpriteWebGL = cc.Node.extend(/** @lends cc.SpriteWebGL# */{
26862686
cc.p(offsetPixG2.x + drawSizeG2.width, offsetPixG2.y + drawSizeG2.height), cc.p(offsetPixG2.x, offsetPixG2.y + drawSizeG2.height)];
26872687
cc.drawingUtil.drawPoly(verticesG2, 4, true);
26882688
} // CC_SPRITE_DEBUG_DRAW
2689+
},
2690+
onExit:function(){
2691+
cc.Node.prototype.onExit.call(this);
2692+
cc.renderContext.deleteBuffer(this._quadWebBuffer);
26892693
}
26902694
});
26912695

cocos2d/sprite_nodes/CCSpriteBatchNode.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,12 @@ cc.SpriteBatchNodeWebGL = cc.Node.extend(/** @lends cc.SpriteBatchNodeWebGL# */{
14241424
cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
14251425

14261426
this._textureAtlas.drawQuads();
1427+
},
1428+
1429+
onExit:function(){
1430+
cc.Node.prototype.onExit.call(this);
1431+
if(this._textureAtlas)
1432+
this._textureAtlas._releaseBuffer();
14271433
}
14281434
});
14291435

@@ -1435,7 +1441,7 @@ cc.SpriteBatchNodeWebGL = cc.Node.extend(/** @lends cc.SpriteBatchNodeWebGL# */{
14351441
* </p>
14361442
* @param {String} fileImage
14371443
* @param {Number} capacity
1438-
* @return {cc.SpriteBatchNodeCanvas}
1444+
* @return {cc.SpriteBatchNodeWebGL}
14391445
* @example
14401446
* //create a SpriteBatchNode
14411447
* var parent2 = cc.SpriteBatchNode.create("res/animations/grossini.png", 50);
@@ -1454,7 +1460,7 @@ cc.SpriteBatchNodeWebGL.create = function (fileImage, capacity) {
14541460
* </p>
14551461
* @param {cc.Texture2D} texture
14561462
* @param {Number} capacity
1457-
* @return {cc.SpriteBatchNodeCanvas}
1463+
* @return {cc.SpriteBatchNodeWebGL}
14581464
*/
14591465
cc.SpriteBatchNodeWebGL.createWithTexture = function (texture, capacity) {
14601466
capacity = capacity || cc.DEFAULT_SPRITE_BATCH_CAPACITY;

cocos2d/textures/CCTextureAtlas.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,18 @@ cc.TextureAtlas = cc.Class.extend(/** @lends cc.TextureAtlas# */{
572572
*/
573573
drawQuads:function () {
574574
this.drawNumberOfQuads(this._totalQuads, 0);
575+
},
576+
577+
_releaseBuffer: function () {
578+
var gl = cc.renderContext;
579+
if (this._buffersVBO) {
580+
if (this._buffersVBO[0])
581+
gl.deleteBuffer(this._buffersVBO[0]);
582+
if (this._buffersVBO[1])
583+
gl.deleteBuffer(this._buffersVBO[1])
584+
}
585+
if (this._quadsWebBuffer)
586+
gl.deleteBuffer(this._quadsWebBuffer);
575587
}
576588
});
577589

cocos2d/textures/CCTextureCache.js

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,34 @@ cc.TextureCacheCanvas = cc.Class.extend(/** @lends cc.TextureCacheCanvas# */{
163163
return this._textureColorsCache[key];
164164
},
165165

166+
/**
167+
* <p>Returns a Texture2D object given an PVR filename<br />
168+
* If the file image was not previously loaded, it will create a new Texture2D<br />
169+
* object and it will return it. Otherwise it will return a reference of a previously loaded image </p>
170+
* @param {String} path
171+
* @return {cc.Texture2D}
172+
*/
173+
addPVRImage:function (path) {
174+
cc.Assert(path != null, "TextureCache: file image MUST not be null");
175+
176+
path = cc.FileUtils.getInstance().fullPathForFilename(path);
177+
178+
var key = path;
179+
180+
if (this._textures[key] != null) {
181+
return this._textures[key];
182+
}
183+
184+
// Split up directory and filename
185+
var tex = new cc.Texture2D();
186+
if (tex.initWithPVRFile(key)) {
187+
this._textures[key] = tex;
188+
} else {
189+
cc.log("cocos2d: Couldn't add PVRImage:" + key + " in TextureCache");
190+
}
191+
return tex;
192+
},
193+
/// ---- common properties end ----
166194
/**
167195
* <p>Purges the dictionary of loaded textures. <br />
168196
* Call this method if you receive the "Memory Warning" <br />
@@ -210,35 +238,6 @@ cc.TextureCacheCanvas = cc.Class.extend(/** @lends cc.TextureCacheCanvas# */{
210238
delete(this._textures[fullPath]);
211239
},
212240

213-
/**
214-
* <p>Returns a Texture2D object given an PVR filename<br />
215-
* If the file image was not previously loaded, it will create a new Texture2D<br />
216-
* object and it will return it. Otherwise it will return a reference of a previously loaded image </p>
217-
* @param {String} path
218-
* @return {cc.Texture2D}
219-
*/
220-
addPVRImage:function (path) {
221-
cc.Assert(path != null, "TextureCache: file image MUST not be null");
222-
223-
path = cc.FileUtils.getInstance().fullPathForFilename(path);
224-
225-
var key = path;
226-
227-
if (this._textures[key] != null) {
228-
return this._textures[key];
229-
}
230-
231-
// Split up directory and filename
232-
var tex = new cc.Texture2D();
233-
if (tex.initWithPVRFile(key)) {
234-
this._textures[key] = tex;
235-
} else {
236-
cc.log("cocos2d: Couldn't add PVRImage:" + key + " in TextureCache");
237-
}
238-
return tex;
239-
},
240-
/// ---- common properties end ----
241-
242241
/**
243242
* Loading the images asynchronously
244243
* @param {String} path
@@ -522,6 +521,35 @@ cc.TextureCacheWebGL = cc.Class.extend({
522521
return this._textureColorsCache[key];
523522
},
524523

524+
/**
525+
* <p>Returns a Texture2D object given an PVR filename<br />
526+
* If the file image was not previously loaded, it will create a new Texture2D<br />
527+
* object and it will return it. Otherwise it will return a reference of a previously loaded image </p>
528+
* @param {String} path
529+
* @return {cc.Texture2D}
530+
*/
531+
addPVRImage:function (path) {
532+
cc.Assert(path != null, "TextureCache: file image MUST not be null");
533+
534+
path = cc.FileUtils.getInstance().fullPathForFilename(path);
535+
536+
var key = path;
537+
538+
if (this._textures[key] != null) {
539+
return this._textures[key];
540+
}
541+
542+
// Split up directory and filename
543+
var tex = new cc.Texture2D();
544+
if (tex.initWithPVRFile(key)) {
545+
this._textures[key] = tex;
546+
} else {
547+
cc.log("cocos2d: Couldn't add PVRImage:" + key + " in TextureCache");
548+
}
549+
return tex;
550+
},
551+
/// ---- common properties end ----
552+
525553
/**
526554
* <p>Purges the dictionary of loaded textures. <br />
527555
* Call this method if you receive the "Memory Warning" <br />
@@ -576,35 +604,6 @@ cc.TextureCacheWebGL = cc.Class.extend({
576604
}
577605
},
578606

579-
/**
580-
* <p>Returns a Texture2D object given an PVR filename<br />
581-
* If the file image was not previously loaded, it will create a new Texture2D<br />
582-
* object and it will return it. Otherwise it will return a reference of a previously loaded image </p>
583-
* @param {String} path
584-
* @return {cc.Texture2D}
585-
*/
586-
addPVRImage:function (path) {
587-
cc.Assert(path != null, "TextureCache: file image MUST not be null");
588-
589-
path = cc.FileUtils.getInstance().fullPathForFilename(path);
590-
591-
var key = path;
592-
593-
if (this._textures[key] != null) {
594-
return this._textures[key];
595-
}
596-
597-
// Split up directory and filename
598-
var tex = new cc.Texture2D();
599-
if (tex.initWithPVRFile(key)) {
600-
this._textures[key] = tex;
601-
} else {
602-
cc.log("cocos2d: Couldn't add PVRImage:" + key + " in TextureCache");
603-
}
604-
return tex;
605-
},
606-
/// ---- common properties end ----
607-
608607
/**
609608
* Loading the images asynchronously
610609
* @param {String} path

0 commit comments

Comments
 (0)