OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "spriteloader/grf.hpp"
13#include "error_func.h"
14#include "zoom_func.h"
15#include "settings_type.h"
16#include "blitter/factory.hpp"
17#include "core/math_func.hpp"
19#include "spritecache.h"
21
22#include "table/sprites.h"
24
25#include "safeguards.h"
26
29
30
31static std::vector<SpriteCache> _spritecache;
32static size_t _spritecache_bytes_used = 0;
33static uint32_t _sprite_lru_counter;
34static std::vector<std::unique_ptr<SpriteFile>> _sprite_files;
35
36static inline SpriteCache *GetSpriteCache(uint index)
37{
38 return &_spritecache[index];
39}
40
41SpriteCache *AllocateSpriteCache(uint index)
42{
43 if (index >= _spritecache.size()) {
44 /* Add another 1024 items to the 'pool' */
45 uint items = Align(index + 1, 1024);
46
47 Debug(Facility::Sprite, Severity::Info, "Increasing sprite cache to {} items ({} bytes)", items, items * sizeof(SpriteCache));
48
49 _spritecache.resize(items);
50 }
51
52 return GetSpriteCache(index);
53}
54
60static SpriteFile *GetCachedSpriteFileByName(const std::string &filename)
61{
62 for (auto &f : _sprite_files) {
63 if (f->GetFilename() == filename) {
64 return f.get();
65 }
66 }
67 return nullptr;
68}
69
74std::span<const std::unique_ptr<SpriteFile>> GetCachedSpriteFiles()
75{
76 return _sprite_files;
77}
78
86SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
87{
88 SpriteFile *file = GetCachedSpriteFileByName(filename);
89 if (file == nullptr) {
90 file = _sprite_files.insert(std::end(_sprite_files), std::make_unique<SpriteFile>(filename, subdir, palette_remap))->get();
91 } else {
92 file->SeekToBegin();
93 }
94 return *file;
95}
96
104bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
105{
106 if (type & 2) {
107 file.SkipBytes(num);
108 } else {
109 while (num > 0) {
110 int8_t i = file.ReadByte();
111 if (i >= 0) {
112 int size = (i == 0) ? 0x80 : i;
113 if (size > num) return false;
114 num -= size;
115 file.SkipBytes(size);
116 } else {
117 i = -(i >> 3);
118 num -= i;
119 file.ReadByte();
120 }
121 }
122 }
123 return true;
124}
125
126/* Check if the given Sprite ID exists */
127bool SpriteExists(SpriteID id)
128{
129 if (id >= _spritecache.size()) return false;
130
131 /* Special case for Sprite ID zero -- its position is also 0... */
132 if (id == 0) return true;
133 return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file == nullptr);
134}
135
142{
143 if (!SpriteExists(sprite)) return SpriteType::Invalid;
144 return GetSpriteCache(sprite)->type;
145}
146
153{
154 if (!SpriteExists(sprite)) return nullptr;
155 return GetSpriteCache(sprite)->file;
156}
157
164{
165 if (!SpriteExists(sprite)) return 0;
166 return GetSpriteCache(sprite)->id;
167}
168
176uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
177{
178 SpriteFile *file = GetCachedSpriteFileByName(filename);
179 if (file == nullptr) return 0;
180
181 uint count = 0;
182 for (SpriteID i = begin; i != end; i++) {
183 if (SpriteExists(i)) {
184 SpriteCache *sc = GetSpriteCache(i);
185 if (sc->file == file) {
186 count++;
187 Debug(Facility::Sprite, Severity::Info, "Sprite: {}", i);
188 }
189 }
190 }
191 return count;
192}
193
203{
204 return static_cast<SpriteID>(_spritecache.size());
205}
206
207static bool ResizeSpriteIn(SpriteLoader::SpriteCollection &sprite, ZoomLevel src, ZoomLevel tgt)
208{
209 uint8_t scaled_1 = AdjustByZoom(1, src - tgt);
210 const auto &src_sprite = sprite[src];
211 auto &dest_sprite = sprite[tgt];
212
213 /* Check for possible memory overflow. */
214 if (src_sprite.width * scaled_1 > UINT16_MAX || src_sprite.height * scaled_1 > UINT16_MAX) return false;
215
216 dest_sprite.width = src_sprite.width * scaled_1;
217 dest_sprite.height = src_sprite.height * scaled_1;
218 dest_sprite.x_offs = src_sprite.x_offs * scaled_1;
219 dest_sprite.y_offs = src_sprite.y_offs * scaled_1;
220 dest_sprite.colours = src_sprite.colours;
221
222 dest_sprite.AllocateData(tgt, static_cast<size_t>(dest_sprite.width) * dest_sprite.height);
223
224 SpriteLoader::CommonPixel *dst = dest_sprite.data;
225 for (int y = 0; y < dest_sprite.height; y++) {
226 const SpriteLoader::CommonPixel *src_ln = &src_sprite.data[y / scaled_1 * src_sprite.width];
227 for (int x = 0; x < dest_sprite.width; x++) {
228 *dst = src_ln[x / scaled_1];
229 dst++;
230 }
231 }
232
233 return true;
234}
235
236static void ResizeSpriteOut(SpriteLoader::SpriteCollection &sprite, ZoomLevel zoom)
237{
238 const auto &root_sprite = sprite.Root();
239 const auto &src_sprite = sprite[zoom - 1];
240 auto &dest_sprite = sprite[zoom];
241
242 /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
243 dest_sprite.width = UnScaleByZoom(root_sprite.width, zoom);
244 dest_sprite.height = UnScaleByZoom(root_sprite.height, zoom);
245 dest_sprite.x_offs = UnScaleByZoom(root_sprite.x_offs, zoom);
246 dest_sprite.y_offs = UnScaleByZoom(root_sprite.y_offs, zoom);
247 dest_sprite.colours = root_sprite.colours;
248
249 dest_sprite.AllocateData(zoom, static_cast<size_t>(dest_sprite.height) * dest_sprite.width);
250
251 SpriteLoader::CommonPixel *dst = dest_sprite.data;
252 const SpriteLoader::CommonPixel *src = src_sprite.data;
253 [[maybe_unused]] const SpriteLoader::CommonPixel *src_end = src + src_sprite.height * src_sprite.width;
254
255 for (uint y = 0; y < dest_sprite.height; y++) {
256 const SpriteLoader::CommonPixel *src_ln = src + src_sprite.width;
257 assert(src_ln <= src_end);
258 for (uint x = 0; x < dest_sprite.width; x++) {
259 assert(src < src_ln);
260 if (src + 1 != src_ln && (src + 1)->a != 0) {
261 *dst = *(src + 1);
262 } else {
263 *dst = *src;
264 }
265 dst++;
266 src += 2;
267 }
268 src = src_ln + src_sprite.width;
269 }
270}
271
272static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
273{
274 uint width = sprite->width + pad_left + pad_right;
275 uint height = sprite->height + pad_top + pad_bottom;
276
277 if (width > UINT16_MAX || height > UINT16_MAX) return false;
278
279 /* Copy source data and reallocate sprite memory. */
280 size_t sprite_size = static_cast<size_t>(sprite->width) * sprite->height;
281 std::vector<SpriteLoader::CommonPixel> src_data(sprite->data, sprite->data + sprite_size);
282 sprite->AllocateData(zoom, static_cast<size_t>(width) * height);
283
284 /* Copy with padding to destination. */
285 SpriteLoader::CommonPixel *src = src_data.data();
286 SpriteLoader::CommonPixel *data = sprite->data;
287 for (uint y = 0; y < height; y++) {
288 if (y < pad_top || pad_bottom + y >= height) {
289 /* Top/bottom padding. */
290 std::fill_n(data, width, SpriteLoader::CommonPixel{});
291 data += width;
292 } else {
293 if (pad_left > 0) {
294 /* Pad left. */
295 std::fill_n(data, pad_left, SpriteLoader::CommonPixel{});
296 data += pad_left;
297 }
298
299 /* Copy pixels. */
300 std::copy_n(src, sprite->width, data);
301 src += sprite->width;
302 data += sprite->width;
303
304 if (pad_right > 0) {
305 /* Pad right. */
306 std::fill_n(data, pad_right, SpriteLoader::CommonPixel{});
307 data += pad_right;
308 }
309 }
310 }
311
312 /* Update sprite size. */
313 sprite->width = width;
314 sprite->height = height;
315 sprite->x_offs -= pad_left;
316 sprite->y_offs -= pad_top;
317
318 return true;
319}
320
321static bool PadSprites(SpriteLoader::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
322{
323 /* Get minimum top left corner coordinates. */
324 int min_xoffs = INT32_MAX;
325 int min_yoffs = INT32_MAX;
326 for (ZoomLevel zoom : sprite_avail) {
327 min_xoffs = std::min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
328 min_yoffs = std::min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
329 }
330
331 /* Get maximum dimensions taking necessary padding at the top left into account. */
332 int max_width = INT32_MIN;
333 int max_height = INT32_MIN;
334 for (ZoomLevel zoom : sprite_avail) {
335 max_width = std::max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
336 max_height = std::max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
337 }
338
339 /* Align height and width if required to match the needs of the sprite encoder. */
340 uint align = encoder->GetSpriteAlignment();
341 if (align != 0) {
342 max_width = Align(max_width, align);
343 max_height = Align(max_height, align);
344 }
345
346 /* Pad sprites where needed. */
347 for (ZoomLevel zoom : sprite_avail) {
348 auto &cur_sprite = sprite[zoom];
349 /* Scaling the sprite dimensions in the blitter is done with rounding up,
350 * so a negative padding here is not an error. */
351 int pad_left = std::max(0, cur_sprite.x_offs - UnScaleByZoom(min_xoffs, zoom));
352 int pad_top = std::max(0, cur_sprite.y_offs - UnScaleByZoom(min_yoffs, zoom));
353 int pad_right = std::max(0, UnScaleByZoom(max_width, zoom) - cur_sprite.width - pad_left);
354 int pad_bottom = std::max(0, UnScaleByZoom(max_height, zoom) - cur_sprite.height - pad_top);
355
356 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
357 if (!PadSingleSprite(&cur_sprite, zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
358 }
359 }
360
361 return true;
362}
363
364static bool ResizeSprites(SpriteLoader::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
365{
366 /* Create a fully zoomed image if it does not exist */
367 ZoomLevel first_avail = ZoomLevel::End;
368 for (ZoomLevel zoom = ZoomLevel::Min; zoom <= ZoomLevel::Max; ++zoom) {
369 if (!sprite_avail.Test(zoom)) continue;
370 first_avail = zoom;
371 if (zoom != ZoomLevel::Min) {
372 if (!ResizeSpriteIn(sprite, zoom, ZoomLevel::Min)) return false;
373 sprite_avail.Set(ZoomLevel::Min);
374 }
375 break;
376 }
377
378 /* Pad sprites to make sizes match. */
379 if (!PadSprites(sprite, sprite_avail, encoder)) return false;
380
381 /* Create other missing zoom levels */
383 if (sprite_avail.Test(zoom)) {
384 /* Check that size and offsets match the fully zoomed image. */
385 [[maybe_unused]] const auto &root_sprite = sprite[ZoomLevel::Min];
386 [[maybe_unused]] const auto &dest_sprite = sprite[zoom];
387 assert(dest_sprite.width == UnScaleByZoom(root_sprite.width, zoom));
388 assert(dest_sprite.height == UnScaleByZoom(root_sprite.height, zoom));
389 assert(dest_sprite.x_offs == UnScaleByZoom(root_sprite.x_offs, zoom));
390 assert(dest_sprite.y_offs == UnScaleByZoom(root_sprite.y_offs, zoom));
391 } else {
392 /* Zoom level is not available, or unusable, so create it */
393 ResizeSpriteOut(sprite, zoom);
394 }
395 }
396
397 /* Replace sprites with higher resolution than the desired maximum source resolution with scaled up sprites, if not already done. */
398 if (first_avail < _settings_client.gui.sprite_zoom_min) {
399 for (ZoomLevel zoom = std::min(ZoomLevel::Normal, _settings_client.gui.sprite_zoom_min); zoom > ZoomLevel::Min; --zoom) {
400 ResizeSpriteIn(sprite, zoom, zoom - 1);
401 }
402 }
403
404 return true;
405}
406
415static void *ReadRecolourSprite(SpriteFile &file, size_t file_pos, uint num, SpriteAllocator &allocator)
416{
417 /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
418 * number of recolour sprites that are 17 bytes that only exist in DOS
419 * GRFs which are the same as 257 byte recolour sprites, but with the last
420 * 240 bytes zeroed. */
421 static const uint RECOLOUR_SPRITE_SIZE = 257;
422 uint8_t *dest = allocator.Allocate<uint8_t>(std::max(RECOLOUR_SPRITE_SIZE, num));
423
424 file.SeekTo(file_pos, SEEK_SET);
425 if (file.NeedsPaletteRemap()) {
426 uint8_t *dest_tmp = new uint8_t[std::max(RECOLOUR_SPRITE_SIZE, num)];
427
428 /* Only a few recolour sprites are less than 257 bytes */
429 if (num < RECOLOUR_SPRITE_SIZE) std::fill_n(dest_tmp, RECOLOUR_SPRITE_SIZE, 0);
430 file.ReadBlock(dest_tmp, num);
431
432 /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
433 for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
434 dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
435 }
436 delete[] dest_tmp;
437 } else {
438 file.ReadBlock(dest, num);
439 }
440
441 return dest;
442}
443
453static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
454{
455 /* Use current blitter if no other sprite encoder is given. */
456 if (encoder == nullptr) encoder = BlitterFactory::GetCurrentBlitter();
457
458 SpriteFile &file = *sc->file;
459 size_t file_pos = sc->file_pos;
460
461 assert(sprite_type != SpriteType::Recolour);
462 assert(IsMapgenSpriteID(id) == (sprite_type == SpriteType::MapGen));
463 assert(sc->type == sprite_type);
464
465 Debug(Facility::Sprite, Severity::Trace3, "Load sprite {}", id);
466
468 ZoomLevels sprite_avail;
469 ZoomLevels avail_8bpp;
470 ZoomLevels avail_32bpp;
471
472 SpriteLoaderGrf sprite_loader(file.GetContainerVersion());
473 if (sprite_type != SpriteType::MapGen && encoder->Is32BppSupported()) {
474 /* Try for 32bpp sprites first. */
475 sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, true, sc->control_flags, avail_8bpp, avail_32bpp);
476 }
477 if (sprite_avail.None()) {
478 sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, false, sc->control_flags, avail_8bpp, avail_32bpp);
479 if (sprite_type == SpriteType::Normal && avail_32bpp.Any() && !encoder->Is32BppSupported() && sprite_avail.None()) {
480 /* No 8bpp available, try converting from 32bpp. */
481 SpriteLoaderMakeIndexed make_indexed(sprite_loader);
482 sprite_avail = make_indexed.LoadSprite(sprite, file, file_pos, sprite_type, true, sc->control_flags, sprite_avail, avail_32bpp);
483 }
484 }
485
486 if (sprite_avail.None()) {
487 if (sprite_type == SpriteType::MapGen) return nullptr;
488 if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
489 return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, &allocator, encoder);
490 }
491
492 if (sprite_type == SpriteType::MapGen) {
493 /* Ugly hack to work around the problem that the old landscape
494 * generator assumes that those sprites are stored uncompressed in
495 * the memory, and they are only read directly by the code, never
496 * send to the blitter. So do not send it to the blitter (which will
497 * result in a data array in the format the blitter likes most), but
498 * extract the data directly and store that as sprite.
499 * Ugly: yes. Other solution: no. Blame the original author or
500 * something ;) The image should really have been a data-stream
501 * (so type = 0xFF basically). */
502 const auto &root_sprite = sprite.Root();
503 uint num = root_sprite.width * root_sprite.height;
504
505 Sprite *s = allocator.Allocate<Sprite>(sizeof(*s) + num);
506 s->width = root_sprite.width;
507 s->height = root_sprite.height;
508 s->x_offs = root_sprite.x_offs;
509 s->y_offs = root_sprite.y_offs;
510
511 SpriteLoader::CommonPixel *src = root_sprite.data;
512 uint8_t *dest = reinterpret_cast<uint8_t *>(s->data);
513 while (num-- > 0) {
514 *dest++ = src->m;
515 src++;
516 }
517
518 return s;
519 }
520
521 if (!ResizeSprites(sprite, sprite_avail, encoder)) {
522 if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
523 return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, &allocator, encoder);
524 }
525
526 if (sprite_type == SpriteType::Font && _font_zoom != ZoomLevel::Min) {
527 /* Make ZoomLevel::Min the desired font zoom level. */
528 sprite[ZoomLevel::Min] = sprite[_font_zoom];
529 }
530
531 return encoder->Encode(sprite_type, sprite, allocator);
532}
533
535 size_t file_pos = 0;
536 SpriteCacheCtrlFlags control_flags{};
537};
538
540static std::map<uint32_t, GrfSpriteOffset> _grf_sprite_offsets;
541
547size_t GetGRFSpriteOffset(uint32_t id)
548{
549 return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id].file_pos : SIZE_MAX;
550}
551
557{
558 _grf_sprite_offsets.clear();
559
560 if (file.GetContainerVersion() >= 2) {
561 /* Seek to sprite section of the GRF. */
562 size_t data_offset = file.ReadDword();
563 size_t old_pos = file.GetPos();
564 file.SeekTo(data_offset, SEEK_CUR);
565
566 GrfSpriteOffset offset{};
567
568 /* Loop over all sprite section entries and store the file
569 * offset for each newly encountered ID. */
570 SpriteID id, prev_id = 0;
571 while ((id = file.ReadDword()) != 0) {
572 if (id != prev_id) {
573 _grf_sprite_offsets[prev_id] = offset;
574 offset.file_pos = file.GetPos() - 4;
575 offset.control_flags.Reset();
576 }
577 prev_id = id;
578 uint length = file.ReadDword();
579 if (length > 0) {
580 SpriteComponents colour{file.ReadByte()};
581 length--;
582 if (length > 0) {
583 uint8_t zoom = file.ReadByte();
584 length--;
585 if (colour.Any() && zoom == 0) { // ZoomLevel::Normal (normal zoom)
588 }
589 if (colour.Any() && zoom == 2) { // ZoomLevel::In2x (2x zoomed in)
591 }
592 }
593 }
594 file.SkipBytes(length);
595 }
596 if (prev_id != 0) _grf_sprite_offsets[prev_id] = offset;
597
598 /* Continue processing the data section. */
599 file.SeekTo(old_pos, SEEK_SET);
600 }
601}
602
603
611bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
612{
613 size_t file_pos = file.GetPos();
614
615 /* Read sprite header. */
616 uint32_t num = file.GetContainerVersion() >= 2 ? file.ReadDword() : file.ReadWord();
617 if (num == 0) return false;
618 uint8_t grf_type = file.ReadByte();
619
620 SpriteType type;
621 SpriteCacheCtrlFlags control_flags;
622 if (grf_type == 0xFF) {
623 /* Some NewGRF files have "empty" pseudo-sprites which are 1
624 * byte long. Catch these so the sprites won't be displayed. */
625 if (num == 1) {
626 file.ReadByte();
627 return false;
628 }
629 file_pos = file.GetPos();
631 file.SkipBytes(num);
632 } else if (file.GetContainerVersion() >= 2 && grf_type == 0xFD) {
633 if (num != 4) {
634 /* Invalid sprite section include, ignore. */
635 file.SkipBytes(num);
636 return false;
637 }
638 /* It is not an error if no sprite with the provided ID is found in the sprite section. */
639 auto iter = _grf_sprite_offsets.find(file.ReadDword());
640 if (iter != _grf_sprite_offsets.end()) {
641 file_pos = iter->second.file_pos;
642 control_flags = iter->second.control_flags;
643 } else {
644 file_pos = SIZE_MAX;
645 }
646 type = SpriteType::Normal;
647 } else {
648 file.SkipBytes(7);
649 type = SkipSpriteData(file, grf_type, num - 8) ? SpriteType::Normal : SpriteType::Invalid;
650 /* Inline sprites are not supported for container version >= 2. */
651 if (file.GetContainerVersion() >= 2) return false;
652 }
653
654 if (type == SpriteType::Invalid) return false;
655
656 if (load_index >= MAX_SPRITES) {
657 UserError("Tried to load too many sprites (#{}; max {})", load_index, MAX_SPRITES);
658 }
659
660 bool is_mapgen = IsMapgenSpriteID(load_index);
661
662 if (is_mapgen) {
663 if (type != SpriteType::Normal) UserError("Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites?");
664 type = SpriteType::MapGen;
665 }
666
667 SpriteCache *sc = AllocateSpriteCache(load_index);
668 sc->file = &file;
669 sc->file_pos = file_pos;
670 sc->length = num;
671 sc->lru = 0;
672 sc->id = file_sprite_id;
673 sc->type = type;
674 sc->warned = false;
675 sc->control_flags = control_flags;
676
677 return true;
678}
679
680
681void DupSprite(SpriteID old_spr, SpriteID new_spr)
682{
683 SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
684 SpriteCache *scold = GetSpriteCache(old_spr);
685
686 scnew->file = scold->file;
687 scnew->file_pos = scold->file_pos;
688 scnew->ClearSpriteData();
689 scnew->id = scold->id;
690 scnew->type = scold->type;
691 scnew->warned = false;
692 scnew->control_flags = scold->control_flags;
693}
694
701static void DeleteEntriesFromSpriteCache(size_t to_remove)
702{
703 const size_t initial_in_use = _spritecache_bytes_used;
704
705 struct SpriteInfo {
706 uint32_t lru;
707 SpriteID id;
708 size_t size;
709
710 auto operator<=>(const SpriteInfo &other) const noexcept
711 {
712 return this->lru <=> other.lru;
713 }
714 };
715
716 std::vector<SpriteInfo> candidates; // max heap, ordered by LRU
717 size_t candidate_bytes = 0; // total bytes that would be released when clearing all sprites in candidates
718
719 auto push = [&](SpriteInfo info) {
720 candidates.push_back(info);
721 std::push_heap(candidates.begin(), candidates.end());
722 candidate_bytes += info.size;
723 };
724
725 auto pop = [&]() {
726 candidate_bytes -= candidates.front().size;
727 std::pop_heap(candidates.begin(), candidates.end());
728 candidates.pop_back();
729 };
730
731 SpriteID i = 0;
732 for (; i != static_cast<SpriteID>(_spritecache.size()) && candidate_bytes < to_remove; i++) {
733 const SpriteCache *sc = GetSpriteCache(i);
734 if (sc->ptr != nullptr) {
735 push({ sc->lru, i, sc->length });
736 if (candidate_bytes >= to_remove) break;
737 }
738 }
739 /* candidates now contains enough bytes to meet to_remove.
740 * only sprites with LRU values <= the maximum (i.e. the top of the heap) need to be considered */
741 for (; i != static_cast<SpriteID>(_spritecache.size()); i++) {
742 const SpriteCache *sc = GetSpriteCache(i);
743 if (sc->ptr != nullptr && sc->lru <= candidates.front().lru) {
744 push({ sc->lru, i, sc->length });
745 while (!candidates.empty() && candidate_bytes - candidates.front().size >= to_remove) {
746 pop();
747 }
748 }
749 }
750
751 for (const auto &it : candidates) {
752 GetSpriteCache(it.id)->ClearSpriteData();
753 }
754
755 Debug(Facility::Sprite, Severity::Notice, "DeleteEntriesFromSpriteCache, deleted: {}, freed: {}, in use: {} --> {}, requested: {}",
756 candidates.size(), candidate_bytes, initial_in_use, _spritecache_bytes_used, to_remove);
757}
758
759void IncreaseSpriteLRU()
760{
762 uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
763 if (_spritecache_bytes_used > target_size) {
764 DeleteEntriesFromSpriteCache(_spritecache_bytes_used - target_size + 512 * 1024);
765 }
766
767 if (_sprite_lru_counter >= 0xC0000000) {
768 Debug(Facility::Sprite, Severity::Notice, "Fixing lru {}, inuse={}", _sprite_lru_counter, _spritecache_bytes_used);
769
770 for (SpriteCache &sc : _spritecache) {
771 if (sc.ptr != nullptr) {
772 if (sc.lru > 0x80000000) {
773 sc.lru -= 0x80000000;
774 } else {
775 sc.lru = 0;
776 }
777 }
778 }
779 _sprite_lru_counter -= 0x80000000;
780 }
781}
782
783void SpriteCache::ClearSpriteData()
784{
785 _spritecache_bytes_used -= this->length;
786 this->ptr.reset();
787}
788
790{
791 this->data = std::make_unique<std::byte[]>(size);
792 this->size = size;
793 return this->data.get();
794}
795
807static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, SpriteAllocator *allocator, SpriteEncoder *encoder)
808{
809 static const std::string_view sprite_types[] = {
810 "normal", // SpriteType::Normal
811 "map generator", // SpriteType::MapGen
812 "character", // SpriteType::Font
813 "recolour", // SpriteType::Recolour
814 };
815
816 SpriteType available = sc->type;
817 if (requested == SpriteType::Font && available == SpriteType::Normal) {
818 if (sc->ptr == nullptr) sc->type = SpriteType::Font;
819 return GetRawSprite(sprite, sc->type, allocator, encoder);
820 }
821
822 Severity warning_level = sc->warned ? Severity::Debug2 : Severity::Critical;
823 sc->warned = true;
824 Debug(Facility::Sprite, warning_level, "Tried to load {} sprite #{} as a {} sprite. Probable cause: NewGRF interference", sprite_types[static_cast<uint8_t>(available)], sprite, sprite_types[static_cast<uint8_t>(requested)]);
825
826 switch (requested) {
828 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?");
829 [[fallthrough]];
830 case SpriteType::Font:
831 return GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, allocator, encoder);
833 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?");
834 return GetRawSprite(PALETTE_TO_DARK_BLUE, SpriteType::Recolour, allocator, encoder);
836 /* this shouldn't happen, overriding of SpriteType::MapGen sprites is checked in LoadNextSprite()
837 * (the only case the check fails is when these sprites weren't even loaded...) */
838 default:
839 NOT_REACHED();
840 }
841}
842
852void *GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
853{
854 assert(type != SpriteType::MapGen || IsMapgenSpriteID(sprite));
855 assert(type < SpriteType::Invalid);
856
857 if (!SpriteExists(sprite)) {
858 Debug(Facility::Sprite, Severity::Error, "Tried to load non-existing sprite #{}. Probable cause: Wrong/missing NewGRFs", sprite);
859
860 /* SPR_IMG_QUERY is a BIG FAT RED ? */
861 sprite = SPR_IMG_QUERY;
862 }
863
864 SpriteCache *sc = GetSpriteCache(sprite);
865
866 if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator, encoder);
867
868 if (allocator == nullptr) {
869 assert(encoder == nullptr);
870 /* Load sprite into/from spritecache */
871
872 /* Update LRU */
873 sc->lru = ++_sprite_lru_counter;
874
875 /* Load the sprite, if it is not loaded, yet */
876 if (sc->ptr == nullptr) {
877 UniquePtrSpriteAllocator cache_allocator;
878 if (sc->type == SpriteType::Recolour) {
879 ReadRecolourSprite(*sc->file, sc->file_pos, sc->length, cache_allocator);
880 } else {
881 ReadSprite(sc, sprite, type, cache_allocator, nullptr);
882 }
883 sc->ptr = std::move(cache_allocator.data);
884 sc->length = static_cast<uint32_t>(cache_allocator.size);
885 _spritecache_bytes_used += sc->length;
886 }
887
888 return static_cast<void *>(sc->ptr.get());
889 } else {
890 assert(encoder != nullptr);
891 /* Do not use the spritecache, but a different allocator. */
892 return ReadSprite(sc, sprite, type, *allocator, encoder);
893 }
894}
895
896void GfxInitSpriteMem()
897{
898 /* Reset the spritecache 'pool' */
899 _spritecache.clear();
900 _spritecache.shrink_to_fit();
901
902 _sprite_files.clear();
903 _spritecache_bytes_used = 0;
904}
905
911{
912 /* Clear sprite ptr for all cached items */
913 for (SpriteCache &sc : _spritecache) {
914 if (sc.ptr != nullptr) sc.ClearSpriteData();
915 }
916
918}
919
925{
926 /* Clear sprite ptr for all cached font items */
927 for (SpriteCache &sc : _spritecache) {
928 if (sc.type == SpriteType::Font && sc.ptr != nullptr) sc.ClearSpriteData();
929 }
930}
931
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr bool None() const
Test if none of the values are set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:139
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
Iterate a range of enum values.
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
Map zoom level to data.
Interface for something that can encode a sprite.
virtual bool Is32BppSupported()=0
Can the sprite encoder make use of RGBA sprites?
virtual uint GetSpriteAlignment()
Get the value which the height and width on a sprite have to be aligned by.
virtual Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
RandomAccessFile with some extra information specific for sprite files.
bool NeedsPaletteRemap() const
Whether a palette remap is needed when loading sprites from this file.
uint8_t GetContainerVersion() const
Get the version number of container type used by the file.
void SeekToBegin()
Seek to the begin of the content, i.e.
Sprite loader for graphics coming from a (New)GRF.
Definition grf.hpp:16
ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Definition grf.cpp:359
Sprite loader for converting graphics coming from another source.
Definition makeindexed.h:16
ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
SpriteCollMap< Sprite > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
SpriteAllocator that allocates memory via a unique_ptr array.
Definition spritecache.h:20
void * AllocatePtr(size_t size) override
Allocate memory for a sprite.
virtual void ClearSystemSprites()
Clear all cached sprites.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Sprite
Sprite message facility.
Definition debug_type.h:34
Severity
Debug message severity levels.
Definition debug_type.h:14
@ Notice
Notice.
Definition debug_type.h:18
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
@ Debug2
Debug #2 - Low level debug messages.
Definition debug_type.h:21
@ Info
Info, information you might care about.
Definition debug_type.h:19
@ Error
Error, but we are recovering.
Definition debug_type.h:16
@ Trace3
Trace information #3.
Definition debug_type.h:24
Error reporting related functions.
Factory to 'query' all available blitters.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped).
Definition gfx.cpp:58
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:404
@ Recolour
Recolour sprite.
Definition gfx_type.h:408
@ Font
A sprite used for fonts.
Definition gfx_type.h:407
@ MapGen
Special sprite for the map generator.
Definition gfx_type.h:406
@ Invalid
Pseudosprite or other unusable sprite, used only internally.
Definition gfx_type.h:409
@ Normal
The most basic (normal) sprite.
Definition gfx_type.h:405
Base for reading sprites from (New)GRFs.
Base for converting sprites from another source from 32bpp RGBA to indexed 8bpp.
Integer math functions.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition math_func.hpp:37
static auto operator<=>(const GUIBadgeClasses::Element &a, const GUIBadgeClasses::Element &b) noexcept
Compare two GUIBadgeClasses::Elements.
Translation tables from one GRF to another GRF.
static const uint8_t _palmap_d2w[]
Converting from the DOS palette to the Windows palette.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
static void DeleteEntriesFromSpriteCache(size_t to_remove)
Delete entries from the sprite cache to remove the requested number of bytes.
static std::map< uint32_t, GrfSpriteOffset > _grf_sprite_offsets
Map from sprite numbers to position in the GRF file.
SpriteType GetSpriteType(SpriteID sprite)
Get the sprite type of a given sprite.
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
static SpriteFile * GetCachedSpriteFileByName(const std::string &filename)
Get the cached SpriteFile given the name of the file.
uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file in a range of SpriteIDs.
static void * ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
Read a sprite from disk.
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
SpriteID GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
SpriteFile & OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
Open/get the SpriteFile that is cached for use in the sprite cache.
uint _sprite_cache_size
Default of 4MB spritecache.
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
Reads a sprite (from disk or sprite cache).
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles()
Get the list of cached SpriteFiles.
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
size_t GetGRFSpriteOffset(uint32_t id)
Get the file offset for a specific sprite in the sprite section of a GRF.
static void * ReadRecolourSprite(SpriteFile &file, size_t file_pos, uint num, SpriteAllocator &allocator)
Load a recolour sprite into memory.
bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
Skip the given amount of sprite graphics data.
uint32_t GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
void ReadGRFSpriteOffsets(SpriteFile &file)
Parse the sprite section of GRFs.
static void * HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, SpriteAllocator *allocator, SpriteEncoder *encoder)
Handles the case when a sprite of different type is requested than is present in the SpriteCache.
Functions to cache sprites in memory.
Internal functions to cache sprites in memory.
@ AllowZoomMin1x32bpp
Allow use of sprite min zoom setting at 1x in 32bpp mode.
@ AllowZoomMin2x32bpp
Allow use of sprite min zoom setting at 2x in 32bpp mode.
@ AllowZoomMin1xPal
Allow use of sprite min zoom setting at 1x in palette mode.
@ AllowZoomMin2xPal
Allow use of sprite min zoom setting at 2x in palette mode.
EnumBitSet< SpriteCacheCtrlFlag, uint8_t > SpriteCacheCtrlFlags
Bitset of SpriteCacheCtrlFlag elements.
@ Palette
Sprite has palette data.
EnumBitSet< SpriteComponent, uint8_t, SpriteComponent::End > SpriteComponents
Bitset of SpriteComponent elements.
This file contains all sprite-related enums and defines.
static constexpr uint32_t MAX_SPRITES
Masks needed for sprite operations.
Definition sprites.h:1742
static const PaletteID PALETTE_TO_DARK_BLUE
Definition sprites.h:1762
static const SpriteID SPR_IMG_QUERY
Definition sprites.h:1275
Definition of base types and functions in a cross-platform compatible way.
uint32_t length
Length of sprite data.
bool warned
True iff the user has been warned about incorrect use of this sprite.
SpriteCacheCtrlFlags control_flags
Control flags, see SpriteCacheCtrlFlags.
SpriteType type
In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as recolour spr...
SpriteFile * file
The file the sprite in this entry can be found in.
Definition of a common pixel in OpenTTD's realm.
uint8_t m
Remap-channel.
Structure for passing information from the sprite loader to the blitter.
static SpriteCollMap< ReusableBuffer< SpriteLoader::CommonPixel > > buffer
Allocated memory to pass sprite data around.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
uint16_t width
Width of the sprite.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
uint16_t width
Width of the sprite.
uint16_t height
Height of the sprite.
int16_t y_offs
Number of pixels to shift the sprite downwards.
std::byte data[]
Sprite data.
int16_t x_offs
Number of pixels to shift the sprite to the right.
Base of all video drivers.
Functions related to zooming.
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
int AdjustByZoom(int value, int zoom)
Adjust by zoom level; zoom < 0 shifts right, zoom >= 0 shifts left.
Definition zoom_func.h:45
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:34
EnumBitSet< ZoomLevel, uint8_t > ZoomLevels
Bitset of ZoomLevel elements.
Definition zoom_type.h:51
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
@ In2x
Zoomed 2 times in.
Definition zoom_type.h:25
@ Max
Maximum zoom level.
Definition zoom_type.h:30
@ Min
Minimum zoom level.
Definition zoom_type.h:23
@ End
End for iteration.
Definition zoom_type.h:31
@ Normal
The normal zoom level.
Definition zoom_type.h:26