-
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webcodecs): add a WebGL renderer
- Loading branch information
Showing
4 changed files
with
160 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { FrameRenderer } from "./renderer.js"; | ||
|
||
export class BitmapFrameRenderer implements FrameRenderer { | ||
#context: ImageBitmapRenderingContext; | ||
|
||
constructor(canvas: HTMLCanvasElement) { | ||
this.#context = canvas.getContext("bitmaprenderer", { alpha: false })!; | ||
} | ||
|
||
draw(frame: VideoFrame): void { | ||
createImageBitmap(frame) | ||
.then((bitmap) => { | ||
this.#context.transferFromImageBitmap(bitmap); | ||
frame.close(); | ||
}) | ||
.catch((e) => { | ||
console.warn( | ||
"[@yume-chan/scrcpy-decoder-webcodecs]", | ||
"VideoDecoder error", | ||
e, | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface FrameRenderer { | ||
draw(frame: VideoFrame): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import type { FrameRenderer } from "./renderer.js"; | ||
|
||
export class WebGLFrameRenderer implements FrameRenderer { | ||
static vertexShaderSource = ` | ||
attribute vec2 xy; | ||
varying highp vec2 uv; | ||
void main(void) { | ||
gl_Position = vec4(xy, 0.0, 1.0); | ||
// Map vertex coordinates (-1 to +1) to UV coordinates (0 to 1). | ||
// UV coordinates are Y-flipped relative to vertex coordinates. | ||
uv = vec2((1.0 + xy.x) / 2.0, (1.0 - xy.y) / 2.0); | ||
} | ||
`; | ||
|
||
static fragmentShaderSource = ` | ||
varying highp vec2 uv; | ||
uniform sampler2D texture; | ||
void main(void) { | ||
gl_FragColor = texture2D(texture, uv); | ||
} | ||
`; | ||
|
||
#context: WebGLRenderingContext; | ||
|
||
constructor(canvas: HTMLCanvasElement) { | ||
const gl = | ||
canvas.getContext("webgl2", { alpha: false }) || | ||
canvas.getContext("webgl", { alpha: false }); | ||
if (!gl) { | ||
throw new Error("WebGL not supported"); | ||
} | ||
this.#context = gl; | ||
|
||
const vertexShader = gl.createShader(gl.VERTEX_SHADER)!; | ||
gl.shaderSource(vertexShader, WebGLFrameRenderer.vertexShaderSource); | ||
gl.compileShader(vertexShader); | ||
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) { | ||
throw new Error(gl.getShaderInfoLog(vertexShader)!); | ||
} | ||
|
||
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)!; | ||
gl.shaderSource( | ||
fragmentShader, | ||
WebGLFrameRenderer.fragmentShaderSource, | ||
); | ||
gl.compileShader(fragmentShader); | ||
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) { | ||
throw new Error(gl.getShaderInfoLog(fragmentShader)!); | ||
} | ||
|
||
const shaderProgram = gl.createProgram()!; | ||
gl.attachShader(shaderProgram, vertexShader); | ||
gl.attachShader(shaderProgram, fragmentShader); | ||
gl.linkProgram(shaderProgram); | ||
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { | ||
throw new Error(gl.getProgramInfoLog(shaderProgram)!); | ||
} | ||
gl.useProgram(shaderProgram); | ||
|
||
// Vertex coordinates, clockwise from bottom-left. | ||
const vertexBuffer = gl.createBuffer(); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); | ||
gl.bufferData( | ||
gl.ARRAY_BUFFER, | ||
new Float32Array([-1.0, -1.0, -1.0, +1.0, +1.0, +1.0, +1.0, -1.0]), | ||
gl.STATIC_DRAW, | ||
); | ||
|
||
const xyLocation = gl.getAttribLocation(shaderProgram, "xy"); | ||
gl.vertexAttribPointer(xyLocation, 2, gl.FLOAT, false, 0, 0); | ||
gl.enableVertexAttribArray(xyLocation); | ||
|
||
// Create one texture to upload frames to. | ||
const texture = gl.createTexture(); | ||
gl.bindTexture(gl.TEXTURE_2D, texture); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | ||
} | ||
|
||
draw(frame: VideoFrame) { | ||
const gl = this.#context; | ||
gl.texImage2D( | ||
gl.TEXTURE_2D, | ||
0, | ||
gl.RGBA, | ||
gl.RGBA, | ||
gl.UNSIGNED_BYTE, | ||
frame, | ||
); | ||
frame.close(); | ||
|
||
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); | ||
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4); | ||
} | ||
} |