@next2d/renderer 3.11.1 → 3.12.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/renderer",
3
- "version": "3.11.1",
3
+ "version": "3.12.1",
4
4
  "description": "Next2D Renderer Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -24,9 +24,9 @@
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
26
  "dependencies": {
27
- "@next2d/webgl": "3.11.1",
28
- "@next2d/webgpu": "3.11.1",
29
- "@next2d/cache": "3.11.1",
30
- "@next2d/texture-packer": "3.11.1"
27
+ "@next2d/webgl": "3.12.1",
28
+ "@next2d/webgpu": "3.12.1",
29
+ "@next2d/cache": "3.12.1",
30
+ "@next2d/texture-packer": "3.12.1"
31
31
  }
32
32
  }
@@ -3,6 +3,7 @@ import type { IMessage } from "./interface/IMessage";
3
3
  * @class
4
4
  */
5
5
  export declare class CommandController {
6
+ private readonly frameLimiter;
6
7
  /**
7
8
  * @description workerの実行状態
8
9
  * Execution status of worker
@@ -4,6 +4,8 @@ import { execute as commandRenderUseCase } from "./Command/usecase/CommandRender
4
4
  import { execute as commandRemoveCacheService } from "./Command/service/CommandRemoveCacheService";
5
5
  import { execute as commandCaptureUseCase } from "./Command/usecase/CommandCaptureUseCase";
6
6
  import { $cacheStore } from "@next2d/cache";
7
+ import { $context } from "./RendererUtil";
8
+ import { RenderFrameLimiter } from "./RenderFrameLimiter";
7
9
  /**
8
10
  * @class
9
11
  */
@@ -13,6 +15,12 @@ export class CommandController {
13
15
  * @public
14
16
  */
15
17
  constructor() {
18
+ Object.defineProperty(this, "frameLimiter", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: new RenderFrameLimiter(2)
23
+ });
16
24
  /**
17
25
  * @description workerの実行状態
18
26
  * Execution status of worker
@@ -63,6 +71,12 @@ export class CommandController {
63
71
  case "render":
64
72
  commandRenderUseCase(object.buffer.subarray(0, object.length), object.imageBitmaps);
65
73
  // 描画完了したらメインスレッドにbufferを返却する
74
+ if ("getRenderCompletion" in $context) {
75
+ const capacity = this.frameLimiter.track($context.getRenderCompletion());
76
+ if (capacity) {
77
+ await capacity;
78
+ }
79
+ }
66
80
  globalThis.postMessage({
67
81
  "message": "render",
68
82
  "buffer": object.buffer
@@ -74,6 +88,12 @@ export class CommandController {
74
88
  break;
75
89
  case "initialize":
76
90
  await commandInitializeContextService(object.canvas, object.devicePixelRatio);
91
+ globalThis.postMessage({
92
+ "message": "renderFlowControl",
93
+ // Both backends must bound Main -> Worker render requests.
94
+ // Both backends additionally wait for GPU frame capacity above.
95
+ "enabled": true
96
+ });
77
97
  break;
78
98
  case "removeCache":
79
99
  commandRemoveCacheService(object.buffer);
@@ -0,0 +1,8 @@
1
+ /** Limits outstanding rendered frames without mixing buffer ownership with GPU completion. */
2
+ export declare class RenderFrameLimiter {
3
+ private readonly limit;
4
+ private readonly pending;
5
+ constructor(limit?: number);
6
+ /** The caller must await the returned capacity before submitting another frame. */
7
+ track(completion: Promise<void>): Promise<void> | null;
8
+ }
@@ -0,0 +1,33 @@
1
+ /** Limits outstanding rendered frames without mixing buffer ownership with GPU completion. */
2
+ export class RenderFrameLimiter {
3
+ constructor(limit = 2) {
4
+ Object.defineProperty(this, "limit", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: limit
9
+ });
10
+ Object.defineProperty(this, "pending", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: []
15
+ });
16
+ if (!Number.isInteger(limit) || limit < 1) {
17
+ throw new RangeError("Invalid frame limit");
18
+ }
19
+ }
20
+ /** The caller must await the returned capacity before submitting another frame. */
21
+ track(completion) {
22
+ const release = () => {
23
+ const index = this.pending.indexOf(settled);
24
+ if (index >= 0) {
25
+ this.pending.splice(index, 1);
26
+ }
27
+ };
28
+ // A failed/lost queue must not leave the renderer's buffer return blocked.
29
+ const settled = completion.then(release, release);
30
+ this.pending.push(settled);
31
+ return this.pending.length >= this.limit ? this.pending[0] : null;
32
+ }
33
+ }
@@ -13,12 +13,21 @@ import { $context } from "../../RendererUtil";
13
13
  * @protected
