summaryrefslogtreecommitdiffstats
path: root/gfx/layers/client/TextureClientRecycleAllocator.cpp
blob: c45b3668366f90c41148cbab3d543be9bbbb9651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gfxPlatform.h"
#include "ImageContainer.h"
#include "mozilla/layers/BufferTexture.h"
#include "mozilla/layers/ISurfaceAllocator.h"
#include "mozilla/layers/TextureForwarder.h"
#include "TextureClientRecycleAllocator.h"

namespace mozilla {
namespace layers {

// Used to keep TextureClient's reference count stable as not to disrupt
// recycling.
class TextureClientHolder final {
  ~TextureClientHolder() = default;

 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureClientHolder)

  explicit TextureClientHolder(TextureClient* aClient)
      : mTextureClient(aClient), mWillRecycle(true) {}

  TextureClient* GetTextureClient() { return mTextureClient; }

  bool WillRecycle() { return mWillRecycle; }

  void ClearWillRecycle() { mWillRecycle = false; }

  void ClearTextureClient() { mTextureClient = nullptr; }

 protected:
  RefPtr<TextureClient> mTextureClient;
  bool mWillRecycle;
};

class MOZ_RAII DefaultTextureClientAllocationHelper
    : public ITextureClientAllocationHelper {
 public:
  DefaultTextureClientAllocationHelper(
      TextureClientRecycleAllocator* aAllocator, gfx::SurfaceFormat aFormat,
      gfx::IntSize aSize, BackendSelector aSelector, TextureFlags aTextureFlags,
      TextureAllocationFlags aAllocationFlags)
      : ITextureClientAllocationHelper(aFormat, aSize, aSelector, aTextureFlags,
                                       aAllocationFlags),
        mAllocator(aAllocator) {}

  bool IsCompatible(TextureClient* aTextureClient) override {
    if (aTextureClient->GetFormat() != mFormat ||
        aTextureClient->GetSize() != mSize) {
      return false;
    }
    return true;
  }

  already_AddRefed<TextureClient> Allocate(
      KnowsCompositor* aKnowsCompositor) override {
    return mAllocator->Allocate(mFormat, mSize, mSelector, mTextureFlags,
                                mAllocationFlags);
  }

 protected:
  TextureClientRecycleAllocator* mAllocator;
};

YCbCrTextureClientAllocationHelper::YCbCrTextureClientAllocationHelper(
    const PlanarYCbCrData& aData, const gfx::IntSize& aYSize,
    const gfx::IntSize& aCbCrSize, TextureFlags aTextureFlags)
    : ITextureClientAllocationHelper(gfx::SurfaceFormat::YUV, aYSize,
                                     BackendSelector::Content, aTextureFlags,
                                     ALLOC_DEFAULT),
      mData(aData),
      mYSize(aYSize),
      mCbCrSize(aCbCrSize) {}

YCbCrTextureClientAllocationHelper::YCbCrTextureClientAllocationHelper(
    const PlanarYCbCrData& aData, TextureFlags aTextureFlags)
    : YCbCrTextureClientAllocationHelper(aData, aData.YDataSize(),
                                         aData.CbCrDataSize(), aTextureFlags) {}

bool YCbCrTextureClientAllocationHelper::IsCompatible(
    TextureClient* aTextureClient) {
  MOZ_ASSERT(aTextureClient->GetFormat() == gfx::SurfaceFormat::YUV);

  BufferTextureData* bufferData =
      aTextureClient->GetInternalData()->AsBufferTextureData();

  if (!bufferData ||
      !bufferData->GetPictureRect().IsEqualEdges(mData.mPictureRect) ||
      bufferData->GetYSize().isNothing() ||
      bufferData->GetYSize().ref() != mYSize ||
      bufferData->GetCbCrSize().isNothing() ||
      bufferData->GetCbCrSize().ref() != mCbCrSize ||
      bufferData->GetYStride().isNothing() ||
      bufferData->GetYStride().ref() != mData.mYStride ||
      bufferData->GetCbCrStride().isNothing() ||
      bufferData->GetCbCrStride().ref() != mData.mCbCrStride ||
      bufferData->GetYUVColorSpace().isNothing() ||
      bufferData->GetYUVColorSpace().ref() != mData.mYUVColorSpace ||
      bufferData->GetColorDepth().isNothing() ||
      bufferData->GetColorDepth().ref() != mData.mColorDepth ||
      bufferData->GetStereoMode().isNothing() ||
      bufferData->GetStereoMode().ref() != mData.mStereoMode ||
      bufferData->GetChromaSubsampling().isNothing() ||
      bufferData->GetChromaSubsampling().ref() != mData.mChromaSubsampling) {
    return false;
  }
  return true;
}

already_AddRefed<TextureClient> YCbCrTextureClientAllocationHelper::Allocate(
    KnowsCompositor* aKnowsCompositor) {
  return TextureClient::CreateForYCbCr(
      aKnowsCompositor, mData.mPictureRect, mYSize, mData.mYStride, mCbCrSize,
      mData.mCbCrStride, mData.mStereoMode, mData.mColorDepth,
      mData.mYUVColorSpace, mData.mColorRange, mData.mChromaSubsampling,
      mTextureFlags);
}

TextureClientRecycleAllocator::TextureClientRecycleAllocator(
    KnowsCompositor* aKnowsCompositor)
    : mKnowsCompositor(aKnowsCompositor),
      mMaxPooledSize(kMaxPooledSized),
      mLock("TextureClientRecycleAllocatorImp.mLock"),
      mIsDestroyed(false) {}

TextureClientRecycleAllocator::~TextureClientRecycleAllocator() {
  MutexAutoLock lock(mLock);
  while (!mPooledClients.empty()) {
    mPooledClients.pop();
  }
  MOZ_ASSERT(mInUseClients.empty());
}

void TextureClientRecycleAllocator::SetMaxPoolSize(uint32_t aMax) {
  mMaxPooledSize = aMax;
}

already_AddRefed<TextureClient> TextureClientRecycleAllocator::CreateOrRecycle(
    gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector,
    TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
  MOZ_ASSERT(!(aTextureFlags & TextureFlags::RECYCLE));
  DefaultTextureClientAllocationHelper helper(this, aFormat, aSize, aSelector,
                                              aTextureFlags, aAllocFlags);
  return CreateOrRecycle(helper).unwrapOr(nullptr);
}

Result<already_AddRefed<TextureClient>, nsresult>
TextureClientRecycleAllocator::CreateOrRecycle(
    ITextureClientAllocationHelper& aHelper) {
  MOZ_ASSERT(aHelper.mTextureFlags & TextureFlags::RECYCLE);

  RefPtr<TextureClientHolder> textureHolder;

  {
    MutexAutoLock lock(mLock);
    if (mIsDestroyed || !mKnowsCompositor->GetTextureForwarder()) {
      return Err(NS_ERROR_NOT_AVAILABLE);
    }
    if (!mPooledClients.empty()) {
      textureHolder = mPooledClients.top();
      mPooledClients.pop();
      // If the texture's allocator is not open or a pooled TextureClient is
      // not compatible, release it.
      if (!textureHolder->GetTextureClient()->GetAllocator()->IPCOpen() ||
          !aHelper.IsCompatible(textureHolder->GetTextureClient())) {
        // Release TextureClient.
        RefPtr<Runnable> task =
            new TextureClientReleaseTask(textureHolder->GetTextureClient());
        textureHolder->ClearTextureClient();
        textureHolder = nullptr;
        mKnowsCompositor->GetTextureForwarder()->GetThread()->Dispatch(
            task.forget());
      } else {
        textureHolder->GetTextureClient()->RecycleTexture(
            aHelper.mTextureFlags);
      }
    }
  }

  if (!textureHolder) {
    // Allocate new TextureClient
    RefPtr<TextureClient> texture = aHelper.Allocate(mKnowsCompositor);
    if (!texture) {
      return Err(NS_ERROR_OUT_OF_MEMORY);
    }
    textureHolder = new TextureClientHolder(texture);
  }

  {
    MutexAutoLock lock(mLock);
    MOZ_ASSERT(mInUseClients.find(textureHolder->GetTextureClient()) ==
               mInUseClients.end());
    // Register TextureClient
    mInUseClients[textureHolder->GetTextureClient()] = textureHolder;
  }
  RefPtr<TextureClient> client(textureHolder->GetTextureClient());

  // Make sure the texture holds a reference to us, and ask it to call
  // RecycleTextureClient when its ref count drops to 1.
  client->SetRecycleAllocator(this);
  return client.forget();
}

already_AddRefed<TextureClient> TextureClientRecycleAllocator::Allocate(
    gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector,
    TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
  return TextureClient::CreateForDrawing(mKnowsCompositor, aFormat, aSize,
                                         aSelector, aTextureFlags, aAllocFlags);
}

void TextureClientRecycleAllocator::ShrinkToMinimumSize() {
  MutexAutoLock lock(mLock);
  while (!mPooledClients.empty()) {
    mPooledClients.pop();
  }
  // We can not clear using TextureClients safely.
  // Just clear WillRecycle here.
  std::map<TextureClient*, RefPtr<TextureClientHolder> >::iterator it;
  for (it = mInUseClients.begin(); it != mInUseClients.end(); it++) {
    RefPtr<TextureClientHolder> holder = it->second;
    holder->ClearWillRecycle();
  }
}

void TextureClientRecycleAllocator::Destroy() {
  MutexAutoLock lock(mLock);
  while (!mPooledClients.empty()) {
    mPooledClients.pop();
  }
  mIsDestroyed = true;
}

void TextureClientRecycleAllocator::RecycleTextureClient(
    TextureClient* aClient) {
  // Clearing the recycle allocator drops a reference, so make sure we stay
  // alive for the duration of this function.
  RefPtr<TextureClientRecycleAllocator> kungFuDeathGrip(this);
  aClient->SetRecycleAllocator(nullptr);

  RefPtr<TextureClientHolder> textureHolder;
  {
    MutexAutoLock lock(mLock);
    if (mInUseClients.find(aClient) != mInUseClients.end()) {
      textureHolder =
          mInUseClients[aClient];  // Keep reference count of
                                   // TextureClientHolder within lock.
      if (textureHolder->WillRecycle() && !mIsDestroyed &&
          mPooledClients.size() < mMaxPooledSize) {
        mPooledClients.push(textureHolder);
      }
      mInUseClients.erase(aClient);
    }
  }
}

}  // namespace layers
}  // namespace mozilla