Skip to content

Commit 6b33422

Browse files
committed
Merge pull request mozilla#3179 from yurydelendik/pr-3171
Cont of mozilla#3171, Reusing pattern canvas fixes
2 parents 3c0705d + d7808b0 commit 6b33422

File tree

2 files changed

+97
-63
lines changed

2 files changed

+97
-63
lines changed

src/canvas.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function addContextCurrentTransform(ctx) {
5757
ctx._originalScale = ctx.scale;
5858
ctx._originalTranslate = ctx.translate;
5959
ctx._originalTransform = ctx.transform;
60+
ctx._originalSetTransform = ctx.setTransform;
6061

6162
ctx._transformMatrix = [ctx._scaleX, 0, 0, ctx._scaleY, 0, 0];
6263
ctx._transformStack = [];
@@ -138,6 +139,12 @@ function addContextCurrentTransform(ctx) {
138139
ctx._originalTransform(a, b, c, d, e, f);
139140
};
140141

142+
ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
143+
this._transformMatrix = [a, b, c, d, e, f];
144+
145+
ctx._originalSetTransform(a, b, c, d, e, f);
146+
};
147+
141148
ctx.rotate = function ctxRotate(angle) {
142149
var cosValue = Math.cos(angle);
143150
var sinValue = Math.sin(angle);

src/pattern.js

Lines changed: 90 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,69 @@ 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+
// for simulated mozCurrentTransform canvas (normaly setting width/height
355+
// will reset the matrix)
356+
tmpCtx.setTransform(1, 0, 0, 1, 0, 0);
357+
var graphics = new CanvasGraphics(tmpCtx, commonObjs, objs);
358+
359+
this.setFillAndStrokeStyleToContext(tmpCtx, paintType, color);
360+
361+
this.setScale(width, height, xstep, ystep);
362+
this.transformToScale(graphics);
363+
364+
// transform coordinates to pattern space
365+
var tmpTranslate = [1, 0, 0, 1, -topLeft[0], -topLeft[1]];
366+
graphics.transform.apply(graphics, tmpTranslate);
367+
368+
this.clipBbox(graphics, bbox, x0, y0, x1, y1);
369+
370+
graphics.executeOperatorList(operatorList);
371+
},
372+
350373
setScale: function TilingPattern_setScale(width, height, xstep, ystep) {
351374
this.scale = [width / xstep, height / ystep];
352375
},
@@ -392,15 +415,19 @@ var TilingPattern = (function TilingPatternClosure() {
392415
},
393416

394417
getPattern: function TilingPattern_getPattern() {
395-
var matrix = this.matrix;
396-
var curMatrix = this.curMatrix;
397-
var ctx = this.ctx;
418+
// The temporary canvas is created only because the memory is released
419+
// more quickly than creating multiple temporary canvases.
420+
if (temporaryPatternCanvas === null) {
421+
temporaryPatternCanvas = createScratchCanvas(1, 1);
422+
}
423+
this.createPatternCanvas(temporaryPatternCanvas);
398424

399-
ctx.setTransform.apply(ctx, curMatrix);
400-
ctx.transform.apply(ctx, matrix);
425+
var ctx = this.ctx;
426+
ctx.setTransform.apply(ctx, this.curMatrix);
427+
ctx.transform.apply(ctx, this.matrix);
401428
this.scaleToContext();
402429

403-
return ctx.createPattern(this.canvas, 'repeat');
430+
return ctx.createPattern(temporaryPatternCanvas, 'repeat');
404431
}
405432
};
406433

0 commit comments

Comments
 (0)