1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* -*- 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"
#include "nsThreadUtils.h" // for nsRunnable
#include "gfxPlatformFontList.h"
#include "mozilla/gfx/Logging.h"
#include <d3d11.h>
using namespace mozilla;
using services::GetObserverService;
#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)
void
FontInfoData::Load()
{
TimeStamp start = TimeStamp::Now();
uint32_t i, n = mFontFamiliesToLoad.Length();
mLoadStats.families = n;
for (i = 0; i < n && !mCanceled; i++) {
// 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();
}
}
mLoadTime = TimeStamp::Now() - start;
}
class FontInfoLoadCompleteEvent : public Runnable {
virtual ~FontInfoLoadCompleteEvent() {}
NS_DECL_ISUPPORTS_INHERITED
explicit FontInfoLoadCompleteEvent(FontInfoData *aFontInfo) :
mFontInfo(aFontInfo)
{}
NS_IMETHOD Run() override;
RefPtr<FontInfoData> mFontInfo;
};
class AsyncFontInfoLoader : public Runnable {
virtual ~AsyncFontInfoLoader() {}
NS_DECL_ISUPPORTS_INHERITED
explicit AsyncFontInfoLoader(FontInfoData *aFontInfo) :
mFontInfo(aFontInfo)
{
mCompleteEvent = new FontInfoLoadCompleteEvent(aFontInfo);
}
NS_IMETHOD Run() override;
RefPtr<FontInfoData> mFontInfo;
RefPtr<FontInfoLoadCompleteEvent> mCompleteEvent;
};
class ShutdownThreadEvent : public Runnable {
virtual ~ShutdownThreadEvent() {}
NS_DECL_ISUPPORTS_INHERITED
explicit ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {}
NS_IMETHOD Run() override {
mThread->Shutdown();
return NS_OK;
}
nsCOMPtr<nsIThread> mThread;
};
NS_IMPL_ISUPPORTS_INHERITED0(ShutdownThreadEvent, Runnable);
// 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;
}
NS_IMPL_ISUPPORTS_INHERITED0(FontInfoLoadCompleteEvent, Runnable);
// 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
NS_DispatchToMainThread(mCompleteEvent);
return NS_OK;
}
NS_IMPL_ISUPPORTS_INHERITED0(AsyncFontInfoLoader, Runnable);
NS_IMPL_ISUPPORTS(gfxFontInfoLoader::ShutdownObserver, nsIObserver)
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;
NS_ASSERTION(!mFontInfo,
"fontinfo should be null when starting font loader");
// sanity check
if (mState != stateInitial &&
mState != stateTimerOff &&
mState != stateTimerOnDelay) {
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;
}
}
AddShutdownObserver();
// delay? ==> start async thread after a delay
if (aDelay) {
mState = stateTimerOnDelay;
mTimer->InitWithFuncCallback(DelayedStartCallback, this, aDelay,
nsITimer::TYPE_ONE_SHOT);
return;
}
mFontInfo = CreateFontInfoData();
// initialize
InitLoader();
// start async load
nsresult rv = NS_NewNamedThread("Font Loader",
getter_AddRefs(mFontLoaderThread),
nullptr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
mState = stateAsyncLoad;
nsCOMPtr<nsIRunnable> loadEvent = new AsyncFontInfoLoader(mFontInfo);
mFontLoaderThread->Dispatch(loadEvent.forget(), NS_DISPATCH_NORMAL);
if (LOG_FONTINIT_ENABLED()) {
LOG_FONTINIT(("(fontinit) fontloader started (fontinfo: %p)\n",
mFontInfo.get()));
}
}
void
gfxFontInfoLoader::FinalizeLoader(FontInfoData *aFontInfo)
{
// 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.
if (mState != stateAsyncLoad || mFontInfo != aFontInfo) {
return;
}
mLoadTime = mFontInfo->mLoadTime;
// try to load all font data immediately
if (LoadFontInfo()) {
CancelLoader();
return;
}
// not all work completed ==> run load on interval
mState = stateTimerOnInterval;
mTimer->InitWithFuncCallback(LoadFontInfoCallback, this, mInterval,
nsITimer::TYPE_REPEATING_SLACK);
}
void
gfxFontInfoLoader::CancelLoader()
{
if (mState == stateInitial) {
return;
}
mState = stateTimerOff;
if (mTimer) {
mTimer->Cancel();
mTimer = nullptr;
}
if (mFontInfo) // null during any initial delay
mFontInfo->mCanceled = true;
if (mFontLoaderThread) {
NS_DispatchToMainThread(new ShutdownThreadEvent(mFontLoaderThread));
mFontLoaderThread = nullptr;
}
RemoveShutdownObserver();
CleanupLoader();
}
void
gfxFontInfoLoader::LoadFontInfoTimerFire()
{
if (mState == stateTimerOnDelay) {
mState = stateTimerOnInterval;
mTimer->SetDelay(mInterval);
}
bool done = LoadFontInfo();
if (done) {
CancelLoader();
}
}
gfxFontInfoLoader::~gfxFontInfoLoader()
{
RemoveShutdownObserver();
MOZ_COUNT_DTOR(gfxFontInfoLoader);
}
void
gfxFontInfoLoader::AddShutdownObserver()
{
if (mObserver) {
return;
}
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
mObserver = new ShutdownObserver(this);
obs->AddObserver(mObserver, "quit-application", false);
}
}
void
gfxFontInfoLoader::RemoveShutdownObserver()
{
if (mObserver) {
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
obs->RemoveObserver(mObserver, "quit-application");
mObserver = nullptr;
}
}
}
|