TextureClientRecycleAllocator.h (4694B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H 8 #define MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H 9 10 #include <map> 11 #include <stack> 12 13 #include "TextureClient.h" 14 #include "mozilla/Mutex.h" 15 #include "mozilla/RefPtr.h" 16 #include "mozilla/Result.h" 17 #include "mozilla/gfx/Types.h" 18 #include "mozilla/layers/TextureForwarder.h" 19 20 namespace mozilla { 21 namespace layers { 22 23 class TextureClientHolder; 24 struct PlanarYCbCrData; 25 26 class ITextureClientRecycleAllocator { 27 protected: 28 virtual ~ITextureClientRecycleAllocator() = default; 29 30 public: 31 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ITextureClientRecycleAllocator) 32 33 protected: 34 friend class TextureClient; 35 virtual void RecycleTextureClient(TextureClient* aClient) = 0; 36 }; 37 38 class ITextureClientAllocationHelper { 39 public: 40 ITextureClientAllocationHelper(gfx::SurfaceFormat aFormat, gfx::IntSize aSize, 41 BackendSelector aSelector, 42 TextureFlags aTextureFlags, 43 TextureAllocationFlags aAllocationFlags) 44 : mFormat(aFormat), 45 mSize(aSize), 46 mSelector(aSelector), 47 mTextureFlags(aTextureFlags | 48 TextureFlags::RECYCLE) // Set recycle flag 49 , 50 mAllocationFlags(aAllocationFlags) {} 51 52 virtual already_AddRefed<TextureClient> Allocate( 53 KnowsCompositor* aKnowsCompositor) = 0; 54 virtual bool IsCompatible(TextureClient* aTextureClient) = 0; 55 56 const gfx::SurfaceFormat mFormat; 57 const gfx::IntSize mSize; 58 const BackendSelector mSelector; 59 const TextureFlags mTextureFlags; 60 const TextureAllocationFlags mAllocationFlags; 61 }; 62 63 class MOZ_RAII YCbCrTextureClientAllocationHelper 64 : public ITextureClientAllocationHelper { 65 public: 66 YCbCrTextureClientAllocationHelper(const PlanarYCbCrData& aData, 67 const gfx::IntSize& aYSize, 68 const gfx::IntSize& aCbCrSize, 69 TextureFlags aTextureFlags); 70 71 YCbCrTextureClientAllocationHelper(const PlanarYCbCrData& aData, 72 TextureFlags aTextureFlags); 73 74 bool IsCompatible(TextureClient* aTextureClient) override; 75 76 already_AddRefed<TextureClient> Allocate( 77 KnowsCompositor* aKnowsCompositor) override; 78 79 protected: 80 const PlanarYCbCrData& mData; 81 const gfx::IntSize mYSize; 82 const gfx::IntSize mCbCrSize; 83 }; 84 85 /** 86 * TextureClientRecycleAllocator provides TextureClients allocation and 87 * recycling capabilities. It expects allocations of same sizes and 88 * attributres. If a recycled TextureClient is different from 89 * requested one, the recycled one is dropped and new TextureClient is 90 * allocated. 91 * 92 * By default this uses TextureClient::CreateForDrawing to allocate new texture 93 * clients. 94 */ 95 class TextureClientRecycleAllocator : public ITextureClientRecycleAllocator { 96 protected: 97 virtual ~TextureClientRecycleAllocator(); 98 99 public: 100 explicit TextureClientRecycleAllocator(KnowsCompositor* aKnowsCompositor); 101 102 void SetMaxPoolSize(uint32_t aMax); 103 104 // Creates and allocates a TextureClient. 105 already_AddRefed<TextureClient> CreateOrRecycle( 106 gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, 107 TextureFlags aTextureFlags, TextureAllocationFlags flags = ALLOC_DEFAULT); 108 109 Result<already_AddRefed<TextureClient>, nsresult> CreateOrRecycle( 110 ITextureClientAllocationHelper& aHelper); 111 112 void ShrinkToMinimumSize(); 113 114 void Destroy(); 115 116 KnowsCompositor* GetKnowsCompositor() { return mKnowsCompositor; } 117 118 protected: 119 virtual already_AddRefed<TextureClient> Allocate( 120 gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, 121 TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags); 122 123 const RefPtr<KnowsCompositor> mKnowsCompositor; 124 125 friend class DefaultTextureClientAllocationHelper; 126 void RecycleTextureClient(TextureClient* aClient) override; 127 128 static const uint32_t kMaxPooledSized = 2; 129 uint32_t mMaxPooledSize; 130 131 std::map<TextureClient*, RefPtr<TextureClientHolder> > mInUseClients; 132 133 // stack is good from Graphics cache usage point of view. 134 std::stack<RefPtr<TextureClientHolder> > mPooledClients; 135 Mutex mLock MOZ_UNANNOTATED; 136 bool mIsDestroyed; 137 }; 138 139 } // namespace layers 140 } // namespace mozilla 141 142 #endif /* MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H */