Coverage Report

Created: 2018-09-25 14:53

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