@swf2es/web 0.1.28 → 0.1.29
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/config.d.ts +7 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/element.d.ts +49 -1
- package/dist/element.d.ts.map +1 -1
- package/dist/element.js +228 -5
- package/dist/element.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/meter.d.ts +68 -0
- package/dist/meter.d.ts.map +1 -0
- package/dist/meter.js +408 -0
- package/dist/meter.js.map +1 -0
- package/dist/playback.d.ts +37 -0
- package/dist/playback.d.ts.map +1 -1
- package/dist/playback.js +43 -1
- package/dist/playback.js.map +1 -1
- package/package.json +5 -5
- package/src/config.ts +7 -0
- package/src/element.ts +274 -2
- package/src/index.ts +2 -0
- package/src/meter.ts +484 -0
- package/src/playback.ts +80 -1
package/src/meter.ts
ADDED
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
// The performance meter the element shows over its player, off until the
|
|
2
|
+
// viewer turns it on (the context menu's "Show performance", or the page,
|
|
3
|
+
// through `performanceMeter`): twice a second, the SWF's frames played a
|
|
4
|
+
// second against its frame rate, the player's ms in a frame (its scripts
|
|
5
|
+
// and its drawing, as the frame listener tells them), the draws of a frame
|
|
6
|
+
// the player drew and the WebGL calls that made them, the JS heap where the
|
|
7
|
+
// browser tells it (Chromium's performance.memory), and the WebAssembly
|
|
8
|
+
// memories behind the player. Off, it costs nothing: it is not even made
|
|
9
|
+
// until first shown, and once hidden it hears no frames, keeps no timer and
|
|
10
|
+
// leaves the context its own functions.
|
|
11
|
+
import type { FrameStats } from "./playback.js";
|
|
12
|
+
|
|
13
|
+
interface MemoryInfo {
|
|
14
|
+
usedJSHeapSize: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type GL = WebGLRenderingContext | WebGL2RenderingContext;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Its look, which a page may give its own through custom properties on the
|
|
21
|
+
* element (font, color, background, border and muted, the labels' colour),
|
|
22
|
+
* as the demo site gives its theme's; by default the browser's light or dark
|
|
23
|
+
* colours, as the context menu's.
|
|
24
|
+
*/
|
|
25
|
+
export const METER_STYLE =
|
|
26
|
+
" .meter { position: absolute; top: 6px; left: 6px; z-index: 3; display: grid;" +
|
|
27
|
+
" grid-template-columns: max-content max-content; gap: 0 8px; box-sizing: border-box;" +
|
|
28
|
+
" max-width: calc(100% - 12px); max-height: calc(100% - 12px); overflow: hidden;" +
|
|
29
|
+
" margin: 0; padding: 3px 7px;" +
|
|
30
|
+
" font: var(--swf2es-meter-font, 11px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);" +
|
|
31
|
+
" font-variant-numeric: tabular-nums; color-scheme: light dark;" +
|
|
32
|
+
" color: var(--swf2es-meter-color, CanvasText); background: var(--swf2es-meter-background, Canvas);" +
|
|
33
|
+
" border: 1px solid var(--swf2es-meter-border, GrayText); border-radius: 4px;" +
|
|
34
|
+
" pointer-events: none; user-select: none }" +
|
|
35
|
+
" .meter[data-corner$=right] { left: auto; right: 6px }" +
|
|
36
|
+
" .meter[data-corner^=bottom] { top: auto; bottom: 6px }" +
|
|
37
|
+
" .meter[hidden], .meter > div[hidden] { display: none }" +
|
|
38
|
+
" .meter > div { display: contents }" +
|
|
39
|
+
" .meter dt { color: var(--swf2es-meter-muted, GrayText) }" +
|
|
40
|
+
" .meter dd { margin: 0; text-align: right }";
|
|
41
|
+
|
|
42
|
+
/** Each figure's name, in the dd's `data-figure`, and its label. */
|
|
43
|
+
const FIGURES = [
|
|
44
|
+
["fps", "fps"],
|
|
45
|
+
["frame", "frame"],
|
|
46
|
+
["sub-draws", "sub-draws"],
|
|
47
|
+
["gl-calls", "GL calls"],
|
|
48
|
+
["heap", "JS heap"],
|
|
49
|
+
["codegen", "codegen"],
|
|
50
|
+
["native", "native"],
|
|
51
|
+
] as const;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The draw functions counted, each with where its count of draws is: -1 for
|
|
55
|
+
* one, else the index of its last argument, a multi-draw's drawcount.
|
|
56
|
+
*/
|
|
57
|
+
const DRAWS: [string, number][] = [
|
|
58
|
+
["drawArrays", -1],
|
|
59
|
+
["drawElements", -1],
|
|
60
|
+
["drawArraysInstanced", -1],
|
|
61
|
+
["drawElementsInstanced", -1],
|
|
62
|
+
];
|
|
63
|
+
const MULTI_DRAWS: [string, number][] = [
|
|
64
|
+
["multiDrawArraysWEBGL", 5],
|
|
65
|
+
["multiDrawElementsWEBGL", 6],
|
|
66
|
+
["multiDrawArraysInstancedWEBGL", 7],
|
|
67
|
+
["multiDrawElementsInstancedWEBGL", 8],
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
/** The calls of the loop kept: a second's at 500 a second, more than any display's rate. */
|
|
71
|
+
const KEPT = 512;
|
|
72
|
+
/** The span the figures cover, and how often they are shown, in ms. */
|
|
73
|
+
const SPAN = 1000;
|
|
74
|
+
const EVERY = 500;
|
|
75
|
+
const MB = 1024 * 1024;
|
|
76
|
+
/** Where the viewer's choice is kept for every element of the origin, "1" for on. */
|
|
77
|
+
const STORED = "swf2es.performanceMeter";
|
|
78
|
+
/** Where the viewer's choice of corner is kept, likewise. */
|
|
79
|
+
const STORED_CORNER = "swf2es.performanceMeterCorner";
|
|
80
|
+
|
|
81
|
+
/** The element's corners the meter may sit in, in the order the context menu moves it through. */
|
|
82
|
+
export const METER_CORNERS = ["top-left", "top-right", "bottom-left", "bottom-right"] as const;
|
|
83
|
+
export type MeterCorner = (typeof METER_CORNERS)[number];
|
|
84
|
+
|
|
85
|
+
export function isMeterCorner(value: unknown): value is MeterCorner {
|
|
86
|
+
return METER_CORNERS.includes(value as MeterCorner);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The corner after `corner`, the first after the last. */
|
|
90
|
+
export function nextCorner(corner: MeterCorner): MeterCorner {
|
|
91
|
+
return METER_CORNERS[(METER_CORNERS.indexOf(corner) + 1) % METER_CORNERS.length];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The viewer's corner as last kept: top-left where none is, or storage is refused. */
|
|
95
|
+
export function storedCorner(): MeterCorner {
|
|
96
|
+
try {
|
|
97
|
+
const corner = localStorage.getItem(STORED_CORNER);
|
|
98
|
+
return isMeterCorner(corner) ? corner : "top-left";
|
|
99
|
+
} catch {
|
|
100
|
+
return "top-left";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function storeCorner(corner: MeterCorner): void {
|
|
105
|
+
try {
|
|
106
|
+
if (corner === "top-left") {
|
|
107
|
+
localStorage.removeItem(STORED_CORNER);
|
|
108
|
+
} else {
|
|
109
|
+
localStorage.setItem(STORED_CORNER, corner);
|
|
110
|
+
}
|
|
111
|
+
} catch {
|
|
112
|
+
// Kept for this page alone.
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The viewer's choice as last kept: off where none is, or storage is refused. */
|
|
117
|
+
export function storedMeter(): boolean {
|
|
118
|
+
try {
|
|
119
|
+
return localStorage.getItem(STORED) === "1";
|
|
120
|
+
} catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function storeMeter(on: boolean): void {
|
|
126
|
+
try {
|
|
127
|
+
if (on) {
|
|
128
|
+
localStorage.setItem(STORED, "1");
|
|
129
|
+
} else {
|
|
130
|
+
localStorage.removeItem(STORED);
|
|
131
|
+
}
|
|
132
|
+
} catch {
|
|
133
|
+
// Kept for this page alone.
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The draw calls made on a context, counted by wrapping its draw functions
|
|
139
|
+
* only while the meter shows: off, and before a context is given, the
|
|
140
|
+
* context has its own functions back, its canvas none of the counter's
|
|
141
|
+
* listeners, and the count costs nothing. A multi-draw is one call and the
|
|
142
|
+
* draws it makes, as the GPU sees them. The renderer counts neither itself.
|
|
143
|
+
*/
|
|
144
|
+
class DrawCounter {
|
|
145
|
+
draws = 0;
|
|
146
|
+
calls = 0;
|
|
147
|
+
private gl: GL | null = null;
|
|
148
|
+
private shown = false;
|
|
149
|
+
private readonly restores: (() => void)[] = [];
|
|
150
|
+
|
|
151
|
+
/** Count `gl`'s draws, while shown; null for none. */
|
|
152
|
+
attach(gl: GL | null): void {
|
|
153
|
+
if (gl === this.gl) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
this.listen(false);
|
|
158
|
+
this.unwrap();
|
|
159
|
+
this.gl = gl;
|
|
160
|
+
if (this.shown) {
|
|
161
|
+
this.listen(true);
|
|
162
|
+
this.wrap();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
show(shown: boolean): void {
|
|
167
|
+
if (shown === this.shown) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.shown = shown;
|
|
172
|
+
this.listen(shown);
|
|
173
|
+
if (shown) {
|
|
174
|
+
this.wrap();
|
|
175
|
+
} else {
|
|
176
|
+
this.unwrap();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Hear the context lost and given back, or no longer. */
|
|
181
|
+
private listen(on: boolean): void {
|
|
182
|
+
const canvas = this.gl?.canvas;
|
|
183
|
+
if (!canvas) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (on) {
|
|
188
|
+
canvas.addEventListener("webglcontextlost", this.lost);
|
|
189
|
+
canvas.addEventListener("webglcontextrestored", this.restored);
|
|
190
|
+
} else {
|
|
191
|
+
canvas.removeEventListener("webglcontextlost", this.lost);
|
|
192
|
+
canvas.removeEventListener("webglcontextrestored", this.restored);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private readonly lost = () => this.unwrap();
|
|
197
|
+
// The renderer's own listener, added before this one, has fetched the extensions anew.
|
|
198
|
+
private readonly restored = () => this.wrap();
|
|
199
|
+
|
|
200
|
+
private wrap(): void {
|
|
201
|
+
const gl = this.gl;
|
|
202
|
+
this.unwrap();
|
|
203
|
+
if (!gl || !this.shown || gl.isContextLost()) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
for (const [name, drawcount] of DRAWS) {
|
|
208
|
+
this.patch(gl, name, drawcount);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const multi = gl.getExtension("WEBGL_multi_draw");
|
|
212
|
+
if (multi) {
|
|
213
|
+
for (const [name, drawcount] of MULTI_DRAWS) {
|
|
214
|
+
this.patch(multi, name, drawcount);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private unwrap(): void {
|
|
220
|
+
for (const restore of this.restores.splice(0)) {
|
|
221
|
+
restore();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Wrap `target[name]`, to be put back as it was, a property of its own or
|
|
227
|
+
* its prototype's: with as many arguments as the widest multi-draw takes,
|
|
228
|
+
* the rest undefined, which WebGL ignores, so that a draw allocates no
|
|
229
|
+
* array of its arguments.
|
|
230
|
+
*/
|
|
231
|
+
private patch(target: object, name: string, drawcount: number): void {
|
|
232
|
+
const functions = target as Record<string, unknown>;
|
|
233
|
+
const original = functions[name] as (...args: unknown[]) => unknown;
|
|
234
|
+
if (typeof original !== "function") {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const own = Object.getOwnPropertyDescriptor(target, name);
|
|
239
|
+
functions[name] = (
|
|
240
|
+
a: unknown,
|
|
241
|
+
b: unknown,
|
|
242
|
+
c: unknown,
|
|
243
|
+
d: unknown,
|
|
244
|
+
e: unknown,
|
|
245
|
+
f: unknown,
|
|
246
|
+
g: unknown,
|
|
247
|
+
h: unknown,
|
|
248
|
+
i: unknown,
|
|
249
|
+
) => {
|
|
250
|
+
this.calls++;
|
|
251
|
+
this.draws +=
|
|
252
|
+
drawcount === 5
|
|
253
|
+
? (f as number)
|
|
254
|
+
: drawcount === 6
|
|
255
|
+
? (g as number)
|
|
256
|
+
: drawcount === 7
|
|
257
|
+
? (h as number)
|
|
258
|
+
: drawcount === 8
|
|
259
|
+
? (i as number)
|
|
260
|
+
: 1;
|
|
261
|
+
return original.call(target, a, b, c, d, e, f, g, h, i);
|
|
262
|
+
};
|
|
263
|
+
this.restores.push(() => {
|
|
264
|
+
if (own) {
|
|
265
|
+
Object.defineProperty(target, name, own);
|
|
266
|
+
} else {
|
|
267
|
+
delete functions[name];
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export class PerformanceMeter {
|
|
274
|
+
private readonly root: HTMLDListElement;
|
|
275
|
+
private readonly cells = new Map<string, HTMLElement>();
|
|
276
|
+
private shown = false;
|
|
277
|
+
/** Whether a player plays, whose frames the meter hears. */
|
|
278
|
+
private playing = false;
|
|
279
|
+
private timer: ReturnType<typeof setInterval> | undefined;
|
|
280
|
+
private readonly counter = new DrawCounter();
|
|
281
|
+
/** The loop's last KEPT calls, a ring: each one's time, frames played, and ms (-1 for no work). */
|
|
282
|
+
private readonly times = new Float64Array(KEPT);
|
|
283
|
+
private readonly frames = new Float64Array(KEPT);
|
|
284
|
+
private readonly costs = new Float64Array(KEPT);
|
|
285
|
+
private readonly sorted = new Float64Array(KEPT);
|
|
286
|
+
private next = 0;
|
|
287
|
+
private count = 0;
|
|
288
|
+
/** When the calls counted began: shown, or a SWF started, whichever came last. */
|
|
289
|
+
private since = 0;
|
|
290
|
+
/** The SWF's frame rate as of the last call, 0 before one. */
|
|
291
|
+
private rate = 0;
|
|
292
|
+
/** The draws, and the calls that made them, of the frames that drew since the last showing. */
|
|
293
|
+
private draws = 0;
|
|
294
|
+
private calls = 0;
|
|
295
|
+
private drawnFrames = 0;
|
|
296
|
+
|
|
297
|
+
/** Shown in `parent`, the element's shadow root; `memory` is the element's wasmMemory. */
|
|
298
|
+
constructor(
|
|
299
|
+
parent: Node,
|
|
300
|
+
private readonly memory: () => { codegen: number; native: number },
|
|
301
|
+
) {
|
|
302
|
+
this.root = document.createElement("dl");
|
|
303
|
+
this.root.className = "meter";
|
|
304
|
+
this.root.dataset.corner = "top-left";
|
|
305
|
+
this.root.hidden = true;
|
|
306
|
+
this.root.setAttribute("aria-label", "Performance");
|
|
307
|
+
for (const [name, label] of FIGURES) {
|
|
308
|
+
const row = document.createElement("div");
|
|
309
|
+
const dt = document.createElement("dt");
|
|
310
|
+
dt.textContent = label;
|
|
311
|
+
const dd = document.createElement("dd");
|
|
312
|
+
dd.dataset.figure = name;
|
|
313
|
+
dd.textContent = "–";
|
|
314
|
+
row.append(dt, dd);
|
|
315
|
+
this.root.append(row);
|
|
316
|
+
this.cells.set(name, dd);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
parent.appendChild(this.root);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Put the meter in `corner` of the element. */
|
|
323
|
+
place(corner: MeterCorner): void {
|
|
324
|
+
this.root.dataset.corner = corner;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Show the meter and measure, or hide it and stop. */
|
|
328
|
+
show(on: boolean): void {
|
|
329
|
+
if (on === this.shown) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
this.shown = on;
|
|
334
|
+
this.root.hidden = !on;
|
|
335
|
+
this.counter.show(on);
|
|
336
|
+
if (on) {
|
|
337
|
+
this.reset();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
this.tick();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** A player begun, drawing with `gl`, or none playing: what was counted before is let go. */
|
|
344
|
+
attach(gl: GL | null, playing: boolean): void {
|
|
345
|
+
this.counter.attach(gl);
|
|
346
|
+
this.playing = playing;
|
|
347
|
+
this.reset();
|
|
348
|
+
this.tick();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** The timer runs while the meter shows a player's figures; it shows them at once as it starts. */
|
|
352
|
+
private tick(): void {
|
|
353
|
+
const run = this.shown && this.playing;
|
|
354
|
+
if (run && this.timer === undefined) {
|
|
355
|
+
this.timer = setInterval(this.update, EVERY);
|
|
356
|
+
} else if (!run && this.timer !== undefined) {
|
|
357
|
+
clearInterval(this.timer);
|
|
358
|
+
this.timer = undefined;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (this.shown) {
|
|
362
|
+
this.update();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private reset(): void {
|
|
367
|
+
this.next = this.count = 0;
|
|
368
|
+
this.since = performance.now();
|
|
369
|
+
this.rate = 0;
|
|
370
|
+
this.counter.draws = this.counter.calls = 0;
|
|
371
|
+
this.draws = this.calls = this.drawnFrames = 0;
|
|
372
|
+
this.text("sub-draws", "–");
|
|
373
|
+
this.text("gl-calls", "–");
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Each call of the player's loop, kept in the ring without allocating. */
|
|
377
|
+
readonly heard = (stats: FrameStats): void => {
|
|
378
|
+
const i = this.next;
|
|
379
|
+
this.next = i + 1 === KEPT ? 0 : i + 1;
|
|
380
|
+
if (this.count < KEPT) {
|
|
381
|
+
this.count++;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
this.times[i] = stats.time;
|
|
385
|
+
this.frames[i] = stats.frames;
|
|
386
|
+
this.costs[i] = stats.frames > 0 || stats.drew ? stats.play + stats.draw : -1;
|
|
387
|
+
this.rate = stats.frameRate;
|
|
388
|
+
// What the context was asked since the last call belongs to the frame this one drew.
|
|
389
|
+
if (stats.drew && this.counter.calls > 0) {
|
|
390
|
+
this.draws += this.counter.draws;
|
|
391
|
+
this.calls += this.counter.calls;
|
|
392
|
+
this.drawnFrames++;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
this.counter.draws = this.counter.calls = 0;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
private readonly update = () => {
|
|
399
|
+
const now = performance.now();
|
|
400
|
+
const from = Math.max(now - SPAN, this.since);
|
|
401
|
+
let played = 0;
|
|
402
|
+
let working = 0;
|
|
403
|
+
// From the newest back, to the span's start.
|
|
404
|
+
for (let k = 0, i = this.next; k < this.count; k++) {
|
|
405
|
+
i = i === 0 ? KEPT - 1 : i - 1;
|
|
406
|
+
if (this.times[i] < from) {
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
played += this.frames[i];
|
|
411
|
+
if (this.costs[i] >= 0) {
|
|
412
|
+
this.insert(working++, this.costs[i]);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const span = now - from;
|
|
417
|
+
const playing = this.playing && this.rate > 0;
|
|
418
|
+
this.text(
|
|
419
|
+
"fps",
|
|
420
|
+
playing && span >= EVERY / 2
|
|
421
|
+
? `${((played * 1000) / span).toFixed(1)} / ${rateText(this.rate)}`
|
|
422
|
+
: "–",
|
|
423
|
+
);
|
|
424
|
+
this.text(
|
|
425
|
+
"frame",
|
|
426
|
+
playing && working > 0
|
|
427
|
+
? `${median(this.sorted, working).toFixed(1)} ms, max ${this.sorted[working - 1].toFixed(1)}`
|
|
428
|
+
: "–",
|
|
429
|
+
);
|
|
430
|
+
// A still stage draws nothing: the last frame drawn stands.
|
|
431
|
+
if (this.drawnFrames > 0) {
|
|
432
|
+
this.text("sub-draws", String(Math.round(this.draws / this.drawnFrames)));
|
|
433
|
+
this.text("gl-calls", String(Math.round(this.calls / this.drawnFrames)));
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const heap = (performance as { memory?: MemoryInfo }).memory;
|
|
437
|
+
const heapRow = this.cells.get("heap")?.parentElement;
|
|
438
|
+
if (heapRow) {
|
|
439
|
+
heapRow.hidden = !heap;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (heap) {
|
|
443
|
+
this.text("heap", megabytes(heap.usedJSHeapSize));
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const wasm = this.memory();
|
|
447
|
+
this.text("codegen", wasm.codegen > 0 ? megabytes(wasm.codegen) : "–");
|
|
448
|
+
this.text("native", megabytes(wasm.native));
|
|
449
|
+
this.draws = this.calls = this.drawnFrames = 0;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
private text(figure: string, text: string): void {
|
|
453
|
+
const cell = this.cells.get(figure);
|
|
454
|
+
if (cell && cell.textContent !== text) {
|
|
455
|
+
cell.textContent = text;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** Put `cost` among the first `n` sorted, by insertion: a second's few hundred, twice a second. */
|
|
460
|
+
private insert(n: number, cost: number): void {
|
|
461
|
+
const sorted = this.sorted;
|
|
462
|
+
let j = n;
|
|
463
|
+
while (j > 0 && sorted[j - 1] > cost) {
|
|
464
|
+
sorted[j] = sorted[j - 1];
|
|
465
|
+
j--;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
sorted[j] = cost;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function median(sorted: Float64Array, n: number): number {
|
|
473
|
+
const middle = n >> 1;
|
|
474
|
+
return n % 2 === 1 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** A frame rate as a SWF declares it, whole or to the hundredth: its 8.8 fixed point. */
|
|
478
|
+
function rateText(rate: number): string {
|
|
479
|
+
return `${Number.isInteger(rate) ? rate : Number(rate.toFixed(2))} fps`;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function megabytes(bytes: number): string {
|
|
483
|
+
return `${(bytes / MB).toFixed(bytes < 10 * MB ? 1 : 0)} MB`;
|
|
484
|
+
}
|
package/src/playback.ts
CHANGED
|
@@ -31,7 +31,52 @@ export interface Look {
|
|
|
31
31
|
bgcolor: number | null;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* What the player did in one call of its loop, as a frame listener hears it
|
|
36
|
+
* (`Swf2esPlayerElement.frameListener`): a call each animation frame, or
|
|
37
|
+
* from the timer that plays a hidden page's frames.
|
|
38
|
+
*/
|
|
39
|
+
export interface FrameStats {
|
|
40
|
+
/** The call's time, by performance.now()'s clock: the animation frame's, or the timer's. */
|
|
41
|
+
time: number;
|
|
42
|
+
/** The SWF's frames it played: none where the display's frame fell between two of the SWF's. */
|
|
43
|
+
frames: number;
|
|
44
|
+
/** The SWF's frame rate as it played them, which a script may change. */
|
|
45
|
+
frameRate: number;
|
|
46
|
+
/** The ms the frames took: their scripts, events, timeline and sound, the SWF's own work. */
|
|
47
|
+
play: number;
|
|
48
|
+
/**
|
|
49
|
+
* The ms the rest of the call took on the CPU where it drew: drawing the stage, the GPU's own
|
|
50
|
+
* work aside, and the little before it (the text input's focus kept, the samples chosen anew
|
|
51
|
+
* for a changed quality); 0 where it did not draw.
|
|
52
|
+
*/
|
|
53
|
+
draw: number;
|
|
54
|
+
/** Whether it drew the stage: a coarse clock may time a short drawing as 0 ms. */
|
|
55
|
+
drew: boolean;
|
|
56
|
+
/** Whether it was an animation frame, which draws, rather than the timer, which never does. */
|
|
57
|
+
shown: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Hears what the player did in each call of its loop, given the same object each time. */
|
|
61
|
+
export type FrameListener = (stats: FrameStats) => void;
|
|
62
|
+
|
|
34
63
|
export class Playback {
|
|
64
|
+
/**
|
|
65
|
+
* Told after every call of the loop, while set: one listener, which
|
|
66
|
+
* another replaces; unset, the loop measures nothing for it. Its time is
|
|
67
|
+
* not the player's: the frames' pacing counts only the frames' own.
|
|
68
|
+
*/
|
|
69
|
+
onFrame: FrameListener | null = null;
|
|
70
|
+
/** What onFrame hears, filled anew for each call. */
|
|
71
|
+
private readonly stats: FrameStats = {
|
|
72
|
+
time: 0,
|
|
73
|
+
frames: 0,
|
|
74
|
+
frameRate: 0,
|
|
75
|
+
play: 0,
|
|
76
|
+
draw: 0,
|
|
77
|
+
drew: false,
|
|
78
|
+
shown: false,
|
|
79
|
+
};
|
|
35
80
|
private frame = 0;
|
|
36
81
|
private last: number | null = null;
|
|
37
82
|
/**
|
|
@@ -204,8 +249,9 @@ export class Playback {
|
|
|
204
249
|
}
|
|
205
250
|
|
|
206
251
|
const start = performance.now();
|
|
252
|
+
let played = 0;
|
|
207
253
|
try {
|
|
208
|
-
this.player.advance(dt, this.busy);
|
|
254
|
+
played = this.player.advance(dt, this.busy);
|
|
209
255
|
} catch (error) {
|
|
210
256
|
this.report(error);
|
|
211
257
|
}
|
|
@@ -218,8 +264,41 @@ export class Playback {
|
|
|
218
264
|
this.requality();
|
|
219
265
|
}
|
|
220
266
|
|
|
267
|
+
let drew = false;
|
|
221
268
|
if (show && (this.player.changes !== this.drawn || this.dirty)) {
|
|
222
269
|
this.draw();
|
|
270
|
+
drew = true;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// A frame's script may have destroyed the player: there is nothing left to tell of.
|
|
274
|
+
if (this.onFrame && !this.destroyed) {
|
|
275
|
+
this.told(this.onFrame, now, played, start, drew, show);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Tell `listener` of the call that began at `start`, its frames' part `this.busy`. */
|
|
280
|
+
private told(
|
|
281
|
+
listener: FrameListener,
|
|
282
|
+
now: number,
|
|
283
|
+
frames: number,
|
|
284
|
+
start: number,
|
|
285
|
+
drew: boolean,
|
|
286
|
+
shown: boolean,
|
|
287
|
+
): void {
|
|
288
|
+
const end = performance.now();
|
|
289
|
+
const stats = this.stats;
|
|
290
|
+
stats.time = now;
|
|
291
|
+
stats.frames = frames;
|
|
292
|
+
stats.frameRate = this.player.frameRate;
|
|
293
|
+
stats.play = this.busy;
|
|
294
|
+
stats.draw = drew ? end - start - this.busy : 0;
|
|
295
|
+
stats.drew = drew;
|
|
296
|
+
stats.shown = shown;
|
|
297
|
+
// A listener that throws is the page's error, not the loop's: the loop goes on.
|
|
298
|
+
try {
|
|
299
|
+
listener(stats);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
reportError(error);
|
|
223
302
|
}
|
|
224
303
|
}
|
|
225
304
|
|