OpenTTD
spritecache.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "fileio_func.h"
14 #include "spriteloader/grf.hpp"
15 #include "gfx_func.h"
16 #include "error.h"
17 #include "zoom_func.h"
18 #include "settings_type.h"
19 #include "blitter/factory.hpp"
20 #include "core/math_func.hpp"
21 #include "core/mem_func.hpp"
22 
23 #include "table/sprites.h"
24 #include "table/strings.h"
25 #include "table/palette_convert.h"
26 
27 #include "safeguards.h"
28 
29 /* Default of 4MB spritecache */
30 uint _sprite_cache_size = 4;
31 
32 struct SpriteCache {
33  void *ptr;
34  size_t file_pos;
35  uint32 id;
36  uint16 file_slot;
37  int16 lru;
39  bool warned;
41 };
42 
43 
44 static uint _spritecache_items = 0;
45 static SpriteCache *_spritecache = nullptr;
46 
47 
48 static inline SpriteCache *GetSpriteCache(uint index)
49 {
50  return &_spritecache[index];
51 }
52 
53 static inline bool IsMapgenSpriteID(SpriteID sprite)
54 {
55  return IsInsideMM(sprite, 4845, 4882);
56 }
57 
58 static SpriteCache *AllocateSpriteCache(uint index)
59 {
60  if (index >= _spritecache_items) {
61  /* Add another 1024 items to the 'pool' */
62  uint items = Align(index + 1, 1024);
63 
64  DEBUG(sprite, 4, "Increasing sprite cache to %u items (" PRINTF_SIZE " bytes)", items, items * sizeof(*_spritecache));
65 
66  _spritecache = ReallocT(_spritecache, items);
67 
68  /* Reset the new items and update the count */
69  memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
70  _spritecache_items = items;
71  }
72 
73  return GetSpriteCache(index);
74 }
75 
76 
77 struct MemBlock {
78  size_t size;
79  byte data[];
80 };
81 
82 static uint _sprite_lru_counter;
83 static MemBlock *_spritecache_ptr;
84 static uint _allocated_sprite_cache_size = 0;
85 static int _compact_cache_counter;
86 
87 static void CompactSpriteCache();
88 static void *AllocSprite(size_t mem_req);
89 
96 bool SkipSpriteData(byte type, uint16 num)
97 {
98  if (type & 2) {
99  FioSkipBytes(num);
100  } else {
101  while (num > 0) {
102  int8 i = FioReadByte();
103  if (i >= 0) {
104  int size = (i == 0) ? 0x80 : i;
105  if (size > num) return false;
106  num -= size;
107  FioSkipBytes(size);
108  } else {
109  i = -(i >> 3);
110  num -= i;
111  FioReadByte();
112  }
113  }
114  }
115  return true;
116 }
117 
118 /* Check if the given Sprite ID exists */
119 bool SpriteExists(SpriteID id)
120 {
121  if (id >= _spritecache_items) return false;
122 
123  /* Special case for Sprite ID zero -- its position is also 0... */
124  if (id == 0) return true;
125  return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
126 }
127 
134 {
135  if (!SpriteExists(sprite)) return ST_INVALID;
136  return GetSpriteCache(sprite)->type;
137 }
138 
145 {
146  if (!SpriteExists(sprite)) return 0;
147  return GetSpriteCache(sprite)->file_slot;
148 }
149 
157 uint GetSpriteCountForSlot(uint file_slot, SpriteID begin, SpriteID end)
158 {
159  uint count = 0;
160  for (SpriteID i = begin; i != end; i++) {
161  if (SpriteExists(i)) {
162  SpriteCache *sc = GetSpriteCache(i);
163  if (sc->file_slot == file_slot) count++;
164  }
165  }
166  return count;
167 }
168 
178 {
179  return _spritecache_items;
180 }
181 
182 static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt)
183 {
184  uint8 scaled_1 = ScaleByZoom(1, (ZoomLevel)(src - tgt));
185 
186  /* Check for possible memory overflow. */
187  if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
188 
189  sprite[tgt].width = sprite[src].width * scaled_1;
190  sprite[tgt].height = sprite[src].height * scaled_1;
191  sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
192  sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
193 
194  sprite[tgt].AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
195 
196  SpriteLoader::CommonPixel *dst = sprite[tgt].data;
197  for (int y = 0; y < sprite[tgt].height; y++) {
198  const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
199  for (int x = 0; x < sprite[tgt].width; x++) {
200  *dst = src_ln[x / scaled_1];
201  dst++;
202  }
203  }
204 
205  return true;
206 }
207 
208 static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom)
209 {
210  /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
211  sprite[zoom].width = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width, zoom);
212  sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom);
213  sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom);
214  sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom);
215 
216  sprite[zoom].AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
217 
218  SpriteLoader::CommonPixel *dst = sprite[zoom].data;
219  const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
220  const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
221 
222  for (uint y = 0; y < sprite[zoom].height; y++) {
223  const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width;
224  assert(src_ln <= src_end);
225  for (uint x = 0; x < sprite[zoom].width; x++) {
226  assert(src < src_ln);
227  if (src + 1 != src_ln && (src + 1)->a != 0) {
228  *dst = *(src + 1);
229  } else {
230  *dst = *src;
231  }
232  dst++;
233  src += 2;
234  }
235  src = src_ln + sprite[zoom - 1].width;
236  }
237 }
238 
239 static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
240 {
241  uint width = sprite->width + pad_left + pad_right;
242  uint height = sprite->height + pad_top + pad_bottom;
243 
244  if (width > UINT16_MAX || height > UINT16_MAX) return false;
245 
246  /* Copy source data and reallocate sprite memory. */
247  SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
248  MemCpyT(src_data, sprite->data, sprite->width * sprite->height);
249  sprite->AllocateData(zoom, width * height);
250 
251  /* Copy with padding to destination. */
252  SpriteLoader::CommonPixel *src = src_data;
253  SpriteLoader::CommonPixel *data = sprite->data;
254  for (uint y = 0; y < height; y++) {
255  if (y < pad_top || pad_bottom + y >= height) {
256  /* Top/bottom padding. */
257  MemSetT(data, 0, width);
258  data += width;
259  } else {
260  if (pad_left > 0) {
261  /* Pad left. */
262  MemSetT(data, 0, pad_left);
263  data += pad_left;
264  }
265 
266  /* Copy pixels. */
267  MemCpyT(data, src, sprite->width);
268  src += sprite->width;
269  data += sprite->width;
270 
271  if (pad_right > 0) {
272  /* Pad right. */
273  MemSetT(data, 0, pad_right);
274  data += pad_right;
275  }
276  }
277  }
278  free(src_data);
279 
280  /* Update sprite size. */
281  sprite->width = width;
282  sprite->height = height;
283  sprite->x_offs -= pad_left;
284  sprite->y_offs -= pad_top;
285 
286  return true;
287 }
288 
289 static bool PadSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail)
290 {
291  /* Get minimum top left corner coordinates. */
292  int min_xoffs = INT32_MAX;
293  int min_yoffs = INT32_MAX;
294  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
295  if (HasBit(sprite_avail, zoom)) {
296  min_xoffs = min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
297  min_yoffs = min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
298  }
299  }
300 
301  /* Get maximum dimensions taking necessary padding at the top left into account. */
302  int max_width = INT32_MIN;
303  int max_height = INT32_MIN;
304  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
305  if (HasBit(sprite_avail, zoom)) {
306  max_width = max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
307  max_height = max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
308  }
309  }
310 
311  /* Pad sprites where needed. */
312  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
313  if (HasBit(sprite_avail, zoom)) {
314  /* Scaling the sprite dimensions in the blitter is done with rounding up,
315  * so a negative padding here is not an error. */
316  int pad_left = max(0, sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom));
317  int pad_top = max(0, sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom));
318  int pad_right = max(0, UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
319  int pad_bottom = max(0, UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
320 
321  if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
322  if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
323  }
324  }
325  }
326 
327  return true;
328 }
329 
330 static bool ResizeSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, uint32 file_slot, uint32 file_pos)
331 {
332  /* Create a fully zoomed image if it does not exist */
333  ZoomLevel first_avail = static_cast<ZoomLevel>(FIND_FIRST_BIT(sprite_avail));
334  if (first_avail != ZOOM_LVL_NORMAL) {
335  if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_NORMAL)) return false;
336  SetBit(sprite_avail, ZOOM_LVL_NORMAL);
337  }
338 
339  /* Pad sprites to make sizes match. */
340  if (!PadSprites(sprite, sprite_avail)) return false;
341 
342  /* Create other missing zoom levels */
343  for (ZoomLevel zoom = ZOOM_LVL_OUT_2X; zoom != ZOOM_LVL_END; zoom++) {
344  if (HasBit(sprite_avail, zoom)) {
345  /* Check that size and offsets match the fully zoomed image. */
346  assert(sprite[zoom].width == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width, zoom));
347  assert(sprite[zoom].height == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom));
348  assert(sprite[zoom].x_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom));
349  assert(sprite[zoom].y_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom));
350  }
351 
352  /* Zoom level is not available, or unusable, so create it */
353  if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
354  }
355 
356  return true;
357 }
358 
365 static void *ReadRecolourSprite(uint16 file_slot, uint num)
366 {
367  /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
368  * number of recolour sprites that are 17 bytes that only exist in DOS
369  * GRFs which are the same as 257 byte recolour sprites, but with the last
370  * 240 bytes zeroed. */
371  static const uint RECOLOUR_SPRITE_SIZE = 257;
372  byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
373 
374  if (_palette_remap_grf[file_slot]) {
375  byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
376 
377  /* Only a few recolour sprites are less than 257 bytes */
378  if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
379  FioReadBlock(dest_tmp, num);
380 
381  /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
382  for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
383  dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
384  }
385  } else {
386  FioReadBlock(dest, num);
387  }
388 
389  return dest;
390 }
391 
400 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
401 {
402  uint8 file_slot = sc->file_slot;
403  size_t file_pos = sc->file_pos;
404 
405  assert(sprite_type != ST_RECOLOUR);
406  assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
407  assert(sc->type == sprite_type);
408 
409  DEBUG(sprite, 9, "Load sprite %d", id);
410 
412  uint8 sprite_avail = 0;
413  sprite[ZOOM_LVL_NORMAL].type = sprite_type;
414 
415  SpriteLoaderGrf sprite_loader(sc->container_ver);
416  if (sprite_type != ST_MAPGEN && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 32) {
417  /* Try for 32bpp sprites first. */
418  sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, true);
419  }
420  if (sprite_avail == 0) {
421  sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, false);
422  }
423 
424  if (sprite_avail == 0) {
425  if (sprite_type == ST_MAPGEN) return nullptr;
426  if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
427  return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
428  }
429 
430  if (sprite_type == ST_MAPGEN) {
431  /* Ugly hack to work around the problem that the old landscape
432  * generator assumes that those sprites are stored uncompressed in
433  * the memory, and they are only read directly by the code, never
434  * send to the blitter. So do not send it to the blitter (which will
435  * result in a data array in the format the blitter likes most), but
436  * extract the data directly and store that as sprite.
437  * Ugly: yes. Other solution: no. Blame the original author or
438  * something ;) The image should really have been a data-stream
439  * (so type = 0xFF basically). */
440  uint num = sprite[ZOOM_LVL_NORMAL].width * sprite[ZOOM_LVL_NORMAL].height;
441 
442  Sprite *s = (Sprite *)allocator(sizeof(*s) + num);
443  s->width = sprite[ZOOM_LVL_NORMAL].width;
444  s->height = sprite[ZOOM_LVL_NORMAL].height;
445  s->x_offs = sprite[ZOOM_LVL_NORMAL].x_offs;
446  s->y_offs = sprite[ZOOM_LVL_NORMAL].y_offs;
447 
449  byte *dest = s->data;
450  while (num-- > 0) {
451  *dest++ = src->m;
452  src++;
453  }
454 
455  return s;
456  }
457 
458  if (!ResizeSprites(sprite, sprite_avail, file_slot, sc->id)) {
459  if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
460  return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
461  }
462 
463  if (sprite->type == ST_FONT && ZOOM_LVL_FONT != ZOOM_LVL_NORMAL) {
464  /* Make ZOOM_LVL_NORMAL be ZOOM_LVL_FONT */
465  sprite[ZOOM_LVL_NORMAL].width = sprite[ZOOM_LVL_FONT].width;
466  sprite[ZOOM_LVL_NORMAL].height = sprite[ZOOM_LVL_FONT].height;
467  sprite[ZOOM_LVL_NORMAL].x_offs = sprite[ZOOM_LVL_FONT].x_offs;
468  sprite[ZOOM_LVL_NORMAL].y_offs = sprite[ZOOM_LVL_FONT].y_offs;
469  sprite[ZOOM_LVL_NORMAL].data = sprite[ZOOM_LVL_FONT].data;
470  }
471 
472  return BlitterFactory::GetCurrentBlitter()->Encode(sprite, allocator);
473 }
474 
475 
477 static std::map<uint32, size_t> _grf_sprite_offsets;
478 
484 size_t GetGRFSpriteOffset(uint32 id)
485 {
486  return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id] : SIZE_MAX;
487 }
488 
493 void ReadGRFSpriteOffsets(byte container_version)
494 {
495  _grf_sprite_offsets.clear();
496 
497  if (container_version >= 2) {
498  /* Seek to sprite section of the GRF. */
499  size_t data_offset = FioReadDword();
500  size_t old_pos = FioGetPos();
501  FioSeekTo(data_offset, SEEK_CUR);
502 
503  /* Loop over all sprite section entries and store the file
504  * offset for each newly encountered ID. */
505  uint32 id, prev_id = 0;
506  while ((id = FioReadDword()) != 0) {
507  if (id != prev_id) _grf_sprite_offsets[id] = FioGetPos() - 4;
508  prev_id = id;
510  }
511 
512  /* Continue processing the data section. */
513  FioSeekTo(old_pos, SEEK_SET);
514  }
515 }
516 
517 
526 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id, byte container_version)
527 {
528  size_t file_pos = FioGetPos();
529 
530  /* Read sprite header. */
531  uint32 num = container_version >= 2 ? FioReadDword() : FioReadWord();
532  if (num == 0) return false;
533  byte grf_type = FioReadByte();
534 
536  void *data = nullptr;
537  if (grf_type == 0xFF) {
538  /* Some NewGRF files have "empty" pseudo-sprites which are 1
539  * byte long. Catch these so the sprites won't be displayed. */
540  if (num == 1) {
541  FioReadByte();
542  return false;
543  }
544  type = ST_RECOLOUR;
545  data = ReadRecolourSprite(file_slot, num);
546  } else if (container_version >= 2 && grf_type == 0xFD) {
547  if (num != 4) {
548  /* Invalid sprite section include, ignore. */
549  FioSkipBytes(num);
550  return false;
551  }
552  /* It is not an error if no sprite with the provided ID is found in the sprite section. */
553  file_pos = GetGRFSpriteOffset(FioReadDword());
554  type = ST_NORMAL;
555  } else {
556  FioSkipBytes(7);
557  type = SkipSpriteData(grf_type, num - 8) ? ST_NORMAL : ST_INVALID;
558  /* Inline sprites are not supported for container version >= 2. */
559  if (container_version >= 2) return false;
560  }
561 
562  if (type == ST_INVALID) return false;
563 
564  if (load_index >= MAX_SPRITES) {
565  usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
566  }
567 
568  bool is_mapgen = IsMapgenSpriteID(load_index);
569 
570  if (is_mapgen) {
571  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?");
572  type = ST_MAPGEN;
573  }
574 
575  SpriteCache *sc = AllocateSpriteCache(load_index);
576  sc->file_slot = file_slot;
577  sc->file_pos = file_pos;
578  sc->ptr = data;
579  sc->lru = 0;
580  sc->id = file_sprite_id;
581  sc->type = type;
582  sc->warned = false;
583  sc->container_ver = container_version;
584 
585  return true;
586 }
587 
588 
589 void DupSprite(SpriteID old_spr, SpriteID new_spr)
590 {
591  SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
592  SpriteCache *scold = GetSpriteCache(old_spr);
593 
594  scnew->file_slot = scold->file_slot;
595  scnew->file_pos = scold->file_pos;
596  scnew->ptr = nullptr;
597  scnew->id = scold->id;
598  scnew->type = scold->type;
599  scnew->warned = false;
600  scnew->container_ver = scold->container_ver;
601 }
602 
609 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
610 
611 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
612 assert_compile(sizeof(MemBlock) == sizeof(size_t));
613 /* make sure it's a power of two */
614 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
615 
616 static inline MemBlock *NextBlock(MemBlock *block)
617 {
618  return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
619 }
620 
621 static size_t GetSpriteCacheUsage()
622 {
623  size_t tot_size = 0;
624  MemBlock *s;
625 
626  for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
627  if (!(s->size & S_FREE_MASK)) tot_size += s->size;
628  }
629 
630  return tot_size;
631 }
632 
633 
634 void IncreaseSpriteLRU()
635 {
636  /* Increase all LRU values */
637  if (_sprite_lru_counter > 16384) {
638  SpriteID i;
639 
640  DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
641 
642  for (i = 0; i != _spritecache_items; i++) {
643  SpriteCache *sc = GetSpriteCache(i);
644  if (sc->ptr != nullptr) {
645  if (sc->lru >= 0) {
646  sc->lru = -1;
647  } else if (sc->lru != -32768) {
648  sc->lru--;
649  }
650  }
651  }
652  _sprite_lru_counter = 0;
653  }
654 
655  /* Compact sprite cache every now and then. */
656  if (++_compact_cache_counter >= 740) {
658  _compact_cache_counter = 0;
659  }
660 }
661 
666 static void CompactSpriteCache()
667 {
668  MemBlock *s;
669 
670  DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
671 
672  for (s = _spritecache_ptr; s->size != 0;) {
673  if (s->size & S_FREE_MASK) {
674  MemBlock *next = NextBlock(s);
675  MemBlock temp;
676  SpriteID i;
677 
678  /* Since free blocks are automatically coalesced, this should hold true. */
679  assert(!(next->size & S_FREE_MASK));
680 
681  /* If the next block is the sentinel block, we can safely return */
682  if (next->size == 0) break;
683 
684  /* Locate the sprite belonging to the next pointer. */
685  for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
686  assert(i != _spritecache_items);
687  }
688 
689  GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
690  /* Swap this and the next block */
691  temp = *s;
692  memmove(s, next, next->size);
693  s = NextBlock(s);
694  *s = temp;
695 
696  /* Coalesce free blocks */
697  while (NextBlock(s)->size & S_FREE_MASK) {
698  s->size += NextBlock(s)->size & ~S_FREE_MASK;
699  }
700  } else {
701  s = NextBlock(s);
702  }
703  }
704 }
705 
710 static void DeleteEntryFromSpriteCache(uint item)
711 {
712  /* Mark the block as free (the block must be in use) */
713  MemBlock *s = (MemBlock*)GetSpriteCache(item)->ptr - 1;
714  assert(!(s->size & S_FREE_MASK));
715  s->size |= S_FREE_MASK;
716  GetSpriteCache(item)->ptr = nullptr;
717 
718  /* And coalesce adjacent free blocks */
719  for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
720  if (s->size & S_FREE_MASK) {
721  while (NextBlock(s)->size & S_FREE_MASK) {
722  s->size += NextBlock(s)->size & ~S_FREE_MASK;
723  }
724  }
725  }
726 }
727 
728 static void DeleteEntryFromSpriteCache()
729 {
730  uint best = UINT_MAX;
731  int cur_lru;
732 
733  DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
734 
735  cur_lru = 0xffff;
736  for (SpriteID i = 0; i != _spritecache_items; i++) {
737  SpriteCache *sc = GetSpriteCache(i);
738  if (sc->type != ST_RECOLOUR && sc->ptr != nullptr && sc->lru < cur_lru) {
739  cur_lru = sc->lru;
740  best = i;
741  }
742  }
743 
744  /* Display an error message and die, in case we found no sprite at all.
745  * This shouldn't really happen, unless all sprites are locked. */
746  if (best == UINT_MAX) error("Out of sprite memory");
747 
749 }
750 
751 static void *AllocSprite(size_t mem_req)
752 {
753  mem_req += sizeof(MemBlock);
754 
755  /* Align this to correct boundary. This also makes sure at least one
756  * bit is not used, so we can use it for other things. */
757  mem_req = Align(mem_req, S_FREE_MASK + 1);
758 
759  for (;;) {
760  MemBlock *s;
761 
762  for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
763  if (s->size & S_FREE_MASK) {
764  size_t cur_size = s->size & ~S_FREE_MASK;
765 
766  /* Is the block exactly the size we need or
767  * big enough for an additional free block? */
768  if (cur_size == mem_req ||
769  cur_size >= mem_req + sizeof(MemBlock)) {
770  /* Set size and in use */
771  s->size = mem_req;
772 
773  /* Do we need to inject a free block too? */
774  if (cur_size != mem_req) {
775  NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
776  }
777 
778  return s->data;
779  }
780  }
781  }
782 
783  /* Reached sentinel, but no block found yet. Delete some old entry. */
785  }
786 }
787 
797 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
798 {
799  static const char * const sprite_types[] = {
800  "normal", // ST_NORMAL
801  "map generator", // ST_MAPGEN
802  "character", // ST_FONT
803  "recolour", // ST_RECOLOUR
804  };
805 
806  SpriteType available = sc->type;
807  if (requested == ST_FONT && available == ST_NORMAL) {
808  if (sc->ptr == nullptr) sc->type = ST_FONT;
809  return GetRawSprite(sprite, sc->type, allocator);
810  }
811 
812  byte warning_level = sc->warned ? 6 : 0;
813  sc->warned = true;
814  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]);
815 
816  switch (requested) {
817  case ST_NORMAL:
818  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?");
819  FALLTHROUGH;
820  case ST_FONT:
821  return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
822  case ST_RECOLOUR:
823  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?");
824  return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
825  case ST_MAPGEN:
826  /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
827  * (the only case the check fails is when these sprites weren't even loaded...) */
828  default:
829  NOT_REACHED();
830  }
831 }
832 
841 void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
842 {
843  assert(type != ST_MAPGEN || IsMapgenSpriteID(sprite));
844  assert(type < ST_INVALID);
845 
846  if (!SpriteExists(sprite)) {
847  DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
848 
849  /* SPR_IMG_QUERY is a BIG FAT RED ? */
850  sprite = SPR_IMG_QUERY;
851  }
852 
853  SpriteCache *sc = GetSpriteCache(sprite);
854 
855  if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
856 
857  if (allocator == nullptr) {
858  /* Load sprite into/from spritecache */
859 
860  /* Update LRU */
861  sc->lru = ++_sprite_lru_counter;
862 
863  /* Load the sprite, if it is not loaded, yet */
864  if (sc->ptr == nullptr) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
865 
866  return sc->ptr;
867  } else {
868  /* Do not use the spritecache, but a different allocator. */
869  return ReadSprite(sc, sprite, type, allocator);
870  }
871 }
872 
873 
874 static void GfxInitSpriteCache()
875 {
876  /* initialize sprite cache heap */
878  uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
879 
880  /* Remember 'target_size' from the previous allocation attempt, so we do not try to reach the target_size multiple times in case of failure. */
881  static uint last_alloc_attempt = 0;
882 
883  if (_spritecache_ptr == nullptr || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
884  delete[] reinterpret_cast<byte *>(_spritecache_ptr);
885 
886  last_alloc_attempt = target_size;
887  _allocated_sprite_cache_size = target_size;
888 
889  do {
890  try {
891  /* Try to allocate 50% more to make sure we do not allocate almost all available. */
892  _spritecache_ptr = reinterpret_cast<MemBlock *>(new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
893  } catch (std::bad_alloc &) {
894  _spritecache_ptr = nullptr;
895  }
896 
897  if (_spritecache_ptr != nullptr) {
898  /* Allocation succeeded, but we wanted less. */
899  delete[] reinterpret_cast<byte *>(_spritecache_ptr);
900  _spritecache_ptr = reinterpret_cast<MemBlock *>(new byte[_allocated_sprite_cache_size]);
901  } else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
902  usererror("Cannot allocate spritecache");
903  } else {
904  /* Try again to allocate half. */
905  _allocated_sprite_cache_size >>= 1;
906  }
907  } while (_spritecache_ptr == nullptr);
908 
909  if (_allocated_sprite_cache_size != target_size) {
910  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);
911 
912  ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
913  msg.SetDParam(0, target_size);
914  msg.SetDParam(1, _allocated_sprite_cache_size);
916  }
917  }
918 
919  /* A big free block */
920  _spritecache_ptr->size = (_allocated_sprite_cache_size - sizeof(MemBlock)) | S_FREE_MASK;
921  /* Sentinel block (identified by size == 0) */
922  NextBlock(_spritecache_ptr)->size = 0;
923 }
924 
925 void GfxInitSpriteMem()
926 {
927  GfxInitSpriteCache();
928 
929  /* Reset the spritecache 'pool' */
930  free(_spritecache);
931  _spritecache_items = 0;
932  _spritecache = nullptr;
933 
934  _compact_cache_counter = 0;
935 }
936 
942 {
943  /* Clear sprite ptr for all cached items */
944  for (uint i = 0; i != _spritecache_items; i++) {
945  SpriteCache *sc = GetSpriteCache(i);
946  if (sc->type != ST_RECOLOUR && sc->ptr != nullptr) DeleteEntryFromSpriteCache(i);
947  }
948 }
949 
static const size_t S_FREE_MASK
S_FREE_MASK is used to mask-out lower bits of MemBlock::size If they are non-zero, the block is free.
Pseudosprite or other unusable sprite, used only internally.
Definition: gfx_type.h:302
uint GetOriginFileSlot(SpriteID sprite)
Get the (FIOS) file slot of a given sprite.
static ReusableBuffer< SpriteLoader::CommonPixel > buffer[ZOOM_LVL_COUNT]
Allocated memory to pass sprite data around.
static void * HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
Handles the case when a sprite of different type is requested than is present in the SpriteCache...
Zoomed 2 times out.
Definition: zoom_type.h:25
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
static int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:36
Data structure describing a sprite.
Definition: spritecache.h:18
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: fileio.cpp:166
End for iteration.
Definition: zoom_type.h:30
uint8 LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Definition: grf.cpp:328
void SetDParam(uint n, uint64 v)
Set a error string parameter.
Definition: error_gui.cpp:152
static void DeleteEntryFromSpriteCache(uint item)
Delete a single entry from the sprite cache.
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:24
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Functions for Standard In/Out file operations.
Maximum number of sprites that can be loaded at a given time.
Definition: sprites.h:1538
Number of zoom levels.
Definition: zoom_type.h:32
const byte _palmap_w2d[]
Converting from the Windows palette to the DOS palette.
virtual Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)=0
Convert a sprite from the loader to our own format.
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:134
static void * ReadRecolourSprite(uint16 file_slot, uint num)
Load a recolour sprite into memory.
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
byte FioReadByte()
Read a byte from the file.
Definition: fileio.cpp:133
bool warned
True iff the user has been warned about incorrect use of this sprite.
Definition: spritecache.cpp:39
SpriteType GetSpriteType(SpriteID sprite)
Get the sprite type of a given sprite.
size_t GetGRFSpriteOffset(uint32 id)
Get the file offset for a specific sprite in the sprite section of a GRF.
Definition of a common pixel in OpenTTD&#39;s realm.
SpriteType type
The sprite type.
Base for reading sprites from (New)GRFs.
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
Begin for iteration.
Definition: zoom_type.h:23
void ReadGRFSpriteOffsets(byte container_version)
Parse the sprite section of GRFs.
static void * ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
Read a sprite from disk.
Special sprite for the map generator.
Definition: gfx_type.h:299
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:22
Functions related to errors.
A sprite used for fonts.
Definition: gfx_type.h:300
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:97
#define FIND_FIRST_BIT(x)
Returns the first non-zero bit in a 6-bit value (from right).
Functions related to the gfx engine.
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:94
A number of safeguards to prevent using unsafe methods.
void FioSeekTo(size_t pos, int mode)
Seek in the current file.
Definition: fileio.cpp:88
int16 x_offs
The x-offset of where the sprite will be drawn.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
Definition: alloc_func.hpp:113
void ScheduleErrorMessage(const ErrorMessageData &data)
Schedule an error.
Definition: error_gui.cpp:444
SpriteLoader::CommonPixel * data
The sprite itself.
byte data[]
Sprite data.
Definition: spritecache.h:23
Structure for passing information from the sprite loader to the blitter.
void FioReadBlock(void *ptr, size_t size)
Read a block.
Definition: fileio.cpp:187
bool _palette_remap_grf[]
Whether the given NewGRFs must get a palette remap from windows to DOS or not.
Definition: gfxinit.cpp:32
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information...
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
bool SkipSpriteData(byte type, uint16 num)
Skip the given amount of sprite graphics data.
Definition: spritecache.cpp:96
Integer math functions.
SpriteType
Types of sprites that might be loaded.
Definition: gfx_type.h:297
uint16 height
Height of the sprite.
Definition: spritecache.h:19
The most basic (normal) sprite.
Definition: gfx_type.h:298
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:25
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
byte container_ver
Container version of the GRF the sprite is from.
Definition: spritecache.cpp:40
uint8 m
Remap-channel.
uint GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
static std::map< uint32, size_t > _grf_sprite_offsets
Map from sprite numbers to position in the GRF file.
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: fileio.cpp:176
uint16 width
Width of the sprite.
uint16 width
Width of the sprite.
Definition: spritecache.h:20
static void CompactSpriteCache()
Called when holes in the sprite cache should be removed.
The data of the error message.
Definition: error.h:30
Recolour sprite.
Definition: gfx_type.h:301
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
void * GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
Reads a sprite (from disk or sprite cache).
Sprite loader for graphics coming from a (New)GRF.
Definition: grf.hpp:18
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
Definition: fileio.cpp:150
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
Functions related to zooming.
uint16 height
Height of the sprite.
bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id, byte container_version)
Load a real or recolour sprite.
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:21
The normal zoom level.
Definition: zoom_type.h:24
uint GetSpriteCountForSlot(uint file_slot, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file slot in a range of SpriteIDs.
static const byte _palmap_d2w[]
Converting from the DOS palette to the Windows palette.
int16 y_offs
The y-offset of where the sprite will be drawn.
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
SpriteType type
In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as recolour spr...
Definition: spritecache.cpp:38
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
size_t FioGetPos()
Get position in the current file.
Definition: fileio.cpp:68
Functions related to memory operations.
This file contains all sprite-related enums and defines.
Factory to &#39;query&#39; all available blitters.
Translation tables from one GRF to another GRF.
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:51