HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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_Cache.h>
18 #include <UT/UT_Map.h>
19 
20 typedef const char * RE_BufferCacheKey;
22 
23 class re_UnusedBuffer;
25 
27 {
28 public:
29  static RE_BufferCache *getCache();
30 
31  /// Maximum cache size, in bytes
32  int64 getMaxSizeB() const { return myMaxSizeB; }
33 
34  /// Set the maximum size of the cache, in bytes
35  void setMaxSizeB(int64 maxsize);
36 
37  /// Current cache size, in bytes
38  int64 getCurSizeB() const { return myCurSizeB; }
39 
40  /// Put a buffer in the cache. Will replace any buffer with the same name.
41  void cacheBuffer(const RE_BufferCacheKey &name,
42  const RE_OGLBufferHandle &buf);
43 
44  /// Return a cached buffer in 'bufh' and true, or false if not found.
45  bool findBuffer(const RE_BufferCacheKey &name,
46  RE_OGLBufferHandle &bufh);
47 
48  /// Change the key of a cached buffer
49  void recacheBuffer(const RE_BufferCacheKey &old_name,
50  const RE_BufferCacheKey &new_name);
51 
52  /// Move item to the used list from unused. Returns false if not found.
53  bool moveToUsed(const RE_BufferCacheKey &name);
54 
55  /// Move item to the unused list from used. Returns false if not found.
56  bool moveToUnused(const RE_BufferCacheKey &name);
57 
58  /// Remove a buffer from the cache. Returns true if it existed in the cache
59  bool removeBuffer(const RE_BufferCacheKey &name);
60 
61  /// call if a cached buffer changes size, with the change in size.
62  void updateBufferSize(int64 delta_size)
63  {
64  UT_ASSERT(myCurSizeB + delta_size >= 0);
65  myCurSizeB = SYSmax(0, myCurSizeB+delta_size);
66  }
67 
68 
69  /// Explicitly prune the cache if beyond its max size. This does not
70  /// happen automatically.
71  void pruneCache();
72 
73  /// Explicitly prune all unused buffer.
74  void pruneUnusedBuffers();
75 
76  /// Debug dump of the cache contents.
77  void dumpCache();
78 protected:
79  RE_BufferCache(int64 maxsize);
80  ~RE_BufferCache();
81 
82  // UT_Cache interface.
83  virtual const char *utGetCacheName() const { return "OpenGL Vertex Cache"; }
84  virtual int64 utGetCurrentSize() const { return myCurSizeB; }
85  virtual int64 utReduceCacheSizeBy(int64 amount);
86 
87  virtual bool utHasMaxSize() const { return true; }
88  virtual int64 utGetMaxSize() const { return myMaxSizeB; }
89  virtual void utSetMaxSize(int64 size) { setMaxSizeB(size); }
90 
91  virtual int64 utGetMinSize() const
92  { return myCurSizeB - myUnusedSizeB; }
93 
94 private:
95  void addToUnusedBufferList(std::string &key,
96  RE_OGLBufferHandle &bh);
97 
98  void removeFromUnusedBufferList(std::string &key,
99  re_UnusedBuffer *buf);
100 
101  RE_BufferPool myUsedBuffers;
102  RE_UnusedBufferPool myUnusedBuffers;
103  re_UnusedBuffer *myLRUBufferList;
104  re_UnusedBuffer *myLRUBufferTail;
105 
106  int64 myMaxSizeB;
107  int64 myCurSizeB;
108  int64 myUnusedSizeB;
109 };
110 
111 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1365
#define RE_API
Definition: RE_API.h:10
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
virtual void utSetMaxSize(int64 size)
GLsizeiptr size
Definition: glcorearb.h:663
UT_Map< std::string, RE_OGLBufferHandle > RE_BufferPool
long long int64
Definition: SYS_Types.h:106
virtual int64 utReduceCacheSizeBy(int64 amount)=0
int64 getCurSizeB() const
Current cache size, in bytes.
UT_Map< std::string, re_UnusedBuffer * > RE_UnusedBufferPool
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
virtual bool utHasMaxSize() const
optional - override if the cache has a well defined maximum size
virtual int64 utGetMaxSize() const
Wrapper around hboost::intrusive_ptr.
const char * RE_BufferCacheKey
GLuint const GLchar * name
Definition: glcorearb.h:785
Common base class for various caches.
Definition: UT_Cache.h:21
virtual int64 utGetMinSize() const
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
virtual int64 utGetCurrentSize() const
required - return the current cache size, in bytes
void updateBufferSize(int64 delta_size)
call if a cached buffer changes size, with the change in size.
virtual const char * utGetCacheName() const
required - return the english name for this cache.
int64 getMaxSizeB() const
Maximum cache size, in bytes.