-
Notifications
You must be signed in to change notification settings - Fork 54
/
SharedFontList-impl.h
307 lines (268 loc) · 12 KB
/
SharedFontList-impl.h
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* 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/. */
#ifndef SharedFontList_impl_h
#define SharedFontList_impl_h
#include "SharedFontList.h"
#include "mozilla/ipc/SharedMemoryBasic.h"
#include "gfxFontUtils.h"
#include "nsClassHashtable.h"
#include "nsDataHashtable.h"
// This is split out from SharedFontList.h because that header is included
// quite widely (via gfxPlatformFontList.h, gfxTextRun.h, etc), and other code
// such as the generated DOM bindings code gets upset at (indirect) inclusion
// of <windows.h> via SharedMemoryBasic.h. So this header, which defines the
// actual shared-memory FontList class, is included only by the .cpp files that
// implement or directly interface with the font list, to avoid polluting other
// headers.
namespace mozilla {
namespace fontlist {
/**
* The Shared Font List is a collection of data that lives in shared memory
* so that all processes can use it, rather than maintaining their own copies,
* and provides the metadata needed for CSS font-matching (a list of all the
* available font families and their faces, style properties, etc, as well as
* character coverage).
*
* An important assumption is that all processes see the same collection of
* installed fonts; therefore it is valid for them all to share the same set
* of font metadata. The data is updated only by the parent process; content
* processes have read-only access to it.
*
* The total size of this data varies greatly depending on the user's installed
* fonts; and it is not known at startup because we load a lot of the font data
* on first use rather than preloading during initialization (because that's
* too expensive/slow).
*
* Therefore, the shared memory area needs to be able to grow during the
* session; we can't predict how much space will be needed, and we can't afford
* to pre-allocate such a huge block that it could never overflow. To handle
* this, we maintain a (generally short) array of blocks of shared memory,
* and then allocate our Family, Face, etc. objects within these. Because we
* only ever add data (never delete what we've already stored), we can use a
* simplified allocator that doesn't ever need to free blocks; the only time
* the memory is released during a session is the (rare) case where a font is
* installed or deleted while the browser is running, and in this case we just
* delete the entire shared font list and start afresh.
*/
class FontList {
public:
friend struct Pointer;
explicit FontList(uint32_t aGeneration);
~FontList();
/**
* Initialize the master list of installed font families. This must be
* set during font-list creation, before the list is shared with any
* content processes. All installed font families known to the browser
* appear in this list, although some may be marked as "hidden" so that
* they are not exposed to the font-family property.
*
* Once initialized, the master family list is immutable; in the (rare)
* event that the system's collection of installed fonts changes, we discard
* the FontList and create a new one.
*
* In some cases, a font family may be known by multiple names (e.g.
* localizations in multiple languages, or there may be legacy family names
* that correspond to specific styled faces like "Arial Black"). Such names
* do not appear in this master list, but are referred to as aliases (see
* SetAliases below); the alias list need not be populated before the font
* list is shared to content processes and used.
*
* Only used in the parent process.
*/
void SetFamilyNames(const nsTArray<Family::InitData>& aFamilies);
/**
* Aliases are Family records whose Face entries are already part of another
* family (either because the family has multiple localized names, or because
* the alias family is a legacy name like "Arial Narrow" that is a subset of
* the faces in the main "Arial" family). The table of aliases is initialized
* from a hash of alias family name -> array of Face records.
*
* Like the master family list, the list of family aliases is immutable once
* initialized.
*
* Only used in the parent process.
*/
void SetAliases(
nsClassHashtable<nsCStringHashKey, nsTArray<Pointer>>& aAliasTable);
/**
* Local names are PostScript or Full font names of individual faces, used
* to look up faces for @font-face { src: local(...) } rules. Some platforms
* (e.g. macOS) can look up local names directly using platform font APIs,
* in which case the local names table here is unused.
*
* The list of local names is immutable once initialized. Local font name
* lookups may occur before this list has been set up, in which case they
* will use the SearchForLocalFace method.
*
* Only used in the parent process.
*/
void SetLocalNames(nsDataHashtable<nsCStringHashKey, LocalFaceRec::InitData>&
aLocalNameTable);
/**
* Look up a Family record by name, typically to satisfy the font-family
* property or a font family listed in preferences.
*/
Family* FindFamily(const nsCString& aName);
/**
* Look up an individual Face by PostScript or Full name, for @font-face
* rules using src:local(...). This requires the local names list to have
* been initialized.
*/
LocalFaceRec* FindLocalFace(const nsCString& aName);
/**
* Search families for a face with local name aName; should only be used if
* the mLocalFaces array has not yet been set up, as this will be a more
* expensive search than FindLocalFace.
*/
void SearchForLocalFace(const nsACString& aName, Family** aFamily,
Face** aFace);
bool Initialized() { return mBlocks.Length() > 0 && NumFamilies() > 0; }
uint32_t NumFamilies() { return GetHeader().mFamilyCount; }
Family* Families() {
return static_cast<Family*>(GetHeader().mFamilies.ToPtr(this));
}
uint32_t NumAliases() { return GetHeader().mAliasCount; }
Family* AliasFamilies() {
return static_cast<Family*>(GetHeader().mAliases.ToPtr(this));
}
uint32_t NumLocalFaces() { return GetHeader().mLocalFaceCount; }
LocalFaceRec* LocalFaces() {
return static_cast<LocalFaceRec*>(GetHeader().mLocalFaces.ToPtr(this));
}
/**
* Ask the font list to initialize the character map for a given face.
*/
void LoadCharMapFor(Face& aFace, const Family* aFamily);
/**
* Allocate shared-memory space for a record of aSize bytes. The returned
* pointer will be 32-bit aligned. (This method may trigger the allocation of
* a new shared memory block, if required.)
*
* Only used in the parent process.
*/
Pointer Alloc(uint32_t aSize);
/**
* Convert a native pointer to a shared-memory Pointer record that can be
* passed between processes.
*/
Pointer ToSharedPointer(const void* aPtr);
uint32_t GetGeneration() { return GetHeader().mGeneration; }
/**
* Header info that is stored at the beginning of the first shared-memory
* block for the font list.
* (Subsequent blocks have only the mAllocated field, accessed via the
* Block::Allocated() method.)
* The mGeneration and mFamilyCount fields are set by the parent process
* during font-list construction, before the list has been shared with any
* other process, and subsequently never change; therefore, we don't need
* to use std::atomic<> for these.
*/
struct Header {
std::atomic<uint32_t> mAllocated; // Space allocated from this block;
// must be first field in Header
uint32_t mGeneration; // Font-list generation ID
uint32_t mFamilyCount; // Number of font families in the list
std::atomic<uint32_t> mBlockCount; // Total number of blocks that exist
std::atomic<uint32_t> mAliasCount; // Number of family aliases
std::atomic<uint32_t> mLocalFaceCount; // Number of local face names
Pointer mFamilies; // Pointer to array of |mFamilyCount| families
Pointer mAliases; // Pointer to array of |mAliasCount| aliases
Pointer mLocalFaces; // Pointer to array of |mLocalFaceCount| face records
};
/**
* Used by the parent process to pass a handle to a shared block to a
* specific child process.
*/
void ShareShmBlockToProcess(uint32_t aIndex, base::ProcessId aPid,
mozilla::ipc::SharedMemoryBasic::Handle* aOut) {
if (aIndex >= mBlocks.Length()) {
// Block index out of range
*aOut = mozilla::ipc::SharedMemoryBasic::NULLHandle();
}
if (!mBlocks[aIndex]->mShmem->ShareToProcess(aPid, aOut)) {
MOZ_CRASH("failed to share block");
}
}
/**
* Support for memory reporter.
*/
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
size_t result = mBlocks.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (const auto& b : mBlocks) {
result += aMallocSizeOf(b.get()) + aMallocSizeOf(b->mShmem);
}
return result;
}
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
size_t AllocatedShmemSize() const {
return mBlocks.Length() * SHM_BLOCK_SIZE;
}
/**
* This must be large enough that we can allocate the largest possible
* SharedBitSet (around 140K, see comments on SharedBitSet in gfxFontUtils.h)
* within a single ShmBlock.
* Using a larger block size will speed up allocation, at the cost of more
* wasted space in the shared memory (on average).
*/
static const size_t SHM_BLOCK_SIZE = 256 * 1024;
static_assert(SHM_BLOCK_SIZE <= (1 << Pointer::kBlockShift),
"SHM_BLOCK_SIZE too large");
static_assert(SHM_BLOCK_SIZE >= SharedBitSet::kMaxSize + 4,
"SHM_BLOCK_SIZE too small");
private:
struct ShmBlock {
ShmBlock(mozilla::ipc::SharedMemoryBasic* aShmem, void* aAddr)
: mShmem(aShmem), mAddr(aAddr) {}
// The first 32-bit word of each block holds the current amount allocated
// in that block; this is updated whenever a new record is stored in the
// block.
std::atomic<uint32_t>& Allocated() const {
return *static_cast<std::atomic<uint32_t>*>(mAddr);
}
RefPtr<mozilla::ipc::SharedMemoryBasic> mShmem;
void* mAddr; // Address where the shared memory block is mapped in this
// process; avoids virtual call to mShmem->memory() each time
// we need to convert between Pointer and a real C++ pointer.
};
Header& GetHeader() {
// It's invalid to try and access this before the first block exists.
MOZ_ASSERT(mBlocks.Length() > 0);
return *static_cast<Header*>(Pointer(0, 0).ToPtr(this));
}
/**
* Create a new shared memory block and append to the FontList's list
* of blocks.
*
* Only used in the parent process.
*/
bool AppendShmBlock();
/**
* Used by child processes to ensure all the blocks are registered.
* Returns false on failure.
*/
MOZ_MUST_USE bool UpdateShmBlocks();
/**
* This makes a *sync* IPC call to get a shared block from the parent.
* As such, it may block for a while if the parent is busy; fortunately,
* we'll generally only call this a handful of times in the course of an
* entire session. If the requested block does not yet exist (because the
* child is wanting to allocate an object, and there wasn't room in any
* existing block), the parent will create a new shared block and return it.
* This may (in rare cases) return null, if the parent has recreated the
* font list and we actually need to reinitialize.
*/
ShmBlock* GetBlockFromParent(uint32_t aIndex);
void DetachShmBlocks();
/**
* Array of pointers to the shared-memory block records.
* NOTE: if mBlocks.Length() < GetHeader().mBlockCount, then the parent has
* added a block (or blocks) to the list, and we need to update!
*/
nsTArray<mozilla::UniquePtr<ShmBlock>> mBlocks;
};
} // namespace fontlist
} // namespace mozilla
#endif /* SharedFontList_impl_h */