• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2010 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 
11 #ifndef GrTextStrike_DEFINED
12 #define GrTextStrike_DEFINED
13 
14 #include "GrAllocPool.h"
15 #include "GrFontScaler.h"
16 #include "GrTHashTable.h"
17 #include "GrPoint.h"
18 #include "GrGlyph.h"
19 #include "GrDrawTarget.h"
20 #include "GrAtlas.h"
21 
22 class GrFontCache;
23 class GrGpu;
24 class GrFontPurgeListener;
25 
26 /**
27  *  The textcache maps a hostfontscaler instance to a dictionary of
28  *  glyphid->strike
29  */
30 class GrTextStrike {
31 public:
32     GrTextStrike(GrFontCache*, const GrKey* fontScalerKey, GrMaskFormat, GrAtlasMgr*);
33     ~GrTextStrike();
34 
getFontScalerKey()35     const GrKey* getFontScalerKey() const { return fFontScalerKey; }
getFontCache()36     GrFontCache* getFontCache() const { return fFontCache; }
getMaskFormat()37     GrMaskFormat getMaskFormat() const { return fMaskFormat; }
38 
39     inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
40     bool getGlyphAtlas(GrGlyph*, GrFontScaler*);
41 
42     // testing
countGlyphs()43     int countGlyphs() const { return fCache.getArray().count(); }
glyphAt(int index)44     const GrGlyph* glyphAt(int index) const {
45         return fCache.getArray()[index];
46     }
47 
48     // returns true if a plot was removed
49     bool removeUnusedPlots();
50 
51 public:
52     // for LRU
53     GrTextStrike*   fPrev;
54     GrTextStrike*   fNext;
55 
56 private:
57     class Key;
58     GrTHashTable<GrGlyph, Key, 7> fCache;
59     const GrKey* fFontScalerKey;
60     GrTAllocPool<GrGlyph> fPool;
61 
62     GrFontCache*    fFontCache;
63     GrAtlasMgr*     fAtlasMgr;
64     GrMaskFormat    fMaskFormat;
65 #if SK_DISTANCEFIELD_FONTS
66     bool            fUseDistanceField;
67 #endif
68 
69     GrAtlas         fAtlas;
70 
71     GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
72 
73     friend class GrFontCache;
74 };
75 
76 class GrFontCache {
77 public:
78     GrFontCache(GrGpu*);
79     ~GrFontCache();
80 
81 #if SK_DISTANCEFIELD_FONTS
82     inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
83 #else
84     inline GrTextStrike* getStrike(GrFontScaler*);
85 #endif
86 
87     void freeAll();
88 
89     void purgeExceptFor(GrTextStrike*);
90 
91     // remove an unused plot and its strike (if necessary)
92     void freePlotExceptFor(GrTextStrike*);
93 
94     // testing
countStrikes()95     int countStrikes() const { return fCache.getArray().count(); }
strikeAt(int index)96     const GrTextStrike* strikeAt(int index) const {
97         return fCache.getArray()[index];
98     }
getHeadStrike()99     GrTextStrike* getHeadStrike() const { return fHead; }
100 
101 #ifdef SK_DEBUG
102     void validate() const;
103 #else
validate()104     void validate() const {}
105 #endif
106 
107 #ifdef SK_DEVELOPER
108     void dump() const;
109 #endif
110 
111     enum AtlasType {
112         kA8_AtlasType,   //!< 1-byte per pixel
113         k565_AtlasType,  //!< 2-bytes per pixel
114         k8888_AtlasType, //!< 4-bytes per pixel
115 
116         kLast_AtlasType = k8888_AtlasType
117     };
118     static const int kAtlasCount = kLast_AtlasType + 1;
119 
120 private:
121     friend class GrFontPurgeListener;
122 
123     class Key;
124     GrTHashTable<GrTextStrike, Key, 8> fCache;
125     // for LRU
126     GrTextStrike* fHead;
127     GrTextStrike* fTail;
128 
129     GrGpu*      fGpu;
130     GrAtlasMgr* fAtlasMgr[kAtlasCount];
131 
132     GrTextStrike* generateStrike(GrFontScaler*, const Key&);
133     inline void detachStrikeFromList(GrTextStrike*);
134     void purgeStrike(GrTextStrike* strike);
135 };
136 
137 #endif
138