webgl-utils.js (1465B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 const WEBGL_CONTEXT_NAME = "experimental-webgl"; 7 8 function isWebGLForceEnabled() { 9 return Services.prefs.getBoolPref("webgl.force-enabled"); 10 } 11 12 function isWebGLSupportedByGFX() { 13 let supported = false; 14 15 try { 16 const gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo); 17 const angle = gfxInfo.FEATURE_WEBGL_ANGLE; 18 const opengl = gfxInfo.FEATURE_WEBGL_OPENGL; 19 20 // if either the Angle or OpenGL renderers are available, WebGL should work 21 supported = 22 gfxInfo.getFeatureStatus(angle) === gfxInfo.FEATURE_STATUS_OK || 23 gfxInfo.getFeatureStatus(opengl) === gfxInfo.FEATURE_STATUS_OK; 24 } catch (e) { 25 return false; 26 } 27 return supported; 28 } 29 30 function create3DContext(canvas) { 31 // try to get a valid context from an existing canvas 32 let context = null; 33 try { 34 context = canvas.getContext(WEBGL_CONTEXT_NAME); 35 } catch (e) { 36 return null; 37 } 38 return context; 39 } 40 41 function createCanvas(doc) { 42 return doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); 43 } 44 45 function isWebGLSupported(doc) { 46 const supported = 47 !isWebGLForceEnabled() && 48 isWebGLSupportedByGFX() && 49 create3DContext(createCanvas(doc)); 50 51 return supported; 52 } 53 exports.isWebGLSupported = isWebGLSupported;