# spritecache.cpp

Go to the documentation of this file.

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