webgl-utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. const { Cc, Ci } = require("chrome");
  6. const Services = require("Services");
  7. const WEBGL_CONTEXT_NAME = "experimental-webgl";
  8. function isWebGLForceEnabled() {
  9. return Services.prefs.getBoolPref("webgl.force-enabled");
  10. }
  11. function isWebGLSupportedByGFX() {
  12. let supported = false;
  13. try {
  14. let gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);
  15. let angle = gfxInfo.FEATURE_WEBGL_ANGLE;
  16. let opengl = gfxInfo.FEATURE_WEBGL_OPENGL;
  17. // if either the Angle or OpenGL renderers are available, WebGL should work
  18. supported = gfxInfo.getFeatureStatus(angle) === gfxInfo.FEATURE_STATUS_OK ||
  19. gfxInfo.getFeatureStatus(opengl) === gfxInfo.FEATURE_STATUS_OK;
  20. } catch (e) {
  21. return false;
  22. }
  23. return supported;
  24. }
  25. function create3DContext(canvas) {
  26. // try to get a valid context from an existing canvas
  27. let context = null;
  28. try {
  29. context = canvas.getContext(WEBGL_CONTEXT_NAME, aFlags);
  30. } catch (e) {
  31. return null;
  32. }
  33. return context;
  34. }
  35. function createCanvas(doc) {
  36. return doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
  37. }
  38. function isWebGLSupported(doc) {
  39. let supported =
  40. !isWebGLForceEnabled() &&
  41. isWebGLSupportedByGFX() &&
  42. create3DContext(createCanvas(doc));
  43. return supported;
  44. }
  45. exports.isWebGLSupported = isWebGLSupported;