blob: 401bd7325909aa39be8a2e0ee655e1a08cf3f3e9 [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"
jvanverthdd6d2272014-07-22 13:25:26 -070016#include "SkTDynamicHash.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/**
jvanverthe817dbb2014-10-10 08:52:03 -070026 * The textcache maps a hostfontscaler instance to a dictionary of
reed@google.comac10a2d2010-12-22 21:39:39 +000027 * glyphid->strike
28 */
29class GrTextStrike {
30public:
jvanverthe817dbb2014-10-10 08:52:03 -070031 GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey, GrMaskFormat, GrAtlas*);
reed@google.comac10a2d2010-12-22 21:39:39 +000032 ~GrTextStrike();
33
jvanverth733f5f52014-07-11 19:45:16 -070034 const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; }
reed@google.comac10a2d2010-12-22 21:39:39 +000035 GrFontCache* getFontCache() const { return fFontCache; }
jvanverthe817dbb2014-10-10 08:52:03 -070036 GrMaskFormat getMaskFormat() const { return fMaskFormat; }
37 GrTexture* getTexture() const { return fAtlas->getTexture(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000038
39 inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
jvanverth681e65b2014-09-19 13:07:38 -070040 // returns true if glyph (or glyph+padding for distance field)
41 // is too large to ever fit in texture atlas subregions (GrPlots)
42 bool glyphTooLargeForAtlas(GrGlyph*);
43 // returns true if glyph successfully added to texture atlas, false otherwise
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000044 bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +000045
46 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070047 int countGlyphs() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000048
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000049 // remove any references to this plot
50 void removePlot(const GrPlot* plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000051
jvanverthdd6d2272014-07-22 13:25:26 -070052 static const GrFontDescKey& GetKey(const GrTextStrike& ts) {
53 return *(ts.fFontScalerKey);
54 }
55 static uint32_t Hash(const GrFontDescKey& key) {
56 return key.getHash();
57 }
58
reed@google.comac10a2d2010-12-22 21:39:39 +000059public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000060 // for easy removal from list
reed@google.comac10a2d2010-12-22 21:39:39 +000061 GrTextStrike* fPrev;
62 GrTextStrike* fNext;
63
64private:
jvanverthdd6d2272014-07-22 13:25:26 -070065 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
jvanverth733f5f52014-07-11 19:45:16 -070066 const GrFontDescKey* fFontScalerKey;
reed@google.comac10a2d2010-12-22 21:39:39 +000067 GrTAllocPool<GrGlyph> fPool;
68
69 GrFontCache* fFontCache;
jvanverthe817dbb2014-10-10 08:52:03 -070070 GrAtlas* fAtlas;
71 GrMaskFormat fMaskFormat;
jvanverth@google.comd830d132013-11-11 20:54:09 +000072 bool fUseDistanceField;
jvanverth@google.comd830d132013-11-11 20:54:09 +000073
robertphillips1d86ee82014-06-24 15:08:49 -070074 GrAtlas::ClientPlotUsage fPlotUsage;
reed@google.com98539c62011-03-15 15:40:16 +000075
reed@google.comac10a2d2010-12-22 21:39:39 +000076 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +000077
78 friend class GrFontCache;
79};
80
81class GrFontCache {
82public:
83 GrFontCache(GrGpu*);
84 ~GrFontCache();
85
jvanverth@google.comd830d132013-11-11 20:54:09 +000086 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
reed@google.comac10a2d2010-12-22 21:39:39 +000087
88 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000089
jvanverthe817dbb2014-10-10 08:52:03 -070090 // make an unused plot available
91 bool freeUnusedPlot(GrTextStrike* preserveStrike);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000092
reed@google.comac10a2d2010-12-22 21:39:39 +000093 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070094 int countStrikes() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000095 GrTextStrike* getHeadStrike() const { return fHead; }
96
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000097 void updateTextures() {
98 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070099 if (fAtlases[i]) {
100 fAtlases[i]->uploadPlotsToTexture();
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000101 }
102 }
103 }
104
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000105#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 void validate() const;
107#else
108 void validate() const {}
109#endif
110
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000111 void dump() const;
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000112
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
jvanverthdd6d2272014-07-22 13:25:26 -0700125 SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache;
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 // for LRU
127 GrTextStrike* fHead;
128 GrTextStrike* fTail;
129
130 GrGpu* fGpu;
robertphillips1d86ee82014-06-24 15:08:49 -0700131 GrAtlas* fAtlases[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
jvanverthdd6d2272014-07-22 13:25:26 -0700133 GrTextStrike* generateStrike(GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000135 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136};
137
138#endif