spritecache.cpp

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