# spritecache.cpp

Go to the documentation of this file.

    00001 /* $Id: spritecache.cpp 21788 2011-01-14 16:36:34Z frosch $ */
    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
    00148 SpriteType GetSpriteType(SpriteID sprite)
    00149 {
    00150   if (!SpriteExists(sprite)) return ST_INVALID;
    00151   return GetSpriteCache(sprite)->type;
    00152 }
    00153
    00159 uint GetOriginFileSlot(SpriteID sprite)
    00160 {
    00161   if (!SpriteExists(sprite)) return 0;
    00162   return GetSpriteCache(sprite)->file_slot;
    00163 }
    00164
    00173 uint GetMaxSpriteID()
    00174 {
    00175   return _spritecache_items;
    00176 }
    00177
    00186 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
    00187 {
    00188   uint8 file_slot = sc->file_slot;
    00189   size_t file_pos = sc->file_pos;
    00190
    00191   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
    00192   assert(sc->type == sprite_type);
    00193
    00194   DEBUG(sprite, 9, "Load sprite %d", id);
    00195
    00196   if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
    00197 #ifdef WITH_PNG
    00198     /* Try loading 32bpp graphics in case we are 32bpp output */
    00199     SpriteLoaderPNG sprite_loader;
    00200     SpriteLoader::Sprite sprite;
    00201
    00202     if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id, sprite_type)) {
    00203       return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
    00204     }
    00205     /* If the PNG couldn't be loaded, fall back to 8bpp grfs */
    00206 #else
    00207     static bool show_once = true;
    00208     if (show_once) {
    00209       DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics");
    00210       show_once = false;
    00211     }
    00212 #endif /* WITH_PNG */
    00213   }
    00214
    00215   FioSeekToFile(file_slot, file_pos);
    00216
    00217   /* Read the size and type */
    00218   int num  = FioReadWord();
    00219   byte type = FioReadByte();
    00220
    00221   /* Type 0xFF indicates either a colourmap or some other non-sprite info */
    00222   assert((type == 0xFF) == (sprite_type == ST_RECOLOUR));
    00223   if (type == 0xFF) {
    00224     /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
    00225      * number of recolour sprites that are 17 bytes that only exist in DOS
    00226      * GRFs which are the same as 257 byte recolour sprites, but with the last
    00227      * 240 bytes zeroed.  */
    00228     static const int RECOLOUR_SPRITE_SIZE = 257;
    00229     byte *dest = (byte *)allocator(max(RECOLOUR_SPRITE_SIZE, num));
    00230
    00231     if (_palette_remap_grf[sc->file_slot]) {
    00232       byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
    00233
    00234       /* Only a few recolour sprites are less than 257 bytes */
    00235       if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
    00236       FioReadBlock(dest_tmp, num);
    00237
    00238       /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
    00239       for (int i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
    00240         dest[i] = _palette_remap[dest_tmp[_palette_reverse_remap[i - 1] + 1]];
    00241       }
    00242     } else {
    00243       FioReadBlock(dest, num);
    00244     }
    00245
    00246     return dest;
    00247   }
    00248
    00249   /* Ugly hack to work around the problem that the old landscape
    00250    *  generator assumes that those sprites are stored uncompressed in
    00251    *  the memory, and they are only read directly by the code, never
    00252    *  send to the blitter. So do not send it to the blitter (which will
    00253    *  result in a data array in the format the blitter likes most), but
    00254    *  read the data directly from disk and store that as sprite.
    00255    * Ugly: yes. Other solution: no. Blame the original author or
    00256    *  something ;) The image should really have been a data-stream
    00257    *  (so type = 0xFF basicly). */
    00258   if (sprite_type == ST_MAPGEN) {
    00259     uint height = FioReadByte();
    00260     uint width  = FioReadWord();
    00261     Sprite *sprite;
    00262     byte *dest;
    00263
    00264     num = width * height;
    00265     sprite = (Sprite *)allocator(sizeof(*sprite) + num);
    00266     sprite->height = height;
    00267     sprite->width  = width;
    00268     sprite->x_offs = FioReadWord();
    00269     sprite->y_offs = FioReadWord();
    00270
    00271     dest = sprite->data;
    00272     while (num > 0) {
    00273       int8 i = FioReadByte();
    00274       if (i >= 0) {
    00275         num -= i;
    00276         for (; i > 0; --i) *dest++ = FioReadByte();
    00277       } else {
    00278         const byte *rel = dest - (((i & 7) << 8) | FioReadByte());
    00279         i = -(i >> 3);
    00280         num -= i;
    00281         for (; i > 0; --i) *dest++ = *rel++;
    00282       }
    00283     }
    00284
    00285     return sprite;
    00286   }
    00287
    00288   assert(sprite_type == ST_NORMAL || sprite_type == ST_FONT);
    00289
    00290   SpriteLoaderGrf sprite_loader;
    00291   SpriteLoader::Sprite sprite;
    00292
    00293   if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type)) {
    00294     if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
    00295     return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
    00296   }
    00297   return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
    00298 }
    00299
    00300
    00301 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
    00302 {
    00303   size_t file_pos = FioGetPos();
    00304
    00305   SpriteType type = ReadSpriteHeaderSkipData();
    00306
    00307   if (type == ST_INVALID) return false;
    00308
    00309   if (load_index >= MAX_SPRITES) {
    00310     usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
    00311   }
    00312
    00313   bool is_mapgen = IsMapgenSpriteID(load_index);
    00314
    00315   if (is_mapgen) {
    00316     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?");
    00317     type = ST_MAPGEN;
    00318   }
    00319
    00320   SpriteCache *sc = AllocateSpriteCache(load_index);
    00321   sc->file_slot = file_slot;
    00322   sc->file_pos = file_pos;
    00323   sc->ptr = NULL;
    00324   sc->lru = 0;
    00325   sc->id = file_sprite_id;
    00326   sc->type = type;
    00327   sc->warned = false;
    00328
    00329   return true;
    00330 }
    00331
    00332
    00333 void DupSprite(SpriteID old_spr, SpriteID new_spr)
    00334 {
    00335   SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
    00336   SpriteCache *scold = GetSpriteCache(old_spr);
    00337
    00338   scnew->file_slot = scold->file_slot;
    00339   scnew->file_pos = scold->file_pos;
    00340   scnew->ptr = NULL;
    00341   scnew->id = scold->id;
    00342   scnew->type = scold->type;
    00343   scnew->warned = false;
    00344 }
    00345
    00352 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
    00353
    00354 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
    00355 assert_compile(sizeof(MemBlock) == sizeof(size_t));
    00356 /* make sure it's a power of two */
    00357 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
    00358
    00359 static inline MemBlock *NextBlock(MemBlock *block)
    00360 {
    00361   return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
    00362 }
    00363
    00364 static size_t GetSpriteCacheUsage()
    00365 {
    00366   size_t tot_size = 0;
    00367   MemBlock *s;
    00368
    00369   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00370     if (!(s->size & S_FREE_MASK)) tot_size += s->size;
    00371   }
    00372
    00373   return tot_size;
    00374 }
    00375
    00376
    00377 void IncreaseSpriteLRU()
    00378 {
    00379   /* Increase all LRU values */
    00380   if (_sprite_lru_counter > 16384) {
    00381     SpriteID i;
    00382
    00383     DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
    00384
    00385     for (i = 0; i != _spritecache_items; i++) {
    00386       SpriteCache *sc = GetSpriteCache(i);
    00387       if (sc->ptr != NULL) {
    00388         if (sc->lru >= 0) {
    00389           sc->lru = -1;
    00390         } else if (sc->lru != -32768) {
    00391           sc->lru--;
    00392         }
    00393       }
    00394     }
    00395     _sprite_lru_counter = 0;
    00396   }
    00397
    00398   /* Compact sprite cache every now and then. */
    00399   if (++_compact_cache_counter >= 740) {
    00400     CompactSpriteCache();
    00401     _compact_cache_counter = 0;
    00402   }
    00403 }
    00404
    00409 static void CompactSpriteCache()
    00410 {
    00411   MemBlock *s;
    00412
    00413   DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00414
    00415   for (s = _spritecache_ptr; s->size != 0;) {
    00416     if (s->size & S_FREE_MASK) {
    00417       MemBlock *next = NextBlock(s);
    00418       MemBlock temp;
    00419       SpriteID i;
    00420
    00421       /* Since free blocks are automatically coalesced, this should hold true. */
    00422       assert(!(next->size & S_FREE_MASK));
    00423
    00424       /* If the next block is the sentinel block, we can safely return */
    00425       if (next->size == 0) break;
    00426
    00427       /* Locate the sprite belonging to the next pointer. */
    00428       for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
    00429         assert(i != _spritecache_items);
    00430       }
    00431
    00432       GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
    00433       /* Swap this and the next block */
    00434       temp = *s;
    00435       memmove(s, next, next->size);
    00436       s = NextBlock(s);
    00437       *s = temp;
    00438
    00439       /* Coalesce free blocks */
    00440       while (NextBlock(s)->size & S_FREE_MASK) {
    00441         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00442       }
    00443     } else {
    00444       s = NextBlock(s);
    00445     }
    00446   }
    00447 }
    00448
    00449 static void DeleteEntryFromSpriteCache()
    00450 {
    00451   SpriteID i;
    00452   uint best = UINT_MAX;
    00453   MemBlock *s;
    00454   int cur_lru;
    00455
    00456   DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00457
    00458   cur_lru = 0xffff;
    00459   for (i = 0; i != _spritecache_items; i++) {
    00460     SpriteCache *sc = GetSpriteCache(i);
    00461     if (sc->ptr != NULL && sc->lru < cur_lru) {
    00462       cur_lru = sc->lru;
    00463       best = i;
    00464     }
    00465   }
    00466
    00467   /* Display an error message and die, in case we found no sprite at all.
    00468    * This shouldn't really happen, unless all sprites are locked. */
    00469   if (best == UINT_MAX) error("Out of sprite memory");
    00470
    00471   /* Mark the block as free (the block must be in use) */
    00472   s = (MemBlock*)GetSpriteCache(best)->ptr - 1;
    00473   assert(!(s->size & S_FREE_MASK));
    00474   s->size |= S_FREE_MASK;
    00475   GetSpriteCache(best)->ptr = NULL;
    00476
    00477   /* And coalesce adjacent free blocks */
    00478   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00479     if (s->size & S_FREE_MASK) {
    00480       while (NextBlock(s)->size & S_FREE_MASK) {
    00481         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00482       }
    00483     }
    00484   }
    00485 }
    00486
    00487 static void *AllocSprite(size_t mem_req)
    00488 {
    00489   mem_req += sizeof(MemBlock);
    00490
    00491   /* Align this to correct boundary. This also makes sure at least one
    00492    * bit is not used, so we can use it for other things. */
    00493   mem_req = Align(mem_req, S_FREE_MASK + 1);
    00494
    00495   for (;;) {
    00496     MemBlock *s;
    00497
    00498     for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00499       if (s->size & S_FREE_MASK) {
    00500         size_t cur_size = s->size & ~S_FREE_MASK;
    00501
    00502         /* Is the block exactly the size we need or
    00503          * big enough for an additional free block? */
    00504         if (cur_size == mem_req ||
    00505             cur_size >= mem_req + sizeof(MemBlock)) {
    00506           /* Set size and in use */
    00507           s->size = mem_req;
    00508
    00509           /* Do we need to inject a free block too? */
    00510           if (cur_size != mem_req) {
    00511             NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
    00512           }
    00513
    00514           return s->data;
    00515         }
    00516       }
    00517     }
    00518
    00519     /* Reached sentinel, but no block found yet. Delete some old entry. */
    00520     DeleteEntryFromSpriteCache();
    00521   }
    00522 }
    00523
    00533 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
    00534 {
    00535   static const char * const sprite_types[] = {
    00536     "normal",        // ST_NORMAL
    00537     "map generator", // ST_MAPGEN
    00538     "character",     // ST_FONT
    00539     "recolour",      // ST_RECOLOUR
    00540   };
    00541
    00542   SpriteType available = sc->type;
    00543   if (requested == ST_FONT && available == ST_NORMAL) {
    00544     if (sc->ptr == NULL) sc->type = ST_FONT;
    00545     return GetRawSprite(sprite, sc->type, allocator);
    00546   }
    00547
    00548   byte warning_level = sc->warned ? 6 : 0;
    00549   sc->warned = true;
    00550   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]);
    00551
    00552   switch (requested) {
    00553     case ST_NORMAL:
    00554       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?");
    00555       /* FALL THROUGH */
    00556     case ST_FONT:
    00557       return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
    00558     case ST_RECOLOUR:
    00559       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?");
    00560       return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
    00561     case ST_MAPGEN:
    00562       /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
    00563        * (the only case the check fails is when these sprites weren't even loaded...) */
    00564     default:
    00565       NOT_REACHED();
    00566   }
    00567 }
    00568
    00577 void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
    00578 {
    00579   assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN));
    00580   assert(type < ST_INVALID);
    00581
    00582   if (!SpriteExists(sprite)) {
    00583     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
    00584
    00585     /* SPR_IMG_QUERY is a BIG FAT RED ? */
    00586     sprite = SPR_IMG_QUERY;
    00587   }
    00588
    00589   SpriteCache *sc = GetSpriteCache(sprite);
    00590
    00591   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
    00592
    00593   if (allocator == NULL) {
    00594     /* Load sprite into/from spritecache */
    00595
    00596     /* Update LRU */
    00597     sc->lru = ++_sprite_lru_counter;
    00598
    00599     /* Load the sprite, if it is not loaded, yet */
    00600     if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
    00601
    00602     return sc->ptr;
    00603   } else {
    00604     /* Do not use the spritecache, but a different allocator. */
    00605     return ReadSprite(sc, sprite, type, allocator);
    00606   }
    00607 }
    00608
    00609
    00610 void GfxInitSpriteMem()
    00611 {
    00612   /* initialize sprite cache heap */
    00613   if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
    00614
    00615   /* A big free block */
    00616   _spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
    00617   /* Sentinel block (identified by size == 0) */
    00618   NextBlock(_spritecache_ptr)->size = 0;
    00619
    00620   /* Reset the spritecache 'pool' */
    00621   free(_spritecache);
    00622   _spritecache_items = 0;
    00623   _spritecache = NULL;
    00624
    00625   _compact_cache_counter = 0;
    00626 }
    00627
    00628 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer;