Skip to content

Commit

Permalink
Don't create unnecessary scratch canvases
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekucera committed Dec 16, 2024
1 parent 33229de commit b470122
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/extensions/renderer/canvas/webgl/atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ export class Atlas {
// if the texture wraps then there's a second location
this.keyToLocation = new Map(); // styleKey -> [ location, location ]

// for unit tests
if(!opts.createTextureCanvas) {
opts.createTextureCanvas = util.createTextureCanvas;
}

this.canvas = opts.createTextureCanvas(r, this.atlasSize, this.atlasSize);
this.scratch = opts.createTextureCanvas(r, this.atlasSize, this.texHeight);
this.scratch = opts.createTextureCanvas(r, this.atlasSize, this.texHeight, 'scratch');
}

getKeys() {
Expand Down Expand Up @@ -222,10 +217,6 @@ export class AtlasCollection {
this.r = r;
this.opts = opts;

if(!opts.createTextureCanvas) {
opts.createTextureCanvas = util.createTextureCanvas;
}

this.keyToIds = new Map();
this.idToKey = new Map();

Expand Down Expand Up @@ -259,7 +250,7 @@ export class AtlasCollection {
const { r, opts } = this;
const atlasSize = opts.webglTexSize;
const texHeight = Math.floor(atlasSize / opts.webglTexRows);
this.scratch = opts.createTextureCanvas(r, atlasSize, texHeight);
this.scratch = opts.createTextureCanvas(r, atlasSize, texHeight, 'scratch');
}
return this.scratch;
}
Expand Down Expand Up @@ -448,13 +439,38 @@ export class AtlasManager {
constructor(r, globalOptions) {
this.r = r;

this.globalOptions = globalOptions;
this.maxAtlases = globalOptions.webglTexPerBatch;
this.atlasSize = globalOptions.webglTexSize;
const opts = globalOptions;
this.globalOptions = opts;
this.maxAtlases = opts.webglTexPerBatch;
this.atlasSize = opts.webglTexSize;

this.renderTypes = new Map(); // string -> object
this.maxAtlasesPerBatch = globalOptions.webglTexPerBatch;
this.batchAtlases = [];

this._cacheScratchCanvas(opts);
}

_cacheScratchCanvas(opts) {
console.log("cachedScratchFactory", opts);
let prevW = -1;
let prevH = -1;
let scratchCanvas = null;

const baseCreateTextureCanvas = opts.createTextureCanvas;

opts.createTextureCanvas = (r, w, h, scratch) => {
if(scratch) {
if(!scratchCanvas || w != prevW || h != prevH) {
prevW = w;
prevH = h;
scratchCanvas = baseCreateTextureCanvas(r, w, h);
}
return scratchCanvas;
} else {
return baseCreateTextureCanvas(r, w, h);
}
};
}

addRenderType(type, renderTypeOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class ElementDrawingWebGL {
this.bgColor = opts.bgColor;

opts.enableWrapping = true;
opts.createTextureCanvas = util.createTextureCanvas; // Unit tests mock this
this.atlasManager = new AtlasManager(r, opts);

this.program = this.createShaderProgram(RENDER_TARGET.SCREEN);
Expand Down

0 comments on commit b470122

Please sign in to comment.