Bug 1135935 - Part 2: Merge TextureClientRecycleAllocator into base class. r=sotaro
authorMatt Woodrow <mwoodrow@mozilla.com>
Mon, 10 Aug 2015 16:02:14 -0400
changeset 257894 47d0d08c131c0dd508dd71ab9db11f3bf61edd7e
parent 257893 844bfdb58072f4a376e3af87c111a5fd6d377724
child 257895 62a8922fe05c321523a23efd30fafd2b633456b9
push id63753
push usermwoodrow@mozilla.com
push dateFri, 14 Aug 2015 21:18:23 +0000
treeherdermozilla-inbound@fc15644207d8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssotaro
bugs1135935
milestone43.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 1135935 - Part 2: Merge TextureClientRecycleAllocator into base class. r=sotaro
gfx/layers/client/TextureClientRecycleAllocator.cpp
gfx/layers/client/TextureClientRecycleAllocator.h
--- a/gfx/layers/client/TextureClientRecycleAllocator.cpp
+++ b/gfx/layers/client/TextureClientRecycleAllocator.cpp
@@ -1,129 +1,83 @@
 /* -*- Mode: C++; tab-width: 20; 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/. */
 
-#include <map>
-#include <stack>
-
 #include "gfxPlatform.h"
-#include "mozilla/layers/GrallocTextureClient.h"
 #include "mozilla/layers/ISurfaceAllocator.h"
-#include "mozilla/Mutex.h"
-
 #include "TextureClientRecycleAllocator.h"
 
 namespace mozilla {
 namespace layers {
 
-class TextureClientRecycleAllocatorImp
+// Used to keep TextureClient's reference count stable as not to disrupt recycling.
+class TextureClientHolder
 {
-  ~TextureClientRecycleAllocatorImp();
-
+  ~TextureClientHolder() {}
 public:
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureClientRecycleAllocatorImp)
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureClientHolder)
 
-  explicit TextureClientRecycleAllocatorImp(ISurfaceAllocator* aAllocator);
+  explicit TextureClientHolder(TextureClient* aClient)
+    : mTextureClient(aClient)
+  {}
 
-  void SetMaxPoolSize(uint32_t aMax)
+  TextureClient* GetTextureClient()
   {
-    if (aMax > 0) {
-      mMaxPooledSize = aMax;
-    }
+    return mTextureClient;
   }
 
-  // Creates and allocates a TextureClient.
-  already_AddRefed<TextureClient>
-  CreateOrRecycleForDrawing(gfx::SurfaceFormat aFormat,
-                            gfx::IntSize aSize,
-                            BackendSelector aSelector,
-                            TextureFlags aTextureFlags,
-                            TextureAllocationFlags flags);
-
-  void Destroy();
-
-  void RecycleCallbackImp(TextureClient* aClient);
-
-  static void RecycleCallback(TextureClient* aClient, void* aClosure);
-
-private:
-  static const uint32_t kMaxPooledSized = 2;
-
-  // Used to keep TextureClient's reference count stable as not to disrupt recycling.
-  class TextureClientHolder
-  {
-    ~TextureClientHolder() {}
-  public:
-    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureClientHolder)
-
-    explicit TextureClientHolder(TextureClient* aClient)
-      : mTextureClient(aClient)
-    {}
-
-    TextureClient* GetTextureClient()
-    {
-      return mTextureClient;
-    }
-    void ClearTextureClient() { mTextureClient = nullptr; }
-  protected:
-    RefPtr<TextureClient> mTextureClient;
-  };
-
-  bool mDestroyed;
-  uint32_t mMaxPooledSize;
-  RefPtr<ISurfaceAllocator> mSurfaceAllocator;
-  std::map<TextureClient*, RefPtr<TextureClientHolder> > mInUseClients;
-
-  // On b2g gonk, std::queue might be a better choice.
-  // On ICS, fence wait happens implicitly before drawing.
-  // Since JB, fence wait happens explicitly when fetching a client from the pool.
-  // stack is good from Graphics cache usage point of view.
-  std::stack<RefPtr<TextureClientHolder> > mPooledClients;
-  Mutex mLock;
+  void ClearTextureClient() { mTextureClient = nullptr; }
+protected:
+  RefPtr<TextureClient> mTextureClient;
 };
 
