Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
StrikeCache.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
8
12#include "src/core/SkGlyph.h"
17#include "src/text/gpu/Glyph.h"
18
19#include <optional>
20#include <utility>
21
22class SkStrike;
23
24namespace sktext::gpu {
25
29
31 fCache.reset();
32}
33
35 if (sk_sp<TextStrike>* cached = fCache.find(strikeSpec.descriptor())) {
36 return *cached;
37 }
38 return this->generateStrike(strikeSpec);
39}
40
41sk_sp<TextStrike> StrikeCache::generateStrike(const SkStrikeSpec& strikeSpec) {
42 sk_sp<TextStrike> strike = sk_make_sp<TextStrike>(strikeSpec);
43 fCache.set(strike);
44 return strike;
45}
46
47const SkDescriptor& StrikeCache::HashTraits::GetKey(const sk_sp<TextStrike>& strike) {
48 return strike->fStrikeSpec.descriptor();
49}
50
51uint32_t StrikeCache::HashTraits::Hash(const SkDescriptor& descriptor) {
52 return descriptor.getChecksum();
53}
54
55TextStrike::TextStrike(const SkStrikeSpec& strikeSpec) : fStrikeSpec{strikeSpec} {}
56
58 Glyph* glyph = fCache.findOrNull(packedGlyphID);
59 if (glyph == nullptr) {
60 glyph = fAlloc.make<Glyph>(packedGlyphID);
61 fCache.set(glyph);
62 }
63 return glyph;
64}
65
66const SkPackedGlyphID& TextStrike::HashTraits::GetKey(const Glyph* glyph) {
67 return glyph->fPackedID;
68}
69
70uint32_t TextStrike::HashTraits::Hash(SkPackedGlyphID key) {
71 return key.hash();
72}
73
74} // namespace sktext::gpu
75
76namespace sktext {
77std::optional<SkStrikePromise> SkStrikePromise::MakeFromBuffer(
78 SkReadBuffer& buffer, const SkStrikeClient* client, SkStrikeCache* strikeCache) {
79 std::optional<SkAutoDescriptor> descriptor = SkAutoDescriptor::MakeFromBuffer(buffer);
80 if (!buffer.validate(descriptor.has_value())) {
81 return std::nullopt;
82 }
83
84 // If there is a client, then this from a different process. Translate the SkTypefaceID from
85 // the strike server (Renderer) process to strike client (GPU) process.
86 if (client != nullptr) {
87 if (!client->translateTypefaceID(&descriptor.value())) {
88 return std::nullopt;
89 }
90 }
91
92 sk_sp<SkStrike> strike = strikeCache->findStrike(*descriptor->getDesc());
93 SkASSERT(strike != nullptr);
94 if (!buffer.validate(strike != nullptr)) {
95 return std::nullopt;
96 }
97
98 return SkStrikePromise{std::move(strike)};
99}
100} // namespace sktext
#define SkASSERT(cond)
Definition SkAssert.h:116
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
static std::optional< SkAutoDescriptor > MakeFromBuffer(SkReadBuffer &buffer)
uint32_t getChecksum() const
sk_sp< SkStrike > findStrike(const SkDescriptor &desc) SK_EXCLUDES(fLock)
SK_SPI bool translateTypefaceID(SkAutoDescriptor *descriptor) const
const SkDescriptor & descriptor() const
T * find(const K &key) const
Definition SkTHash.h:96
static std::optional< SkStrikePromise > MakeFromBuffer(SkReadBuffer &buffer, const SkStrikeClient *client, SkStrikeCache *strikeCache)
const SkDescriptor & descriptor() const
const SkPackedGlyphID fPackedID
Definition Glyph.h:39
sk_sp< TextStrike > findOrCreateStrike(const SkStrikeSpec &strikeSpec)
TextStrike(const SkStrikeSpec &strikeSpec)
Glyph * getGlyph(SkPackedGlyphID)
static const uint8_t buffer[]
uint32_t hash() const
Definition SkGlyph.h:122