Skip to content

Emit 'plotly_webglcontextlost' event on WebGL context lost #2986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/prepare_regl.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ module.exports = function prepareRegl(gd, extensions) {
} catch(e) {
success = false;
}

if(success) {
this.addEventListener('webglcontextlost', function(event) {
if(gd && gd.emit) {
gd.emit('plotly_webglcontextlost', {
event: event,
layer: d.key
});
}
}, false);
}
});

if(!success) {
Expand Down
16 changes: 11 additions & 5 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ function render(scene) {
}

function initializeGLPlot(scene, fullLayout, canvas, gl) {
var gd = scene.graphDiv;

var glplotOptions = {
canvas: canvas,
gl: gl,
Expand Down Expand Up @@ -220,18 +222,22 @@ function initializeGLPlot(scene, fullLayout, canvas, gl) {

var update = {};
update[scene.id + '.camera'] = getLayoutCamera(scene.camera);
scene.saveCamera(scene.graphDiv.layout);
scene.saveCamera(gd.layout);
scene.graphDiv.emit('plotly_relayout', update);
};

scene.glplot.canvas.addEventListener('mouseup', relayoutCallback.bind(null, scene));
scene.glplot.canvas.addEventListener('wheel', relayoutCallback.bind(null, scene), passiveSupported ? {passive: false} : false);

if(!scene.staticMode) {
scene.glplot.canvas.addEventListener('webglcontextlost', function(ev) {
Lib.warn('Lost WebGL context.');
ev.preventDefault();
});
scene.glplot.canvas.addEventListener('webglcontextlost', function(event) {
if(gd && gd.emit) {
gd.emit('plotly_webglcontextlost', {
event: event,
layer: scene.id
});
}
}, false);
}

if(!scene.camera) {
Expand Down
21 changes: 21 additions & 0 deletions test/jasmine/tests/gl2d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ describe('Test gl plot side effects', function() {
.catch(failTest)
.then(done);
});

it('@gl should fire *plotly_webglcontextlost* when on webgl context lost', function(done) {
var _mock = Lib.extendDeep({}, require('@mocks/gl2d_12.json'));

Plotly.plot(gd, _mock).then(function() {
return new Promise(function(resolve, reject) {
gd.on('plotly_webglcontextlost', resolve);
setTimeout(reject, 10);

var ev = new window.WebGLContextEvent('webglcontextlost');
var canvas = gd.querySelector('.gl-canvas-context');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant by "triggered on any of the canvases losing their context" is, for this one plot we make 3 canvases, each with its own webgl context... any of them could in principle lose its context, right?

> gd.querySelectorAll('canvas')
NodeList(3) [
    canvas.gl-canvas.gl-canvas-context,
    canvas.gl-canvas.gl-canvas-focus,
    canvas.gl-canvas.gl-canvas-pick
]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you caught me being lazy.

Better tests in 905956a

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even better tests in 3a38d73

canvas.dispatchEvent(ev);
});
})
.then(function(eventData) {
expect((eventData || {}).event).toBeDefined();
expect((eventData || {}).layer).toBe('contextLayer');
})
.catch(failTest)
.then(done);
});
});

describe('Test gl2d plots', function() {
Expand Down
21 changes: 21 additions & 0 deletions test/jasmine/tests/gl3d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1425,4 +1425,25 @@ describe('Test removal of gl contexts', function() {
})
.then(done);
});

it('@gl should fire *plotly_webglcontextlost* when on webgl context lost', function(done) {
var _mock = Lib.extendDeep({}, require('@mocks/gl3d_marker-arrays.json'));

Plotly.plot(gd, _mock).then(function() {
return new Promise(function(resolve, reject) {
gd.on('plotly_webglcontextlost', resolve);
setTimeout(reject, 10);

var ev = new window.WebGLContextEvent('webglcontextlost');
var canvas = gd.querySelector('div#scene > canvas');
canvas.dispatchEvent(ev);
});
})
.then(function(eventData) {
expect((eventData || {}).event).toBeDefined();
expect((eventData || {}).layer).toBe('scene');
})
.catch(failTest)
.then(done);
});
});