blob: fc2f3b7a29f413259a4792a4796384e6b2feb4ed [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);
36 if (NULL == 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
46 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
47 GrMaskFormat expectedMaskFormat, GrFontScaler* scaler) {
48 GrGlyph* glyph = fCache.find(packed);
49 if (NULL == glyph) {
50 glyph = this->generateGlyph(skGlyph, packed, scaler);
51 glyph->fMaskFormat = expectedMaskFormat;
52 }
53 return glyph;
54 }
55
joshualitt4f19ca32015-07-30 07:59:20 -070056 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
57 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
58 // happen.
59 // TODO we can handle some of these cases if we really want to, but the long term solution is to
60 // get the actual glyph image itself when we get the glyph metrics.
bsalomon75398562015-08-17 12:55:38 -070061 bool addGlyphToAtlas(GrDrawBatch::Target*, GrGlyph*, GrFontScaler*, const SkGlyph&,
joshualitt4f19ca32015-07-30 07:59:20 -070062 GrMaskFormat expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -070063
64 // testing
65 int countGlyphs() const { return fCache.count(); }
66
67 // remove any references to this plot
68 void removeID(GrBatchAtlas::AtlasID);
69
joshualittae32c102015-04-21 09:37:57 -070070 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
71 bool isAbandoned() const { return fIsAbandoned; }
72
joshualitt7c3a2f82015-03-31 13:32:05 -070073 static const GrFontDescKey& GetKey(const GrBatchTextStrike& ts) {
74 return *(ts.fFontScalerKey);
75 }
76 static uint32_t Hash(const GrFontDescKey& key) {
77 return key.getHash();
78 }
79
80private:
81 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
82 SkAutoTUnref<const GrFontDescKey> fFontScalerKey;
83 SkVarAlloc fPool;
84
85 GrBatchFontCache* fBatchFontCache;
86 int fAtlasedGlyphs;
joshualittae32c102015-04-21 09:37:57 -070087 bool fIsAbandoned;
joshualitt7c3a2f82015-03-31 13:32:05 -070088
joshualitt6c2c2b02015-07-24 10:37:00 -070089 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, GrFontScaler*);
joshualitt7c3a2f82015-03-31 13:32:05 -070090
91 friend class GrBatchFontCache;
92};
93
94/*
95 * GrBatchFontCache manages strikes which are indexed by a GrFontScaler. These strikes can then be
96 * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtlases, though this is
joshualitt62db8ba2015-04-09 08:22:37 -070097 * more or less transparent to the client(aside from atlasGeneration, described below).
98 * Note - we used to initialize the backing atlas for the GrBatchFontCache at initialization time.
99 * However, this caused a regression, even when the GrBatchFontCache was unused. We now initialize
100 * the backing atlases lazily. Its not immediately clear why this improves the situation.
joshualitt7c3a2f82015-03-31 13:32:05 -0700101 */
102class GrBatchFontCache {
103public:
joshualitt62db8ba2015-04-09 08:22:37 -0700104 GrBatchFontCache(GrContext*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700105 ~GrBatchFontCache();
joshualittae32c102015-04-21 09:37:57 -0700106 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
107 // another client of the cache may cause the strike to be purged while it is still reffed.
108 // Therefore, the caller must check GrBatchTextStrike::isAbandoned() if there are other
109 // interactions with the cache since the strike was received.
joshualitt7c3a2f82015-03-31 13:32:05 -0700110 inline GrBatchTextStrike* getStrike(GrFontScaler* scaler) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700111 GrBatchTextStrike* strike = fCache.find(*(scaler->getKey()));
112 if (NULL == strike) {
113 strike = this->generateStrike(scaler);
114 }
115 return strike;
116 }
117
joshualitt62db8ba2015-04-09 08:22:37 -0700118 void freeAll();
119
120 // if getTexture returns NULL, the client must not try to use other functions on the
121 // GrBatchFontCache which use the atlas. This function *must* be called first, before other
122 // functions which use the atlas.
123 GrTexture* getTexture(GrMaskFormat format) {
124 if (this->initAtlas(format)) {
125 return this->getAtlas(format)->getTexture();
126 }
127 return NULL;
128 }
129
130 bool hasGlyph(GrGlyph* glyph) {
131 SkASSERT(glyph);
132 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
133 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700134
135 // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store,
bsalomon75398562015-08-17 12:55:38 -0700136 // the client must pass in the current batch token along with the GrGlyph.
joshualittb4c507e2015-04-08 08:07:59 -0700137 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
138 // For convenience, this function will also set the use token for the current glyph if required
139 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
joshualitt62db8ba2015-04-09 08:22:37 -0700140 void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater,
bsalomon75398562015-08-17 12:55:38 -0700141 GrGlyph* glyph, GrBatchToken token) {
joshualitt62db8ba2015-04-09 08:22:37 -0700142 SkASSERT(glyph);
143 updater->add(glyph->fID);
144 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
145 }
joshualittb4c507e2015-04-08 08:07:59 -0700146
joshualitt62db8ba2015-04-09 08:22:37 -0700147 void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater,
bsalomon75398562015-08-17 12:55:38 -0700148 GrBatchToken token,
joshualitt62db8ba2015-04-09 08:22:37 -0700149 GrMaskFormat format) {
150 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
151 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700152
153 // add to texture atlas that matches this format
joshualitt62db8ba2015-04-09 08:22:37 -0700154 bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id,
bsalomon75398562015-08-17 12:55:38 -0700155 GrDrawBatch::Target* target,
joshualitt62db8ba2015-04-09 08:22:37 -0700156 GrMaskFormat format, int width, int height, const void* image,
157 SkIPoint16* loc) {
158 fPreserveStrike = strike;
bsalomon75398562015-08-17 12:55:38 -0700159 return this->getAtlas(format)->addToAtlas(id, target, width, height, image, loc);
joshualitt62db8ba2015-04-09 08:22:37 -0700160 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700161
162 // Some clients may wish to verify the integrity of the texture backing store of the
163 // GrBatchAtlas. The atlasGeneration returned below is a monitonically increasing number which
164 // changes everytime something is removed from the texture backing store.
joshualitt62db8ba2015-04-09 08:22:37 -0700165 uint64_t atlasGeneration(GrMaskFormat format) const {
166 return this->getAtlas(format)->atlasGeneration();
167 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700168
joshualittda04e0e2015-08-19 08:16:43 -0700169 ///////////////////////////////////////////////////////////////////////////
170 // Functions intended debug only
joshualitt7c3a2f82015-03-31 13:32:05 -0700171 void dump() const;
172
joshualittda04e0e2015-08-19 08:16:43 -0700173 void setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]);
174
joshualitt7c3a2f82015-03-31 13:32:05 -0700175private:
bsalomon265697d2015-07-22 10:17:26 -0700176 static GrPixelConfig MaskFormatToPixelConfig(GrMaskFormat format) {
177 static const GrPixelConfig kPixelConfigs[] = {
178 kAlpha_8_GrPixelConfig,
179 kRGB_565_GrPixelConfig,
180 kSkia8888_GrPixelConfig
181 };
182 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_size_mismatch);
183
184 return kPixelConfigs[format];
185 }
186
joshualitt7c3a2f82015-03-31 13:32:05 -0700187 // There is a 1:1 mapping between GrMaskFormats and atlas indices
joshualitt62db8ba2015-04-09 08:22:37 -0700188 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
189 static const int sAtlasIndices[] = {
190 kA8_GrMaskFormat,
191 kA565_GrMaskFormat,
192 kARGB_GrMaskFormat,
193 };
194 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch);
joshualitt7c3a2f82015-03-31 13:32:05 -0700195
joshualitt62db8ba2015-04-09 08:22:37 -0700196 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
197 return sAtlasIndices[format];
198 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700199
joshualitt62db8ba2015-04-09 08:22:37 -0700200 bool initAtlas(GrMaskFormat);
201
202 GrBatchTextStrike* generateStrike(GrFontScaler* scaler) {
203 GrBatchTextStrike* strike = SkNEW_ARGS(GrBatchTextStrike, (this, scaler->getKey()));
204 fCache.add(strike);
205 return strike;
206 }
207
208 GrBatchAtlas* getAtlas(GrMaskFormat format) const {
209 int atlasIndex = MaskFormatToAtlasIndex(format);
210 SkASSERT(fAtlases[atlasIndex]);
211 return fAtlases[atlasIndex];
212 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700213
214 static void HandleEviction(GrBatchAtlas::AtlasID, void*);
215
joshualitt62db8ba2015-04-09 08:22:37 -0700216 GrContext* fContext;
joshualitt7c3a2f82015-03-31 13:32:05 -0700217 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey> fCache;
joshualitt7c3a2f82015-03-31 13:32:05 -0700218 GrBatchAtlas* fAtlases[kMaskFormatCount];
219 GrBatchTextStrike* fPreserveStrike;
joshualittda04e0e2015-08-19 08:16:43 -0700220 GrBatchAtlasConfig fAtlasConfigs[kMaskFormatCount];
joshualitt7c3a2f82015-03-31 13:32:05 -0700221};
222
223#endif