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* ufe = static_cast<gfxUserFontEntry*>(fe);
gfxUserFontEntry::UserFontLoadState state = ufe->LoadState(); gfxUserFontEntry::UserFontLoadState state = ufe->LoadState();
switch (state) { switch (state) {
case gfxUserFontEntry::STATUS_LOAD_PENDING:
case gfxUserFontEntry::STATUS_LOADING: case gfxUserFontEntry::STATUS_LOADING:
SetLoading(true); SetLoading(true);
break; break;

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

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

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

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

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

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

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

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

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

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