Skip to content

Commit

Permalink
Get rid of user-specified refs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Nov 3, 2016
1 parent f829bd0 commit 5e69c64
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 374 deletions.
22 changes: 18 additions & 4 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ class Bucket {
};
}

/**
* Release the WebGL resources associated with the buffers. Note that because
* buckets are shared between layers having the same layout properties, they
* must be destroyed in groups (all buckets for a tile, or all symbol buckets).
*
* @private
*/
destroy() {
this.buffers.destroy();
}
}
if (this.buffers) {
this.buffers.destroy();
this.buffers = null;
}
}}

module.exports = Bucket;

Expand All @@ -70,6 +79,7 @@ Bucket.deserialize = function(input, style) {
if (!style) return;

const output = {};

for (const serialized of input) {
const layers = serialized.layerIds
.map((id) => style.getLayer(id))
Expand All @@ -79,7 +89,11 @@ Bucket.deserialize = function(input, style) {
continue;
}

output[layers[0].id] = layers[0].createBucket(util.extend({layers}, serialized));
const bucket = layers[0].createBucket(util.extend({layers}, serialized));
for (const layer of layers) {
output[layer.id] = bucket;
}
}

return output;
};
9 changes: 6 additions & 3 deletions js/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ class SymbolBucket {
}

destroy() {
this.buffers.icon.destroy();
this.buffers.glyph.destroy();
this.buffers.collisionBox.destroy();
if (this.buffers) {
this.buffers.icon.destroy();
this.buffers.glyph.destroy();
this.buffers.collisionBox.destroy();
this.buffers = null;
}
}

