Skip to content

Commit

Permalink
Do not require passing WebGL context in order to destroy buffers/VAOs
Browse files Browse the repository at this point in the history
This mirrors Unique{Buffer,VAO} in native: a reference to the context is necessary to create, but the resulting object knows how to destroy itself without additional arguments.

It also reduces the number of places where Source objects need a painter or map reference.
  • Loading branch information
jfirebaugh committed Oct 12, 2016
1 parent c7a9543 commit 31523a8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 33 deletions.
4 changes: 2 additions & 2 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ Bucket.prototype.createArrays = function() {
}
};

Bucket.prototype.destroy = function(gl) {
Bucket.prototype.destroy = function() {
for (var programName in this.bufferGroups) {
var programBufferGroups = this.bufferGroups[programName];
for (var i = 0; i < programBufferGroups.length; i++) {
programBufferGroups[i].destroy(gl);
programBufferGroups[i].destroy();
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions js/data/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Buffer.prototype.bind = function(gl) {
var type = gl[this.type];

if (!this.buffer) {
this.gl = gl;
this.buffer = gl.createBuffer();
gl.bindBuffer(type, this.buffer);
gl.bufferData(type, this.arrayBuffer, gl.STATIC_DRAW);
Expand Down Expand Up @@ -82,9 +83,9 @@ Buffer.prototype.setVertexAttribPointers = function(gl, program) {
* @private
* @param gl The WebGL context
*/
Buffer.prototype.destroy = function(gl) {
Buffer.prototype.destroy = function() {
if (this.buffer) {
gl.deleteBuffer(this.buffer);
this.gl.deleteBuffer(this.buffer);
}
};

Expand Down
14 changes: 7 additions & 7 deletions js/data/buffer_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ function BufferGroup(arrayGroup, arrayTypes) {
});
}

BufferGroup.prototype.destroy = function(gl) {
this.layoutVertexBuffer.destroy(gl);
BufferGroup.prototype.destroy = function() {
this.layoutVertexBuffer.destroy();
if (this.elementBuffer) {
this.elementBuffer.destroy(gl);
this.elementBuffer.destroy();
}
if (this.elementBuffer2) {
this.elementBuffer2.destroy(gl);
this.elementBuffer2.destroy();
}
for (var n in this.paintVertexBuffers) {
this.paintVertexBuffers[n].destroy(gl);
this.paintVertexBuffers[n].destroy();
}
for (var j in this.vaos) {
this.vaos[j].destroy(gl);
this.vaos[j].destroy();
}
for (var k in this.secondVaos) {
this.secondVaos[k].destroy(gl);
this.secondVaos[k].destroy();
}
};
10 changes: 5 additions & 5 deletions js/render/vertex_array_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ VertexArrayObject.prototype.bind = function(gl, program, layoutVertexBuffer, ele

if (!gl.extVertexArrayObject || isFreshBindRequired) {
this.freshBind(gl, program, layoutVertexBuffer, elementBuffer, vertexBuffer2);
this.gl = gl;
} else {
gl.extVertexArrayObject.bindVertexArrayOES(this.vao);
}
Expand All @@ -38,7 +39,7 @@ VertexArrayObject.prototype.freshBind = function(gl, program, layoutVertexBuffer
var numNextAttributes = program.numAttributes;

if (gl.extVertexArrayObject) {
if (this.vao) this.destroy(gl);
if (this.vao) this.destroy();
this.vao = gl.extVertexArrayObject.createVertexArrayOES();
gl.extVertexArrayObject.bindVertexArrayOES(this.vao);
numPrevAttributes = 0;
Expand Down Expand Up @@ -80,10 +81,9 @@ VertexArrayObject.prototype.freshBind = function(gl, program, layoutVertexBuffer
gl.currentNumAttributes = numNextAttributes;
};

VertexArrayObject.prototype.destroy = function(gl) {
var ext = gl.extVertexArrayObject;
if (ext && this.vao) {
ext.deleteVertexArrayOES(this.vao);
VertexArrayObject.prototype.destroy = function() {
if (this.vao) {
this.gl.extVertexArrayObject.deleteVertexArrayOES(this.vao);
this.vao = null;
}
};
4 changes: 2 additions & 2 deletions js/source/geojson_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototy

tile.workerID = this.dispatcher.send('load tile', params, function(err, data) {

tile.unloadVectorData(this.map.painter);
tile.unloadVectorData();

if (tile.aborted)
return;
Expand All @@ -197,7 +197,7 @@ GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototy
},

unloadTile: function(tile) {
tile.unloadVectorData(this.map.painter);
tile.unloadVectorData();
this.dispatcher.send('remove tile', { uid: tile.uid, source: this.id }, function() {}, tile.workerID);
},

Expand Down
22 changes: 8 additions & 14 deletions js/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,22 @@ Tile.prototype = {
},

/**
* given a data object and a GL painter, destroy and re-create
* all of its buffers.
* Replace this tile's symbol buckets with fresh data.
* @param {Object} data
* @param {Object} painter
* @param {Style} style
* @returns {undefined}
* @private
*/
reloadSymbolData: function(data, painter, style) {
reloadSymbolData: function(data, style) {
if (this.state === 'unloaded') return;

this.collisionTile = new CollisionTile(data.collisionTile, this.collisionBoxArray);
this.featureIndex.setCollisionTile(this.collisionTile);

// Destroy and delete existing symbol buckets
for (var id in this.buckets) {
var bucket = this.buckets[id];
if (bucket.type === 'symbol') {
bucket.destroy(painter.gl);
bucket.destroy();
delete this.buckets[id];
}
}
Expand All @@ -104,17 +102,13 @@ Tile.prototype = {
},

/**
* Make sure that this tile doesn't own any data within a given
* painter, so that it doesn't consume any memory or maintain
* any references to the painter.
* @param {Object} painter gl painter object
* Release any data or WebGL resources referenced by this tile.
* @returns {undefined}
* @private
*/
unloadVectorData: function(painter) {
unloadVectorData: function() {
for (var id in this.buckets) {
var bucket = this.buckets[id];
bucket.destroy(painter.gl);
this.buckets[id].destroy();
}

this.collisionBoxArray = null;
Expand Down Expand Up @@ -143,7 +137,7 @@ Tile.prototype = {
}, done.bind(this), this.workerID);

function done(_, data) {
this.reloadSymbolData(data, source.map.painter, source.map.style);
this.reloadSymbolData(data, source.map.style);
source.fire('data', {tile: this, dataType: 'tile'});

// HACK this is nescessary to fix https://github.com/mapbox/mapbox-gl-js/issues/2986
Expand Down
2 changes: 1 addition & 1 deletion js/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ VectorTileSource.prototype = util.inherit(Evented, {
},

unloadTile: function(tile) {
tile.unloadVectorData(this.map.painter);
tile.unloadVectorData();
this.dispatcher.send('remove tile', { uid: tile.uid, source: this.id }, null, tile.workerID);
}
});

0 comments on commit 31523a8

Please sign in to comment.