Debian Codegraph @

src/core/SkGlyph.cpp

raw ◀ ▶
2 Debian patches touch this file
1 /*
2 * Copyright 2018 Google LLC
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/SkGlyph.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkDrawable.h"
13 #include "include/core/SkPicture.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSerialProcs.h"
16 #include "include/core/SkSpan.h"
17 #include "include/private/base/SkFloatingPoint.h"
18 #include "include/private/base/SkTFitsIn.h"
19 #include "include/private/base/SkTo.h"
20 #include "src/base/SkArenaAlloc.h"
21 #include "src/base/SkBezierCurves.h"
•22 #include "src/core/SkPictureData.h"
23 #include "src/core/SkReadBuffer.h"
24 #include "src/core/SkScalerContext.h"
25 #include "src/core/SkWriteBuffer.h"
26 #include "src/text/StrikeForGPU.h"
27
28 #include <cstring>
29 #include <optional>
30 #include <tuple>
31 #include <utility>
32
33 using namespace skglyph;
34 using namespace sktext;
35
36 // -- SkPictureBackedGlyphDrawable -----------------------------------------------------------------
37 sk_sp<SkPictureBackedGlyphDrawable>
38 SkPictureBackedGlyphDrawable::MakeFromBuffer(SkReadBuffer& buffer) {
39 SkASSERT(buffer.isValid());
40
41 sk_sp<SkData> pictureData = buffer.readByteArrayAsData();
42
43 // Return nullptr if invalid or there an empty drawable, which is represented by nullptr.
44 if (!buffer.isValid() || pictureData->empty()) {
45 return nullptr;
46 }
47
48 // Propagate the outer buffer's allow-SkSL setting to the picture decoder, using the flag on
49 // the deserial procs.
50 SkDeserialProcs procs;
51 procs.fAllowSkSL = buffer.allowSkSL();
•52 procs.fAllowTagsProc = [](SkFourByteTag tag, void*) -> bool {
53 return tag == SK_PICT_BUFFER_SIZE_TAG ||
54 tag == SK_PICT_FACTORY_TAG ||
55 tag == SK_PICT_PAINT_BUFFER_TAG ||
56 tag == SK_PICT_PATH_BUFFER_TAG ||
57 tag == SK_PICT_READER_TAG;
58 };
59 sk_sp<SkPicture> picture = SkPicture::MakeFromData(pictureData.get(), &procs);
60 if (!buffer.validate(picture != nullptr)) {
61 return nullptr;
62 }
63
64 return sk_make_sp<SkPictureBackedGlyphDrawable>(std::move(picture));
65 }
66
67 void SkPictureBackedGlyphDrawable::FlattenDrawable(SkWriteBuffer& buffer, SkDrawable* drawable) {
68 if (drawable == nullptr) {
69 buffer.writeByteArray(nullptr, 0);
70 return;
71 }
72
73 sk_sp<SkPicture> picture = drawable->makePictureSnapshot();
74 // These drawables should not have SkImages, SkTypefaces or SkPictures inside of them, so
75 // the default SkSerialProcs are sufficient.
76 sk_sp<SkData> data = picture->serialize();
77
78 // If the picture is too big, or there is no picture, then drop by sending an empty byte array.
79 if (!SkTFitsIn<uint32_t>(data->size()) || data->empty()) {
80 buffer.writeByteArray(nullptr, 0);
81 return;
82 }
83
84 buffer.writeByteArray(data->data(), data->size());
85 }
86
87 SkPictureBackedGlyphDrawable::SkPictureBackedGlyphDrawable(sk_sp<SkPicture> picture)
88 : fPicture(std::move(picture)) {}
89
90 SkRect SkPictureBackedGlyphDrawable::onGetBounds() {
91 return fPicture->cullRect();
92 }
93
94 size_t SkPictureBackedGlyphDrawable::onApproximateBytesUsed() {
95 return sizeof(SkPictureBackedGlyphDrawable) + fPicture->approximateBytesUsed();
96 }
97
98 void SkPictureBackedGlyphDrawable::onDraw(SkCanvas* canvas) {
99 canvas->drawPicture(fPicture);
100 }
101
102 //-- SkGlyph ---------------------------------------------------------------------------------------
103 std::optional<SkGlyph> SkGlyph::MakeFromBuffer(SkReadBuffer& buffer) {
104 SkASSERT(buffer.isValid());
105 const SkPackedGlyphID packedID{buffer.readUInt()};
106 const SkVector advance = buffer.readPoint();
107 const uint32_t dimensions = buffer.readUInt();
108 const uint32_t leftTop = buffer.readUInt();
109 const SkMask::Format format = SkTo<SkMask::Format>(buffer.readUInt());
110
111 if (!buffer.validate(SkMask::IsValidFormat(format))) {
112 return std::nullopt;
113 }
114
115 SkGlyph glyph{packedID};
116 glyph.fAdvanceX = advance.x();
117 glyph.fAdvanceY = advance.y();
118 glyph.fWidth = dimensions >> 16;
119 glyph.fHeight = dimensions & 0xffffu;
120 glyph.fLeft = leftTop >> 16;
121 glyph.fTop = leftTop & 0xffffu;
122 glyph.fMaskFormat = format;
123 SkDEBUGCODE(glyph.fAdvancesBoundsFormatAndInitialPathDone = true;)
124 return glyph;
125 }
126
127 SkMask SkGlyph::mask() const {
128 SkIRect bounds = SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight);
129 return SkMask(static_cast<const uint8_t*>(fImage), bounds, this->rowBytes(), fMaskFormat);
130 }
131
132 SkMask SkGlyph::mask(SkPoint position) const {
133 SkASSERT(SkScalarIsInt(position.x()) && SkScalarIsInt(position.y()));
134 SkIRect bounds = SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight);
135 bounds.offset(SkScalarFloorToInt(position.x()), SkScalarFloorToInt(position.y()));
136 return SkMask(static_cast<const uint8_t*>(fImage), bounds, this->rowBytes(), fMaskFormat);
137 }
138
139 void SkGlyph::zeroMetrics() {
140 fAdvanceX = 0;
141 fAdvanceY = 0;
142 fWidth = 0;
143 fHeight = 0;
144 fTop = 0;
145 fLeft = 0;
146 }
147
148 static size_t bits_to_bytes(size_t bits) {
149 return (bits + 7) >> 3;
150 }
151
152 static size_t format_alignment(SkMask::Format format) {
153 switch (format) {
154 case SkMask::kBW_Format: [[fallthrough]];
155 case SkMask::kA8_Format: [[fallthrough]];
156 case SkMask::k3D_Format: [[fallthrough]];
157 case SkMask::kSDF_Format:
158 return alignof(uint8_t);
159 case SkMask::kARGB32_Format:
160 return alignof(uint32_t);
161 case SkMask::kLCD16_Format:
162 return alignof(uint16_t);
163 }
164 SkUNREACHABLE;
165 }
166
167 static size_t format_rowbytes(int width, SkMask::Format format) {
168 return format == SkMask::kBW_Format ? bits_to_bytes(width)
169 : width * format_alignment(format);
170 }
171
172 size_t SkGlyph::formatAlignment() const {
173 return format_alignment(this->maskFormat());
174 }
175
176 size_t SkGlyph::allocImage(SkArenaAlloc* alloc) {
177 SkASSERT(!this->isEmpty());
178 auto size = this->imageSize();
179 fImage = alloc->makeBytesAlignedTo(size, this->formatAlignment());
180
181 return size;
182 }
183
184 bool SkGlyph::setImage(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
185 if (!this->setImageHasBeenCalled()) {
186 // It used to be that getImage() could change the fMaskFormat. Extra checking to make
187 // sure there are no regressions.
188 SkDEBUGCODE(SkMask::Format oldFormat = this->maskFormat());
189 this->allocImage(alloc);
190 scalerContext->getImage(*this);
191 SkASSERT(oldFormat == this->maskFormat());
192 return true;
193 }
194 return false;
195 }
196
197 bool SkGlyph::setImage(SkArenaAlloc* alloc, const void* image) {
198 if (!this->setImageHasBeenCalled()) {
199 this->allocImage(alloc);
200 memcpy(fImage, image, this->imageSize());
201 return true;
202 }
203 return false;
204 }
205
206 size_t SkGlyph::setMetricsAndImage(SkArenaAlloc* alloc, const SkGlyph& from) {
207 // Since the code no longer tries to find replacement glyphs, the image should always be
208 // nullptr.
209 SkASSERT(fImage == nullptr || from.fImage == nullptr);
210
211 // TODO(herb): remove "if" when we are sure there are no colliding glyphs.
212 if (fImage == nullptr) {
213 fAdvanceX = from.fAdvanceX;
214 fAdvanceY = from.fAdvanceY;
215 fWidth = from.fWidth;
216 fHeight = from.fHeight;
217 fTop = from.fTop;
218 fLeft = from.fLeft;
219 fScalerContextBits = from.fScalerContextBits;
220 fMaskFormat = from.fMaskFormat;
221
222 // From glyph may not have an image because the glyph is too large.
223 if (from.fImage != nullptr && this->setImage(alloc, from.image())) {
224 return this->imageSize();
225 }
226
227 SkDEBUGCODE(fAdvancesBoundsFormatAndInitialPathDone = from.fAdvancesBoundsFormatAndInitialPathDone;)
228 }
229 return 0;
230 }
231
232 size_t SkGlyph::rowBytes() const {
233 return format_rowbytes(fWidth, fMaskFormat);
234 }
235
236 size_t SkGlyph::rowBytesUsingFormat(SkMask::Format format) const {
237 return format_rowbytes(fWidth, format);
238 }
239
240 size_t SkGlyph::imageSize() const {
241 if (this->isEmpty() || this->imageTooLarge()) { return 0; }
242
243 size_t size = this->rowBytes() * fHeight;
244
245 if (fMaskFormat == SkMask::k3D_Format) {
246 size *= 3;
247 }
248
249 return size;
250 }
251
252 void SkGlyph::installPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline, bool modified) {
253 SkASSERT(fPathData == nullptr);
254 SkASSERT(!this->setPathHasBeenCalled());
255 fPathData = alloc->make<SkGlyph::PathData>();
256 if (path != nullptr) {
257 fPathData->fPath = *path;
258 fPathData->fPath.updateBoundsCache();
259 fPathData->fPath.getGenerationID();
260 fPathData->fHasPath = true;
261 fPathData->fHairline = hairline;
262 fPathData->fModified = modified;
263 }
264 }
265
266 bool SkGlyph::setPath(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
267 if (!this->setPathHasBeenCalled()) {
268 scalerContext->getPath(*this, alloc);
269 SkASSERT(this->setPathHasBeenCalled());
270 return this->path() != nullptr;
271 }
272
273 return false;
274 }
275
276 bool SkGlyph::setPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline, bool modified) {
277 if (!this->setPathHasBeenCalled()) {
278 this->installPath(alloc, path, hairline, modified);
279 return this->path() != nullptr;
280 }
281 return false;
282 }
283
284 const SkPath* SkGlyph::path() const {
285 // setPath must have been called previously.
286 SkASSERT(this->setPathHasBeenCalled());
287 if (fPathData->fHasPath) {
288 return &fPathData->fPath;
289 }
290 return nullptr;
291 }
292
293 bool SkGlyph::pathIsHairline() const {
294 // setPath must have been called previously.
295 SkASSERT(this->setPathHasBeenCalled());
296 return fPathData->fHairline;
297 }
298
299 bool SkGlyph::pathIsModified() const {
300 // setPath must have been called previously.
301 SkASSERT(this->setPathHasBeenCalled());
302 return fPathData->fModified;
303 }
304
305 void SkGlyph::installDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) {
306 SkASSERT(fDrawableData == nullptr);
307 SkASSERT(!this->setDrawableHasBeenCalled());
308 fDrawableData = alloc->make<SkGlyph::DrawableData>();
309 if (drawable != nullptr) {
310 fDrawableData->fDrawable = std::move(drawable);
311 fDrawableData->fDrawable->getGenerationID();
312 fDrawableData->fHasDrawable = true;
313 }
314 }
315
316 bool SkGlyph::setDrawable(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
317 if (!this->setDrawableHasBeenCalled()) {
318 sk_sp<SkDrawable> drawable = scalerContext->getDrawable(*this);
319 this->installDrawable(alloc, std::move(drawable));
320 return this->drawable() != nullptr;
321 }
322 return false;
323 }
324
325 bool SkGlyph::setDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) {
326 if (!this->setDrawableHasBeenCalled()) {
327 this->installDrawable(alloc, std::move(drawable));
328 return this->drawable() != nullptr;
329 }
330 return false;
331 }
332
333 SkDrawable* SkGlyph::drawable() const {
334 // setDrawable must have been called previously.
335 SkASSERT(this->setDrawableHasBeenCalled());
336 if (fDrawableData->fHasDrawable) {
337 return fDrawableData->fDrawable.get();
338 }
339 return nullptr;
340 }
341
342 void SkGlyph::flattenMetrics(SkWriteBuffer& buffer) const {
343 buffer.writeUInt(fID.value());
344 buffer.writePoint({fAdvanceX, fAdvanceY});
345 buffer.writeUInt(fWidth << 16 | fHeight);
346 // Note: << has undefined behavior for negative values, so convert everything to the bit
347 // values of uint16_t. Using the cast keeps the signed values fLeft and fTop from sign
348 // extending.
349 const uint32_t left = static_cast<uint16_t>(fLeft);
350 const uint32_t top = static_cast<uint16_t>(fTop);
351 buffer.writeUInt(left << 16 | top);
352 buffer.writeUInt(SkTo<uint32_t>(fMaskFormat));
353 }
354
355 void SkGlyph::flattenImage(SkWriteBuffer& buffer) const {
356 SkASSERT(this->setImageHasBeenCalled());
357
358 // If the glyph is empty or too big, then no image data is sent.
359 if (!this->isEmpty() && SkGlyphDigest::FitsInAtlas(*this)) {
360 buffer.writeByteArray(this->image(), this->imageSize());
361 }
362 }
363
364 size_t SkGlyph::addImageFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) {
365 SkASSERT(buffer.isValid());
366
367 // If the glyph is empty or too big, then no image data is received.
368 if (this->isEmpty() || !SkGlyphDigest::FitsInAtlas(*this)) {
369 return 0;
370 }
371
372 size_t memoryIncrease = 0;
373
374 void* imageData = alloc->makeBytesAlignedTo(this->imageSize(), this->formatAlignment());
375 buffer.readByteArray(imageData, this->imageSize());
376 if (buffer.isValid()) {
377 this->installImage(imageData);
378 memoryIncrease += this->imageSize();
379 }
380
381 return memoryIncrease;
382 }
383
384 void SkGlyph::flattenPath(SkWriteBuffer& buffer) const {
385 SkASSERT(this->setPathHasBeenCalled());
386
387 const bool hasPath = this->path() != nullptr;
388 buffer.writeBool(hasPath);
389 if (hasPath) {
390 buffer.writeBool(this->pathIsHairline());
391 buffer.writeBool(this->pathIsModified());
392 buffer.writePath(*this->path());
393 }
394 }
395
396 size_t SkGlyph::addPathFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) {
397 SkASSERT(buffer.isValid());
398
399 size_t memoryIncrease = 0;
400 const bool hasPath = buffer.readBool();
401 // Check if the buffer is invalid, so as to not make a logical decision on invalid data.
402 if (!buffer.isValid()) {
403 return 0;
404 }
405 if (hasPath) {
406 const bool pathIsHairline = buffer.readBool();
407 const bool pathIsModified = buffer.readBool();
408 if (auto path = buffer.readPath()) {
•409 if (fMaskFormat != SkMask::kBW_Format &&
410 fMaskFormat != SkMask::kA8_Format &&
411 fMaskFormat != SkMask::kLCD16_Format) {
412 buffer.validate(false);
413 return 0;
414 }
415 if (this->setPath(alloc, &path.value(), pathIsHairline, pathIsModified)) {
416 memoryIncrease += path->approximateBytesUsed();
417 }
418 }
419 } else {
420 this->setPath(alloc, nullptr, false, false);
421 }
422
423 return memoryIncrease;
424 }
425
426 void SkGlyph::flattenDrawable(SkWriteBuffer& buffer) const {
427 SkASSERT(this->setDrawableHasBeenCalled());
428
429 if (this->isEmpty() || this->drawable() == nullptr) {
430 SkPictureBackedGlyphDrawable::FlattenDrawable(buffer, nullptr);
431 return;
432 }
433
434 SkPictureBackedGlyphDrawable::FlattenDrawable(buffer, this->drawable());
435 }
436
437 size_t SkGlyph::addDrawableFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) {
438 SkASSERT(buffer.isValid());
439
440 sk_sp<SkDrawable> drawable = SkPictureBackedGlyphDrawable::MakeFromBuffer(buffer);
441 if (!buffer.isValid()) {
442 return 0;
443 }
444
445 if (this->setDrawable(alloc, std::move(drawable))) {
446 return this->drawable()->approximateBytesUsed();
447 }
448
449 return 0;
450 }
451
452 static std::tuple<SkScalar, SkScalar> calculate_path_gap(
453 SkScalar topOffset, SkScalar bottomOffset, const SkPath& path) {
454
455 // Left and Right of an ever expanding gap around the path.
456 SkScalar left = SK_ScalarMax,
457 right = SK_ScalarMin;
458
459 auto expandGap = [&left, &right](SkScalar v) {
460 left = std::min(left, v);
461 right = std::max(right, v);
462 };
463
464 // Handle all the different verbs for the path.
465 auto addLine = [&](SkSpan<const SkPoint> pts, SkScalar offset) {
466 SkScalar t = sk_ieee_float_divide(offset - pts[0].fY, pts[1].fY - pts[0].fY);
467 if (0 <= t && t < 1) { // this handles divide by zero above
468 expandGap(pts[0].fX + t * (pts[1].fX - pts[0].fX));
469 }
470 };
471
472 auto addQuad = [&](SkSpan<const SkPoint> pts, SkScalar offset) {
473 float intersectionStorage[2];
474 auto intersections = SkBezierQuad::IntersectWithHorizontalLine(
475 pts, offset, intersectionStorage);
476
477 for(float intersection : intersections) {
478 expandGap(intersection);
479 }
480 };
481
482 auto addCubic = [&](SkSpan<const SkPoint> pts, SkScalar offset) {
483 float intersectionStorage[3];
484 auto intersections = SkBezierCubic::IntersectWithHorizontalLine(
485 pts, offset, intersectionStorage);
486
487 for(float intersection : intersections) {
488 expandGap(intersection);
489 }
490 };
491
492 // Handle when a verb's points are in the gap between top and bottom.
493 auto addPts = [&expandGap, topOffset, bottomOffset](SkSpan<const SkPoint> pts) {
494 for (const SkPoint p : pts) {
495 if (topOffset < p.fY && p.fY < bottomOffset) {
496 expandGap(p.fX);
497 }
498 }
499 };
500
501 SkPath::Iter iter(path, false);
502 while (auto rec = iter.next()) {
503 const SkSpan<const SkPoint> pts = rec->fPoints;
504 switch (rec->fVerb) {
505 case SkPathVerb::kMove: {
506 break;
507 }
508 case SkPathVerb::kLine: {
509 auto [lineTop, lineBottom] = std::minmax({pts[0].fY, pts[1].fY});
510
511 // The y-coordinates of the points intersect the top and bottom offsets.
512 if (topOffset <= lineBottom && lineTop <= bottomOffset) {
513 addLine(pts, topOffset);
514 addLine(pts, bottomOffset);
515 addPts(pts);
516 }
517 break;
518 }
519 case SkPathVerb::kQuad: {
520 auto [quadTop, quadBottom] = std::minmax({pts[0].fY, pts[1].fY, pts[2].fY});
521
522 // The y-coordinates of the points intersect the top and bottom offsets.
523 if (topOffset <= quadBottom && quadTop <= bottomOffset) {
524 addQuad(pts, topOffset);
525 addQuad(pts, bottomOffset);
526 addPts(pts);
527 }
528 break;
529 }
530 case SkPathVerb::kConic: {
531 SkDEBUGFAIL("There should be no conic primitives in glyph outlines.");
532 break;
533 }
534 case SkPathVerb::kCubic: {
535 auto [cubicTop, cubicBottom] =
536 std::minmax({pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY});
537
538 // The y-coordinates of the points intersect the top and bottom offsets.
539 if (topOffset <= cubicBottom && cubicTop <= bottomOffset) {
540 addCubic(pts, topOffset);
541 addCubic(pts, bottomOffset);
542 addPts(pts);
543 }
544 break;
545 }
546 case SkPathVerb::kClose: {
547 break;
548 }
549 }
550 }
551
552 return std::tie(left, right);
553 }
554
555 void SkGlyph::ensureIntercepts(const SkScalar* bounds, SkScalar scale, SkScalar xPos,
556 SkScalar* array, int* count, SkArenaAlloc* alloc) {
557
558 auto offsetResults = [scale, xPos](
559 const SkGlyph::Intercept* intercept,SkScalar* array, int* count) {
560 if (array) {
561 array += *count;
562 for (int index = 0; index < 2; index++) {
563 *array++ = intercept->fInterval[index] * scale + xPos;
564 }
565 }
566 *count += 2;
567 };
568
569 const SkGlyph::Intercept* match =
570 [this](const SkScalar bounds[2]) -> const SkGlyph::Intercept* {
571 if (fPathData == nullptr) {
572 return nullptr;
573 }
574 const SkGlyph::Intercept* intercept = fPathData->fIntercept;
575 while (intercept != nullptr) {
576 if (bounds[0] == intercept->fBounds[0] && bounds[1] == intercept->fBounds[1]) {
577 return intercept;
578 }
579 intercept = intercept->fNext;
580 }
581 return nullptr;
582 }(bounds);
583
584 if (match != nullptr) {
585 if (match->fInterval[0] < match->fInterval[1]) {
586 offsetResults(match, array, count);
587 }
588 return;
589 }
590
591 SkGlyph::Intercept* intercept = alloc->make<SkGlyph::Intercept>();
592 intercept->fNext = fPathData->fIntercept;
593 intercept->fBounds[0] = bounds[0];
594 intercept->fBounds[1] = bounds[1];
595 intercept->fInterval[0] = SK_ScalarMax;
596 intercept->fInterval[1] = SK_ScalarMin;
597 fPathData->fIntercept = intercept;
598 const SkPath* path = &(fPathData->fPath);
599 const SkRect& pathBounds = path->getBounds();
600 if (pathBounds.fBottom < bounds[0] || bounds[1] < pathBounds.fTop) {
601 return;
602 }
603
604 std::tie(intercept->fInterval[0], intercept->fInterval[1])
605 = calculate_path_gap(bounds[0], bounds[1], *path);
606
607 if (intercept->fInterval[0] >= intercept->fInterval[1]) {
608 intercept->fInterval[0] = SK_ScalarMax;
609 intercept->fInterval[1] = SK_ScalarMin;
610 return;
611 }
612 offsetResults(intercept, array, count);
613 }
614
615 namespace {
616 uint32_t init_actions(const SkGlyph& glyph) {
617 constexpr uint32_t kAllUnset = 0;
618 constexpr uint32_t kDrop = SkTo<uint32_t>(GlyphAction::kDrop);
619 constexpr uint32_t kAllDrop =
620 kDrop << kDirectMask |
621 kDrop << kDirectMaskCPU |
622 kDrop << kMask |
623 kDrop << kSDFT |
624 kDrop << kPath |
625 kDrop << kDrawable;
626 return glyph.isEmpty() ? kAllDrop : kAllUnset;
627 }
628 } // namespace
629
630 // -- SkGlyphDigest --------------------------------------------------------------------------------
631 SkGlyphDigest::SkGlyphDigest(size_t index, const SkGlyph& glyph)
632 : fPackedID{SkTo<uint64_t>(glyph.getPackedID().value())}
633 , fIndex{SkTo<uint64_t>(index)}
634 , fIsEmpty(glyph.isEmpty())
635 , fFormat(glyph.maskFormat())
636 , fActions{init_actions(glyph)}
637 , fLeft{SkTo<int16_t>(glyph.left())}
638 , fTop{SkTo<int16_t>(glyph.top())}
639 , fWidth{SkTo<uint16_t>(glyph.width())}
640 , fHeight{SkTo<uint16_t>(glyph.height())} {}
641
642 void SkGlyphDigest::setActionFor(skglyph::ActionType actionType,
643 SkGlyph* glyph,
644 StrikeForGPU* strike) {
645 // We don't have to do any more if the glyph is marked as kDrop because it was isEmpty().
646 if (this->actionFor(actionType) == GlyphAction::kUnset) {
647 GlyphAction action = GlyphAction::kReject;
648 switch (actionType) {
649 case kDirectMask: {
650 if (this->fitsInAtlasDirect()) {
651 action = GlyphAction::kAccept;
652 }
653 break;
654 }
655 case kDirectMaskCPU: {
656 if (strike->prepareForImage(glyph)) {
657 SkASSERT(!glyph->isEmpty());
658 action = GlyphAction::kAccept;
659 }
660 break;
661 }
662 case kMask: {
663 if (this->fitsInAtlasInterpolated()) {
664 action = GlyphAction::kAccept;
665 }
666 break;
667 }
668 case kSDFT: {
669 if (this->fitsInAtlasDirect() &&
670 this->maskFormat() == SkMask::Format::kSDF_Format) {
671 action = GlyphAction::kAccept;
672 }
673 break;
674 }
675 case kPath: {
676 if (strike->prepareForPath(glyph)) {
677 action = GlyphAction::kAccept;
678 }
679 break;
680 }
681 case kDrawable: {
682 if (strike->prepareForDrawable(glyph)) {
683 action = GlyphAction::kAccept;
684 }
685 break;
686 }
687 }
688 this->setAction(actionType, action);
689 }
690 }
691
692 bool SkGlyphDigest::FitsInAtlas(const SkGlyph& glyph) {
693 return glyph.maxDimension() <= kSkSideTooBigForAtlas;
694 }
695
696 // -- SkGlyphPositionRoundingSpec ------------------------------------------------------------------
697 SkVector SkGlyphPositionRoundingSpec::HalfAxisSampleFreq(
698 bool isSubpixel, SkAxisAlignment axisAlignment) {
699 if (!isSubpixel) {
700 return {SK_ScalarHalf, SK_ScalarHalf};
701 } else {
702 switch (axisAlignment) {
703 case SkAxisAlignment::kX:
704 return {SkPackedGlyphID::kSubpixelRound, SK_ScalarHalf};
705 case SkAxisAlignment::kY:
706 return {SK_ScalarHalf, SkPackedGlyphID::kSubpixelRound};
707 case SkAxisAlignment::kNone:
708 return {SkPackedGlyphID::kSubpixelRound, SkPackedGlyphID::kSubpixelRound};
709 }
710 }
711
712 // Some compilers need this.
713 return {0, 0};
714 }
715
716 SkIPoint SkGlyphPositionRoundingSpec::IgnorePositionMask(
717 bool isSubpixel, SkAxisAlignment axisAlignment) {
718 return SkIPoint::Make((!isSubpixel || axisAlignment == SkAxisAlignment::kY) ? 0 : ~0,
719 (!isSubpixel || axisAlignment == SkAxisAlignment::kX) ? 0 : ~0);
720 }
721
722 SkIPoint SkGlyphPositionRoundingSpec::IgnorePositionFieldMask(bool isSubpixel,
723 SkAxisAlignment axisAlignment) {
724 SkIPoint ignoreMask = IgnorePositionMask(isSubpixel, axisAlignment);
725 SkIPoint answer{ignoreMask.x() & SkPackedGlyphID::kXYFieldMask.x(),
726 ignoreMask.y() & SkPackedGlyphID::kXYFieldMask.y()};
727 return answer;
728 }
729
730 SkGlyphPositionRoundingSpec::SkGlyphPositionRoundingSpec(
731 bool isSubpixel, SkAxisAlignment axisAlignment)
732 : halfAxisSampleFreq{HalfAxisSampleFreq(isSubpixel, axisAlignment)}
733 , ignorePositionMask{IgnorePositionMask(isSubpixel, axisAlignment)}
734 , ignorePositionFieldMask {IgnorePositionFieldMask(isSubpixel, axisAlignment)} {}