• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #ifndef COMMON_MEMORYBUFFER_H_
8 #define COMMON_MEMORYBUFFER_H_
9 
10 #include "common/Optional.h"
11 #include "common/angleutils.h"
12 #include "common/debug.h"
13 
14 #include <stdint.h>
15 #include <cstddef>
16 
17 namespace angle
18 {
19 
20 class MemoryBuffer final : NonCopyable
21 {
22   public:
23     MemoryBuffer() = default;
24     ~MemoryBuffer();
25 
26     MemoryBuffer(MemoryBuffer &&other);
27     MemoryBuffer &operator=(MemoryBuffer &&other);
28 
29     ANGLE_NO_DISCARD bool resize(size_t size);
clear()30     void clear() { (void)resize(0); }
size()31     size_t size() const { return mSize; }
empty()32     bool empty() const { return mSize == 0; }
33 
data()34     const uint8_t *data() const { return mData; }
data()35     uint8_t *data()
36     {
37         ASSERT(mData);
38         return mData;
39     }
40 
41     uint8_t &operator[](size_t pos)
42     {
43         ASSERT(pos < mSize);
44         return mData[pos];
45     }
46     const uint8_t &operator[](size_t pos) const
47     {
48         ASSERT(pos < mSize);
49         return mData[pos];
50     }
51 
52     void fill(uint8_t datum);
53 
54   private:
55     size_t mSize   = 0;
56     uint8_t *mData = nullptr;
57 };
58 
59 class ScratchBuffer final : NonCopyable
60 {
61   public:
62     // If we request a scratch buffer requesting a smaller size this many times, release and
63     // recreate the scratch buffer. This ensures we don't have a degenerate case where we are stuck
64     // hogging memory.
65     ScratchBuffer();
66     ScratchBuffer(uint32_t lifetime);
67     ~ScratchBuffer();
68 
69     ScratchBuffer(ScratchBuffer &&other);
70     ScratchBuffer &operator=(ScratchBuffer &&other);
71 
72     // Returns true with a memory buffer of the requested size, or false on failure.
73     bool get(size_t requestedSize, MemoryBuffer **memoryBufferOut);
74 
75     // Same as get, but ensures new values are initialized to a fixed constant.
76     bool getInitialized(size_t requestedSize, MemoryBuffer **memoryBufferOut, uint8_t initValue);
77 
78     // Ticks the release counter for the scratch buffer. Also done implicitly in get().
79     void tick();
80 
81     void clear();
82 
83   private:
84     bool getImpl(size_t requestedSize, MemoryBuffer **memoryBufferOut, Optional<uint8_t> initValue);
85 
86     uint32_t mLifetime;
87     uint32_t mResetCounter;
88     MemoryBuffer mScratchMemory;
89 };
90 
91 }  // namespace angle
92 
93 #endif  // COMMON_MEMORYBUFFER_H_
94