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

Generated on Fri May 27 04:19:49 2011 for OpenTTD by  doxygen 1.6.1