blob: 903cbfd434c724889021e99ad469a0c5059a344f [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 "GrGlyph.h"
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000018#include "GrDrawTarget.h"
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000019#include "GrAtlas.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021class GrFontCache;
22class GrGpu;
23class GrFontPurgeListener;
24
25/**
26 * The textcache maps a hostfontscaler instance to a dictionary of
27 * glyphid->strike
28 */
29class GrTextStrike {
30public:
robertphillips1d86ee82014-06-24 15:08:49 -070031 GrTextStrike(GrFontCache*, const GrKey* fontScalerKey, GrMaskFormat, GrAtlas*);
reed@google.comac10a2d2010-12-22 21:39:39 +000032 ~GrTextStrike();
33
34 const GrKey* getFontScalerKey() const { return fFontScalerKey; }
35 GrFontCache* getFontCache() const { return fFontCache; }
reed@google.com98539c62011-03-15 15:40:16 +000036 GrMaskFormat getMaskFormat() const { return fMaskFormat; }
reed@google.comac10a2d2010-12-22 21:39:39 +000037
38 inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000039 bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +000040
41 // testing
42 int countGlyphs() const { return fCache.getArray().count(); }
43 const GrGlyph* glyphAt(int index) const {
44 return fCache.getArray()[index];
45 }
reed@google.comac10a2d2010-12-22 21:39:39 +000046
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000047 // remove any references to this plot
48 void removePlot(const GrPlot* plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000051 // for easy removal from list
reed@google.comac10a2d2010-12-22 21:39:39 +000052 GrTextStrike* fPrev;
53 GrTextStrike* fNext;
54
55private:
56 class Key;
57 GrTHashTable<GrGlyph, Key, 7> fCache;
58 const GrKey* fFontScalerKey;
59 GrTAllocPool<GrGlyph> fPool;
60
61 GrFontCache* fFontCache;
robertphillips1d86ee82014-06-24 15:08:49 -070062 GrAtlas* fAtlas;
commit-bot@chromium.org95294412013-09-26 15:28:40 +000063 GrMaskFormat fMaskFormat;
jvanverth@google.comd830d132013-11-11 20:54:09 +000064 bool fUseDistanceField;
jvanverth@google.comd830d132013-11-11 20:54:09 +000065
robertphillips1d86ee82014-06-24 15:08:49 -070066 GrAtlas::ClientPlotUsage fPlotUsage;
reed@google.com98539c62011-03-15 15:40:16 +000067
reed@google.comac10a2d2010-12-22 21:39:39 +000068 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +000069
70 friend class GrFontCache;
71};
72
73class GrFontCache {
74public:
75 GrFontCache(GrGpu*);
76 ~GrFontCache();
77
jvanverth@google.comd830d132013-11-11 20:54:09 +000078 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
reed@google.comac10a2d2010-12-22 21:39:39 +000079
80 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000081
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000082 // make an unused plot available
83 bool freeUnusedPlot(GrTextStrike* preserveStrike);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000084
reed@google.comac10a2d2010-12-22 21:39:39 +000085 // testing
86 int countStrikes() const { return fCache.getArray().count(); }
87 const GrTextStrike* strikeAt(int index) const {
88 return fCache.getArray()[index];
89 }
90 GrTextStrike* getHeadStrike() const { return fHead; }
91
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000092 void updateTextures() {
93 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070094 if (fAtlases[i]) {
95 fAtlases[i]->uploadPlotsToTexture();
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000096 }
97 }
98 }
99
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000100#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 void validate() const;
102#else
103 void validate() const {}
104#endif
105
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000106 void dump() const;
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000107
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000108 enum AtlasType {
109 kA8_AtlasType, //!< 1-byte per pixel
110 k565_AtlasType, //!< 2-bytes per pixel
111 k8888_AtlasType, //!< 4-bytes per pixel
112
113 kLast_AtlasType = k8888_AtlasType
114 };
115 static const int kAtlasCount = kLast_AtlasType + 1;
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117private:
118 friend class GrFontPurgeListener;
119
120 class Key;
121 GrTHashTable<GrTextStrike, Key, 8> fCache;
122 // for LRU
123 GrTextStrike* fHead;
124 GrTextStrike* fTail;
125
126 GrGpu* fGpu;
robertphillips1d86ee82014-06-24 15:08:49 -0700127 GrAtlas* fAtlases[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 GrTextStrike* generateStrike(GrFontScaler*, const Key&);
130 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000131 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000132};
133
134#endif