00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef TIL_TILE_CACHE_H
00020 #define TIL_TILE_CACHE_H
00021
00022 #include "TIL_API.h"
00023 #include <iostream.h>
00024 #include <UT/UT_Cache.h>
00025 #include <UT/UT_HashTable.h>
00026 #include <UT/UT_PtrArray.h>
00027 #include <UT/UT_StopWatch.h>
00028 #include <UT/UT_Lock.h>
00029 #include "TIL_Defines.h"
00030 #include "TIL_Tile.h"
00031 #include "TIL_PriorityQueue.h"
00032
00033 class UT_TokenString;
00034 class til_ResourceSlush;
00035 class TIL_Plane;
00036 class TIL_Tile;
00037 class TIL_TileManager;
00038 class TIL_NodePlaneId;
00039 class UI_Timer;
00040
00041 typedef bool (*TIL_IsPlaneCheckpointedCallbackType)(void *opaque_cop_node,
00042 const char *plane_name,
00043 float cook_time);
00044
00045 class TIL_API TIL_TileCache : public UT_Cache
00046 {
00047 public:
00048 TIL_TileCache(TIL_TileManager *tman);
00049 ~TIL_TileCache();
00050
00051
00052
00053 void setCacheSize(int size_in_kb, bool force_redo = false);
00054 void setInactiveCacheReduce(bool enable, int size_in_kb);
00055
00056
00057 inline void setExternalResourceUsage(int64 bytes_used)
00058 { myExternalResourceUsage = bytes_used; }
00059
00060
00061
00062
00063
00064 void setCookMode(bool cook);
00065
00066
00067
00068 void clear();
00069
00070
00071 inline void lockCache() { TIL_LOCK(myCacheLock); }
00072 inline void unlockCache() { myCacheLock.unlock(); }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 TIL_Tile *getCachedTile(UT_TokenString *&token,
00085 int x,
00086 int y,
00087 int xsize,
00088 int ysize,
00089 void *node,
00090 const TIL_Plane *plane,
00091 int array_index,
00092 int component,
00093 int image_index,
00094 bool create_flag,
00095 bool &blocked,
00096 bool &created,
00097 TIL_Storage storage);
00098
00099
00100
00101 TIL_Tile *getBlackTile(UT_TokenString *&token,
00102 int x,
00103 int y,
00104 int xsize,
00105 int ysize,
00106 void *node,
00107 const TIL_Plane *plane,
00108 int array_index,
00109 int component,
00110 int image_index);
00111
00112
00113 bool isTileInCache(const UT_TokenString *token,
00114 int x,
00115 int y);
00116
00117 bool areTilesInCache(const UT_TokenString *token, int x1,int y1,
00118 int x2,int y2);
00119
00120
00121 inline int lockTile (TIL_Tile *tile, int access, int block = 1)
00122 { return privLockTile(tile, access, block, false); }
00123
00124 inline void unlockTile(TIL_Tile *tile, int access)
00125 { privUnlockTile(tile,access, false); }
00126
00127 inline void lockTileWriteToRead(TIL_Tile *tile, int score)
00128 { privLockTileWriteToRead(tile,score, false); }
00129
00130 inline int isReadLocked(const TIL_Tile *t) const
00131 { return t->myReadLocks; }
00132 inline int isWriteLocked(const TIL_Tile *t) const
00133 { return t->myWriteLock; }
00134
00135
00136 void releaseTile(TIL_Tile *tile);
00137
00138
00139
00140
00141 void freeTile(TIL_Tile *&tile);
00142
00143
00144 void moveTileFromCacheToCheckpointList(
00145 const UT_TokenString *token,
00146 int xstart,
00147 int ystart,
00148 int xend,
00149 int yend,
00150 UT_PtrArray<TIL_Tile *> &tile_list);
00151
00152
00153
00154 void decrementTileCheckpointCount(UT_PtrArray<TIL_Tile *>&tile_list);
00155
00156
00157
00158
00159 void possiblyResumeTrimming();
00160
00161
00162 void removeAllWithParent(void *node);
00163
00164 unsigned getNumCheckpointedTiles() const
00165 { return myCheckpointTiles.entries(); }
00166
00167 unsigned int getNumLockedTiles() const
00168 { return myLockedTiles.entries(); }
00169
00170
00171 void uncheckpointAllTiles();
00172 void unlockAllTiles();
00173
00174
00175 int64 getCacheSize() const { return myCacheSize; }
00176 int64 getMaxCacheSize() const { return myMaxCacheSize; }
00177 int64 getInactiveCacheSize() const { return myMaxInactiveSize; }
00178 bool getInactiveReduce() const { return myInactiveReduce; }
00179
00180
00181
00182
00183 TIL_Tile *getTempTile();
00184 void returnTile(TIL_Tile *tile);
00185
00186 static void cacheReducePing(UI_Timer *t, void *data);
00187
00188 static TIL_TileCache * getTileCache();
00189
00190
00191 void dumpContents(ostream &os);
00192
00193 protected:
00194
00195 virtual const char *utGetCacheName() const { return "COP Cook Cache"; }
00196 virtual int64 utGetCurrentSize() const { return getCacheSize(); }
00197 virtual bool utHasMaxSize() const { return true; }
00198 virtual int64 utGetMaxSize() const { return getMaxCacheSize(); }
00199 virtual void utSetMaxSize(int64 size);
00200 virtual int64 utReduceCacheSizeBy(int64 amount);
00201
00202 private:
00203
00204
00205 TIL_Tile *getFreeTile();
00206 TIL_Tile *allocateTile(TIL_DataFormat tile_type,
00207 void *parent,
00208 TIL_Storage storage,
00209 const UT_TokenString *token,
00210 til_AlignedBlock *usemem);
00211 void deallocateTile(TIL_Tile *tile);
00212
00213 bool returnTileFromLocked(TIL_Tile *tile);
00214 void makeRoomFor(int64 newmemsize);
00215
00216 void rebuildTileHashTable();
00217 void destroy();
00218 void freeAllLocks(TIL_Tile *tile);
00219
00220 int privLockTile(TIL_Tile *tile, int access, int block,
00221 bool already_locked);
00222 void privUnlockTile(TIL_Tile *tile, int access,
00223 bool already_locked);
00224 void privLockTileWriteToRead(TIL_Tile *tile, int score,
00225 bool already_locked);
00226
00227
00228 void reduceCacheSize();
00229
00230
00231 private:
00232
00233 TIL_PriorityQueue myCachedTiles;
00234 TIL_HoldingQueue myCheckpointTiles;
00235 TIL_HoldingQueue myLockedTiles;
00236 TIL_HoldingQueue myFreeTiles;
00237
00238
00239 UT_HashTable *myHashTable;
00240
00241 int64 myCacheSize;
00242 int64 myMaxCacheSize;
00243 int64 myMaxInactiveSize;
00244 int64 myExternalResourceUsage;
00245 int myNumTiles;
00246
00247 TIL_FastLock myCacheLock;
00248
00249
00250 UT_PtrArray<TIL_Tile *> myTileBlocks;
00251 til_ResourceSlush *my8BitSlush;
00252 til_ResourceSlush *my16BitSlush;
00253 til_ResourceSlush *my32BitSlush;
00254
00255 TIL_TileManager *myTileManager;
00256 int myCookMode;
00257 bool myInactiveReduce;
00258 int mySuspendCacheTrimCount;
00259 UT_StopWatch myInactiveTimer;
00260 };
00261
00262
00263
00264 #endif