createArrays() {
Expand Down
4 changes: 1 addition & 3 deletions js/data/feature_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ class FeatureIndex {
}

const geojsonFeature = new GeoJSONFeature(feature, this.z, this.x, this.y);
geojsonFeature.layer = styleLayer.serialize({
includeRefProperties: true
});
geojsonFeature.layer = styleLayer.serialize();
let layerResult = result[layerID];
if (layerResult === undefined) {
layerResult = result[layerID] = [];
Expand Down
4 changes: 2 additions & 2 deletions js/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ class Tile {
for (const id in this.buckets) {
this.buckets[id].destroy();
}
this.buckets = {};

this.collisionBoxArray = null;
this.symbolQuadsArray = null;
this.symbolInstancesArray = null;
this.collisionTile = null;
this.featureIndex = null;
this.buckets = null;
this.state = 'unloaded';
}

Expand Down Expand Up @@ -152,7 +152,7 @@ class Tile {
}

getBucket(layer) {
return this.buckets && this.buckets[layer.ref || layer.id];
return this.buckets[layer.id];
}

querySourceFeatures(result, params) {
Expand Down
61 changes: 13 additions & 48 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SourceCache = require('../source/source_cache');
const styleSpec = require('./style_spec');
const MapboxGLFunction = require('mapbox-gl-function');
const getWorkerPool = require('../global_worker_pool');
const deref = require('mapbox-gl-style-spec/lib/deref');

/**
* @private
Expand Down Expand Up @@ -136,28 +137,15 @@ class Style extends Evented {
}

_resolve() {
let layer, layerJSON;
const layers = deref(this.stylesheet.layers);

this._layers = {};
this._order = this.stylesheet.layers.map((layer) => {
this._order = layers.map((layer) => {
return layer.id;
});

// resolve all layers WITHOUT a ref
for (let i = 0; i < this.stylesheet.layers.length; i++) {
layerJSON = this.stylesheet.layers[i];
if (layerJSON.ref) continue;
layer = StyleLayer.create(layerJSON);
this._layers[layer.id] = layer;
layer.setEventedParent(this, {layer: {id: layer.id}});
}

// resolve all layers WITH a ref
for (let j = 0; j < this.stylesheet.layers.length; j++) {
layerJSON = this.stylesheet.layers[j];
if (!layerJSON.ref) continue;
const refLayer = this.getLayer(layerJSON.ref);
layer = StyleLayer.create(layerJSON, refLayer);
this._layers = {};
for (let layer of layers) {
layer = StyleLayer.create(layer);
this._layers[layer.id] = layer;
layer.setEventedParent(this, {layer: {id: layer.id}});
}
Expand Down Expand Up @@ -194,9 +182,8 @@ class Style extends Evented {
_serializeLayers(ids) {
ids = ids || this._order;
const serialized = [];
const options = {includeRefProperties: true};
for (let i = 0; i < ids.length; i++) {
serialized.push(this._layers[ids[i]].serialize(options));
serialized.push(this._layers[ids[i]].serialize());
}
return serialized;
}
Expand Down Expand Up @@ -410,8 +397,7 @@ class Style extends Evented {
if (this._validate(validateStyle.layer,
`layers.${layer.id}`, layer, {arrayIndex: -1}, options)) return this;

const refLayer = layer.ref && this.getLayer(layer.ref);
layer = StyleLayer.create(layer, refLayer);
layer = StyleLayer.create(layer);
}
this._validateLayer(layer);

Expand Down Expand Up @@ -442,11 +428,6 @@ class Style extends Evented {
if (layer === undefined) {
throw new Error('There is no layer with this ID');
}
for (const i in this._layers) {
if (this._layers[i].ref === id) {
this.removeLayer(i);
}
}

layer.setEventedParent(null);

Expand All @@ -472,26 +453,10 @@ class Style extends Evented {
return this._layers[id];
}

/**
* If a layer has a `ref` property that makes it derive some values
* from another layer, return that referent layer. Otherwise,
* returns the layer itself.
* @param {string} id the layer's id
* @returns {Layer} the referent layer or the layer itself
* @private
*/
getReferentLayer(id) {
let layer = this.getLayer(id);
if (layer.ref) {
layer = this.getLayer(layer.ref);
}
return layer;
}

setLayerZoomRange(layerId, minzoom, maxzoom) {
this._checkLoaded();

const layer = this.getReferentLayer(layerId);
const layer = this.getLayer(layerId);

if (layer.minzoom === minzoom && layer.maxzoom === maxzoom) return this;

Expand All @@ -507,7 +472,7 @@ class Style extends Evented {
setFilter(layerId, filter) {
this._checkLoaded();

const layer = this.getReferentLayer(layerId);
const layer = this.getLayer(layerId);

if (filter !== null && this._validate(validateStyle.filter, `layers.${layer.id}.filter`, filter)) return this;

Expand All @@ -524,13 +489,13 @@ class Style extends Evented {
* @private
*/
getFilter(layer) {
return util.clone(this.getReferentLayer(layer).filter);
return util.clone(this.getLayer(layer).filter);
}

setLayoutProperty(layerId, name, value) {
this._checkLoaded();

const layer = this.getReferentLayer(layerId);
const layer = this.getLayer(layerId);

if (util.deepEqual(layer.getLayoutProperty(name), value)) return this;

Expand All @@ -546,7 +511,7 @@ class Style extends Evented {
* @private
*/
getLayoutProperty(layer, name) {
return this.getReferentLayer(layer).getLayoutProperty(name);
return this.getLayer(layer).getLayoutProperty(name);
}

setPaintProperty(layerId, name, value, klass) {
Expand Down
53 changes: 21 additions & 32 deletions js/style/style_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ const Evented = require('../util/evented');
const TRANSITION_SUFFIX = '-transition';

class StyleLayer extends Evented {
constructor(layer, refLayer) {
constructor(layer) {
super();
this.set(layer, refLayer);
this.set(layer);
}

set(layer, refLayer) {
set(layer) {
this.id = layer.id;
this.ref = layer.ref;
this.metadata = layer.metadata;
this.type = (refLayer || layer).type;
this.source = (refLayer || layer).source;
this.sourceLayer = (refLayer || layer)['source-layer'];
this.minzoom = (refLayer || layer).minzoom;
this.maxzoom = (refLayer || layer).maxzoom;
this.filter = (refLayer || layer).filter;
this.type = layer.type;
this.source = layer.source;
this.sourceLayer = layer['source-layer'];
this.minzoom = layer.minzoom;
this.maxzoom = layer.maxzoom;
this.filter = layer.filter;

this.paint = {};
this.layout = {};
Expand Down Expand Up @@ -54,12 +53,8 @@ class StyleLayer extends Evented {
}

// Resolve layout declarations
if (this.ref) {
this._layoutDeclarations = refLayer._layoutDeclarations;
} else {
for (layoutName in layer.layout) {
this.setLayoutProperty(layoutName, layer.layout[layoutName], options);
}
for (layoutName in layer.layout) {
this.setLayoutProperty(layoutName, layer.layout[layoutName], options);
}

// set initial layout/paint values
Expand Down Expand Up @@ -245,30 +240,24 @@ class StyleLayer extends Evented {
}
}

serialize(options) {
serialize() {
const output = {
'id': this.id,
'ref': this.ref,
'type': this.type,
'source': this.source,
'source-layer': this.sourceLayer,
'metadata': this.metadata,
'minzoom': this.minzoom,
'maxzoom': this.maxzoom
'maxzoom': this.maxzoom,
'filter': this.filter,
'layout': util.mapObject(this._layoutDeclarations, getDeclarationValue)
};

for (const klass in this._paintDeclarations) {
const key = klass === '' ? 'paint' : `paint.${klass}`;
output[key] = util.mapObject(this._paintDeclarations[klass], getDeclarationValue);
}

if (!this.ref || (options && options.includeRefProperties)) {
util.extend(output, {
'type': this.type,
'source': this.source,
'source-layer': this.sourceLayer,
'filter': this.filter,
'layout': util.mapObject(this._layoutDeclarations, getDeclarationValue)
});
}

return util.filterObject(output, (value, key) => {
return value !== undefined && !(key === 'layout' && !Object.keys(value).length);
});
Expand Down Expand Up @@ -339,9 +328,9 @@ const subclasses = {
'symbol': require('./style_layer/symbol_style_layer')
};

StyleLayer.create = function(layer, refLayer) {
const LayerClass = subclasses[(refLayer || layer).type] || StyleLayer;
return new LayerClass(layer, refLayer);
StyleLayer.create = function(layer) {
const LayerClass = subclasses[layer.type] || StyleLayer;
return new LayerClass(layer);
};

function getDeclarationValue(declaration) {
Expand Down
Loading

0 comments on commit 5e69c64

Please sign in to comment.