blob: 89b054291d72ea4578ad768b1afbf583ff054c50 [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/**
26 * The textcache maps a hostfontscaler instance to a dictionary of
27 * glyphid->strike
28 */
29class GrTextStrike {
30public:
jvanverth733f5f52014-07-11 19:45:16 -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; }
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
jvanverthdd6d2272014-07-22 13:25:26 -070042 int countGlyphs() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000043
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000044 // remove any references to this plot
45 void removePlot(const GrPlot* plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000046
jvanverthdd6d2272014-07-22 13:25:26 -070047 static const GrFontDescKey& GetKey(const GrTextStrike& ts) {
48 return *(ts.fFontScalerKey);
49 }
50 static uint32_t Hash(const GrFontDescKey& key) {
51 return key.getHash();
52 }
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000055 // for easy removal from list
reed@google.comac10a2d2010-12-22 21:39:39 +000056 GrTextStrike* fPrev;
57 GrTextStrike* fNext;
58
59private:
jvanverthdd6d2272014-07-22 13:25:26 -070060 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
jvanverth733f5f52014-07-11 19:45:16 -070061 const GrFontDescKey* fFontScalerKey;
reed@google.comac10a2d2010-12-22 21:39:39 +000062 GrTAllocPool<GrGlyph> fPool;
63
64 GrFontCache* fFontCache;
robertphillips1d86ee82014-06-24 15:08:49 -070065 GrAtlas* fAtlas;
commit-bot@chromium.org95294412013-09-26 15:28:40 +000066 GrMaskFormat fMaskFormat;
jvanverth@google.comd830d132013-11-11 20:54:09 +000067 bool fUseDistanceField;
jvanverth@google.comd830d132013-11-11 20:54:09 +000068
robertphillips1d86ee82014-06-24 15:08:49 -070069 GrAtlas::ClientPlotUsage fPlotUsage;
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 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
reed@google.comac10a2d2010-12-22 21:39:39 +000082
83 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000084
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000085 // make an unused plot available
86 bool freeUnusedPlot(GrTextStrike* preserveStrike);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000087
reed@google.comac10a2d2010-12-22 21:39:39 +000088 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070089 int countStrikes() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000090 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
jvanverthdd6d2272014-07-22 13:25:26 -0700120 SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache;
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 // for LRU
122 GrTextStrike* fHead;
123 GrTextStrike* fTail;
124
125 GrGpu* fGpu;
robertphillips1d86ee82014-06-24 15:08:49 -0700126 GrAtlas* fAtlases[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
jvanverthdd6d2272014-07-22 13:25:26 -0700128 GrTextStrike* generateStrike(GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000130 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
133#endif