Bug 1325476 - Use helper for Parameter[fi] funcs instead of maybeFloat/IntParam. - r=daoshengmu
authorJeff Gilbert <jgilbert@mozilla.com>
Thu, 22 Dec 2016 13:52:53 -0800
changeset 358151 8888afc873a200478d23dc45b52886badc66ab4f
parent 358150 13227477ca656ad458573798bcc8f7377ce37af0
child 358152 0f050ca0dbce82239e3108331fbe9f8f0f31bb7c
push id17
push userfmarier@mozilla.com
push dateFri, 13 Jan 2017 22:14:57 +0000
reviewersdaoshengmu
bugs1325476
milestone53.0a1
Bug 1325476 - Use helper for Parameter[fi] funcs instead of maybeFloat/IntParam. - r=daoshengmu MozReview-Commit-ID: 3jGFXfDNkPt
dom/canvas/WebGL2ContextSamplers.cpp
dom/canvas/WebGLContext.h
dom/canvas/WebGLContextTextures.cpp
dom/canvas/WebGLSampler.cpp
dom/canvas/WebGLSampler.h
dom/canvas/WebGLTexture.cpp
dom/canvas/WebGLTexture.h
--- a/dom/canvas/WebGL2ContextSamplers.cpp
+++ b/dom/canvas/WebGL2ContextSamplers.cpp
@@ -67,39 +67,39 @@ WebGL2Context::BindSampler(GLuint unit, 
     gl->MakeCurrent();
     gl->fBindSampler(unit, sampler ? sampler->mGLName : 0);
 
     InvalidateResolveCacheForTextureWithTexUnit(unit);
     mBoundSamplers[unit] = sampler;
 }
 
 void
-WebGL2Context::SamplerParameteri(WebGLSampler& sampler, GLenum pname, GLint paramInt)
+WebGL2Context::SamplerParameteri(WebGLSampler& sampler, GLenum pname, GLint param)
 {
     const char funcName[] = "samplerParameteri";
     if (IsContextLost())
         return;
 
     if (!ValidateObject(funcName, sampler))
         return;
 
-    sampler.SamplerParameter(funcName, pname, paramInt);
+    sampler.SamplerParameter(funcName, pname, FloatOrInt(param));
 }
 
 void
-WebGL2Context::SamplerParameterf(WebGLSampler& sampler, GLenum pname, GLfloat paramFloat)
+WebGL2Context::SamplerParameterf(WebGLSampler& sampler, GLenum pname, GLfloat param)
 {
     const char funcName[] = "samplerParameterf";
     if (IsContextLost())
         return;
 
     if (!ValidateObject(funcName, sampler))
         return;
 
-    sampler.SamplerParameter(funcName, pname, WebGLIntOrFloat(paramFloat).AsInt());
+    sampler.SamplerParameter(funcName, pname, FloatOrInt(param));
 }
 
 void
 WebGL2Context::GetSamplerParameter(JSContext*, const WebGLSampler& sampler, GLenum pname,
                                    JS::MutableHandleValue retval)
 {
     const char funcName[] = "getSamplerParameter";
     retval.setNull();
--- a/dom/canvas/WebGLContext.h
+++ b/dom/canvas/WebGLContext.h
@@ -190,16 +190,42 @@ struct IndexedBufferBinding
     uint64_t mRangeStart;
     uint64_t mRangeSize;
 
     IndexedBufferBinding();
 
     uint64_t ByteCount() const;
 };
 
+////
+
+struct FloatOrInt final // For TexParameter[fi] and friends.
+{
+    const bool isFloat;
+    const GLfloat f;
+    const GLint i;
+
+    explicit FloatOrInt(GLint x)
+        : isFloat(false)
+        , f(x)
+        , i(x)
+    { }
+
+    explicit FloatOrInt(GLfloat x)
+        : isFloat(true)
+        , f(x)
+        , i(roundf(x))
+    { }
+
+    FloatOrInt& operator =(const FloatOrInt& x) {
+        memcpy(this, &x, sizeof(x));
+        return *this;
+    }
+};
+
 ////////////////////////////////////
 
 struct TexImageSource
 {
     const dom::ArrayBufferView* mView;
     GLuint mViewElemOffset;
     GLuint mViewElemLengthOverride;
 
@@ -997,27 +1023,26 @@ public:
                          JS::MutableHandle<JS::Value> retval)
     {
         retval.set(GetTexParameter(texTarget, pname));
     }
 
     bool IsTexture(WebGLTexture* tex);
 
     void TexParameterf(GLenum texTarget, GLenum pname, GLfloat param) {
-        TexParameter_base(texTarget, pname, nullptr, &param);
+        TexParameter_base(texTarget, pname, FloatOrInt(param));
     }
 
     void TexParameteri(GLenum texTarget, GLenum pname, GLint param) {
-        TexParameter_base(texTarget, pname, &param, nullptr);
+        TexParameter_base(texTarget, pname, FloatOrInt(param));
     }
 
 protected:
     JS::Value GetTexParameter(GLenum texTarget, GLenum pname);
