# spritecache.cpp

Go to the documentation of this file.

    00001 /* $Id: spritecache.cpp 16423 2009-05-25 17:15:15Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "gfx_type.h"
    00007 #include "fileio_func.h"
    00008 #include "spriteloader/grf.hpp"
    00009 #include "core/alloc_func.hpp"
    00010 #include "gfx_func.h"
    00011 #ifdef WITH_PNG
    00012 #include "spriteloader/png.hpp"
    00013 #endif /* WITH_PNG */
    00014 #include "blitter/factory.hpp"
    00015 #include "core/math_func.hpp"
    00016
    00017 #include "table/sprites.h"
    00018
    00019 /* Default of 4MB spritecache */
    00020 uint _sprite_cache_size = 4;
    00021
    00022 typedef SimpleTinyEnumT<SpriteType, byte> SpriteTypeByte;
    00023
    00024 struct SpriteCache {
    00025   void *ptr;
    00026   size_t file_pos;
    00027   uint32 id;
    00028   uint16 file_slot;
    00029   int16 lru;
    00030   SpriteTypeByte type;
    00031   bool warned;
    00032 };
    00033
    00034
    00035 static uint _spritecache_items = 0;
    00036 static SpriteCache *_spritecache = NULL;
    00037
    00038
    00039 static inline SpriteCache *GetSpriteCache(uint index)
    00040 {
    00041   return &_spritecache[index];
    00042 }
    00043
    00044 static inline bool IsMapgenSpriteID(SpriteID sprite)
    00045 {
    00046   return IsInsideMM(sprite, 4845, 4882);
    00047 }
    00048
    00049 static SpriteCache *AllocateSpriteCache(uint index)
    00050 {
    00051   if (index >= _spritecache_items) {
    00052     /* Add another 1024 items to the 'pool' */
    00053     uint items = Align(index + 1, 1024);
    00054
    00055     DEBUG(sprite, 4, "Increasing sprite cache to %u items (" PRINTF_SIZE " bytes)", items, items * sizeof(*_spritecache));
    00056
    00057     _spritecache = ReallocT(_spritecache, items);
    00058
    00059     /* Reset the new items and update the count */
    00060     memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
    00061     _spritecache_items = items;
    00062   }
    00063
    00064   return GetSpriteCache(index);
    00065 }
    00066
    00067
    00068 struct MemBlock {
    00069   size_t size;
    00070   byte data[VARARRAY_SIZE];
    00071 };
    00072
    00073 static uint _sprite_lru_counter;
    00074 static MemBlock *_spritecache_ptr;
    00075 static int _compact_cache_counter;
    00076
    00077 static void CompactSpriteCache();
    00078
    00085 bool SkipSpriteData(byte type, uint16 num)
    00086 {
    00087   if (type & 2) {
    00088     FioSkipBytes(num);
    00089   } else {
    00090     while (num > 0) {
    00091       int8 i = FioReadByte();
    00092       if (i >= 0) {
    00093         int size = (i == 0) ? 0x80 : i;
    00094         if (size > num) return false;
    00095         num -= size;
    00096         FioSkipBytes(size);
    00097       } else {
    00098         i = -(i >> 3);
    00099         num -= i;
    00100         FioReadByte();
    00101       }
    00102     }
    00103   }
    00104   return true;
    00105 }
    00106
    00111 static SpriteType ReadSpriteHeaderSkipData()
    00112 {
    00113   uint16 num = FioReadWord();
    00114
    00115   if (num == 0) return ST_INVALID;
    00116
    00117   byte type = FioReadByte();
    00118   if (type == 0xFF) {
    00119     FioSkipBytes(num);
    00120     /* Some NewGRF files have "empty" pseudo-sprites which are 1
    00121      * byte long. Catch these so the sprites won't be displayed. */
    00122     return (num == 1) ? ST_INVALID : ST_RECOLOUR;
    00123   }
    00124
    00125   FioSkipBytes(7);
    00126   return SkipSpriteData(type, num - 8) ? ST_NORMAL : ST_INVALID;
    00127 }
    00128
    00129 /* Check if the given Sprite ID exists */
    00130 bool SpriteExists(SpriteID id)
    00131 {
    00132   /* Special case for Sprite ID zero -- its position is also 0... */
    00133   if (id == 0) return true;
    00134   if (id >= _spritecache_items) return false;
    00135   return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
    00136 }
    00137
    00138 void *AllocSprite(size_t);
    00139
    00140 static void *ReadSprite(SpriteCache *sc, SpriteID id, SpriteType sprite_type)
    00141 {
    00142   uint8 file_slot = sc->file_slot;
    00143   size_t file_pos = sc->file_pos;
    00144
    00145   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
    00146   assert(sc->type == sprite_type);
    00147
    00148   DEBUG(sprite, 9, "Load sprite %d", id);
    00149
    00150   if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
    00151 #ifdef WITH_PNG
    00152     /* Try loading 32bpp graphics in case we are 32bpp output */
    00153     SpriteLoaderPNG sprite_loader;
    00154     SpriteLoader::Sprite sprite;
    00155
    00156     if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id, sprite_type)) {
    00157       sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
    00158
    00159       return sc->ptr;
    00160     }
    00161     /* If the PNG couldn't be loaded, fall back to 8bpp grfs */
    00162 #else
    00163     static bool show_once = true;
    00164     if (show_once) {
    00165       DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics");
    00166       show_once = false;
    00167     }
    00168 #endif /* WITH_PNG */
    00169   }
    00170
    00171   FioSeekToFile(file_slot, file_pos);
    00172
    00173   /* Read the size and type */
    00174   int num  = FioReadWord();
    00175   byte type = FioReadByte();
    00176
    00177   /* Type 0xFF indicates either a colourmap or some other non-sprite info */
    00178   assert((type == 0xFF) == (sprite_type == ST_RECOLOUR));
    00179   if (type == 0xFF) {
    00180     /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
    00181      * number of recolour sprites that are 17 bytes that only exist in DOS
    00182      * GRFs which are the same as 257 byte recolour sprites, but with the last
    00183      * 240 bytes zeroed.  */
    00184     static const int RECOLOUR_SPRITE_SIZE = 257;
    00185     byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
    00186
    00187     sc->ptr = dest;
    00188
    00189     if (_palette_remap_grf[sc->file_slot]) {
    00190       byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
    00191
    00192       /* Only a few recolour sprites are less than 257 bytes */
    00193       if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
    00194       FioReadBlock(dest_tmp, num);
    00195
    00196       /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
    00197       for (int i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
    00198         dest[i] = _palette_remap[dest_tmp[_palette_reverse_remap[i - 1] + 1]];
    00199       }
    00200     } else {
    00201       FioReadBlock(dest, num);
    00202     }
    00203
    00204     return sc->ptr;
    00205   }
    00206
    00207   /* Ugly hack to work around the problem that the old landscape
    00208    *  generator assumes that those sprites are stored uncompressed in
    00209    *  the memory, and they are only read directly by the code, never
    00210    *  send to the blitter. So do not send it to the blitter (which will
    00211    *  result in a data array in the format the blitter likes most), but
    00212    *  read the data directly from disk and store that as sprite.
    00213    * Ugly: yes. Other solution: no. Blame the original author or
    00214    *  something ;) The image should really have been a data-stream
    00215    *  (so type = 0xFF basicly). */
    00216   if (sprite_type == ST_MAPGEN) {
    00217     uint height = FioReadByte();
    00218     uint width  = FioReadWord();
    00219     Sprite *sprite;
    00220     byte *dest;
    00221
    00222     num = width * height;
    00223     sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num);
    00224     sc->ptr = sprite;
    00225     sprite->height = height;
    00226     sprite->width  = width;
    00227     sprite->x_offs = FioReadWord();
    00228     sprite->y_offs = FioReadWord();
    00229
    00230     dest = sprite->data;
    00231     while (num > 0) {
    00232       int8 i = FioReadByte();
    00233       if (i >= 0) {
    00234         num -= i;
    00235         for (; i > 0; --i) *dest++ = FioReadByte();
    00236       } else {
    00237         const byte *rel = dest - (((i & 7) << 8) | FioReadByte());
    00238         i = -(i >> 3);
    00239         num -= i;
    00240         for (; i > 0; --i) *dest++ = *rel++;
    00241       }
    00242     }
    00243
    00244     sc->type = sprite_type;
    00245
    00246     return sc->ptr;
    00247   }
    00248
    00249   assert(sprite_type == ST_NORMAL || sprite_type == ST_FONT);
    00250
    00251   SpriteLoaderGrf sprite_loader;
    00252   SpriteLoader::Sprite sprite;
    00253
    00254   if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type)) {
    00255     if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
    00256     return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL);
    00257   }
    00258   sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
    00259
    00260   return sc->ptr;
    00261 }
    00262
    00263
    00264 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
    00265 {
    00266   size_t file_pos = FioGetPos();
    00267
    00268   SpriteType type = ReadSpriteHeaderSkipData();
    00269
    00270   if (type == ST_INVALID) return false;
    00271
    00272   if (load_index >= MAX_SPRITES) {
    00273     usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
    00274   }
    00275
    00276   bool is_mapgen = IsMapgenSpriteID(load_index);
    00277
    00278   if (is_mapgen) {
    00279     if (type != ST_NORMAL) usererror("Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites?");
    00280     type = ST_MAPGEN;
    00281   }
    00282
    00283   SpriteCache *sc = AllocateSpriteCache(load_index);
    00284   sc->file_slot = file_slot;
    00285   sc->file_pos = file_pos;
    00286   sc->ptr = NULL;
    00287   sc->lru = 0;
    00288   sc->id = file_sprite_id;
    00289   sc->type = type;
    00290   sc->warned = false;
    00291
    00292   return true;
    00293 }
    00294
    00295
    00296 void DupSprite(SpriteID old_spr, SpriteID new_spr)
    00297 {
    00298   SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
    00299   SpriteCache *scold = GetSpriteCache(old_spr);
    00300
    00301   scnew->file_slot = scold->file_slot;
    00302   scnew->file_pos = scold->file_pos;
    00303   scnew->ptr = NULL;
    00304   scnew->id = scold->id;
    00305   scnew->type = scold->type;
    00306   scnew->warned = false;
    00307 }
    00308
    00315 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
    00316
    00317 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
    00318 assert_compile(sizeof(MemBlock) == sizeof(size_t));
    00319 /* make sure it's a power of two */
    00320 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
    00321
    00322 static inline MemBlock *NextBlock(MemBlock *block)
    00323 {
    00324   return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
    00325 }
    00326
    00327 static size_t GetSpriteCacheUsage()
    00328 {
    00329   size_t tot_size = 0;
    00330   MemBlock *s;
    00331
    00332   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00333     if (!(s->size & S_FREE_MASK)) tot_size += s->size;
    00334   }
    00335
    00336   return tot_size;
    00337 }
    00338
    00339
    00340 void IncreaseSpriteLRU()
    00341 {
    00342   /* Increase all LRU values */
    00343   if (_sprite_lru_counter > 16384) {
    00344     SpriteID i;
    00345
    00346     DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
    00347
    00348     for (i = 0; i != _spritecache_items; i++) {
    00349       SpriteCache *sc = GetSpriteCache(i);
    00350       if (sc->ptr != NULL) {
    00351         if (sc->lru >= 0) {
    00352           sc->lru = -1;
    00353         } else if (sc->lru != -32768) {
    00354           sc->lru--;
    00355         }
    00356       }
    00357     }
    00358     _sprite_lru_counter = 0;
    00359   }
    00360
    00361   /* Compact sprite cache every now and then. */
    00362   if (++_compact_cache_counter >= 740) {
    00363     CompactSpriteCache();
    00364     _compact_cache_counter = 0;
    00365   }
    00366 }
    00367
    00370 static void CompactSpriteCache()
    00371 {
    00372   MemBlock *s;
    00373
    00374   DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00375
    00376   for (s = _spritecache_ptr; s->size != 0;) {
    00377     if (s->size & S_FREE_MASK) {
    00378       MemBlock *next = NextBlock(s);
    00379       MemBlock temp;
    00380       SpriteID i;
    00381
    00382       /* Since free blocks are automatically coalesced, this should hold true. */
    00383       assert(!(next->size & S_FREE_MASK));
    00384
    00385       /* If the next block is the sentinel block, we can safely return */
    00386       if (next->size == 0) break;
    00387
    00388       /* Locate the sprite belonging to the next pointer. */
    00389       for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
    00390         assert(i != _spritecache_items);
    00391       }
    00392
    00393       GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
    00394       /* Swap this and the next block */
    00395       temp = *s;
    00396       memmove(s, next, next->size);
    00397       s = NextBlock(s);
    00398       *s = temp;
    00399
    00400       /* Coalesce free blocks */
    00401       while (NextBlock(s)->size & S_FREE_MASK) {
    00402         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00403       }
    00404     } else {
    00405       s = NextBlock(s);
    00406     }
    00407   }
    00408 }
    00409
    00410 static void DeleteEntryFromSpriteCache()
    00411 {
    00412   SpriteID i;
    00413   uint best = UINT_MAX;
    00414   MemBlock *s;
    00415   int cur_lru;
    00416
    00417   DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00418
    00419   cur_lru = 0xffff;
    00420   for (i = 0; i != _spritecache_items; i++) {
    00421     SpriteCache *sc = GetSpriteCache(i);
    00422     if (sc->ptr != NULL && sc->lru < cur_lru) {
    00423       cur_lru = sc->lru;
    00424       best = i;
    00425     }
    00426   }
    00427
    00428   /* Display an error message and die, in case we found no sprite at all.
    00429    * This shouldn't really happen, unless all sprites are locked. */
    00430   if (best == UINT_MAX) error("Out of sprite memory");
    00431
    00432   /* Mark the block as free (the block must be in use) */
    00433   s = (MemBlock*)GetSpriteCache(best)->ptr - 1;
    00434   assert(!(s->size & S_FREE_MASK));
    00435   s->size |= S_FREE_MASK;
    00436   GetSpriteCache(best)->ptr = NULL;
    00437
    00438   /* And coalesce adjacent free blocks */
    00439   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00440     if (s->size & S_FREE_MASK) {
    00441       while (NextBlock(s)->size & S_FREE_MASK) {
    00442         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00443       }
    00444     }
    00445   }
    00446 }
    00447
    00448 void *AllocSprite(size_t mem_req)
    00449 {
    00450   mem_req += sizeof(MemBlock);
    00451
    00452   /* Align this to correct boundary. This also makes sure at least one
    00453    * bit is not used, so we can use it for other things. */
    00454   mem_req = Align(mem_req, S_FREE_MASK + 1);
    00455
    00456   for (;;) {
    00457     MemBlock *s;
    00458
    00459     for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00460       if (s->size & S_FREE_MASK) {
    00461         size_t cur_size = s->size & ~S_FREE_MASK;
    00462
    00463         /* Is the block exactly the size we need or
    00464          * big enough for an additional free block? */
    00465         if (cur_size == mem_req ||
    00466             cur_size >= mem_req + sizeof(MemBlock)) {
    00467           /* Set size and in use */
    00468           s->size = mem_req;
    00469
    00470           /* Do we need to inject a free block too? */
    00471           if (cur_size != mem_req) {
    00472             NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
    00473           }
    00474
    00475           return s->data;
    00476         }
    00477       }
    00478     }
    00479
    00480     /* Reached sentinel, but no block found yet. Delete some old entry. */
    00481     DeleteEntryFromSpriteCache();
    00482   }
    00483 }
    00484
    00492 static const void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc)
    00493 {
    00494   static const char *sprite_types[] = {
    00495     "normal",        // ST_NORMAL
    00496     "map generator", // ST_MAPGEN
    00497     "character",     // ST_FONT
    00498     "recolour",      // ST_RECOLOUR
    00499   };
    00500
    00501   SpriteType available = sc->type;
    00502   if (requested == ST_FONT && available == ST_NORMAL) {
    00503     if (sc->ptr == NULL) sc->type = ST_FONT;
    00504     return GetRawSprite(sprite, sc->type);
    00505   }
    00506
    00507   byte warning_level = sc->warned ? 6 : 0;
    00508   sc->warned = true;
    00509   DEBUG(sprite, warning_level, "Tried to load %s sprite #%d as a %s sprite. Probable cause: NewGRF interference", sprite_types[available], sprite, sprite_types[requested]);
    00510
    00511   switch (requested) {
    00512     case ST_NORMAL:
    00513       if (sprite == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite?");
    00514       /* FALLTHROUGH */
    00515     case ST_FONT:
    00516       return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL);
    00517     case ST_RECOLOUR:
    00518       if (sprite == PALETTE_TO_DARK_BLUE) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite?");
    00519       return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR);
    00520     case ST_MAPGEN:
    00521       /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
    00522        * (the only case the check fails is when these sprites weren't even loaded...) */
    00523     default:
    00524       NOT_REACHED();
    00525   }
    00526 }
    00527
    00528 const void *GetRawSprite(SpriteID sprite, SpriteType type)
    00529 {
    00530   assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN));
    00531   assert(type < ST_INVALID);
    00532
    00533   if (!SpriteExists(sprite)) {
    00534     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
    00535
    00536     /* SPR_IMG_QUERY is a BIG FAT RED ? */
    00537     sprite = SPR_IMG_QUERY;
    00538   }
    00539
    00540   SpriteCache *sc = GetSpriteCache(sprite);
    00541
    00542   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc);
    00543
    00544   /* Update LRU */
    00545   sc->lru = ++_sprite_lru_counter;
    00546
    00547   void *p = sc->ptr;
    00548
    00549   /* Load the sprite, if it is not loaded, yet */
    00550   if (p == NULL) p = ReadSprite(sc, sprite, type);
    00551
    00552   return p;
    00553 }
    00554
    00555
    00556 void GfxInitSpriteMem()
    00557 {
    00558   /* initialize sprite cache heap */
    00559   if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
    00560
    00561   /* A big free block */
    00562   _spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
    00563   /* Sentinel block (identified by size == 0) */
    00564   NextBlock(_spritecache_ptr)->size = 0;
    00565
    00566   /* Reset the spritecache 'pool' */
    00567   free(_spritecache);
    00568   _spritecache_items = 0;
    00569   _spritecache = NULL;
    00570
    00571   _compact_cache_counter = 0;
    00572 }
    00573
    00574 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer;