Blender V2.27

cache.c

00001 /*
00002  * $Id: cache.c 34160 2011-01-07 19:18:31Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00023 #include "MEM_guardedalloc.h"
00024 
00025 #include "BLI_utildefines.h"
00026 #include "BLI_ghash.h"
00027 #include "BLI_listbase.h"
00028 #include "BLI_memarena.h"
00029 #include "BLI_threads.h"
00030 
00031 
00032 
00033 #include "IMB_imbuf.h"
00034 #include "IMB_imbuf_types.h"
00035 #include "IMB_filetype.h"
00036 
00037 #include "imbuf.h"
00038 
00039 /* We use a two level cache here. A per-thread cache with limited number of
00040    tiles. This can be accessed without locking and so is hoped to lead to most
00041    tile access being lock-free. The global cache is shared between all threads
00042    and requires slow locking to access, and contains all tiles.
00043    
00044    The per-thread cache should be big enough that one might hope to not fall
00045    back to the global cache every pixel, but not to big to keep too many tiles
00046    locked and using memory. */
00047 
00048 #define IB_THREAD_CACHE_SIZE    100
00049 
00050 typedef struct ImGlobalTile {
00051         struct ImGlobalTile *next, *prev;
00052 
00053         ImBuf *ibuf;
00054         int tx, ty;
00055         int refcount;
00056         volatile int loading;
00057 } ImGlobalTile;
00058 
00059 typedef struct ImThreadTile {
00060         struct ImThreadTile *next, *prev;
00061 
00062         ImBuf *ibuf;
00063         int tx, ty;
00064 
00065         ImGlobalTile *global;
00066 } ImThreadTile;
00067 
00068 typedef struct ImThreadTileCache {
00069         ListBase tiles;
00070         ListBase unused;
00071         GHash *tilehash;
00072 } ImThreadTileCache;
00073 
00074 typedef struct ImGlobalTileCache {
00075         ListBase tiles;
00076         ListBase unused;
00077         GHash *tilehash;
00078 
00079         MemArena *memarena;
00080         uintptr_t totmem, maxmem;
00081 
00082         ImThreadTileCache thread_cache[BLENDER_MAX_THREADS+1];
00083         int totthread;
00084 
00085         ThreadMutex mutex;
00086 
00087         int initialized;
00088 } ImGlobalTileCache;
00089 
00090 static ImGlobalTileCache GLOBAL_CACHE;
00091 
00092 /***************************** Hash Functions ********************************/
00093 
00094 static unsigned int imb_global_tile_hash(const void *gtile_p)
00095 {
00096         const ImGlobalTile *gtile= gtile_p;
00097 
00098         return ((unsigned int)(intptr_t)gtile->ibuf)*769 + gtile->tx*53 + gtile->ty*97;
00099 }
00100 
00101 static int imb_global_tile_cmp(const void *a_p, const void *b_p)
00102 {
00103         const ImGlobalTile *a= a_p;
00104         const ImGlobalTile *b= b_p;
00105 
00106         if(a->ibuf == b->ibuf && a->tx == b->tx && a->ty == b->ty) return 0;
00107         else if(a->ibuf < b->ibuf || a->tx < b->tx || a->ty < b->ty) return -1;
00108         else return 1;
00109 }
00110 
00111 static unsigned int imb_thread_tile_hash(const void *ttile_p)
00112 {
00113         const ImThreadTile *ttile= ttile_p;
00114 
00115         return ((unsigned int)(intptr_t)ttile->ibuf)*769 + ttile->tx*53 + ttile->ty*97;
00116 }
00117 
00118 static int imb_thread_tile_cmp(const void *a_p, const void *b_p)
00119 {
00120         const ImThreadTile *a= a_p;
00121         const ImThreadTile *b= b_p;
00122 
00123         if(a->ibuf == b->ibuf && a->tx == b->tx && a->ty == b->ty) return 0;
00124         else if(a->ibuf < b->ibuf || a->tx < b->tx || a->ty < b->ty) return -1;
00125         else return 1;
00126 }
00127 
00128 /******************************** Load/Unload ********************************/
00129 
00130 static void imb_global_cache_tile_load(ImGlobalTile *gtile)
00131 {
00132         ImBuf *ibuf= gtile->ibuf;
00133         int toffs= ibuf->xtiles*gtile->ty + gtile->tx;
00134         unsigned int *rect;
00135 
00136         rect = MEM_callocN(sizeof(unsigned int)*ibuf->tilex*ibuf->tiley, "imb_tile");
00137         imb_loadtile(ibuf, gtile->tx, gtile->ty, rect);
00138         ibuf->tiles[toffs]= rect;
00139 }
00140 
00141 static void imb_global_cache_tile_unload(ImGlobalTile *gtile)
00142 {
00143         ImBuf *ibuf= gtile->ibuf;
00144         int toffs= ibuf->xtiles*gtile->ty + gtile->tx;
00145 
00146         MEM_freeN(ibuf->tiles[toffs]);
00147         ibuf->tiles[toffs]= NULL;
00148 
00149         GLOBAL_CACHE.totmem -= sizeof(unsigned int)*ibuf->tilex*ibuf->tiley;
00150 }
00151 
00152 /* external free */
00153 void imb_tile_cache_tile_free(ImBuf *ibuf, int tx, int ty)
00154 {
00155         ImGlobalTile *gtile, lookuptile;
00156 
00157         BLI_mutex_lock(&GLOBAL_CACHE.mutex);
00158 
00159         lookuptile.ibuf = ibuf;
00160         lookuptile.tx = tx;
00161         lookuptile.ty = ty;
00162         gtile= BLI_ghash_lookup(GLOBAL_CACHE.tilehash, &lookuptile);
00163 
00164         if(gtile) {
00165                 /* in case another thread is loading this */
00166                 while(gtile->loading)
00167                         ;
00168 
00169                 BLI_ghash_remove(GLOBAL_CACHE.tilehash, gtile, NULL, NULL);
00170                 BLI_remlink(&GLOBAL_CACHE.tiles, gtile);
00171                 BLI_addtail(&GLOBAL_CACHE.unused, gtile);
00172         }
00173 
00174         BLI_mutex_unlock(&GLOBAL_CACHE.mutex);
00175 }
00176 
00177 /******************************* Init/Exit ***********************************/
00178 
00179 static void imb_thread_cache_init(ImThreadTileCache *cache)
00180 {
00181         ImThreadTile *ttile;
00182         int a;
00183 
00184         memset(cache, 0, sizeof(ImThreadTileCache));
00185 
00186         cache->tilehash= BLI_ghash_new(imb_thread_tile_hash, imb_thread_tile_cmp, "imb_thread_cache_init gh");
00187 
00188         /* pre-allocate all thread local tiles in unused list */
00189         for(a=0; a<IB_THREAD_CACHE_SIZE; a++) {
00190                 ttile= BLI_memarena_alloc(GLOBAL_CACHE.memarena, sizeof(ImThreadTile));
00191                 BLI_addtail(&cache->unused, ttile);
00192         }
00193 }
00194 
00195 static void imb_thread_cache_exit(ImThreadTileCache *cache)
00196 {
00197         BLI_ghash_free(cache->tilehash, NULL, NULL);
00198 }
00199 
00200 void imb_tile_cache_init(void)
00201 {
00202         memset(&GLOBAL_CACHE, 0, sizeof(ImGlobalTileCache));
00203 
00204         BLI_mutex_init(&GLOBAL_CACHE.mutex);
00205 
00206         /* initialize for one thread, for places that access textures
00207            outside of rendering (displace modifier, painting, ..) */
00208         IMB_tile_cache_params(0, 0);
00209 
00210         GLOBAL_CACHE.initialized = 1;
00211 }
00212 
00213 void imb_tile_cache_exit(void)
00214 {
00215         ImGlobalTile *gtile;
00216         int a;
00217 
00218         if(GLOBAL_CACHE.initialized) {
00219                 for(gtile=GLOBAL_CACHE.tiles.first; gtile; gtile=gtile->next)
00220                         imb_global_cache_tile_unload(gtile);
00221 
00222                 for(a=0; a<GLOBAL_CACHE.totthread; a++)
00223                         imb_thread_cache_exit(&GLOBAL_CACHE.thread_cache[a]);
00224 
00225                 if(GLOBAL_CACHE.memarena)
00226                         BLI_memarena_free(GLOBAL_CACHE.memarena);
00227 
00228                 if(GLOBAL_CACHE.tilehash)
00229                         BLI_ghash_free(GLOBAL_CACHE.tilehash, NULL, NULL);
00230 
00231                 BLI_mutex_end(&GLOBAL_CACHE.mutex);
00232 
00233                 memset(&GLOBAL_CACHE, 0, sizeof(ImGlobalTileCache));
00234         }
00235 }
00236 
00237 /* presumed to be called when no threads are running */
00238 void IMB_tile_cache_params(int totthread, int maxmem)
00239 {
00240         int a;
00241 
00242         /* always one cache for non-threaded access */
00243         totthread++;
00244 
00245         /* lazy initialize cache */
00246         if(GLOBAL_CACHE.totthread == totthread && GLOBAL_CACHE.maxmem == maxmem)
00247                 return;
00248 
00249         imb_tile_cache_exit();
00250 
00251         memset(&GLOBAL_CACHE, 0, sizeof(ImGlobalTileCache));
00252 
00253         GLOBAL_CACHE.tilehash= BLI_ghash_new(imb_global_tile_hash, imb_global_tile_cmp, "tile_cache_params gh");
00254 
00255         GLOBAL_CACHE.memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "ImTileCache arena");
00256         BLI_memarena_use_calloc(GLOBAL_CACHE.memarena);
00257 
00258         GLOBAL_CACHE.maxmem= maxmem*1024*1024;
00259 
00260         GLOBAL_CACHE.totthread= totthread;
00261         for(a=0; a<totthread; a++)
00262                 imb_thread_cache_init(&GLOBAL_CACHE.thread_cache[a]);
00263 
00264         BLI_mutex_init(&GLOBAL_CACHE.mutex);
00265 }
00266 
00267 /***************************** Global Cache **********************************/
00268 
00269 static ImGlobalTile *imb_global_cache_get_tile(ImBuf *ibuf, int tx, int ty, ImGlobalTile *replacetile)
00270 {
00271         ImGlobalTile *gtile, lookuptile;
00272 
00273         BLI_mutex_lock(&GLOBAL_CACHE.mutex);
00274 
00275         if(replacetile)
00276                 replacetile->refcount--;
00277 
00278         /* find tile in global cache */
00279         lookuptile.ibuf = ibuf;
00280         lookuptile.tx = tx;
00281         lookuptile.ty = ty;
00282         gtile= BLI_ghash_lookup(GLOBAL_CACHE.tilehash, &lookuptile);
00283         
00284         if(gtile) {
00285                 /* found tile. however it may be in the process of being loaded
00286                    by another thread, in that case we do stupid busy loop waiting
00287                    for the other thread to load the tile */
00288                 gtile->refcount++;
00289 
00290                 BLI_mutex_unlock(&GLOBAL_CACHE.mutex);
00291 
00292                 while(gtile->loading)
00293                         ;
00294         }
00295         else {
00296                 /* not found, let's load it from disk */
00297 
00298                 /* first check if we hit the memory limit */
00299                 if(GLOBAL_CACHE.maxmem && GLOBAL_CACHE.totmem > GLOBAL_CACHE.maxmem) {
00300                         /* find an existing tile to unload */
00301                         for(gtile=GLOBAL_CACHE.tiles.last; gtile; gtile=gtile->prev)
00302                                 if(gtile->refcount == 0 && gtile->loading == 0)
00303                                         break;
00304                 }
00305 
00306                 if(gtile) {
00307                         /* found a tile to unload */
00308                         imb_global_cache_tile_unload(gtile);
00309                         BLI_ghash_remove(GLOBAL_CACHE.tilehash, gtile, NULL, NULL);
00310                         BLI_remlink(&GLOBAL_CACHE.tiles, gtile);
00311                 }
00312                 else {
00313                         /* allocate a new tile or reuse unused */
00314                         if(GLOBAL_CACHE.unused.first) {
00315                                 gtile= GLOBAL_CACHE.unused.first;
00316                                 BLI_remlink(&GLOBAL_CACHE.unused, gtile);
00317                         }
00318                         else
00319                                 gtile= BLI_memarena_alloc(GLOBAL_CACHE.memarena, sizeof(ImGlobalTile));
00320                 }
00321 
00322                 /* setup new tile */
00323                 gtile->ibuf= ibuf;
00324                 gtile->tx= tx;
00325                 gtile->ty= ty;
00326                 gtile->refcount= 1;
00327                 gtile->loading= 1;
00328 
00329                 BLI_ghash_insert(GLOBAL_CACHE.tilehash, gtile, gtile);
00330                 BLI_addhead(&GLOBAL_CACHE.tiles, gtile);
00331 
00332                 /* mark as being loaded and unlock to allow other threads to load too */
00333                 GLOBAL_CACHE.totmem += sizeof(unsigned int)*ibuf->tilex*ibuf->tiley;
00334 
00335                 BLI_mutex_unlock(&GLOBAL_CACHE.mutex);
00336 
00337                 /* load from disk */
00338                 imb_global_cache_tile_load(gtile);
00339 
00340                 /* mark as done loading */
00341                 gtile->loading= 0;
00342         }
00343 
00344         return gtile;
00345 }
00346 
00347 /***************************** Per-Thread Cache ******************************/
00348 
00349 static unsigned int *imb_thread_cache_get_tile(ImThreadTileCache *cache, ImBuf *ibuf, int tx, int ty)
00350 {
00351         ImThreadTile *ttile, lookuptile;
00352         ImGlobalTile *gtile, *replacetile;
00353         int toffs= ibuf->xtiles*ty + tx;
00354 
00355         /* test if it is already in our thread local cache */
00356         if((ttile=cache->tiles.first)) {
00357                 /* check last used tile before going to hash */
00358                 if(ttile->ibuf == ibuf && ttile->tx == tx && ttile->ty == ty)
00359                         return ibuf->tiles[toffs];
00360 
00361                 /* find tile in hash */
00362                 lookuptile.ibuf = ibuf;
00363                 lookuptile.tx = tx;
00364                 lookuptile.ty = ty;
00365 
00366                 if((ttile=BLI_ghash_lookup(cache->tilehash, &lookuptile))) {
00367                         BLI_remlink(&cache->tiles, ttile);
00368                         BLI_addhead(&cache->tiles, ttile);
00369 
00370                         return ibuf->tiles[toffs];
00371                 }
00372         }
00373 
00374         /* not found, have to do slow lookup in global cache */
00375         if(cache->unused.first == NULL) {
00376                 ttile= cache->tiles.last;
00377                 replacetile= ttile->global;
00378                 BLI_remlink(&cache->tiles, ttile);
00379                 BLI_ghash_remove(cache->tilehash, ttile, NULL, NULL);
00380         }
00381         else {
00382                 ttile= cache->unused.first;
00383                 replacetile= NULL;
00384                 BLI_remlink(&cache->unused, ttile);
00385         }
00386 
00387         BLI_addhead(&cache->tiles, ttile);
00388         BLI_ghash_insert(cache->tilehash, ttile, ttile);
00389 
00390         gtile= imb_global_cache_get_tile(ibuf, tx, ty, replacetile);
00391 
00392         ttile->ibuf= gtile->ibuf;
00393         ttile->tx= gtile->tx;
00394         ttile->ty= gtile->ty;
00395         ttile->global= gtile;
00396 
00397         return ibuf->tiles[toffs];
00398 }
00399 
00400 unsigned int *IMB_gettile(ImBuf *ibuf, int tx, int ty, int thread)
00401 {
00402         return imb_thread_cache_get_tile(&GLOBAL_CACHE.thread_cache[thread+1], ibuf, tx, ty);
00403 }
00404 
00405 void IMB_tiles_to_rect(ImBuf *ibuf)
00406 {
00407         ImBuf *mipbuf;
00408         ImGlobalTile *gtile;
00409         unsigned int *to, *from;
00410         int a, tx, ty, y, w, h;
00411 
00412         for(a=0; a<ibuf->miptot; a++) {
00413                 mipbuf= IMB_getmipmap(ibuf, a);
00414 
00415                 /* don't call imb_addrectImBuf, it frees all mipmaps */
00416                 if(!mipbuf->rect) {
00417                         if((mipbuf->rect = MEM_mapallocN(ibuf->x*ibuf->y*sizeof(unsigned int), "imb_addrectImBuf"))) {
00418                                 mipbuf->mall |= IB_rect;
00419                                 mipbuf->flags |= IB_rect;
00420                         }
00421                         else
00422                                 break;
00423                 }
00424 
00425                 for(ty=0; ty<mipbuf->ytiles; ty++) {
00426                         for(tx=0; tx<mipbuf->xtiles; tx++) {
00427                                 /* acquire tile through cache, this assumes cache is initialized,
00428                                    which it is always now but it's a weak assumption ... */
00429                                 gtile= imb_global_cache_get_tile(mipbuf, tx, ty, NULL);
00430 
00431                                 /* setup pointers */
00432                                 from= mipbuf->tiles[mipbuf->xtiles*ty + tx];
00433                                 to= mipbuf->rect + mipbuf->x*ty*mipbuf->tiley + tx*mipbuf->tilex;
00434 
00435                                 /* exception in tile width/height for tiles at end of image */
00436                                 w= (tx == mipbuf->xtiles-1)? mipbuf->x - tx*mipbuf->tilex: mipbuf->tilex;
00437                                 h= (ty == mipbuf->ytiles-1)? mipbuf->y - ty*mipbuf->tiley: mipbuf->tiley;
00438 
00439                                 for(y=0; y<h; y++) {
00440                                         memcpy(to, from, sizeof(unsigned int)*w);
00441                                         from += mipbuf->tilex;
00442                                         to += mipbuf->x;
00443                                 }
00444 
00445                                 /* decrease refcount for tile again */
00446                                 BLI_mutex_lock(&GLOBAL_CACHE.mutex);
00447                                 gtile->refcount--;
00448                                 BLI_mutex_unlock(&GLOBAL_CACHE.mutex);
00449                         }
00450                 }
00451         }
00452 }
00453