michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_SIMPLETEXTURECLIENTPOOL_H michael@0: #define MOZILLA_GFX_SIMPLETEXTURECLIENTPOOL_H michael@0: michael@0: #include "mozilla/gfx/Types.h" michael@0: #include "mozilla/gfx/Point.h" michael@0: #include "mozilla/RefPtr.h" michael@0: #include "TextureClient.h" michael@0: #include "nsITimer.h" michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: class ISurfaceAllocator; michael@0: michael@0: class SimpleTextureClientPool michael@0: { michael@0: ~SimpleTextureClientPool() michael@0: { michael@0: for (auto it = mOutstandingTextureClients.begin(); it != mOutstandingTextureClients.end(); ++it) { michael@0: (*it)->ClearRecycleCallback(); michael@0: } michael@0: } michael@0: michael@0: public: michael@0: NS_INLINE_DECL_REFCOUNTING(SimpleTextureClientPool) michael@0: michael@0: SimpleTextureClientPool(gfx::SurfaceFormat aFormat, gfx::IntSize aSize, michael@0: ISurfaceAllocator *aAllocator); michael@0: michael@0: /** michael@0: * If a TextureClient is AutoRecycled, when the last reference is michael@0: * released this object will be automatically return to the pool as michael@0: * soon as the compositor informs us it is done with it. michael@0: */ michael@0: TemporaryRef GetTextureClient(bool aAutoRecycle = false); michael@0: TemporaryRef GetTextureClientWithAutoRecycle() { return GetTextureClient(true); } michael@0: michael@0: void ReturnTextureClient(TextureClient *aClient); michael@0: michael@0: void ShrinkToMinimumSize(); michael@0: michael@0: void Clear(); michael@0: michael@0: private: michael@0: // The time in milliseconds before the pool will be shrunk to the minimum michael@0: // size after returning a client. michael@0: static const uint32_t sShrinkTimeout = 3000; michael@0: michael@0: // The minimum size of the pool (the number of tiles that will be kept after michael@0: // shrinking). michael@0: static const uint32_t sMinCacheSize = 16; michael@0: michael@0: // This is the number of cached texture clients we don't want to exceed, even michael@0: // temporarily (pre-shrink) michael@0: static const uint32_t sMaxTextureClients = 50; michael@0: michael@0: static void ShrinkCallback(nsITimer *aTimer, void *aClosure); michael@0: static void RecycleCallback(TextureClient* aClient, void* aClosure); michael@0: static void WaitForCompositorRecycleCallback(TextureClient* aClient, void* aClosure); michael@0: michael@0: gfx::SurfaceFormat mFormat; michael@0: gfx::IntSize mSize; michael@0: michael@0: // We use a std::stack and make sure to use it the following way: michael@0: // new (available to be used) elements are push()'d to the front michael@0: // requests are served from the front via pop() michael@0: // -- the thinking is that recently-used elements are most likely michael@0: // to be in any data cache, so we can get some wins there michael@0: // -- the converse though is that if there is some GPU locking going on michael@0: // the most recently used elements may also have the most contention; michael@0: // if we see that, then we should use push_back() to add new elements michael@0: // when we shrink this list, we use pop(), but should use pop_back() to michael@0: // nuke the oldest. michael@0: // We may need to switch to a std::deque michael@0: // On b2g gonk, std::queue might be a better choice. michael@0: // On ICS, fence wait happens implicitly before drawing. michael@0: // Since JB, fence wait happens explicitly when fetching a client from the pool. michael@0: std::stack > mAvailableTextureClients; michael@0: std::list > mOutstandingTextureClients; michael@0: michael@0: nsRefPtr mTimer; michael@0: RefPtr mSurfaceAllocator; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: #endif /* MOZILLA_GFX_SIMPLETEXTURECLIENTPOOL_H */