14
14
  */
15
15
  export const execute = (render_queue, index) => {
16
- const matrix = render_queue.subarray(index, index + 6);
17
- index += 6;
18
- const colorTransform = render_queue.subarray(index, index + 8);
16
+ // フィルター以外はスカラーで読むことでShape毎の一時ビュー生成を避ける。
17
+ // Only filters need a matrix view; ordinary drawing reads scalar values.
18
+ const matrixIndex = index;
19
+ const a = render_queue[index++];
20
+ const b = render_queue[index++];
21
+ const c = render_queue[index++];
22
+ const d = render_queue[index++];
23
+ const e = render_queue[index++];
24
+ const f = render_queue[index++];
25
+ const colorTransformIndex = index;
19
26
  index += 8;
20
- const bounds = render_queue.subarray(index, index + 4);
21
- index += 4;
27
+ const boundsXMin = render_queue[index++];
28
+ const boundsYMin = render_queue[index++];
29
+ const boundsXMax = render_queue[index++];
30
+ const boundsYMax = render_queue[index++];
22
31
  // baseBounds
23
32
  const xMin = render_queue[index++];
24
33
  const yMin = render_queue[index++];
@@ -145,44 +154,48 @@ export const execute = (render_queue, index) => {
145
154
  index += 4;
146
155
  const length = render_queue[index++];
147
156
  const params = render_queue.subarray(index, index + length);
148
- const width = Math.ceil(Math.abs(bounds[2] - bounds[0]));
149
- const height = Math.ceil(Math.abs(bounds[3] - bounds[1]));
157
+ const width = Math.ceil(Math.abs(boundsXMax - boundsXMin));
158
+ const height = Math.ceil(Math.abs(boundsYMax - boundsYMin));
159
+ const matrix = render_queue.subarray(matrixIndex, matrixIndex + 6);
160
+ const colorTransform = render_queue.subarray(colorTransformIndex, colorTransformIndex + 8);
150
161
  $context.applyFilter(node, `${filterKeyNumber}`, updated, width, height, isBitmap, matrix, colorTransform, displayObjectGetBlendModeService(blendMode), filterBounds, params);
151
162
  index += length;
152
163
  return index;
153
164
  }
154
- $context.globalAlpha = Math.min(Math.max(0, colorTransform[3] + colorTransform[7] / 255), 1);
165
+ $context.globalAlpha = Math.min(Math.max(0, render_queue[colorTransformIndex + 3] + render_queue[colorTransformIndex + 7] / 255), 1);
155
166
  $context.imageSmoothingEnabled = true;
156
167
  $context.globalCompositeOperation = displayObjectGetBlendModeService(blendMode);
157
168
  if (isBitmap && !isGridEnabled) {
158
- $context.setTransform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
159
- $context.drawDisplayObject(node, bounds[0], bounds[1], bounds[2], bounds[3], colorTransform);
169
+ $context.setTransform(a, b, c, d, e, f);
170
+ $context.drawDisplayObject(node, boundsXMin, boundsYMin, boundsXMax, boundsYMax, render_queue, colorTransformIndex);
160
171
  }
161
172
  else if (isCacheAsBitmap) {
162
173
  // cacheAsBitmap: Bitmapと同様の描画パスで、cacheScaleを補正
163
174
  // baseBounds原点(xMin,yMin)のスクリーン座標をtranslationに反映
164
- const screenX = matrix[0] * xMin + matrix[2] * yMin + matrix[4];
165
- const screenY = matrix[1] * xMin + matrix[3] * yMin + matrix[5];
166
- $context.setTransform(matrix[0] / xScale, matrix[1] / xScale, matrix[2] / yScale, matrix[3] / yScale, screenX, screenY);
167
- $context.drawDisplayObject(node, bounds[0], bounds[1], bounds[2], bounds[3], colorTransform);
175
+ const screenX = a * xMin + c * yMin + e;
176
+ const screenY = b * xMin + d * yMin + f;
177
+ $context.setTransform(a / xScale, b / xScale, c / yScale, d / yScale, screenX, screenY);
178
+ $context.drawDisplayObject(node, boundsXMin, boundsYMin, boundsXMax, boundsYMax, render_queue, colorTransformIndex);
168
179
  }
169
180
  else {
170
- const radianX = Math.atan2(matrix[1], matrix[0]);
171
- const radianY = Math.atan2(-matrix[2], matrix[3]);
181
+ const radianX = Math.atan2(b, a);
182
+ const radianY = Math.atan2(-c, d);
172
183
  if (radianX || radianY) {
173
184
  const tx = xMin * xScale;
174
185
  const ty = yMin * yScale;
175
186
  const cosX = Math.cos(radianX);
176
187
  const sinX = Math.sin(radianX);
177
- const cosY = Math.cos(radianY);
178
- const sinY = Math.sin(radianY);
179
- $context.setTransform(cosX, sinX, -sinY, cosY, tx * cosX - ty * sinY + matrix[4], tx * sinX + ty * cosY + matrix[5]);
188
+ // 両軸の角度が完全一致する場合だけ再利用し、せん断や符号付きゼロを保つ。
189
+ const sameAngle = Object.is(radianX, radianY);
190
+ const cosY = sameAngle ? cosX : Math.cos(radianY);
191
+ const sinY = sameAngle ? sinX : Math.sin(radianY);
192
+ $context.setTransform(cosX, sinX, -sinY, cosY, tx * cosX - ty * sinY + e, tx * sinX + ty * cosY + f);
180
193
  }
181
194
  else {
182
- $context.setTransform(1, 0, 0, 1, bounds[0], bounds[1]);
195
+ $context.setTransform(1, 0, 0, 1, boundsXMin, boundsYMin);
183
196
  }
184
197
  // 描画範囲をinstanced arrayに設定
185
- $context.drawDisplayObject(node, bounds[0], bounds[1], bounds[2], bounds[3], colorTransform);
198
+ $context.drawDisplayObject(node, boundsXMin, boundsYMin, boundsXMax, boundsYMax, render_queue, colorTransformIndex);
186
199
  }
187
200
  return index;
188
201
  };
