23 #include "table/strings.h"
29 uint _sprite_cache_size = 4;
42 static uint _spritecache_items = 0;
44 static std::vector<std::unique_ptr<SpriteFile>> _sprite_files;
46 static inline SpriteCache *GetSpriteCache(uint index)
48 return &_spritecache[index];
51 static inline bool IsMapgenSpriteID(
SpriteID sprite)
58 if (index >= _spritecache_items) {
60 uint items =
Align(index + 1, 1024);
62 Debug(sprite, 4,
"Increasing sprite cache to {} items ({} bytes)", items, items *
sizeof(*_spritecache));
64 _spritecache =
ReallocT(_spritecache, items);
67 memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) *
sizeof(*_spritecache));
68 _spritecache_items = items;
71 return GetSpriteCache(index);
80 for (
auto &f : _sprite_files) {
81 if (f->GetFilename() == filename) {
98 if (file ==
nullptr) {
99 file = _sprite_files.emplace_back(
new SpriteFile(filename, subdir, palette_remap)).get();
111 static uint _sprite_lru_counter;
113 static uint _allocated_sprite_cache_size = 0;
114 static int _compact_cache_counter;
117 static void *AllocSprite(
size_t mem_req);
133 int size = (i == 0) ? 0x80 : i;
134 if (size > num)
return false;
150 if (
id >= _spritecache_items)
return false;
153 if (
id == 0)
return true;
154 return !(GetSpriteCache(
id)->file_pos == 0 && GetSpriteCache(
id)->
file ==
nullptr);
165 return GetSpriteCache(sprite)->
type;
175 if (!SpriteExists(sprite))
return nullptr;
176 return GetSpriteCache(sprite)->
file;
186 if (!SpriteExists(sprite))
return 0;
187 return GetSpriteCache(sprite)->id;
200 if (file ==
nullptr)
return 0;
203 for (
SpriteID i = begin; i != end; i++) {
204 if (SpriteExists(i)) {
206 if (sc->
file == file) count++;
222 return _spritecache_items;
230 if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX)
return false;
232 sprite[tgt].
width = sprite[src].
width * scaled_1;
238 sprite[tgt].
AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
241 for (
int y = 0; y < sprite[tgt].
height; y++) {
243 for (
int x = 0; x < sprite[tgt].width; x++) {
244 *dst = src_ln[x / scaled_1];
261 sprite[zoom].
AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
267 for (uint y = 0; y < sprite[zoom].
height; y++) {
269 assert(src_ln <= src_end);
270 for (uint x = 0; x < sprite[zoom].
width; x++) {
271 assert(src < src_ln);
272 if (src + 1 != src_ln && (src + 1)->a != 0) {
280 src = src_ln + sprite[zoom - 1].
width;
286 uint width = sprite->
width + pad_left + pad_right;
287 uint height = sprite->
height + pad_top + pad_bottom;
289 if (width > UINT16_MAX || height > UINT16_MAX)
return false;
299 for (uint y = 0; y < height; y++) {
300 if (y < pad_top || pad_bottom + y >= height) {
313 src += sprite->
width;
314 data += sprite->
width;
326 sprite->
width = width;
328 sprite->
x_offs -= pad_left;
329 sprite->
y_offs -= pad_top;
337 int min_xoffs = INT32_MAX;
338 int min_yoffs = INT32_MAX;
340 if (
HasBit(sprite_avail, zoom)) {
341 min_xoffs = std::min(min_xoffs,
ScaleByZoom(sprite[zoom].x_offs, zoom));
342 min_yoffs = std::min(min_yoffs,
ScaleByZoom(sprite[zoom].y_offs, zoom));
347 int max_width = INT32_MIN;
348 int max_height = INT32_MIN;
350 if (
HasBit(sprite_avail, zoom)) {
351 max_width = std::max(max_width,
ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs -
UnScaleByZoom(min_xoffs, zoom), zoom));
352 max_height = std::max(max_height,
ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs -
UnScaleByZoom(min_yoffs, zoom), zoom));
359 max_width =
Align(max_width, align);
360 max_height =
Align(max_height, align);
365 if (
HasBit(sprite_avail, zoom)) {
368 int pad_left = std::max(0, sprite[zoom].x_offs -
UnScaleByZoom(min_xoffs, zoom));
369 int pad_top = std::max(0, sprite[zoom].y_offs -
UnScaleByZoom(min_yoffs, zoom));
370 int pad_right = std::max(0,
UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
371 int pad_bottom = std::max(0,
UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
373 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
374 if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom))
return false;
387 if (!ResizeSpriteIn(sprite, first_avail,
ZOOM_LVL_NORMAL))
return false;
392 if (!PadSprites(sprite, sprite_avail, encoder))
return false;
396 if (
HasBit(sprite_avail, zoom)) {
405 if (!
HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
423 static const uint RECOLOUR_SPRITE_SIZE = 257;
424 byte *dest = (
byte *)AllocSprite(std::max(RECOLOUR_SPRITE_SIZE, num));
427 byte *dest_tmp =
AllocaM(
byte, std::max(RECOLOUR_SPRITE_SIZE, num));
430 if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
434 for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
459 size_t file_pos = sc->file_pos;
462 assert(IsMapgenSpriteID(
id) == (sprite_type ==
ST_MAPGEN));
463 assert(sc->
type == sprite_type);
465 Debug(sprite, 9,
"Load sprite {}",
id);
468 uint8 sprite_avail = 0;
474 sprite_avail = sprite_loader.
LoadSprite(sprite, file, file_pos, sprite_type,
true);
476 if (sprite_avail == 0) {
477 sprite_avail = sprite_loader.
LoadSprite(sprite, file, file_pos, sprite_type,
false);
480 if (sprite_avail == 0) {
481 if (sprite_type ==
ST_MAPGEN)
return nullptr;
482 if (
id == SPR_IMG_QUERY)
usererror(
"Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
505 byte *dest = s->
data;
514 if (!ResizeSprites(sprite, sprite_avail, encoder)) {
515 if (
id == SPR_IMG_QUERY)
usererror(
"Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
529 return encoder->
Encode(sprite, allocator);
557 size_t old_pos = file.
GetPos();
558 file.
SeekTo(data_offset, SEEK_CUR);
562 uint32 id, prev_id = 0;
570 file.
SeekTo(old_pos, SEEK_SET);
585 size_t file_pos = file.
GetPos();
589 if (num == 0)
return false;
593 void *data =
nullptr;
594 if (grf_type == 0xFF) {
625 bool is_mapgen = IsMapgenSpriteID(load_index);
628 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?");
634 sc->file_pos = file_pos;
637 sc->id = file_sprite_id;
651 scnew->file_pos = scold->file_pos;
652 scnew->ptr =
nullptr;
653 scnew->id = scold->id;
667 static_assert(
sizeof(
MemBlock) ==
sizeof(
size_t));
669 static_assert((
sizeof(
size_t) & (
sizeof(
size_t) - 1)) == 0);
676 static size_t GetSpriteCacheUsage()
681 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
689 void IncreaseSpriteLRU()
692 if (_sprite_lru_counter > 16384) {
695 Debug(sprite, 3,
"Fixing lru {}, inuse={}", _sprite_lru_counter, GetSpriteCacheUsage());
697 for (i = 0; i != _spritecache_items; i++) {
699 if (sc->ptr !=
nullptr) {
702 }
else if (sc->lru != -32768) {
707 _sprite_lru_counter = 0;
711 if (++_compact_cache_counter >= 740) {
713 _compact_cache_counter = 0;
725 Debug(sprite, 3,
"Compacting sprite cache, inuse={}", GetSpriteCacheUsage());
727 for (s = _spritecache_ptr; s->size != 0;) {
737 if (next->size == 0)
break;
740 for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
741 assert(i != _spritecache_items);
744 GetSpriteCache(i)->ptr = s->data;
747 memmove(s, next, next->size);
771 GetSpriteCache(item)->ptr =
nullptr;
774 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
785 uint best = UINT_MAX;
788 Debug(sprite, 3,
"DeleteEntryFromSpriteCache, inuse={}", GetSpriteCacheUsage());
791 for (
SpriteID i = 0; i != _spritecache_items; i++) {
793 if (sc->
type !=
ST_RECOLOUR && sc->ptr !=
nullptr && sc->lru < cur_lru) {
801 if (best == UINT_MAX)
error(
"Out of sprite memory");
806 static void *AllocSprite(
size_t mem_req)
817 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
823 if (cur_size == mem_req ||
824 cur_size >= mem_req +
sizeof(
MemBlock)) {
829 if (cur_size != mem_req) {
830 NextBlock(s)->size = (cur_size - mem_req) |
S_FREE_MASK;
848 return MallocT<byte>(size);
862 static const char *
const sprite_types[] = {
875 byte warning_level = sc->
warned ? 6 : 0;
877 Debug(sprite, warning_level,
"Tried to load {} sprite #{} as a {} sprite. Probable cause: NewGRF interference", sprite_types[available], sprite, sprite_types[requested]);
881 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?");
886 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?");
907 assert(type !=
ST_MAPGEN || IsMapgenSpriteID(sprite));
910 if (!SpriteExists(sprite)) {
911 Debug(sprite, 1,
"Tried to load non-existing sprite #{}. Probable cause: Wrong/missing NewGRFs", sprite);
914 sprite = SPR_IMG_QUERY;
921 if (allocator ==
nullptr && encoder ==
nullptr) {
925 sc->lru = ++_sprite_lru_counter;
928 if (sc->ptr ==
nullptr) sc->ptr =
ReadSprite(sc, sprite, type, AllocSprite,
nullptr);
933 return ReadSprite(sc, sprite, type, allocator, encoder);
938 static void GfxInitSpriteCache()
942 uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
945 static uint last_alloc_attempt = 0;
947 if (_spritecache_ptr ==
nullptr || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
948 delete[]
reinterpret_cast<byte *
>(_spritecache_ptr);
950 last_alloc_attempt = target_size;
951 _allocated_sprite_cache_size = target_size;
956 _spritecache_ptr =
reinterpret_cast<MemBlock *
>(
new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
957 }
catch (std::bad_alloc &) {
958 _spritecache_ptr =
nullptr;
961 if (_spritecache_ptr !=
nullptr) {
963 delete[]
reinterpret_cast<byte *
>(_spritecache_ptr);
964 _spritecache_ptr =
reinterpret_cast<MemBlock *
>(
new byte[_allocated_sprite_cache_size]);
965 }
else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
966 usererror(
"Cannot allocate spritecache");
969 _allocated_sprite_cache_size >>= 1;
971 }
while (_spritecache_ptr ==
nullptr);
973 if (_allocated_sprite_cache_size != target_size) {
974 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);
976 ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
977 msg.SetDParam(0, target_size);
978 msg.SetDParam(1, _allocated_sprite_cache_size);
986 NextBlock(_spritecache_ptr)->size = 0;
989 void GfxInitSpriteMem()
991 GfxInitSpriteCache();
995 _spritecache_items = 0;
996 _spritecache =
nullptr;
998 _compact_cache_counter = 0;
999 _sprite_files.clear();
1009 for (uint i = 0; i != _spritecache_items; i++) {