blob: 46ab1c82741a2820f00fdc36358f2e8b17c549c7 [file] [log] [blame]
joshualitt7c3a2f82015-03-31 13:32:05 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrBatchFontCache_DEFINED
9#define GrBatchFontCache_DEFINED
10
11#include "GrBatchAtlas.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070012#include "GrFontScaler.h"
13#include "GrGlyph.h"
joshualitt6c2c2b02015-07-24 10:37:00 -070014#include "SkGlyph.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070015#include "SkTDynamicHash.h"
16#include "SkVarAlloc.h"
17
18class GrBatchFontCache;
joshualitt7c3a2f82015-03-31 13:32:05 -070019class GrGpu;
20
21/**
joshualitt4765bdc2015-07-23 10:58:48 -070022 * The GrBatchTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
23 * is indexed by a PackedID and GrFontScaler. The GrFontScaler is what actually creates the mask.
joshualitt7c3a2f82015-03-31 13:32:05 -070024 */
joshualittae32c102015-04-21 09:37:57 -070025class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> {
joshualitt7c3a2f82015-03-31 13:32:05 -070026public:
27 GrBatchTextStrike(GrBatchFontCache*, const GrFontDescKey* fontScalerKey);
28 ~GrBatchTextStrike();
29
30 const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; }
31 GrBatchFontCache* getBatchFontCache() const { return fBatchFontCache; }
32
joshualitt6c2c2b02015-07-24 10:37:00 -070033 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
34 GrFontScaler* scaler) {
joshualitt7c3a2f82015-03-31 13:32:05 -070035 GrGlyph* glyph = fCache.find(packed);
halcanary96fcdcc2015-08-27 07:41:13 -070036 if (nullptr == glyph) {
joshualitt6c2c2b02015-07-24 10:37:00 -070037 glyph = this->generateGlyph(skGlyph, packed, scaler);
joshualitt7c3a2f82015-03-31 13:32:05 -070038 }
39 return glyph;
40 }
41
joshualitt76cc6572015-07-31 05:51:45 -070042 // This variant of the above function is called by TextBatch. At this point, it is possible
43 // that the maskformat of the glyph differs from what we expect. In these cases we will just
44 // draw a clear square.
45 // skbug:4143 crbug:510931
joshualitt50aa15b2015-11-23 14:09:55 -080046 inline GrGlyph* getGlyph(GrGlyph::PackedID packed,
47 GrMaskFormat expectedMaskFormat,
48 GrFontScaler* scaler) {
joshualitt76cc6572015-07-31 05:51:45 -070049 GrGlyph* glyph = fCache.find(packed);
halcanary96fcdcc2015-08-27 07:41:13 -070050 if (nullptr == glyph) {
joshualitt50aa15b2015-11-23 14:09:55 -080051 // We could return this to the caller, but in practice it adds code complexity for
52 // potentially little benefit(ie, if the glyph is not in our font cache, then its not
53 // in the atlas and we're going to be doing a texture upload anyways).
54 const SkGlyph& skGlyph = scaler->grToSkGlyph(packed);
joshualitt76cc6572015-07-31 05:51:45 -070055 glyph = this->generateGlyph(skGlyph, packed, scaler);
56 glyph->fMaskFormat = expectedMaskFormat;
57 }
58 return glyph;
59 }
60
joshualitt4f19ca32015-07-30 07:59:20 -070061 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
62 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
63 // happen.
64 // TODO we can handle some of these cases if we really want to, but the long term solution is to
65 // get the actual glyph image itself when we get the glyph metrics.
joshualitt50aa15b2015-11-23 14:09:55 -080066 bool addGlyphToAtlas(GrDrawBatch::Target*, GrGlyph*, GrFontScaler*,
joshualitt4f19ca32015-07-30 07:59:20 -070067 GrMaskFormat expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -070068
69 // testing
70 int countGlyphs() const { return fCache.count(); }
71
72 // remove any references to this plot
73 void removeID(GrBatchAtlas::AtlasID);
74
joshualittae32c102015-04-21 09:37:57 -070075 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
76 bool isAbandoned() const { return fIsAbandoned; }
77
joshualitt7c3a2f82015-03-31 13:32:05 -070078 static const GrFontDescKey& GetKey(const GrBatchTextStrike& ts) {
79 return *(ts.fFontScalerKey);
80 }
81 static uint32_t Hash(const GrFontDescKey& key) {
82 return key.getHash();
83 }
84
85private:
86 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
87 SkAutoTUnref<const GrFontDescKey> fFontScalerKey;
88 SkVarAlloc fPool;
89
90 GrBatchFontCache* fBatchFontCache;
91 int fAtlasedGlyphs;
joshualittae32c102015-04-21 09:37:57 -070092 bool fIsAbandoned;
joshualitt7c3a2f82015-03-31 13:32:05 -070093
joshualitt6c2c2b02015-07-24 10:37:00 -070094 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, GrFontScaler*);
joshualitt7c3a2f82015-03-31 13:32:05 -070095
96 friend class GrBatchFontCache;
97};
98
99/*
100 * GrBatchFontCache manages strikes which are indexed by a GrFontScaler. These strikes can then be
101 * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtlases, though this is
joshualitt62db8ba2015-04-09 08:22:37 -0700102 * more or less transparent to the client(aside from atlasGeneration, described below).
103 * Note - we used to initialize the backing atlas for the GrBatchFontCache at initialization time.
104 * However, this caused a regression, even when the GrBatchFontCache was unused. We now initialize
105 * the backing atlases lazily. Its not immediately clear why this improves the situation.
joshualitt7c3a2f82015-03-31 13:32:05 -0700106 */
107class GrBatchFontCache {
108public:
joshualitt62db8ba2015-04-09 08:22:37 -0700109 GrBatchFontCache(GrContext*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700110 ~GrBatchFontCache();
joshualittae32c102015-04-21 09:37:57 -0700111 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
112 // another client of the cache may cause the strike to be purged while it is still reffed.
113 // Therefore, the caller must check GrBatchTextStrike::isAbandoned() if there are other
114 // interactions with the cache since the strike was received.
joshualitt7c3a2f82015-03-31 13:32:05 -0700115 inline GrBatchTextStrike* getStrike(GrFontScaler* scaler) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700116 GrBatchTextStrike* strike = fCache.find(*(scaler->getKey()));
halcanary96fcdcc2015-08-27 07:41:13 -0700117 if (nullptr == strike) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700118 strike = this->generateStrike(scaler);
119 }
120 return strike;
121 }
122
joshualitt62db8ba2015-04-09 08:22:37 -0700123 void freeAll();
124
halcanary96fcdcc2015-08-27 07:41:13 -0700125 // if getTexture returns nullptr, the client must not try to use other functions on the
joshualitt62db8ba2015-04-09 08:22:37 -0700126 // GrBatchFontCache which use the atlas. This function *must* be called first, before other
127 // functions which use the atlas.
128 GrTexture* getTexture(GrMaskFormat format) {
129 if (this->initAtlas(format)) {
130 return this->getAtlas(format)->getTexture();
131 }
halcanary96fcdcc2015-08-27 07:41:13 -0700132 return nullptr;
joshualitt62db8ba2015-04-09 08:22:37 -0700133 }
134
135 bool hasGlyph(GrGlyph* glyph) {
136 SkASSERT(glyph);
137 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
138 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700139
140 // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store,
bsalomon75398562015-08-17 12:55:38 -0700141 // the client must pass in the current batch token along with the GrGlyph.
joshualittb4c507e2015-04-08 08:07:59 -0700142 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
143 // For convenience, this function will also set the use token for the current glyph if required
144 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
joshualitt62db8ba2015-04-09 08:22:37 -0700145 void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater,
bsalomon75398562015-08-17 12:55:38 -0700146 GrGlyph* glyph, GrBatchToken token) {
joshualitt62db8ba2015-04-09 08:22:37 -0700147 SkASSERT(glyph);
148 updater->add(glyph->fID);
149 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
150 }
joshualittb4c507e2015-04-08 08:07:59 -0700151
joshualitt62db8ba2015-04-09 08:22:37 -0700152 void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater,
bsalomon75398562015-08-17 12:55:38 -0700153 GrBatchToken token,
joshualitt62db8ba2015-04-09 08:22:37 -0700154 GrMaskFormat format) {
155 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
156 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700157
158 // add to texture atlas that matches this format
joshualitt62db8ba2015-04-09 08:22:37 -0700159 bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id,
bsalomon75398562015-08-17 12:55:38 -0700160 GrDrawBatch::Target* target,
joshualitt62db8ba2015-04-09 08:22:37 -0700161 GrMaskFormat format, int width, int height, const void* image,
162 SkIPoint16* loc) {
163 fPreserveStrike = strike;
bsalomon75398562015-08-17 12:55:38 -0700164 return this->getAtlas(format)->addToAtlas(id, target, width, height, image, loc);
joshualitt62db8ba2015-04-09 08:22:37 -0700165 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700166
167 // Some clients may wish to verify the integrity of the texture backing store of the
168 // GrBatchAtlas. The atlasGeneration returned below is a monitonically increasing number which
169 // changes everytime something is removed from the texture backing store.
joshualitt62db8ba2015-04-09 08:22:37 -0700170 uint64_t atlasGeneration(GrMaskFormat format) const {
171 return this->getAtlas(format)->atlasGeneration();
172 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700173
joshualittda04e0e2015-08-19 08:16:43 -0700174 ///////////////////////////////////////////////////////////////////////////
175 // Functions intended debug only
joshualitt7c3a2f82015-03-31 13:32:05 -0700176 void dump() const;
177
joshualittda04e0e2015-08-19 08:16:43 -0700178 void setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]);
179
joshualitt7c3a2f82015-03-31 13:32:05 -0700180private:
bsalomon265697d2015-07-22 10:17:26 -0700181 static GrPixelConfig MaskFormatToPixelConfig(GrMaskFormat format) {
182 static const GrPixelConfig kPixelConfigs[] = {
183 kAlpha_8_GrPixelConfig,
184 kRGB_565_GrPixelConfig,
185 kSkia8888_GrPixelConfig
186 };
bungeman99fe8222015-08-20 07:57:51 -0700187 static_assert(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, "array_size_mismatch");
bsalomon265697d2015-07-22 10:17:26 -0700188
189 return kPixelConfigs[format];
190 }
191
joshualitt7c3a2f82015-03-31 13:32:05 -0700192 // There is a 1:1 mapping between GrMaskFormats and atlas indices
joshualitt62db8ba2015-04-09 08:22:37 -0700193 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
194 static const int sAtlasIndices[] = {
195 kA8_GrMaskFormat,
196 kA565_GrMaskFormat,
197 kARGB_GrMaskFormat,
198 };
bungeman99fe8222015-08-20 07:57:51 -0700199 static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch");
joshualitt7c3a2f82015-03-31 13:32:05 -0700200
joshualitt62db8ba2015-04-09 08:22:37 -0700201 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
202 return sAtlasIndices[format];
203 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700204
joshualitt62db8ba2015-04-09 08:22:37 -0700205 bool initAtlas(GrMaskFormat);
206
207 GrBatchTextStrike* generateStrike(GrFontScaler* scaler) {
halcanary385fe4d2015-08-26 13:07:48 -0700208 GrBatchTextStrike* strike = new GrBatchTextStrike(this, scaler->getKey());
joshualitt62db8ba2015-04-09 08:22:37 -0700209 fCache.add(strike);
210 return strike;
211 }
212
213 GrBatchAtlas* getAtlas(GrMaskFormat format) const {
214 int atlasIndex = MaskFormatToAtlasIndex(format);
215 SkASSERT(fAtlases[atlasIndex]);
216 return fAtlases[atlasIndex];
217 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700218
219 static void HandleEviction(GrBatchAtlas::AtlasID, void*);
220
joshualitt62db8ba2015-04-09 08:22:37 -0700221 GrContext* fContext;
joshualitt7c3a2f82015-03-31 13:32:05 -0700222 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey> fCache;
joshualitt7c3a2f82015-03-31 13:32:05 -0700223 GrBatchAtlas* fAtlases[kMaskFormatCount];
224 GrBatchTextStrike* fPreserveStrike;
joshualittda04e0e2015-08-19 08:16:43 -0700225 GrBatchAtlasConfig fAtlasConfigs[kMaskFormatCount];
joshualitt7c3a2f82015-03-31 13:32:05 -0700226};
227
228#endif