Skip to content

Commit

Permalink
Merge pull request #5196 from ahocevar/batch-fill-stroke
Browse files Browse the repository at this point in the history
Batch polygon and circle fills and strokes
  • Loading branch information
ahocevar committed Aug 24, 2016
2 parents 2d65ffb + 395793b commit b7c84b2
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 26 deletions.
1 change: 1 addition & 0 deletions examples/osm-vector-tiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var map = new ol.Map({
new ol.layer.VectorTile({
source: new ol.source.VectorTile({
format: format,
overlaps: false,
tileGrid: tileGrid,
url: 'http://{a-c}.tile.openstreetmap.us/' +
'vectiles-land-usages/{z}/{x}/{y}.topojson'
Expand Down
3 changes: 2 additions & 1 deletion examples/topojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var style = new ol.style.Style({
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'data/topojson/world-110m.json',
format: new ol.format.TopoJSON()
format: new ol.format.TopoJSON(),
overlaps: false
}),
style: function(feature) {
// don't want to render the full world polygon, which repeats all countries
Expand Down
24 changes: 24 additions & 0 deletions externs/olx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4366,6 +4366,7 @@ olx.source.TileImageOptions.prototype.wrapX;
* cacheSize: (number|undefined),
* format: (ol.format.Feature|undefined),
* logo: (string|olx.LogoOptions|undefined),
* overlaps: (boolean|undefined),
* projection: ol.ProjectionLike,
* state: (ol.source.State|undefined),
* tileClass: (function(new: ol.VectorTile, ol.TileCoord,
Expand Down Expand Up @@ -4415,6 +4416,17 @@ olx.source.VectorTileOptions.prototype.format;
olx.source.VectorTileOptions.prototype.logo;


/**
* This source may have overlapping geometries. Default is `true`. Setting this
* to `false` (e.g. for sources with polygons that represent administrative
* boundaries or TopoJSON sources) allows the renderer to optimise fill and
* stroke operations.
* @type {boolean|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.overlaps;


/**
* Projection.
* @type {ol.ProjectionLike}
Expand Down Expand Up @@ -5797,6 +5809,7 @@ olx.source.TileWMSOptions.prototype.wrapX;
* format: (ol.format.Feature|undefined),
* loader: (ol.FeatureLoader|undefined),
* logo: (string|olx.LogoOptions|undefined),
* overlaps: (boolean|undefined),
* strategy: (ol.LoadingStrategy|undefined),
* url: (string|ol.FeatureUrlFunction|undefined),
* useSpatialIndex: (boolean|undefined),
Expand Down Expand Up @@ -5849,6 +5862,17 @@ olx.source.VectorOptions.prototype.loader;
olx.source.VectorOptions.prototype.logo;


/**
* This source may have overlapping geometries. Default is `true`. Setting this
* to `false` (e.g. for sources with polygons that represent administrative
* boundaries or TopoJSON sources) allows the renderer to optimise fill and
* stroke operations.
* @type {boolean|undefined}
* @api
*/
olx.source.VectorOptions.prototype.overlaps;


/**
* The loading strategy to use. By default an {@link ol.loadingstrategy.all}
* strategy is used, a one-off strategy which loads all features at once.
Expand Down
93 changes: 75 additions & 18 deletions src/ol/render/canvas/replay.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ ol.render.canvas.Instruction = {
* @param {number} tolerance Tolerance.
* @param {ol.Extent} maxExtent Maximum extent.
* @param {number} resolution Resolution.
* @param {boolean} overlaps The replay can have overlapping geometries.
* @protected
* @struct
*/
ol.render.canvas.Replay = function(tolerance, maxExtent, resolution) {
ol.render.canvas.Replay = function(tolerance, maxExtent, resolution, overlaps) {
ol.render.VectorContext.call(this);

/**
Expand All @@ -71,6 +72,12 @@ ol.render.canvas.Replay = function(tolerance, maxExtent, resolution) {
*/
this.maxExtent = maxExtent;

/**
* @protected
* @type {boolean}
*/
this.overlaps = overlaps;

/**
* @private
* @type {ol.Extent}
Expand Down Expand Up @@ -257,6 +264,12 @@ ol.render.canvas.Replay.prototype.replay_ = function(
var localTransform = this.tmpLocalTransform_;
var localTransformInv = this.tmpLocalTransformInv_;
var prevX, prevY, roundX, roundY;
var pendingFill = 0;
var pendingStroke = 0;
// When the batch size gets too big, performance decreases. 200 is a good
// balance between batch size and number of fill/stroke instructions.
var batchSize =
this.instructions != instructions || this.overlaps ? 0 : 200;
while (i < ii) {
var instruction = instructions[i];
var type = /** @type {ol.render.canvas.Instruction} */ (instruction[0]);
Expand All @@ -276,7 +289,17 @@ ol.render.canvas.Replay.prototype.replay_ = function(
}
break;
case ol.render.canvas.Instruction.BEGIN_PATH:
context.beginPath();
if (pendingFill > batchSize) {
context.fill();
pendingFill = 0;
}
if (pendingStroke > batchSize) {
context.stroke();
pendingStroke = 0;
}
if (!pendingFill && !pendingStroke) {
context.beginPath();
}
++i;
break;
case ol.render.canvas.Instruction.CIRCLE:
Expand All @@ -290,6 +313,7 @@ ol.render.canvas.Replay.prototype.replay_ = function(
var dx = x2 - x1;
var dy = y2 - y1;
var r = Math.sqrt(dx * dx + dy * dy);
context.moveTo(x2, y2);
context.arc(x1, y1, r, 0, 2 * Math.PI, true);
++i;
break;
Expand Down Expand Up @@ -442,7 +466,11 @@ ol.render.canvas.Replay.prototype.replay_ = function(
++i;
break;
case ol.render.canvas.Instruction.FILL:
context.fill();
if (batchSize) {
pendingFill++;
} else {
context.fill();
}
++i;
break;
case ol.render.canvas.Instruction.MOVE_TO_LINE_TO:
Expand Down Expand Up @@ -479,6 +507,11 @@ ol.render.canvas.Replay.prototype.replay_ = function(
ol.colorlike.isColorLike(instruction[1]),
'2nd instruction should be a string, ' +
'CanvasPattern, or CanvasGradient');
if (pendingFill) {
context.fill();
pendingFill = 0;
}

context.fillStyle = /** @type {ol.ColorLike} */ (instruction[1]);
++i;
break;
Expand All @@ -498,6 +531,10 @@ ol.render.canvas.Replay.prototype.replay_ = function(
var usePixelRatio = instruction[7] !== undefined ?
instruction[7] : true;
var lineWidth = /** @type {number} */ (instruction[2]);
if (pendingStroke) {
context.stroke();
pendingStroke = 0;
}
context.strokeStyle = /** @type {string} */ (instruction[1]);
context.lineWidth = usePixelRatio ? lineWidth * pixelRatio : lineWidth;
context.lineCap = /** @type {string} */ (instruction[3]);
Expand All @@ -523,7 +560,11 @@ ol.render.canvas.Replay.prototype.replay_ = function(
++i;
break;
case ol.render.canvas.Instruction.STROKE:
context.stroke();
if (batchSize) {
pendingStroke++;
} else {
context.stroke();
}
++i;
break;
default:
Expand All @@ -532,6 +573,12 @@ ol.render.canvas.Replay.prototype.replay_ = function(
break;
}
}
if (pendingFill) {
context.fill();
}
if (pendingStroke) {
context.stroke();
}
// assert that all instructions were consumed
goog.DEBUG && console.assert(i == instructions.length,
'all instructions should be consumed');
Expand All @@ -551,7 +598,7 @@ ol.render.canvas.Replay.prototype.replay = function(
context, pixelRatio, transform, viewRotation, skippedFeaturesHash) {
var instructions = this.instructions;
this.replay_(context, pixelRatio, transform, viewRotation,
skippedFeaturesHash, instructions, undefined);
skippedFeaturesHash, instructions, undefined, undefined);
};


Expand Down Expand Up @@ -651,11 +698,12 @@ ol.render.canvas.Replay.prototype.getBufferedMaxExtent = function() {
* @param {number} tolerance Tolerance.
* @param {ol.Extent} maxExtent Maximum extent.
* @param {number} resolution Resolution.
* @param {boolean} overlaps The replay can have overlapping geometries.
* @protected
* @struct
*/
ol.render.canvas.ImageReplay = function(tolerance, maxExtent, resolution) {
ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution);
ol.render.canvas.ImageReplay = function(tolerance, maxExtent, resolution, overlaps) {
ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution, overlaps);

/**
* @private
Expand Down Expand Up @@ -917,12 +965,13 @@ ol.render.canvas.ImageReplay.prototype.setImageStyle = function(imageStyle) {
* @param {number} tolerance Tolerance.
* @param {ol.Extent} maxExtent Maximum extent.
* @param {number} resolution Resolution.
* @param {boolean} overlaps The replay can have overlapping geometries.
* @protected
* @struct
*/
ol.render.canvas.LineStringReplay = function(tolerance, maxExtent, resolution) {
ol.render.canvas.LineStringReplay = function(tolerance, maxExtent, resolution, overlaps) {

ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution);
ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution, overlaps);

/**
* @private
Expand Down Expand Up @@ -1151,12 +1200,13 @@ ol.render.canvas.LineStringReplay.prototype.setFillStrokeStyle = function(fillSt
* @param {number} tolerance Tolerance.
* @param {ol.Extent} maxExtent Maximum extent.
* @param {number} resolution Resolution.
* @param {boolean} overlaps The replay can have overlapping geometries.
* @protected
* @struct
*/
ol.render.canvas.PolygonReplay = function(tolerance, maxExtent, resolution) {
ol.render.canvas.PolygonReplay = function(tolerance, maxExtent, resolution, overlaps) {

ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution);
ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution, overlaps);

/**
* @private
Expand Down Expand Up @@ -1223,8 +1273,6 @@ ol.render.canvas.PolygonReplay.prototype.drawFlatCoordinatess_ = function(flatCo
closePathInstruction);
offset = end;
}
// FIXME is it quicker to fill and stroke each polygon individually,
// FIXME or all polygons together?
var fillInstruction = [ol.render.canvas.Instruction.FILL];
this.hitDetectionInstructions.push(fillInstruction);
if (state.fillStyle !== undefined) {
Expand Down Expand Up @@ -1506,12 +1554,13 @@ ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyles_ = function() {
* @param {number} tolerance Tolerance.
* @param {ol.Extent} maxExtent Maximum extent.
* @param {number} resolution Resolution.
* @param {boolean} overlaps The replay can have overlapping geometries.
* @protected
* @struct
*/
ol.render.canvas.TextReplay = function(tolerance, maxExtent, resolution) {
ol.render.canvas.TextReplay = function(tolerance, maxExtent, resolution, overlaps) {

ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution);
ol.render.canvas.Replay.call(this, tolerance, maxExtent, resolution, overlaps);

/**
* @private
Expand Down Expand Up @@ -1823,10 +1872,12 @@ ol.render.canvas.TextReplay.prototype.setTextStyle = function(textStyle) {
* @param {number} tolerance Tolerance.
* @param {ol.Extent} maxExtent Max extent.
* @param {number} resolution Resolution.
* @param {boolean} overlaps The replay group can have overlapping geometries.
* @param {number=} opt_renderBuffer Optional rendering buffer.
* @struct
*/
ol.render.canvas.ReplayGroup = function(tolerance, maxExtent, resolution, opt_renderBuffer) {
ol.render.canvas.ReplayGroup = function(
tolerance, maxExtent, resolution, overlaps, opt_renderBuffer) {
ol.render.ReplayGroup.call(this);

/**
Expand All @@ -1841,6 +1892,12 @@ ol.render.canvas.ReplayGroup = function(tolerance, maxExtent, resolution, opt_re
*/
this.maxExtent_ = maxExtent;

/**
* @private
* @type {boolean}
*/
this.overlaps_ = overlaps;

/**
* @private
* @type {number}
Expand Down Expand Up @@ -1960,7 +2017,7 @@ ol.render.canvas.ReplayGroup.prototype.getReplay = function(zIndex, replayType)
replayType +
' constructor missing from ol.render.canvas.BATCH_CONSTRUCTORS_');
replay = new Constructor(this.tolerance_, this.maxExtent_,
this.resolution_);
this.resolution_, this.overlaps_);
replays[replayType] = replay;
}
return replay;
Expand Down Expand Up @@ -2074,7 +2131,7 @@ ol.render.canvas.ReplayGroup.prototype.replayHitDetection_ = function(
* @private
* @type {Object.<ol.render.ReplayType,
* function(new: ol.render.canvas.Replay, number, ol.Extent,
* number)>}
* number, boolean)>}
*/
ol.render.canvas.BATCH_CONSTRUCTORS_ = {
'Image': ol.render.canvas.ImageReplay,
Expand Down
2 changes: 1 addition & 1 deletion src/ol/renderer/canvas/vectorlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ ol.renderer.canvas.VectorLayer.prototype.prepareFrame = function(frameState, lay
var replayGroup =
new ol.render.canvas.ReplayGroup(
ol.renderer.vector.getTolerance(resolution, pixelRatio), extent,
resolution, vectorLayer.getRenderBuffer());
resolution, vectorSource.getOverlaps(), vectorLayer.getRenderBuffer());
vectorSource.loadFeatures(extent, resolution, projection);
/**
* @param {ol.Feature} feature Feature.
Expand Down
2 changes: 1 addition & 1 deletion src/ol/renderer/canvas/vectortilelayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ ol.renderer.canvas.VectorTileLayer.prototype.createReplayGroup = function(tile,
}
replayState.dirty = false;
var replayGroup = new ol.render.canvas.ReplayGroup(0, extent,
tileResolution, layer.getRenderBuffer());
tileResolution, source.getOverlaps(), layer.getRenderBuffer());
var squaredTolerance = ol.renderer.vector.getSquaredTolerance(
tileResolution, pixelRatio);

Expand Down
2 changes: 1 addition & 1 deletion src/ol/renderer/dom/vectorlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ ol.renderer.dom.VectorLayer.prototype.prepareFrame = function(frameState, layerS
var replayGroup =
new ol.render.canvas.ReplayGroup(
ol.renderer.vector.getTolerance(resolution, pixelRatio), extent,
resolution, vectorLayer.getRenderBuffer());
resolution, vectorSource.getOverlaps(), vectorLayer.getRenderBuffer());
vectorSource.loadFeatures(extent, resolution, projection);
/**
* @param {ol.Feature} feature Feature.
Expand Down
2 changes: 1 addition & 1 deletion src/ol/source/imagevector.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ ol.source.ImageVector.prototype.canvasFunctionInternal_ = function(extent, resol

var replayGroup = new ol.render.canvas.ReplayGroup(
ol.renderer.vector.getTolerance(resolution, pixelRatio), extent,
resolution, this.renderBuffer_);
resolution, this.source_.getOverlaps(), this.renderBuffer_);

this.source_.loadFeatures(extent, resolution, projection);

Expand Down
14 changes: 14 additions & 0 deletions src/ol/source/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ ol.source.Vector = function(opt_options) {
*/
this.format_ = options.format;

/**
* @private
* @type {boolean}
*/
this.overlaps_ = options.overlaps == undefined ? true : options.overlaps;

/**
* @private
* @type {string|ol.FeatureUrlFunction|undefined}
Expand Down Expand Up @@ -695,6 +701,14 @@ ol.source.Vector.prototype.getFormat = function() {
};


/**
* @return {boolean} The source can have overlapping geometries.
*/
ol.source.Vector.prototype.getOverlaps = function() {
return this.overlaps_;
};


/**
* Get the url associated with this source.
*
Expand Down
Loading

0 comments on commit b7c84b2

Please sign in to comment.