Bug 765137 - 3/3 - WebGL: more cleanup and renaming around extensions - r=jgilbert
authorBenoit Jacob <bjacob@mozilla.com>
Sat, 16 Jun 2012 01:12:30 -0400
changeset 96880 45fc4aa0941e6547869d179eeb05fef1cbb072fe
parent 96879 b4155b0db5682dd74740275b33c23895ef081cdc
child 96881 b589776a7912ab5740bd708138071473b4952868
push id22938
push userryanvm@gmail.com
push dateSat, 16 Jun 2012 13:39:00 +0000
treeherdermozilla-central@9602a9e99045 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgilbert
bugs765137
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 765137 - 3/3 - WebGL: more cleanup and renaming around extensions - r=jgilbert
content/canvas/src/WebGLContext.cpp
content/canvas/src/WebGLContext.h
content/canvas/src/WebGLContextGL.cpp
content/canvas/src/WebGLContextValidate.cpp
--- a/content/canvas/src/WebGLContext.cpp
+++ b/content/canvas/src/WebGLContext.cpp
@@ -71,17 +71,17 @@ NS_NewCanvasRenderingContextWebGL(nsIDOM
     NS_ADDREF(*aResult = ctx);
     return NS_OK;
 }
 
 WebGLContext::WebGLContext()
     : gl(nsnull)
 {
     SetIsDOMBinding();
-    mEnabledExtensions.SetLength(WebGLExtensionID_Max);
+    mExtensions.SetLength(WebGLExtensionID_number_of_extensions);
 
     mGeneration = 0;
     mInvalidated = false;
     mResetLayer = true;
     mOptionsFrozen = false;
 
     mActiveTexture = 0;
     mWebGLError = LOCAL_GL_NO_ERROR;
@@ -835,34 +835,34 @@ WebGLContext::MozGetUnderlyingParamStrin
 
     default:
         return NS_ERROR_INVALID_ARG;
     }
 
     return NS_OK;
 }
 
