# spritecache.cpp

Go to the documentation of this file.

    00001 /* $Id: spritecache.cpp 26209 2014-01-02 22:41:58Z 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 #include "error.h"
    00017 #include "zoom_func.h"
    00018 #include "settings_type.h"
    00019 #include "blitter/factory.hpp"
    00020 #include "core/math_func.hpp"
    00021 #include "core/mem_func.hpp"
    00022
    00023 #include "table/sprites.h"
    00024 #include "table/strings.h"
    00025 #include "table/palette_convert.h"
    00026
    00027 /* Default of 4MB spritecache */
    00028 uint _sprite_cache_size = 4;
    00029
    00030 typedef SimpleTinyEnumT<SpriteType, byte> SpriteTypeByte;
    00031
    00032 struct SpriteCache {
    00033   void *ptr;
    00034   size_t file_pos;
    00035   uint32 id;
    00036   uint16 file_slot;
    00037   int16 lru;
    00038   SpriteTypeByte type;
    00039   bool warned;
    00040   byte container_ver;
    00041 };
    00042
    00043
    00044 static uint _spritecache_items = 0;
    00045 static SpriteCache *_spritecache = NULL;
    00046
    00047
    00048 static inline SpriteCache *GetSpriteCache(uint index)
    00049 {
    00050   return &_spritecache[index];
    00051 }
    00052
    00053 static inline bool IsMapgenSpriteID(SpriteID sprite)
    00054 {
    00055   return IsInsideMM(sprite, 4845, 4882);
    00056 }
    00057
    00058 static SpriteCache *AllocateSpriteCache(uint index)
    00059 {
    00060   if (index >= _spritecache_items) {
    00061     /* Add another 1024 items to the 'pool' */
    00062     uint items = Align(index + 1, 1024);
    00063
    00064     DEBUG(sprite, 4, "Increasing sprite cache to %u items (" PRINTF_SIZE " bytes)", items, items * sizeof(*_spritecache));
    00065
    00066     _spritecache = ReallocT(_spritecache, items);
    00067
    00068     /* Reset the new items and update the count */
    00069     memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
    00070     _spritecache_items = items;
    00071   }
    00072
    00073   return GetSpriteCache(index);
    00074 }
    00075
    00076
    00077 struct MemBlock {
    00078   size_t size;
    00079   byte data[];
    00080 };
    00081
    00082 static uint _sprite_lru_counter;
    00083 static MemBlock *_spritecache_ptr;
    00084 static uint _allocated_sprite_cache_size = 0;
    00085 static int _compact_cache_counter;
    00086
    00087 static void CompactSpriteCache();
    00088 static void *AllocSprite(size_t mem_req);
    00089
    00096 bool SkipSpriteData(byte type, uint16 num)
    00097 {
    00098   if (type & 2) {
    00099     FioSkipBytes(num);
    00100   } else {
    00101     while (num > 0) {
    00102       int8 i = FioReadByte();
    00103       if (i >= 0) {
    00104         int size = (i == 0) ? 0x80 : i;
    00105         if (size > num) return false;
    00106         num -= size;
    00107         FioSkipBytes(size);
    00108       } else {
    00109         i = -(i >> 3);
    00110         num -= i;
    00111         FioReadByte();
    00112       }
    00113     }
    00114   }
    00115   return true;
    00116 }
    00117
    00118 /* Check if the given Sprite ID exists */
    00119 bool SpriteExists(SpriteID id)
    00120 {
    00121   if (id >= _spritecache_items) return false;
    00122
    00123   /* Special case for Sprite ID zero -- its position is also 0... */
    00124   if (id == 0) return true;
    00125   return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
    00126 }
    00127
    00133 SpriteType GetSpriteType(SpriteID sprite)
    00134 {
    00135   if (!SpriteExists(sprite)) return ST_INVALID;
    00136   return GetSpriteCache(sprite)->type;
    00137 }
    00138
    00144 uint GetOriginFileSlot(SpriteID sprite)
    00145 {
    00146   if (!SpriteExists(sprite)) return 0;
    00147   return GetSpriteCache(sprite)->file_slot;
    00148 }
    00149
    00158 uint GetMaxSpriteID()
    00159 {
    00160   return _spritecache_items;
    00161 }
    00162
    00163 static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt)
    00164 {
    00165   uint8 scaled_1 = ScaleByZoom(1, (ZoomLevel)(src - tgt));
    00166
    00167   /* Check for possible memory overflow. */
    00168   if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
    00169
    00170   sprite[tgt].width  = sprite[src].width  * scaled_1;
    00171   sprite[tgt].height = sprite[src].height * scaled_1;
    00172   sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
    00173   sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
    00174
    00175   sprite[tgt].AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
    00176
    00177   SpriteLoader::CommonPixel *dst = sprite[tgt].data;
    00178   for (int y = 0; y < sprite[tgt].height; y++) {
    00179     const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
    00180     for (int x = 0; x < sprite[tgt].width; x++) {
    00181       *dst = src_ln[x / scaled_1];
    00182       dst++;
    00183     }
    00184   }
    00185
    00186   return true;
    00187 }
    00188
    00189 static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom)
    00190 {
    00191   /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
    00192   sprite[zoom].width  = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom);
    00193   sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom);
    00194   sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom);
    00195   sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom);
    00196
    00197   sprite[zoom].AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
    00198
    00199   SpriteLoader::CommonPixel *dst = sprite[zoom].data;
    00200   const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
    00201   const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
    00202
    00203   for (uint y = 0; y < sprite[zoom].height; y++) {
    00204     const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width;
    00205     assert(src_ln <= src_end);
    00206     for (uint x = 0; x < sprite[zoom].width; x++) {
    00207       assert(src < src_ln);
    00208       if (src + 1 != src_ln && (src + 1)->a != 0) {
    00209         *dst = *(src + 1);
    00210       } else {
    00211         *dst = *src;
    00212       }
    00213       dst++;
    00214       src += 2;
    00215     }
    00216     src = src_ln + sprite[zoom - 1].width;
    00217   }
    00218 }
    00219
    00220 static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
    00221 {
    00222   uint width  = sprite->width + pad_left + pad_right;
    00223   uint height = sprite->height + pad_top + pad_bottom;
    00224
    00225   if (width > UINT16_MAX || height > UINT16_MAX) return false;
    00226
    00227   /* Copy source data and reallocate sprite memory. */
    00228   SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
    00229   MemCpyT(src_data, sprite->data, sprite->width * sprite->height);
    00230   sprite->AllocateData(zoom, width * height);
    00231
    00232   /* Copy with padding to destination. */
    00233   SpriteLoader::CommonPixel *src = src_data;
    00234   SpriteLoader::CommonPixel *data = sprite->data;
    00235   for (uint y = 0; y < height; y++) {
    00236     if (y < pad_top || pad_bottom + y >= height) {
    00237       /* Top/bottom padding. */
    00238       MemSetT(data, 0, width);
    00239       data += width;
    00240     } else {
    00241       if (pad_left > 0) {
    00242         /* Pad left. */
    00243         MemSetT(data, 0, pad_left);
    00244         data += pad_left;
    00245       }
    00246
    00247       /* Copy pixels. */
    00248       MemCpyT(data, src, sprite->width);
    00249       src += sprite->width;
    00250       data += sprite->width;
    00251
    00252       if (pad_right > 0) {
    00253         /* Pad right. */
    00254         MemSetT(data, 0, pad_right);
    00255         data += pad_right;
    00256       }
    00257     }
    00258   }
    00259   free(src_data);
    00260
    00261   /* Update sprite size. */
    00262   sprite->width   = width;
    00263   sprite->height  = height;
    00264   sprite->x_offs -= pad_left;
    00265   sprite->y_offs -= pad_top;
    00266
    00267   return true;
    00268 }
    00269
    00270 static bool PadSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail)
    00271 {
    00272   /* Get minimum top left corner coordinates. */
    00273   int min_xoffs = INT32_MAX;
    00274   int min_yoffs = INT32_MAX;
    00275   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
    00276     if (HasBit(sprite_avail, zoom)) {
    00277       min_xoffs = min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
    00278       min_yoffs = min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
    00279     }
    00280   }
    00281
    00282   /* Get maximum dimensions taking necessary padding at the top left into account. */
    00283   int max_width  = INT32_MIN;
    00284   int max_height = INT32_MIN;
    00285   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
    00286     if (HasBit(sprite_avail, zoom)) {
    00287       max_width  = max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
    00288       max_height = max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
    00289     }
    00290   }
    00291
    00292   /* Pad sprites where needed. */
    00293   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
    00294     if (HasBit(sprite_avail, zoom)) {
    00295       /* Scaling the sprite dimensions in the blitter is done with rounding up,
    00296        * so a negative padding here is not an error. */
    00297       int pad_left   = max(0, sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom));
    00298       int pad_top    = max(0, sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom));
    00299       int pad_right  = max(0, UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
    00300       int pad_bottom = max(0, UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
    00301
    00302       if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
    00303         if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
    00304       }
    00305     }
    00306   }
    00307
    00308   return true;
    00309 }
    00310
    00311 static bool ResizeSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, uint32 file_slot, uint32 file_pos)
    00312 {
    00313   /* Create a fully zoomed image if it does not exist */
    00314   ZoomLevel first_avail = static_cast<ZoomLevel>(FIND_FIRST_BIT(sprite_avail));
    00315   if (first_avail != ZOOM_LVL_NORMAL) {
    00316     if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_NORMAL)) return false;
    00317     SetBit(sprite_avail, ZOOM_LVL_NORMAL);
    00318   }
    00319
    00320   /* Pad sprites to make sizes match. */
    00321   if (!PadSprites(sprite, sprite_avail)) return false;
    00322
    00323   /* Create other missing zoom levels */
    00324   for (ZoomLevel zoom = ZOOM_LVL_OUT_2X; zoom != ZOOM_LVL_END; zoom++) {
    00325     if (HasBit(sprite_avail, zoom)) {
    00326       /* Check that size and offsets match the fully zoomed image. */
    00327       assert(sprite[zoom].width  == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom));
    00328       assert(sprite[zoom].height == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom));
    00329       assert(sprite[zoom].x_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom));
    00330       assert(sprite[zoom].y_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom));
    00331     }
    00332
    00333     /* Zoom level is not available, or unusable, so create it */
    00334     if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
    00335   }
    00336
    00337   return  true;
    00338 }
    00339
    00346 static void *ReadRecolourSprite(uint16 file_slot, uint num)
    00347 {
    00348   /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
    00349    * number of recolour sprites that are 17 bytes that only exist in DOS
    00350    * GRFs which are the same as 257 byte recolour sprites, but with the last
    00351    * 240 bytes zeroed.  */
    00352   static const uint RECOLOUR_SPRITE_SIZE = 257;
    00353   byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
    00354
    00355   if (_palette_remap_grf[file_slot]) {
    00356     byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
    00357
    00358     /* Only a few recolour sprites are less than 257 bytes */
    00359     if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
    00360     FioReadBlock(dest_tmp, num);
    00361
    00362     /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
    00363     for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
    00364       dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
    00365     }
    00366   } else {
    00367     FioReadBlock(dest, num);
    00368   }
    00369
    00370   return dest;
    00371 }
    00372
    00381 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
    00382 {
    00383   uint8 file_slot = sc->file_slot;
    00384   size_t file_pos = sc->file_pos;
    00385
    00386   assert(sprite_type != ST_RECOLOUR);
    00387   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
    00388   assert(sc->type == sprite_type);
    00389
    00390   DEBUG(sprite, 9, "Load sprite %d", id);
    00391
    00392   SpriteLoader::Sprite sprite[ZOOM_LVL_COUNT];
    00393   uint8 sprite_avail = 0;
    00394   sprite[ZOOM_LVL_NORMAL].type = sprite_type;
    00395
    00396   SpriteLoaderGrf sprite_loader(sc->container_ver);
    00397   if (sprite_type != ST_MAPGEN && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 32) {
    00398     /* Try for 32bpp sprites first. */
    00399     sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, true);
    00400   }
    00401   if (sprite_avail == 0) {
    00402     sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, false);
    00403   }
    00404
    00405   if (sprite_avail == 0) {
    00406     if (sprite_type == ST_MAPGEN) return NULL;
    00407     if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
    00408     return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
    00409   }
    00410
    00411   if (sprite_type == ST_MAPGEN) {
    00412     /* Ugly hack to work around the problem that the old landscape
    00413      *  generator assumes that those sprites are stored uncompressed in
    00414      *  the memory, and they are only read directly by the code, never
    00415      *  send to the blitter. So do not send it to the blitter (which will
    00416      *  result in a data array in the format the blitter likes most), but
    00417      *  extract the data directly and store that as sprite.
    00418      * Ugly: yes. Other solution: no. Blame the original author or
    00419      *  something ;) The image should really have been a data-stream
    00420      *  (so type = 0xFF basically). */
    00421     uint num = sprite[ZOOM_LVL_NORMAL].width * sprite[ZOOM_LVL_NORMAL].height;
    00422
    00423     Sprite *s = (Sprite *)allocator(sizeof(*s) + num);
    00424     s->width  = sprite[ZOOM_LVL_NORMAL].width;
    00425     s->height = sprite[ZOOM_LVL_NORMAL].height;
    00426     s->x_offs = sprite[ZOOM_LVL_NORMAL].x_offs;
    00427     s->y_offs = sprite[ZOOM_LVL_NORMAL].y_offs;
    00428
    00429     SpriteLoader::CommonPixel *src = sprite[ZOOM_LVL_NORMAL].data;
    00430     byte *dest = s->data;
    00431     while (num-- > 0) {
    00432       *dest++ = src->m;
    00433       src++;
    00434     }
    00435
    00436     return s;
    00437   }
    00438
    00439   if (sprite_type == ST_NORMAL) {
    00440     if (!ResizeSprites(sprite, sprite_avail, file_slot, sc->id)) {
    00441       if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
    00442       return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
    00443     }
    00444   }
    00445   return BlitterFactory::GetCurrentBlitter()->Encode(sprite, allocator);
    00446 }
    00447
    00448
    00450 static std::map<uint32, size_t> _grf_sprite_offsets;
    00451
    00457 size_t GetGRFSpriteOffset(uint32 id)
    00458 {
    00459   return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id] : SIZE_MAX;
    00460 }
    00461
    00466 void ReadGRFSpriteOffsets(byte container_version)
    00467 {
    00468   _grf_sprite_offsets.clear();
    00469
    00470   if (container_version >= 2) {
    00471     /* Seek to sprite section of the GRF. */
    00472     size_t data_offset = FioReadDword();
    00473     size_t old_pos = FioGetPos();
    00474     FioSeekTo(data_offset, SEEK_CUR);
    00475
    00476     /* Loop over all sprite section entries and store the file
    00477      * offset for each newly encountered ID. */
    00478     uint32 id, prev_id = 0;
    00479     while ((id = FioReadDword()) != 0) {
    00480       if (id != prev_id) _grf_sprite_offsets[id] = FioGetPos() - 4;
    00481       prev_id = id;
    00482       FioSkipBytes(FioReadDword());
    00483     }
    00484
    00485     /* Continue processing the data section. */
    00486     FioSeekTo(old_pos, SEEK_SET);
    00487   }
    00488 }
    00489
    00490
    00499 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id, byte container_version)
    00500 {
    00501   size_t file_pos = FioGetPos();
    00502
    00503   /* Read sprite header. */
    00504   uint32 num = container_version >= 2 ? FioReadDword() : FioReadWord();
    00505   if (num == 0) return false;
    00506   byte grf_type = FioReadByte();
    00507
    00508   SpriteType type;
    00509   void *data = NULL;
    00510   if (grf_type == 0xFF) {
    00511     /* Some NewGRF files have "empty" pseudo-sprites which are 1
    00512      * byte long. Catch these so the sprites won't be displayed. */
    00513     if (num == 1) {
    00514       FioReadByte();
    00515       return false;
    00516     }
    00517     type = ST_RECOLOUR;
    00518     data = ReadRecolourSprite(file_slot, num);
    00519   } else if (container_version >= 2 && grf_type == 0xFD) {
    00520     if (num != 4) {
    00521       /* Invalid sprite section include, ignore. */
    00522       FioSkipBytes(num);
    00523       return false;
    00524     }
    00525     /* It is not an error if no sprite with the provided ID is found in the sprite section. */
    00526     file_pos = GetGRFSpriteOffset(FioReadDword());
    00527     type = ST_NORMAL;
    00528   } else {
    00529     FioSkipBytes(7);
    00530     type = SkipSpriteData(grf_type, num - 8) ? ST_NORMAL : ST_INVALID;
    00531     /* Inline sprites are not supported for container version >= 2. */
    00532     if (container_version >= 2) return false;
    00533   }
    00534
    00535   if (type == ST_INVALID) return false;
    00536
    00537   if (load_index >= MAX_SPRITES) {
    00538     usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
    00539   }
    00540
    00541   bool is_mapgen = IsMapgenSpriteID(load_index);
    00542
    00543   if (is_mapgen) {
    00544     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?");
    00545     type = ST_MAPGEN;
    00546   }
    00547
    00548   SpriteCache *sc = AllocateSpriteCache(load_index);
    00549   sc->file_slot = file_slot;
    00550   sc->file_pos = file_pos;
    00551   sc->ptr = data;
    00552   sc->lru = 0;
    00553   sc->id = file_sprite_id;
    00554   sc->type = type;
    00555   sc->warned = false;
    00556   sc->container_ver = container_version;
    00557
    00558   return true;
    00559 }
    00560
    00561
    00562 void DupSprite(SpriteID old_spr, SpriteID new_spr)
    00563 {
    00564   SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
    00565   SpriteCache *scold = GetSpriteCache(old_spr);
    00566
    00567   scnew->file_slot = scold->file_slot;
    00568   scnew->file_pos = scold->file_pos;
    00569   scnew->ptr = NULL;
    00570   scnew->id = scold->id;
    00571   scnew->type = scold->type;
    00572   scnew->warned = false;
    00573   scnew->container_ver = scold->container_ver;
    00574 }
    00575
    00582 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
    00583
    00584 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
    00585 assert_compile(sizeof(MemBlock) == sizeof(size_t));
    00586 /* make sure it's a power of two */
    00587 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
    00588
    00589 static inline MemBlock *NextBlock(MemBlock *block)
    00590 {
    00591   return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
    00592 }
    00593
    00594 static size_t GetSpriteCacheUsage()
    00595 {
    00596   size_t tot_size = 0;
    00597   MemBlock *s;
    00598
    00599   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00600     if (!(s->size & S_FREE_MASK)) tot_size += s->size;
    00601   }
    00602
    00603   return tot_size;
    00604 }
    00605
    00606
    00607 void IncreaseSpriteLRU()
    00608 {
    00609   /* Increase all LRU values */
    00610   if (_sprite_lru_counter > 16384) {
    00611     SpriteID i;
    00612
    00613     DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
    00614
    00615     for (i = 0; i != _spritecache_items; i++) {
    00616       SpriteCache *sc = GetSpriteCache(i);
    00617       if (sc->ptr != NULL) {
    00618         if (sc->lru >= 0) {
    00619           sc->lru = -1;
    00620         } else if (sc->lru != -32768) {
    00621           sc->lru--;
    00622         }
    00623       }
    00624     }
    00625     _sprite_lru_counter = 0;
    00626   }
    00627
    00628   /* Compact sprite cache every now and then. */
    00629   if (++_compact_cache_counter >= 740) {
    00630     CompactSpriteCache();
    00631     _compact_cache_counter = 0;
    00632   }
    00633 }
    00634
    00639 static void CompactSpriteCache()
    00640 {
    00641   MemBlock *s;
    00642
    00643   DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00644
    00645   for (s = _spritecache_ptr; s->size != 0;) {
    00646     if (s->size & S_FREE_MASK) {
    00647       MemBlock *next = NextBlock(s);
    00648       MemBlock temp;
    00649       SpriteID i;
    00650
    00651       /* Since free blocks are automatically coalesced, this should hold true. */
    00652       assert(!(next->size & S_FREE_MASK));
    00653
    00654       /* If the next block is the sentinel block, we can safely return */
    00655       if (next->size == 0) break;
    00656
    00657       /* Locate the sprite belonging to the next pointer. */
    00658       for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
    00659         assert(i != _spritecache_items);
    00660       }
    00661
    00662       GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
    00663       /* Swap this and the next block */
    00664       temp = *s;
    00665       memmove(s, next, next->size);
    00666       s = NextBlock(s);
    00667       *s = temp;
    00668
    00669       /* Coalesce free blocks */
    00670       while (NextBlock(s)->size & S_FREE_MASK) {
    00671         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00672       }
    00673     } else {
    00674       s = NextBlock(s);
    00675     }
    00676   }
    00677 }
    00678
    00683 static void DeleteEntryFromSpriteCache(uint item)
    00684 {
    00685   /* Mark the block as free (the block must be in use) */
    00686   MemBlock *s = (MemBlock*)GetSpriteCache(item)->ptr - 1;
    00687   assert(!(s->size & S_FREE_MASK));
    00688   s->size |= S_FREE_MASK;
    00689   GetSpriteCache(item)->ptr = NULL;
    00690
    00691   /* And coalesce adjacent free blocks */
    00692   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00693     if (s->size & S_FREE_MASK) {
    00694       while (NextBlock(s)->size & S_FREE_MASK) {
    00695         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00696       }
    00697     }
    00698   }
    00699 }
    00700
    00701 static void DeleteEntryFromSpriteCache()
    00702 {
    00703   uint best = UINT_MAX;
    00704   int cur_lru;
    00705
    00706   DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00707
    00708   cur_lru = 0xffff;
    00709   for (SpriteID i = 0; i != _spritecache_items; i++) {
    00710     SpriteCache *sc = GetSpriteCache(i);
    00711     if (sc->type != ST_RECOLOUR && sc->ptr != NULL && sc->lru < cur_lru) {
    00712       cur_lru = sc->lru;
    00713       best = i;
    00714     }
    00715   }
    00716
    00717   /* Display an error message and die, in case we found no sprite at all.
    00718    * This shouldn't really happen, unless all sprites are locked. */
    00719   if (best == UINT_MAX) error("Out of sprite memory");
    00720
    00721   DeleteEntryFromSpriteCache(best);
    00722 }
    00723
    00724 static void *AllocSprite(size_t mem_req)
    00725 {
    00726   mem_req += sizeof(MemBlock);
    00727
    00728   /* Align this to correct boundary. This also makes sure at least one
    00729    * bit is not used, so we can use it for other things. */
    00730   mem_req = Align(mem_req, S_FREE_MASK + 1);
    00731
    00732   for (;;) {
    00733     MemBlock *s;
    00734
    00735     for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00736       if (s->size & S_FREE_MASK) {
    00737         size_t cur_size = s->size & ~S_FREE_MASK;
    00738
    00739         /* Is the block exactly the size we need or
    00740          * big enough for an additional free block? */
    00741         if (cur_size == mem_req ||
    00742             cur_size >= mem_req + sizeof(MemBlock)) {
    00743           /* Set size and in use */
    00744           s->size = mem_req;
    00745
    00746           /* Do we need to inject a free block too? */
    00747           if (cur_size != mem_req) {
    00748             NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
    00749           }
    00750
    00751           return s->data;
    00752         }
    00753       }
    00754     }
    00755
    00756     /* Reached sentinel, but no block found yet. Delete some old entry. */
    00757     DeleteEntryFromSpriteCache();
    00758   }
    00759 }
    00760
    00770 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
    00771 {
    00772   static const char * const sprite_types[] = {
    00773     "normal",        // ST_NORMAL
    00774     "map generator", // ST_MAPGEN
    00775     "character",     // ST_FONT
    00776     "recolour",      // ST_RECOLOUR
    00777   };
    00778
    00779   SpriteType available = sc->type;
    00780   if (requested == ST_FONT && available == ST_NORMAL) {
    00781     if (sc->ptr == NULL) sc->type = ST_FONT;
    00782     return GetRawSprite(sprite, sc->type, allocator);
    00783   }
    00784
    00785   byte warning_level = sc->warned ? 6 : 0;
    00786   sc->warned = true;
    00787   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]);
    00788
    00789   switch (requested) {
    00790     case ST_NORMAL:
    00791       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?");
    00792       /* FALL THROUGH */
    00793     case ST_FONT:
    00794       return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
    00795     case ST_RECOLOUR:
    00796       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?");
    00797       return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
    00798     case ST_MAPGEN:
    00799       /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
    00800        * (the only case the check fails is when these sprites weren't even loaded...) */
    00801     default:
    00802       NOT_REACHED();
    00803   }
    00804 }
    00805
    00814 void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
    00815 {
    00816   assert(type != ST_MAPGEN || IsMapgenSpriteID(sprite));
    00817   assert(type < ST_INVALID);
    00818
    00819   if (!SpriteExists(sprite)) {
    00820     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
    00821
    00822     /* SPR_IMG_QUERY is a BIG FAT RED ? */
    00823     sprite = SPR_IMG_QUERY;
    00824   }
    00825
    00826   SpriteCache *sc = GetSpriteCache(sprite);
    00827
    00828   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
    00829
    00830   if (allocator == NULL) {
    00831     /* Load sprite into/from spritecache */
    00832
    00833     /* Update LRU */
    00834     sc->lru = ++_sprite_lru_counter;
    00835
    00836     /* Load the sprite, if it is not loaded, yet */
    00837     if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
    00838
    00839     return sc->ptr;
    00840   } else {
    00841     /* Do not use the spritecache, but a different allocator. */
    00842     return ReadSprite(sc, sprite, type, allocator);
    00843   }
    00844 }
    00845
    00846
    00847 static void GfxInitSpriteCache()
    00848 {
    00849   /* initialize sprite cache heap */
    00850   int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
    00851   uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
    00852
    00853   /* Remember 'target_size' from the previous allocation attempt, so we do not try to reach the target_size multiple times in case of failure. */
    00854   static uint last_alloc_attempt = 0;
    00855
    00856   if (_spritecache_ptr == NULL || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
    00857     delete[] reinterpret_cast<byte *>(_spritecache_ptr);
    00858
    00859     last_alloc_attempt = target_size;
    00860     _allocated_sprite_cache_size = target_size;
    00861
    00862     do {
    00863       try {
    00864         /* Try to allocate 50% more to make sure we do not allocate almost all available. */
    00865         _spritecache_ptr = reinterpret_cast<MemBlock *>(new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
    00866       } catch (std::bad_alloc &) {
    00867         _spritecache_ptr = NULL;
    00868       }
    00869
    00870       if (_spritecache_ptr != NULL) {
    00871         /* Allocation succeeded, but we wanted less. */
    00872         delete[] reinterpret_cast<byte *>(_spritecache_ptr);
    00873         _spritecache_ptr = reinterpret_cast<MemBlock *>(new byte[_allocated_sprite_cache_size]);
    00874       } else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
    00875         usererror("Cannot allocate spritecache");
    00876       } else {
    00877         /* Try again to allocate half. */
    00878         _allocated_sprite_cache_size >>= 1;
    00879       }
    00880     } while (_spritecache_ptr == NULL);
    00881
    00882     if (_allocated_sprite_cache_size != target_size) {
    00883       DEBUG(misc, 0, "Not enough memory to allocate %d MiB of spritecache. Spritecache was reduced to %d MiB.", target_size / 1024 / 1024, _allocated_sprite_cache_size / 1024 / 1024);
    00884
    00885       ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
    00886       msg.SetDParam(0, target_size);
    00887       msg.SetDParam(1, _allocated_sprite_cache_size);
    00888       ScheduleErrorMessage(msg);
    00889     }
    00890   }
    00891
    00892   /* A big free block */
    00893   _spritecache_ptr->size = (_allocated_sprite_cache_size - sizeof(MemBlock)) | S_FREE_MASK;
    00894   /* Sentinel block (identified by size == 0) */
    00895   NextBlock(_spritecache_ptr)->size = 0;
    00896 }
    00897
    00898 void GfxInitSpriteMem()
    00899 {
    00900   GfxInitSpriteCache();
    00901
    00902   /* Reset the spritecache 'pool' */
    00903   free(_spritecache);
    00904   _spritecache_items = 0;
    00905   _spritecache = NULL;
    00906
    00907   _compact_cache_counter = 0;
    00908 }
    00909
    00914 void GfxClearSpriteCache()
    00915 {
    00916   /* Clear sprite ptr for all cached items */
    00917   for (uint i = 0; i != _spritecache_items; i++) {
    00918     SpriteCache *sc = GetSpriteCache(i);
    00919     if (sc->type != ST_RECOLOUR && sc->ptr != NULL) DeleteEntryFromSpriteCache(i);
    00920   }
    00921 }
    00922
    00923 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer[ZOOM_LVL_COUNT];