Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef MOZILLA_GFX_TEXTURECLIENTPOOL_H
7 : #define MOZILLA_GFX_TEXTURECLIENTPOOL_H
8 :
9 : #include "mozilla/gfx/Types.h"
10 : #include "mozilla/gfx/Point.h"
11 : #include "mozilla/RefPtr.h"
12 : #include "TextureClient.h"
13 : #include "nsITimer.h"
14 : #include <stack>
15 : #include <list>
16 :
17 : namespace mozilla {
18 : namespace layers {
19 :
20 : class ISurfaceAllocator;
21 : class TextureForwarder;
22 : class TextureReadLock;
23 :
24 0 : class TextureClientAllocator
25 : {
26 : protected:
27 0 : virtual ~TextureClientAllocator() {}
28 : public:
29 0 : NS_INLINE_DECL_REFCOUNTING(TextureClientAllocator)
30 :
31 : virtual already_AddRefed<TextureClient> GetTextureClient() = 0;
32 :
33 : /**
34 : * Return a TextureClient that is not yet ready to be reused, but will be
35 : * imminently.
36 : */
37 : virtual void ReturnTextureClientDeferred(TextureClient *aClient) = 0;
38 :
39 : virtual void ReportClientLost() = 0;
40 : };
41 :
42 : class TextureClientPool final : public TextureClientAllocator
43 : {
44 : ~TextureClientPool();
45 :
46 : public:
47 : TextureClientPool(LayersBackend aBackend,
48 : int32_t aMaxTextureSize,
49 : gfx::SurfaceFormat aFormat,
50 : gfx::IntSize aSize,
51 : TextureFlags aFlags,
52 : uint32_t aShrinkTimeoutMsec,
53 : uint32_t aClearTimeoutMsec,
54 : uint32_t aInitialPoolSize,
55 : uint32_t aPoolUnusedSize,
56 : TextureForwarder* aAllocator);
57 :
58 : /**
59 : * Gets an allocated TextureClient of size and format that are determined
60 : * by the initialisation parameters given to the pool. This will either be
61 : * a cached client that was returned to the pool, or a newly allocated
62 : * client if one isn't available.
63 : *
64 : * All clients retrieved by this method should be returned using the return
65 : * functions, or reported lost so that the pool can manage its size correctly.
66 : */
67 : already_AddRefed<TextureClient> GetTextureClient() override;
68 :
69 : /**
70 : * Return a TextureClient that is no longer being used and is ready for
71 : * immediate re-use or destruction.
72 : */
73 : void ReturnTextureClient(TextureClient *aClient);
74 :
75 : /**
76 : * Return a TextureClient that is not yet ready to be reused, but will be
77 : * imminently.
78 : */
79 : void ReturnTextureClientDeferred(TextureClient *aClient) override;
80 :
81 : /**
82 : * Return any clients to the pool that were previously returned in
83 : * ReturnTextureClientDeferred.
84 : */
85 : void ReturnDeferredClients();
86 :
87 : /**
88 : * Attempt to shrink the pool so that there are no more than
89 : * mInitialPoolSize outstanding.
90 : */
91 : void ShrinkToMaximumSize();
92 :
93 : /**
94 : * Report that a client retrieved via GetTextureClient() has become
95 : * unusable, so that it will no longer be tracked.
96 : */
97 : virtual void ReportClientLost() override;
98 :
99 : /**
100 : * Calling this will cause the pool to attempt to relinquish any unused
101 : * clients.
102 : */
103 : void Clear();
104 :
105 0 : LayersBackend GetBackend() const { return mBackend; }
106 0 : int32_t GetMaxTextureSize() const { return mMaxTextureSize; }
107 0 : gfx::SurfaceFormat GetFormat() { return mFormat; }
108 0 : TextureFlags GetFlags() const { return mFlags; }
109 :
110 : /**
111 : * Clear the pool and put it in a state where it won't recycle any new texture.
112 : */
113 : void Destroy();
114 :
115 : private:
116 : void ReturnUnlockedClients();
117 :
118 : /// Allocate a single TextureClient to be returned from the pool.
119 : void AllocateTextureClient();
120 :
121 : /// Reset and/or initialise timers for shrinking/clearing the pool.
122 : void ResetTimers();
123 :
124 : /// Backend passed to the TextureClient for buffer creation.
125 : LayersBackend mBackend;
126 :
127 : // Max texture size passed to the TextureClient for buffer creation.
128 : int32_t mMaxTextureSize;
129 :
130 : /// Format is passed to the TextureClient for buffer creation.
131 : gfx::SurfaceFormat mFormat;
132 :
133 : /// The width and height of the tiles to be used.
134 : gfx::IntSize mSize;
135 :
136 : /// Flags passed to the TextureClient for buffer creation.
137 : const TextureFlags mFlags;
138 :
139 : /// How long to wait after a TextureClient is returned before trying
140 : /// to shrink the pool to its maximum size of mPoolUnusedSize.
141 : uint32_t mShrinkTimeoutMsec;
142 :
143 : /// How long to wait after a TextureClient is returned before trying
144 : /// to clear the pool.
145 : uint32_t mClearTimeoutMsec;
146 :
147 : // The initial number of unused texture clients to seed the pool with
148 : // on construction
149 : uint32_t mInitialPoolSize;
150 :
151 : // How many unused texture clients to try and keep around if we go over
152 : // the initial allocation
153 : uint32_t mPoolUnusedSize;
154 :
155 : /// This is a total number of clients in the wild and in the stack of
156 : /// deferred clients (see below). So, the total number of clients in
157 : /// existence is always mOutstandingClients + the size of mTextureClients.
158 : uint32_t mOutstandingClients;
159 :
160 : std::stack<RefPtr<TextureClient> > mTextureClients;
161 :
162 : std::list<RefPtr<TextureClient>> mTextureClientsDeferred;
163 : RefPtr<nsITimer> mShrinkTimer;
164 : RefPtr<nsITimer> mClearTimer;
165 : // This mSurfaceAllocator owns us, so no need to hold a ref to it
166 : TextureForwarder* mSurfaceAllocator;
167 :
168 : // Keep track of whether this pool has been destroyed or not. If it has,
169 : // we won't accept returns of TextureClients anymore, and the refcounting
170 : // should take care of their destruction.
171 : bool mDestroyed;
172 : };
173 :
174 : } // namespace layers
175 : } // namespace mozilla
176 :
177 : #endif /* MOZILLA_GFX_TEXTURECLIENTPOOL_H */
|