feat(decoder): add an option to enable capture WebGL canvas

This commit is contained in:
Simon Chan 2024-05-03 14:23:46 +08:00
parent 1eed9aaf8b
commit b75669fb67
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
2 changed files with 22 additions and 3 deletions

View file

@ -81,13 +81,23 @@ export class WebCodecsVideoDecoder implements ScrcpyVideoDecoder {
#currentFrameRendered = false; #currentFrameRendered = false;
#animationFrameId = 0; #animationFrameId = 0;
constructor(codec: ScrcpyVideoCodecId) { /**
* Create a new WebCodecs video decoder.
* @param codec The video codec to decode
* @param enableCapture
* Whether to allow capturing the canvas content using APIs like `readPixels` and `toDataURL`.
* Enable this option may reduce performance.
*/
constructor(codec: ScrcpyVideoCodecId, enableCapture: boolean) {
this.#codec = codec; this.#codec = codec;
this.#canvas = document.createElement("canvas"); this.#canvas = document.createElement("canvas");
try { try {
this.#renderer = new WebGLFrameRenderer(this.#canvas); this.#renderer = new WebGLFrameRenderer(
this.#canvas,
enableCapture,
);
} catch { } catch {
this.#renderer = new BitmapFrameRenderer(this.#canvas); this.#renderer = new BitmapFrameRenderer(this.#canvas);
} }

View file

@ -26,15 +26,24 @@ void main(void) {
#context: WebGLRenderingContext; #context: WebGLRenderingContext;
constructor(canvas: HTMLCanvasElement) { /**
* Create a new WebGL frame renderer.
* @param canvas The canvas to render frames to.
* @param enableCapture
* Whether to allow capturing the canvas content using APIs like `readPixels` and `toDataURL`.
* Enable this option may reduce performance.
*/
constructor(canvas: HTMLCanvasElement, enableCapture: boolean) {
const gl = const gl =
canvas.getContext("webgl2", { canvas.getContext("webgl2", {
alpha: false, alpha: false,
failIfMajorPerformanceCaveat: true, failIfMajorPerformanceCaveat: true,
preserveDrawingBuffer: enableCapture,
}) || }) ||
canvas.getContext("webgl", { canvas.getContext("webgl", {
alpha: false, alpha: false,
failIfMajorPerformanceCaveat: true, failIfMajorPerformanceCaveat: true,
preserveDrawingBuffer: enableCapture,
}); });
if (!gl) { if (!gl) {
throw new Error("WebGL not supported"); throw new Error("WebGL not supported");