-    void TexParameter_base(GLenum texTarget, GLenum pname, GLint* maybeIntParam,
-                           GLfloat* maybeFloatParam);
+    void TexParameter_base(GLenum texTarget, GLenum pname, const FloatOrInt& param);
 
     virtual bool IsTexParamValid(GLenum pname) const;
 
     ////////////////////////////////////
 
 public:
     template<typename T>
     void CompressedTexImage2D(GLenum target, GLint level, GLenum internalFormat,
--- a/dom/canvas/WebGLContextTextures.cpp
+++ b/dom/canvas/WebGLContextTextures.cpp
@@ -292,30 +292,28 @@ WebGLContext::IsTexture(WebGLTexture* te
 {
     if (!ValidateIsObject("isTexture", tex))
         return false;
 
     return tex->IsTexture();
 }
 
 void
-WebGLContext::TexParameter_base(GLenum rawTexTarget, GLenum pname, GLint* maybeIntParam,
-                                GLfloat* maybeFloatParam)
+WebGLContext::TexParameter_base(GLenum rawTexTarget, GLenum pname,
+                                const FloatOrInt& param)
 {
-    MOZ_ASSERT(maybeIntParam || maybeFloatParam);
-
     const char funcName[] = "texParameter";
     const uint8_t funcDims = 0;
 
     TexTarget texTarget;
     WebGLTexture* tex;
     if (!ValidateTexTarget(this, funcName, funcDims, rawTexTarget, &texTarget, &tex))
         return;
 
-    tex->TexParameter(texTarget, pname, maybeIntParam, maybeFloatParam);
+    tex->TexParameter(texTarget, pname, param);
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
 // Uploads
 
 void
 WebGLContext::CompressedTexImage(const char* funcName, uint8_t funcDims, GLenum rawTarget,
                                  GLint level, GLenum internalFormat, GLsizei width,
--- a/dom/canvas/WebGLSampler.cpp
+++ b/dom/canvas/WebGLSampler.cpp
@@ -50,18 +50,20 @@ WebGLSampler::GetParentObject() const
 JSObject*
 WebGLSampler::WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto)
 {
     return dom::WebGLSamplerBinding::Wrap(cx, this, givenProto);
 }
 
 static bool
 ValidateSamplerParameterParams(WebGLContext* webgl, const char* funcName, GLenum pname,
-                               GLint paramInt)
+                               const FloatOrInt& param)
 {
+    const auto& paramInt = param.i;
+
     switch (pname) {
     case LOCAL_GL_TEXTURE_MIN_FILTER:
         switch (paramInt) {
         case LOCAL_GL_NEAREST:
         case LOCAL_GL_LINEAR:
         case LOCAL_GL_NEAREST_MIPMAP_NEAREST:
         case LOCAL_GL_NEAREST_MIPMAP_LINEAR:
         case LOCAL_GL_LINEAR_MIPMAP_NEAREST:
@@ -136,72 +138,77 @@ ValidateSamplerParameterParams(WebGLCont
         return false;
     }
 
     webgl->ErrorInvalidEnum("%s: invalid param: %s", funcName, webgl->EnumName(paramInt));
     return false;
 }
 
 void
-WebGLSampler::SamplerParameter(const char* funcName, GLenum pname, GLint paramInt)
+WebGLSampler::SamplerParameter(const char* funcName, GLenum pname,
+                               const FloatOrInt& param)
 {
-    if (!ValidateSamplerParameterParams(mContext, funcName, pname, paramInt))
+    if (!ValidateSamplerParameterParams(mContext, funcName, pname, param))
         return;
 
     switch (pname) {
     case LOCAL_GL_TEXTURE_MIN_FILTER:
-        mMinFilter = paramInt;
+        mMinFilter = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_MAG_FILTER:
-        mMagFilter = paramInt;
+        mMagFilter = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_WRAP_S:
-        mWrapS = paramInt;
+        mWrapS = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_WRAP_T:
-        mWrapT = paramInt;
+        mWrapT = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_WRAP_R:
-        mWrapR = paramInt;
+        mWrapR = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_COMPARE_MODE:
-        mCompareMode = paramInt;
+        mCompareMode = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_COMPARE_FUNC:
-        mCompareFunc = paramInt;
+        mCompareFunc = param.i;
         break;
 
     case LOCAL_GL_TEXTURE_MIN_LOD:
-        mMinLod = paramInt;
+        mMinLod = param.f;
         break;
 
     case LOCAL_GL_TEXTURE_MAX_LOD:
-        mMaxLod = paramInt;
+        mMaxLod = param.f;
         break;
 
     default:
         MOZ_CRASH("GFX: Unhandled pname");
         break;
     }
 
     for (uint32_t i = 0; i < mContext->mBoundSamplers.Length(); ++i) {
         if (this == mContext->mBoundSamplers[i])
             mContext->InvalidateResolveCacheForTextureWithTexUnit(i);
     }
 
     ////
 
     mContext->gl->MakeCurrent();
-    mContext->gl->fSamplerParameteri(mGLName, pname, paramInt);
+    if (param.isFloat) {
+        mContext->gl->fSamplerParameterf(mGLName, pname, param.f);
+    } else {
+        mContext->gl->fSamplerParameteri(mGLName, pname, param.i);
+    }
 }
 
 ////
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLSampler)
 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLSampler, AddRef)
 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLSampler, Release)
 
--- a/dom/canvas/WebGLSampler.h
+++ b/dom/canvas/WebGLSampler.h
@@ -26,29 +26,29 @@ public:
 
     const GLuint mGLName;
 
     void Delete();
     WebGLContext* GetParentObject() const;
 
     virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override;
 
-    void SamplerParameter(const char* funcName, GLenum pname, GLint paramInt);
+    void SamplerParameter(const char* funcName, GLenum pname, const FloatOrInt& param);
 
 private:
     NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLSampler)
     NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLSampler)
 
     TexMinFilter mMinFilter;
     TexMagFilter mMagFilter;
     TexWrap mWrapS;
     TexWrap mWrapT;
     TexWrap mWrapR;
