gfx/thebes/gfxFontInfoLoader.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "gfxFontInfoLoader.h"
michael@0 7 #include "nsCRT.h"
michael@0 8 #include "nsIObserverService.h"
michael@0 9 #include "nsThreadUtils.h" // for nsRunnable
michael@0 10 #include "gfxPlatformFontList.h"
michael@0 11
michael@0 12 using namespace mozilla;
michael@0 13 using mozilla::services::GetObserverService;
michael@0 14
michael@0 15 void
michael@0 16 FontInfoData::Load()
michael@0 17 {
michael@0 18 TimeStamp start = TimeStamp::Now();
michael@0 19
michael@0 20 uint32_t i, n = mFontFamiliesToLoad.Length();
michael@0 21 mLoadStats.families = n;
michael@0 22 for (i = 0; i < n; i++) {
michael@0 23 LoadFontFamilyData(mFontFamiliesToLoad[i]);
michael@0 24 }
michael@0 25
michael@0 26 mLoadTime = TimeStamp::Now() - start;
michael@0 27 }
michael@0 28
michael@0 29 class FontInfoLoadCompleteEvent : public nsRunnable {
michael@0 30 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 31
michael@0 32 FontInfoLoadCompleteEvent(FontInfoData *aFontInfo) :
michael@0 33 mFontInfo(aFontInfo)
michael@0 34 {}
michael@0 35 virtual ~FontInfoLoadCompleteEvent() {}
michael@0 36
michael@0 37 NS_IMETHOD Run();
michael@0 38
michael@0 39 nsRefPtr<FontInfoData> mFontInfo;
michael@0 40 };
michael@0 41
michael@0 42 class AsyncFontInfoLoader : public nsRunnable {
michael@0 43 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 44
michael@0 45 AsyncFontInfoLoader(FontInfoData *aFontInfo) :
michael@0 46 mFontInfo(aFontInfo)
michael@0 47 {
michael@0 48 mCompleteEvent = new FontInfoLoadCompleteEvent(aFontInfo);
michael@0 49 }
michael@0 50 virtual ~AsyncFontInfoLoader() {}
michael@0 51
michael@0 52 NS_IMETHOD Run();
michael@0 53
michael@0 54 nsRefPtr<FontInfoData> mFontInfo;
michael@0 55 nsRefPtr<FontInfoLoadCompleteEvent> mCompleteEvent;
michael@0 56 };
michael@0 57
michael@0 58 // runs on main thread after async font info loading is done
michael@0 59 nsresult
michael@0 60 FontInfoLoadCompleteEvent::Run()
michael@0 61 {
michael@0 62 gfxFontInfoLoader *loader =
michael@0 63 static_cast<gfxFontInfoLoader*>(gfxPlatformFontList::PlatformFontList());
michael@0 64
michael@0 65 loader->FinalizeLoader(mFontInfo);
michael@0 66
michael@0 67 mFontInfo = nullptr;
michael@0 68 return NS_OK;
michael@0 69 }
michael@0 70
michael@0 71 NS_IMPL_ISUPPORTS(FontInfoLoadCompleteEvent, nsIRunnable);
michael@0 72
michael@0 73 // runs on separate thread
michael@0 74 nsresult
michael@0 75 AsyncFontInfoLoader::Run()
michael@0 76 {
michael@0 77 // load platform-specific font info
michael@0 78 mFontInfo->Load();
michael@0 79
michael@0 80 // post a completion event that transfer the data to the fontlist
michael@0 81 NS_DispatchToMainThread(mCompleteEvent, NS_DISPATCH_NORMAL);
michael@0 82 mFontInfo = nullptr;
michael@0 83
michael@0 84 return NS_OK;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMPL_ISUPPORTS(AsyncFontInfoLoader, nsIRunnable);
michael@0 88
michael@0 89 NS_IMPL_ISUPPORTS(gfxFontInfoLoader::ShutdownObserver, nsIObserver)
michael@0 90
michael@0 91 NS_IMETHODIMP
michael@0 92 gfxFontInfoLoader::ShutdownObserver::Observe(nsISupports *aSubject,
michael@0 93 const char *aTopic,
michael@0 94 const char16_t *someData)
michael@0 95 {
michael@0 96 if (!nsCRT::strcmp(aTopic, "quit-application")) {
michael@0 97 mLoader->CancelLoader();
michael@0 98 } else {
michael@0 99 NS_NOTREACHED("unexpected notification topic");
michael@0 100 }
michael@0 101 return NS_OK;
michael@0 102 }
michael@0 103
michael@0 104 void
michael@0 105 gfxFontInfoLoader::StartLoader(uint32_t aDelay, uint32_t aInterval)
michael@0 106 {
michael@0 107 mInterval = aInterval;
michael@0 108
michael@0 109 // sanity check
michael@0 110 if (mState != stateInitial &&
michael@0 111 mState != stateTimerOff &&
michael@0 112 mState != stateTimerOnDelay) {
michael@0 113 CancelLoader();
michael@0 114 }
michael@0 115
michael@0 116 // set up timer
michael@0 117 if (!mTimer) {
michael@0 118 mTimer = do_CreateInstance("@mozilla.org/timer;1");
michael@0 119 if (!mTimer) {
michael@0 120 NS_WARNING("Failure to create font info loader timer");
michael@0 121 return;
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 AddShutdownObserver();
michael@0 126
michael@0 127 // delay? ==> start async thread after a delay
michael@0 128 if (aDelay) {
michael@0 129 mState = stateTimerOnDelay;
michael@0 130 mTimer->InitWithFuncCallback(DelayedStartCallback, this, aDelay,
michael@0 131 nsITimer::TYPE_ONE_SHOT);
michael@0 132 return;
michael@0 133 }
michael@0 134
michael@0 135 mFontInfo = CreateFontInfoData();
michael@0 136
michael@0 137 // initialize
michael@0 138 InitLoader();
michael@0 139
michael@0 140 // start async load
michael@0 141 mState = stateAsyncLoad;
michael@0 142 nsresult rv = NS_NewNamedThread("Font Loader",
michael@0 143 getter_AddRefs(mFontLoaderThread),
michael@0 144 nullptr);
michael@0 145 if (NS_FAILED(rv)) {
michael@0 146 return;
michael@0 147 }
michael@0 148
michael@0 149 nsCOMPtr<nsIRunnable> loadEvent = new AsyncFontInfoLoader(mFontInfo);
michael@0 150
michael@0 151 mFontLoaderThread->Dispatch(loadEvent, NS_DISPATCH_NORMAL);
michael@0 152 }
michael@0 153
michael@0 154 void
michael@0 155 gfxFontInfoLoader::FinalizeLoader(FontInfoData *aFontInfo)
michael@0 156 {
michael@0 157 // avoid loading data if loader has already been canceled
michael@0 158 if (mState != stateAsyncLoad) {
michael@0 159 return;
michael@0 160 }
michael@0 161
michael@0 162 mLoadTime = mFontInfo->mLoadTime;
michael@0 163
michael@0 164 // try to load all font data immediately
michael@0 165 if (LoadFontInfo()) {
michael@0 166 CancelLoader();
michael@0 167 return;
michael@0 168 }
michael@0 169
michael@0 170 // not all work completed ==> run load on interval
michael@0 171 mState = stateTimerOnInterval;
michael@0 172 mTimer->InitWithFuncCallback(LoadFontInfoCallback, this, mInterval,
michael@0 173 nsITimer::TYPE_REPEATING_SLACK);
michael@0 174 }
michael@0 175
michael@0 176 void
michael@0 177 gfxFontInfoLoader::CancelLoader()
michael@0 178 {
michael@0 179 if (mState == stateInitial) {
michael@0 180 return;
michael@0 181 }
michael@0 182 mState = stateTimerOff;
michael@0 183 if (mTimer) {
michael@0 184 mTimer->Cancel();
michael@0 185 mTimer = nullptr;
michael@0 186 }
michael@0 187 if (mFontLoaderThread) {
michael@0 188 mFontLoaderThread->Shutdown();
michael@0 189 mFontLoaderThread = nullptr;
michael@0 190 }
michael@0 191 RemoveShutdownObserver();
michael@0 192 CleanupLoader();
michael@0 193 }
michael@0 194
michael@0 195 void
michael@0 196 gfxFontInfoLoader::LoadFontInfoTimerFire()
michael@0 197 {
michael@0 198 if (mState == stateTimerOnDelay) {
michael@0 199 mState = stateTimerOnInterval;
michael@0 200 mTimer->SetDelay(mInterval);
michael@0 201 }
michael@0 202
michael@0 203 bool done = LoadFontInfo();
michael@0 204 if (done) {
michael@0 205 CancelLoader();
michael@0 206 }
michael@0 207 }
michael@0 208
michael@0 209 gfxFontInfoLoader::~gfxFontInfoLoader()
michael@0 210 {
michael@0 211 RemoveShutdownObserver();
michael@0 212 }
michael@0 213
michael@0 214 void
michael@0 215 gfxFontInfoLoader::AddShutdownObserver()
michael@0 216 {
michael@0 217 if (mObserver) {
michael@0 218 return;
michael@0 219 }
michael@0 220
michael@0 221 nsCOMPtr<nsIObserverService> obs = GetObserverService();
michael@0 222 if (obs) {
michael@0 223 mObserver = new ShutdownObserver(this);
michael@0 224 obs->AddObserver(mObserver, "quit-application", false);
michael@0 225 }
michael@0 226 }
michael@0 227
michael@0 228 void
michael@0 229 gfxFontInfoLoader::RemoveShutdownObserver()
michael@0 230 {
michael@0 231 if (mObserver) {
michael@0 232 nsCOMPtr<nsIObserverService> obs = GetObserverService();
michael@0 233 if (obs) {
michael@0 234 obs->RemoveObserver(mObserver, "quit-application");
michael@0 235 mObserver = nullptr;
michael@0 236 }
michael@0 237 }
michael@0 238 }

mercurial