Bug 1356103 - Part 9: Use a PostTraversalTask to deal with downloadable fonts in gfxUserFontSet. r=bholley,jfkthame

Here we add a new UserFontLoadState value, STATUS_LOAD_PENDING, which
represents the state just after a gfxUserFontEntry's url()-valued source
would being loading, except that we can't start the load due to being
on a Servo style worker thread.  In that case, we defer the work of
initiating the load until just after the Servo traversal is finished.

URLs that can normally be loaded synchronously, such as data: URLs
and script-implemented protocols marked as synchronous, must be
handled asynchronously when encountered during Servo traversal, since
various main-thread only work (in FontFaceSet::SyncLoadFontData) must
happen.  This is a user visible change from stock Gecko, but should
only happen when font metrics for a data: URL font are requested
due to ch/ex unit resolution when layout hasn't previously requested
the font load.  Hopefully nobody relies on synchronous resolution of
ch/ex units with data: URLs.

We unfortunately also can't pick gfxUserFontEntry objects out of the
UserFontCache during Servo traversal, since validating the cache
entry involves doing content policy checking, which is not thread-safe
(due in part to taking strong references to nsIPrincipals).

Platform fonts and ArrayBuffer-backed DOM FontFace objects continue
to be handled synchronously.

The PostTraversalTask does not take a strong reference to the
gfxUserFontEntry object, since it is held on to by the DOM FontFace
object, which itself won't go away before the PostTraversalTask
is run.

MozReview-Commit-ID: J9ODLsusrNV

--HG--
extra : rebase_source : 1651e2917bd31b87fc1c1be94b0eced1273df86a
This commit is contained in:
Cameron McCormack 2017-04-30 14:57:25 +08:00
Родитель 9133a05c63
Коммит d3acda35aa
6 изменённых файлов: 64 добавлений и 7 удалений

Просмотреть файл

