Bug 914823 - Use a WeakPtr to the SurfaceAllocator - r=jgilbert,nical, a=koi+
authorBenoit Jacob <bjacob@mozilla.com>
Wed, 09 Oct 2013 08:47:32 -0400
changeset 160619 3f16dc100b1f4e6bb57e8c2959b9fa0cc249f624
parent 160618 5c2a7c96df5284687054fce166179535e6396168
child 160620 f14bc3ab62a94adcf1f06fadd28bb744649464c1
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgilbert, nical, koi
bugs914823
milestone26.0a2
Bug 914823 - Use a WeakPtr to the SurfaceAllocator - r=jgilbert,nical, a=koi+
gfx/gl/SharedSurfaceGralloc.cpp
gfx/gl/SharedSurfaceGralloc.h
gfx/layers/ipc/ISurfaceAllocator.h
--- a/gfx/gl/SharedSurfaceGralloc.cpp
+++ b/gfx/gl/SharedSurfaceGralloc.cpp
@@ -38,17 +38,17 @@ SurfaceFactory_Gralloc::SurfaceFactory_G
     : SurfaceFactory_GL(prodGL, SharedSurfaceType::Gralloc, caps)
 {
     if (caps.surfaceAllocator) {
         allocator = caps.surfaceAllocator;
     }
 
     MOZ_ASSERT(allocator);
 
-    mAllocator = allocator;
+    mAllocator = allocator->asWeakPtr();
 }
 
 SharedSurface_Gralloc*
 SharedSurface_Gralloc::Create(GLContext* prodGL,
                               const GLFormats& formats,
                               const gfxIntSize& size,
                               bool hasAlpha,
                               ISurfaceAllocator* allocator)
@@ -127,17 +127,20 @@ SharedSurface_Gralloc::~SharedSurface_Gr
 {
 
     DEBUG_PRINT("[SharedSurface_Gralloc %p] destroyed\n", this);
 
     mGL->MakeCurrent();
     mGL->fDeleteTextures(1, (GLuint*)&mProdTex);
 
     SurfaceDescriptor desc(mDesc);
-    mAllocator->DestroySharedSurface(&desc);
+
+    if (mAllocator) {
+        mAllocator->DestroySharedSurface(&desc);
+    }
 }
 
 void
 SharedSurface_Gralloc::Fence()
 {
     // We should be able to rely on genlock write locks/read locks.
     // But they're broken on some configs, and even a glFinish doesn't
     // work.  glReadPixels seems to, though.
--- a/gfx/gl/SharedSurfaceGralloc.h
+++ b/gfx/gl/SharedSurfaceGralloc.h
@@ -3,16 +3,17 @@
  * 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 SHARED_SURFACE_GRALLOC_H_
 #define SHARED_SURFACE_GRALLOC_H_
 
 #include "SharedSurfaceGL.h"
 #include "mozilla/layers/LayersSurfaces.h"
+#include "mozilla/layers/ISurfaceAllocator.h"
 
 namespace mozilla {
 namespace layers {
 class ISurfaceAllocator;
 class SurfaceDescriptorGralloc;
 }
 
 namespace gl {
@@ -87,25 +88,28 @@ public:
         return mDesc;
     }
 };
 
 class SurfaceFactory_Gralloc
     : public SurfaceFactory_GL
 {
 protected:
-    layers::ISurfaceAllocator* mAllocator;
+    WeakPtr<layers::ISurfaceAllocator> mAllocator;
 
 public:
     SurfaceFactory_Gralloc(GLContext* prodGL,
                            const SurfaceCaps& caps,
                            layers::ISurfaceAllocator* allocator = nullptr);
 
     virtual SharedSurface* CreateShared(const gfxIntSize& size) {
         bool hasAlpha = mReadCaps.alpha;
+        if (!mAllocator) {
+            return nullptr;
+        }
         return SharedSurface_Gralloc::Create(mGL, mFormats, size, hasAlpha, mAllocator);
     }
 };
 
 } /* namespace gl */
 } /* namespace mozilla */
 
 #endif /* SHARED_SURFACE_GRALLOC_H_ */
--- a/gfx/layers/ipc/ISurfaceAllocator.h
+++ b/gfx/layers/ipc/ISurfaceAllocator.h
@@ -6,16 +6,17 @@
 #ifndef GFX_LAYERS_ISURFACEDEALLOCATOR
 #define GFX_LAYERS_ISURFACEDEALLOCATOR
 
 #include <stddef.h>                     // for size_t
 #include <stdint.h>                     // for uint32_t
 #include "gfxASurface.h"                // for gfxASurface, etc
 #include "gfxPoint.h"                   // for gfxIntSize
 #include "mozilla/ipc/SharedMemory.h"   // for SharedMemory, etc
+#include "mozilla/WeakPtr.h"
 
 /*
  * FIXME [bjacob] *** PURE CRAZYNESS WARNING ***
  *
  * This #define is actually needed here, because subclasses of ISurfaceAllocator,
  * namely ShadowLayerForwarder, will or will not override AllocGrallocBuffer
  * depending on whether MOZ_HAVE_SURFACEDESCRIPTORGRALLOC is defined.
  */
@@ -64,17 +65,17 @@ bool ReleaseOwnedSurfaceDescriptor(const
  * An interface used to create and destroy surfaces that are shared with the
  * Compositor process (using shmem, or gralloc, or other platform specific memory)
  *
  * Most of the methods here correspond to methods that are implemented by IPDL
  * actors without a common polymorphic interface.
  * These methods should be only called in the ipdl implementor's thread, unless
  * specified otherwise in the implementing class.
  */
-class ISurfaceAllocator
+class ISurfaceAllocator : public SupportsWeakPtr<ISurfaceAllocator>
 {
 public:
 ISurfaceAllocator() {}
 
   /**
    * Allocate shared memory that can be accessed by only one process at a time.
    * Ownership of this memory is passed when the memory is sent in an IPDL
    * message.