Skip to content

Commit

Permalink
Android: Delay TextDrawer init in PPGe.
Browse files Browse the repository at this point in the history
We need it to init on the correct thread.
  • Loading branch information
unknownbrackets committed Mar 11, 2020
1 parent 08a6047 commit 9c9ace6
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions Core/Util/PPGeDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ static AtlasCharLine char_one_line;
static AtlasLineArray char_lines;
static AtlasTextMetrics char_lines_metrics;

static bool textDrawerInited = false;
static TextDrawer *textDrawer = nullptr;
struct PPGeTextDrawerCacheKey {
bool operator < (const PPGeTextDrawerCacheKey &other) const {
Expand Down Expand Up @@ -267,13 +268,9 @@ void __PPGeInit()

free(imageData[0]);

// TODO: Should we pass a draw_?
textDrawer = TextDrawer::Create(nullptr);
if (textDrawer) {
textDrawer->SetFontScale(1.0f, 1.0f);
textDrawer->SetForcedDPIScale(1.0f);
textDrawer->SetFont(g_Config.sFont.c_str(), 20, 0);
}
// We can't create it here, because Android needs it on the right thread.
textDrawerInited = false;
textDrawer = nullptr;
textDrawerImages.clear();

DEBUG_LOG(SCEGE, "PPGe drawing library initialized. DL: %08x Data: %08x Atlas: %08x (%i) Args: %08x",
Expand Down Expand Up @@ -696,10 +693,27 @@ static AtlasTextMetrics BreakLines(const char *text, const AtlasFont &atlasfont,
return metrics;
}

static bool HasTextDrawer() {
// We create this on first use so it's on the correct thread.
if (textDrawerInited) {
return textDrawer != nullptr;
}

// TODO: Should we pass a draw_?
textDrawer = TextDrawer::Create(nullptr);
if (textDrawer) {
textDrawer->SetFontScale(1.0f, 1.0f);
textDrawer->SetForcedDPIScale(1.0f);
textDrawer->SetFont(g_Config.sFont.c_str(), 20, 0);
}
textDrawerInited = true;
return textDrawer != nullptr;
}

void PPGeMeasureText(float *w, float *h, int *n,
const char *text, float scale, int WrapType, int wrapWidth)
{
if (textDrawer) {
if (HasTextDrawer()) {
float mw, mh;
textDrawer->SetFontScale(scale, scale);
int dtalign = (WrapType & PPGE_LINE_WRAP_WORD) ? FLAG_WRAP_TEXT : 0;
Expand Down Expand Up @@ -871,7 +885,7 @@ static void PPGeDrawTextImage(PPGeTextDrawerImage im, float x, float y, int alig
}

void PPGeDrawText(const char *text, float x, float y, int align, float scale, u32 color) {
if (textDrawer) {
if (HasTextDrawer()) {
PPGeTextDrawerImage im = PPGeGetTextImage(text, align, scale, 480.0f - x, false);
PPGeDrawTextImage(im, x, y, align, scale, color);
return;
Expand Down Expand Up @@ -913,7 +927,7 @@ void PPGeDrawTextWrapped(const char *text, float x, float y, float wrapWidth, fl
int zoom = (PSP_CoreParameter().pixelHeight + 479) / 480;
float maxScaleDown = zoom == 1 ? 1.3f : 2.0f;

if (textDrawer) {
if (HasTextDrawer()) {
float actualWidth, actualHeight;
Bounds b(0, 0, wrapWidth <= 0 ? 480.0f - x : wrapWidth, wrapHeight);
int tdalign = (align & PPGE_ALIGN_HCENTER) ? ALIGN_HCENTER : 0;
Expand Down

0 comments on commit 9c9ace6

Please sign in to comment.