From fa2c9d642389056e2113354307ec4edd91779ace Mon Sep 17 00:00:00 2001 From: onecamp Date: Thu, 24 Jan 2019 14:22:53 -0500 Subject: [PATCH] Verify that webgl2 is supported before using it (#3435) * Verify that webgl2 is supported before using it * First check if WebGLRenderingContext exists at all * Move webgl warnings to getTermOptions --- lib/components/term.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/components/term.js b/lib/components/term.js index 869224e0..19692f4f 100644 --- a/lib/components/term.js +++ b/lib/components/term.js @@ -20,11 +20,38 @@ const CURSOR_STYLES = { BLOCK: 'block' }; +const isWebgl2Supported = (() => { + let isSupported = window.WebGL2RenderingContext ? undefined : false; + return () => { + if (isSupported === undefined) { + const canvas = document.createElement('canvas'); + const gl = canvas.getContext('webgl2', {depth: false, antialias: false}); + isSupported = gl instanceof window.WebGL2RenderingContext; + } + return isSupported; + }; +})(); + const getTermOptions = props => { // Set a background color only if it is opaque const needTransparency = Color(props.backgroundColor).alpha() < 1; const backgroundColor = needTransparency ? 'transparent' : props.backgroundColor; - const useWebGL = props.webGLRenderer && !needTransparency; + + let useWebGL = false; + if (props.webGLRenderer) { + if (needTransparency) { + // eslint-disable-next-line no-console + console.warn( + 'WebGL Renderer has been disabled since it does not support transparent backgrounds yet. ' + + 'Falling back to canvas-based rendering.' + ); + } else if (!isWebgl2Supported()) { + // eslint-disable-next-line no-console + console.warn('WebGL2 is not supported on your machine. Falling back to canvas-based rendering.'); + } else { + useWebGL = true; + } + } return { macOptionIsMeta: props.modifierKeys.altIsMeta, scrollback: props.scrollback,