Skip to content

Commit

Permalink
fix: handle WebGL contextLost
Browse files Browse the repository at this point in the history
handle WebGL contextLost
(phoboslab/jsmpeg@cafd02d)
  • Loading branch information
cycjimmy committed Mar 15, 2022
1 parent d3cc3a5 commit b04df7e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class WebGLRenderer {
this.width = this.canvas.width;
this.height = this.canvas.height;
this.enabled = true;
this.contextLost = false;

this.hasTextureData = {};

Expand All @@ -24,6 +25,19 @@ class WebGLRenderer {
throw new Error('Failed to get WebGL Context');
}

this.canvas.addEventListener('webglcontextlost', this.handleContextLost.bind(this), false);
this.canvas.addEventListener(
'webglcontextrestored',
this.handleContextRestored.bind(this),
false
);

this.initGL();
}

initGL() {
this.hasTextureData = {};

const { gl } = this;
let vertexAttr = null;

Expand Down Expand Up @@ -60,7 +74,22 @@ class WebGLRenderer {
this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
}

handleContextLost(ev) {
ev.preventDefault();
this.contextLost = true;
}

handleContextRestored() {
this.initGL();
this.contextLost = false;
}

destroy() {
if (this.contextLost) {
// Nothing to do here
return;
}

const { gl } = this;

this.deleteTexture(gl.TEXTURE0, this.textureY);
Expand All @@ -76,6 +105,7 @@ class WebGLRenderer {

gl.getExtension('WEBGL_lose_context').loseContext();
this.canvas.remove();
this.contextLost = true;
}

resize(width, height) {
Expand Down

0 comments on commit b04df7e

Please sign in to comment.