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