Skip to content

Commit c20c911

Browse files
authored
Fix excessive WebGL context creation with improved resource cleanup, safer logging, and version bump (#21)
* fix(webgl): properly dispose of game view and canvas after capture * fix(capture): disable logs during production * chore: bump version to 0.9.2
1 parent ad8795c commit c20c911

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

fxmanifest.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fx_version 'bodacious'
22

3-
version '0.9.1'
3+
version '0.9.2'
44

55
game "gta5"
66

game/nui/src/capture.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ type CaptureRequest = {
2020
maxHeight?: number;
2121
};
2222

23+
function log(...args: any[]) {
24+
if (import.meta.env.MODE === 'production') return
25+
log(...args);
26+
}
27+
2328
export class Capture {
2429
#gameView: any;
2530
#canvas: HTMLCanvasElement | null = null;
@@ -73,7 +78,7 @@ export class Capture {
7378
this.#canvas.width = width;
7479
this.#canvas.height = height;
7580

76-
console.log(`Capturing at ${width}x${height} (original: ${window.innerWidth}x${window.innerHeight})`);
81+
log(`Capturing at ${width}x${height} (original: ${window.innerWidth}x${window.innerHeight})`);
7782

7883
this.#gameView = createGameView(this.#canvas);
7984

@@ -93,7 +98,10 @@ export class Capture {
9398
if (!imageData) return console.error('No image available');
9499

95100
await this.httpUploadImage(request, imageData);
101+
this.#gameView.dispose();
96102
this.#canvas.remove();
103+
this.#gameView = null;
104+
this.#canvas = null;
97105
}
98106

99107
async httpUploadImage(request: CaptureRequest, imageData: string | Blob) {
@@ -170,12 +178,12 @@ export class Capture {
170178
}
171179
}
172180

173-
console.log(`Using quality ${quality} for ${canvas.width}x${canvas.height} (${pixelCount} pixels)`);
181+
log(`Using quality ${quality} for ${canvas.width}x${canvas.height} (${pixelCount} pixels)`);
174182

175183
canvas.toBlob(
176184
(blob) => {
177185
if (blob) {
178-
console.log(`Generated blob: ${(blob.size / 1024 / 1024).toFixed(2)}MB`);
186+
log(`Generated blob: ${(blob.size / 1024 / 1024).toFixed(2)}MB`);
179187
resolve(blob);
180188
} else {
181189
reject('No blob available');

packages/gameview/webgl.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,32 @@ export function createGameView(canvas: HTMLCanvasElement) {
141141
canvas,
142142
gl,
143143
animationFrame: void 0,
144+
disposed: false,
144145
resize: (width: number, height: number) => {
146+
if (gameView.disposed) return;
145147
gl.viewport(0, 0, width, height);
146148
gl.canvas.width = width;
147149
gl.canvas.height = height;
148150
},
151+
dispose: () => {
152+
if (gameView.disposed) return;
153+
gameView.disposed = true;
154+
if (gameView.animationFrame) {
155+
cancelAnimationFrame(gameView.animationFrame);
156+
gameView.animationFrame = void 0;
157+
}
158+
const ext = gl.getExtension('WEBGL_lose_context');
159+
if (ext) {
160+
ext.loseContext();
161+
}
162+
if (canvas.parentNode) {
163+
canvas.parentNode.removeChild(canvas);
164+
}
165+
},
149166
};
150167

151168
render = () => {
169+
if (gameView.disposed) return;
152170
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
153171
gl.finish();
154172

0 commit comments

Comments
 (0)