• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/base/SkArenaAlloc.h"
9 #include "src/core/SkStrikeSpec.h"
10 #include "src/text/gpu/Glyph.h"
11 #include "src/text/gpu/StrikeCache.h"
12 
13 namespace sktext::gpu {
14 
~StrikeCache()15 StrikeCache::~StrikeCache() {
16     this->freeAll();
17 }
18 
freeAll()19 void StrikeCache::freeAll() {
20     fCache.reset();
21 }
22 
findOrCreateStrike(const SkStrikeSpec & strikeSpec)23 sk_sp<TextStrike> StrikeCache::findOrCreateStrike(const SkStrikeSpec& strikeSpec) {
24     if (sk_sp<TextStrike>* cached = fCache.find(strikeSpec.descriptor())) {
25         return *cached;
26     }
27     return this->generateStrike(strikeSpec);
28 }
29 
generateStrike(const SkStrikeSpec & strikeSpec)30 sk_sp<TextStrike> StrikeCache::generateStrike(const SkStrikeSpec& strikeSpec) {
31     sk_sp<TextStrike> strike = sk_make_sp<TextStrike>(strikeSpec);
32     fCache.set(strike);
33     return strike;
34 }
35 
GetKey(const sk_sp<TextStrike> & strike)36 const SkDescriptor& StrikeCache::HashTraits::GetKey(const sk_sp<TextStrike>& strike) {
37     return strike->fStrikeSpec.descriptor();
38 }
39 
Hash(const SkDescriptor & descriptor)40 uint32_t StrikeCache::HashTraits::Hash(const SkDescriptor& descriptor) {
41     return descriptor.getChecksum();
42 }
43 
TextStrike(const SkStrikeSpec & strikeSpec)44 TextStrike::TextStrike(const SkStrikeSpec& strikeSpec) : fStrikeSpec{strikeSpec} {}
45 
getGlyph(SkPackedGlyphID packedGlyphID)46 Glyph* TextStrike::getGlyph(SkPackedGlyphID packedGlyphID) {
47     Glyph* glyph = fCache.findOrNull(packedGlyphID);
48     if (glyph == nullptr) {
49         glyph = fAlloc.make<Glyph>(packedGlyphID);
50         fCache.set(glyph);
51     }
52     return glyph;
53 }
54 
GetKey(const Glyph * glyph)55 const SkPackedGlyphID& TextStrike::HashTraits::GetKey(const Glyph* glyph) {
56     return glyph->fPackedID;
57 }
58 
Hash(SkPackedGlyphID key)59 uint32_t TextStrike::HashTraits::Hash(SkPackedGlyphID key) {
60     return key.hash();
61 }
62 
63 }  // namespace sktext::gpu
64