• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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/text/gpu/GlyphVector.h"
9 
10 #include "src/core/SkReadBuffer.h"
11 #include "src/core/SkStrike.h"
12 #include "src/core/SkStrikeCache.h"
13 #include "src/core/SkWriteBuffer.h"
14 #include "src/text/StrikeForGPU.h"
15 
16 #include <optional>
17 
18 class SkStrikeClient;
19 
20 using MaskFormat = skgpu::MaskFormat;
21 
22 namespace sktext::gpu {
23 // -- GlyphVector ----------------------------------------------------------------------------------
GlyphVector(SkStrikePromise && strikePromise,SkSpan<Variant> glyphs)24 GlyphVector::GlyphVector(SkStrikePromise&& strikePromise, SkSpan<Variant> glyphs)
25         : fStrikePromise{std::move(strikePromise)}
26         , fGlyphs{glyphs} {
27     SkASSERT(fGlyphs.size() > 0);
28 }
29 
30 GlyphVector::Variant*
MakeGlyphs(SkSpan<SkPackedGlyphID> glyphs,sktext::gpu::SubRunAllocator * alloc)31 GlyphVector::MakeGlyphs(SkSpan<SkPackedGlyphID> glyphs, sktext::gpu::SubRunAllocator* alloc) {
32     Variant* variants = alloc->makePODArray<Variant>(glyphs.size());
33     for (auto [i, gv] : SkMakeEnumerate(glyphs)) {
34         variants[i] = gv;
35     }
36     return variants;
37 }
38 
Make(SkStrikePromise && promise,SkSpan<SkPackedGlyphID> glyphs,SubRunAllocator * alloc)39 GlyphVector GlyphVector::Make(
40         SkStrikePromise&& promise, SkSpan<SkPackedGlyphID> glyphs, SubRunAllocator* alloc) {
41     SkASSERT(glyphs.size() > 0);
42     Variant* variants = MakeGlyphs(glyphs, alloc);
43     return GlyphVector{std::move(promise), SkSpan(variants, glyphs.size())};
44 }
45 
MakeFromBuffer(SkReadBuffer & buffer,const SkStrikeClient * client,SubRunAllocator * alloc)46 std::optional<GlyphVector> GlyphVector::MakeFromBuffer(SkReadBuffer& buffer,
47                                                        const SkStrikeClient* client,
48                                                        SubRunAllocator* alloc) {
49     std::optional<SkStrikePromise> promise =
50             SkStrikePromise::MakeFromBuffer(buffer, client, SkStrikeCache::GlobalStrikeCache());
51     if (!buffer.validate(promise.has_value())) {
52         return std::nullopt;
53     }
54 
55     int32_t glyphCount = buffer.read32();
56     // Since the glyph count can never be zero. There was a buffer reading problem.
57     if (!buffer.validate(glyphCount > 0)) {
58         return std::nullopt;
59     }
60 
61     // Make sure we can multiply without overflow in the check below.
62     static constexpr int kMaxCount = (int)(INT_MAX / sizeof(uint32_t));
63     if (!buffer.validate(glyphCount <= kMaxCount)) {
64         return std::nullopt;
65     }
66 
67     // Check for enough bytes to populate the packedGlyphID array. If not enough something has
68     // gone wrong.
69     if (!buffer.validate(glyphCount * sizeof(uint32_t) <= buffer.available())) {
70         return std::nullopt;
71     }
72 
73     Variant* variants = alloc->makePODArray<Variant>(glyphCount);
74     for (int i = 0; i < glyphCount; i++) {
75         variants[i].packedGlyphID = SkPackedGlyphID(buffer.readUInt());
76     }
77     return GlyphVector{std::move(promise.value()), SkSpan(variants, glyphCount)};
78 }
79 
flatten(SkWriteBuffer & buffer) const80 void GlyphVector::flatten(SkWriteBuffer& buffer) const {
81     // There should never be a glyph vector with zero glyphs.
82     SkASSERT(fGlyphs.size() != 0);
83     fStrikePromise.flatten(buffer);
84 
85     // Write out the span of packedGlyphIDs.
86     buffer.write32(SkTo<int32_t>(fGlyphs.size()));
87     for (Variant variant : fGlyphs) {
88         buffer.writeUInt(variant.packedGlyphID.value());
89     }
90 }
91 
glyphs() const92 SkSpan<const Glyph*> GlyphVector::glyphs() const {
93     return SkSpan(reinterpret_cast<const Glyph**>(fGlyphs.data()), fGlyphs.size());
94 }
95 
96 // packedGlyphIDToGlyph must be run in single-threaded mode.
97 // If fSkStrike is not sk_sp<SkStrike> then the conversion to Glyph* has not happened.
packedGlyphIDToGlyph(StrikeCache * cache)98 void GlyphVector::packedGlyphIDToGlyph(StrikeCache* cache) {
99     if (fTextStrike == nullptr) {
100         SkStrike* strike = fStrikePromise.strike();
101         fTextStrike = cache->findOrCreateStrike(strike->strikeSpec());
102 
103         // Get all the atlas locations for each glyph.
104         for (Variant& variant : fGlyphs) {
105             variant.glyph = fTextStrike->getGlyph(variant.packedGlyphID);
106         }
107 
108         // This must be pinned for the Atlas filling to work.
109         strike->verifyPinnedStrike();
110 
111         // Drop the ref to the strike so that it can be purged if needed.
112         fStrikePromise.resetStrike();
113     }
114 }
115 }  // namespace sktext::gpu
116