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