23 #include "table/strings.h"
29 uint _sprite_cache_size = 4;
43 static uint _spritecache_items = 0;
45 static std::vector<std::unique_ptr<SpriteFile>> _sprite_files;
47 static inline SpriteCache *GetSpriteCache(uint index)
49 return &_spritecache[index];
52 static inline bool IsMapgenSpriteID(
SpriteID sprite)
59 if (index >= _spritecache_items) {
61 uint items =
Align(index + 1, 1024);
63 Debug(sprite, 4,
"Increasing sprite cache to {} items ({} bytes)", items, items *
sizeof(*_spritecache));
65 _spritecache =
ReallocT(_spritecache, items);
68 memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) *
sizeof(*_spritecache));
69 _spritecache_items = items;
72 return GetSpriteCache(index);
81 for (
auto &f : _sprite_files) {
82 if (f->GetFilename() == filename) {
99 if (file ==
nullptr) {
100 file = _sprite_files.emplace_back(
new SpriteFile(filename, subdir, palette_remap)).get();
112 static uint _sprite_lru_counter;
114 static uint _allocated_sprite_cache_size = 0;
115 static int _compact_cache_counter;
118 static void *AllocSprite(
size_t mem_req);
134 int size = (i == 0) ? 0x80 : i;
135 if (size > num)
return false;
151 if (
id >= _spritecache_items)
return false;
154 if (
id == 0)
return true;
155 return !(GetSpriteCache(
id)->file_pos == 0 && GetSpriteCache(
id)->
file ==
nullptr);
166 return GetSpriteCache(sprite)->
type;
176 if (!SpriteExists(sprite))
return nullptr;
177 return GetSpriteCache(sprite)->
file;
187 if (!SpriteExists(sprite))
return 0;
188 return GetSpriteCache(sprite)->id;
201 if (file ==
nullptr)
return 0;
204 for (
SpriteID i = begin; i != end; i++) {
205 if (SpriteExists(i)) {
207 if (sc->
file == file) count++;
223 return _spritecache_items;
231 if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX)
return false;
233 sprite[tgt].
width = sprite[src].
width * scaled_1;
239 sprite[tgt].
AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
242 for (
int y = 0; y < sprite[tgt].
height; y++) {
244 for (
int x = 0; x < sprite[tgt].width; x++) {
245 *dst = src_ln[x / scaled_1];
262 sprite[zoom].
AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
268 for (uint y = 0; y < sprite[zoom].
height; y++) {
270 assert(src_ln <= src_end);
271 for (uint x = 0; x < sprite[zoom].
width; x++) {
272 assert(src < src_ln);
273 if (src + 1 != src_ln && (src + 1)->a != 0) {
281 src = src_ln + sprite[zoom - 1].
width;
287 uint width = sprite->
width + pad_left + pad_right;
288 uint height = sprite->
height + pad_top + pad_bottom;
290 if (width > UINT16_MAX || height > UINT16_MAX)
return false;
300 for (uint y = 0; y < height; y++) {
301 if (y < pad_top || pad_bottom + y >= height) {
314 src += sprite->
width;
315 data += sprite->
width;
327 sprite->
width = width;
329 sprite->
x_offs -= pad_left;
330 sprite->
y_offs -= pad_top;
338 int min_xoffs = INT32_MAX;
339 int min_yoffs = INT32_MAX;
341 if (
HasBit(sprite_avail, zoom)) {
342 min_xoffs = std::min(min_xoffs,
ScaleByZoom(sprite[zoom].x_offs, zoom));
343 min_yoffs = std::min(min_yoffs,
ScaleByZoom(sprite[zoom].y_offs, zoom));
348 int max_width = INT32_MIN;
349 int max_height = INT32_MIN;
351 if (
HasBit(sprite_avail, zoom)) {
352 max_width = std::max(max_width,
ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs -
UnScaleByZoom(min_xoffs, zoom), zoom));
353 max_height = std::max(max_height,
ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs -
UnScaleByZoom(min_yoffs, zoom), zoom));
360 max_width =
Align(max_width, align);
361 max_height =
Align(max_height, align);
366 if (
HasBit(sprite_avail, zoom)) {
369 int pad_left = std::max(0, sprite[zoom].x_offs -
UnScaleByZoom(min_xoffs, zoom));
370 int pad_top = std::max(0, sprite[zoom].y_offs -
UnScaleByZoom(min_yoffs, zoom));
371 int pad_right = std::max(0,
UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
372 int pad_bottom = std::max(0,
UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
374 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
375 if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom))
return false;
388 if (!ResizeSpriteIn(sprite, first_avail,
ZOOM_LVL_NORMAL))
return false;
393 if (!PadSprites(sprite, sprite_avail, encoder))
return false;
397 if (
HasBit(sprite_avail, zoom)) {
406 if (!
HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
424 static const uint RECOLOUR_SPRITE_SIZE = 257;
425 byte *dest = (
byte *)AllocSprite(std::max(RECOLOUR_SPRITE_SIZE, num));
428 byte *dest_tmp =
AllocaM(
byte, std::max(RECOLOUR_SPRITE_SIZE, num));
431 if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
435 for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
460 size_t file_pos = sc->file_pos;
463 assert(IsMapgenSpriteID(
id) == (sprite_type ==
ST_MAPGEN));
464 assert(sc->
type == sprite_type);
466 Debug(sprite, 9,
"Load sprite {}",
id);
469 uint8 sprite_avail = 0;
477 if (sprite_avail == 0) {
481 if (sprite_avail == 0) {
482 if (sprite_type ==
ST_MAPGEN)
return nullptr;
483 if (
id == SPR_IMG_QUERY)
usererror(
"Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
506 byte *dest = s->
data;
515 if (!ResizeSprites(sprite, sprite_avail, encoder)) {
516 if (
id == SPR_IMG_QUERY)
usererror(
"Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
530 return encoder->
Encode(sprite, allocator);
562 size_t old_pos = file.
GetPos();
563 file.
SeekTo(data_offset, SEEK_CUR);
569 uint32 id, prev_id = 0;
573 offset.file_pos = file.
GetPos() - 4;
574 offset.control_flags = 0;
584 if (colour != 0 && zoom == 0) {
588 if (colour != 0 && zoom == 2) {
598 file.
SeekTo(old_pos, SEEK_SET);
613 size_t file_pos = file.
GetPos();
617 if (num == 0)
return false;
621 void *data =
nullptr;
622 byte control_flags = 0;
623 if (grf_type == 0xFF) {
641 file_pos = iter->second.file_pos;
642 control_flags = iter->second.control_flags;
660 bool is_mapgen = IsMapgenSpriteID(load_index);
663 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?");
669 sc->file_pos = file_pos;
672 sc->id = file_sprite_id;
687 scnew->file_pos = scold->file_pos;
688 scnew->ptr =
nullptr;
689 scnew->id = scold->id;
703 static_assert(
sizeof(
MemBlock) ==
sizeof(
size_t));
705 static_assert((
sizeof(
size_t) & (
sizeof(
size_t) - 1)) == 0);
712 static size_t GetSpriteCacheUsage()
717 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
725 void IncreaseSpriteLRU()
728 if (_sprite_lru_counter > 16384) {
731 Debug(sprite, 3,
"Fixing lru {}, inuse={}", _sprite_lru_counter, GetSpriteCacheUsage());
733 for (i = 0; i != _spritecache_items; i++) {
735 if (sc->ptr !=
nullptr) {
738 }
else if (sc->lru != -32768) {
743 _sprite_lru_counter = 0;
747 if (++_compact_cache_counter >= 740) {
749 _compact_cache_counter = 0;
761 Debug(sprite, 3,
"Compacting sprite cache, inuse={}", GetSpriteCacheUsage());
763 for (s = _spritecache_ptr; s->size != 0;) {
773 if (next->size == 0)
break;
776 for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
777 assert(i != _spritecache_items);
780 GetSpriteCache(i)->ptr = s->data;
783 memmove(s, next, next->size);
807 GetSpriteCache(item)->ptr =
nullptr;
810 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
821 uint best = UINT_MAX;
824 Debug(sprite, 3,
"DeleteEntryFromSpriteCache, inuse={}", GetSpriteCacheUsage());
827 for (
SpriteID i = 0; i != _spritecache_items; i++) {
829 if (sc->
type !=
ST_RECOLOUR && sc->ptr !=
nullptr && sc->lru < cur_lru) {
837 if (best == UINT_MAX)
error(
"Out of sprite memory");
842 static void *AllocSprite(
size_t mem_req)
853 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
859 if (cur_size == mem_req ||
860 cur_size >= mem_req +
sizeof(
MemBlock)) {
865 if (cur_size != mem_req) {
866 NextBlock(s)->size = (cur_size - mem_req) |
S_FREE_MASK;
884 return MallocT<byte>(size);
898 static const char *
const sprite_types[] = {
911 byte warning_level = sc->
warned ? 6 : 0;
913 Debug(sprite, warning_level,
"Tried to load {} sprite #{} as a {} sprite. Probable cause: NewGRF interference", sprite_types[available], sprite, sprite_types[requested]);
917 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?");
922 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?");
943 assert(type !=
ST_MAPGEN || IsMapgenSpriteID(sprite));
946 if (!SpriteExists(sprite)) {
947 Debug(sprite, 1,
"Tried to load non-existing sprite #{}. Probable cause: Wrong/missing NewGRFs", sprite);
950 sprite = SPR_IMG_QUERY;
957 if (allocator ==
nullptr && encoder ==
nullptr) {
961 sc->lru = ++_sprite_lru_counter;
964 if (sc->ptr ==
nullptr) sc->ptr =
ReadSprite(sc, sprite, type, AllocSprite,
nullptr);
969 return ReadSprite(sc, sprite, type, allocator, encoder);
974 static void GfxInitSpriteCache()
978 uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
981 static uint last_alloc_attempt = 0;
983 if (_spritecache_ptr ==
nullptr || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
984 delete[]
reinterpret_cast<byte *
>(_spritecache_ptr);
986 last_alloc_attempt = target_size;
987 _allocated_sprite_cache_size = target_size;
992 _spritecache_ptr =
reinterpret_cast<MemBlock *
>(
new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
993 }
catch (std::bad_alloc &) {
994 _spritecache_ptr =
nullptr;
997 if (_spritecache_ptr !=
nullptr) {
999 delete[]
reinterpret_cast<byte *
>(_spritecache_ptr);
1000 _spritecache_ptr =
reinterpret_cast<MemBlock *
>(
new byte[_allocated_sprite_cache_size]);
1001 }
else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
1002 usererror(
"Cannot allocate spritecache");
1005 _allocated_sprite_cache_size >>= 1;
1007 }
while (_spritecache_ptr ==
nullptr);
1009 if (_allocated_sprite_cache_size != target_size) {
1010 Debug(misc, 0,
"Not enough memory to allocate {} MiB of spritecache. Spritecache was reduced to {} MiB.", target_size / 1024 / 1024, _allocated_sprite_cache_size / 1024 / 1024);
1012 ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
1013 msg.SetDParam(0, target_size);
1014 msg.SetDParam(1, _allocated_sprite_cache_size);
1022 NextBlock(_spritecache_ptr)->size = 0;
1025 void GfxInitSpriteMem()
1027 GfxInitSpriteCache();
1031 _spritecache_items = 0;
1032 _spritecache =
nullptr;
1034 _compact_cache_counter = 0;
1035 _sprite_files.clear();
1045 for (uint i = 0; i != _spritecache_items; i++) {