Skip to content

Commit 8ac0690

Browse files
committed
Use only one temp canvas for patterns
This fixes a regression mozilla#3153 which was introduced by mozilla#2177. The temp vanvases that are being created are not being re-used, which lead to high memory use.
1 parent ff616c8 commit 8ac0690

File tree

1 file changed

+87
-63
lines changed

1 file changed

+87
-63
lines changed

src/pattern.js

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
'use strict';
2222

23+
// This global variable is used to minimize the memory usage when patterns are
24+
// used.
25+
var temporaryPatternCanvas = null;
26+
2327
var PatternType = {
2428
AXIAL: 2,
2529
RADIAL: 3
@@ -268,68 +272,24 @@ var TilingPattern = (function TilingPatternClosure() {
268272
COLORED: 1,
269273
UNCOLORED: 2
270274
};
271-
var MAX_PATTERN_SIZE = 4096;
275+
276+
var MAX_PATTERN_SIZE = 8192;
272277

273278
function TilingPattern(IR, color, ctx, objs, commonObjs) {
274-
var operatorList = IR[2];
279+
this.name = IR[1][0].name;
280+
this.operatorList = IR[2];
275281
this.matrix = IR[3] || [1, 0, 0, 1, 0, 0];
276-
var bbox = IR[4];
277-
var xstep = IR[5];
278-
var ystep = IR[6];
279-
var paintType = IR[7];
280-
var tilingType = IR[8];
281-
282-
TODO('TilingType: ' + tilingType);
283-
282+
this.bbox = IR[4];
283+
this.xstep = IR[5];
284+
this.ystep = IR[6];
285+
this.paintType = IR[7];
286+
this.tilingType = IR[8];
287+
this.color = color;
288+
this.objs = objs;
289+
this.commonObjs = commonObjs;
284290
this.curMatrix = ctx.mozCurrentTransform;
285-
this.ctx = ctx;
286291
this.type = 'Pattern';
287-
288-
var x0 = bbox[0], y0 = bbox[1], x1 = bbox[2], y1 = bbox[3];
289-
290-
var topLeft = [x0, y0];
291-
// we want the canvas to be as large as the step size
292-
var botRight = [x0 + xstep, y0 + ystep];
293-
294-
var width = botRight[0] - topLeft[0];
295-
var height = botRight[1] - topLeft[1];
296-
297-
// Obtain scale from matrix and current transformation matrix.
298-
var matrixScale = Util.singularValueDecompose2dScale(this.matrix);
299-
var curMatrixScale = Util.singularValueDecompose2dScale(this.curMatrix);
300-
var combinedScale = [matrixScale[0] * curMatrixScale[0],
301-
matrixScale[1] * curMatrixScale[1]];
302-
303-
// MAX_PATTERN_SIZE is used to avoid OOM situation.
304-
// Use width and height values that are as close as possible to the end
305-
// result when the pattern is used. Too low value makes the pattern look
306-
// blurry. Too large value makes it look too crispy.
307-
width = Math.min(Math.ceil(Math.abs(width * combinedScale[0])),
308-
MAX_PATTERN_SIZE);
309-
310-
height = Math.min(Math.ceil(Math.abs(height * combinedScale[1])),
311-
MAX_PATTERN_SIZE);
312-
313-
var tmpCanvas = createScratchCanvas(width, height);
314-
315-
// set the new canvas element context as the graphics context
316-
var tmpCtx = tmpCanvas.getContext('2d');
317-
var graphics = new CanvasGraphics(tmpCtx, commonObjs, objs);
318-
319-
this.setFillAndStrokeStyleToContext(tmpCtx, paintType, color);
320-
321-
this.setScale(width, height, xstep, ystep);
322-
this.transformToScale(graphics);
323-
324-
// transform coordinates to pattern space
325-
var tmpTranslate = [1, 0, 0, 1, -topLeft[0], -topLeft[1]];
326-
graphics.transform.apply(graphics, tmpTranslate);
327-
328-
this.clipBbox(graphics, bbox, x0, y0, x1, y1);
329-
330-
graphics.executeOperatorList(operatorList);
331-
332-
this.canvas = tmpCanvas;
292+
this.ctx = ctx;
333293
}
334294

335295
TilingPattern.getIR = function TilingPattern_getIR(operatorList, dict, args) {
@@ -347,6 +307,66 @@ var TilingPattern = (function TilingPatternClosure() {
347307
};
348308

349309
TilingPattern.prototype = {
310+
createPatternCanvas: function TilinPattern_createPatternCanvas(tmpCanvas) {
311+
var operatorList = this.operatorList;
312+
var bbox = this.bbox;
313+
var xstep = this.xstep;
314+
var ystep = this.ystep;
315+
var paintType = this.paintType;
316+
var tilingType = this.tilingType;
317+
var color = this.color;
318+
var objs = this.objs;
319+
var commonObjs = this.commonObjs;
320+
var ctx = this.ctx;
321+
322+
TODO('TilingType: ' + tilingType);
323+
324+
var x0 = bbox[0], y0 = bbox[1], x1 = bbox[2], y1 = bbox[3];
325+
326+
var topLeft = [x0, y0];
327+
// we want the canvas to be as large as the step size
328+
var botRight = [x0 + xstep, y0 + ystep];
329+
330+
var width = botRight[0] - topLeft[0];
331+
var height = botRight[1] - topLeft[1];
332+
333+
// Obtain scale from matrix and current transformation matrix.
334+
var matrixScale = Util.singularValueDecompose2dScale(this.matrix);
335+
var curMatrixScale = Util.singularValueDecompose2dScale(this.curMatrix);
336+
var combinedScale = [matrixScale[0] * curMatrixScale[0],
337+
matrixScale[1] * curMatrixScale[1]];
338+
339+
// MAX_PATTERN_SIZE is used to avoid OOM situation.
340+
// Use width and height values that are as close as possible to the end
341+
// result when the pattern is used. Too low value makes the pattern look
342+
// blurry. Too large value makes it look too crispy.
343+
width = Math.min(Math.ceil(Math.abs(width * combinedScale[0])),
344+
MAX_PATTERN_SIZE);
345+
346+
height = Math.min(Math.ceil(Math.abs(height * combinedScale[1])),
347+
MAX_PATTERN_SIZE);
348+
349+
tmpCanvas.width = width;
350+
tmpCanvas.height = height;
351+
352+
// set the new canvas element context as the graphics context
353+
var tmpCtx = tmpCanvas.getContext('2d');
354+
var graphics = new CanvasGraphics(tmpCtx, commonObjs, objs);
355+
356+
this.setFillAndStrokeStyleToContext(tmpCtx, paintType, color);
357+
358+
this.setScale(width, height, xstep, ystep);
359+
this.transformToScale(graphics);
360+
361+
// transform coordinates to pattern space
362+
var tmpTranslate = [1, 0, 0, 1, -topLeft[0], -topLeft[1]];
363+
graphics.transform.apply(graphics, tmpTranslate);
364+
365+
this.clipBbox(graphics, bbox, x0, y0, x1, y1);
366+
367+
graphics.executeOperatorList(operatorList);
368+
},
369+
350370
setScale: function TilingPattern_setScale(width, height, xstep, ystep) {
351371
this.scale = [width / xstep, height / ystep];
352372
},
@@ -392,15 +412,19 @@ var TilingPattern = (function TilingPatternClosure() {
392412
},
393413

394414
getPattern: function TilingPattern_getPattern() {
395-
var matrix = this.matrix;
396-
var curMatrix = this.curMatrix;
397-
var ctx = this.ctx;
415+
// The temporary canvas is created only because the memory is released
416+
// more quickly than creating multiple temporary canvases.
417+
if (temporaryPatternCanvas === null) {
418+
temporaryPatternCanvas = createScratchCanvas(0, 0);
419+
}
420+
this.createPatternCanvas(temporaryPatternCanvas);
398421

399-
ctx.setTransform.apply(ctx, curMatrix);
400-
ctx.transform.apply(ctx, matrix);
422+
var ctx = this.ctx;
423+
ctx.setTransform.apply(ctx, this.curMatrix);
424+
ctx.transform.apply(ctx, this.matrix);
401425
this.scaleToContext();
402426

403-
return ctx.createPattern(this.canvas, 'repeat');
427+
return ctx.createPattern(temporaryPatternCanvas, 'repeat');
404428
}
405429
};
406430

0 commit comments

Comments
 (0)