Skip to content

Commit

Permalink
Debug UI (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmoebaChant authored Sep 18, 2024
1 parent 3d4f4ad commit 3e61728
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
33 changes: 32 additions & 1 deletion packages/demo/src/fhl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,47 @@ import { Observable } from "@babylonjs/core/Misc/observable";
// Read page elements
const likeButton = document.getElementById("likeButton") as HTMLButtonElement;
const outputCanvas = document.getElementById("outputCanvas") as HTMLCanvasElement;
const debugUi = document.getElementById("debugUi") as HTMLDivElement;
const avgProcessingTimeDiv = document.getElementById("avgProcessingTime") as HTMLDivElement;
const fpsDiv = document.getElementById("fps") as HTMLDivElement;

// Register button click handlers
const onLikeClickedObservable = new Observable<void>();
likeButton.addEventListener("click", () => {
onLikeClickedObservable.notifyObservers();
});

// Debug UI update code
const onNewAverageFrameProcessingValue: Observable<number> = new Observable<number>();
onNewAverageFrameProcessingValue.add((value) => {
avgProcessingTimeDiv.innerText = `${value.toFixed(2)}ms`;
});
const onNewFpsValue: Observable<number> = new Observable<number>();
onNewFpsValue.add((value) => {
fpsDiv.innerText = value.toFixed(2);
});

// Initialize the SmartFilter Video App
console.log("Initializing SmartFilter Video App...");
const videoApp = new SmartFilterVideoApp(outputCanvas, onLikeClickedObservable);
const videoApp = new SmartFilterVideoApp(
outputCanvas,
onLikeClickedObservable,
onNewAverageFrameProcessingValue,
onNewFpsValue
);

// Set up hidden keystroke approach to showing the debug UI
document.addEventListener("keydown", (e) => {
if (debugUi.style.display === "none") {
if (e.key === "d") {
debugUi.style.display = "block";
}
} else {
if (e.key === "Escape") {
debugUi.style.display = "none";
}
}
});

/**
* Main function to initialize the app.
Expand Down
39 changes: 36 additions & 3 deletions packages/demo/src/fhl/smartFilterVideoApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import { ReactionsSmartFilter } from "./reactionsSmartFilter";
import type { IEffect } from "./effects/IEffect";
import { LikeEffect } from "./effects/likeEffect";
import type { Observable } from "@babylonjs/core/Misc/observable";
import { PerfCounter } from "@babylonjs/core/Misc/perfCounter";

export const SMART_FILTER_EFFECT_ID = "f71bd30b-c5e9-48ff-b039-42bc19df95a8";
export const LOCAL_SMART_FILTER_EFFECT_ID = "fb9f0fab-9eb9-4756-8588-8dc3c6ad04d0";

export class SmartFilterVideoApp {
private _outputCanvas: HTMLCanvasElement;
private _onLikeClickedObservable: Observable<void>;
private readonly _outputCanvas: HTMLCanvasElement;
private readonly _onLikeClickedObservable: Observable<void>;
private _frameProcessingTimePerfCounter: PerfCounter = new PerfCounter();
private _frameCount: number = 0;

private _timeOfLastDebugUpdate: number = 0;

private _engine: ThinEngine;
private _internalInputTexture: InternalTexture;
Expand All @@ -25,9 +30,19 @@ export class SmartFilterVideoApp {
private _currentEffect: Nullable<IEffect> = null;
private _smartFilterRuntime: Nullable<SmartFilterRuntime> = null;

constructor(outputCanvas: HTMLCanvasElement, onLikeClickedObservable: Observable<void>) {
private readonly _onNewAverageFrameProcessingValue: Observable<number>;
private readonly _onNewFpsValue: Observable<number>;

constructor(
outputCanvas: HTMLCanvasElement,
onLikeClickedObservable: Observable<void>,
onNewAverageFrameProcessingValue: Observable<number>,
onNewFpsValue: Observable<number>
) {
this._outputCanvas = outputCanvas;
this._onLikeClickedObservable = onLikeClickedObservable;
this._onNewAverageFrameProcessingValue = onNewAverageFrameProcessingValue;
this._onNewFpsValue = onNewFpsValue;

this._engine = new ThinEngine(this._outputCanvas);

Expand Down Expand Up @@ -72,9 +87,13 @@ export class SmartFilterVideoApp {
}
});
this._currentEffect.start();

this._frameProcessingTimePerfCounter = new PerfCounter();
}

async videoFrameHandler(frame: videoEffects.VideoFrameData): Promise<VideoFrame> {
this._frameProcessingTimePerfCounter.beginMonitoring();
this._frameCount++;
try {
const videoFrame = frame.videoFrame as VideoFrame;

Expand All @@ -98,6 +117,20 @@ export class SmartFilterVideoApp {
} catch (e) {
console.error(e);
throw e;
} finally {
this._frameProcessingTimePerfCounter.endMonitoring();

const currentTime = performance.now();
const timeSinceLastDebugUpdate = currentTime - this._timeOfLastDebugUpdate;

if (timeSinceLastDebugUpdate >= 1000) {
this._timeOfLastDebugUpdate = currentTime;
this._onNewAverageFrameProcessingValue.notifyObservers(
this._frameProcessingTimePerfCounter.lastSecAverage
);
this._onNewFpsValue.notifyObservers(this._frameCount / (timeSinceLastDebugUpdate / 1000));
this._frameCount = 0;
}
}
}

Expand Down
37 changes: 35 additions & 2 deletions packages/demo/www/fhl.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@
body {
background-color: #f5f5f5;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 10pt;
}

.topContainer {
display: flex;
flex-direction: row;
justify-content: center;
flex-direction: column;
align-items: center;
}

.dialog {
border-radius: 4px;
background-color: white;
filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.12)) drop-shadow(0 8px 16px rgba(0, 0, 0, 0.14));
padding: 14px 16px;
display: flex;
margin-bottom: 50px;
}

.reactionCollection {
Expand Down Expand Up @@ -62,9 +68,26 @@
width: 32px;
transform: scale(0.75);
}

.buttonContainer:hover {
transform: unset;
}

.debugData {
display: grid;
width: 150px;
grid-template-columns: auto auto;
}

.debugLabel {
text-align: right;
}

.debugValue {
text-align: left;
min-width: 40px;
margin-left: 3px;
}
</style>
</head>
<body>
Expand Down Expand Up @@ -113,6 +136,16 @@
</div>
</div>
</div>
<div id="debugUi" style="display: none">
<div class="dialog">
<div class="debugData">
<div class="debugLabel" style="grid-column: 1; grid-row: 1">FPS:</div>
<div class="debugValue" style="grid-column: 2; grid-row: 1" id="fps"></div>
<div class="debugLabel" style="grid-column: 1; grid-row: 2">Avg proc time:</div>
<div class="debugValue" style="grid-column: 2; grid-row: 2" id="avgProcessingTime"></div>
</div>
</div>
</div>
</div>

<canvas height="1080" width="1920" id="inputCanvas" style="display: none"></canvas>
Expand Down

0 comments on commit 3e61728

Please sign in to comment.