spine-html 1.22.1 → 1.22.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6939,13 +6939,13 @@ attribute vec2 aPos;
6939
6939
  attribute vec2 aUV;
6940
6940
  attribute vec4 aColor;
6941
6941
  attribute vec3 aDark;
6942
- uniform vec2 uResolution;
6942
+ uniform vec2 uScale;
6943
6943
  uniform vec2 uOrigin;
6944
6944
  varying vec2 vUV;
6945
6945
  varying vec4 vColor;
6946
6946
  varying vec3 vDark;
6947
6947
  void main() {
6948
- vec2 clip = (aPos + uOrigin) / uResolution * 2.0 - 1.0;
6948
+ vec2 clip = (aPos - uOrigin) * uScale - 1.0;
6949
6949
  gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);
6950
6950
  vUV = aUV;
6951
6951
  vColor = aColor;
@@ -6964,13 +6964,14 @@ void main() {
6964
6964
  const VERTEX_SHADER = `
6965
6965
  attribute vec2 aPos;
6966
6966
  attribute vec2 aUV;
6967
- uniform vec2 uResolution;
6967
+ uniform vec2 uScale;
6968
6968
  uniform vec2 uOrigin;
6969
6969
  varying vec2 vUV;
6970
6970
  void main() {
6971
- // aPos is in offscreen-canvas device px, top-left origin (matching the
6972
- // top-left-origin drawImage read of the same rect).
6973
- vec2 clip = (aPos - uOrigin) / uResolution * 2.0 - 1.0;
6971
+ // aPos is in the job's own device px, top-left origin (matching the
6972
+ // top-left-origin drawImage read of the same rect); uOrigin is the top-left
6973
+ // of the viewport window within the job, 0 unless the job passes maxSize.
6974
+ vec2 clip = (aPos - uOrigin) * uScale - 1.0;
6974
6975
  gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);
6975
6976
  vUV = aUV;
6976
6977
  }`;
@@ -6983,6 +6984,39 @@ void main() {
6983
6984
  }`;
6984
6985
  const GUTTER = 1;
6985
6986
  const GROW_STEP = 256;
6987
+ const ALIGN = 32;
6988
+ function onPhase(v) {
6989
+ return v + ((GUTTER - v) % ALIGN + ALIGN) % ALIGN;
6990
+ }
6991
+ function shelfPack(pieces, bufW, bufH) {
6992
+ const order = pieces.slice().sort((a, b) => b.th - a.th);
6993
+ let pass = 0;
6994
+ let shelfX = GUTTER;
6995
+ let shelfY = GUTTER;
6996
+ let shelfH = 0;
6997
+ let height = GUTTER;
6998
+ for (const piece of order) {
6999
+ if (shelfX + piece.tw + GUTTER > bufW) {
7000
+ shelfY = onPhase(shelfY + shelfH + GUTTER);
7001
+ shelfX = GUTTER;
7002
+ shelfH = 0;
7003
+ }
7004
+ if (shelfY + piece.th + GUTTER > bufH) {
7005
+ pass++;
7006
+ shelfX = GUTTER;
7007
+ shelfY = GUTTER;
7008
+ shelfH = 0;
7009
+ height = GUTTER;
7010
+ }
7011
+ piece.bx = shelfX;
7012
+ piece.by = shelfY;
7013
+ piece.pass = pass;
7014
+ shelfX = onPhase(shelfX + piece.tw + GUTTER);
7015
+ if (piece.th > shelfH) shelfH = piece.th;
7016
+ if (shelfY + piece.th + GUTTER > height) height = shelfY + piece.th + GUTTER;
7017
+ }
7018
+ return height;
7019
+ }
6986
7020
  class MeshGlBlitter {
6987
7021
  lost = false;
6988
7022
  /**
@@ -6993,13 +7027,13 @@ class MeshGlBlitter {
6993
7027
  liveTextureCount = 0;
6994
7028
  canvas = document.createElement("canvas");
6995
7029
  gl;
6996
- uResolution;
7030
+ uScale;
6997
7031
  uOrigin;
6998
7032
  meshProgram;
6999
7033
  meshPos;
7000
7034
  meshUV;
7001
7035
  frameProgram;
7002
- frameResolution;
7036
+ frameScale;
7003
7037
  frameOrigin;
7004
7038
  framePos;
7005
7039
  frameUV;
@@ -7015,8 +7049,24 @@ class MeshGlBlitter {
7015
7049
  */
7016
7050
  textures = /* @__PURE__ */ new WeakMap();
7017
7051
  maxSize;
7052
+ /**
7053
+ * The smallest drawing-buffer area the browser has refused to grant in full
7054
+ * (see the module comment); a growth to this area or more is not attempted.
7055
+ */
7056
+ refusedArea = Number.POSITIVE_INFINITY;
7057
+ /** Test-only caps on the grant, and the tiling plant — see limitForTesting(). */
7058
+ testArea = Number.POSITIVE_INFINITY;
7059
+ testWidth = Number.POSITIVE_INFINITY;
7060
+ testHeight = Number.POSITIVE_INFINITY;
7061
+ tiling = true;
7062
+ /**
7063
+ * What the last flush() or flushFrame() did.
7064
+ *
7065
+ * @internal Read by the test harness; not part of the package API.
7066
+ */
7067
+ stats = { jobs: 0, passes: 0, pieces: 0, tiledJobs: 0 };
7018
7068
  vertexData = new Float32Array(8192);
7019
- /** Per-job packed rect origins, filled by flush(). */
7069
+ /** Per-job packed rect origins (a tiled job's first tile), filled by flush(). */
7020
7070
  packX = [];
7021
7071
  packY = [];
7022
7072
  constructor() {
@@ -7036,10 +7086,10 @@ class MeshGlBlitter {
7036
7086
  const program = this.link(VERTEX_SHADER, FRAGMENT_SHADER);
7037
7087
  this.meshProgram = program;
7038
7088
  gl.useProgram(program);
7039
- const uResolution = gl.getUniformLocation(program, "uResolution");
7089
+ const uScale = gl.getUniformLocation(program, "uScale");
7040
7090
  const uOrigin = gl.getUniformLocation(program, "uOrigin");
7041
- if (!uResolution || !uOrigin) throw new Error("mesh program uniforms not found");
7042
- this.uResolution = uResolution;
7091
+ if (!uScale || !uOrigin) throw new Error("mesh program uniforms not found");
7092
+ this.uScale = uScale;
7043
7093
  this.uOrigin = uOrigin;
7044
7094
  gl.uniform1i(gl.getUniformLocation(program, "uTex"), 0);
7045
7095
  this.meshPos = gl.getAttribLocation(program, "aPos");
@@ -7047,10 +7097,10 @@ class MeshGlBlitter {
7047
7097
  const frame2 = this.link(FRAME_VERTEX_SHADER, FRAME_FRAGMENT_SHADER);
7048
7098
  this.frameProgram = frame2;
7049
7099
  gl.useProgram(frame2);
7050
- const frameResolution = gl.getUniformLocation(frame2, "uResolution");
7100
+ const frameScale = gl.getUniformLocation(frame2, "uScale");
7051
7101
  const frameOrigin = gl.getUniformLocation(frame2, "uOrigin");
7052
- if (!frameResolution || !frameOrigin) throw new Error("frame program uniforms not found");
7053
- this.frameResolution = frameResolution;
7102
+ if (!frameScale || !frameOrigin) throw new Error("frame program uniforms not found");
7103
+ this.frameScale = frameScale;
7054
7104
  this.frameOrigin = frameOrigin;
7055
7105
  gl.uniform1i(gl.getUniformLocation(frame2, "uTex"), 0);
7056
7106
  this.framePos = gl.getAttribLocation(frame2, "aPos");
@@ -7062,48 +7112,50 @@ class MeshGlBlitter {
7062
7112
  gl.enable(gl.SCISSOR_TEST);
7063
7113
  gl.clearColor(0, 0, 0, 0);
7064
7114
  gl.activeTexture(gl.TEXTURE0);
7065
- this.maxSize = Math.min(gl.getParameter(gl.MAX_RENDERBUFFER_SIZE) || 4096, 8192);
7115
+ const viewport = gl.getParameter(gl.MAX_VIEWPORT_DIMS);
7116
+ const side = Math.min(gl.getParameter(gl.MAX_RENDERBUFFER_SIZE) || 4096, 8192, viewport?.[0] || 4096, viewport?.[1] || 4096);
7117
+ this.maxSize = Math.floor(side / ALIGN) * ALIGN;
7066
7118
  }
7067
7119
  /**
7068
- * Rasterize every job into the offscreen buffer, then blit each rect onto
7069
- * its per-part canvas. Returns false when the context is lost (or the jobs
7070
- * cannot fit) so the caller can rasterize the batch on the canvas2d path.
7120
+ * Rasterizes every job and blits it onto its per-part canvas. Returns false
7121
+ * only when the context is lost, so that the caller draws the batch on its
7122
+ * canvas2d path; a batch's size never sends it there.
7123
+ *
7124
+ * A batch that fits one buffer is one pass, shelf-packed as it always was.
7125
+ * One that does not — a zoomed-in stage asks for more than the browser
7126
+ * grants, or for more than `maxSize` — is drawn in passes through the
7127
+ * largest buffer the browser grants in full: its pieces are shelf-packed
7128
+ * into that buffer, a pass is drawn and blitted, and the next pass reuses
7129
+ * the buffer. A piece is a whole job, or one tile of a job larger than the
7130
+ * buffer.
7131
+ *
7132
+ * Every piece is rastered in its own job's rect (#187): the vertex upload is
7133
+ * relative to the job and nothing else, and the viewport is the job's rect
7134
+ * wherever the piece sits. A tile is the same viewport moved so that the
7135
+ * tile lands in the buffer, scissored to the tile and blitted into the
7136
+ * matching sub-rect of the part canvas — so a pixel does not depend on how
7137
+ * its job was packed, which pass drew it, or how it was tiled.
7071
7138
  */
7072
7139
  flush(jobs) {
7073
7140
  const gl = this.gl;
7141
+ this.stats = { jobs: jobs.length, passes: 0, pieces: 0, tiledJobs: 0 };
7074
7142
  if (this.lost || gl.isContextLost()) return false;
7075
7143
  let width = Math.max(512, this.canvas.width);
7076
7144
  for (const job of jobs) width = Math.max(width, job.width + GUTTER * 2);
7077
- if (width > this.maxSize) return false;
7078
- const order = jobs.map((_, i) => i).sort((a, b) => jobs[b].height - jobs[a].height);
7145
+ let pieces = jobs.map((job, i) => ({ job: i, tx: 0, ty: 0, tw: job.width, th: job.height, bx: 0, by: 0, pass: 0 }));
7146
+ const height = shelfPack(pieces, width, Number.POSITIVE_INFINITY);
7147
+ if (!this.reserve(width, height)) {
7148
+ if (!this.acquire(width, height)) return false;
7149
+ const planned = this.plan(jobs, this.canvas.width, this.canvas.height);
7150
+ if (!planned) return false;
7151
+ pieces = planned;
7152
+ }
7079
7153
  const packX = this.packX = new Array(jobs.length);
7080
7154
  const packY = this.packY = new Array(jobs.length);
7081
- let shelfX = GUTTER;
7082
- let shelfY = GUTTER;
7083
- let shelfH = 0;
7084
- let height = GUTTER;
7085
- for (const i of order) {
7086
- const job = jobs[i];
7087
- if (shelfX + job.width + GUTTER > width) {
7088
- shelfY += shelfH + GUTTER;
7089
- shelfX = GUTTER;
7090
- shelfH = 0;
7091
- }
7092
- packX[i] = shelfX;
7093
- packY[i] = shelfY;
7094
- shelfX += job.width + GUTTER;
7095
- if (job.height > shelfH) shelfH = job.height;
7096
- if (shelfY + job.height + GUTTER > height) height = shelfY + job.height + GUTTER;
7097
- }
7098
- if (height > this.maxSize) return false;
7099
- if (width > this.canvas.width || height > this.canvas.height) {
7100
- this.canvas.width = Math.min(this.maxSize, Math.ceil(width / GROW_STEP) * GROW_STEP);
7101
- this.canvas.height = Math.min(
7102
- this.maxSize,
7103
- Math.ceil(Math.max(height, this.canvas.height) / GROW_STEP) * GROW_STEP
7104
- );
7155
+ for (let k = pieces.length - 1; k >= 0; k--) {
7156
+ packX[pieces[k].job] = pieces[k].bx;
7157
+ packY[pieces[k].job] = pieces[k].by;
7105
7158
  }
7106
- const bufH = this.canvas.height;
7107
7159
  this.bindMesh();
7108
7160
  let floats = 0;
7109
7161
  for (const job of jobs) floats += job.triangles.length * 4;
@@ -7111,105 +7163,304 @@ class MeshGlBlitter {
7111
7163
  this.vertexData = new Float32Array(1 << Math.ceil(Math.log2(floats)));
7112
7164
  }
7113
7165
  const data = this.vertexData;
7166
+ const firsts = new Array(jobs.length);
7114
7167
  let f = 0;
7115
7168
  for (let i = 0; i < jobs.length; i++) {
7116
7169
  const { vertices, uvs, ratio } = jobs[i];
7117
7170
  const triangles = triangleReadSource(jobs[i].triangles);
7118
- const ox = packX[i];
7119
- const oy = packY[i];
7171
+ firsts[i] = f / 4;
7120
7172
  for (let t = 0; t < triangles.length; t++) {
7121
7173
  const vi = triangles[t] * 2;
7122
- data[f++] = ox + vertices[vi] * ratio;
7123
- data[f++] = oy + vertices[vi + 1] * ratio;
7174
+ data[f++] = vertices[vi] * ratio;
7175
+ data[f++] = vertices[vi + 1] * ratio;
7124
7176
  data[f++] = uvs[vi];
7125
7177
  data[f++] = uvs[vi + 1];
7126
7178
  }
7127
7179
  }
7128
- gl.bufferData(gl.ARRAY_BUFFER, data.subarray(0, floats), gl.DYNAMIC_DRAW);
7129
- let boundPage = null;
7130
- let first = 0;
7131
- for (let i = 0; i < jobs.length; i++) {
7132
- const job = jobs[i];
7133
- if (job.page !== boundPage) {
7134
- gl.bindTexture(gl.TEXTURE_2D, this.textureFor(job.page, job.pma));
7135
- boundPage = job.page;
7136
- }
7137
- gl.viewport(packX[i], bufH - packY[i] - job.height, job.width, job.height);
7138
- gl.uniform2f(this.uResolution, job.width, job.height);
7139
- gl.uniform2f(this.uOrigin, packX[i], packY[i]);
7140
- gl.scissor(packX[i], bufH - packY[i] - job.height, job.width, job.height);
7141
- gl.clear(gl.COLOR_BUFFER_BIT);
7142
- gl.drawArrays(gl.TRIANGLES, first, job.triangles.length);
7143
- first += job.triangles.length;
7144
- }
7145
- if (gl.isContextLost()) {
7146
- this.markLost();
7147
- return false;
7180
+ if (floats > 0) gl.bufferData(gl.ARRAY_BUFFER, data.subarray(0, floats), gl.DYNAMIC_DRAW);
7181
+ const cleared = new Uint8Array(jobs.length);
7182
+ const bufH = this.canvas.height;
7183
+ let start = 0;
7184
+ while (start < pieces.length) {
7185
+ const pass = pieces[start].pass;
7186
+ let end = start;
7187
+ while (end < pieces.length && pieces[end].pass === pass) end++;
7188
+ let boundPage = null;
7189
+ for (let k = start; k < end; k++) {
7190
+ const piece = pieces[k];
7191
+ const job = jobs[piece.job];
7192
+ if (job.page !== boundPage) {
7193
+ gl.bindTexture(gl.TEXTURE_2D, this.textureFor(job.page, job.pma));
7194
+ boundPage = job.page;
7195
+ }
7196
+ this.placePiece(piece, job.width, job.height, bufH, this.uScale, this.uOrigin);
7197
+ gl.clear(gl.COLOR_BUFFER_BIT);
7198
+ gl.drawArrays(gl.TRIANGLES, firsts[piece.job], job.triangles.length);
7199
+ }
7200
+ if (gl.isContextLost()) {
7201
+ this.markLost();
7202
+ return false;
7203
+ }
7204
+ for (let k = start; k < end; k++) {
7205
+ const piece = pieces[k];
7206
+ const job = jobs[piece.job];
7207
+ const ctx = job.canvas.getContext("2d");
7208
+ if (!ctx) continue;
7209
+ if (!cleared[piece.job]) {
7210
+ cleared[piece.job] = 1;
7211
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
7212
+ ctx.clearRect(0, 0, job.canvas.width, job.canvas.height);
7213
+ }
7214
+ if (piece.tw > 0 && piece.th > 0) {
7215
+ ctx.drawImage(this.canvas, piece.bx, piece.by, piece.tw, piece.th, piece.tx, piece.ty, piece.tw, piece.th);
7216
+ }
7217
+ }
7218
+ this.stats.passes++;
7219
+ start = end;
7148
7220
  }
7221
+ this.stats.pieces = pieces.length;
7149
7222
  for (let i = 0; i < jobs.length; i++) {
7150
- const job = jobs[i];
7151
- const ctx = job.canvas.getContext("2d");
7223
+ if (cleared[i]) continue;
7224
+ const ctx = jobs[i].canvas.getContext("2d");
7152
7225
  if (!ctx) continue;
7153
7226
  ctx.setTransform(1, 0, 0, 1, 0, 0);
7154
- ctx.clearRect(0, 0, job.canvas.width, job.canvas.height);
7155
- ctx.drawImage(this.canvas, packX[i], packY[i], job.width, job.height, 0, 0, job.width, job.height);
7227
+ ctx.clearRect(0, 0, jobs[i].canvas.width, jobs[i].canvas.height);
7156
7228
  }
7157
7229
  return true;
7158
7230
  }
7159
7231
  /**
7160
- * Draws one player's frame (the canvas render mode) into a rect of the
7161
- * offscreen buffer and blits it onto `job.canvas`, whose full backing is
7162
- * cleared first. Returns false when the context is lost or the rect cannot
7163
- * fit, so the caller can draw the frame on its canvas2d fallback instead.
7232
+ * Sets the viewport, the two uniforms and the scissor for one piece. The
7233
+ * viewport is the job's own rect — or, for a job wider or taller than
7234
+ * `maxSize`, the `maxSize` window of it that holds the piece, since a
7235
+ * viewport cannot be larger — placed so that the piece's sub-rect lands on
7236
+ * the piece's rect of the buffer; the scissor is that rect. Scissor and
7237
+ * viewport are bottom-left origin; pieces are placed top-left.
7238
+ *
7239
+ * The clip-space mapping is `(p - origin) * scale - 1` with `scale` the
7240
+ * window's 2 / size, a function of the job and the window alone. Written as
7241
+ * `(p - origin) / size * 2 - 1` instead, SwiftShader drew the canvas mode's
7242
+ * 6,901 px frame differently in two tilings (51,537 pixels); this form drew
7243
+ * them identically, and the mesh tier identically either way.
7244
+ */
7245
+ placePiece(piece, width, height, bufH, scale, origin) {
7246
+ const gl = this.gl;
7247
+ const wx = piece.tx - piece.tx % this.maxSize;
7248
+ const wy = piece.ty - piece.ty % this.maxSize;
7249
+ const ww = Math.min(this.maxSize, width - wx);
7250
+ const wh = Math.min(this.maxSize, height - wy);
7251
+ const left = piece.bx - piece.tx;
7252
+ const top = piece.by - piece.ty;
7253
+ gl.viewport(left + wx, bufH - (top + wy) - wh, ww, wh);
7254
+ gl.uniform2f(scale, 2 / ww, 2 / wh);
7255
+ gl.uniform2f(origin, wx, wy);
7256
+ gl.scissor(piece.bx, bufH - piece.by - piece.th, piece.tw, piece.th);
7257
+ }
7258
+ /**
7259
+ * Cuts each rect into pieces that fit a `bufW` × `bufH` buffer with its
7260
+ * gutter — a rect that fits is one piece, a larger one is tiled, tiles never
7261
+ * crossing a `maxSize` window — and shelf-packs them, tallest first, into
7262
+ * consecutive passes of that buffer. A tile starts on the `ALIGN` grid of
7263
+ * its job, so its job's corner keeps the buffer phase every piece has.
7264
+ * Returns the pieces in pass order, or null when a rect needs tiling and
7265
+ * tiling is off (a test-only plant) or the buffer is too small to tile.
7266
+ */
7267
+ plan(rects, bufW, bufH) {
7268
+ const tileW = Math.floor((bufW - GUTTER * 2) / ALIGN) * ALIGN;
7269
+ const tileH = Math.floor((bufH - GUTTER * 2) / ALIGN) * ALIGN;
7270
+ const max = this.maxSize;
7271
+ const pieces = [];
7272
+ for (let i = 0; i < rects.length; i++) {
7273
+ const { width, height } = rects[i];
7274
+ if (width + GUTTER * 2 <= bufW && height + GUTTER * 2 <= bufH && width <= max && height <= max) {
7275
+ pieces.push({ job: i, tx: 0, ty: 0, tw: width, th: height, bx: 0, by: 0, pass: 0 });
7276
+ continue;
7277
+ }
7278
+ if (!this.tiling || tileW <= 0 || tileH <= 0) return null;
7279
+ this.stats.tiledJobs++;
7280
+ for (let wy = 0; wy < height; wy += max) {
7281
+ const wh = Math.min(max, height - wy);
7282
+ for (let wx = 0; wx < width; wx += max) {
7283
+ const ww = Math.min(max, width - wx);
7284
+ for (let ty = wy; ty < wy + wh; ty += tileH) {
7285
+ for (let tx = wx; tx < wx + ww; tx += tileW) {
7286
+ pieces.push({ job: i, tx, ty, tw: Math.min(tileW, wx + ww - tx), th: Math.min(tileH, wy + wh - ty), bx: 0, by: 0, pass: 0 });
7287
+ }
7288
+ }
7289
+ }
7290
+ }
7291
+ }
7292
+ shelfPack(pieces, bufW, bufH);
7293
+ return pieces.sort((a, b) => a.pass - b.pass);
7294
+ }
7295
+ /** The buffer sides this module will use: `maxSize`, or a test cap below it. */
7296
+ get limitW() {
7297
+ return Math.floor(Math.min(this.maxSize, this.testWidth) / ALIGN) * ALIGN;
7298
+ }
7299
+ get limitH() {
7300
+ return Math.floor(Math.min(this.maxSize, this.testHeight) / ALIGN) * ALIGN;
7301
+ }
7302
+ /**
7303
+ * Makes the drawing buffer hold at least `width` × `height`, growing it
7304
+ * grow-only and quantized, and returns whether it does — as granted, not as
7305
+ * asked. A growth the browser grants only in part is undone (the previous
7306
+ * size was granted in full) and its area remembered, so a zoom held past
7307
+ * the cap declines each frame without reallocating the buffer twice per
7308
+ * frame. Resizing clears the buffer, which is fine: every rect is cleared
7309
+ * and redrawn anyway.
7310
+ */
7311
+ reserve(width, height) {
7312
+ const canvas = this.canvas;
7313
+ const gl = this.gl;
7314
+ const limitW = this.limitW;
7315
+ const limitH = this.limitH;
7316
+ if (width > limitW || height > limitH) return false;
7317
+ if (width > canvas.width || height > canvas.height) {
7318
+ const w = Math.min(limitW, Math.ceil(Math.max(width, canvas.width) / GROW_STEP) * GROW_STEP);
7319
+ const h = Math.min(limitH, Math.ceil(Math.max(height, canvas.height) / GROW_STEP) * GROW_STEP);
7320
+ if (w * h >= this.refusedArea) return false;
7321
+ if (w * h > this.testArea) {
7322
+ this.refusedArea = w * h;
7323
+ return false;
7324
+ }
7325
+ const previousW = canvas.width;
7326
+ const previousH = canvas.height;
7327
+ canvas.width = w;
7328
+ canvas.height = h;
7329
+ if (gl.drawingBufferWidth < w || gl.drawingBufferHeight < h) {
7330
+ this.refusedArea = Math.min(this.refusedArea, w * h);
7331
+ canvas.width = previousW;
7332
+ canvas.height = previousH;
7333
+ return false;
7334
+ }
7335
+ }
7336
+ return gl.drawingBufferWidth === canvas.width && gl.drawingBufferHeight === canvas.height;
7337
+ }
7338
+ /**
7339
+ * Grows the buffer toward `width` × `height` as far as the browser grants
7340
+ * in full, for a batch that one buffer cannot hold. The request is shrunk a
7341
+ * quantum at a time, larger side first, never below the buffer already
7342
+ * held, past every area the browser has refused; each refusal lowers that
7343
+ * area, so the search ends — at worst on the buffer already held. Returns
7344
+ * false only when even that is not granted in full.
7345
+ */
7346
+ acquire(width, height) {
7347
+ const canvas = this.canvas;
7348
+ let w = Math.max(canvas.width, Math.min(this.limitW, Math.ceil(width / GROW_STEP) * GROW_STEP));
7349
+ let h = Math.max(canvas.height, Math.min(this.limitH, Math.ceil(height / GROW_STEP) * GROW_STEP));
7350
+ for (; ; ) {
7351
+ const held = w <= canvas.width && h <= canvas.height;
7352
+ if (held || w * h < this.refusedArea) {
7353
+ if (this.reserve(w, h)) return true;
7354
+ if (held) return false;
7355
+ }
7356
+ if (w >= h && w - GROW_STEP >= canvas.width) w -= GROW_STEP;
7357
+ else if (h - GROW_STEP >= canvas.height) h -= GROW_STEP;
7358
+ else if (w - GROW_STEP >= canvas.width) w -= GROW_STEP;
7359
+ else {
7360
+ w = canvas.width;
7361
+ h = canvas.height;
7362
+ }
7363
+ }
7364
+ }
7365
+ /**
7366
+ * Test-only limits: `area` caps the drawing-buffer area this module will
7367
+ * take as granted (a browser that grants less), `width` and `height` cap
7368
+ * its sides below `maxSize`, and `tiling: false` is the planted control that declines a job
7369
+ * larger than the buffer instead of tiling it. Setting limits drops the
7370
+ * buffer held so far, so they hold from the next flush; null lifts them.
7371
+ * Viewport windows stay `maxSize`: the caps stand for a smaller grant, not
7372
+ * a different GPU.
7373
+ *
7374
+ * @internal Read by the test harness; not part of the package API.
7375
+ */
7376
+ limitForTesting(limits) {
7377
+ this.testArea = limits?.area ?? Number.POSITIVE_INFINITY;
7378
+ this.testWidth = limits?.width ?? Number.POSITIVE_INFINITY;
7379
+ this.testHeight = limits?.height ?? Number.POSITIVE_INFINITY;
7380
+ this.tiling = limits?.tiling ?? true;
7381
+ this.refusedArea = Number.POSITIVE_INFINITY;
7382
+ if (limits) {
7383
+ this.canvas.width = 1;
7384
+ this.canvas.height = 1;
7385
+ }
7386
+ }
7387
+ /**
7388
+ * Draws one player's frame (the canvas render mode) and blits it onto
7389
+ * `job.canvas`, whose full backing is cleared first. Returns false only when
7390
+ * the context is lost, so the caller draws the frame on its canvas2d
7391
+ * fallback. The frame is one job: drawn into one rect when the buffer holds
7392
+ * it, otherwise tiled through passes exactly as flush() tiles a mesh, every
7393
+ * tile re-issuing the frame's runs under its own scissor.
7164
7394
  */
7165
7395
  flushFrame(job) {
7166
7396
  const gl = this.gl;
7397
+ this.stats = { jobs: 1, passes: 0, pieces: 0, tiledJobs: 0 };
7167
7398
  if (this.lost || gl.isContextLost()) return false;
7168
- const width = job.width + GUTTER * 2;
7169
- const height = job.height + GUTTER * 2;
7170
- if (width > this.maxSize || height > this.maxSize) return false;
7171
- if (width > this.canvas.width || height > this.canvas.height) {
7172
- this.canvas.width = Math.min(this.maxSize, Math.ceil(Math.max(width, this.canvas.width) / GROW_STEP) * GROW_STEP);
7173
- this.canvas.height = Math.min(this.maxSize, Math.ceil(Math.max(height, this.canvas.height) / GROW_STEP) * GROW_STEP);
7174
- }
7175
- const bufW = this.canvas.width;
7399
+ let pieces;
7400
+ if (this.reserve(job.width + GUTTER * 2, job.height + GUTTER * 2)) {
7401
+ pieces = [{ job: 0, tx: 0, ty: 0, tw: job.width, th: job.height, bx: GUTTER, by: GUTTER, pass: 0 }];
7402
+ } else {
7403
+ if (!this.acquire(job.width + GUTTER * 2, job.height + GUTTER * 2)) return false;
7404
+ const planned = this.plan([job], this.canvas.width, this.canvas.height);
7405
+ if (!planned) return false;
7406
+ pieces = planned;
7407
+ }
7176
7408
  const bufH = this.canvas.height;
7177
- gl.viewport(0, 0, bufW, bufH);
7178
7409
  this.bindFrame(job.stride);
7179
- gl.uniform2f(this.frameResolution, bufW, bufH);
7180
- gl.uniform2f(this.frameOrigin, GUTTER, GUTTER);
7181
7410
  let vertices = 0;
7182
7411
  for (const run of job.runs) vertices = Math.max(vertices, run.first + run.count);
7183
7412
  if (vertices > 0) gl.bufferData(gl.ARRAY_BUFFER, job.data.subarray(0, vertices * job.stride), gl.DYNAMIC_DRAW);
7184
- gl.scissor(GUTTER, bufH - GUTTER - job.height, job.width, job.height);
7185
- gl.clear(gl.COLOR_BUFFER_BIT);
7413
+ const ctx = job.canvas.getContext("2d");
7414
+ if (ctx) {
7415
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
7416
+ ctx.globalAlpha = 1;
7417
+ ctx.globalCompositeOperation = "source-over";
7418
+ }
7419
+ let cleared = false;
7186
7420
  let boundPage = null;
7187
7421
  let boundBlend = null;
7188
- for (const run of job.runs) {
7189
- if (run.page !== boundPage) {
7190
- gl.bindTexture(gl.TEXTURE_2D, this.textureFor(run.page, run.pma));
7191
- boundPage = run.page;
7422
+ let start = 0;
7423
+ while (start < pieces.length) {
7424
+ const pass = pieces[start].pass;
7425
+ let end = start;
7426
+ while (end < pieces.length && pieces[end].pass === pass) end++;
7427
+ for (let k = start; k < end; k++) {
7428
+ this.placePiece(pieces[k], job.width, job.height, bufH, this.frameScale, this.frameOrigin);
7429
+ gl.clear(gl.COLOR_BUFFER_BIT);
7430
+ for (const run of job.runs) {
7431
+ if (run.page !== boundPage) {
7432
+ gl.bindTexture(gl.TEXTURE_2D, this.textureFor(run.page, run.pma));
7433
+ boundPage = run.page;
7434
+ }
7435
+ if (run.blend !== boundBlend) {
7436
+ const [src, dst] = BLEND_FACTORS[run.blend];
7437
+ gl.blendFuncSeparate(src, dst, gl.ONE, dst);
7438
+ boundBlend = run.blend;
7439
+ }
7440
+ gl.drawArrays(gl.TRIANGLES, run.first, run.count);
7441
+ }
7192
7442
  }
7193
- if (run.blend !== boundBlend) {
7194
- const [src, dst] = BLEND_FACTORS[run.blend];
7195
- gl.blendFuncSeparate(src, dst, gl.ONE, dst);
7196
- boundBlend = run.blend;
7443
+ if (gl.isContextLost()) {
7444
+ this.markLost();
7445
+ return false;
7197
7446
  }
7198
- gl.drawArrays(gl.TRIANGLES, run.first, run.count);
7199
- }
7200
- if (gl.isContextLost()) {
7201
- this.markLost();
7202
- return false;
7203
- }
7204
- const ctx = job.canvas.getContext("2d");
7205
- if (!ctx) return true;
7206
- ctx.setTransform(1, 0, 0, 1, 0, 0);
7207
- ctx.globalAlpha = 1;
7208
- ctx.globalCompositeOperation = "source-over";
7209
- ctx.clearRect(0, 0, job.canvas.width, job.canvas.height);
7210
- if (job.width > 0 && job.height > 0) {
7211
- ctx.drawImage(this.canvas, GUTTER, GUTTER, job.width, job.height, 0, 0, job.width, job.height);
7447
+ if (ctx) {
7448
+ if (!cleared) {
7449
+ cleared = true;
7450
+ ctx.clearRect(0, 0, job.canvas.width, job.canvas.height);
7451
+ }
7452
+ for (let k = start; k < end; k++) {
7453
+ const piece = pieces[k];
7454
+ if (piece.tw > 0 && piece.th > 0) {
7455
+ ctx.drawImage(this.canvas, piece.bx, piece.by, piece.tw, piece.th, piece.tx, piece.ty, piece.tw, piece.th);
7456
+ }
7457
+ }
7458
+ }
7459
+ this.stats.passes++;
7460
+ start = end;
7212
7461
  }
7462
+ this.stats.pieces = pieces.length;
7463
+ if (ctx && !cleared) ctx.clearRect(0, 0, job.canvas.width, job.canvas.height);
7213
7464
  return true;
7214
7465
  }
7215
7466
  /** The mesh tier's layout, [x, y, u, v], and its premultiplied source-over. */