Merge pull request #8002 from mukulmishra18/refactor-canvas

[api-minor] Fix #7798: Refactor scratch canvas usage.
This commit is contained in:
Yury Delendik 2017-02-06 07:45:41 -06:00 committed by GitHub
commit d842c9c6b0
6 changed files with 64 additions and 25 deletions

View file

@ -80,13 +80,6 @@ var IsLittleEndianCached = {
}
};
function createScratchCanvas(width, height) {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
}
function addContextCurrentTransform(ctx) {
// If the context doesn't expose a `mozCurrentTransform`, add a JS based one.
if (!ctx.mozCurrentTransform) {
@ -204,7 +197,8 @@ function addContextCurrentTransform(ctx) {
}
var CachedCanvases = (function CachedCanvasesClosure() {
function CachedCanvases() {
function CachedCanvases(canvasFactory) {
this.canvasFactory = canvasFactory;
this.cache = Object.create(null);
}
CachedCanvases.prototype = {
@ -213,12 +207,11 @@ var CachedCanvases = (function CachedCanvasesClosure() {
var canvasEntry;
if (this.cache[id] !== undefined) {
canvasEntry = this.cache[id];
canvasEntry.canvas.width = width;
canvasEntry.canvas.height = height;
this.canvasFactory.reset(canvasEntry.canvas, width, height);
// reset canvas transform for emulated mozCurrentTransform, if needed
canvasEntry.context.setTransform(1, 0, 0, 1, 0, 0);
} else {
var canvas = createScratchCanvas(width, height);
var canvas = this.canvasFactory.create(width, height);
var ctx = canvas.getContext('2d');
if (trackTransform) {
addContextCurrentTransform(ctx);
@ -230,10 +223,7 @@ var CachedCanvases = (function CachedCanvasesClosure() {
clear: function () {
for (var id in this.cache) {
var canvasEntry = this.cache[id];
// Zeroing the width and height causes Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
canvasEntry.canvas.width = 0;
canvasEntry.canvas.height = 0;
this.canvasFactory.destroy(canvasEntry.canvas);
delete this.cache[id];
}
}
@ -456,7 +446,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
// Defines the number of steps before checking the execution time
var EXECUTION_STEPS = 10;
function CanvasGraphics(canvasCtx, commonObjs, objs, imageLayer) {
function CanvasGraphics(canvasCtx, commonObjs, objs, canvasFactory,
imageLayer) {
this.ctx = canvasCtx;
this.current = new CanvasExtraState();
this.stateStack = [];
@ -466,6 +457,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.xobjs = null;
this.commonObjs = commonObjs;
this.objs = objs;
this.canvasFactory = canvasFactory;
this.imageLayer = imageLayer;
this.groupStack = [];
this.processingType3 = null;
@ -477,7 +469,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.smaskStack = [];
this.smaskCounter = 0;
this.tempSMask = null;
this.cachedCanvases = new CachedCanvases();
this.cachedCanvases = new CachedCanvases(this.canvasFactory);
if (canvasCtx) {
// NOTE: if mozCurrentTransform is polyfilled, then the current state of
// the transformation must already be set in canvasCtx._transformMatrix.
@ -1448,7 +1440,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
get isFontSubpixelAAEnabled() {
// Checks if anti-aliasing is enabled when scaled text is painted.
// On Windows GDI scaled fonts looks bad.
var ctx = document.createElement('canvas').getContext('2d');
var ctx = this.canvasFactory.create(10, 10).getContext('2d');
ctx.scale(1.5, 1);
ctx.fillText('I', 0, 10);
var data = ctx.getImageData(0, 0, 10, 10).data;
@ -1694,7 +1686,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var self = this;
var canvasGraphicsFactory = {
createCanvasGraphics: function (ctx) {
return new CanvasGraphics(ctx, self.commonObjs, self.objs);
return new CanvasGraphics(ctx, self.commonObjs, self.objs,
self.canvasFactory);
}
};
pattern = new TilingPattern(IR, color, this.ctx, canvasGraphicsFactory,
@ -2308,5 +2301,4 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
})();
exports.CanvasGraphics = CanvasGraphics;
exports.createScratchCanvas = createScratchCanvas;
}));