Bug 1288643 - mContext is mutable, so is now protected. - r=jerry draft
authorJeff Gilbert <jgilbert@mozilla.com>
Thu, 21 Jul 2016 23:36:14 -0700
changeset 391214 2daec738be0ec28df1503a2b637606f9ebb76dcd
parent 391213 ba1bd91de8858daeeac68e84736e193e87b2a34a
child 391215 36673ebb956b1e54d5ddac6b650fe81a34863315
push id23845
push userbmo:jgilbert@mozilla.com
push dateFri, 22 Jul 2016 08:40:49 +0000
reviewersjerry
bugs1288643
milestone50.0a1
Bug 1288643 - mContext is mutable, so is now protected. - r=jerry MozReview-Commit-ID: 1R2qZVngSYI
dom/canvas/TexUnpackBlob.cpp
dom/canvas/WebGLContextUtils.h
dom/canvas/WebGLFramebuffer.cpp
dom/canvas/WebGLObjectModel.h
dom/canvas/WebGLProgram.cpp
dom/canvas/WebGLTextureUpload.cpp
--- a/dom/canvas/TexUnpackBlob.cpp
+++ b/dom/canvas/TexUnpackBlob.cpp
@@ -327,17 +327,17 @@ TexUnpackBytes::TexUnpackBytes(const Web
 { }
 
 bool
 TexUnpackBytes::TexOrSubImage(bool isSubImage, bool needsRespec, const char* funcName,
                               WebGLTexture* tex, TexImageTarget target, GLint level,
                               const webgl::DriverUnpackInfo* dui, GLint xOffset,
                               GLint yOffset, GLint zOffset, GLenum* const out_error) const
 {
-    WebGLContext* webgl = tex->mContext;
+    const auto webgl = tex->Context();
 
     const auto pi = dui->ToPacking();
 
     const auto bytesPerPixel = webgl::BytesPerPixel(pi);
     const auto bytesPerRow = CheckedUint32(mRowLength) * bytesPerPixel;
     const auto rowStride = RoundUpToMultipleOf(bytesPerRow, mAlignment);
     if (!rowStride.isValid()) {
         MOZ_CRASH("Should be checked earlier.");
@@ -373,17 +373,17 @@ TexUnpackImage::TexUnpackImage(const Web
 bool
 TexUnpackImage::TexOrSubImage(bool isSubImage, bool needsRespec, const char* funcName,
                               WebGLTexture* tex, TexImageTarget target, GLint level,
                               const webgl::DriverUnpackInfo* dui, GLint xOffset,
                               GLint yOffset, GLint zOffset, GLenum* const out_error) const
 {
     MOZ_ASSERT_IF(needsRespec, !isSubImage);
 
-    WebGLContext* webgl = tex->mContext;
+    const auto webgl = tex->Context();
 
     gl::GLContext* gl = webgl->GL();
     gl->MakeCurrent();
 
     if (needsRespec) {
         *out_error = DoTexOrSubImage(isSubImage, gl, target.get(), level, dui, xOffset,
                                      yOffset, zOffset, mWidth, mHeight, mDepth,
                                      nullptr);
@@ -527,17 +527,17 @@ GetFormatForSurf(gfx::SourceSurface* sur
 
 bool
 TexUnpackSurface::TexOrSubImage(bool isSubImage, bool needsRespec, const char* funcName,
                                 WebGLTexture* tex, TexImageTarget target, GLint level,
                                 const webgl::DriverUnpackInfo* dstDUI, GLint xOffset,
                                 GLint yOffset, GLint zOffset,
                                 GLenum* const out_error) const
 {
-    WebGLContext* webgl = tex->mContext;
+    const auto webgl = tex->Context();
 
     WebGLTexelFormat srcFormat;
     uint8_t srcBPP;
     if (!GetFormatForSurf(mSurf, &srcFormat, &srcBPP)) {
         webgl->ErrorImplementationBug("%s: GetFormatForSurf failed for"
                                       " WebGLTexelFormat::%u.",
                                       funcName, uint32_t(mSurf->GetFormat()));
         return false;
--- a/dom/canvas/WebGLContextUtils.h
+++ b/dom/canvas/WebGLContextUtils.h
@@ -59,17 +59,17 @@ struct GLComponents
 template <typename WebGLObjectType>
 JS::Value
 WebGLContext::WebGLObjectAsJSValue(JSContext* cx, const WebGLObjectType* object,
                                    ErrorResult& rv) const
 {
     if (!object)
         return JS::NullValue();
 
-    MOZ_ASSERT(this == object->mContext);
+    MOZ_ASSERT(this == object->Context());
     JS::Rooted<JS::Value> v(cx);
     JS::Rooted<JSObject*> wrapper(cx, GetWrapper());
     JSAutoCompartment ac(cx, wrapper);
     if (!dom::GetOrCreateDOMReflector(cx, const_cast<WebGLObjectType*>(object), &v)) {
         rv.Throw(NS_ERROR_FAILURE);
         return JS::NullValue();
     }
     return v;
--- a/dom/canvas/WebGLFramebuffer.cpp
+++ b/dom/canvas/WebGLFramebuffer.cpp
@@ -358,17 +358,17 @@ WebGLFBAttachPoint::FinalizeAttachment(g
             break;
         }
 
         return;
     }
     MOZ_ASSERT(HasImage());
 
     if (Texture()) {
-        MOZ_ASSERT(gl == Texture()->mContext->GL());
+        MOZ_ASSERT(gl == Texture()->Context()->GL());
 
         const GLenum imageTarget = ImageTarget().get();
         const GLint mipLevel = MipLevel();
         const GLint layer = Layer();
         const GLuint glName = Texture()->mGLName;
 
         switch (imageTarget) {
         case LOCAL_GL_TEXTURE_2D:
--- a/dom/canvas/WebGLObjectModel.h
+++ b/dom/canvas/WebGLObjectModel.h
@@ -258,26 +258,40 @@ private:
 protected:
     T* mRawPtr;
 };
 
 // This class is a mixin for objects that are tied to a specific
 // context (which is to say, all of them).
 class WebGLContextBoundObject
 {
+    friend class WebGLContext;
+
+protected:
     WebGLContext* mContext;
 
     explicit WebGLContextBoundObject(WebGLContext* webgl)
         : mContext(webgl)
-    { }
+    {
+        MOZ_ASSERT(mContext);
+    }
 
     virtual ~WebGLContextBoundObject() {
         MOZ_ASSERT(!mContext, "Should be Detach()'d first before destroyed.");
     }
 
+    void DetachOnce() {
+        if (mContext) {
+            Detach();
+        }
+    }
+
+public:
+    const decltype(mContext)& Context() const { return mContext; }
+
 private:
     void Detach();
     virtual void DetachImpl() = 0;
 };
 
 // this class is a mixin for GL objects that have dimensions
 // that we need to track.
 class WebGLRectangleObject
--- a/dom/canvas/WebGLProgram.cpp
+++ b/dom/canvas/WebGLProgram.cpp
@@ -116,17 +116,17 @@ webgl::UniformInfo::UniformInfo(WebGLAct
 
 //////////
 
 //#define DUMP_SHADERVAR_MAPPINGS
 
 static already_AddRefed<const webgl::LinkedProgramInfo>
 QueryProgramInfo(WebGLProgram* prog, gl::GLContext* gl)
 {
-    WebGLContext* const webgl = prog->mContext;
+    const auto& webgl = prog->Context();
 
     RefPtr<webgl::LinkedProgramInfo> info(new webgl::LinkedProgramInfo(prog));
 
     GLuint maxAttribLenWithNull = 0;
     gl->fGetProgramiv(prog->mGLName, LOCAL_GL_ACTIVE_ATTRIBUTE_MAX_LENGTH,
                       (GLint*)&maxAttribLenWithNull);
     if (maxAttribLenWithNull < 1)
         maxAttribLenWithNull = 1;
--- a/dom/canvas/WebGLTextureUpload.cpp
+++ b/dom/canvas/WebGLTextureUpload.cpp
@@ -765,17 +765,17 @@ EnsureImageDataInitializedForUpload(WebG
     if (!imageInfo->IsDataInitialized()) {
         const bool isFullUpload = (!xOffset && !yOffset && !zOffset &&
                                    width == imageInfo->mWidth &&
                                    height == imageInfo->mHeight &&
                                    depth == imageInfo->mDepth);
         if (isFullUpload) {
             *out_uploadWillInitialize = true;
         } else {
-            WebGLContext* webgl = tex->mContext;
+            const auto& webgl = tex->Context();
             webgl->GenerateWarning("%s: Texture has not been initialized prior to a"
                                    " partial upload, forcing the browser to clear it."
                                    " This may be slow.",
                                    funcName);
             if (!tex->InitializeImageData(funcName, target, level)) {
                 MOZ_ASSERT(false, "Unexpected failure to init image data.");
                 return false;
             }