/src/skia/src/text/gpu/StrikeCache.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | */ |
7 | | #include "src/text/gpu/StrikeCache.h" |
8 | | |
9 | | #include "include/private/base/SkAssert.h" |
10 | | #include "include/private/chromium/SkChromeRemoteGlyphCache.h" |
11 | | #include "src/base/SkArenaAlloc.h" |
12 | | #include "src/core/SkGlyph.h" |
13 | | #include "src/core/SkReadBuffer.h" |
14 | | #include "src/core/SkStrikeCache.h" |
15 | | #include "src/core/SkStrikeSpec.h" |
16 | | #include "src/text/StrikeForGPU.h" |
17 | | #include "src/text/gpu/Glyph.h" |
18 | | |
19 | | #include <optional> |
20 | | #include <utility> |
21 | | |
22 | | class SkStrike; |
23 | | |
24 | | namespace sktext::gpu { |
25 | | |
26 | 2.58k | StrikeCache::~StrikeCache() { |
27 | 2.58k | this->freeAll(); |
28 | 2.58k | } |
29 | | |
30 | 2.58k | void StrikeCache::freeAll() { |
31 | 2.58k | fCache.reset(); |
32 | 2.58k | } |
33 | | |
34 | 557 | sk_sp<TextStrike> StrikeCache::findOrCreateStrike(const SkStrikeSpec& strikeSpec) { |
35 | 557 | if (sk_sp<TextStrike>* cached = fCache.find(strikeSpec.descriptor())) { |
36 | 324 | return *cached; |
37 | 324 | } |
38 | 233 | return this->generateStrike(strikeSpec); |
39 | 557 | } |
40 | | |
41 | 233 | sk_sp<TextStrike> StrikeCache::generateStrike(const SkStrikeSpec& strikeSpec) { |
42 | 233 | sk_sp<TextStrike> strike = sk_make_sp<TextStrike>(strikeSpec); |
43 | 233 | fCache.set(strike); |
44 | 233 | return strike; |
45 | 233 | } |
46 | | |
47 | 569 | const SkDescriptor& StrikeCache::HashTraits::GetKey(const sk_sp<TextStrike>& strike) { |
48 | 569 | return strike->fStrikeSpec.descriptor(); |
49 | 569 | } |
50 | | |
51 | 802 | uint32_t StrikeCache::HashTraits::Hash(const SkDescriptor& descriptor) { |
52 | 802 | return descriptor.getChecksum(); |
53 | 802 | } |
54 | | |
55 | 233 | TextStrike::TextStrike(const SkStrikeSpec& strikeSpec) : fStrikeSpec{strikeSpec} {} |
56 | | |
57 | 9.51k | Glyph* TextStrike::getGlyph(SkPackedGlyphID packedGlyphID) { |
58 | 9.51k | Glyph* glyph = fCache.findOrNull(packedGlyphID); |
59 | 9.51k | if (glyph == nullptr) { |
60 | 644 | glyph = fAlloc.make<Glyph>(packedGlyphID); |
61 | 644 | fCache.set(glyph); |
62 | 644 | } |
63 | 9.51k | return glyph; |
64 | 9.51k | } |
65 | | |
66 | 9.78k | const SkPackedGlyphID& TextStrike::HashTraits::GetKey(const Glyph* glyph) { |
67 | 9.78k | return glyph->fPackedID; |
68 | 9.78k | } |
69 | | |
70 | 10.4k | uint32_t TextStrike::HashTraits::Hash(SkPackedGlyphID key) { |
71 | 10.4k | return key.hash(); |
72 | 10.4k | } |
73 | | |
74 | | } // namespace sktext::gpu |
75 | | |
76 | | namespace sktext { |
77 | | std::optional<SkStrikePromise> SkStrikePromise::MakeFromBuffer( |
78 | 0 | SkReadBuffer& buffer, const SkStrikeClient* client, SkStrikeCache* strikeCache) { |
79 | 0 | std::optional<SkAutoDescriptor> descriptor = SkAutoDescriptor::MakeFromBuffer(buffer); |
80 | 0 | if (!buffer.validate(descriptor.has_value())) { |
81 | 0 | return std::nullopt; |
82 | 0 | } |
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 | 0 | if (client != nullptr) { |
87 | 0 | if (!client->translateTypefaceID(&descriptor.value())) { |
88 | 0 | return std::nullopt; |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | 0 | sk_sp<SkStrike> strike = strikeCache->findStrike(*descriptor->getDesc()); |
93 | 0 | SkASSERT(strike != nullptr); |
94 | 0 | if (!buffer.validate(strike != nullptr)) { |
95 | 0 | return std::nullopt; |
96 | 0 | } |
97 | | |
98 | 0 | return SkStrikePromise{std::move(strike)}; |
99 | 0 | } Unexecuted instantiation: sktext::SkStrikePromise::MakeFromBuffer(SkReadBuffer&, SkStrikeClient const*, SkStrikeCache*) Unexecuted instantiation: sktext::SkStrikePromise::MakeFromBuffer(SkReadBuffer&, SkStrikeClient const*, SkStrikeCache*) |
100 | | } // namespace sktext |