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();
80 
81  // UT_Cache interface.
82  virtual const char *utGetCacheName() const { return "OpenGL Vertex Cache"; }
83  virtual int64 utGetCurrentSize() const { return myCurSizeB; }
84  virtual int64 utReduceCacheSizeBy(int64 amount);
85 
86  virtual bool utHasMaxSize() const { return true; }
87  virtual int64 utGetMaxSize() const { return myMaxSizeB; }
88  virtual void utSetMaxSize(int64 size) { setMaxSizeB(size); }
89 
90  virtual int64 utGetMinSize() const
91  { return myCurSizeB - myUnusedSizeB; }
92 
93 private:
94  void addToUnusedBufferList(const UT_StringHolder &key,
95  RE_OGLBufferHandle &bh);
96 
97  void removeFromUnusedBufferList(const UT_StringRef &key,
98  UnusedBuffer *buf);
99 
100  BufferPool myUsedBuffers;
101  UnusedBufferPool myUnusedBuffers;
102  UnusedBuffer *myLRUBufferList;
103  UnusedBuffer *myLRUBufferTail;
104 
105  int64 myMaxSizeB;
106  int64 myCurSizeB;
107  int64 myUnusedSizeB;
108 };
109 
110 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1367
#define RE_API
Definition: RE_API.h:10
virtual void utSetMaxSize(int64 size)
GLsizeiptr size
Definition: glcorearb.h:663
long long int64
Definition: SYS_Types.h:107
virtual int64 utReduceCacheSizeBy(int64 amount)=0
int64 getCurSizeB() const
Current cache size, in bytes.
virtual bool utHasMaxSize() const
optional - override if the cache has a well defined maximum size
virtual int64 utGetMaxSize() const
Wrapper around hboost::intrusive_ptr.
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.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126