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()15StrikeCache::~StrikeCache() { 16 this->freeAll(); 17 } 18 freeAll()19void StrikeCache::freeAll() { 20 fCache.reset(); 21 } 22 findOrCreateStrike(const SkStrikeSpec & strikeSpec)23sk_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)30sk_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)36const SkDescriptor& StrikeCache::HashTraits::GetKey(const sk_sp<TextStrike>& strike) { 37 return strike->fStrikeSpec.descriptor(); 38 } 39 Hash(const SkDescriptor & descriptor)40uint32_t StrikeCache::HashTraits::Hash(const SkDescriptor& descriptor) { 41 return descriptor.getChecksum(); 42 } 43 TextStrike(const SkStrikeSpec & strikeSpec)44TextStrike::TextStrike(const SkStrikeSpec& strikeSpec) : fStrikeSpec{strikeSpec} {} 45 getGlyph(SkPackedGlyphID packedGlyphID)46Glyph* 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)55const SkPackedGlyphID& TextStrike::HashTraits::GetKey(const Glyph* glyph) { 56 return glyph->fPackedID; 57 } 58 Hash(SkPackedGlyphID key)59uint32_t TextStrike::HashTraits::Hash(SkPackedGlyphID key) { 60 return key.hash(); 61 } 62 63 } // namespace sktext::gpu 64