michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GFX_FONT_INFO_LOADER_H michael@0: #define GFX_FONT_INFO_LOADER_H michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsITimer.h" michael@0: #include "nsIThread.h" michael@0: #include "nsRefPtrHashtable.h" michael@0: #include "nsString.h" michael@0: #include "gfxFont.h" michael@0: #include "nsIRunnable.h" michael@0: #include "mozilla/TimeStamp.h" michael@0: #include "nsISupportsImpl.h" michael@0: michael@0: // data retrieved for a given face michael@0: michael@0: struct FontFaceData { michael@0: FontFaceData() : mUVSOffset(0), mSymbolFont(false) {} michael@0: michael@0: FontFaceData(const FontFaceData& aFontFaceData) { michael@0: mFullName = aFontFaceData.mFullName; michael@0: mPostscriptName = aFontFaceData.mPostscriptName; michael@0: mCharacterMap = aFontFaceData.mCharacterMap; michael@0: mUVSOffset = aFontFaceData.mUVSOffset; michael@0: mSymbolFont = aFontFaceData.mSymbolFont; michael@0: } michael@0: michael@0: nsString mFullName; michael@0: nsString mPostscriptName; michael@0: nsRefPtr mCharacterMap; michael@0: uint32_t mUVSOffset; michael@0: bool mSymbolFont; michael@0: }; michael@0: michael@0: // base class used to contain cached system-wide font info. michael@0: // methods in this class are called on off-main threads so michael@0: // all methods use only static methods or other thread-safe michael@0: // font data access API's. specifically, no use is made of michael@0: // gfxPlatformFontList, gfxFontFamily, gfxFamily or any michael@0: // harfbuzz API methods within FontInfoData subclasses. michael@0: michael@0: class FontInfoData { michael@0: public: michael@0: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontInfoData) michael@0: michael@0: FontInfoData(bool aLoadOtherNames, michael@0: bool aLoadFaceNames, michael@0: bool aLoadCmaps) : michael@0: mLoadOtherNames(aLoadOtherNames), michael@0: mLoadFaceNames(aLoadFaceNames), michael@0: mLoadCmaps(aLoadCmaps) michael@0: { michael@0: MOZ_COUNT_CTOR(FontInfoData); michael@0: } michael@0: michael@0: protected: michael@0: // Protected destructor, to discourage deletion outside of Release(): michael@0: virtual ~FontInfoData() { michael@0: MOZ_COUNT_DTOR(FontInfoData); michael@0: } michael@0: michael@0: public: michael@0: virtual void Load(); michael@0: michael@0: // loads font data for all fonts of a given family michael@0: // (called on async thread) michael@0: virtual void LoadFontFamilyData(const nsAString& aFamilyName) = 0; michael@0: michael@0: // -- methods overriden by platform-specific versions -- michael@0: michael@0: // fetches cmap data for a particular font from cached font data michael@0: virtual already_AddRefed michael@0: GetCMAP(const nsAString& aFontName, michael@0: uint32_t& aUVSOffset, michael@0: bool& aSymbolFont) michael@0: { michael@0: FontFaceData faceData; michael@0: if (!mFontFaceData.Get(aFontName, &faceData) || michael@0: !faceData.mCharacterMap) { michael@0: return nullptr; michael@0: } michael@0: michael@0: aUVSOffset = faceData.mUVSOffset; michael@0: aSymbolFont = faceData.mSymbolFont; michael@0: nsRefPtr cmap = faceData.mCharacterMap; michael@0: return cmap.forget(); michael@0: } michael@0: michael@0: // fetches fullname/postscript names from cached font data michael@0: virtual void GetFaceNames(const nsAString& aFontName, michael@0: nsAString& aFullName, michael@0: nsAString& aPostscriptName) michael@0: { michael@0: FontFaceData faceData; michael@0: if (!mFontFaceData.Get(aFontName, &faceData)) { michael@0: return; michael@0: } michael@0: michael@0: aFullName = faceData.mFullName; michael@0: aPostscriptName = faceData.mPostscriptName; michael@0: } michael@0: michael@0: // fetches localized family name data from cached font data michael@0: virtual bool GetOtherFamilyNames(const nsAString& aFamilyName, michael@0: nsTArray& aOtherFamilyNames) michael@0: { michael@0: return mOtherFamilyNames.Get(aFamilyName, &aOtherFamilyNames); michael@0: } michael@0: michael@0: nsTArray mFontFamiliesToLoad; michael@0: michael@0: // time spent on the loader thread michael@0: mozilla::TimeDuration mLoadTime; michael@0: michael@0: struct FontCounts { michael@0: uint32_t families; michael@0: uint32_t fonts; michael@0: uint32_t cmaps; michael@0: uint32_t facenames; michael@0: uint32_t othernames; michael@0: }; michael@0: michael@0: FontCounts mLoadStats; michael@0: michael@0: bool mLoadOtherNames; michael@0: bool mLoadFaceNames; michael@0: bool mLoadCmaps; michael@0: michael@0: // face name ==> per-face data michael@0: nsDataHashtable mFontFaceData; michael@0: michael@0: // canonical family name ==> array of localized family names michael@0: nsDataHashtable > mOtherFamilyNames; michael@0: }; michael@0: michael@0: // gfxFontInfoLoader - helper class for loading font info on async thread michael@0: // For large, "all fonts on system" data, data needed on a given platform michael@0: // (e.g. localized names, face names, cmaps) are loaded async. michael@0: michael@0: // helper class for loading in font info on a separate async thread michael@0: // once async thread completes, completion process is run on regular michael@0: // intervals to prevent tying up the main thread michael@0: michael@0: class gfxFontInfoLoader { michael@0: public: michael@0: michael@0: // state transitions: michael@0: // initial ---StartLoader with delay---> timer on delay michael@0: // initial ---StartLoader without delay---> timer on interval michael@0: // timer on delay ---LoaderTimerFire---> timer on interval michael@0: // timer on delay ---CancelLoader---> timer off michael@0: // timer on interval ---CancelLoader---> timer off michael@0: // timer off ---StartLoader with delay---> timer on delay michael@0: // timer off ---StartLoader without delay---> timer on interval michael@0: typedef enum { michael@0: stateInitial, michael@0: stateTimerOnDelay, michael@0: stateAsyncLoad, michael@0: stateTimerOnInterval, michael@0: stateTimerOff michael@0: } TimerState; michael@0: michael@0: gfxFontInfoLoader() : michael@0: mInterval(0), mState(stateInitial) michael@0: { michael@0: } michael@0: michael@0: virtual ~gfxFontInfoLoader(); michael@0: michael@0: // start timer with an initial delay, then call Run method at regular intervals michael@0: void StartLoader(uint32_t aDelay, uint32_t aInterval); michael@0: michael@0: // Finalize - async load complete, transfer data (on intervals if necessary) michael@0: virtual void FinalizeLoader(FontInfoData *aFontInfo); michael@0: michael@0: // cancel the timer and cleanup michael@0: void CancelLoader(); michael@0: michael@0: uint32_t GetInterval() { return mInterval; } michael@0: michael@0: protected: michael@0: class ShutdownObserver : public nsIObserver michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: ShutdownObserver(gfxFontInfoLoader *aLoader) michael@0: : mLoader(aLoader) michael@0: { } michael@0: michael@0: virtual ~ShutdownObserver() michael@0: { } michael@0: michael@0: protected: michael@0: gfxFontInfoLoader *mLoader; michael@0: }; michael@0: michael@0: // CreateFontInfo - create platform-specific object used michael@0: // to load system-wide font info michael@0: virtual already_AddRefed CreateFontInfoData() { michael@0: return nullptr; michael@0: } michael@0: michael@0: // Init - initialization before async loader thread runs michael@0: virtual void InitLoader() = 0; michael@0: michael@0: // LoadFontInfo - transfer font info data within a time limit, return michael@0: // true when done michael@0: virtual bool LoadFontInfo() = 0; michael@0: michael@0: // Cleanup - finish and cleanup after done, including possible reflows michael@0: virtual void CleanupLoader() { michael@0: mFontInfo = nullptr; michael@0: } michael@0: michael@0: // Timer interval callbacks michael@0: static void LoadFontInfoCallback(nsITimer *aTimer, void *aThis) { michael@0: gfxFontInfoLoader *loader = static_cast(aThis); michael@0: loader->LoadFontInfoTimerFire(); michael@0: } michael@0: michael@0: static void DelayedStartCallback(nsITimer *aTimer, void *aThis) { michael@0: gfxFontInfoLoader *loader = static_cast(aThis); michael@0: loader->StartLoader(0, loader->GetInterval()); michael@0: } michael@0: michael@0: void LoadFontInfoTimerFire(); michael@0: michael@0: void AddShutdownObserver(); michael@0: void RemoveShutdownObserver(); michael@0: michael@0: nsCOMPtr mTimer; michael@0: nsCOMPtr mObserver; michael@0: nsCOMPtr mFontLoaderThread; michael@0: uint32_t mInterval; michael@0: TimerState mState; michael@0: michael@0: // after async font loader completes, data is stored here michael@0: nsRefPtr mFontInfo; michael@0: michael@0: // time spent on the loader thread michael@0: mozilla::TimeDuration mLoadTime; michael@0: }; michael@0: michael@0: #endif /* GFX_FONT_INFO_LOADER_H */