Changeset - r24900:959a621b6aa9
[Not reviewed]
master
0 6 0
Michael Lutz - 3 years ago 2021-01-16 15:43:31
michi@icosahedron.de
Codechange: Make the simple Malloc sprite allocator globally usable.
6 files changed with 13 insertions and 11 deletions:
0 comments (0 inline, 0 general)
src/fontcache.cpp
Show inline comments
 
@@ -256,53 +256,48 @@ TrueTypeFontCache::GlyphEntry *TrueTypeF
 
{
 
	if (this->glyph_to_sprite == nullptr) return nullptr;
 
	if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) return nullptr;
 
	return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
 
}
 

	
 
void TrueTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
 
{
 
	if (this->glyph_to_sprite == nullptr) {
 
		DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
 
		this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
 
	}
 

	
 
	if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
 
		DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
 
		this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
 
	}
 

	
 
	DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
 
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
 
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
 
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
 
}
 

	
 
void *AllocateFont(size_t size)
 
{
 
	return MallocT<byte>(size);
 
}
 

	
 

	
 
/* Check if a glyph should be rendered with anti-aliasing. */
 
static bool GetFontAAState(FontSize size)
 
{
 
	/* AA is only supported for 32 bpp */
 
	if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
 

	
 
	switch (size) {
 
		default: NOT_REACHED();
 
		case FS_NORMAL: return _freetype.medium.aa;
 
		case FS_SMALL:  return _freetype.small.aa;
 
		case FS_LARGE:  return _freetype.large.aa;
 
		case FS_MONO:   return _freetype.mono.aa;
 
	}
 
}
 

	
 
bool TrueTypeFontCache::GetDrawGlyphShadow()
 
{
 
	return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
 
}
 

	
 
uint TrueTypeFontCache::GetGlyphWidth(GlyphID key)
 
{
 
	if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
 
@@ -334,49 +329,49 @@ const Sprite *TrueTypeFontCache::GetGlyp
 
			static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
 
				CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
 
				CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
 
				CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
 
				CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
 
				CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
 
				CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
 
				CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
 
				CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
 
				CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
 
				CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
 
			};
 
#undef CPSET
 
#undef CP___
 
			static const SpriteLoader::Sprite builtin_questionmark = {
 
				10, // height
 
				8,  // width
 
				0,  // x_offs
 
				0,  // y_offs
 
				ST_FONT,
 
				SCC_PAL,
 
				builtin_questionmark_data
 
			};
 

	
 
			Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
 
			Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, SimpleSpriteAlloc);
 
			assert(spr != nullptr);
 
			GlyphEntry new_glyph;
 
			new_glyph.sprite = spr;
 
			new_glyph.width  = spr->width + (this->fs != FS_NORMAL);
 
			this->SetGlyphPtr(key, &new_glyph, false);
 
			return new_glyph.sprite;
 
		} else {
 
			/* Use '?' for missing characters. */
 
			this->GetGlyph(question_glyph);
 
			glyph = this->GetGlyphPtr(question_glyph);
 
			this->SetGlyphPtr(key, glyph, true);
 
			return glyph->sprite;
 
		}
 
	}
 

	
 
	return this->InternalGetGlyph(key, GetFontAAState(this->fs));
 
}
 

	
 
const void *TrueTypeFontCache::GetFontTable(uint32 tag, size_t &length)
 
{
 
	const FontTable::iterator iter = this->font_tables.Find(tag);
 
	if (iter != this->font_tables.data() + this->font_tables.size()) {
 
		length = iter->second.first;
 
		return iter->second.second;
 
@@ -622,49 +617,49 @@ const Sprite *FreeTypeFontCache::Interna
 
	sprite.y_offs = this->ascender - slot->bitmap_top;
 

	
 
	/* Draw shadow for medium size */
 
	if (this->fs == FS_NORMAL && !aa) {
 
		for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
 
			for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
 
				if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
 
					sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
 
					sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
 
				}
 
			}
 
		}
 
	}
 

	
 
	for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
 
		for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
 
			if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
 
				sprite.data[x + y * sprite.width].m = FACE_COLOUR;
 
				sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
 
			}
 
		}
 
	}
 

	
 
	GlyphEntry new_glyph;
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
 
	new_glyph.width  = slot->advance.x >> 6;
 

	
 
	this->SetGlyphPtr(key, &new_glyph);
 

	
 
	return new_glyph.sprite;
 
}
 

	
 

	
 
GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
 
{
 
	assert(IsPrintable(key));
 

	
 
	if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
 
		return this->parent->MapCharToGlyph(key);
 
	}
 

	
 
	return FT_Get_Char_Index(this->face, key);
 
}
 

	
 
const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag, size_t &length)
 
