Skip to content

Commit

Permalink
Handle WebGL contextLost; close phoboslab#392
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab authored and scarletsky committed Apr 29, 2022
1 parent bcbfaf6 commit 6eb0e66
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var WebGLRenderer = function(options) {
this.width = this.canvas.width;
this.height = this.canvas.height;
this.enabled = true;
this.contextLost = false;

this.hasTextureData = {};

Expand All @@ -25,6 +26,15 @@ var WebGLRenderer = function(options) {
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();
};

WebGLRenderer.prototype.initGL = function() {
this.hasTextureData = {};

var gl = this.gl;
var vertexAttr = null;

Expand Down Expand Up @@ -62,7 +72,22 @@ var WebGLRenderer = function(options) {
this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
};

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

WebGLRenderer.prototype.handleContextRestored = function(ev) {
this.initGL();
this.contextLost = false;
};

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

var gl = this.gl;

this.deleteTexture(gl.TEXTURE0, this.textureY);
Expand All @@ -78,6 +103,7 @@ WebGLRenderer.prototype.destroy = function() {

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

WebGLRenderer.prototype.clear = function (options) {
Expand Down

0 comments on commit 6eb0e66

Please sign in to comment.