Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/libs/prx/libSceFreeType/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void FinalizeFace(void* object) {
auto* host = static_cast<FT_Face>(object);
auto* face = static_cast<GuestFace*>(host->generic.data);
if (face->rec.generic.finalizer) face->rec.generic.finalizer(&face->rec);
face->finalized = true;
}

void PublishFace(GuestLibrary* library, std::unique_ptr<GuestFace> face, FT_Face host, GuestFaceRec** aface) {
Expand Down Expand Up @@ -144,10 +145,15 @@ int APS5_VABI FT_New_Memory_Face_nid_postfix(GuestLibrary* library, const unsign
return 0;
}

int APS5_VABI FT_Reference_Face_nid_postfix(GuestFaceRec* face) {
const GuestFace* owner = FaceOf(face);
return FT_Reference_Face(owner ? owner->host : nullptr);
}

int APS5_VABI FT_Done_Face_nid_postfix(GuestFaceRec* face) {
GuestFace* owner = FaceOf(face);
const FT_Error error = FT_Done_Face(owner ? owner->host : nullptr);
if (error == 0) owner->library->faces.remove_if([owner](const auto& entry) { return entry.get() == owner; });
if (error == 0 && owner->finalized) owner->library->faces.remove_if([owner](const auto& entry) { return entry.get() == owner; });
return error;
}

Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceFreeType/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ int APS5_VABI FT_Init_FreeType_nid_postfix(FreeType::GuestLibrary** alibrary);
int APS5_VABI FT_Done_FreeType_nid_postfix(FreeType::GuestLibrary* library);
int APS5_VABI FT_New_Face_nid_postfix(FreeType::GuestLibrary* library, const char* filepathname, std::int64_t faceIndex, FreeType::GuestFaceRec** aface);
int APS5_VABI FT_New_Memory_Face_nid_postfix(FreeType::GuestLibrary* library, const unsigned char* fileBase, std::int64_t fileSize, std::int64_t faceIndex, FreeType::GuestFaceRec** aface);
int APS5_VABI FT_Reference_Face_nid_postfix(FreeType::GuestFaceRec* face);
int APS5_VABI FT_Done_Face_nid_postfix(FreeType::GuestFaceRec* face);
int APS5_VABI FT_Set_Pixel_Sizes_nid_postfix(FreeType::GuestFaceRec* face, std::uint32_t pixelWidth, std::uint32_t pixelHeight);
int APS5_VABI FT_Set_Char_Size_nid_postfix(FreeType::GuestFaceRec* face, std::int64_t charWidth, std::int64_t charHeight, std::uint32_t horizontalResolution, std::uint32_t verticalResolution);
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceFreeType/include/GuestObjects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct GuestFace {
GuestSizeRec size{};
GuestLibrary* library = nullptr;
FT_Face host = nullptr;
bool finalized = false;
std::vector<unsigned char> bytes;
std::vector<GuestBitmapSize> fixedSizes;
std::vector<GuestCharMapRec> charmaps;
Expand Down
126 changes: 123 additions & 3 deletions core/libs/tests/GuestFreeType.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "prx/libSceFreeType/include/Api.hpp"
#include "FreeTypeFontFixture.hpp"
#include "prx/libSceFreeType/include/GuestFreeType.hpp"
#include "prx/libSceFreeType/include/GuestObjects.hpp"
#include "prx/libc/include/General.hpp"

#include <ft2build.h>
Expand Down Expand Up @@ -468,6 +468,7 @@ static void Trigonometry() {

struct Finalizers {
std::vector<int> calls;
bool reenter = false;
};

static std::vector<unsigned char> CidMetadataFont(bool keyed, const std::string& registry, const std::string& ordering,
Expand Down Expand Up @@ -639,7 +640,13 @@ static void NativeSizeFinalizer(void* object) {
static void NativeFaceFinalizer(void* object) {
auto* face = static_cast<FT_Face>(object);
Require(face->glyph == nullptr && face->size == nullptr);
static_cast<Finalizers*>(face->generic.data)->calls.push_back(3);
auto* events = static_cast<Finalizers*>(face->generic.data);
events->calls.push_back(3);
if (events->reenter) {
Require(FT_Reference_Face(face) == 0 && FT_Reference_Face(face) == 0);
Require(FT_Done_Face(face) == 0);
Require(face->glyph == nullptr && face->size == nullptr);
}
}

static void APS5_VABI GuestSlotFinalizer(void* object) {
Expand All @@ -657,7 +664,14 @@ static void APS5_VABI GuestSizeFinalizer(void* object) {
static void APS5_VABI GuestFaceFinalizer(void* object) {
auto* face = static_cast<GuestFaceRec*>(object);
Require(face->glyph == nullptr && face->size == nullptr);
static_cast<Finalizers*>(face->generic.data)->calls.push_back(3);
auto* events = static_cast<Finalizers*>(face->generic.data);
events->calls.push_back(3);
if (events->reenter) {
const auto* library = static_cast<const GuestFace*>(face->internal)->library;
Require(FT_Reference_Face_nid_postfix(face) == 0 && FT_Reference_Face_nid_postfix(face) == 0);
Require(FT_Done_Face_nid_postfix(face) == 0 && library->faces.size() == 1);
Require(face->glyph == nullptr && face->size == nullptr);
}
}

static void SetFinalizers(FT_Face face, Finalizers& events) {
Expand Down Expand Up @@ -762,6 +776,99 @@ static void CheckTransforms(const std::vector<unsigned char>& font) {
}

int main() {
{
const auto referenceFont = SquareGlyphFont('A', 700);
GuestLibrary* referenceLibrary = nullptr;
FT_Library oracle = nullptr;
Require(FT_Init_FreeType_nid_postfix(&referenceLibrary) == 0 && FT_Init_FreeType(&oracle) == 0);
auto* owners = referenceLibrary;
Require(FT_Reference_Face_nid_postfix(nullptr) == FT_Reference_Face(nullptr));
int references = 0;
int retained = 0;
int finalized = 0;
for (const unsigned depth : {1u, 2u, 3u, 17u}) {
GuestFaceRec* shared = nullptr;
FT_Face native = nullptr;
Require(FT_New_Memory_Face_nid_postfix(referenceLibrary, referenceFont.data(), static_cast<std::int64_t>(referenceFont.size()), 0, &shared) == 0);
Require(FT_New_Memory_Face(oracle, referenceFont.data(), static_cast<FT_Long>(referenceFont.size()), 0, &native) == 0);
Finalizers guestEvents;
Finalizers nativeEvents;
guestEvents.reenter = nativeEvents.reenter = depth == 3;
SetFinalizers(shared, guestEvents);
SetFinalizers(native, nativeEvents);
for (unsigned index = 1; index < depth; ++index) {
Require(FT_Reference_Face_nid_postfix(shared) == FT_Reference_Face(native));
++references;
}
for (unsigned index = 1; index < depth; ++index) {
Require(FT_Done_Face(native) == 0 && FT_Done_Face_nid_postfix(shared) == 0);
Require(owners->faces.size() == 1 && guestEvents.calls.empty() && nativeEvents.calls.empty());
Require(FT_Get_Char_Index_nid_postfix(shared, 'A') == FT_Get_Char_Index(native, 'A'));
Require(FT_Set_Pixel_Sizes_nid_postfix(shared, 0, 16 + index) == 0 && FT_Set_Pixel_Sizes(native, 0, 16 + index) == 0);
Require(FT_Load_Char_nid_postfix(shared, 'A', FT_LOAD_RENDER) == 0 && FT_Load_Char(native, 'A', FT_LOAD_RENDER) == 0);
SlotMatches(shared->glyph, native->glyph);
++retained;
}
Require(FT_Done_Face(native) == 0 && FT_Done_Face_nid_postfix(shared) == 0);
if (guestEvents.reenter) {
references += 2;
++retained;
}
Require(owners->faces.empty() && guestEvents.calls == nativeEvents.calls && guestEvents.calls == std::vector<int>({1, 2, 3}));
++finalized;
}
GuestFaceRec* plain = nullptr;
FT_Face nativePlain = nullptr;
Require(FT_New_Memory_Face_nid_postfix(referenceLibrary, referenceFont.data(), static_cast<std::int64_t>(referenceFont.size()), 0, &plain) == 0);
Require(FT_New_Memory_Face(oracle, referenceFont.data(), static_cast<FT_Long>(referenceFont.size()), 0, &nativePlain) == 0);
Require(FT_Reference_Face_nid_postfix(plain) == FT_Reference_Face(nativePlain));
++references;
Require(FT_Done_Face(nativePlain) == 0 && FT_Done_Face_nid_postfix(plain) == 0 && owners->faces.size() == 1);
++retained;
Require(FT_Done_Face(nativePlain) == 0 && FT_Done_Face_nid_postfix(plain) == 0 && owners->faces.empty());
++finalized;
GuestFaceRec* probe = nullptr;
FT_Face nativeProbe = nullptr;
Require(FT_New_Memory_Face_nid_postfix(referenceLibrary, referenceFont.data(), static_cast<std::int64_t>(referenceFont.size()), -1, &probe) == 0);
Require(FT_New_Memory_Face(oracle, referenceFont.data(), static_cast<FT_Long>(referenceFont.size()), -1, &nativeProbe) == 0);
Finalizers probeEvents;
Finalizers nativeProbeEvents;
probe->generic = {&probeEvents, GuestFaceFinalizer};
nativeProbe->generic = {&nativeProbeEvents, NativeFaceFinalizer};
Require(probe->glyph == nullptr && probe->size == nullptr);
Require(FT_Reference_Face_nid_postfix(probe) == FT_Reference_Face(nativeProbe));
++references;
Require(FT_Done_Face(nativeProbe) == 0 && FT_Done_Face_nid_postfix(probe) == 0 && owners->faces.size() == 1);
++retained;
Require(probeEvents.calls.empty() && nativeProbeEvents.calls.empty());
Require(FT_Done_Face(nativeProbe) == 0 && FT_Done_Face_nid_postfix(probe) == 0);
Require(owners->faces.empty() && probeEvents.calls == nativeProbeEvents.calls && probeEvents.calls == std::vector<int>({3}));
++finalized;
std::array<Finalizers, 2> shutdownEvents;
std::array<Finalizers, 2> nativeShutdownEvents;
std::array<GuestFaceRec*, 2> shutdown{};
std::array<FT_Face, 2> nativeShutdown{};
for (unsigned index = 0; index < shutdown.size(); ++index) {
Require(FT_New_Memory_Face_nid_postfix(referenceLibrary, referenceFont.data(), static_cast<std::int64_t>(referenceFont.size()), 0, &shutdown[index]) == 0);
Require(FT_New_Memory_Face(oracle, referenceFont.data(), static_cast<FT_Long>(referenceFont.size()), 0, &nativeShutdown[index]) == 0);
SetFinalizers(shutdown[index], shutdownEvents[index]);
SetFinalizers(nativeShutdown[index], nativeShutdownEvents[index]);
for (unsigned reference = 0; reference < 3 - index; ++reference) {
Require(FT_Reference_Face_nid_postfix(shutdown[index]) == FT_Reference_Face(nativeShutdown[index]));
++references;
}
Require(FT_Done_Face(nativeShutdown[index]) == 0 && FT_Done_Face_nid_postfix(shutdown[index]) == 0);
Require(owners->faces.size() == index + 1 && shutdownEvents[index].calls.empty());
++retained;
}
Require(FT_Done_FreeType(oracle) == 0 && FT_Done_FreeType_nid_postfix(referenceLibrary) == 0);
for (unsigned index = 0; index < shutdown.size(); ++index) {
Require(shutdownEvents[index].calls == nativeShutdownEvents[index].calls && shutdownEvents[index].calls == std::vector<int>({1, 2, 3}));
++finalized;
}
Require(references == 28 && retained == 24 && finalized == 8);
std::printf("FreeType face references: %d retains, %d non-final releases, %d finalized faces compared with native\n", references, retained, finalized);
}
const auto root = std::filesystem::temp_directory_path() / ("anyps5_guest_freetype-" + std::to_string(std::random_device{}()));
std::filesystem::create_directories(root / "app0");
CheckGasp();
Expand Down Expand Up @@ -801,6 +908,19 @@ int main() {
GuestFaceRec* masked = nullptr;
Require(FT_New_Face_nid_postfix(library, "/app0/font.ttf", 0x180000000ll, &masked) == 0 && masked != nullptr && masked->face_index == 0);
Require(FT_New_Face_nid_postfix(library, "/app0/font.ttf", 0, &face) == 0 && face != nullptr && face != masked);
{
FT_Face nativeFile = nullptr;
Require(FT_New_Face(hostLibrary, "app0/font.ttf", 0, &nativeFile) == 0);
const auto* bytes = FaceOf(face)->bytes.data();
Require(FaceOf(face)->bytes.size() == font.size() && std::memcmp(bytes, font.data(), font.size()) == 0);
Require(FT_Reference_Face_nid_postfix(face) == FT_Reference_Face(nativeFile));
Require(FT_Done_Face(nativeFile) == 0 && FT_Done_Face_nid_postfix(face) == 0);
Require(static_cast<GuestLibrary*>(library)->faces.size() == 2);
Require(FaceOf(face)->bytes.data() == bytes && std::memcmp(bytes, font.data(), font.size()) == 0);
Require(FT_Get_Char_Index_nid_postfix(face, 'A') == FT_Get_Char_Index(nativeFile, 'A'));
Require(FT_Done_Face(nativeFile) == 0);
std::puts("File-backed face reference: owned font bytes survive a non-final release");
}
FaceMatches(face, host, library);

const auto fontSize = static_cast<std::int64_t>(font.size());
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libSceFreeType.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# libSceFreeType technical debt

### Unknown function info
- [FT_Reference_Face](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - console signature, reference-counter limits and finalizer behavior are unverified; the LP64 guest adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftobjs.c). Counter overflow and reentrant finalization have not been measured on the console.

- [FT_Get_Sfnt_Name_Count, FT_Get_Sfnt_Name](../../../core/libs/prx/libSceFreeType/src/Names.cpp) (libSceFreeType) - console signatures, record layout and errors are unverified; the adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftsnames.c) and assumes the public LP64 `FT_SfntName` layout (24 bytes, string pointer at 8, byte length at 16). Strings retain their original encoding, borrow native face storage and remain valid only while the face is alive

Expand Down
Loading