{
 
	FT_ULong len = 0;
 
	FT_Byte *result = nullptr;
 

	
src/fontcache_internal.h
Show inline comments
 
@@ -53,27 +53,25 @@ protected:
 
	 */
 
	GlyphEntry **glyph_to_sprite;
 

	
 
	GlyphEntry *GetGlyphPtr(GlyphID key);
 
	void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
 

	
 
	virtual const void *InternalGetFontTable(uint32 tag, size_t &length) = 0;
 
	virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
 

	
 
public:
 
	TrueTypeFontCache(FontSize fs, int pixels);
 
	virtual ~TrueTypeFontCache();
 
	int GetFontSize() const override { return this->used_size; }
 
	SpriteID GetUnicodeGlyph(WChar key) override { return this->parent->GetUnicodeGlyph(key); }
 
	void SetUnicodeGlyph(WChar key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
 
	void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
 
	const Sprite *GetGlyph(GlyphID key) override;
 
	const void *GetFontTable(uint32 tag, size_t &length) override;
 
	void ClearFontCache() override;
 
	uint GetGlyphWidth(GlyphID key) override;
 
	bool GetDrawGlyphShadow() override;
 
	bool IsBuiltInFont() override { return false; }
 
};
 

	
 
void *AllocateFont(size_t size);
 

	
 
#endif /* FONTCACHE_INTERNAL_H */
src/os/macosx/font_osx.cpp
Show inline comments
 
@@ -312,49 +312,49 @@ const Sprite *CoreTextFontCache::Interna
 
		/* Draw shadow for medium size. */
 
		if (this->fs == FS_NORMAL && !use_aa) {
 
			for (uint y = 0; y < bb_height; y++) {
 
				for (uint x = 0; x < bb_width; x++) {
 
					if (bmp[y * pitch + x] > 0) {
 
						sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
 
						sprite.data[1 + x + (1 + y) * sprite.width].a = use_aa ? bmp[x + y * pitch] : 0xFF;
 
					}
 
				}
 
			}
 
		}
 

	
 
		/* Extract pixel data. */
 
		for (uint y = 0; y < bb_height; y++) {
 
			for (uint x = 0; x < bb_width; x++) {
 
				if (bmp[y * pitch + x] > 0) {
 
					sprite.data[x + y * sprite.width].m = FACE_COLOUR;
 
					sprite.data[x + y * sprite.width].a = use_aa ? bmp[x + y * pitch] : 0xFF;
 
				}
 
			}
 
		}
 
	}
 

	
 
	GlyphEntry new_glyph;
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
 
	new_glyph.width = (byte)std::round(CTFontGetAdvancesForGlyphs(this->font.get(), kCTFontOrientationDefault, &glyph, nullptr, 1));
 
	this->SetGlyphPtr(key, &new_glyph);
 

	
 
	return new_glyph.sprite;
 
}
 

	
 
/**
 
 * Loads the TrueType font.
 
 * If a CoreText font description is present, e.g. from the automatic font
 
 * fallback search, use it. Otherwise, try to resolve it by font name.
 
 * @param fs The font size to load.
 
 */
 
void LoadCoreTextFont(FontSize fs)
 
{
 
	static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
 

	
 
	FreeTypeSubSetting *settings = nullptr;
 
	switch (fs) {
 
		default: NOT_REACHED();
 
		case FS_SMALL:  settings = &_freetype.small;  break;
 
		case FS_NORMAL: settings = &_freetype.medium; break;
 
		case FS_LARGE:  settings = &_freetype.large;  break;
 
		case FS_MONO:   settings = &_freetype.mono;   break;
 
	}
src/os/windows/font_win32.cpp
Show inline comments
 
@@ -511,49 +511,49 @@ void Win32FontCache::ClearFontCache()
 

	
 
		/* Draw shadow for medium size. */
 
		if (this->fs == FS_NORMAL && !aa) {
 
			for (uint y = 0; y < gm.gmBlackBoxY; y++) {
 
				for (uint x = 0; x < gm.gmBlackBoxX; x++) {
 
					if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
 
						sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
 
						sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
 
					}
 
				}
 
			}
 
		}
 

	
 
		for (uint y = 0; y < gm.gmBlackBoxY; y++) {
 
			for (uint x = 0; x < gm.gmBlackBoxX; x++) {
 
				if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
 
					sprite.data[x + y * sprite.width].m = FACE_COLOUR;
 
					sprite.data[x + y * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
 
				}
 
			}
 
		}
 
	}
 

	
 
	GlyphEntry new_glyph;
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
 
	new_glyph.width = gm.gmCellIncX;
 

	
 
	this->SetGlyphPtr(key, &new_glyph);
 

	
 
	return new_glyph.sprite;
 
}
 

	
 
/* virtual */ GlyphID Win32FontCache::MapCharToGlyph(WChar key)
 