@ -1957,6 +1957,7 @@ gfxFontGroup::FamilyFace::CheckState(bool& aSkipDrawing)
gfxUserFontEntry* ufe = static_cast<gfxUserFontEntry*>(fe);
gfxUserFontEntry::UserFontLoadState state = ufe->LoadState();
switch (state) {
case gfxUserFontEntry::STATUS_LOAD_PENDING:
case gfxUserFontEntry::STATUS_LOADING:
SetLoading(true);
break;

Просмотреть файл

@ -21,6 +21,8 @@
#include "mozilla/Telemetry.h"
#include "mozilla/gfx/2D.h"
#include "gfxPlatformFontList.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/PostTraversalTask.h"
#include "opentype-sanitiser.h"
#include "ots-memory-stream.h"
@ -141,6 +143,10 @@ gfxUserFontEntry::gfxUserFontEntry(gfxUserFontSet* aFontSet,
gfxUserFontEntry::~gfxUserFontEntry()
{
// Assert that we don't drop any gfxUserFontEntry objects during a Servo
// traversal, since PostTraversalTask objects can hold raw pointers to
// gfxUserFontEntry objects.
MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal());
}
bool
@ -429,11 +435,10 @@ CopyWOFFMetadata(const uint8_t* aFontData,
void
gfxUserFontEntry::LoadNextSrc()
{
uint32_t numSrc = mSrcList.Length();
NS_ASSERTION(mSrcIndex < numSrc,
NS_ASSERTION(mSrcIndex < mSrcList.Length(),
"already at the end of the src list for user font");
NS_ASSERTION((mUserFontLoadState == STATUS_NOT_LOADED ||
mUserFontLoadState == STATUS_LOAD_PENDING ||
mUserFontLoadState == STATUS_LOADING) &&
mFontDataLoadingState < LOADING_FAILED,
"attempting to load a font that has either completed or failed");
@ -449,6 +454,23 @@ gfxUserFontEntry::LoadNextSrc()
mSrcIndex++;
}
DoLoadNextSrc(false);
}
void
gfxUserFontEntry::ContinueLoad()
{
MOZ_ASSERT(mUserFontLoadState == STATUS_LOAD_PENDING);
MOZ_ASSERT(mSrcList[mSrcIndex].mSourceType == gfxFontFaceSrc::eSourceType_URL);
DoLoadNextSrc(true);
}
void
gfxUserFontEntry::DoLoadNextSrc(bool aForceAsync)
{
uint32_t numSrc = mSrcList.Length();
// load each src entry in turn, until a local face is found
// or a download begins successfully
while (mSrcIndex < numSrc) {
@ -504,6 +526,12 @@ gfxUserFontEntry::LoadNextSrc()
if (gfxPlatform::GetPlatform()->IsFontFormatSupported(currSrc.mURI,
currSrc.mFormatFlags)) {
if (ServoStyleSet* set = ServoStyleSet::Current()) {
set->AppendTask(PostTraversalTask::LoadFontEntry(this));
SetLoadState(STATUS_LOAD_PENDING);
return;
}
nsIPrincipal* principal = nullptr;
bool bypassCache;
nsresult rv = mFontSet->CheckFontLoad(&currSrc, &principal,
@ -537,9 +565,11 @@ gfxUserFontEntry::LoadNextSrc()
mPrincipal = principal;
bool loadDoesntSpin = false;
rv = NS_URIChainHasFlags(currSrc.mURI,
nsIProtocolHandler::URI_SYNC_LOAD_IS_OK,
&loadDoesntSpin);
if (!aForceAsync) {
rv = NS_URIChainHasFlags(currSrc.mURI,
nsIProtocolHandler::URI_SYNC_LOAD_IS_OK,
&loadDoesntSpin);
}
if (NS_SUCCEEDED(rv) && loadDoesntSpin) {
uint8_t* buffer = nullptr;
@ -644,6 +674,7 @@ bool
gfxUserFontEntry::LoadPlatformFont(const uint8_t* aFontData, uint32_t& aLength)
{
NS_ASSERTION((mUserFontLoadState == STATUS_NOT_LOADED ||
mUserFontLoadState == STATUS_LOAD_PENDING ||
mUserFontLoadState == STATUS_LOADING) &&
mFontDataLoadingState < LOADING_FAILED,
"attempting to load a font that has either completed or failed");

Просмотреть файл

@ -17,6 +17,9 @@
#include "mozilla/net/ReferrerPolicy.h"
#include "gfxFontConstants.h"
namespace mozilla {
class PostTraversalTask;
} // namespace mozilla
class nsFontFaceLoader;
//#define DEBUG_USERFONT_CACHE
@ -548,6 +551,7 @@ protected:
// acts a placeholder until the real font is downloaded
class gfxUserFontEntry : public gfxFontEntry {
friend class mozilla::PostTraversalTask;
friend class gfxUserFontSet;
friend class nsUserFontSet;
friend class nsFontFaceLoader;
@ -556,6 +560,7 @@ class gfxUserFontEntry : public gfxFontEntry {
public:
enum UserFontLoadState {
STATUS_NOT_LOADED = 0,
STATUS_LOAD_PENDING,
STATUS_LOADING,
STATUS_LOADED,
STATUS_FAILED
@ -593,7 +598,8 @@ public:
// whether to wait before using fallback font or not
bool WaitForUserFont() const {
return mUserFontLoadState == STATUS_LOADING &&
return (mUserFontLoadState == STATUS_LOAD_PENDING ||
mUserFontLoadState == STATUS_LOADING) &&
mFontDataLoadingState < LOADING_SLOWLY;
}
@ -633,6 +639,8 @@ protected:
// attempt to load the next resource in the src list.
void LoadNextSrc();
void ContinueLoad();
void DoLoadNextSrc(bool aForceAsync);
// change the load state
virtual void SetLoadState(UserFontLoadState aLoadState);

Просмотреть файл

@ -137,6 +137,7 @@ LoadStateToStatus(gfxUserFontEntry::UserFontLoadState aLoadState)
switch (aLoadState) {
case gfxUserFontEntry::UserFontLoadState::STATUS_NOT_LOADED:
return FontFaceLoadStatus::Unloaded;
case gfxUserFontEntry::UserFontLoadState::STATUS_LOAD_PENDING:
case gfxUserFontEntry::UserFontLoadState::STATUS_LOADING:
return FontFaceLoadStatus::Loading;
case gfxUserFontEntry::UserFontLoadState::STATUS_LOADED:

Просмотреть файл

@ -8,6 +8,7 @@
#include "mozilla/dom/FontFace.h"
#include "mozilla/dom/FontFaceSet.h"
#include "gfxUserFontSet.h"
namespace mozilla {
@ -34,6 +35,10 @@ PostTraversalTask::Run()
static_cast<FontFaceSet*>(mTarget)->
DispatchCheckLoadingFinishedAfterDelay();
break;
case Type::LoadFontEntry:
static_cast<gfxUserFontEntry*>(mTarget)->ContinueLoad();
break;
}
}

Просмотреть файл

@ -15,6 +15,7 @@ class FontFace;
class FontFaceSet;
} // namespace dom
} // namespace mozilla
class gfxUserFontEntry;
namespace mozilla {
@ -63,6 +64,13 @@ public:
return task;
}
static PostTraversalTask LoadFontEntry(gfxUserFontEntry* aFontEntry)
{
auto task = PostTraversalTask(Type::LoadFontEntry);
task.mTarget = aFontEntry;
return task;
}
void Run();
private:
@ -83,6 +91,9 @@ private:
// mTarget (FontFaceSet*)
DispatchFontFaceSetCheckLoadingFinishedAfterDelay,
// mTarget (gfxUserFontEntry*)
LoadFontEntry,
};
PostTraversalTask(Type aType)