spritecache.cpp

Go to the documentation of this file.
00001 /* $Id: spritecache.cpp 20286 2010-08-01 19:44:49Z 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 
00178 static void *AllocSprite(size_t);
00179 
00180 static void *ReadSprite(SpriteCache *sc, SpriteID id, SpriteType sprite_type)
00181 {
00182   uint8 file_slot = sc->file_slot;
00183   size_t file_pos = sc->file_pos;
00184 
00185   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
00186   assert(sc->type == sprite_type);
00187 
00188   DEBUG(sprite, 9, "Load sprite %d", id);
00189 
00190   if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
00191 #ifdef WITH_PNG
00192     /* Try loading 32bpp graphics in case we are 32bpp output */
00193     SpriteLoaderPNG sprite_loader;
00194     SpriteLoader::Sprite sprite;
00195 
00196     if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id, sprite_type)) {
00197       sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
00198 
00199       return sc->ptr;
00200     }
00201     /* If the PNG couldn't be loaded, fall back to 8bpp grfs */
00202 #else
00203     static bool show_once = true;
00204     if (show_once) {
00205       DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics");
00206       show_once = false;
00207     }
00208 #endif /* WITH_PNG */
00209   }
00210 
00211   FioSeekToFile(file_slot, file_pos);
00212 
00213   /* Read the size and type */
00214   int num  = FioReadWord();
00215   byte type = FioReadByte();
00216 
00217   /* Type 0xFF indicates either a colourmap or some other non-sprite info */
00218   assert((type == 0xFF) == (sprite_type == ST_RECOLOUR));
00219   if (type == 0xFF) {
00220     /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
00221      * number of recolour sprites that are 17 bytes that only exist in DOS
00222      * GRFs which are the same as 257 byte recolour sprites, but with the last
00223      * 240 bytes zeroed.  */
00224     static const int RECOLOUR_SPRITE_SIZE = 257;
00225     byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
00226 
00227     sc->ptr = dest;
00228 
00229     if (_palette_remap_grf[sc->file_slot]) {
00230       byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
00231 
00232       /* Only a few recolour sprites are less than 257 bytes */
00233       if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
00234       FioReadBlock(dest_tmp, num);
00235 
00236       /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
00237       for (int i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
00238         dest[i] = _palette_remap[dest_tmp[_palette_reverse_remap[i - 1] + 1]];
00239       }
00240     } else {
00241       FioReadBlock(dest, num);
00242     }
00243 
00244     return sc->ptr;
00245   }
00246 
00247   /* Ugly hack to work around the problem that the old landscape
00248    *  generator assumes that those sprites are stored uncompressed in
00249    *  the memory, and they are only read directly by the code, never
00250    *  send to the blitter. So do not send it to the blitter (which will
00251    *  result in a data array in the format the blitter likes most), but
00252    *  read the data directly from disk and store that as sprite.
00253    * Ugly: yes. Other solution: no. Blame the original author or
00254    *  something ;) The image should really have been a data-stream
00255    *  (so type = 0xFF basicly). */
00256   if (sprite_type == ST_MAPGEN) {
00257     uint height = FioReadByte();
00258     uint width  = FioReadWord();
00259     Sprite *sprite;
00260     byte *dest;
00261 
00262     num = width * height;
00263     sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num);
00264     sc->ptr = sprite;
00265     sprite->height = height;
00266     sprite->width  = width;
00267     sprite->x_offs = FioReadWord();
00268     sprite->y_offs = FioReadWord();
00269 
00270     dest = sprite->data;
00271     while (num > 0) {
00272       int8 i = FioReadByte();
00273       if (i >= 0) {
00274         num -= i;
00275         for (; i > 0; --i) *dest++ = FioReadByte();
00276       } else {
00277         const byte *rel = dest - (((i & 7) << 8) | FioReadByte());
00278         i = -(i >> 3);
00279         num -= i;
00280         for (; i > 0; --i) *dest++ = *rel++;
00281       }
00282     }
00283 
00284     sc->type = sprite_type;
00285 
00286     return sc->ptr;
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);
00297   }
00298   sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
00299 
00300   return sc->ptr;
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)
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);
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);
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);
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 
00572 void *GetRawSprite(SpriteID sprite, SpriteType type)
00573 {
00574   assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN));
00575   assert(type < ST_INVALID);
00576 
00577   if (!SpriteExists(sprite)) {
00578     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
00579 
00580     /* SPR_IMG_QUERY is a BIG FAT RED ? */
00581     sprite = SPR_IMG_QUERY;
00582   }
00583 
00584   SpriteCache *sc = GetSpriteCache(sprite);
00585 
00586   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc);
00587 
00588   /* Update LRU */
00589   sc->lru = ++_sprite_lru_counter;
00590 
00591   void *p = sc->ptr;
00592 
00593   /* Load the sprite, if it is not loaded, yet */
00594   if (p == NULL) p = ReadSprite(sc, sprite, type);
00595 
00596   return p;
00597 }
00598 
00599 
00600 void GfxInitSpriteMem()
00601 {
00602   /* initialize sprite cache heap */
00603   if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
00604 
00605   /* A big free block */
00606   _spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
00607   /* Sentinel block (identified by size == 0) */
00608   NextBlock(_spritecache_ptr)->size = 0;
00609 
00610   /* Reset the spritecache 'pool' */
00611   free(_spritecache);
00612   _spritecache_items = 0;
00613   _spritecache = NULL;
00614 
00615   _compact_cache_counter = 0;
00616 }
00617 
00618 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer;

Generated on Fri Dec 31 17:15:38 2010 for OpenTTD by  doxygen 1.6.1