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