Bug 1515052 - Use WeakPtr for extension parent pointer. r=lsalzman a=jcristau
authorJeff Gilbert <jgilbert@mozilla.com>
Fri, 24 May 2019 12:04:12 +0000
changeset 536713 88114c48b1eeac094cd9bc707f0029dd8dc8e857
parent 536712 9b3c45bd2e778ae1ab2da8d4a79d3771619d626b
child 536714 0b0766cf7b2232d80f73710b3a1bfa4fb9a4864f
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslsalzman, jcristau
bugs1515052
milestone68.0
Bug 1515052 - Use WeakPtr for extension parent pointer. r=lsalzman a=jcristau Differential Revision: https://phabricator.services.mozilla.com/D31942
dom/canvas/WebGLContext.h
dom/canvas/WebGLExtensionCompressedTextureASTC.cpp
dom/canvas/WebGLExtensionDebugShaders.cpp
dom/canvas/WebGLExtensionDisjointTimerQuery.cpp
dom/canvas/WebGLExtensionDrawBuffers.cpp
dom/canvas/WebGLExtensionInstancedArrays.cpp
dom/canvas/WebGLExtensionLoseContext.cpp
dom/canvas/WebGLExtensionMOZDebug.cpp
dom/canvas/WebGLExtensionVertexArray.cpp
dom/canvas/WebGLObjectModel.h
--- a/dom/canvas/WebGLContext.h
+++ b/dom/canvas/WebGLContext.h
@@ -16,16 +16,17 @@
 #include "mozilla/dom/HTMLCanvasElement.h"
 #include "mozilla/dom/Nullable.h"
 #include "mozilla/dom/TypedArray.h"
 #include "mozilla/EnumeratedArray.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/gfx/2D.h"
 #include "mozilla/LinkedList.h"
 #include "mozilla/UniquePtr.h"
+#include "mozilla/WeakPtr.h"
 #include "nsCycleCollectionNoteChild.h"
 #include "nsICanvasRenderingContextInternal.h"
 #include "nsLayoutUtils.h"
 #include "nsTArray.h"
 #include "nsWrapperCache.h"
 #include "SurfaceTypes.h"
 #include "ScopedGLHelpers.h"
 #include "TexUnpackBlob.h"
@@ -263,17 +264,18 @@ class AvailabilityRunnable final : publi
   NS_IMETHOD Run() override;
 };
 }  // namespace webgl
 
 ////////////////////////////////////////////////////////////////////////////////
 
 class WebGLContext : public nsICanvasRenderingContextInternal,
                      public nsSupportsWeakReference,
