HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RE_CachedObject.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_CachedObject.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Base class for objects that can be cached in the RE_TextureCache and
10  * RE_BufferCache..
11  */
12 #ifndef RE_CachedObject_h
13 #define RE_CachedObject_h
14 
15 #include "RE_API.h"
16 
17 #include <SYS/SYS_Types.h>
18 #include <SYS/SYS_AtomicInt.h>
19 #include <UT/UT_BoundingBox.h>
20 #include <UT/UT_DeepString.h>
21 #include <UT/UT_IntrusivePtr.h>
22 #include <UT/UT_NonCopyable.h>
23 #include <UT/UT_StackTrace.h>
24 #include <iostream>
25 
26 class RE_CachedExtraData;
27 class RE_OGLTexture;
28 class RE_OGLBuffer;
29 
30 /// Simple class for a mutli-integer cache tag.
32 {
33 public:
35  {
36  myVersion[0] = 0;
37  myVersion[1] = 0;
38  myVersion[2] = 0;
39  myVersion[3] = 0;
40  }
42  {
43  myVersion[0] = cv.myVersion[0];
44  myVersion[1] = cv.myVersion[1];
45  myVersion[2] = cv.myVersion[2];
46  myVersion[3] = cv.myVersion[3];
47  }
48 
50  {
51  myVersion[0] = cv.myVersion[0];
52  myVersion[1] = cv.myVersion[1];
53  myVersion[2] = cv.myVersion[2];
54  myVersion[3] = cv.myVersion[3];
55  return *this;
56  }
57  bool operator==(const RE_CacheVersion &cv) const
58  {
59  return (myVersion[0] == cv.myVersion[0] &&
60  myVersion[1] == cv.myVersion[1] &&
61  myVersion[2] == cv.myVersion[2] &&
62  myVersion[3] == cv.myVersion[3]);
63  }
64  bool operator!=(const RE_CacheVersion &cv) const
65  {
66  return (myVersion[0] != cv.myVersion[0] ||
67  myVersion[1] != cv.myVersion[1] ||
68  myVersion[2] != cv.myVersion[2] ||
69  myVersion[3] != cv.myVersion[3]);
70  }
72  {
73  ++myVersion[0];
74  if(myVersion[0] < 0)
75  {
76  myVersion[0]=0;
77  ++myVersion[1];
78  if(myVersion[1] < 0)
79  {
80  myVersion[1]=0;
81  ++myVersion[2];
82  if(myVersion[2] < 0)
83  {
84  myVersion[2]=0;
85  ++myVersion[3];
86  }
87  }
88  }
89  return *this;
90  }
91 
92  void setElement(int i, int64 v)
93  {
94  UT_ASSERT(i>=0 && i<=3);
95  myVersion[i] = v;
96  }
97  int64 getElement(int i) const
98  {
99  UT_ASSERT(i>=0 && i<=3);
100  return myVersion[i];
101  }
102 
104  {
105  UT_ASSERT(i>=0 && i<=3);
106  myVersion[i] =
107  int64( (uint64(myVersion[i])&(uint64(0xFFFFFFFF)<<uint64(32)))
108  | uint64(v) );
109  }
111  {
112  UT_ASSERT(i>=0 && i<=3);
113  myVersion[i] = int64((uint64(myVersion[i])&uint64(0xFFFFFFFF))
114  | (uint64(v)<<uint64(32)));
115  }
116  void clear()
117  {
118  myVersion[0] = 0;
119  myVersion[1] = 0;
120  myVersion[2] = 0;
121  myVersion[3] = 0;
122  }
123 
124  std::ostream &operator<<(std::ostream &os) const
125  {
126  os << myVersion[0] << "," << myVersion[1] << "," << myVersion[2] << ","
127  << myVersion[3];
128  return os;
129  }
130 
131 private:
132  /// I/O friends
133  // @{
134  friend std::ostream &operator<<(std::ostream &os, const RE_CacheVersion &v)
135  {
136  os << v.myVersion[0] << "," << v.myVersion[1] << ","
137  << v.myVersion[2] << "," << v.myVersion[3];
138  return os;
139  }
140  // @}
141 
142 private:
143  int64 myVersion[4];
144 };
145 
146 // ----------------------------------------------------------------------
147 /// This class is bumped whenever a cached item is evicted.
149 {
150 public:
151  RE_CacheTag() : myVersion(0), myRefCount(0) {}
152 
153  void bump() { myVersion++; }
154  void reset() { myVersion=0; }
155  int64 getVersion() const { return myVersion; }
156 
157  void incref() { myRefCount.add(1); }
158  void decref()
159  {
160  if (!myRefCount.add(-1))
161  delete this;
162  }
163 private:
164  int64 myVersion;
165  SYS_AtomicInt32 myRefCount;
166 };
168 
169 // -------------------------------------------------------------------------
170 // Derive from this class to stash data in a cached object.
172 {
173 public:
175 
176  virtual const char *className() const { return "RE_CachedExtraData"; }
177 
178  // Called when the parent object is removed from the cache.
179  virtual void cacheFree() {}
180 
181  /// Returns the amount of main memory (NOT graphics memory!)
182  /// owned by this RE_CachedExtraData.
183  /// If inclusive, include sizeof(*this), else only count
184  /// other memory owned by this.
185  virtual int64 getMemoryUsage(bool inclusive) const = 0;
186 
187  void incref() { myRefCount++; }
188  void decref()
189  {
190  myRefCount--;
191  if(myRefCount == 0)
192  delete this;
193  }
194  int refCount() const { return myRefCount; }
195 protected:
196  virtual ~RE_CachedExtraData();
197 private:
198  int myRefCount;
199 };
201 
202 
203 
204 // ------------------------------------------------------------------------
205 /// Basic cached object, with version and extra data only.
207 {
208 public:
210  virtual ~RE_CachedObjectBase();
211 
212  void setVersion(RE_CacheVersion v){ myVersion = v; }
213  RE_CacheVersion getVersion() const { return myVersion; }
214 
215  // Add extra data to be cached along with the object (derive from the class
216  // below). If 'give_own' is true, this will be deleted when the cached
217  // object is, otherwise only 'cacheFree()' will be called.
219  { myExtraData = data; }
220  const RE_CachedExtraDataHandle &getExtraData() const { return myExtraData; }
221 
222 
223  // Cache tags notify other interested parties that we are deleted.
225  { myCacheTagHandle = h; }
227  { myCacheTagHandle = NULL; }
228 
229  void setInCache(bool c)
230  {
231  myCachedFlag = c;
232  if(!c && myExtraData)
233  myExtraData->cacheFree();
234  }
235  bool isInCache() const { return myCachedFlag; }
236 
237  /// Returns the amount of main memory (not VRAM) owned by this object.
238  virtual int64 getMemoryUsage(bool inclusive) const;
239 
240 public:
244 
246 };
247 
248 
249 
250 // ----------------------------------------------------------------------
251 /// Cached object implementation for RE_TextureCache.
253 {
254 public:
255  RE_CachedObject();
256  virtual ~RE_CachedObject();
257 
258  // If this object is being actively used by GL
259  void setInUse(bool inc);
260  virtual bool inUse() { return myUsage > 0; }
261  int getUsage() const { return myUsage; }
262 
263  // How many objects currently point to this object. Decrementing to zero
264  // does not delete the object. Handle object will not delete this object if
265  // it is still referenced by another handle.
266  void incRef() { myRefCount++; }
267  void decRef() { myRefCount--; }
268  int getRefCount() const { return myRefCount; }
269 
270  void setCached(bool cached,const char *mapname);
271  bool isCached() const { return myCachedFlag; }
272 
273  const char *getName() const { return myName; }
274 
275  // Add a removal callback to this object, to be notified when this object
276  // is about to be deleted from the cache.
277  bool setRemovalCallback(bool (*relCB)(void *, void *),
278  void *relObject);
279 
280  bool clearRemovalCallback(void *relObject);
281 
282  /// Returns the amount of main memory (NOT graphics memory!)
283  /// owned by this RE_CachedObject.
284  virtual int64 getMemoryUsage(bool inclusive) const;
285 
286 private:
287  int myUsage;
288  int myRefCount;
289  UT_DeepString myName;
290 };
291 
292 
294 {
295 public:
296  RE_BBoxData(const UT_BoundingBox &bbox) : myBBox(bbox) {}
297  virtual ~RE_BBoxData() {}
298 
299  virtual const char *className() const { return "RE_BBoxData"; }
300 
301  /// Returns the amount of main memory (NOT graphics memory!)
302  /// owned by this RE_BBoxData.
303  virtual int64 getMemoryUsage(bool inclusive) const
304  {
305  int64 mem = inclusive ? sizeof(*this) : 0;
306  return mem;
307  }
308 
310 };
311 
312 
313 inline void
315 {
316  myUsage += inc ? 1 : -1;
317 #ifdef UT_DEBUG
318  if(myUsage < 0)
319  std::cerr << "Bad object usage " << myUsage << " " << this << "\n";
320  else if(myUsage == 32767)
321  std::cerr << "Apparent overflow " << myUsage << " " << this << "\n";
322 #endif
323  UT_ASSERT_MSG(myUsage >= 0, "Underflow");
324  UT_ASSERT_MSG(myUsage < 32767, "Overflow");
325  if(myUsage < 0)
326  myUsage = 0;
327 }
328 
329 RE_API size_t format(char *buf, size_t bufsize, const RE_CacheVersion &v);
330 
331 static inline void intrusive_ptr_add_ref(RE_CacheTag *i) { i->incref(); }
332 static inline void intrusive_ptr_release(RE_CacheTag *i) { i->decref(); }
333 static inline void intrusive_ptr_add_ref(RE_CachedExtraData *i) { i->incref(); }
334 static inline void intrusive_ptr_release(RE_CachedExtraData *i) { i->decref(); }
335 
336 #endif
bool operator==(const RE_CacheVersion &cv) const
bool operator!=(const RE_CacheVersion &cv) const
void setElementLow32(int i, int32 v)
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1817
std::ostream & operator<<(std::ostream &os) const
#define RE_API
Definition: RE_API.h:10
virtual int64 getMemoryUsage(bool inclusive) const
void setInCache(bool c)
const GLdouble * v
Definition: glcorearb.h:836
UT_BoundingBoxD myBBox
UT_IntrusivePtr< RE_CacheTag > RE_CacheTagHandle
virtual const char * className() const
int getUsage() const
RE_API size_t format(char *buf, size_t bufsize, const RE_CacheVersion &v)
RE_CacheVersion getVersion() const
int refCount() const
int64 getElement(int i) const
png_uint_32 i
Definition: png.h:2877
void setExtraData(RE_CachedExtraData *data)
RE_CacheVersion & operator=(const RE_CacheVersion &cv)
int64 getVersion() const
RE_CacheTagHandle myCacheTagHandle
long long int64
Definition: SYS_Types.h:106
void setElementHigh32(int i, int32 v)
void setInUse(bool inc)
unsigned long long uint64
Definition: SYS_Types.h:107
RE_BBoxData(const UT_BoundingBox &bbox)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
RE_CacheVersion myVersion
virtual ~RE_BBoxData()
RE_CacheVersion(const RE_CacheVersion &cv)
void setElement(int i, int64 v)
friend std::ostream & operator<<(std::ostream &os, const RE_CacheVersion &v)
I/O friends.
GLboolean * data
Definition: glcorearb.h:130
int int32
Definition: SYS_Types.h:34
const RE_CachedExtraDataHandle & getExtraData() const
void setCacheTag(RE_CacheTagHandle h)
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
Cached object implementation for RE_TextureCache.
void setVersion(RE_CacheVersion v)
virtual bool inUse()
Basic cached object, with version and extra data only.
virtual const char * className() const
UT_IntrusivePtr< RE_CachedExtraData > RE_CachedExtraDataHandle
bool isInCache() const
bool isCached() const
Simple class for a mutli-integer cache tag.
const char * getName() const
virtual int64 getMemoryUsage(bool inclusive) const
Returns the amount of main memory (not VRAM) owned by this object.
RE_CachedExtraDataHandle myExtraData
RE_CacheVersion & operator++()
virtual void cacheFree()
This class is bumped whenever a cached item is evicted.
#define UT_ASSERT_MSG(ZZ, MM)
Definition: UT_Assert.h:105
int getRefCount() const