Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass correct fps to devtools #682

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions devtools/web/src/devtools-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ export class DevtoolsManager {
*/
private _bufferedData = new BufferedRuntimeData();

private _fpsBuffer : number[] = [0,0,0,0,0,0,0,0,0,0];
private _fpsBufferIdx = 0;
//calculate an average FPS for the last 10 frames
private _calcAvgFPS = () => {
let sum = this._fpsBuffer[0];
for (let i = 1; i < 10; i++)
sum += this._fpsBuffer[i];
return Math.floor(sum / 10);
}
private _nextFPSBufferIdx = () => {
(this._fpsBufferIdx == 9) ? this._fpsBufferIdx = 0 : this._fpsBufferIdx++;
return this._fpsBufferIdx;
}

/**
* Notifies the devtools that the web runtime has completed an update.
*/
Expand All @@ -66,12 +80,12 @@ export class DevtoolsManager {
deltaFrame: number
) => {
if (this._enabled) {
const fps = Math.floor(1_000 / deltaFrame);
this._fpsBuffer[this._nextFPSBufferIdx()] = 1_000 / deltaFrame;
this._bufferedData.update(runtimeInfo.data);
this._notifyUpdateCompleted(
runtimeInfo.data,
runtimeInfo.wasmBufferByteLen,
fps
this._calcAvgFPS()
);
}
};
Expand Down
7 changes: 5 additions & 2 deletions runtimes/web/src/ui/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ export class App extends LitElement {

// When we should perform the next update
let timeNextUpdate = performance.now();
// Track the timestamp of the last frame
let lastTimeFrameStart = timeNextUpdate;

const onFrame = (timeFrameStart: number) => {
requestAnimationFrame(onFrame);
Expand Down Expand Up @@ -480,8 +482,9 @@ export class App extends LitElement {
runtime.composite();

if (import.meta.env.DEV) {
// FIXME(2022-08-20): Pass the correct FPS for display
devtoolsManager.updateCompleted(runtime, 1000/60);
// FIXED(2023-12-13): Pass the correct FPS for display
devtoolsManager.updateCompleted(runtime, timeFrameStart - lastTimeFrameStart);
lastTimeFrameStart = timeFrameStart;
}
}
}
Expand Down
Loading