23 #include "table/strings.h"
29 uint _sprite_cache_size = 4;
43 static uint _spritecache_items = 0;
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 %u items (" PRINTF_SIZE
" 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 static uint _sprite_lru_counter;
83 static uint _allocated_sprite_cache_size = 0;
84 static int _compact_cache_counter;
87 static void *AllocSprite(
size_t mem_req);
103 int size = (i == 0) ? 0x80 : i;
104 if (size > num)
return false;
120 if (
id >= _spritecache_items)
return false;
123 if (
id == 0)
return true;
124 return !(GetSpriteCache(
id)->file_pos == 0 && GetSpriteCache(
id)->file_slot == 0);
135 return GetSpriteCache(sprite)->
type;
145 if (!SpriteExists(sprite))
return 0;
146 return GetSpriteCache(sprite)->file_slot;
156 if (!SpriteExists(sprite))
return 0;
157 return GetSpriteCache(sprite)->id;
170 for (
SpriteID i = begin; i != end; i++) {
171 if (SpriteExists(i)) {
173 if (sc->file_slot == file_slot) count++;
189 return _spritecache_items;
197 if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX)
return false;
199 sprite[tgt].
width = sprite[src].
width * scaled_1;
205 sprite[tgt].
AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
208 for (
int y = 0; y < sprite[tgt].
height; y++) {
210 for (
int x = 0; x < sprite[tgt].width; x++) {
211 *dst = src_ln[x / scaled_1];
228 sprite[zoom].
AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
236 for (uint y = 0; y < sprite[zoom].
height; y++) {
238 assert(src_ln <= src_end);
239 for (uint x = 0; x < sprite[zoom].
width; x++) {
240 assert(src < src_ln);
241 if (src + 1 != src_ln && (src + 1)->a != 0) {
249 src = src_ln + sprite[zoom - 1].
width;
255 uint width = sprite->
width + pad_left + pad_right;
256 uint height = sprite->
height + pad_top + pad_bottom;
258 if (width > UINT16_MAX || height > UINT16_MAX)
return false;
268 for (uint y = 0; y < height; y++) {
269 if (y < pad_top || pad_bottom + y >= height) {
282 src += sprite->
width;
283 data += sprite->
width;
295 sprite->
width = width;
297 sprite->
x_offs -= pad_left;
298 sprite->
y_offs -= pad_top;
306 int min_xoffs = INT32_MAX;
307 int min_yoffs = INT32_MAX;
309 if (
HasBit(sprite_avail, zoom)) {
310 min_xoffs = std::min(min_xoffs,
ScaleByZoom(sprite[zoom].x_offs, zoom));
311 min_yoffs = std::min(min_yoffs,
ScaleByZoom(sprite[zoom].y_offs, zoom));
316 int max_width = INT32_MIN;
317 int max_height = INT32_MIN;
319 if (
HasBit(sprite_avail, zoom)) {
320 max_width = std::max(max_width,
ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs -
UnScaleByZoom(min_xoffs, zoom), zoom));
321 max_height = std::max(max_height,
ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs -
UnScaleByZoom(min_yoffs, zoom), zoom));
328 max_width =
Align(max_width, align);
329 max_height =
Align(max_height, align);
334 if (
HasBit(sprite_avail, zoom)) {
337 int pad_left = std::max(0, sprite[zoom].x_offs -
UnScaleByZoom(min_xoffs, zoom));
338 int pad_top = std::max(0, sprite[zoom].y_offs -
UnScaleByZoom(min_yoffs, zoom));
339 int pad_right = std::max(0,
UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
340 int pad_bottom = std::max(0,
UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
342 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
343 if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom))
return false;
356 if (!ResizeSpriteIn(sprite, first_avail,
ZOOM_LVL_NORMAL))
return false;
361 if (!PadSprites(sprite, sprite_avail, encoder))
return false;
365 if (
HasBit(sprite_avail, zoom)) {
374 if (!
HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
392 static const uint RECOLOUR_SPRITE_SIZE = 257;
393 byte *dest = (
byte *)AllocSprite(std::max(RECOLOUR_SPRITE_SIZE, num));
396 byte *dest_tmp =
AllocaM(
byte, std::max(RECOLOUR_SPRITE_SIZE, num));
399 if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
403 for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
427 uint8 file_slot = sc->file_slot;
428 size_t file_pos = sc->file_pos;
431 assert(IsMapgenSpriteID(
id) == (sprite_type ==
ST_MAPGEN));
432 assert(sc->
type == sprite_type);
434 DEBUG(sprite, 9,
"Load sprite %d",
id);
437 uint8 sprite_avail = 0;
443 sprite_avail = sprite_loader.
LoadSprite(sprite, file_slot, file_pos, sprite_type,
true);
445 if (sprite_avail == 0) {
446 sprite_avail = sprite_loader.
LoadSprite(sprite, file_slot, file_pos, sprite_type,
false);
449 if (sprite_avail == 0) {
450 if (sprite_type ==
ST_MAPGEN)
return nullptr;
451 if (
id == SPR_IMG_QUERY)
usererror(
"Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
474 byte *dest = s->
data;
483 if (!ResizeSprites(sprite, sprite_avail, file_slot, sc->id, encoder)) {
484 if (
id == SPR_IMG_QUERY)
usererror(
"Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
498 return encoder->
Encode(sprite, allocator);
523 if (container_version >= 2) {
531 uint32 id, prev_id = 0;
552 bool LoadNextSprite(
int load_index,
byte file_slot, uint file_sprite_id,
byte container_version)
558 if (num == 0)
return false;
562 void *data =
nullptr;
563 if (grf_type == 0xFF) {
572 }
else if (container_version >= 2 && grf_type == 0xFD) {
585 if (container_version >= 2)
return false;
594 bool is_mapgen = IsMapgenSpriteID(load_index);
597 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?");
602 sc->file_slot = file_slot;
603 sc->file_pos = file_pos;
606 sc->id = file_sprite_id;
620 scnew->file_slot = scold->file_slot;
621 scnew->file_pos = scold->file_pos;
622 scnew->ptr =
nullptr;
623 scnew->id = scold->id;
638 static_assert(
sizeof(
MemBlock) ==
sizeof(
size_t));
640 static_assert((
sizeof(
size_t) & (
sizeof(
size_t) - 1)) == 0);
647 static size_t GetSpriteCacheUsage()
652 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
660 void IncreaseSpriteLRU()
663 if (_sprite_lru_counter > 16384) {
666 DEBUG(sprite, 3,
"Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
668 for (i = 0; i != _spritecache_items; i++) {
670 if (sc->ptr !=
nullptr) {
673 }
else if (sc->lru != -32768) {
678 _sprite_lru_counter = 0;
682 if (++_compact_cache_counter >= 740) {
684 _compact_cache_counter = 0;
696 DEBUG(sprite, 3,
"Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
698 for (s = _spritecache_ptr; s->size != 0;) {
708 if (next->size == 0)
break;
711 for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
712 assert(i != _spritecache_items);
715 GetSpriteCache(i)->ptr = s->data;
718 memmove(s, next, next->size);
742 GetSpriteCache(item)->ptr =
nullptr;
745 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
756 uint best = UINT_MAX;
759 DEBUG(sprite, 3,
"DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
762 for (
SpriteID i = 0; i != _spritecache_items; i++) {
764 if (sc->
type !=
ST_RECOLOUR && sc->ptr !=
nullptr && sc->lru < cur_lru) {
772 if (best == UINT_MAX)
error(
"Out of sprite memory");
777 static void *AllocSprite(
size_t mem_req)
788 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
794 if (cur_size == mem_req ||
795 cur_size >= mem_req +
sizeof(
MemBlock)) {
800 if (cur_size != mem_req) {
801 NextBlock(s)->size = (cur_size - mem_req) |
S_FREE_MASK;
819 return MallocT<byte>(size);
833 static const char *
const sprite_types[] = {
846 byte warning_level = sc->
warned ? 6 : 0;
848 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]);
852 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?");
857 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?");
878 assert(type !=
ST_MAPGEN || IsMapgenSpriteID(sprite));
881 if (!SpriteExists(sprite)) {
882 DEBUG(sprite, 1,
"Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
885 sprite = SPR_IMG_QUERY;
892 if (allocator ==
nullptr && encoder ==
nullptr) {
896 sc->lru = ++_sprite_lru_counter;
899 if (sc->ptr ==
nullptr) sc->ptr =
ReadSprite(sc, sprite, type, AllocSprite,
nullptr);
904 return ReadSprite(sc, sprite, type, allocator, encoder);
909 static void GfxInitSpriteCache()
913 uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
916 static uint last_alloc_attempt = 0;
918 if (_spritecache_ptr ==
nullptr || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
919 delete[]
reinterpret_cast<byte *
>(_spritecache_ptr);
921 last_alloc_attempt = target_size;
922 _allocated_sprite_cache_size = target_size;
927 _spritecache_ptr =
reinterpret_cast<MemBlock *
>(
new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
928 }
catch (std::bad_alloc &) {
929 _spritecache_ptr =
nullptr;
932 if (_spritecache_ptr !=
nullptr) {
934 delete[]
reinterpret_cast<byte *
>(_spritecache_ptr);
935 _spritecache_ptr =
reinterpret_cast<MemBlock *
>(
new byte[_allocated_sprite_cache_size]);
936 }
else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
937 usererror(
"Cannot allocate spritecache");
940 _allocated_sprite_cache_size >>= 1;
942 }
while (_spritecache_ptr ==
nullptr);
944 if (_allocated_sprite_cache_size != target_size) {
945 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);
947 ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
948 msg.SetDParam(0, target_size);
949 msg.SetDParam(1, _allocated_sprite_cache_size);
957 NextBlock(_spritecache_ptr)->size = 0;
960 void GfxInitSpriteMem()
962 GfxInitSpriteCache();
966 _spritecache_items = 0;
967 _spritecache =
nullptr;
969 _compact_cache_counter = 0;
979 for (uint i = 0; i != _spritecache_items; i++) {