-bool WebGLContext::IsExtensionSupported(WebGLExtensionID ei)
+bool WebGLContext::IsExtensionSupported(WebGLExtensionID ext)
 {
     bool isSupported = false;
 
-    switch (ei) {
-        case WebGL_OES_standard_derivatives:
-        case WebGL_WEBGL_lose_context:
+    switch (ext) {
+        case OES_standard_derivatives:
+        case WEBGL_lose_context:
             // We always support these extensions.
             isSupported = true;
             break;
-        case WebGL_OES_texture_float:
+        case OES_texture_float:
             isSupported = gl->IsExtensionSupported(gl->IsGLES2() ? GLContext::OES_texture_float 
                                                                  : GLContext::ARB_texture_float);
             break;
-        case WebGL_EXT_texture_filter_anisotropic:
+        case EXT_texture_filter_anisotropic:
             isSupported = gl->IsExtensionSupported(GLContext::EXT_texture_filter_anisotropic);
             break;
-        case WebGL_WEBGL_compressed_texture_s3tc:
+        case WEBGL_compressed_texture_s3tc:
             if (gl->IsExtensionSupported(GLContext::EXT_texture_compression_s3tc)) {
                 isSupported = true;
             } else if (gl->IsExtensionSupported(GLContext::EXT_texture_compression_dxt1) &&
                        gl->IsExtensionSupported(GLContext::ANGLE_texture_compression_dxt3) &&
                        gl->IsExtensionSupported(GLContext::ANGLE_texture_compression_dxt5))
             {
                 isSupported = true;
             }
@@ -887,74 +887,78 @@ WebGLContext::GetExtension(const nsAStri
 {
     if (!IsContextStable())
         return nsnull;
 
     if (mDisableExtensions) {
         return nsnull;
     }
 
-    WebGLExtensionID ei = WebGLExtensionID_Max;
+    WebGLExtensionID ext = WebGLExtensionID_unknown_extension;
+
     if (aName.Equals(NS_LITERAL_STRING("OES_texture_float"),
         nsCaseInsensitiveStringComparator()))
     {
-        if (IsExtensionSupported(WebGL_OES_texture_float))
-            ei = WebGL_OES_texture_float;
+        if (IsExtensionSupported(OES_texture_float))
+            ext = OES_texture_float;
     }
     else if (aName.Equals(NS_LITERAL_STRING("OES_standard_derivatives"),
              nsCaseInsensitiveStringComparator()))
     {
-        if (IsExtensionSupported(WebGL_OES_standard_derivatives))
-            ei = WebGL_OES_standard_derivatives;
+        if (IsExtensionSupported(OES_standard_derivatives))
+            ext = OES_standard_derivatives;
     }
     else if (aName.Equals(NS_LITERAL_STRING("MOZ_EXT_texture_filter_anisotropic"),
              nsCaseInsensitiveStringComparator()))
     {
-        if (IsExtensionSupported(WebGL_EXT_texture_filter_anisotropic))
-            ei = WebGL_EXT_texture_filter_anisotropic;
+        if (IsExtensionSupported(EXT_texture_filter_anisotropic))
+            ext = EXT_texture_filter_anisotropic;
     }
     else if (aName.Equals(NS_LITERAL_STRING("MOZ_WEBGL_lose_context"),
              nsCaseInsensitiveStringComparator()))
     {
-        if (IsExtensionSupported(WebGL_WEBGL_lose_context))
-            ei = WebGL_WEBGL_lose_context;
+        if (IsExtensionSupported(WEBGL_lose_context))
+            ext = WEBGL_lose_context;
     }
     else if (aName.Equals(NS_LITERAL_STRING("MOZ_WEBGL_compressed_texture_s3tc"),
              nsCaseInsensitiveStringComparator()))
     {
-        if (IsExtensionSupported(WebGL_WEBGL_compressed_texture_s3tc))
-            ei = WebGL_WEBGL_compressed_texture_s3tc;
+        if (IsExtensionSupported(WEBGL_compressed_texture_s3tc))
+            ext = WEBGL_compressed_texture_s3tc;
+    }
+
+    if (ext == WebGLExtensionID_unknown_extension) {
+      return nsnull;
     }
 
-    if (ei != WebGLExtensionID_Max) {
-        if (!IsExtensionEnabled(ei)) {
-            switch (ei) {
-                case WebGL_OES_standard_derivatives:
-                    mEnabledExtensions[ei] = new WebGLExtensionStandardDerivatives(this);
-                    break;
-                case WebGL_EXT_texture_filter_anisotropic:
-                    mEnabledExtensions[ei] = new WebGLExtensionTextureFilterAnisotropic(this);
-                    break;
-                case WebGL_WEBGL_lose_context:
-                    mEnabledExtensions[ei] = new WebGLExtensionLoseContext(this);
-                    break;
-                case WebGL_WEBGL_compressed_texture_s3tc:
-                    mEnabledExtensions[ei] = new WebGLExtensionCompressedTextureS3TC(this);
-                    break;
-                // create an extension for any types that don't
-                // have any additional tokens or methods
-                default:
-                    mEnabledExtensions[ei] = new WebGLExtension(this);
-                    break;
-            }
+    if (!mExtensions[ext]) {
+        switch (ext) {
+            case OES_standard_derivatives:
+                mExtensions[ext] = new WebGLExtensionStandardDerivatives(this);
+                break;
+            case EXT_texture_filter_anisotropic:
+                mExtensions[ext] = new WebGLExtensionTextureFilterAnisotropic(this);
+                break;
+            case WEBGL_lose_context:
+                mExtensions[ext] = new WebGLExtensionLoseContext(this);
+                break;
+            case WEBGL_compressed_texture_s3tc:
+                mExtensions[ext] = new WebGLExtensionCompressedTextureS3TC(this);
+                break;
+            default:
+                // create a generic WebGLExtension object for any extensions that don't
+                // have any additional tokens or methods. We still need these to be separate
+                // objects in case the user might extend the corresponding JS objects with custom
+                // properties.
+                mExtensions[ext] = new WebGLExtension(this);
+                break;
         }
-        return mEnabledExtensions[ei];
     }
 
-    return nsnull;
+    return mExtensions[ext];
 }
 
 void
 WebGLContext::ForceClearFramebufferWithDefaultValues(PRUint32 mask, const nsIntRect& viewportRect)
 {
     MakeContextCurrent();
 
     bool initializeColorBuffer = 0 != (mask & LOCAL_GL_COLOR_BUFFER_BIT);
@@ -1211,23 +1215,23 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(WebGLCo
 NS_IMPL_CYCLE_COLLECTION_CLASS(WebGLContext)
 
 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(WebGLContext)
   NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
 NS_IMPL_CYCLE_COLLECTION_TRACE_END
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(WebGLContext)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCanvasElement)
-  NS_IMPL_CYCLE_COLLECTION_UNLINK_NSTARRAY(mEnabledExtensions)
+  NS_IMPL_CYCLE_COLLECTION_UNLINK_NSTARRAY(mExtensions)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(WebGLContext)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mCanvasElement, nsINode)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSTARRAY_OF_NSCOMPTR(mEnabledExtensions)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSTARRAY_OF_NSCOMPTR(mExtensions)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 DOMCI_DATA(WebGLRenderingContext, WebGLContext)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebGLContext)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsIDOMWebGLRenderingContext)
@@ -1497,25 +1501,25 @@ WebGLContext::GetSupportedExtensions(Nul
         return;
     
     if (mDisableExtensions) {
         return;
     }
 
     nsTArray<nsString>& arr = retval.SetValue();
     
-    if (IsExtensionSupported(WebGL_OES_texture_float))
+    if (IsExtensionSupported(OES_texture_float))
         arr.AppendElement(NS_LITERAL_STRING("OES_texture_float"));
-    if (IsExtensionSupported(WebGL_OES_standard_derivatives))
+    if (IsExtensionSupported(OES_standard_derivatives))
         arr.AppendElement(NS_LITERAL_STRING("OES_standard_derivatives"));
-    if (IsExtensionSupported(WebGL_EXT_texture_filter_anisotropic))
+    if (IsExtensionSupported(EXT_texture_filter_anisotropic))
         arr.AppendElement(NS_LITERAL_STRING("MOZ_EXT_texture_filter_anisotropic"));
-    if (IsExtensionSupported(WebGL_WEBGL_lose_context))
+    if (IsExtensionSupported(WEBGL_lose_context))
         arr.AppendElement(NS_LITERAL_STRING("MOZ_WEBGL_lose_context"));
-    if (IsExtensionSupported(WebGL_WEBGL_compressed_texture_s3tc))
+    if (IsExtensionSupported(WEBGL_compressed_texture_s3tc))
         arr.AppendElement(NS_LITERAL_STRING("MOZ_WEBGL_compressed_texture_s3tc"));
 }
 
 NS_IMETHODIMP
 WebGLContext::IsContextLost(WebGLboolean *retval)
 {
     *retval = mContextStatus != ContextStable;
     return NS_OK;
--- a/content/canvas/src/WebGLContext.h
+++ b/content/canvas/src/WebGLContext.h
@@ -1151,29 +1151,33 @@ protected:
         // The context is lost, an event has been sent to the script, and the
         // script correctly handled the event. We are waiting for the context to
         // be restored.
         ContextLostAwaitingRestore
     };
 
     // extensions
     enum WebGLExtensionID {
-        WebGL_OES_texture_float,
-        WebGL_OES_standard_derivatives,
-        WebGL_EXT_texture_filter_anisotropic,
-        WebGL_WEBGL_lose_context,
-        WebGL_WEBGL_compressed_texture_s3tc,
-        WebGLExtensionID_Max
+        OES_texture_float,
+        OES_standard_derivatives,
+        EXT_texture_filter_anisotropic,
+        WEBGL_lose_context,
+        WEBGL_compressed_texture_s3tc,
+        WebGLExtensionID_number_of_extensions,
+        WebGLExtensionID_unknown_extension
     };
-    nsAutoTArray<nsRefPtr<WebGLExtension>, WebGLExtensionID_Max> mEnabledExtensions;
-    bool IsExtensionEnabled(WebGLExtensionID ext) const {
-        NS_ABORT_IF_FALSE(ext >= 0 && ext < WebGLExtensionID_Max, "bogus index!");
-        return mEnabledExtensions[ext] != nsnull;
+    nsAutoTArray<nsRefPtr<WebGLExtension>, WebGLExtensionID_number_of_extensions> mExtensions;
+
+    // returns true if the extension has been enabled by calling getExtension.
+    bool IsExtensionEnabled(WebGLExtensionID ext) {
+        return mExtensions[ext];
     }
-    bool IsExtensionSupported(WebGLExtensionID ei);
+
+    // returns true if the extension is supported (as returned by getSupportedExtensions)
+    bool IsExtensionSupported(WebGLExtensionID ext);
 
     nsTArray<WebGLenum> mCompressedTextureFormats;
 
     bool InitAndValidateGL();
     bool ValidateBuffers(int32_t *maxAllowedCount, const char *info);
     bool ValidateCapabilityEnum(WebGLenum cap, const char *info);
     bool ValidateBlendEquationEnum(WebGLenum cap, const char *info);
     bool ValidateBlendFuncDstEnum(WebGLenum mode, const char *info);
--- a/content/canvas/src/WebGLContextGL.cpp
+++ b/content/canvas/src/WebGLContextGL.cpp
@@ -2418,17 +2418,17 @@ WebGLContext::GetParameter(JSContext* cx
         case LOCAL_GL_DEPTH_BITS:
         case LOCAL_GL_STENCIL_BITS:
         {
             GLint i = 0;
             gl->fGetIntegerv(pname, &i);
             return JS::Int32Value(i);
         }
         case LOCAL_GL_FRAGMENT_SHADER_DERIVATIVE_HINT:
-            if (mEnabledExtensions[WebGL_OES_standard_derivatives]) {
+            if (IsExtensionEnabled(OES_standard_derivatives)) {
                 GLint i = 0;
                 gl->fGetIntegerv(pname, &i);
                 return JS::Int32Value(i);
             }
             else {
                 ErrorInvalidEnum("getParameter: parameter", pname);
                 return JS::NullValue();
             }
@@ -2465,17 +2465,17 @@ WebGLContext::GetParameter(JSContext* cx
             gl->fGetIntegerv(pname, &i);
             GLuint i_unsigned(i); // this is where -1 becomes 2^32-1
             double i_double(i_unsigned); // pass as FP value to allow large values such as 2^32-1.
             return JS::DoubleValue(i_double);
         }
 
 // float
         case LOCAL_GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
-            if (mEnabledExtensions[WebGL_EXT_texture_filter_anisotropic]) {
+            if (IsExtensionEnabled(EXT_texture_filter_anisotropic)) {
                 GLfloat f = 0.f;
                 gl->fGetFloatv(pname, &f);
                 return JS::DoubleValue(f);
             } else {
                 ErrorInvalidEnum("getParameter: parameter", pname);
                 return JS::NullValue();
             }
         case LOCAL_GL_DEPTH_CLEAR_VALUE:
@@ -3109,17 +3109,17 @@ void WebGLContext::TexParameter_base(Web
                 case LOCAL_GL_REPEAT:
                     tex->SetWrapT(intParam);
                     break;
                 default:
                     pnameAndParamAreIncompatible = true;
             }
             break;
         case LOCAL_GL_TEXTURE_MAX_ANISOTROPY_EXT:
-            if (mEnabledExtensions[WebGL_EXT_texture_filter_anisotropic]) {
+            if (IsExtensionEnabled(EXT_texture_filter_anisotropic)) {
                 if (floatParamPtr && floatParam < 1.f)
                     paramValueInvalid = true;
                 else if (intParamPtr && intParam < 1)
                     paramValueInvalid = true;
             }
             else
                 pnameAndParamAreIncompatible = true;
             break;
@@ -3193,17 +3193,17 @@ WebGLContext::GetTexParameter(WebGLenum 
         case LOCAL_GL_TEXTURE_WRAP_S:
         case LOCAL_GL_TEXTURE_WRAP_T:
         {
             GLint i = 0;
             gl->fGetTexParameteriv(target, pname, &i);
             return JS::NumberValue(uint32_t(i));
         }
         case LOCAL_GL_TEXTURE_MAX_ANISOTROPY_EXT:
-            if (mEnabledExtensions[WebGL_EXT_texture_filter_anisotropic]) {
+            if (IsExtensionEnabled(EXT_texture_filter_anisotropic)) {
                 GLfloat f = 0.f;
                 gl->fGetTexParameterfv(target, pname, &f);
                 return JS::DoubleValue(f);
             }
 
             
             ErrorInvalidEnumInfo("getTexParameter: parameter", pname);
             break;
@@ -3524,17 +3524,17 @@ WebGLContext::Hint(WebGLenum target, Web
 
     bool isValid = false;
 
     switch (target) {
         case LOCAL_GL_GENERATE_MIPMAP_HINT:
             isValid = true;
             break;
         case LOCAL_GL_FRAGMENT_SHADER_DERIVATIVE_HINT:
-            if (mEnabledExtensions[WebGL_OES_standard_derivatives]) 
+            if (IsExtensionEnabled(OES_standard_derivatives))
                 isValid = true;
             break;
     }
 
     if (!isValid)
         return ErrorInvalidEnum("hint: invalid hint");
 
     gl->fHint(target, mode);
@@ -4913,17 +4913,17 @@ WebGLContext::CompileShader(WebGLShader 
         resources.MaxVertexAttribs = mGLMaxVertexAttribs;
         resources.MaxVertexUniformVectors = mGLMaxVertexUniformVectors;
         resources.MaxVaryingVectors = mGLMaxVaryingVectors;
         resources.MaxVertexTextureImageUnits = mGLMaxVertexTextureImageUnits;
         resources.MaxCombinedTextureImageUnits = mGLMaxTextureUnits;
         resources.MaxTextureImageUnits = mGLMaxTextureImageUnits;
         resources.MaxFragmentUniformVectors = mGLMaxFragmentUniformVectors;
         resources.MaxDrawBuffers = 1;
-        if (mEnabledExtensions[WebGL_OES_standard_derivatives])
+        if (IsExtensionEnabled(OES_standard_derivatives))
             resources.OES_standard_derivatives = 1;
 
         // We're storing an actual instance of StripComments because, if we don't, the 
         // cleanSource nsAString instance will be destroyed before the reference is
         // actually used.
         StripComments stripComments(shader->Source());
         const nsAString& cleanSource = nsString(stripComments.result().Elements(), stripComments.length());
         if (!ValidateGLSLString(cleanSource, "compileShader"))
--- a/content/canvas/src/WebGLContextValidate.cpp
+++ b/content/canvas/src/WebGLContextValidate.cpp
@@ -481,17 +481,17 @@ uint32_t WebGLContext::GetBitsPerTexel(W
     NS_ABORT();
     return 0;
 }
 
 bool WebGLContext::ValidateTexFormatAndType(WebGLenum format, WebGLenum type, int jsArrayType,
                                               uint32_t *texelSize, const char *info)
 {
     if (type == LOCAL_GL_UNSIGNED_BYTE ||
-        (IsExtensionEnabled(WebGL_OES_texture_float) && type == LOCAL_GL_FLOAT))
+        (IsExtensionEnabled(OES_texture_float) && type == LOCAL_GL_FLOAT))
     {
         if (jsArrayType != -1) {
             if ((type == LOCAL_GL_UNSIGNED_BYTE && jsArrayType != js::ArrayBufferView::TYPE_UINT8) ||
                 (type == LOCAL_GL_FLOAT && jsArrayType != js::ArrayBufferView::TYPE_FLOAT32))
             {
                 ErrorInvalidOperation("%s: invalid typed array type for given texture data type", info);
                 return false;
             }