-TextureClientRecycleAllocatorImp::TextureClientRecycleAllocatorImp(ISurfaceAllocator *aAllocator)
-  : mDestroyed(false)
-  , mMaxPooledSize(kMaxPooledSized)
+TextureClientRecycleAllocator::TextureClientRecycleAllocator(ISurfaceAllocator *aAllocator)
+  : mMaxPooledSize(kMaxPooledSized)
   , mSurfaceAllocator(aAllocator)
   , mLock("TextureClientRecycleAllocatorImp.mLock")
 {
 }
 
-TextureClientRecycleAllocatorImp::~TextureClientRecycleAllocatorImp()
+TextureClientRecycleAllocator::~TextureClientRecycleAllocator()
 {
-  MOZ_ASSERT(mDestroyed);
-  MOZ_ASSERT(mPooledClients.empty());
+  MutexAutoLock lock(mLock);
+  while (!mPooledClients.empty()) {
+    mPooledClients.pop();
+  }
   MOZ_ASSERT(mInUseClients.empty());
 }
 
+void
+TextureClientRecycleAllocator::SetMaxPoolSize(uint32_t aMax)
+{
+  mMaxPooledSize = aMax;
+}
+
 already_AddRefed<TextureClient>
-TextureClientRecycleAllocatorImp::CreateOrRecycleForDrawing(
-                                             gfx::SurfaceFormat aFormat,
-                                             gfx::IntSize aSize,
-                                             BackendSelector aSelector,
-                                             TextureFlags aTextureFlags,
-                                             TextureAllocationFlags aAllocFlags)
+TextureClientRecycleAllocator::CreateOrRecycleForDrawing(
+                                          gfx::SurfaceFormat aFormat,
+                                          gfx::IntSize aSize,
+                                          BackendSelector aSelector,
+                                          TextureFlags aTextureFlags,
+                                          TextureAllocationFlags aAllocFlags)
 {
   // TextureAllocationFlags is actually used only by ContentClient.
   // This class does not handle ConteClient's TextureClient allocation.
   MOZ_ASSERT(aAllocFlags == TextureAllocationFlags::ALLOC_DEFAULT ||
              aAllocFlags == TextureAllocationFlags::ALLOC_DISALLOW_BUFFERTEXTURECLIENT);
   MOZ_ASSERT(!(aTextureFlags & TextureFlags::RECYCLE));
   aTextureFlags = aTextureFlags | TextureFlags::RECYCLE; // Set recycle flag
 
   RefPtr<TextureClientHolder> textureHolder;
 
   {
     MutexAutoLock lock(mLock);
-    if (mDestroyed) {
-      return nullptr;
-    } else if (!mPooledClients.empty()) {
+    if (!mPooledClients.empty()) {
       textureHolder = mPooledClients.top();
       mPooledClients.pop();
       // If a pooled TextureClient is not compatible, release it.
       if (textureHolder->GetTextureClient()->GetFormat() != aFormat ||
           textureHolder->GetTextureClient()->GetSize() != aSize)
       {
         TextureClientReleaseTask* task = new TextureClientReleaseTask(textureHolder->GetTextureClient());
         textureHolder->ClearTextureClient();
@@ -148,85 +102,40 @@ TextureClientRecycleAllocatorImp::Create
   }
 
   {
     MutexAutoLock lock(mLock);
     MOZ_ASSERT(mInUseClients.find(textureHolder->GetTextureClient()) == mInUseClients.end());
     // Register TextureClient
     mInUseClients[textureHolder->GetTextureClient()] = textureHolder;
   }
-  textureHolder->GetTextureClient()->SetRecycleCallback(TextureClientRecycleAllocatorImp::RecycleCallback, this);
+  textureHolder->GetTextureClient()->SetRecycleCallback(TextureClientRecycleAllocator::RecycleCallback, this);
   RefPtr<TextureClient> client(textureHolder->GetTextureClient());
   return client.forget();
 }
 
 void
-TextureClientRecycleAllocatorImp::Destroy()
-{
-  MutexAutoLock lock(mLock);
-  if (mDestroyed) {
-    return;
-  }
-  mDestroyed = true;
-  while (!mPooledClients.empty()) {
-    mPooledClients.pop();
-  }
-}
-
-void
-TextureClientRecycleAllocatorImp::RecycleCallbackImp(TextureClient* aClient)
+TextureClientRecycleAllocator::RecycleCallbackImp(TextureClient* aClient)
 {
   RefPtr<TextureClientHolder> textureHolder;
   aClient->ClearRecycleCallback();
   {
     MutexAutoLock lock(mLock);
     if (mInUseClients.find(aClient) != mInUseClients.end()) {
       textureHolder = mInUseClients[aClient]; // Keep reference count of TextureClientHolder within lock.
-      if (!mDestroyed && mPooledClients.size() < mMaxPooledSize) {
+      if (mPooledClients.size() < mMaxPooledSize) {
         mPooledClients.push(textureHolder);
       }
       mInUseClients.erase(aClient);
     }
   }
 }
 
 /* static */ void
-TextureClientRecycleAllocatorImp::RecycleCallback(TextureClient* aClient, void* aClosure)
+TextureClientRecycleAllocator::RecycleCallback(TextureClient* aClient, void* aClosure)
 {
   MOZ_ASSERT(aClient && !aClient->IsDead());
-  TextureClientRecycleAllocatorImp* recycleAllocator = static_cast<TextureClientRecycleAllocatorImp*>(aClosure);
+  TextureClientRecycleAllocator* recycleAllocator = static_cast<TextureClientRecycleAllocator*>(aClosure);
   recycleAllocator->RecycleCallbackImp(aClient);
 }
 
-TextureClientRecycleAllocator::TextureClientRecycleAllocator(ISurfaceAllocator *aAllocator)
-{
-  mAllocator = new TextureClientRecycleAllocatorImp(aAllocator);
-}
-
-TextureClientRecycleAllocator::~TextureClientRecycleAllocator()
-{
-  mAllocator->Destroy();
-  mAllocator = nullptr;
-}
-
-void
-TextureClientRecycleAllocator::SetMaxPoolSize(uint32_t aMax)
-{
-  mAllocator->SetMaxPoolSize(aMax);
-}
-
-already_AddRefed<TextureClient>
-TextureClientRecycleAllocator::CreateOrRecycleForDrawing(
-                                            gfx::SurfaceFormat aFormat,
-                                            gfx::IntSize aSize,
-                                            BackendSelector aSelector,
-                                            TextureFlags aTextureFlags,
-                                            TextureAllocationFlags aAllocFlags)
-{
-  return mAllocator->CreateOrRecycleForDrawing(aFormat,
-                                               aSize,
-                                               aSelector,
-                                               aTextureFlags,
-                                               aAllocFlags);
-}
-
 } // namespace layers
 } // namespace mozilla
--- a/gfx/layers/client/TextureClientRecycleAllocator.h
+++ b/gfx/layers/client/TextureClientRecycleAllocator.h
@@ -1,25 +1,28 @@
 /* -*- Mode: C++; tab-width: 20; 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 MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H
 #define MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H
 
+#include <map>
+#include <stack>
 #include "mozilla/gfx/Types.h"
 #include "mozilla/RefPtr.h"
 #include "TextureClient.h"
+#include "mozilla/Mutex.h"
 
 namespace mozilla {
 namespace layers {
 
 class ISurfaceAllocator;
-class TextureClientRecycleAllocatorImp;
+class TextureClientHolder;
 
 
 /**
  * TextureClientRecycleAllocator provides TextureClients allocation and
  * recycling capabilities. It expects allocations of same sizes and
  * attributres. If a recycled TextureClient is different from
  * requested one, the recycled one is dropped and new TextureClient is allocated.
  */
@@ -38,15 +41,30 @@ public:
   already_AddRefed<TextureClient>
   CreateOrRecycleForDrawing(gfx::SurfaceFormat aFormat,
                             gfx::IntSize aSize,
                             BackendSelector aSelector,
                             TextureFlags aTextureFlags,
                             TextureAllocationFlags flags = ALLOC_DEFAULT);
 
 private:
-  RefPtr<TextureClientRecycleAllocatorImp> mAllocator;
+  void RecycleCallbackImp(TextureClient* aClient);
+
+  static void RecycleCallback(TextureClient* aClient, void* aClosure);
+
+  static const uint32_t kMaxPooledSized = 2;
+
+  uint32_t mMaxPooledSize;
+  RefPtr<ISurfaceAllocator> mSurfaceAllocator;
+  std::map<TextureClient*, RefPtr<TextureClientHolder> > mInUseClients;
+
+  // On b2g gonk, std::queue might be a better choice.
+  // On ICS, fence wait happens implicitly before drawing.
+  // Since JB, fence wait happens explicitly when fetching a client from the pool.
+  // stack is good from Graphics cache usage point of view.
+  std::stack<RefPtr<TextureClientHolder> > mPooledClients;
+  Mutex mLock;
 };
 
 } // namespace layers
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H */