From d3acda35aa535ee27b1f614f79ad6f9ff5f1d90e Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Sun, 30 Apr 2017 14:57:25 +0800 Subject: [PATCH] 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 --- gfx/thebes/gfxTextRun.cpp | 1 + gfx/thebes/gfxUserFontSet.cpp | 43 +++++++++++++++++++++++++----- gfx/thebes/gfxUserFontSet.h | 10 ++++++- layout/style/FontFace.cpp | 1 + layout/style/PostTraversalTask.cpp | 5 ++++ layout/style/PostTraversalTask.h | 11 ++++++++ 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/gfx/thebes/gfxTextRun.cpp b/gfx/thebes/gfxTextRun.cpp index 6dc4f6bf29bf..40fe28bed0d5 100644 --- a/gfx/thebes/gfxTextRun.cpp +++ b/gfx/thebes/gfxTextRun.cpp @@ -1957,6 +1957,7 @@ gfxFontGroup::FamilyFace::CheckState(bool& aSkipDrawing) gfxUserFontEntry* ufe = static_cast(fe); gfxUserFontEntry::UserFontLoadState state = ufe->LoadState(); switch (state) { + case gfxUserFontEntry::STATUS_LOAD_PENDING: case gfxUserFontEntry::STATUS_LOADING: SetLoading(true); break; diff --git a/gfx/thebes/gfxUserFontSet.cpp b/gfx/thebes/gfxUserFontSet.cpp index 7074f55404b6..fbaab95b0f43 100644 --- a/gfx/thebes/gfxUserFontSet.cpp +++ b/gfx/thebes/gfxUserFontSet.cpp @@ -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"); diff --git a/gfx/thebes/gfxUserFontSet.h b/gfx/thebes/gfxUserFontSet.h index 384b1c712808..87ff0efdb579 100644 --- a/gfx/thebes/gfxUserFontSet.h +++ b/gfx/thebes/gfxUserFontSet.h @@ -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); diff --git a/layout/style/FontFace.cpp b/layout/style/FontFace.cpp index ed665c3466e8..c58cf819b359 100644 --- a/layout/style/FontFace.cpp +++ b/layout/style/FontFace.cpp @@ -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: diff --git a/layout/style/PostTraversalTask.cpp b/layout/style/PostTraversalTask.cpp index f1863cdbb8ea..2638ecc7d3d1 100644 --- a/layout/style/PostTraversalTask.cpp +++ b/layout/style/PostTraversalTask.cpp @@ -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(mTarget)-> DispatchCheckLoadingFinishedAfterDelay(); break; + + case Type::LoadFontEntry: + static_cast(mTarget)->ContinueLoad(); + break; } } diff --git a/layout/style/PostTraversalTask.h b/layout/style/PostTraversalTask.h index 93e869006e9c..0e653617ce83 100644 --- a/layout/style/PostTraversalTask.h +++ b/layout/style/PostTraversalTask.h @@ -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)