blob: 17dcec618c500964abbdaed0a62e09bee7f85ee4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTextStrike_DEFINED
12#define GrTextStrike_DEFINED
13
14#include "GrAllocPool.h"
15#include "GrFontScaler.h"
mtklein@google.com4c2af742013-10-21 21:04:06 +000016#include "GrTHashTable.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrPoint.h"
18#include "GrGlyph.h"
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000019#include "GrDrawTarget.h"
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000020#include "GrAtlas.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022class GrFontCache;
23class GrGpu;
24class GrFontPurgeListener;
25
26/**
27 * The textcache maps a hostfontscaler instance to a dictionary of
28 * glyphid->strike
29 */
30class GrTextStrike {
31public:
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000032 GrTextStrike(GrFontCache*, const GrKey* fontScalerKey, GrMaskFormat, GrAtlasMgr*);
reed@google.comac10a2d2010-12-22 21:39:39 +000033 ~GrTextStrike();
34
35 const GrKey* getFontScalerKey() const { return fFontScalerKey; }
36 GrFontCache* getFontCache() const { return fFontCache; }
reed@google.com98539c62011-03-15 15:40:16 +000037 GrMaskFormat getMaskFormat() const { return fMaskFormat; }
reed@google.comac10a2d2010-12-22 21:39:39 +000038
39 inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
commit-bot@chromium.org49e80832013-10-07 18:20:27 +000040 bool getGlyphAtlas(GrGlyph*, GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +000041
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000042 SkISize getAtlasSize() const { return fAtlas.getSize(); }
43
reed@google.comac10a2d2010-12-22 21:39:39 +000044 // testing
45 int countGlyphs() const { return fCache.getArray().count(); }
46 const GrGlyph* glyphAt(int index) const {
47 return fCache.getArray()[index];
48 }
reed@google.comac10a2d2010-12-22 21:39:39 +000049
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000050 // returns true if a plot was removed
51 bool removeUnusedPlots();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000052
reed@google.comac10a2d2010-12-22 21:39:39 +000053public:
54 // for LRU
55 GrTextStrike* fPrev;
56 GrTextStrike* fNext;
57
58private:
59 class Key;
60 GrTHashTable<GrGlyph, Key, 7> fCache;
61 const GrKey* fFontScalerKey;
62 GrTAllocPool<GrGlyph> fPool;
63
64 GrFontCache* fFontCache;
65 GrAtlasMgr* fAtlasMgr;
commit-bot@chromium.org95294412013-09-26 15:28:40 +000066 GrMaskFormat fMaskFormat;
jvanverth@google.comd830d132013-11-11 20:54:09 +000067#if SK_DISTANCEFIELD_FONTS
68 bool fUseDistanceField;
69#endif
70
71 GrAtlas fAtlas;
reed@google.com98539c62011-03-15 15:40:16 +000072
reed@google.comac10a2d2010-12-22 21:39:39 +000073 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +000074
75 friend class GrFontCache;
76};
77
78class GrFontCache {
79public:
80 GrFontCache(GrGpu*);
81 ~GrFontCache();
82
jvanverth@google.comd830d132013-11-11 20:54:09 +000083#if SK_DISTANCEFIELD_FONTS
84 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
85#else
reed@google.comac10a2d2010-12-22 21:39:39 +000086 inline GrTextStrike* getStrike(GrFontScaler*);
jvanverth@google.comd830d132013-11-11 20:54:09 +000087#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000088
89 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000090
91 void purgeExceptFor(GrTextStrike*);
92
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000093 // remove an unused plot and its strike (if necessary)
94 void freePlotExceptFor(GrTextStrike*);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000095
reed@google.comac10a2d2010-12-22 21:39:39 +000096 // testing
97 int countStrikes() const { return fCache.getArray().count(); }
98 const GrTextStrike* strikeAt(int index) const {
99 return fCache.getArray()[index];
100 }
101 GrTextStrike* getHeadStrike() const { return fHead; }
102
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000103#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 void validate() const;
105#else
106 void validate() const {}
107#endif
108
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000109#ifdef SK_DEVELOPER
110 void dump() const;
111#endif
112
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000113 enum AtlasType {
114 kA8_AtlasType, //!< 1-byte per pixel
115 k565_AtlasType, //!< 2-bytes per pixel
116 k8888_AtlasType, //!< 4-bytes per pixel
117
118 kLast_AtlasType = k8888_AtlasType
119 };
120 static const int kAtlasCount = kLast_AtlasType + 1;
121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122private:
123 friend class GrFontPurgeListener;
124
125 class Key;
126 GrTHashTable<GrTextStrike, Key, 8> fCache;
127 // for LRU
128 GrTextStrike* fHead;
129 GrTextStrike* fTail;
130
131 GrGpu* fGpu;
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000132 GrAtlasMgr* fAtlasMgr[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 GrTextStrike* generateStrike(GrFontScaler*, const Key&);
135 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000136 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000137};
138
139#endif