2014-01-29 11:39:01 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "gfxFontInfoLoader.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIObserverService.h"
|
2014-01-29 11:39:01 +04:00
|
|
|
#include "nsThreadUtils.h" // for nsRunnable
|
|
|
|
#include "gfxPlatformFontList.h"
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-08-01 23:37:53 +04:00
|
|
|
using services::GetObserverService;
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2015-07-31 04:10:31 +03:00
|
|
|
#define LOG_FONTINIT(args) MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontinit), \
|
|
|
|
LogLevel::Debug, args)
|
|
|
|
#define LOG_FONTINIT_ENABLED() MOZ_LOG_TEST( \
|
|
|
|
gfxPlatform::GetLog(eGfxLog_fontinit), \
|
|
|
|
LogLevel::Debug)
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
void
|
|
|
|
FontInfoData::Load()
|
|
|
|
{
|
|
|
|
TimeStamp start = TimeStamp::Now();
|
|
|
|
|
|
|
|
uint32_t i, n = mFontFamiliesToLoad.Length();
|
|
|
|
mLoadStats.families = n;
|
2016-02-05 04:24:00 +03:00
|
|
|
for (i = 0; i < n && !mCanceled; i++) {
|
2015-09-02 04:58:23 +03:00
|
|
|
// font file memory mapping sometimes causes exceptions - bug 1100949
|
|
|
|
MOZ_SEH_TRY {
|
|
|
|
LoadFontFamilyData(mFontFamiliesToLoad[i]);
|
|
|
|
} MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
gfxCriticalError() <<
|
|
|
|
"Exception occurred reading font data for " <<
|
|
|
|
NS_ConvertUTF16toUTF8(mFontFamiliesToLoad[i]).get();
|
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mLoadTime = TimeStamp::Now() - start;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class FontInfoLoadCompleteEvent : public Runnable {
|
2014-06-23 22:49:07 +04:00
|
|
|
virtual ~FontInfoLoadCompleteEvent() {}
|
|
|
|
|
2014-07-14 23:21:34 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit FontInfoLoadCompleteEvent(FontInfoData *aFontInfo) :
|
2014-01-29 11:39:01 +04:00
|
|
|
mFontInfo(aFontInfo)
|
|
|
|
{}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override;
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FontInfoData> mFontInfo;
|
2014-01-29 11:39:01 +04:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class AsyncFontInfoLoader : public Runnable {
|
2014-06-23 22:49:07 +04:00
|
|
|
virtual ~AsyncFontInfoLoader() {}
|
|
|
|
|
2014-07-14 23:21:34 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit AsyncFontInfoLoader(FontInfoData *aFontInfo) :
|
2014-01-29 11:39:01 +04:00
|
|
|
mFontInfo(aFontInfo)
|
|
|
|
{
|
|
|
|
mCompleteEvent = new FontInfoLoadCompleteEvent(aFontInfo);
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override;
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FontInfoData> mFontInfo;
|
|
|
|
RefPtr<FontInfoLoadCompleteEvent> mCompleteEvent;
|
2014-01-29 11:39:01 +04:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class ShutdownThreadEvent : public Runnable {
|
2015-07-31 04:10:31 +03:00
|
|
|
virtual ~ShutdownThreadEvent() {}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
explicit ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {}
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mThread->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(ShutdownThreadEvent, Runnable);
|
2015-07-31 04:10:31 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// runs on main thread after async font info loading is done
|
|
|
|
nsresult
|
|
|
|
FontInfoLoadCompleteEvent::Run()
|
|
|
|
{
|
|
|
|
gfxFontInfoLoader *loader =
|
|
|
|
static_cast<gfxFontInfoLoader*>(gfxPlatformFontList::PlatformFontList());
|
|
|
|
|
|
|
|
loader->FinalizeLoader(mFontInfo);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(FontInfoLoadCompleteEvent, Runnable);
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
// runs on separate thread
|
|
|
|
nsresult
|
|
|
|
AsyncFontInfoLoader::Run()
|
|
|
|
{
|
|
|
|
// load platform-specific font info
|
|
|
|
mFontInfo->Load();
|
|
|
|
|
|
|
|
// post a completion event that transfer the data to the fontlist
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(mCompleteEvent);
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AsyncFontInfoLoader, Runnable);
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(gfxFontInfoLoader::ShutdownObserver, nsIObserver)
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
gfxFontInfoLoader::ShutdownObserver::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const char16_t *someData)
|
|
|
|
{
|
|
|
|
if (!nsCRT::strcmp(aTopic, "quit-application")) {
|
|
|
|
mLoader->CancelLoader();
|
|
|
|
} else {
|
|
|
|
NS_NOTREACHED("unexpected notification topic");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontInfoLoader::StartLoader(uint32_t aDelay, uint32_t aInterval)
|
|
|
|
{
|
|
|
|
mInterval = aInterval;
|
|
|
|
|
2015-07-31 04:10:31 +03:00
|
|
|
NS_ASSERTION(!mFontInfo,
|
|
|
|
"fontinfo should be null when starting font loader");
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// sanity check
|
2014-01-29 11:39:01 +04:00
|
|
|
if (mState != stateInitial &&
|
|
|
|
mState != stateTimerOff &&
|
|
|
|
mState != stateTimerOnDelay) {
|
2014-01-29 11:39:01 +04:00
|
|
|
CancelLoader();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up timer
|
|
|
|
if (!mTimer) {
|
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
if (!mTimer) {
|
|
|
|
NS_WARNING("Failure to create font info loader timer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
AddShutdownObserver();
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// delay? ==> start async thread after a delay
|
2014-01-29 11:39:01 +04:00
|
|
|
if (aDelay) {
|
|
|
|
mState = stateTimerOnDelay;
|
2014-01-29 11:39:01 +04:00
|
|
|
mTimer->InitWithFuncCallback(DelayedStartCallback, this, aDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
return;
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
mFontInfo = CreateFontInfoData();
|
|
|
|
|
|
|
|
// initialize
|
2014-01-29 11:39:01 +04:00
|
|
|
InitLoader();
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// start async load
|
|
|
|
nsresult rv = NS_NewNamedThread("Font Loader",
|
|
|
|
getter_AddRefs(mFontLoaderThread),
|
|
|
|
nullptr);
|
2015-07-10 06:21:46 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-01-29 11:39:01 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:21:46 +03:00
|
|
|
mState = stateAsyncLoad;
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
nsCOMPtr<nsIRunnable> loadEvent = new AsyncFontInfoLoader(mFontInfo);
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
mFontLoaderThread->Dispatch(loadEvent.forget(), NS_DISPATCH_NORMAL);
|
2015-07-31 04:10:31 +03:00
|
|
|
|
|
|
|
if (LOG_FONTINIT_ENABLED()) {
|
|
|
|
LOG_FONTINIT(("(fontinit) fontloader started (fontinfo: %p)\n",
|
|
|
|
mFontInfo.get()));
|
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontInfoLoader::FinalizeLoader(FontInfoData *aFontInfo)
|
|
|
|
{
|
2015-07-10 06:21:46 +03:00
|
|
|
// Avoid loading data if loader has already been canceled.
|
|
|
|
// This should mean that CancelLoader() ran and the Load
|
|
|
|
// thread has already Shutdown(), and likely before processing
|
|
|
|
// the Shutdown event it handled the load event and sent back
|
|
|
|
// our Completion event, thus we end up here.
|
2015-07-31 04:10:31 +03:00
|
|
|
if (mState != stateAsyncLoad || mFontInfo != aFontInfo) {
|
2014-01-29 11:39:01 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadTime = mFontInfo->mLoadTime;
|
|
|
|
|
|
|
|
// try to load all font data immediately
|
|
|
|
if (LoadFontInfo()) {
|
|
|
|
CancelLoader();
|
|
|
|
return;
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
// not all work completed ==> run load on interval
|
|
|
|
mState = stateTimerOnInterval;
|
|
|
|
mTimer->InitWithFuncCallback(LoadFontInfoCallback, this, mInterval,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontInfoLoader::CancelLoader()
|
|
|
|
{
|
|
|
|
if (mState == stateInitial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mState = stateTimerOff;
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
2016-02-05 04:24:00 +03:00
|
|
|
if (mFontInfo) // null during any initial delay
|
|
|
|
mFontInfo->mCanceled = true;
|
2014-01-29 11:39:01 +04:00
|
|
|
if (mFontLoaderThread) {
|
2015-07-31 04:10:31 +03:00
|
|
|
NS_DispatchToMainThread(new ShutdownThreadEvent(mFontLoaderThread));
|
|
|
|
mFontLoaderThread = nullptr;
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
RemoveShutdownObserver();
|
2014-01-29 11:39:01 +04:00
|
|
|
CleanupLoader();
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-29 11:39:01 +04:00
|
|
|
gfxFontInfoLoader::LoadFontInfoTimerFire()
|
2014-01-29 11:39:01 +04:00
|
|
|
{
|
|
|
|
if (mState == stateTimerOnDelay) {
|
|
|
|
mState = stateTimerOnInterval;
|
|
|
|
mTimer->SetDelay(mInterval);
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
bool done = LoadFontInfo();
|
2014-01-29 11:39:01 +04:00
|
|
|
if (done) {
|
|
|
|
CancelLoader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontInfoLoader::~gfxFontInfoLoader()
|
|
|
|
{
|
|
|
|
RemoveShutdownObserver();
|
2014-08-01 23:37:53 +04:00
|
|
|
MOZ_COUNT_DTOR(gfxFontInfoLoader);
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
void
|
|
|
|
gfxFontInfoLoader::AddShutdownObserver()
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
mObserver = new ShutdownObserver(this);
|
|
|
|
obs->AddObserver(mObserver, "quit-application", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
void
|
|
|
|
gfxFontInfoLoader::RemoveShutdownObserver()
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(mObserver, "quit-application");
|
|
|
|
mObserver = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|