/src/mozilla-central/gfx/layers/client/TextureClientRecycleAllocator.h
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/gfx/Types.h" |
13 | | #include "mozilla/layers/TextureForwarder.h" |
14 | | #include "mozilla/RefPtr.h" |
15 | | #include "TextureClient.h" |
16 | | #include "mozilla/Mutex.h" |
17 | | |
18 | | namespace mozilla { |
19 | | namespace layers { |
20 | | |
21 | | class TextureClientHolder; |
22 | | struct PlanarYCbCrData; |
23 | | |
24 | | class ITextureClientRecycleAllocator |
25 | | { |
26 | | protected: |
27 | 0 | virtual ~ITextureClientRecycleAllocator() {} |
28 | | |
29 | | public: |
30 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ITextureClientRecycleAllocator) |
31 | | |
32 | | protected: |
33 | | friend class TextureClient; |
34 | | virtual void RecycleTextureClient(TextureClient* aClient) = 0; |
35 | | }; |
36 | | |
37 | | class ITextureClientAllocationHelper |
38 | | { |
39 | | public: |
40 | | ITextureClientAllocationHelper(gfx::SurfaceFormat aFormat, |
41 | | gfx::IntSize aSize, |
42 | | BackendSelector aSelector, |
43 | | TextureFlags aTextureFlags, |
44 | | TextureAllocationFlags aAllocationFlags) |
45 | | : mFormat(aFormat) |
46 | | , mSize(aSize) |
47 | | , mSelector(aSelector) |
48 | | , mTextureFlags(aTextureFlags | TextureFlags::RECYCLE) // Set recycle flag |
49 | | , mAllocationFlags(aAllocationFlags) |
50 | 0 | {} |
51 | | |
52 | | virtual already_AddRefed<TextureClient> Allocate(KnowsCompositor* aAllocator) = 0; |
53 | | virtual bool IsCompatible(TextureClient* aTextureClient) = 0; |
54 | | |
55 | | const gfx::SurfaceFormat mFormat; |
56 | | const gfx::IntSize mSize; |
57 | | const BackendSelector mSelector; |
58 | | const TextureFlags mTextureFlags; |
59 | | const TextureAllocationFlags mAllocationFlags; |
60 | | }; |
61 | | |
62 | | class YCbCrTextureClientAllocationHelper : public ITextureClientAllocationHelper |
63 | | { |
64 | | public: |
65 | | YCbCrTextureClientAllocationHelper(const PlanarYCbCrData& aData, |
66 | | TextureFlags aTextureFlags); |
67 | | |
68 | | bool IsCompatible(TextureClient* aTextureClient) override; |
69 | | |
70 | | already_AddRefed<TextureClient> Allocate(KnowsCompositor* aAllocator) override; |
71 | | |
72 | | protected: |
73 | | const PlanarYCbCrData& mData; |
74 | | }; |
75 | | |
76 | | |
77 | | /** |
78 | | * TextureClientRecycleAllocator provides TextureClients allocation and |
79 | | * recycling capabilities. It expects allocations of same sizes and |
80 | | * attributres. If a recycled TextureClient is different from |
81 | | * requested one, the recycled one is dropped and new TextureClient is allocated. |
82 | | * |
83 | | * By default this uses TextureClient::CreateForDrawing to allocate new texture |
84 | | * clients. |
85 | | */ |
86 | | class TextureClientRecycleAllocator : public ITextureClientRecycleAllocator |
87 | | { |
88 | | protected: |
89 | | virtual ~TextureClientRecycleAllocator(); |
90 | | |
91 | | public: |
92 | | explicit TextureClientRecycleAllocator(KnowsCompositor* aAllocator); |
93 | | |
94 | | void SetMaxPoolSize(uint32_t aMax); |
95 | | |
96 | | // Creates and allocates a TextureClient. |
97 | | already_AddRefed<TextureClient> |
98 | | CreateOrRecycle(gfx::SurfaceFormat aFormat, |
99 | | gfx::IntSize aSize, |
100 | | BackendSelector aSelector, |
101 | | TextureFlags aTextureFlags, |
102 | | TextureAllocationFlags flags = ALLOC_DEFAULT); |
103 | | |
104 | | already_AddRefed<TextureClient> |
105 | | CreateOrRecycle(ITextureClientAllocationHelper& aHelper); |
106 | | |
107 | | void ShrinkToMinimumSize(); |
108 | | |
109 | | void Destroy(); |
110 | | |
111 | | protected: |
112 | | virtual already_AddRefed<TextureClient> |
113 | | Allocate(gfx::SurfaceFormat aFormat, |
114 | | gfx::IntSize aSize, |
115 | | BackendSelector aSelector, |
116 | | TextureFlags aTextureFlags, |
117 | | TextureAllocationFlags aAllocFlags); |
118 | | |
119 | | RefPtr<KnowsCompositor> mSurfaceAllocator; |
120 | | |
121 | | friend class DefaultTextureClientAllocationHelper; |
122 | | void RecycleTextureClient(TextureClient* aClient) override; |
123 | | |
124 | | static const uint32_t kMaxPooledSized = 2; |
125 | | uint32_t mMaxPooledSize; |
126 | | |
127 | | std::map<TextureClient*, RefPtr<TextureClientHolder> > mInUseClients; |
128 | | |
129 | | // stack is good from Graphics cache usage point of view. |
130 | | std::stack<RefPtr<TextureClientHolder> > mPooledClients; |
131 | | Mutex mLock; |
132 | | bool mIsDestroyed; |
133 | | }; |
134 | | |
135 | | } // namespace layers |
136 | | } // namespace mozilla |
137 | | |
138 | | #endif /* MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H */ |