-    GLint mMinLod;
-    GLint mMaxLod;
+    GLfloat mMinLod;
+    GLfloat mMaxLod;
     TexCompareMode mCompareMode;
     TexCompareFunc mCompareFunc;
 
 private:
     ~WebGLSampler();
 };
 
 } // namespace mozilla
--- a/dom/canvas/WebGLTexture.cpp
+++ b/dom/canvas/WebGLTexture.cpp
@@ -1000,24 +1000,18 @@ WebGLTexture::IsTexture() const
 {
     return HasEverBeenBound() && !IsDeleted();
 }
 
 // Here we have to support all pnames with both int and float params.
 // See this discussion:
 //   https://www.khronos.org/webgl/public-mailing-list/archives/1008/msg00014.html
 void
-WebGLTexture::TexParameter(TexTarget texTarget, GLenum pname, GLint* maybeIntParam,
-                           GLfloat* maybeFloatParam)
+WebGLTexture::TexParameter(TexTarget texTarget, GLenum pname, const FloatOrInt& param)
 {
-    MOZ_ASSERT(maybeIntParam || maybeFloatParam);
-
-    GLint   intParam   = maybeIntParam   ? *maybeIntParam   : GLint(roundf(*maybeFloatParam));
-    GLfloat floatParam = maybeFloatParam ? *maybeFloatParam : GLfloat(*maybeIntParam);
-
     bool isPNameValid = false;
     switch (pname) {
     // GLES 2.0.25 p76:
     case LOCAL_GL_TEXTURE_WRAP_S:
     case LOCAL_GL_TEXTURE_WRAP_T:
     case LOCAL_GL_TEXTURE_MIN_FILTER:
     case LOCAL_GL_TEXTURE_MAG_FILTER:
         isPNameValid = true;
@@ -1050,26 +1044,26 @@ WebGLTexture::TexParameter(TexTarget tex
     // Validate params and invalidate if needed.
 
     bool paramBadEnum = false;
     bool paramBadValue = false;
 
     switch (pname) {
     case LOCAL_GL_TEXTURE_BASE_LEVEL:
     case LOCAL_GL_TEXTURE_MAX_LEVEL:
-        paramBadValue = (intParam < 0);
+        paramBadValue = (param.i < 0);
         break;
 
     case LOCAL_GL_TEXTURE_COMPARE_MODE:
-        paramBadValue = (intParam != LOCAL_GL_NONE &&
-                         intParam != LOCAL_GL_COMPARE_REF_TO_TEXTURE);
+        paramBadValue = (param.i != LOCAL_GL_NONE &&
+                         param.i != LOCAL_GL_COMPARE_REF_TO_TEXTURE);
         break;
 
     case LOCAL_GL_TEXTURE_COMPARE_FUNC:
-        switch (intParam) {
+        switch (param.i) {
         case LOCAL_GL_LEQUAL:
         case LOCAL_GL_GEQUAL:
         case LOCAL_GL_LESS:
         case LOCAL_GL_GREATER:
         case LOCAL_GL_EQUAL:
         case LOCAL_GL_NOTEQUAL:
         case LOCAL_GL_ALWAYS:
         case LOCAL_GL_NEVER:
@@ -1077,125 +1071,124 @@ WebGLTexture::TexParameter(TexTarget tex
 
         default:
             paramBadValue = true;
             break;
         }
         break;
 
     case LOCAL_GL_TEXTURE_MIN_FILTER:
-        switch (intParam) {
+        switch (param.i) {
         case LOCAL_GL_NEAREST:
         case LOCAL_GL_LINEAR:
         case LOCAL_GL_NEAREST_MIPMAP_NEAREST:
         case LOCAL_GL_LINEAR_MIPMAP_NEAREST:
         case LOCAL_GL_NEAREST_MIPMAP_LINEAR:
         case LOCAL_GL_LINEAR_MIPMAP_LINEAR:
             break;
 
         default:
             paramBadEnum = true;
             break;
         }
         break;
 
     case LOCAL_GL_TEXTURE_MAG_FILTER:
-        switch (intParam) {
+        switch (param.i) {
         case LOCAL_GL_NEAREST:
         case LOCAL_GL_LINEAR:
             break;
 
         default:
             paramBadEnum = true;
             break;
         }
         break;
 
     case LOCAL_GL_TEXTURE_WRAP_S:
     case LOCAL_GL_TEXTURE_WRAP_T:
     case LOCAL_GL_TEXTURE_WRAP_R:
-        switch (intParam) {
+        switch (param.i) {
         case LOCAL_GL_CLAMP_TO_EDGE:
         case LOCAL_GL_MIRRORED_REPEAT:
         case LOCAL_GL_REPEAT:
             break;
 
         default:
             paramBadEnum = true;
             break;
         }
         break;
 
     case LOCAL_GL_TEXTURE_MAX_ANISOTROPY_EXT:
-        if (maybeFloatParam && floatParam < 1.0f)
-            paramBadValue = true;
-        else if (maybeIntParam && intParam < 1)
+        if (param.f < 1.0f)
             paramBadValue = true;
 
         break;
     }
 
     if (paramBadEnum) {
-        if (maybeIntParam) {
+        if (!param.isFloat) {
             mContext->ErrorInvalidEnum("texParameteri: pname 0x%04x: Invalid param"
                                        " 0x%04x.",
-                                       pname, intParam);
+                                       pname, param.i);
         } else {
             mContext->ErrorInvalidEnum("texParameterf: pname 0x%04x: Invalid param %g.",
-                                       pname, floatParam);
+                                       pname, param.f);
         }
         return;
     }
 
     if (paramBadValue) {
-        if (maybeIntParam) {
+        if (!param.isFloat) {
             mContext->ErrorInvalidValue("texParameteri: pname 0x%04x: Invalid param %i"
                                         " (0x%x).",
-                                        pname, intParam, intParam);
+                                        pname, param.i, param.i);
         } else {
             mContext->ErrorInvalidValue("texParameterf: pname 0x%04x: Invalid param %g.",
-                                        pname, floatParam);
+                                        pname, param.f);
         }
         return;
     }
 
     ////////////////
     // Store any needed values
 
+    FloatOrInt clamped = param;
     switch (pname) {
     case LOCAL_GL_TEXTURE_BASE_LEVEL:
-        mBaseMipmapLevel = intParam;
+        mBaseMipmapLevel = clamped.i;
         ClampLevelBaseAndMax();
-        intParam = mBaseMipmapLevel;
+        clamped = FloatOrInt(GLint(mBaseMipmapLevel));
         break;
 
     case LOCAL_GL_TEXTURE_MAX_LEVEL:
-        mMaxMipmapLevel = intParam;
+        mMaxMipmapLevel = clamped.i;
         ClampLevelBaseAndMax();
-        intParam = mMaxMipmapLevel;
+        clamped = FloatOrInt(GLint(mMaxMipmapLevel));
         break;
 
     case LOCAL_GL_TEXTURE_MIN_FILTER:
-        mMinFilter = intParam;
+        mMinFilter = clamped.i;
         break;
 
     case LOCAL_GL_TEXTURE_MAG_FILTER:
-        mMagFilter = intParam;
+        mMagFilter = clamped.i;
         break;
 
     case LOCAL_GL_TEXTURE_WRAP_S:
-        mWrapS = intParam;
+        mWrapS = clamped.i;
         break;
 
     case LOCAL_GL_TEXTURE_WRAP_T:
-        mWrapT = intParam;
+        mWrapT = clamped.i;
         break;
 
     case LOCAL_GL_TEXTURE_COMPARE_MODE:
-        mTexCompareMode = intParam;
+        mTexCompareMode = clamped.i;
         break;
 
     // We don't actually need to store the WRAP_R, since it doesn't change texture
     // completeness rules.
     }
 
     // Only a couple of pnames don't need to invalidate our resolve status cache.
     switch (pname) {
@@ -1206,20 +1199,20 @@ WebGLTexture::TexParameter(TexTarget tex
     default:
         InvalidateResolveCache();
         break;
     }
 
     ////////////////
 
     mContext->MakeContextCurrent();
-    if (maybeIntParam)
-        mContext->gl->fTexParameteri(texTarget.get(), pname, intParam);
+    if (!clamped.isFloat)
+        mContext->gl->fTexParameteri(texTarget.get(), pname, clamped.i);
     else
-        mContext->gl->fTexParameterf(texTarget.get(), pname, floatParam);
+        mContext->gl->fTexParameterf(texTarget.get(), pname, clamped.f);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLTexture)
 
 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLTexture, AddRef)
 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLTexture, Release)
--- a/dom/canvas/WebGLTexture.h
+++ b/dom/canvas/WebGLTexture.h
@@ -20,16 +20,17 @@
 #include "WebGLFramebufferAttachable.h"
 #include "WebGLObjectModel.h"
 #include "WebGLStrongTypes.h"
 #include "WebGLTypes.h"
 
 namespace mozilla {
 class ErrorResult;
 class WebGLContext;
+struct FloatOrInt;
 struct TexImageSource;
 
 namespace dom {
 class Element;
 class HTMLVideoElement;
 class ImageData;
 class ArrayBufferViewOrSharedArrayBufferView;
 } // namespace dom
@@ -221,18 +222,17 @@ protected:
 
 public:
     ////////////////////////////////////
     // GL calls
     bool BindTexture(TexTarget texTarget);
     void GenerateMipmap(TexTarget texTarget);
     JS::Value GetTexParameter(TexTarget texTarget, GLenum pname);
     bool IsTexture() const;
-    void TexParameter(TexTarget texTarget, GLenum pname, GLint* maybeIntParam,
-                      GLfloat* maybeFloatParam);
+    void TexParameter(TexTarget texTarget, GLenum pname, const FloatOrInt& param);
 
     ////////////////////////////////////
     // WebGLTextureUpload.cpp
 
 protected:
     void TexOrSubImageBlob(bool isSubImage, const char* funcName, TexImageTarget target,
                            GLint level, GLenum internalFormat, GLint xOffset,
                            GLint yOffset, GLint zOffset,