Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/mlgpu/BufferCache.cpp
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
#include "BufferCache.h"
8
#include "MLGDevice.h"
9
#include "ShaderDefinitionsMLGPU.h"
10
#include "mozilla/MathAlgorithms.h"
11
12
namespace mozilla {
13
namespace layers {
14
15
using namespace mlg;
16
17
BufferCache::BufferCache(MLGDevice* aDevice)
18
 : mDevice(aDevice),
19
   mFirstSizeClass(CeilingLog2(kConstantBufferElementSize)),
20
   mFrameNumber(0),
21
   mNextSizeClassToShrink(0)
22
0
{
23
0
  // Create a cache of buffers for each size class, where each size class is a
24
0
  // power of 2 between the minimum and maximum size of a constant buffer.
25
0
  size_t maxBindSize = mDevice->GetMaxConstantBufferBindSize();
26
0
  MOZ_ASSERT(IsPowerOfTwo(maxBindSize));
27
0
28
0
  size_t lastSizeClass = CeilingLog2(maxBindSize);
29
0
  MOZ_ASSERT(lastSizeClass >= mFirstSizeClass);
30
0
31
0
  mCaches.resize(lastSizeClass - mFirstSizeClass + 1);
32
0
}
33
34
BufferCache::~BufferCache()
35
0
{
36
0
}
37
38
RefPtr<MLGBuffer>
39
BufferCache::GetOrCreateBuffer(size_t aBytes)
40
0
{
41
0
  size_t sizeClass = CeilingLog2(aBytes);
42
0
  size_t sizeClassIndex = sizeClass - mFirstSizeClass;
43
0
  if (sizeClassIndex >= mCaches.size()) {
44
0
    return mDevice->CreateBuffer(MLGBufferType::Constant, aBytes, MLGUsage::Dynamic, nullptr);
45
0
  }
46
0
47
0
  CachePool& pool = mCaches[sizeClassIndex];
48
0
49
0
  // See if we've cached a buffer that wasn't used in the past 2 frames. A buffer
50
0
  // used this frame could have already been mapped and written to, and a buffer
51
0
  // used the previous frame might still be in-use by the GPU. While the latter
52
0
  // case is okay, it causes aliasing in the driver. Since content is double
53
0
  // buffered we do not let the compositor get more than 1 frames ahead, and a
54
0
  // count of 2 frames should ensure the buffer is unused.
55
0
  if (!pool.empty() && mFrameNumber >= pool.front().mLastUsedFrame + 2) {
56
0
    RefPtr<MLGBuffer> buffer = pool.front().mBuffer;
57
0
    pool.pop_front();
58
0
    pool.push_back(CacheEntry(mFrameNumber, buffer));
59
0
    MOZ_RELEASE_ASSERT(buffer->GetSize() >= aBytes);
60
0
    return buffer;
61
0
  }
62
0
63
0
  // Allocate a new buffer and cache it.
64
0
  size_t bytes = (size_t(1) << sizeClass);
65
0
  MOZ_ASSERT(bytes >= aBytes);
66
0
67
0
  RefPtr<MLGBuffer> buffer =
68
0
    mDevice->CreateBuffer(MLGBufferType::Constant, bytes, MLGUsage::Dynamic, nullptr);
69
0
  if (!buffer) {
70
0
    return nullptr;
71
0
  }
72
0
73
0
  pool.push_back(CacheEntry(mFrameNumber, buffer));
74
0
  return buffer;
75
0
}
76
77
void
78
BufferCache::EndFrame()
79
0
{
80
0
  // Consider a buffer dead after ~5 seconds assuming 60 fps.
81
0
  static size_t kMaxUnusedFrameCount = 60 * 5;
82
0
83
0
  // At the end of each frame we pick one size class and see if it has any
84
0
  // buffers that haven't been used for many frames. If so we clear them.
85
0
  // The next frame we'll search the next size class. (This is just to spread
86
0
  // work over more than one frame.)
87
0
  CachePool& pool = mCaches[mNextSizeClassToShrink];
88
0
  while (!pool.empty()) {
89
0
    // Since the deque is sorted oldest-to-newest, front-to-back, we can stop
90
0
    // searching as soon as a buffer is active.
91
0
    if (mFrameNumber - pool.front().mLastUsedFrame < kMaxUnusedFrameCount) {
92
0
      break;
93
0
    }
94
0
    pool.pop_front();
95
0
  }
96
0
  mNextSizeClassToShrink = (mNextSizeClassToShrink + 1) % mCaches.size();
97
0
98
0
  mFrameNumber++;
99
0
}
100
101
} // namespace layers
102
} // namespace mozilla