HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_BufferCache.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_BufferCache.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * This cache contains GL buffers.
10  */
11 #ifndef RE_BufferCache_h
12 #define RE_BufferCache_h
13 
14 #include "RE_API.h"
15 #include "RE_Types.h"
16 
17 #include <UT/UT_Assert.h>
18 #include <UT/UT_Cache.h>
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_ArrayStringMap.h>
21 #include <SYS/SYS_Math.h>
22 #include <SYS/SYS_Types.h>
23 
24 
26 {
27  class UnusedBuffer;
30 
31 public:
32  static RE_BufferCache *getCache();
33 
34  void setEnabled(bool enabled) { myCacheEnabled = enabled; }
35 
36  /// Maximum cache size, in bytes
37  int64 getMaxSizeB() const { return myMaxSizeB; }
38 
39  /// Set the maximum size of the cache, in bytes
40  void setMaxSizeB(int64 maxsize);
41 
42  /// Current cache size, in bytes
43  int64 getCurSizeB() const { return myCurSizeB; }
44 
45  /// Put a buffer in the cache. Will replace any buffer with the same name.
46  void cacheBuffer(const UT_StringHolder &name,
47  const RE_OGLBufferHandle &buf);
48 
49  /// Return a cached buffer in 'bufh' and true, or false if not found.
50  bool findBuffer(const UT_StringRef &name,
51  RE_OGLBufferHandle &bufh);
52 
53  /// Move item to the used list from unused. Returns false if not found.
54  bool moveToUsed(const UT_StringRef &name);
55 
56  /// Move item to the unused list from used. Returns false if not found.
57  bool moveToUnused(const UT_StringRef &name);
58 
59  /// Remove a buffer from the cache. Returns true if it existed in the cache
60  bool removeBuffer(const UT_StringRef &name);
61 
62  /// call if a cached buffer changes size, with the change in size.
63  void updateBufferSize(int64 delta_size)
64  {
65  UT_ASSERT(myCurSizeB + delta_size >= 0);
66  myCurSizeB = SYSmax(0, myCurSizeB+delta_size);
67  }
68 
69 
70  /// Explicitly prune the cache if beyond its max size. This does not
71  /// happen automatically.
72  void pruneCache();
73 
74  /// Explicitly prune all unused buffer.
75  void pruneUnusedBuffers();
76 
77  /// Debug dump of the cache contents.
78  void dumpCache();
79 protected:
80  RE_BufferCache(int64 maxsize);
81  ~RE_BufferCache() override;
82 
83  // UT_Cache interface.
84  const char *utGetCacheName() const override
85  { return "OpenGL Vertex Cache"; }
86  int64 utGetCurrentSize() const override { return myCurSizeB; }
87  int64 utReduceCacheSizeBy(int64 amount) override;
88 
89  bool utHasMaxSize() const override { return true; }
90  int64 utGetMaxSize() const override { return myMaxSizeB; }
91  void utSetMaxSize(int64 size) override { setMaxSizeB(size); }
92  bool utIsEnabled() const override { return myCacheEnabled; }
93 
94  int64 utGetMinSize() const override
95  { return myCurSizeB - myUnusedSizeB; }
96 
97 private:
98  void addToUnusedBufferList(const UT_StringHolder &key,
99  RE_OGLBufferHandle &bh);
100 
101  void removeFromUnusedBufferList(const UT_StringRef &key,
102  UnusedBuffer *buf);
103 
104  BufferPool myUsedBuffers;
105  UnusedBufferPool myUnusedBuffers;
106  UnusedBuffer *myLRUBufferList;
107  UnusedBuffer *myLRUBufferTail;
108 
109  int64 myMaxSizeB;
110  int64 myCurSizeB;
111  int64 myUnusedSizeB;
112  bool myCacheEnabled = true;
113 };
114 
115 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1582
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
#define RE_API
Definition: RE_API.h:10
int64 utGetMinSize() const override
int64 utGetMaxSize() const override
bool utHasMaxSize() const override
optional - override if the cache has a well defined maximum size
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:2539
virtual int64 utReduceCacheSizeBy(int64 amount)=0
int64 getCurSizeB() const
Current cache size, in bytes.
Wrapper around hboost::intrusive_ptr.
long long int64
Definition: SYS_Types.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
Common base class for various caches.
Definition: UT_Cache.h:21
bool utIsEnabled() const override
optional - is this cache currently operational.
GLsizeiptr size
Definition: glcorearb.h:664
void updateBufferSize(int64 delta_size)
call if a cached buffer changes size, with the change in size.
void setEnabled(bool enabled)
int64 getMaxSizeB() const
Maximum cache size, in bytes.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
void utSetMaxSize(int64 size) override
const char * utGetCacheName() const override
required - return the english name for this cache.
int64 utGetCurrentSize() const override
required - return the current cache size, in bytes