blob: c5a3f656251b7d167f12e7a1922f33d4caae26eb [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
42 // testing
43 int countGlyphs() const { return fCache.getArray().count(); }
44 const GrGlyph* glyphAt(int index) const {
45 return fCache.getArray()[index];
46 }
reed@google.comac10a2d2010-12-22 21:39:39 +000047
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000048 // returns true if a plot was removed
49 bool removeUnusedPlots();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000050
reed@google.comac10a2d2010-12-22 21:39:39 +000051public:
52 // for LRU
53 GrTextStrike* fPrev;
54 GrTextStrike* fNext;
55
56private:
57 class Key;
58 GrTHashTable<GrGlyph, Key, 7> fCache;
59 const GrKey* fFontScalerKey;
60 GrTAllocPool<GrGlyph> fPool;
61
62 GrFontCache* fFontCache;
63 GrAtlasMgr* fAtlasMgr;
commit-bot@chromium.org95294412013-09-26 15:28:40 +000064 GrMaskFormat fMaskFormat;
jvanverth@google.comd830d132013-11-11 20:54:09 +000065#if SK_DISTANCEFIELD_FONTS
66 bool fUseDistanceField;
67#endif
68
69 GrAtlas fAtlas;
reed@google.com98539c62011-03-15 15:40:16 +000070
reed@google.comac10a2d2010-12-22 21:39:39 +000071 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +000072
73 friend class GrFontCache;
74};
75
76class GrFontCache {
77public:
78 GrFontCache(GrGpu*);
79 ~GrFontCache();
80
jvanverth@google.comd830d132013-11-11 20:54:09 +000081#if SK_DISTANCEFIELD_FONTS
82 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
83#else
reed@google.comac10a2d2010-12-22 21:39:39 +000084 inline GrTextStrike* getStrike(GrFontScaler*);
jvanverth@google.comd830d132013-11-11 20:54:09 +000085#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000086
87 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000088
89 void purgeExceptFor(GrTextStrike*);
90
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000091 // remove an unused plot and its strike (if necessary)
92 void freePlotExceptFor(GrTextStrike*);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000093
reed@google.comac10a2d2010-12-22 21:39:39 +000094 // testing
95 int countStrikes() const { return fCache.getArray().count(); }
96 const GrTextStrike* strikeAt(int index) const {
97 return fCache.getArray()[index];
98 }
99 GrTextStrike* getHeadStrike() const { return fHead; }
100
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000101#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 void validate() const;
103#else
104 void validate() const {}
105#endif
106
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000107#ifdef SK_DEVELOPER
108 void dump() const;
109#endif
110
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000111 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
reed@google.comac10a2d2010-12-22 21:39:39 +0000120private:
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;
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000130 GrAtlasMgr* fAtlasMgr[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 GrTextStrike* generateStrike(GrFontScaler*, const Key&);
133 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000134 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000135};
136
137#endif