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 #ifndef sktext_gpu_StrikeCache_DEFINED 9 #define sktext_gpu_StrikeCache_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "src/base/SkArenaAlloc.h" 13 #include "src/core/SkDescriptor.h" 14 #include "src/core/SkStrikeSpec.h" 15 #include "src/core/SkTHash.h" 16 17 #include <cstdint> 18 19 struct SkPackedGlyphID; 20 21 namespace sktext::gpu { 22 23 class Glyph; 24 25 // The TextStrike manages an SkArenaAlloc for Glyphs. The SkStrike is what actually creates 26 // the mask. The TextStrike may outlive the generating SkStrike. However, it retains a copy 27 // of it's SkDescriptor as a key to access (or regenerate) the SkStrike. TextStrikes are 28 // created by and owned by a StrikeCache. 29 class TextStrike : public SkNVRefCnt<TextStrike> { 30 public: 31 TextStrike(const SkStrikeSpec& strikeSpec); 32 33 Glyph* getGlyph(SkPackedGlyphID); strikeSpec()34 const SkStrikeSpec& strikeSpec() const { return fStrikeSpec; } 35 36 private: 37 // Key for retrieving the SkStrike for creating new atlas data. 38 const SkStrikeSpec fStrikeSpec; 39 40 struct HashTraits { 41 static const SkPackedGlyphID& GetKey(const Glyph* glyph); 42 static uint32_t Hash(SkPackedGlyphID key); 43 }; 44 // Map SkPackedGlyphID -> Glyph*. 45 skia_private::THashTable<Glyph*, SkPackedGlyphID, HashTraits> fCache; 46 47 // Store for the glyph information. 48 SkArenaAlloc fAlloc{512}; 49 50 friend class StrikeCache; 51 }; 52 53 // StrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be 54 // used to generate individual Glyph Masks. 55 class StrikeCache { 56 public: 57 ~StrikeCache(); 58 59 // The user of the cache may hold a long-lived ref to the returned strike. 60 sk_sp<TextStrike> findOrCreateStrike(const SkStrikeSpec& strikeSpec); 61 62 void freeAll(); 63 64 private: 65 sk_sp<TextStrike> generateStrike(const SkStrikeSpec& strikeSpec); 66 67 struct HashTraits { 68 static const SkDescriptor& GetKey(const sk_sp<TextStrike>& strike); 69 static uint32_t Hash(const SkDescriptor& strikeSpec); 70 }; 71 72 using StrikeHash = skia_private::THashTable<sk_sp<TextStrike>, const SkDescriptor&, HashTraits>; 73 74 StrikeHash fCache; 75 }; 76 77 } // namespace sktext::gpu 78 79 #endif // sktext_gpu_StrikeCache_DEFINED 80