Revert "Batch polygon fill and stroke instructions"

This commit is contained in:
Andreas Hocevar
2016-04-07 16:30:04 +02:00
parent c9ffcef766
commit fe33910ec2

View File

@@ -153,10 +153,11 @@ goog.inherits(ol.render.canvas.Replay, ol.render.VectorContext);
* @param {number} offset Offset. * @param {number} offset Offset.
* @param {number} end End. * @param {number} end End.
* @param {number} stride Stride. * @param {number} stride Stride.
* @param {boolean} close Close.
* @protected * @protected
* @return {number} My end. * @return {number} My end.
*/ */
ol.render.canvas.Replay.prototype.appendFlatCoordinates = function(flatCoordinates, offset, end, stride) { ol.render.canvas.Replay.prototype.appendFlatCoordinates = function(flatCoordinates, offset, end, stride, close) {
var myEnd = this.coordinates.length; var myEnd = this.coordinates.length;
var extent = this.getBufferedMaxExtent(); var extent = this.getBufferedMaxExtent();
@@ -195,6 +196,10 @@ ol.render.canvas.Replay.prototype.appendFlatCoordinates = function(flatCoordinat
this.coordinates[myEnd++] = lastCoord[1]; this.coordinates[myEnd++] = lastCoord[1];
} }
if (close) {
this.coordinates[myEnd++] = flatCoordinates[offset];
this.coordinates[myEnd++] = flatCoordinates[offset + 1];
}
return myEnd; return myEnd;
}; };
@@ -766,7 +771,8 @@ goog.inherits(ol.render.canvas.ImageReplay, ol.render.canvas.Replay);
* @return {number} My end. * @return {number} My end.
*/ */
ol.render.canvas.ImageReplay.prototype.drawCoordinates_ = function(flatCoordinates, offset, end, stride) { ol.render.canvas.ImageReplay.prototype.drawCoordinates_ = function(flatCoordinates, offset, end, stride) {
return this.appendFlatCoordinates(flatCoordinates, offset, end, stride); return this.appendFlatCoordinates(
flatCoordinates, offset, end, stride, false);
}; };
@@ -987,7 +993,8 @@ goog.inherits(ol.render.canvas.LineStringReplay, ol.render.canvas.Replay);
*/ */
ol.render.canvas.LineStringReplay.prototype.drawFlatCoordinates_ = function(flatCoordinates, offset, end, stride) { ol.render.canvas.LineStringReplay.prototype.drawFlatCoordinates_ = function(flatCoordinates, offset, end, stride) {
var myBegin = this.coordinates.length; var myBegin = this.coordinates.length;
var myEnd = this.appendFlatCoordinates(flatCoordinates, offset, end, stride); var myEnd = this.appendFlatCoordinates(
flatCoordinates, offset, end, stride, false);
var moveToLineToInstruction = var moveToLineToInstruction =
[ol.render.canvas.Instruction.MOVE_TO_LINE_TO, myBegin, myEnd]; [ol.render.canvas.Instruction.MOVE_TO_LINE_TO, myBegin, myEnd];
this.instructions.push(moveToLineToInstruction); this.instructions.push(moveToLineToInstruction);
@@ -1189,9 +1196,7 @@ ol.render.canvas.PolygonReplay = function(tolerance, maxExtent, resolution) {
* lineDash: Array.<number>, * lineDash: Array.<number>,
* lineJoin: (string|undefined), * lineJoin: (string|undefined),
* lineWidth: (number|undefined), * lineWidth: (number|undefined),
* miterLimit: (number|undefined), * miterLimit: (number|undefined)}|null}
* pendingFill: number,
* pendingStroke: number}|null}
*/ */
this.state_ = { this.state_ = {
currentFillStyle: undefined, currentFillStyle: undefined,
@@ -1207,9 +1212,7 @@ ol.render.canvas.PolygonReplay = function(tolerance, maxExtent, resolution) {
lineDash: null, lineDash: null,
lineJoin: undefined, lineJoin: undefined,
lineWidth: undefined, lineWidth: undefined,
miterLimit: undefined, miterLimit: undefined
pendingFill: 0,
pendingStroke: 0
}; };
}; };
@@ -1226,19 +1229,15 @@ goog.inherits(ol.render.canvas.PolygonReplay, ol.render.canvas.Replay);
*/ */
ol.render.canvas.PolygonReplay.prototype.drawFlatCoordinatess_ = function(flatCoordinates, offset, ends, stride) { ol.render.canvas.PolygonReplay.prototype.drawFlatCoordinatess_ = function(flatCoordinates, offset, ends, stride) {
var state = this.state_; var state = this.state_;
if (state.pendingFill > 200 || state.pendingStroke > 200) {
this.fillStroke_();
}
var beginPathInstruction = [ol.render.canvas.Instruction.BEGIN_PATH]; var beginPathInstruction = [ol.render.canvas.Instruction.BEGIN_PATH];
if (!state.pendingFill && !state.pendingStroke) { this.instructions.push(beginPathInstruction);
this.instructions.push(beginPathInstruction);
}
this.hitDetectionInstructions.push(beginPathInstruction); this.hitDetectionInstructions.push(beginPathInstruction);
var i, ii; var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) { for (i = 0, ii = ends.length; i < ii; ++i) {
var end = ends[i]; var end = ends[i];
var myBegin = this.coordinates.length; var myBegin = this.coordinates.length;
var myEnd = this.appendFlatCoordinates(flatCoordinates, offset, end, stride); var myEnd = this.appendFlatCoordinates(
flatCoordinates, offset, end, stride, true);
var moveToLineToInstruction = var moveToLineToInstruction =
[ol.render.canvas.Instruction.MOVE_TO_LINE_TO, myBegin, myEnd]; [ol.render.canvas.Instruction.MOVE_TO_LINE_TO, myBegin, myEnd];
var closePathInstruction = [ol.render.canvas.Instruction.CLOSE_PATH]; var closePathInstruction = [ol.render.canvas.Instruction.CLOSE_PATH];
@@ -1247,15 +1246,19 @@ ol.render.canvas.PolygonReplay.prototype.drawFlatCoordinatess_ = function(flatCo
closePathInstruction); closePathInstruction);
offset = end; offset = end;
} }
this.hitDetectionInstructions.push([ol.render.canvas.Instruction.FILL]); // 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) { if (state.fillStyle !== undefined) {
state.pendingFill++; this.instructions.push(fillInstruction);
} }
if (state.strokeStyle !== undefined) { if (state.strokeStyle !== undefined) {
goog.asserts.assert(state.lineWidth !== undefined, goog.asserts.assert(state.lineWidth !== undefined,
'state.lineWidth should be defined'); 'state.lineWidth should be defined');
state.pendingStroke++; var strokeInstruction = [ol.render.canvas.Instruction.STROKE];
this.hitDetectionInstructions.push([ol.render.canvas.Instruction.STROKE]); this.instructions.push(strokeInstruction);
this.hitDetectionInstructions.push(strokeInstruction);
} }
return offset; return offset;
}; };
@@ -1292,23 +1295,22 @@ ol.render.canvas.PolygonReplay.prototype.drawCircle = function(circleGeometry, f
var stride = circleGeometry.getStride(); var stride = circleGeometry.getStride();
var myBegin = this.coordinates.length; var myBegin = this.coordinates.length;
this.appendFlatCoordinates( this.appendFlatCoordinates(
flatCoordinates, 0, flatCoordinates.length, stride); flatCoordinates, 0, flatCoordinates.length, stride, false);
var beginPathInstruction = [ol.render.canvas.Instruction.BEGIN_PATH]; var beginPathInstruction = [ol.render.canvas.Instruction.BEGIN_PATH];
var circleInstruction = [ol.render.canvas.Instruction.CIRCLE, myBegin]; var circleInstruction = [ol.render.canvas.Instruction.CIRCLE, myBegin];
if (!state.pendingFill && !state.pendingStroke) { this.instructions.push(beginPathInstruction, circleInstruction);
this.instructions.push(beginPathInstruction); this.hitDetectionInstructions.push(beginPathInstruction, circleInstruction);
} var fillInstruction = [ol.render.canvas.Instruction.FILL];
this.instructions.push(circleInstruction); this.hitDetectionInstructions.push(fillInstruction);
this.hitDetectionInstructions.push(circleInstruction, beginPathInstruction);
this.hitDetectionInstructions.push([ol.render.canvas.Instruction.FILL]);
if (state.fillStyle !== undefined) { if (state.fillStyle !== undefined) {
state.pendingFill++; this.instructions.push(fillInstruction);
} }
if (state.strokeStyle !== undefined) { if (state.strokeStyle !== undefined) {
goog.asserts.assert(state.lineWidth !== undefined, goog.asserts.assert(state.lineWidth !== undefined,
'state.lineWidth should be defined'); 'state.lineWidth should be defined');
state.pendingStroke++; var strokeInstruction = [ol.render.canvas.Instruction.STROKE];
this.hitDetectionInstructions.push([ol.render.canvas.Instruction.STROKE]); this.instructions.push(strokeInstruction);
this.hitDetectionInstructions.push(strokeInstruction);
} }
this.endGeometry(circleGeometry, feature); this.endGeometry(circleGeometry, feature);
}; };
@@ -1389,28 +1391,13 @@ ol.render.canvas.PolygonReplay.prototype.drawMultiPolygon = function(multiPolygo
}; };
/**
* @private
*/
ol.render.canvas.PolygonReplay.prototype.fillStroke_ = function() {
var state = this.state_;
if (state.pendingFill) {
this.instructions.push([ol.render.canvas.Instruction.FILL]);
state.pendingFill = 0;
}
if (state.pendingStroke) {
this.instructions.push([ol.render.canvas.Instruction.STROKE]);
state.pendingStroke = 0;
}
};
/** /**
* @inheritDoc * @inheritDoc
*/ */
ol.render.canvas.PolygonReplay.prototype.finish = function() { ol.render.canvas.PolygonReplay.prototype.finish = function() {
goog.asserts.assert(this.state_, 'this.state_ should not be null'); goog.asserts.assert(this.state_, 'this.state_ should not be null');
this.reverseHitDetectionInstructions_(); this.reverseHitDetectionInstructions_();
this.state_ = null;
// We want to preserve topology when drawing polygons. Polygons are // We want to preserve topology when drawing polygons. Polygons are
// simplified using quantization and point elimination. However, we might // simplified using quantization and point elimination. However, we might
// have received a mix of quantized and non-quantized geometries, so ensure // have received a mix of quantized and non-quantized geometries, so ensure
@@ -1423,8 +1410,6 @@ ol.render.canvas.PolygonReplay.prototype.finish = function() {
coordinates[i] = ol.geom.flat.simplify.snap(coordinates[i], tolerance); coordinates[i] = ol.geom.flat.simplify.snap(coordinates[i], tolerance);
} }
} }
this.fillStroke_();
this.state_ = null;
}; };
@@ -1507,7 +1492,6 @@ ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyles_ = function() {
var lineWidth = state.lineWidth; var lineWidth = state.lineWidth;
var miterLimit = state.miterLimit; var miterLimit = state.miterLimit;
if (fillStyle !== undefined && state.currentFillStyle != fillStyle) { if (fillStyle !== undefined && state.currentFillStyle != fillStyle) {
this.fillStroke_();
this.instructions.push( this.instructions.push(
[ol.render.canvas.Instruction.SET_FILL_STYLE, fillStyle]); [ol.render.canvas.Instruction.SET_FILL_STYLE, fillStyle]);
state.currentFillStyle = state.fillStyle; state.currentFillStyle = state.fillStyle;
@@ -1525,7 +1509,6 @@ ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyles_ = function() {
state.currentLineJoin != lineJoin || state.currentLineJoin != lineJoin ||
state.currentLineWidth != lineWidth || state.currentLineWidth != lineWidth ||
state.currentMiterLimit != miterLimit) { state.currentMiterLimit != miterLimit) {
this.fillStroke_();
this.instructions.push( this.instructions.push(
[ol.render.canvas.Instruction.SET_STROKE_STYLE, [ol.render.canvas.Instruction.SET_STROKE_STYLE,
strokeStyle, lineWidth, lineCap, lineJoin, miterLimit, lineDash]); strokeStyle, lineWidth, lineCap, lineJoin, miterLimit, lineDash]);
@@ -1640,7 +1623,8 @@ ol.render.canvas.TextReplay.prototype.drawText = function(flatCoordinates, offse
this.setReplayTextState_(this.textState_); this.setReplayTextState_(this.textState_);
this.beginGeometry(geometry, feature); this.beginGeometry(geometry, feature);
var myBegin = this.coordinates.length; var myBegin = this.coordinates.length;
var myEnd = this.appendFlatCoordinates(flatCoordinates, offset, end, stride); var myEnd =
this.appendFlatCoordinates(flatCoordinates, offset, end, stride, false);
var fill = !!this.textFillState_; var fill = !!this.textFillState_;
var stroke = !!this.textStrokeState_; var stroke = !!this.textStrokeState_;
var drawTextInstruction = [ var drawTextInstruction = [