@@ -8,8 +8,9 @@ import type { ITextSetting } from "../../interface/ITextSetting";
8
8
  * @param {object} text_setting
9
9
  * @param {number} x_scale
10
10
  * @param {number} y_scale
11
+ * @param {OffscreenCanvas | null} reusable_canvas - Caller-owned scratch, consumed before the next call
11
12
  * @return {OffscreenCanvas}
12
13
  * @method
13
14
  * @protected
14
15
  */
15
- export declare const execute: (text_data: ITextData | null, text_setting: ITextSetting, x_scale: number, y_scale: number) => OffscreenCanvas;
16
+ export declare const execute: (text_data: ITextData | null, text_setting: ITextSetting, x_scale: number, y_scale: number, reusable_canvas?: OffscreenCanvas | null) => OffscreenCanvas;
@@ -9,13 +9,28 @@ import { $intToRGBA } from "../../RendererUtil";
9
9
  * @param {object} text_setting
10
10
  * @param {number} x_scale
11
11
  * @param {number} y_scale
12
+ * @param {OffscreenCanvas | null} reusable_canvas - Caller-owned scratch, consumed before the next call
12
13
  * @return {OffscreenCanvas}
13
14
  * @method
14
15
  * @protected
15
16
  */
16
- export const execute = (text_data, text_setting, x_scale, y_scale) => {
17
- const canvas = new OffscreenCanvas(text_setting.width, text_setting.height);
18
- const context = canvas.getContext("2d");
17
+ export const execute = (text_data, text_setting, x_scale, y_scale, reusable_canvas = null) => {
18
+ let canvas = reusable_canvas;
19
+ if (canvas) {
20
+ // Even an unchanged width resets pixels, paths, clipping and drawing state.
21
+ canvas.width = text_setting.width;
22
+ if (canvas.height !== text_setting.height) {
23
+ canvas.height = text_setting.height;
24
+ }
25
+ }
26
+ else {
27
+ canvas = new OffscreenCanvas(text_setting.width, text_setting.height);
28
+ }
29
+ let context = canvas.getContext("2d");
30
+ if (reusable_canvas && (!context || context.isContextLost?.())) {
31
+ canvas = new OffscreenCanvas(text_setting.width, text_setting.height);
32
+ context = canvas.getContext("2d");
33
+ }
19
34
  if (!context) {
20
35
  return canvas;
21
36
  }
@@ -133,6 +148,9 @@ export const execute = (text_data, text_setting, x_scale, y_scale) => {
133
148
  let offsetHeight = 0;
134
149
  let offsetAlign = 0;
135
150
  let verticalAlign = 0;
151
+ let previousFillColor;
152
+ let fillStyle = "";
153
+ let fontStyle = "";
136
154
  let skip = false;
137
155
  let currentIndex = -1;
138
156
  for (let idx = 0; idx < text_data.textTable.length; ++idx) {
@@ -165,8 +183,13 @@ export const execute = (text_data, text_setting, x_scale, y_scale) => {
165
183
  }
166
184
  }
167
185
  // color setting
168
- const color = $intToRGBA(textFormat.color || 0);
169
- context.fillStyle = `rgba(${color.R},${color.G},${color.B},${color.A})`;
186
+ const fillColor = textFormat.color || 0;
187
+ if (fillColor !== previousFillColor) {
188
+ const color = $intToRGBA(fillColor);
189
+ fillStyle = `rgba(${color.R},${color.G},${color.B},${color.A})`;
190
+ context.fillStyle = fillStyle;
191
+ previousFillColor = fillColor;
192
+ }
170
193
  // focus line
171
194
  if (text_setting.focusVisible
172
195
  && text_setting.focusIndex === idx) {
@@ -192,7 +215,7 @@ export const execute = (text_data, text_setting, x_scale, y_scale) => {
192
215
  for (let idx = 0; idx < line; ++idx) {
193
216
  y += text_data.heightTable[idx];
194
217
  }
195
- context.strokeStyle = `rgba(${color.R},${color.G},${color.B},${color.A})`;
218
+ context.strokeStyle = fillStyle;
196
219
  context.beginPath();
197
220
  context.moveTo(x, y);
198
221
  context.lineTo(x, y - h);
@@ -223,14 +246,17 @@ export const execute = (text_data, text_setting, x_scale, y_scale) => {
223
246
  case "text":
224
247
  {
225
248
  context.beginPath();
226
- context.font = textFieldGenerateFontStyleService(textFormat);
249
+ const nextFontStyle = textFieldGenerateFontStyleService(textFormat);
250
+ if (nextFontStyle !== fontStyle) {
251
+ context.font = nextFontStyle;
252
+ fontStyle = nextFontStyle;
253
+ }
227
254
  const x = offsetWidth + offsetAlign;
228
255
  const y = offsetHeight + verticalAlign;
229
256
  if (textFormat.underline) {
230
257
  const color = $intToRGBA(textFormat.color || 0);
231
258
  context.lineWidth = lineWidth;
232
259
  context.strokeStyle = `rgba(${color.R},${color.G},${color.B},${color.A})`;
233
- context.beginPath();
234
260
  context.moveTo(x, y + 2);
235
261
  context.lineTo(x + textObject.w, y + 2);
236
262
  context.stroke();
@@ -1,4 +1,5 @@
1
1
  import { $cacheStore } from "@next2d/cache";
2
+ import { decodeTextData } from "@next2d/render-queue";
2
3
  import { execute as displayObjectGetBlendModeService } from "../../DisplayObject/service/DisplayObjectGetBlendModeService";
3
4
  import { execute as textFieldDrawOffscreenCanvasUseCase } from "./TextFieldDrawOffscreenCanvasUseCase";
4
5
  import { $context } from "../../RendererUtil";
@@ -7,6 +8,9 @@ import { $context } from "../../RendererUtil";
7
8
  * @private
8
9
  */
9
10
  const $textDecoder = new TextDecoder();
11
+ // drawElement consumes the canvas image synchronously on both backends.
12
+ // Keep at most one small scratch canvas; large rasters are not retained.
13
+ let $textCanvas = null;
10
14
  /**
11
15
  * @description TextFieldの描画を実行します。
12
16
  * Execute the drawing of TextField.
@@ -20,7 +24,7 @@ const $textDecoder = new TextDecoder();
20
24
  export const execute = (render_queue, index) => {
21
25
  const matrix = render_queue.subarray(index, index + 6);
22
26
  index += 6;
23
- const colorTransform = render_queue.subarray(index, index + 8);
27
+ const colorTransformIndex = index;
24
28
  index += 8;
25
29
  const bounds = render_queue.subarray(index, index + 4);
26
30
  index += 4;
@@ -52,12 +56,18 @@ export const execute = (render_queue, index) => {
52
56
  }
53
57
  else {
54
58
  // TextFieldResetUseCase 等で Main 側のみ wipe された場合、
55
- // Worker には旧 Node が残っているため、新規作成前に解放してアトラスリーク防止
59
+ // Worker の旧 Node は同寸法なら保持し、寸法変更時は解放して作り直す
56
60
  const oldNode = $cacheStore.get(uniqueKey, cacheKey);
57
- if (oldNode) {
58
- $context.removeNode(oldNode);
61
+ if (oldNode && oldNode.w === width && oldNode.h === height
62
+ && $context.reuseNode(oldNode)) {
63
+ node = oldNode;
64
+ }
65
+ else {
66
+ if (oldNode) {
67
+ $context.removeNode(oldNode);
68
+ }
69
+ node = $context.createNode(width, height);
59
70
  }
60
- node = $context.createNode(width, height);
61
71
  $cacheStore.set(uniqueKey, cacheKey, node);
62
72
  }
63
73
  const length = render_queue[index++];
@@ -104,21 +114,24 @@ export const execute = (render_queue, index) => {
104
114
  "defaultColor": render_queue[index++],
105
115
  "defaultSize": render_queue[index++]
106
116
  };
107
- const canvas = textFieldDrawOffscreenCanvasUseCase(JSON.parse($textDecoder.decode(buffer)), textSetting, xScale, yScale);
108
- // fixed logic
109
- const currentAttachment = $context.currentAttachmentObject;
110
- const atlasAttachment = $context.atlasAttachmentObject;
111
- if (atlasAttachment) {
112
- $context.bind(atlasAttachment);
113
- }
114
- $context.reset();
115
- $context.beginNodeRendering(node);
116
- const offsetY = atlasAttachment ? atlasAttachment.height - node.y - height : 0;
117
- $context.setTransform(1, 0, 0, 1, node.x, offsetY);
118
- $context.drawElement(node, canvas);
119
- $context.endNodeRendering();
120
- if (currentAttachment) {
121
- $context.bind(currentAttachment);
117
+ const canvas = textFieldDrawOffscreenCanvasUseCase(decodeTextData($textDecoder.decode(buffer)), textSetting, xScale, yScale, $textCanvas);
118
+ $textCanvas = width * height <= 1024 * 1024 ? canvas : null;
119
+ if (!("queueTextElement" in $context) || !$context.queueTextElement(node, canvas)) {
120
+ // fixed logic
121
+ const currentAttachment = $context.currentAttachmentObject;
122
+ const atlasAttachment = $context.atlasAttachmentObject;
123
+ if (atlasAttachment) {
124
+ $context.bind(atlasAttachment);
125
+ }
126
+ $context.reset();
127
+ $context.beginNodeRendering(node);
128
+ const offsetY = atlasAttachment ? atlasAttachment.height - node.y - height : 0;
129
+ $context.setTransform(1, 0, 0, 1, node.x, offsetY);
130
+ $context.drawElement(node, canvas);
131
+ $context.endNodeRendering();
132
+ if (currentAttachment) {
133
+ $context.bind(currentAttachment);
134
+ }
122
135
  }
123
136
  }
124
137
  else {
@@ -138,11 +151,12 @@ export const execute = (render_queue, index) => {
138
151
  const params = render_queue.subarray(index, index + length);
139
152
  const width = Math.ceil(Math.abs(bounds[2] - bounds[0]));
140
153
  const height = Math.ceil(Math.abs(bounds[3] - bounds[1]));
154
+ const colorTransform = render_queue.subarray(colorTransformIndex, colorTransformIndex + 8);
141
155
  $context.applyFilter(node, `${filterKeyNumber}`, Boolean(Math.max(+changed, +updated)), width, height, false, matrix, colorTransform, displayObjectGetBlendModeService(blendMode), filterBounds, params);
142
156
  index += length;
143
157
  return index;
144
158
  }
145
- $context.globalAlpha = Math.min(Math.max(0, colorTransform[3] + colorTransform[7] / 255), 1);
159
+ $context.globalAlpha = Math.min(Math.max(0, render_queue[colorTransformIndex + 3] + render_queue[colorTransformIndex + 7] / 255), 1);
146
160
  $context.imageSmoothingEnabled = true;
147
161
  $context.globalCompositeOperation = displayObjectGetBlendModeService(blendMode);
148
162
  if (isCacheAsBitmap) {
@@ -169,6 +183,6 @@ export const execute = (render_queue, index) => {
169
183
  }
170
184
  }
171
185
  // 描画範囲をinstanced arrayに設定
172
- $context.drawDisplayObject(node, bounds[0], bounds[1], bounds[2], bounds[3], colorTransform);
186
+ $context.drawDisplayObject(node, bounds[0], bounds[1], bounds[2], bounds[3], render_queue, colorTransformIndex);
173
187
  return index;
174
188
  };