HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TIL_TileCache.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: TIL_TileCache.h (Tile Image Library, C++)
7  *
8  * COMMENTS:
9  * Flat, priority based TIL_Tile cache.
10  */
11 #ifndef TIL_TILE_CACHE_H
12 #define TIL_TILE_CACHE_H
13 
14 #include "TIL_API.h"
15 #include "TIL_Defines.h"
16 #include "TIL_Tile.h"
17 #include "TIL_PriorityQueue.h"
18 
19 #include <UT/UT_Cache.h>
20 #include <UT/UT_Map.h>
21 #include <UT/UT_ValArray.h>
22 #include <UT/UT_StopWatch.h>
23 #include <UT/UT_Lock.h>
24 #include <SYS/SYS_Hash.h>
25 #include <iosfwd>
26 
27 class UT_TokenString;
28 class til_ResourceSlush;
29 class TIL_Plane;
30 class TIL_Tile;
31 class TIL_TileManager;
32 class TIL_NodePlaneId;
33 class UI_Timer;
34 
35 typedef bool (*TIL_IsPlaneCheckpointedCallbackType)(void *opaque_cop_node,
36  const char *plane_name,
37  float cook_time);
38 
40 {
41 public:
43  ~TIL_TileCache();
44 
45  // Sets the cache size and initializes if required.
46 
47  void setCacheSize(int size_in_kb, bool force_redo = false);
48  void setInactiveCacheReduce(bool enable, int size_in_kb);
49 
50  // used for borrowing memory from the cache
51  inline void setExternalResourceUsage(int64 bytes_used)
52  { myExternalResourceUsage = bytes_used; }
53 
54 
55 
56  // Set when cooking occurs, and reset afterwards. Used when the Inactive
57  // cache size is enabled.
58  void setCookMode(bool cook);
59 
60  // Clears tiles out of the cache.
61 
62  void clear();
63 
64  // CACHE LOCKING
65  inline void lockCache() { TIL_LOCK(myCacheLock); }
66  inline void unlockCache() { myCacheLock.unlock(); }
67 
68 
69  // CACHE LOOKUP
70  // Performa a cache search for a tile, with possible new tile allocation.
71  // If not found, and create_flag is true, a new tile is allocated and
72  // 'created' is set to true. NULL may be returned if not found and
73  // create_flag is false, or the tile is blocked.
74  // If the tile is returned or blocked, the token parm is deleted & zeroed.
75  // Created tiles are returned write locked.
76  // Cached tiles are returned read locked.
77 
78  TIL_Tile *getCachedTile(UT_TokenString *&token,
79  int x,
80  int y,
81  int xsize,
82  int ysize,
83  void *node,
84  const TIL_Plane *plane,
85  int array_index,
86  int component,
87  int image_index,
88  bool create_flag,
89  bool &blocked,
90  bool &created,
92 
93  // creates an empty tile, read-only, which can be used to specify a tile
94  // when one really doesn't exist and one is needed.
95  TIL_Tile *getBlackTile(UT_TokenString *&token,
96  int x,
97  int y,
98  int xsize,
99  int ysize,
100  void *node,
101  const TIL_Plane *plane,
102  int array_index,
103  int component,
104  int image_index);
105 
106  // This method quickly queries to see if a tile is in the cache.
107  bool isTileInCache(const UT_TokenString *token,
108  int x,
109  int y);
110 
111  bool areTilesInCache(const UT_TokenString *token, int x1,int y1,
112  int x2,int y2);
113 
114  // locks or unlocks a given tile with Rd or Wr access.
115  inline int lockTile (TIL_Tile *tile, int access, int block = 1)
116  { return privLockTile(tile, access, block, false); }
117 
118  inline void unlockTile(TIL_Tile *tile, int access)
119  { privUnlockTile(tile,access, false); }
120 
121  inline void lockTileWriteToRead(TIL_Tile *tile, int score)
122  { privLockTileWriteToRead(tile,score, false); }
123 
124  inline int isReadLocked(const TIL_Tile *t) const
125  { return t->myReadLocks; }
126  inline int isWriteLocked(const TIL_Tile *t) const
127  { return t->myWriteLock; }
128 
129  // releases a tile (freeing it from write lock or 1 read lock)
130  void releaseTile(TIL_Tile *tile);
131 
132  // frees a tile(usually due to a cooking error)
133  // if write locked, deallocates.
134  // if read locked, calls releaseTile().
135  void freeTile(TIL_Tile *&tile);
136 
137  // Moves a tile from the regular cache into the list of checkpointed tiles.
138  void moveTileFromCacheToCheckpointList(
139  const UT_TokenString *token,
140  int xstart,
141  int ystart,
142  int xend,
143  int yend,
144  UT_ValArray<TIL_Tile *> &tile_list);
145 
146  // Decrement the checkpoint reference count for a tile, moving it back
147  // into the cache if the count goes to zero.
148  void decrementTileCheckpointCount(UT_ValArray<TIL_Tile *>&tile_list);
149 
150  // if we went over the cache size when checkpointing, this gets called once
151  // the engine uncheckpoints a bunch of tiles, so we can resume trimming
152  // if there's now enough non-checkpointed data to trim.
153  void possiblyResumeTrimming();
154 
155  // removes all tiles that are belong to the passed node ptr. o(n)
156  void removeAllWithParent(void *node);
157 
158  unsigned getNumCheckpointedTiles() const
159  { return myCheckpointTiles.entries(); }
160 
161  unsigned int getNumLockedTiles() const
162  { return myLockedTiles.entries(); }
163 
164  // these methods need to be called if the cook was interrupted.
165  void uncheckpointAllTiles();
166  void unlockAllTiles();
167 
168  // Returns the cache size, in bytes.
169  int64 getCacheSize() const { return myCacheSize; }
170  int64 getMaxCacheSize() const { return myMaxCacheSize; }
171  int64 getInactiveCacheSize() const { return myMaxInactiveSize; }
172  bool getInactiveReduce() const { return myInactiveReduce; }
173 
174  // These methods allow you to get a free, empty tile. Used by TIL_Region
175  // to build a TIL_TileList out of itself. Always call returnTile to
176  // return this tile to the cache when done.
177  TIL_Tile *getTempTile();
178  void returnTile(TIL_Tile *tile);
179 
180  static void cacheReducePing(UI_Timer *t, void *data);
181 
182  static TIL_TileCache *getTileCache();
183 
184  // debug: dumps the current contents of the cache.
185  void dumpContents(std::ostream &os);
186 
187 protected:
188  // the UT_Cache interface
189  const char *utGetCacheName() const override { return "COP Cook Cache"; }
190  int64 utGetCurrentSize() const override { return getCacheSize(); }
191  bool utHasMaxSize() const override { return true; }
192  int64 utGetMaxSize() const override { return getMaxCacheSize(); }
193  void utSetMaxSize(int64 size) override;
194  int64 utReduceCacheSizeBy(int64 amount) override;
195 
196 private:
197  // *** WARNING The following must be called with the cache write locked.
198 
199  TIL_Tile *getFreeTile();
200  TIL_Tile *allocateTile(TIL_DataFormat tile_type,
201  void *parent,
203  const UT_TokenString *token,
204  til_AlignedBlock *usemem);
205  void deallocateTile(TIL_Tile *tile);
206 
207  bool returnTileFromLocked(TIL_Tile *tile);
208  void makeRoomFor(int64 newmemsize);
209 
210  void destroy();
211  void freeAllLocks(TIL_Tile *tile);
212 
213  int privLockTile(TIL_Tile *tile, int access, int block,
214  bool already_locked);
215  void privUnlockTile(TIL_Tile *tile, int access,
216  bool already_locked);
217  void privLockTileWriteToRead(TIL_Tile *tile, int score,
218  bool already_locked);
219 
220 
221  void reduceCacheSize();
222  // *** WARNING END
223 
224 private:
225  class TileKey
226  {
227  public:
228  TileKey(TIL_Tile *tile);
229  TileKey(const UT_TokenString *string, int x, int y);
230 
231  bool operator==(const TileKey &other) const;
232  bool operator!=(const TileKey &other) const;
233 
234  SYS_HashType hash() const;
235 
236  struct Hasher
237  {
238  SYS_HashType operator()(const TileKey &key) const
239  { return key.hash(); }
240  };
241 
242  private:
243  int myOffX, myOffY;
244  const UT_TokenString *myString;
245  };
246 
247  // Tile queues.
248  TIL_PriorityQueue myCachedTiles;
249  TIL_HoldingQueue myCheckpointTiles;
250  TIL_HoldingQueue myLockedTiles;
251  TIL_HoldingQueue myFreeTiles;
252 
253  // Tile hash table.
255 
256  int64 myCacheSize;
257  int64 myMaxCacheSize;
258  int64 myMaxInactiveSize;
259  int64 myExternalResourceUsage;
260  int myNumTiles;
261 
262  TIL_FastLock myCacheLock;
263 
264  // Allocation saving caches
265  UT_ValArray<TIL_Tile *> myTileBlocks;
266  til_ResourceSlush *my8BitSlush;
267  til_ResourceSlush *my16BitSlush;
268  til_ResourceSlush *my32BitSlush;
269 
270  TIL_TileManager *myTileManager;
271  int myCookMode;
272  bool myInactiveReduce;
273  int mySuspendCacheTrimCount;
274  UT_StopWatch myInactiveTimer;
275 };
276 
277 
278 
279 #endif
TIL_Storage
Definition: TIL_Defines.h:101
void unlockTile(TIL_Tile *tile, int access)
bool getInactiveReduce() const
#define TIL_LOCK(loc)
Definition: TIL_Defines.h:253
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
const char * utGetCacheName() const override
required - return the english name for this cache.
GLint y
Definition: glcorearb.h:102
int64 getCacheSize() const
int64 utGetMaxSize() const override
unsigned getNumCheckpointedTiles() const
bool utHasMaxSize() const override
optional - override if the cache has a well defined maximum size
GLsizeiptr size
Definition: glcorearb.h:663
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
long long int64
Definition: SYS_Types.h:107
virtual int64 utReduceCacheSizeBy(int64 amount)=0
#define TIL_DataFormat
Definition: TIL_Defines.h:64
SYS_HashType operator()(const TileKey &key) const
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:2221
int64 getInactiveCacheSize() const
void lockCache()
Definition: TIL_TileCache.h:65
GLboolean * data
Definition: glcorearb.h:130
unsigned int getNumLockedTiles() const
Common base class for various caches.
Definition: UT_Cache.h:21
int64 utGetCurrentSize() const override
required - return the current cache size, in bytes
void unlockCache()
Definition: TIL_TileCache.h:66
GLint GLenum GLint x
Definition: glcorearb.h:408
void lockTileWriteToRead(TIL_Tile *tile, int score)
virtual void utSetMaxSize(int64)
Definition: UT_Cache.h:45
bool(* TIL_IsPlaneCheckpointedCallbackType)(void *opaque_cop_node, const char *plane_name, float cook_time)
Definition: TIL_TileCache.h:35
int isReadLocked(const TIL_Tile *t) const
getOption("OpenEXR.storage") storage
Definition: HDK_Image.dox:276
void setExternalResourceUsage(int64 bytes_used)
Definition: TIL_TileCache.h:51
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
int64 getMaxCacheSize() const
#define TIL_API
Definition: TIL_API.h:10
int isWriteLocked(const TIL_Tile *t) const
int lockTile(TIL_Tile *tile, int access, int block=1)