mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-09-23 14:53:52 +00:00
Allocator refactor
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "CoreDefs.h"
|
||||
#include "GpIOStream.h"
|
||||
#include "IGpAllocator.h"
|
||||
#include "IGpFont.h"
|
||||
#include "IGpFontRenderedGlyph.h"
|
||||
#include "GpRenderedGlyphMetrics.h"
|
||||
@@ -17,6 +18,8 @@
|
||||
#include <new>
|
||||
#include <assert.h>
|
||||
|
||||
struct IGpAllocator;
|
||||
|
||||
class GpFontRenderedGlyph_FreeType2 final : public IGpFontRenderedGlyph
|
||||
{
|
||||
public:
|
||||
@@ -24,14 +27,15 @@ public:
|
||||
const void *GetData() const override;
|
||||
void Destroy() override;
|
||||
|
||||
static GpFontRenderedGlyph_FreeType2 *Create(size_t dataSize, const GpRenderedGlyphMetrics &metrics);
|
||||
static GpFontRenderedGlyph_FreeType2 *Create(IGpAllocator *alloc, size_t dataSize, const GpRenderedGlyphMetrics &metrics);
|
||||
|
||||
void *GetMutableData();
|
||||
|
||||
private:
|
||||
GpFontRenderedGlyph_FreeType2(void *data, const GpRenderedGlyphMetrics &metrics);
|
||||
GpFontRenderedGlyph_FreeType2(IGpAllocator *alloc, void *data, const GpRenderedGlyphMetrics &metrics);
|
||||
~GpFontRenderedGlyph_FreeType2();
|
||||
|
||||
IGpAllocator *m_alloc;
|
||||
void *m_data;
|
||||
GpRenderedGlyphMetrics m_metrics;
|
||||
};
|
||||
@@ -44,17 +48,18 @@ public:
|
||||
bool GetLineSpacing(unsigned int size, int32_t &outSpacing) override;
|
||||
bool SupportScaling() const override;
|
||||
|
||||
static GpFont_FreeType2 *Create(const FT_StreamRec_ &streamRec, GpIOStream *stream);
|
||||
static GpFont_FreeType2 *Create(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream);
|
||||
|
||||
bool FTLoad(const FT_Library &library, int typeFaceIndex);
|
||||
|
||||
private:
|
||||
explicit GpFont_FreeType2(const FT_StreamRec_ &streamRec, GpIOStream *stream);
|
||||
GpFont_FreeType2(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream);
|
||||
~GpFont_FreeType2();
|
||||
|
||||
FT_StreamRec_ m_ftStream;
|
||||
FT_Face m_face;
|
||||
GpIOStream *m_stream;
|
||||
IGpAllocator *m_alloc;
|
||||
unsigned int m_currentSize;
|
||||
};
|
||||
|
||||
@@ -70,20 +75,21 @@ const void *GpFontRenderedGlyph_FreeType2::GetData() const
|
||||
|
||||
void GpFontRenderedGlyph_FreeType2::Destroy()
|
||||
{
|
||||
IGpAllocator *alloc = m_alloc;
|
||||
this->~GpFontRenderedGlyph_FreeType2();
|
||||
free(this);
|
||||
alloc->Release(this);
|
||||
}
|
||||
|
||||
GpFontRenderedGlyph_FreeType2 *GpFontRenderedGlyph_FreeType2::Create(size_t dataSize, const GpRenderedGlyphMetrics &metrics)
|
||||
GpFontRenderedGlyph_FreeType2 *GpFontRenderedGlyph_FreeType2::Create(IGpAllocator *alloc, size_t dataSize, const GpRenderedGlyphMetrics &metrics)
|
||||
{
|
||||
size_t alignedPrefixSize = (sizeof(GpFontRenderedGlyph_FreeType2) + GP_SYSTEM_MEMORY_ALIGNMENT - 1);
|
||||
alignedPrefixSize -= alignedPrefixSize % GP_SYSTEM_MEMORY_ALIGNMENT;
|
||||
|
||||
void *storage = malloc(alignedPrefixSize + dataSize);
|
||||
void *storage = alloc->Alloc(alignedPrefixSize + dataSize);
|
||||
if (!storage)
|
||||
return nullptr;
|
||||
|
||||
return new (storage) GpFontRenderedGlyph_FreeType2(static_cast<uint8_t*>(storage) + alignedPrefixSize, metrics);
|
||||
return new (storage) GpFontRenderedGlyph_FreeType2(alloc, static_cast<uint8_t*>(storage) + alignedPrefixSize, metrics);
|
||||
}
|
||||
|
||||
void *GpFontRenderedGlyph_FreeType2::GetMutableData()
|
||||
@@ -92,9 +98,10 @@ void *GpFontRenderedGlyph_FreeType2::GetMutableData()
|
||||
}
|
||||
|
||||
|
||||
GpFontRenderedGlyph_FreeType2::GpFontRenderedGlyph_FreeType2(void *data, const GpRenderedGlyphMetrics &metrics)
|
||||
GpFontRenderedGlyph_FreeType2::GpFontRenderedGlyph_FreeType2(IGpAllocator *alloc, void *data, const GpRenderedGlyphMetrics &metrics)
|
||||
: m_metrics(metrics)
|
||||
, m_data(data)
|
||||
, m_alloc(alloc)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -104,8 +111,9 @@ GpFontRenderedGlyph_FreeType2::~GpFontRenderedGlyph_FreeType2()
|
||||
|
||||
void GpFont_FreeType2::Destroy()
|
||||
{
|
||||
IGpAllocator *alloc = m_alloc;
|
||||
this->~GpFont_FreeType2();
|
||||
free(this);
|
||||
alloc->Release(this);
|
||||
}
|
||||
|
||||
IGpFontRenderedGlyph *GpFont_FreeType2::Render(uint32_t unicodeCodePoint, unsigned int size, unsigned int xScale, unsigned int yScale, bool aa)
|
||||
@@ -190,7 +198,7 @@ IGpFontRenderedGlyph *GpFont_FreeType2::Render(uint32_t unicodeCodePoint, unsign
|
||||
|
||||
metrics.m_glyphDataPitch = pitchRequired;
|
||||
|
||||
GpFontRenderedGlyph_FreeType2 *renderedGlyph = GpFontRenderedGlyph_FreeType2::Create(glyphDataSize, metrics);
|
||||
GpFontRenderedGlyph_FreeType2 *renderedGlyph = GpFontRenderedGlyph_FreeType2::Create(m_alloc, glyphDataSize, metrics);
|
||||
if (!renderedGlyph)
|
||||
return nullptr;
|
||||
|
||||
@@ -263,13 +271,13 @@ bool GpFont_FreeType2::SupportScaling() const
|
||||
return true;
|
||||
}
|
||||
|
||||
GpFont_FreeType2 *GpFont_FreeType2::Create(const FT_StreamRec_ &streamRec, GpIOStream *stream)
|
||||
GpFont_FreeType2 *GpFont_FreeType2::Create(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream)
|
||||
{
|
||||
void *storage = malloc(sizeof(GpFont_FreeType2));
|
||||
void *storage = alloc->Alloc(sizeof(GpFont_FreeType2));
|
||||
if (!storage)
|
||||
return nullptr;
|
||||
|
||||
return new (storage) GpFont_FreeType2(streamRec, stream);
|
||||
return new (storage) GpFont_FreeType2(alloc, streamRec, stream);
|
||||
}
|
||||
|
||||
bool GpFont_FreeType2::FTLoad(const FT_Library &library, int typeFaceIndex)
|
||||
@@ -288,11 +296,12 @@ bool GpFont_FreeType2::FTLoad(const FT_Library &library, int typeFaceIndex)
|
||||
return true;
|
||||
}
|
||||
|
||||
GpFont_FreeType2::GpFont_FreeType2(const FT_StreamRec_ &streamRec, GpIOStream *stream)
|
||||
GpFont_FreeType2::GpFont_FreeType2(IGpAllocator *alloc, const FT_StreamRec_ &streamRec, GpIOStream *stream)
|
||||
: m_face(nullptr)
|
||||
, m_ftStream(streamRec)
|
||||
, m_stream(stream)
|
||||
, m_currentSize(0)
|
||||
, m_alloc(alloc)
|
||||
{
|
||||
assert(stream);
|
||||
}
|
||||
@@ -305,13 +314,13 @@ GpFont_FreeType2::~GpFont_FreeType2()
|
||||
m_stream->Close();
|
||||
}
|
||||
|
||||
GpFontHandler_FreeType2 *GpFontHandler_FreeType2::Create()
|
||||
GpFontHandler_FreeType2 *GpFontHandler_FreeType2::Create(IGpAllocator *alloc)
|
||||
{
|
||||
void *storage = malloc(sizeof(GpFontHandler_FreeType2));
|
||||
void *storage = alloc->Alloc(sizeof(GpFontHandler_FreeType2));
|
||||
if (!storage)
|
||||
return nullptr;
|
||||
|
||||
GpFontHandler_FreeType2 *fh = new (storage) GpFontHandler_FreeType2();
|
||||
GpFontHandler_FreeType2 *fh = new (storage) GpFontHandler_FreeType2(alloc);
|
||||
if (!fh->Init())
|
||||
{
|
||||
fh->Shutdown();
|
||||
@@ -331,7 +340,7 @@ IGpFont *GpFontHandler_FreeType2::LoadFont(GpIOStream *stream, int typeFaceIndex
|
||||
ftStream.read = FTStreamIo;
|
||||
ftStream.close = FTStreamClose;
|
||||
|
||||
GpFont_FreeType2 *font = GpFont_FreeType2::Create(ftStream, stream);
|
||||
GpFont_FreeType2 *font = GpFont_FreeType2::Create(m_alloc, ftStream, stream);
|
||||
if (!font)
|
||||
{
|
||||
stream->Close();
|
||||
@@ -354,14 +363,16 @@ bool GpFontHandler_FreeType2::KeepStreamOpen() const
|
||||
|
||||
void GpFontHandler_FreeType2::Shutdown()
|
||||
{
|
||||
IGpAllocator *alloc = m_alloc;
|
||||
this->~GpFontHandler_FreeType2();
|
||||
free(this);
|
||||
alloc->Release(this);
|
||||
}
|
||||
|
||||
GpFontHandler_FreeType2::GpFontHandler_FreeType2()
|
||||
GpFontHandler_FreeType2::GpFontHandler_FreeType2(IGpAllocator *alloc)
|
||||
: m_ftIsInitialized(false)
|
||||
, m_library(nullptr)
|
||||
, m_currentSize(0)
|
||||
, m_alloc(alloc)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -411,18 +422,18 @@ void GpFontHandler_FreeType2::FTStreamClose(FT_Stream stream)
|
||||
|
||||
void *GpFontHandler_FreeType2::FTAlloc(long size)
|
||||
{
|
||||
return malloc(static_cast<size_t>(size));
|
||||
return m_alloc->Alloc(static_cast<size_t>(size));
|
||||
}
|
||||
|
||||
void GpFontHandler_FreeType2::FTFree(void* block)
|
||||
{
|
||||
free(block);
|
||||
m_alloc->Release(block);
|
||||
}
|
||||
|
||||
void *GpFontHandler_FreeType2::FTRealloc(long curSize, long newSize, void *block)
|
||||
{
|
||||
(void)curSize;
|
||||
return realloc(block, static_cast<size_t>(newSize));
|
||||
return m_alloc->Realloc(block, static_cast<size_t>(newSize));
|
||||
}
|
||||
|
||||
bool GpFontHandler_FreeType2::Init()
|
||||
@@ -448,5 +459,5 @@ __declspec(dllexport)
|
||||
#endif
|
||||
IGpFontHandler *GpDriver_CreateFontHandler_FreeType2(const GpFontHandlerProperties &properties)
|
||||
{
|
||||
return GpFontHandler_FreeType2::Create();
|
||||
return GpFontHandler_FreeType2::Create(properties.m_alloc);
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
class GpIOStream;
|
||||
struct IGpAllocator;
|
||||
|
||||
namespace PortabilityLayer
|
||||
{
|
||||
@@ -21,10 +22,10 @@ public:
|
||||
|
||||
bool KeepStreamOpen() const override;
|
||||
|
||||
static GpFontHandler_FreeType2 *Create();
|
||||
static GpFontHandler_FreeType2 *Create(IGpAllocator *alloc);
|
||||
|
||||
private:
|
||||
GpFontHandler_FreeType2();
|
||||
explicit GpFontHandler_FreeType2(IGpAllocator *alloc);
|
||||
~GpFontHandler_FreeType2();
|
||||
|
||||
static void *FTAllocThunk(FT_Memory memory, long size);
|
||||
@@ -44,4 +45,5 @@ private:
|
||||
FT_Library m_library;
|
||||
unsigned int m_currentSize;
|
||||
bool m_ftIsInitialized;
|
||||
IGpAllocator *m_alloc;
|
||||
};
|
||||
|
Reference in New Issue
Block a user