Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrTextStrike.h
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 "SkTDynamicHash.h"
17 #include "GrGlyph.h"
18 #include "GrDrawTarget.h"
19 #include "GrAtlas.h"
20
21 class GrFontCache;
22 class GrGpu;
23 class GrFontPurgeListener;
24
25 /**
26  *  The textcache maps a hostfontscaler instance to a dictionary of
27  *  glyphid->strike
28  */
29 class GrTextStrike {
30 public:
31     GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey, GrMaskFormat, GrAtlas*);
32     ~GrTextStrike();
33
34     const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; }
35     GrFontCache* getFontCache() const { return fFontCache; }
36     GrMaskFormat getMaskFormat() const { return fMaskFormat; }
37     GrTexture*   getTexture() const { return fAtlas->getTexture(); }
38
39     inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
40     bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);
41
42     // testing
43     int countGlyphs() const { return fCache.count(); }
44
45     // remove any references to this plot
46     void removePlot(const GrPlot* plot);
47
48     static const GrFontDescKey& GetKey(const GrTextStrike& ts) {
49         return *(ts.fFontScalerKey);
50     }
51     static uint32_t Hash(const GrFontDescKey& key) {
52         return key.getHash();
53     }
54
55 public:
56     // for easy removal from list
57     GrTextStrike*   fPrev;
58     GrTextStrike*   fNext;
59
60 private:
61     SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
62     const GrFontDescKey* fFontScalerKey;
63     GrTAllocPool<GrGlyph> fPool;
64
65     GrFontCache*    fFontCache;
66     GrAtlas*        fAtlas;
67     GrMaskFormat    fMaskFormat;
68     bool            fUseDistanceField;
69
70     GrAtlas::ClientPlotUsage fPlotUsage;
71
72     GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
73
74     friend class GrFontCache;
75 };
76
77 class GrFontCache {
78 public:
79     GrFontCache(GrGpu*);
80     ~GrFontCache();
81
82     inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
83
84     void freeAll();
85
86     // make an unused plot available
87     bool freeUnusedPlot(GrTextStrike* preserveStrike);
88
89     // testing
90     int countStrikes() const { return fCache.count(); }
91     GrTextStrike* getHeadStrike() const { return fHead; }
92
93     void updateTextures() {
94         for (int i = 0; i < kAtlasCount; ++i) {
95             if (fAtlases[i]) {
96                 fAtlases[i]->uploadPlotsToTexture();
97             }
98         }
99     }
100
101 #ifdef SK_DEBUG
102     void validate() const;
103 #else
104     void validate() const {}
105 #endif
106
107     void dump() const;
108
109     enum AtlasType {
110         kA8_AtlasType,   //!< 1-byte per pixel
111         k565_AtlasType,  //!< 2-bytes per pixel
112         k8888_AtlasType, //!< 4-bytes per pixel
113
114         kLast_AtlasType = k8888_AtlasType
115     };
116     static const int kAtlasCount = kLast_AtlasType + 1;
117
118 private:
119     friend class GrFontPurgeListener;
120
121     SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache;
122     // for LRU
123     GrTextStrike* fHead;
124     GrTextStrike* fTail;
125
126     GrGpu*      fGpu;
127     GrAtlas*    fAtlases[kAtlasCount];
128
129     GrTextStrike* generateStrike(GrFontScaler*);
130     inline void detachStrikeFromList(GrTextStrike*);
131     void purgeStrike(GrTextStrike* strike);
132 };
133
134 #endif