Skip to content

Commit

Permalink
fix(streaming/five): sfFont thread safety
Browse files Browse the repository at this point in the history
Fixes #2157
  • Loading branch information
gottfriedleibniz committed Feb 26, 2024
1 parent d28318a commit 4dbc546
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion code/components/gta-streaming-five/include/sfDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class GMemoryHeap
virtual void* Alloc(uint32_t size) = 0;
virtual void m_58() = 0;
virtual void Free(void* memory) = 0;

char pad_08[0xB8];
bool useLocks;
};

// Unification of GRefCountBase and GRefCountBaseNTS. Once the Scaleform bits
Expand Down Expand Up @@ -191,7 +194,9 @@ class GFxMovieRoot : public GRefCountBase
virtual void SetVariable(const char* path, const GFxValue& value, int type) = 0;

public:
char pad_10[0xB8];
char pad_10[0x28];
GMemoryHeap *pHeap;
char pad_40[0x88];
GRectF VisibleFrameRect;
};

Expand Down Expand Up @@ -394,5 +399,7 @@ class GFxSpriteDef : public GFxResource
};

#ifdef _DEBUG
static_assert(offsetof(GFxMovieRoot, pHeap) == 56);
static_assert(offsetof(GFxMovieRoot, VisibleFrameRect) == 200);
static_assert(offsetof(GMemoryHeap, useLocks) == 192);
#endif
12 changes: 12 additions & 0 deletions code/components/gta-streaming-five/src/sfFontStuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ static void HandleEarlyLoading(TFn&& cb)
g_md = (GFxMovieDef*)* movieDefRef;
g_movie = (GFxMovieRoot*)movieRef;

// GH-2157: HandleSprite can be called from both MainThread and
// RenderThread simultaneously: ensure the heap is flagged to be
// thread-safe.
GMemoryHeap* heap = g_movie->pHeap;
if (!heap->useLocks)
{
heap->useLocks = true;
}
cb();
}
}
Expand Down Expand Up @@ -257,8 +265,12 @@ static LONG SafetyFilter(PEXCEPTION_POINTERS pointers)
return EXCEPTION_EXECUTE_HANDLER;
}

// GH-2157: Other methods used path into GASEnvironment/GlobalEnvironment bits
// that are not thread-safe.
static std::mutex g_handleSprite;
static void HandleSprite(GFxResource* resource, GFxStyledText::HTMLImageTagInfo* imgTagInfo, uint64_t document, void* md, void* character)
{
std::unique_lock _(g_handleSprite);
auto sprite = (GFxSpriteDef*)resource;

static uint32_t id = 0x1234;
Expand Down

0 comments on commit 4dbc546

Please sign in to comment.