-                     public nsWrapperCache {
+                     public nsWrapperCache,
+                     public SupportsWeakPtr<WebGLContext> {
   friend class ScopedDrawCallWrapper;
   friend class ScopedDrawWithTransformFeedback;
   friend class ScopedFakeVertexAttrib0;
   friend class ScopedFBRebinder;
   friend class WebGL2Context;
   friend class WebGLContextUserData;
   friend class WebGLExtensionCompressedTextureASTC;
   friend class WebGLExtensionCompressedTextureBPTC;
@@ -338,19 +340,19 @@ class WebGLContext : public nsICanvasRen
  public:
   WebGLContext();
 
  protected:
   virtual ~WebGLContext();
 
  public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
-
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(
       WebGLContext, nsICanvasRenderingContextInternal)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(WebGLContext)
 
   virtual JSObject* WrapObject(JSContext* cx,
                                JS::Handle<JSObject*> givenProto) override = 0;
 
   virtual void OnMemoryPressure() override;
 
   // nsICanvasRenderingContextInternal
   virtual int32_t GetWidth() override { return DrawingBufferWidth(); }
--- a/dom/canvas/WebGLExtensionCompressedTextureASTC.cpp
+++ b/dom/canvas/WebGLExtensionCompressedTextureASTC.cpp
@@ -64,18 +64,20 @@ WebGLExtensionCompressedTextureASTC::Web
 }
 
 WebGLExtensionCompressedTextureASTC::~WebGLExtensionCompressedTextureASTC() {}
 
 void WebGLExtensionCompressedTextureASTC::GetSupportedProfiles(
     dom::Nullable<nsTArray<nsString> >& retval) const {
   retval.SetNull();
   if (mIsLost) {
-    mContext->ErrorInvalidOperation("%s: Extension is lost.",
-                                    "drawElementsInstancedANGLE");
+    if (mContext) {
+      mContext->ErrorInvalidOperation("%s: Extension is lost.",
+                                      "drawElementsInstancedANGLE");
+    }
     return;
   }
 
   nsTArray<nsString>& arr = retval.SetValue();
   arr.AppendElement(NS_LITERAL_STRING("ldr"));
 
   if (mContext->gl->IsExtensionSupported(
           gl::GLContext::KHR_texture_compression_astc_hdr)) {
--- a/dom/canvas/WebGLExtensionDebugShaders.cpp
+++ b/dom/canvas/WebGLExtensionDebugShaders.cpp
@@ -17,17 +17,17 @@ WebGLExtensionDebugShaders::WebGLExtensi
 WebGLExtensionDebugShaders::~WebGLExtensionDebugShaders() {}
 
 // If no source has been defined, compileShader() has not been called, or the
 // translation has failed for shader, an empty string is returned; otherwise,
 // return the translated source.
 void WebGLExtensionDebugShaders::GetTranslatedShaderSource(
     const WebGLShader& shader, nsAString& retval) const {
   retval.SetIsVoid(true);
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
 
   const WebGLContext::FuncScope funcScope(*mContext,
                                           "getShaderTranslatedSource");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   if (!mContext->ValidateObject("shader", shader)) return;
 
   shader.GetShaderTranslatedSource(&retval);
--- a/dom/canvas/WebGLExtensionDisjointTimerQuery.cpp
+++ b/dom/canvas/WebGLExtensionDisjointTimerQuery.cpp
@@ -29,76 +29,76 @@ already_AddRefed<WebGLQuery> WebGLExtens
   if (mIsLost) return nullptr;
   const WebGLContext::FuncScope funcScope(*mContext, "createQueryEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   return mContext->CreateQuery();
 }
 
 void WebGLExtensionDisjointTimerQuery::DeleteQueryEXT(WebGLQuery* query) const {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "deleteQueryEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   mContext->DeleteQuery(query);
 }
 
 bool WebGLExtensionDisjointTimerQuery::IsQueryEXT(
     const WebGLQuery* query) const {
-  if (mIsLost) return false;
+  if (mIsLost || !mContext) return false;
   const WebGLContext::FuncScope funcScope(*mContext, "isQueryEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   return mContext->IsQuery(query);
 }
 
 void WebGLExtensionDisjointTimerQuery::BeginQueryEXT(GLenum target,
                                                      WebGLQuery& query) const {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "beginQueryEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   mContext->BeginQuery(target, query);
 }
 
 void WebGLExtensionDisjointTimerQuery::EndQueryEXT(GLenum target) const {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "endQueryEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   mContext->EndQuery(target);
 }
 
 void WebGLExtensionDisjointTimerQuery::QueryCounterEXT(WebGLQuery& query,
                                                        GLenum target) const {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "queryCounterEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   if (!mContext->ValidateObject("query", query)) return;
 
   query.QueryCounter(target);
 }
 
 void WebGLExtensionDisjointTimerQuery::GetQueryEXT(
     JSContext* cx, GLenum target, GLenum pname,
     JS::MutableHandleValue retval) const {
   retval.setNull();
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "getQueryEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   mContext->GetQuery(cx, target, pname, retval);
 }
 
 void WebGLExtensionDisjointTimerQuery::GetQueryObjectEXT(
     JSContext* cx, const WebGLQuery& query, GLenum pname,
     JS::MutableHandleValue retval) const {
   retval.setNull();
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "getQueryObjectEXT");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   mContext->GetQueryParameter(cx, query, pname, retval);
 }
 
 bool WebGLExtensionDisjointTimerQuery::IsSupported(const WebGLContext* webgl) {
   gl::GLContext* gl = webgl->GL();
--- a/dom/canvas/WebGLExtensionDrawBuffers.cpp
+++ b/dom/canvas/WebGLExtensionDrawBuffers.cpp
@@ -22,17 +22,19 @@ WebGLExtensionDrawBuffers::WebGLExtensio
   webgl->UpdateMaxDrawBuffers();
 }
 
 WebGLExtensionDrawBuffers::~WebGLExtensionDrawBuffers() {}
 
 void WebGLExtensionDrawBuffers::DrawBuffersWEBGL(
     const dom::Sequence<GLenum>& buffers) {
   if (mIsLost) {
-    mContext->ErrorInvalidOperation("drawBuffersWEBGL: Extension is lost.");
+    if (mContext) {
+      mContext->ErrorInvalidOperation("drawBuffersWEBGL: Extension is lost.");
+    }
     return;
   }
 
   mContext->DrawBuffers(buffers);
 }
 
 bool WebGLExtensionDrawBuffers::IsSupported(const WebGLContext* webgl) {
   if (webgl->IsWebGL2()) return false;
--- a/dom/canvas/WebGLExtensionInstancedArrays.cpp
+++ b/dom/canvas/WebGLExtensionInstancedArrays.cpp
@@ -17,41 +17,47 @@ WebGLExtensionInstancedArrays::WebGLExte
   MOZ_ASSERT(IsSupported(webgl), "Don't construct extension if unsupported.");
 }
 
 WebGLExtensionInstancedArrays::~WebGLExtensionInstancedArrays() {}
 
 void WebGLExtensionInstancedArrays::DrawArraysInstancedANGLE(
     GLenum mode, GLint first, GLsizei count, GLsizei primcount) {
   if (mIsLost) {
-    mContext->ErrorInvalidOperation("%s: Extension is lost.",
-                                    "drawArraysInstancedANGLE");
+    if (mContext) {
+      mContext->ErrorInvalidOperation("%s: Extension is lost.",
+                                      "drawArraysInstancedANGLE");
+    }
     return;
   }
 
   mContext->DrawArraysInstanced(mode, first, count, primcount);
 }
 
 void WebGLExtensionInstancedArrays::DrawElementsInstancedANGLE(
     GLenum mode, GLsizei count, GLenum type, WebGLintptr offset,
     GLsizei primcount) {
   if (mIsLost) {
-    mContext->ErrorInvalidOperation("%s: Extension is lost.",
-                                    "drawElementsInstancedANGLE");
+    if (mContext) {
+      mContext->ErrorInvalidOperation("%s: Extension is lost.",
+                                      "drawElementsInstancedANGLE");
+    }
     return;
   }
 
   mContext->DrawElementsInstanced(mode, count, type, offset, primcount);
 }
 
 void WebGLExtensionInstancedArrays::VertexAttribDivisorANGLE(GLuint index,
                                                              GLuint divisor) {
   if (mIsLost) {
-    mContext->ErrorInvalidOperation("%s: Extension is lost.",
-                                    "vertexAttribDivisorANGLE");
+    if (mContext) {
+      mContext->ErrorInvalidOperation("%s: Extension is lost.",
+                                      "vertexAttribDivisorANGLE");
+    }
     return;
   }
 
   mContext->VertexAttribDivisor(index, divisor);
 }
 
 bool WebGLExtensionInstancedArrays::IsSupported(const WebGLContext* webgl) {
   if (webgl->IsWebGL2()) return false;
--- a/dom/canvas/WebGLExtensionLoseContext.cpp
+++ b/dom/canvas/WebGLExtensionLoseContext.cpp
@@ -10,15 +10,21 @@
 
 namespace mozilla {
 
 WebGLExtensionLoseContext::WebGLExtensionLoseContext(WebGLContext* webgl)
     : WebGLExtensionBase(webgl) {}
 
 WebGLExtensionLoseContext::~WebGLExtensionLoseContext() {}
 
-void WebGLExtensionLoseContext::LoseContext() { mContext->LoseContext(); }
+void WebGLExtensionLoseContext::LoseContext() {
+  if (!mContext) return;
+  mContext->LoseContext();
+}
 
-void WebGLExtensionLoseContext::RestoreContext() { mContext->RestoreContext(); }
+void WebGLExtensionLoseContext::RestoreContext() {
+  if (!mContext) return;
+  mContext->RestoreContext();
+}
 
 IMPL_WEBGL_EXTENSION_GOOP(WebGLExtensionLoseContext, WEBGL_lose_context)
 
 }  // namespace mozilla
--- a/dom/canvas/WebGLExtensionMOZDebug.cpp
+++ b/dom/canvas/WebGLExtensionMOZDebug.cpp
@@ -15,17 +15,17 @@ namespace mozilla {
 WebGLExtensionMOZDebug::WebGLExtensionMOZDebug(WebGLContext* webgl)
     : WebGLExtensionBase(webgl) {}
 
 WebGLExtensionMOZDebug::~WebGLExtensionMOZDebug() {}
 
 void WebGLExtensionMOZDebug::GetParameter(JSContext* cx, GLenum pname,
                                           JS::MutableHandle<JS::Value> retval,
                                           ErrorResult& er) const {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
   const WebGLContext::FuncScope funcScope(*mContext, "MOZ_debug.getParameter");
   MOZ_ASSERT(!mContext->IsContextLost());
 
   const auto& gl = mContext->gl;
 
   switch (pname) {
     case LOCAL_GL_EXTENSIONS: {
       nsString ret;
--- a/dom/canvas/WebGLExtensionVertexArray.cpp
+++ b/dom/canvas/WebGLExtensionVertexArray.cpp
@@ -15,35 +15,35 @@ namespace mozilla {
 
 WebGLExtensionVertexArray::WebGLExtensionVertexArray(WebGLContext* webgl)
     : WebGLExtensionBase(webgl) {}
 
 WebGLExtensionVertexArray::~WebGLExtensionVertexArray() {}
 
 already_AddRefed<WebGLVertexArray>
 WebGLExtensionVertexArray::CreateVertexArrayOES() {
-  if (mIsLost) return nullptr;
+  if (mIsLost || !mContext) return nullptr;
 
   return mContext->CreateVertexArray();
 }
 
 void WebGLExtensionVertexArray::DeleteVertexArrayOES(WebGLVertexArray* array) {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
 
   mContext->DeleteVertexArray(array);
 }
 
 bool WebGLExtensionVertexArray::IsVertexArrayOES(
     const WebGLVertexArray* array) {
-  if (mIsLost) return false;
+  if (mIsLost || !mContext) return false;
 
   return mContext->IsVertexArray(array);
 }
 
 void WebGLExtensionVertexArray::BindVertexArrayOES(WebGLVertexArray* array) {
-  if (mIsLost) return;
+  if (mIsLost || !mContext) return;
 
   mContext->BindVertexArray(array);
 }
 
 IMPL_WEBGL_EXTENSION_GOOP(WebGLExtensionVertexArray, OES_vertex_array_object)
 
 }  // namespace mozilla
--- a/dom/canvas/WebGLObjectModel.h
+++ b/dom/canvas/WebGLObjectModel.h
@@ -1,34 +1,34 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef WEBGLOBJECTMODEL_H_
 #define WEBGLOBJECTMODEL_H_
 
+#include "mozilla/WeakPtr.h"
 #include "nsCycleCollectionNoteChild.h"
-
 #include "WebGLTypes.h"
 
 namespace mozilla {
 
 template <typename>
 class LinkedList;
 class WebGLContext;
 
 ////
 
 // This class is a mixin for objects that are tied to a specific
 // context (which is to say, all of them).  They provide initialization
 // as well as comparison with the current context.
 class WebGLContextBoundObject {
  public:
-  WebGLContext* const mContext;
+  const WeakPtr<WebGLContext> mContext;
 
  private:
   const uint32_t mContextGeneration;
 
  public:
   explicit WebGLContextBoundObject(WebGLContext* webgl);
 
   bool IsCompatibleWithContext(const WebGLContext* other) const;