Skip to content

Commit

Permalink
Bug 1778608 - Fix FontFaceSetWorkerImpl destruction off main thread. …
Browse files Browse the repository at this point in the history
…r=emilio

The loaders need to be destroyed on the main thread. Assertions for
gfxUserFontSet and gfxFontFamily need to be updated for workers.

Depends on D151342

Differential Revision: https://phabricator.services.mozilla.com/D151343
  • Loading branch information
aosmond committed Jul 8, 2022
1 parent fc4e22d commit ccb3289
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gfx/thebes/gfxFontEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ void gfxFontFamily::SearchAllFontsForChar(GlobalFontMatch* aMatchData) {
gfxFontFamily::~gfxFontFamily() {
// Should not be dropped by stylo, but the InitFontList thread might use
// a transient gfxFontFamily and that's OK.
MOZ_ASSERT(NS_IsMainThread() || gfxPlatformFontList::IsInitFontListThread());
MOZ_ASSERT(!gfxFontUtils::IsInServoTraversal());
}

// returns true if other names were found, false otherwise
Expand Down
2 changes: 1 addition & 1 deletion gfx/thebes/gfxUserFontSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ size_t gfxUserFontData::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
/*virtual*/
gfxUserFontFamily::~gfxUserFontFamily() {
// Should not be dropped by stylo
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!gfxFontUtils::IsInServoTraversal());
}

already_AddRefed<gfxFontSrcPrincipal> gfxFontFaceSrc::LoadPrincipal(
Expand Down
30 changes: 30 additions & 0 deletions layout/style/FontFaceSetWorkerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,36 @@ void FontFaceSetWorkerImpl::InitializeOnMainThread() {

void FontFaceSetWorkerImpl::Destroy() {
RecursiveMutexAutoLock lock(mMutex);

class DestroyRunnable final : public Runnable {
public:
DestroyRunnable(FontFaceSetWorkerImpl* aFontFaceSet,
nsTHashtable<nsPtrHashKey<nsFontFaceLoader>>&& aLoaders)
: Runnable("FontFaceSetWorkerImpl::Destroy"),
mFontFaceSet(aFontFaceSet),
mLoaders(std::move(aLoaders)) {}

protected:
~DestroyRunnable() override = default;

NS_IMETHOD Run() override {
for (const auto& key : mLoaders.Keys()) {
key->Cancel();
}
return NS_OK;
}

// We save a reference to the FontFaceSetWorkerImpl because the loaders
// contain a non-owning reference to it.
RefPtr<FontFaceSetWorkerImpl> mFontFaceSet;
nsTHashtable<nsPtrHashKey<nsFontFaceLoader>> mLoaders;
};

if (!mLoaders.IsEmpty() && !NS_IsMainThread()) {
auto runnable = MakeRefPtr<DestroyRunnable>(this, std::move(mLoaders));
NS_DispatchToMainThread(runnable);
}

mWorkerRef = nullptr;
FontFaceSetImpl::Destroy();
}
Expand Down

0 comments on commit ccb3289

Please sign in to comment.