{
 
	assert(IsPrintable(key));
 

	
 
	if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
 
		return this->parent->MapCharToGlyph(key);
 
	}
 

	
 
	/* Convert characters outside of the BMP into surrogate pairs. */
 
	WCHAR chars[2];
 
	if (key >= 0x010000U) {
 
		chars[0] = (WCHAR)(((key - 0x010000U) >> 10) + 0xD800);
 
		chars[1] = (WCHAR)(((key - 0x010000U) & 0x3FF) + 0xDC00);
 
	} else {
 
		chars[0] = (WCHAR)(key & 0xFFFF);
 
	}
 

	
src/spritecache.cpp
Show inline comments
 
@@ -788,48 +788,56 @@ static void *AllocSprite(size_t mem_req)
 

	
 
				/* Is the block exactly the size we need or
 
				 * big enough for an additional free block? */
 
				if (cur_size == mem_req ||
 
						cur_size >= mem_req + sizeof(MemBlock)) {
 
					/* Set size and in use */
 
					s->size = mem_req;
 

	
 
					/* Do we need to inject a free block too? */
 
					if (cur_size != mem_req) {
 
						NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
 
					}
 

	
 
					return s->data;
 
				}
 
			}
 
		}
 

	
 
		/* Reached sentinel, but no block found yet. Delete some old entry. */
 
		DeleteEntryFromSpriteCache();
 
	}
 
}
 

	
 
/**
 
 * Sprite allocator simply using malloc.
 
 */
 
void *SimpleSpriteAlloc(size_t size)
 
{
 
	return MallocT<byte>(size);
 
}
 

	
 
/**
 
 * Handles the case when a sprite of different type is requested than is present in the SpriteCache.
 
 * For ST_FONT sprites, it is normal. In other cases, default sprite is loaded instead.
 
 * @param sprite ID of loaded sprite
 
 * @param requested requested sprite type
 
 * @param sc the currently known sprite cache for the requested sprite
 
 * @return fallback sprite
 
 * @note this function will do usererror() in the case the fallback sprite isn't available
 
 */
 
static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
 
{
 
	static const char * const sprite_types[] = {
 
		"normal",        // ST_NORMAL
 
		"map generator", // ST_MAPGEN
 
		"character",     // ST_FONT
 
		"recolour",      // ST_RECOLOUR
 
	};
 

	
 
	SpriteType available = sc->type;
 
	if (requested == ST_FONT && available == ST_NORMAL) {
 
		if (sc->ptr == nullptr) sc->type = ST_FONT;
 
		return GetRawSprite(sprite, sc->type, allocator);
 
	}
 

	
 
	byte warning_level = sc->warned ? 6 : 0;
src/spritecache.h
Show inline comments
 
@@ -5,48 +5,49 @@
 
 * 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/>.
 
 */
 

	
 
/** @file spritecache.h Functions to cache sprites in memory. */
 

	
 
#ifndef SPRITECACHE_H
 
#define SPRITECACHE_H
 

	
 
#include "gfx_type.h"
 
#include "spriteloader/spriteloader.hpp"
 

	
 
/** Data structure describing a sprite. */
 
struct Sprite {
 
	uint16 height; ///< Height of the sprite.
 
	uint16 width;  ///< Width of the sprite.
 
	int16 x_offs;  ///< Number of pixels to shift the sprite to the right.
 
	int16 y_offs;  ///< Number of pixels to shift the sprite downwards.
 
	byte data[];   ///< Sprite data.
 
};
 

	
 
extern uint _sprite_cache_size;
 

	
 
typedef void *AllocatorProc(size_t size);
 

	
 
void *SimpleSpriteAlloc(size_t size);
 
void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator = nullptr, SpriteEncoder *encoder = nullptr);
 
bool SpriteExists(SpriteID sprite);
 

	
 
SpriteType GetSpriteType(SpriteID sprite);
 
uint GetOriginFileSlot(SpriteID sprite);
 
uint32 GetSpriteLocalID(SpriteID sprite);
 
uint GetSpriteCountForSlot(uint file_slot, SpriteID begin, SpriteID end);
 
uint GetMaxSpriteID();
 

	
 

	
 
static inline const Sprite *GetSprite(SpriteID sprite, SpriteType type)
 
{
 
	assert(type != ST_RECOLOUR);
 
	return (Sprite*)GetRawSprite(sprite, type);
 
}
 

	
 
static inline const byte *GetNonSprite(SpriteID sprite, SpriteType type)
 
{
 
	assert(type == ST_RECOLOUR);
 
	return (byte*)GetRawSprite(sprite, type);
 
}
 

	
 
void GfxInitSpriteMem();
 
void GfxClearSpriteCache();
0 comments (0 inline, 0 general)