From 8731d4c61b69f888c25491a69f0f5d3c96d909f9 Mon Sep 17 00:00:00 2001 From: Igor Dykhta Date: Tue, 16 Feb 2021 16:22:48 +0200 Subject: [PATCH] exposed webgl context state from animation loop (#1453) --- docs/api-reference/engine/animation-loop.md | 4 ++++ modules/engine/src/lib/animation-loop.d.ts | 1 + modules/engine/src/lib/animation-loop.js | 8 ++++++++ modules/engine/test/lib/animation-loop.spec.js | 2 ++ 4 files changed, 15 insertions(+) diff --git a/docs/api-reference/engine/animation-loop.md b/docs/api-reference/engine/animation-loop.md index 03aa0d0e5b..e4470fa9d2 100644 --- a/docs/api-reference/engine/animation-loop.md +++ b/docs/api-reference/engine/animation-loop.md @@ -135,6 +135,10 @@ Returns returns a `Promise` that resolves to the data URL of the canvas once dra `animationLoop.toDataURL()` +### isContextLost() + +Returns the current state of the WebGL context used by the animation loop. + ## Callback Parameters The callbacks `onInitialize`, `onRender` and `onFinalize` that the app supplies to the `AnimationLoop`, will be called with an object containing named parameters: diff --git a/modules/engine/src/lib/animation-loop.d.ts b/modules/engine/src/lib/animation-loop.d.ts index bd6eccbcc3..409a984f50 100644 --- a/modules/engine/src/lib/animation-loop.d.ts +++ b/modules/engine/src/lib/animation-loop.d.ts @@ -104,6 +104,7 @@ export default class AnimationLoop { toDataURL(): Promise; setViewParameters(): AnimationLoop; getHTMLControlValue(id: string, defaultValue?: number): number; + isContextLost(): boolean; // Callbacks onCreateContext(opts: CreateGLContextOptions): WebGLRenderingContext; diff --git a/modules/engine/src/lib/animation-loop.js b/modules/engine/src/lib/animation-loop.js index a13d6286cf..0fe88c37b7 100644 --- a/modules/engine/src/lib/animation-loop.js +++ b/modules/engine/src/lib/animation-loop.js @@ -179,6 +179,10 @@ export default class AnimationLoop { // Redraw now redraw() { + if (this.isContextLost()) { + return this; + } + this._beginTimers(); this._setupFrame(); @@ -249,6 +253,10 @@ export default class AnimationLoop { return this.gl.canvas.toDataURL(); } + isContextLost() { + return this.gl.isContextLost(); + } + onCreateContext(...args) { return this.props.onCreateContext(...args); } diff --git a/modules/engine/test/lib/animation-loop.spec.js b/modules/engine/test/lib/animation-loop.spec.js index 3ef98b0219..614fe555f0 100644 --- a/modules/engine/test/lib/animation-loop.spec.js +++ b/modules/engine/test/lib/animation-loop.spec.js @@ -25,6 +25,8 @@ test('core#AnimationLoop start,stop', t => { onRender: () => { renderCalled++; + t.is(animationLoop.isContextLost(), false, 'isContextLost returns false'); + animationLoop.stop(); t.is(initializeCalled, 1, 'onInitialize called');