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  /// Maximum cache size, in bytes
35  int64 getMaxSizeB() const { return myMaxSizeB; }
36 
37  /// Set the maximum size of the cache, in bytes
38  void setMaxSizeB(int64 maxsize);
39 
40  /// Current cache size, in bytes
41  int64 getCurSizeB() const { return myCurSizeB; }
42 
43  /// Put a buffer in the cache. Will replace any buffer with the same name.
44  void cacheBuffer(const UT_StringHolder &name,
45  const RE_OGLBufferHandle &buf);
46 
47  /// Return a cached buffer in 'bufh' and true, or false if not found.
48  bool findBuffer(const UT_StringRef &name,
49  RE_OGLBufferHandle &bufh);
50 
51  /// Move item to the used list from unused. Returns false if not found.
52  bool moveToUsed(const UT_StringRef &name);
53 
54  /// Move item to the unused list from used. Returns false if not found.
55  bool moveToUnused(const UT_StringRef &name);
56 
57  /// Remove a buffer from the cache. Returns true if it existed in the cache
58  bool removeBuffer(const UT_StringRef &name);
59 
60  /// call if a cached buffer changes size, with the change in size.
61  void updateBufferSize(int64 delta_size)
62  {
63  UT_ASSERT(myCurSizeB + delta_size >= 0);
64  myCurSizeB = SYSmax(0, myCurSizeB+delta_size);
65  }
66 
67 
68  /// Explicitly prune the cache if beyond its max size. This does not
69  /// happen automatically.
70  void pruneCache();
71 
72  /// Explicitly prune all unused buffer.
73  void pruneUnusedBuffers();
74 
75  /// Debug dump of the cache contents.
76  void dumpCache();
77 protected:
78  RE_BufferCache(int64 maxsize);
79  ~RE_BufferCache() override;
80 
81  // UT_Cache interface.
82  const char *utGetCacheName() const override
83  { return "OpenGL Vertex Cache"; }
84  int64 utGetCurrentSize() const override { return myCurSizeB; }
85  int64 utReduceCacheSizeBy(int64 amount) override;
86 
87  bool utHasMaxSize() const override { return true; }
88  int64 utGetMaxSize() const override { return myMaxSizeB; }
89  void utSetMaxSize(int64 size) override { setMaxSizeB(size); }
90 
91  int64 utGetMinSize() const override
92  { return myCurSizeB - myUnusedSizeB; }
93 
94 private:
95  void addToUnusedBufferList(const UT_StringHolder &key,
96  RE_OGLBufferHandle &bh);
97 
98  void removeFromUnusedBufferList(const UT_StringRef &key,
99  UnusedBuffer *buf);
100 
101  BufferPool myUsedBuffers;
102  UnusedBufferPool myUnusedBuffers;
103  UnusedBuffer *myLRUBufferList;
104  UnusedBuffer *myLRUBufferTail;
105 
106  int64 myMaxSizeB;
107  int64 myCurSizeB;
108  int64 myUnusedSizeB;
109 };
110 
111 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1570
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
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
GLsizeiptr size
Definition: glcorearb.h:664
void updateBufferSize(int64 delta_size)
call if a cached buffer changes size, with the change in size.
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