diff --git a/libraries/scrcpy-decoder-webcodecs/src/index.ts b/libraries/scrcpy-decoder-webcodecs/src/index.ts index e539e3fe..5528843d 100644 --- a/libraries/scrcpy-decoder-webcodecs/src/index.ts +++ b/libraries/scrcpy-decoder-webcodecs/src/index.ts @@ -81,13 +81,23 @@ export class WebCodecsVideoDecoder implements ScrcpyVideoDecoder { #currentFrameRendered = false; #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.#canvas = document.createElement("canvas"); try { - this.#renderer = new WebGLFrameRenderer(this.#canvas); + this.#renderer = new WebGLFrameRenderer( + this.#canvas, + enableCapture, + ); } catch { this.#renderer = new BitmapFrameRenderer(this.#canvas); } diff --git a/libraries/scrcpy-decoder-webcodecs/src/webgl.ts b/libraries/scrcpy-decoder-webcodecs/src/webgl.ts index 6c1e9853..a25e1bdb 100644 --- a/libraries/scrcpy-decoder-webcodecs/src/webgl.ts +++ b/libraries/scrcpy-decoder-webcodecs/src/webgl.ts @@ -26,15 +26,24 @@ void main(void) { #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 = canvas.getContext("webgl2", { alpha: false, failIfMajorPerformanceCaveat: true, + preserveDrawingBuffer: enableCapture, }) || canvas.getContext("webgl", { alpha: false, failIfMajorPerformanceCaveat: true, + preserveDrawingBuffer: enableCapture, }); if (!gl) { throw new Error("WebGL not supported");