Debian Codegraph @

src/core/SkStrike.cpp

raw
1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/core/SkStrike.h"
9
10 #include "include/core/SkDrawable.h"
11 #include "include/core/SkFontStyle.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkString.h"
15 #include "include/core/SkTraceMemoryDump.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/private/base/SkDebug.h"
18 #include "include/private/base/SkTFitsIn.h"
19 #include "src/core/SkGlyph.h"
20 #include "src/core/SkMask.h"
21 #include "src/core/SkReadBuffer.h"
22 #include "src/core/SkScalerContext.h"
23 #include "src/core/SkStrikeCache.h"
24 #include "src/core/SkWriteBuffer.h"
25 #include "src/text/StrikeForGPU.h"
26
27 #include <cctype>
28 #include <new>
29 #include <optional>
30 #include <utility>
31
32 using namespace skglyph;
33
34 static SkFontMetrics use_or_generate_metrics(
35 const SkFontMetrics* metrics, SkScalerContext* context) {
36 SkFontMetrics answer;
37 if (metrics) {
38 answer = *metrics;
39 } else {
40 context->getFontMetrics(&answer);
41 }
42 return answer;
43 }
44
45 SkStrike::SkStrike(SkStrikeCache* strikeCache,
46 const SkStrikeSpec& strikeSpec,
47 std::unique_ptr<SkScalerContext> scaler,
48 const SkFontMetrics* metrics,
49 std::unique_ptr<SkStrikePinner> pinner)
50 : fFontMetrics{use_or_generate_metrics(metrics, scaler.get())}
51 , fRoundingSpec{scaler->isSubpixel(),
52 scaler->computeAxisAlignmentForHText()}
53 , fStrikeSpec{strikeSpec}
54 , fStrikeCache{strikeCache}
55 , fScalerContext{std::move(scaler)}
56 , fPinner{std::move(pinner)} {
57 SkASSERT(fScalerContext != nullptr);
58 }
59
60 class SK_SCOPED_CAPABILITY SkStrike::Monitor {
61 public:
62 Monitor(SkStrike* strike) SK_ACQUIRE(strike->fStrikeLock)
63 : fStrike{strike} {
64 fStrike->lock();
65 }
66
67 ~Monitor() SK_RELEASE_CAPABILITY() {
68 fStrike->unlock();
69 }
70
71 private:
72 SkStrike* const fStrike;
73 };
74
75 void SkStrike::lock() {
76 fStrikeLock.acquire();
77 fMemoryIncrease = 0;
78 }
79
80 void SkStrike::unlock() {
81 const size_t memoryIncrease = fMemoryIncrease;
82 fStrikeLock.release();
83 this->updateMemoryUsage(memoryIncrease);
84 }
85
86 void
87 SkStrike::FlattenGlyphsByType(SkWriteBuffer& buffer,
88 SkSpan<SkGlyph> images,
89 SkSpan<SkGlyph> paths,
90 SkSpan<SkGlyph> drawables) {
91 SkASSERT_RELEASE(SkTFitsIn<int>(images.size()) &&
92 SkTFitsIn<int>(paths.size()) &&
93 SkTFitsIn<int>(drawables.size()));
94
95 buffer.writeInt(images.size());
96 for (SkGlyph& glyph : images) {
97 SkASSERT(SkMask::IsValidFormat(glyph.maskFormat()));
98 glyph.flattenMetrics(buffer);
99 glyph.flattenImage(buffer);
100 }
101
102 buffer.writeInt(paths.size());
103 for (SkGlyph& glyph : paths) {
104 SkASSERT(SkMask::IsValidFormat(glyph.maskFormat()));
105 glyph.flattenMetrics(buffer);
106 glyph.flattenPath(buffer);
107 }
108
109 buffer.writeInt(drawables.size());
110 for (SkGlyph& glyph : drawables) {
111 SkASSERT(SkMask::IsValidFormat(glyph.maskFormat()));
112 glyph.flattenMetrics(buffer);
113 glyph.flattenDrawable(buffer);
114 }
115 }
116
117 bool SkStrike::mergeFromBuffer(SkReadBuffer& buffer) {
118 // Read glyphs with images for the current strike.
119 const int imagesCount = buffer.readInt();
120 if (imagesCount == 0 && !buffer.isValid()) {
121 return false;
122 }
123
124 {
125 Monitor m{this};
126 for (int curImage = 0; curImage < imagesCount; ++curImage) {
127 if (!this->mergeGlyphAndImageFromBuffer(buffer)) {
128 return false;
129 }
130 }
131 }
132
133 // Read glyphs with paths for the current strike.
134 const int pathsCount = buffer.readInt();
135 if (pathsCount == 0 && !buffer.isValid()) {
136 return false;
137 }
138 {
139 Monitor m{this};
140 for (int curPath = 0; curPath < pathsCount; ++curPath) {
141 if (!this->mergeGlyphAndPathFromBuffer(buffer)) {
142 return false;
143 }
144 }
145 }
146
147 // Read glyphs with drawables for the current strike.
148 const int drawablesCount = buffer.readInt();
149 if (drawablesCount == 0 && !buffer.isValid()) {
150 return false;
151 }
152 {
153 Monitor m{this};
154 for (int curDrawable = 0; curDrawable < drawablesCount; ++curDrawable) {
155 if (!this->mergeGlyphAndDrawableFromBuffer(buffer)) {
156 return false;
157 }
158 }
159 }
160
161 return true;
162 }
163
164 SkGlyph* SkStrike::mergeGlyphAndImage(SkPackedGlyphID toID, const SkGlyph& fromGlyph) {
165 Monitor m{this};
166 // TODO(herb): remove finding the glyph when setting the metrics and image are separated
167 SkGlyphDigest* digest = fDigestForPackedGlyphID.find(toID);
168 if (digest != nullptr) {
169 SkGlyph* glyph = fGlyphForIndex[digest->index()];
170 if (fromGlyph.setImageHasBeenCalled()) {
171 if (glyph->setImageHasBeenCalled()) {
172 // Should never set an image on a glyph which already has an image.
173 SkDEBUGFAIL("Re-adding image to existing glyph. This should not happen.");
174 }
175 // TODO: assert that any metrics on fromGlyph are the same.
176 fMemoryIncrease += glyph->setMetricsAndImage(&fAlloc, fromGlyph);
177 }
178 return glyph;
179 } else {
180 SkGlyph* glyph = fAlloc.make<SkGlyph>(toID);
181 fMemoryIncrease += glyph->setMetricsAndImage(&fAlloc, fromGlyph) + sizeof(SkGlyph);
182 (void)this->addGlyphAndDigest(glyph);
183 return glyph;
184 }
185 }
186
187 const SkPath* SkStrike::mergePath(SkGlyph* glyph, const SkPath* path, bool hairline, bool modified) {
188 Monitor m{this};
189 if (glyph->setPathHasBeenCalled()) {
190 SkDEBUGFAIL("Re-adding path to existing glyph. This should not happen.");
191 }
192 if (glyph->setPath(&fAlloc, path, hairline, modified)) {
193 fMemoryIncrease += glyph->path()->approximateBytesUsed();
194 }
195
196 return glyph->path();
197 }
198
199 const SkDrawable* SkStrike::mergeDrawable(SkGlyph* glyph, sk_sp<SkDrawable> drawable) {
200 Monitor m{this};
201 if (glyph->setDrawableHasBeenCalled()) {
202 SkDEBUGFAIL("Re-adding drawable to existing glyph. This should not happen.");
203 }
204 if (glyph->setDrawable(&fAlloc, std::move(drawable))) {
205 fMemoryIncrease += glyph->drawable()->approximateBytesUsed();
206 SkASSERT(fMemoryIncrease > 0);
207 }
208
209 return glyph->drawable();
210 }
211
212 void SkStrike::findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos,
213 SkGlyph* glyph, SkScalar* array, int* count) {
214 SkAutoMutexExclusive lock{fStrikeLock};
215 glyph->ensureIntercepts(bounds, scale, xPos, array, count, &fAlloc);
216 }
217
218 SkSpan<const SkGlyph*> SkStrike::metrics(
219 SkSpan<const SkGlyphID> glyphIDs, const SkGlyph* results[]) {
220 Monitor m{this};
221 return this->internalPrepare(glyphIDs, kMetricsOnly, results);
222 }
223
224 SkSpan<const SkGlyph*> SkStrike::preparePaths(
225 SkSpan<const SkGlyphID> glyphIDs, const SkGlyph* results[]) {
226 Monitor m{this};
227 return this->internalPrepare(glyphIDs, kMetricsAndPath, results);
228 }
229
230 SkSpan<const SkGlyph*> SkStrike::prepareImages(
231 SkSpan<const SkPackedGlyphID> glyphIDs, const SkGlyph* results[]) {
232 const SkGlyph** cursor = results;
233 Monitor m{this};
234 for (auto glyphID : glyphIDs) {
235 SkGlyph* glyph = this->glyph(glyphID);
236 this->prepareForImage(glyph);
237 *cursor++ = glyph;
238 }
239
240 return {results, glyphIDs.size()};
241 }
242
243 SkSpan<const SkGlyph*> SkStrike::prepareDrawables(
244 SkSpan<const SkGlyphID> glyphIDs, const SkGlyph* results[]) {
245 const SkGlyph** cursor = results;
246 {
247 Monitor m{this};
248 for (auto glyphID : glyphIDs) {
249 SkGlyph* glyph = this->glyph(SkPackedGlyphID{glyphID});
250 this->prepareForDrawable(glyph);
251 *cursor++ = glyph;
252 }
253 }
254
255 return {results, glyphIDs.size()};
256 }
257
258 void SkStrike::glyphIDsToPaths(SkSpan<sktext::IDOrPath> idsOrPaths) {
259 Monitor m{this};
260 for (sktext::IDOrPath& idOrPath : idsOrPaths) {
261 SkGlyph* glyph = this->glyph(SkPackedGlyphID{idOrPath.fGlyphID});
262 this->prepareForPath(glyph);
263 new (&idOrPath.fPath) SkPath{*glyph->path()};
264 }
265 }
266
267 void SkStrike::glyphIDsToDrawables(SkSpan<sktext::IDOrDrawable> idsOrDrawables) {
268 Monitor m{this};
269 for (sktext::IDOrDrawable& idOrDrawable : idsOrDrawables) {
270 SkGlyph* glyph = this->glyph(SkPackedGlyphID{idOrDrawable.fGlyphID});
271 this->prepareForDrawable(glyph);
272 SkASSERT(glyph->drawable() != nullptr);
273 idOrDrawable.fDrawable = glyph->drawable();
274 }
275 }
276
277 void SkStrike::dump() const {
278 SkAutoMutexExclusive lock{fStrikeLock};
279 const SkTypeface* face = fScalerContext->getTypeface();
280 const SkScalerContextRec& rec = fScalerContext->getRec();
281 SkString name;
282 face->getFamilyName(&name);
283
284 SkString msg;
285 SkFontStyle style = face->fontStyle();
286 msg.printf("cache typeface:%x %25s:(%d,%d,%d)\n %s glyphs:%3d",
287 face->uniqueID(), name.c_str(), style.weight(), style.width(), style.slant(),
288 rec.dump().c_str(), fDigestForPackedGlyphID.count());
289 SkDebugf("%s\n", msg.c_str());
290 }
291
292 void SkStrike::dumpMemoryStatistics(SkTraceMemoryDump* dump) const {
293 SkAutoMutexExclusive lock{fStrikeLock};
294 const SkTypeface* face = fScalerContext->getTypeface();
295 const SkScalerContextRec& rec = fScalerContext->getRec();
296
297 SkString fontName;
298 face->getFamilyName(&fontName);
299 // Replace all special characters with '_'.
300 for (size_t index = 0; index < fontName.size(); ++index) {
301 if (!std::isalnum(fontName[index])) {
302 fontName[index] = '_';
303 }
304 }
305
306 SkString dumpName = SkStringPrintf("%s/%s_%u/%p",
307 SkStrikeCache::kGlyphCacheDumpName,
308 fontName.c_str(),
309 rec.fTypefaceID,
310 this);
311
312 dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", fMemoryUsed);
313 dump->dumpNumericValue(dumpName.c_str(),
314 "glyph_count", "objects",
315 fDigestForPackedGlyphID.count());
316 dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr);
317 }
318
319 SkGlyph* SkStrike::glyph(SkGlyphDigest digest) {
320 return fGlyphForIndex[digest.index()];
321 }
322
323 SkGlyph* SkStrike::glyph(SkPackedGlyphID packedGlyphID) {
324 SkGlyphDigest digest = this->digestFor(kDirectMask, packedGlyphID);
325 return this->glyph(digest);
326 }
327
328 SkGlyphDigest SkStrike::digestFor(ActionType actionType, SkPackedGlyphID packedGlyphID) {
329 SkGlyphDigest* digestPtr = fDigestForPackedGlyphID.find(packedGlyphID);
330 if (digestPtr != nullptr && digestPtr->actionFor(actionType) != GlyphAction::kUnset) {
331 return *digestPtr;
332 }
333
334 SkGlyph* glyph;
335 if (digestPtr != nullptr) {
336 glyph = fGlyphForIndex[digestPtr->index()];
337 } else {
338 glyph = fAlloc.make<SkGlyph>(fScalerContext->makeGlyph(packedGlyphID, &fAlloc));
339 fMemoryIncrease += sizeof(SkGlyph);
340 digestPtr = this->addGlyphAndDigest(glyph);
341 }
342
343 digestPtr->setActionFor(actionType, glyph, this);
344
345 return *digestPtr;
346 }
347
348 SkGlyphDigest* SkStrike::addGlyphAndDigest(SkGlyph* glyph) {
349 size_t index = fGlyphForIndex.size();
350 SkGlyphDigest digest = SkGlyphDigest{index, *glyph};
351 SkGlyphDigest* newDigest = fDigestForPackedGlyphID.set(digest);
352 fGlyphForIndex.push_back(glyph);
353 return newDigest;
354 }
355
356 bool SkStrike::prepareForImage(SkGlyph* glyph) {
357 if (glyph->setImage(&fAlloc, fScalerContext.get())) {
358 fMemoryIncrease += glyph->imageSize();
359 }
360 return glyph->image() != nullptr;
361 }
362
363 bool SkStrike::prepareForPath(SkGlyph* glyph) {
364 if (glyph->setPath(&fAlloc, fScalerContext.get())) {
365 fMemoryIncrease += glyph->path()->approximateBytesUsed();
366 }
367 return glyph->path() !=nullptr;
368 }
369
370 bool SkStrike::prepareForDrawable(SkGlyph* glyph) {
371 if (glyph->setDrawable(&fAlloc, fScalerContext.get())) {
372 size_t increase = glyph->drawable()->approximateBytesUsed();
373 SkASSERT(increase > 0);
374 fMemoryIncrease += increase;
375 }
376 return glyph->drawable() != nullptr;
377 }
378
379 SkGlyph* SkStrike::mergeGlyphFromBuffer(SkReadBuffer& buffer) {
380 SkASSERT(buffer.isValid());
381 std::optional<SkGlyph> prototypeGlyph = SkGlyph::MakeFromBuffer(buffer);
382 if (!buffer.validate(prototypeGlyph.has_value())) {
383 return nullptr;
384 }
385
386 // Check if this glyph has already been seen.
387 SkGlyphDigest* digestPtr = fDigestForPackedGlyphID.find(prototypeGlyph->getPackedID());
388 if (digestPtr != nullptr) {
389 return fGlyphForIndex[digestPtr->index()];
390 }
391
392 // This is the first time. Allocate a new glyph.
393 SkGlyph* glyph = fAlloc.make<SkGlyph>(prototypeGlyph.value());
394 fMemoryIncrease += sizeof(SkGlyph);
395 this->addGlyphAndDigest(glyph);
396 return glyph;
397 }
398
399 bool SkStrike::mergeGlyphAndImageFromBuffer(SkReadBuffer& buffer) {
400 SkASSERT(buffer.isValid());
401 SkGlyph* glyph = this->mergeGlyphFromBuffer(buffer);
402 if (!buffer.validate(glyph != nullptr)) {
403 return false;
404 }
405 fMemoryIncrease += glyph->addImageFromBuffer(buffer, &fAlloc);
406 return buffer.isValid();
407 }
408
409 bool SkStrike::mergeGlyphAndPathFromBuffer(SkReadBuffer& buffer) {
410 SkASSERT(buffer.isValid());
411 SkGlyph* glyph = this->mergeGlyphFromBuffer(buffer);
412 if (!buffer.validate(glyph != nullptr)) {
413 return false;
414 }
415 fMemoryIncrease += glyph->addPathFromBuffer(buffer, &fAlloc);
416 return buffer.isValid();
417 }
418
419 bool SkStrike::mergeGlyphAndDrawableFromBuffer(SkReadBuffer& buffer) {
420 SkASSERT(buffer.isValid());
421 SkGlyph* glyph = this->mergeGlyphFromBuffer(buffer);
422 if (!buffer.validate(glyph != nullptr)) {
423 return false;
424 }
425 fMemoryIncrease += glyph->addDrawableFromBuffer(buffer, &fAlloc);
426 return buffer.isValid();
427 }
428
429 SkSpan<const SkGlyph*> SkStrike::internalPrepare(
430 SkSpan<const SkGlyphID> glyphIDs, PathDetail pathDetail, const SkGlyph** results) {
431 const SkGlyph** cursor = results;
432 for (auto glyphID : glyphIDs) {
433 SkGlyph* glyph = this->glyph(SkPackedGlyphID{glyphID});
434 if (pathDetail == kMetricsAndPath) {
435 this->prepareForPath(glyph);
436 }
437 *cursor++ = glyph;
438 }
439
440 return {results, glyphIDs.size()};
441 }
442
443 void SkStrike::updateMemoryUsage(size_t increase) {
444 if (increase > 0) {
445 // fRemoved and the cache's total memory are managed under the cache's lock. This allows
446 // them to be accessed under LRU operation.
447 SkAutoMutexExclusive lock{fStrikeCache->fLock};
448 fMemoryUsed += increase;
449 if (!fRemoved) {
450 fStrikeCache->fTotalMemoryUsed += increase;
451 }
452 }
453 }