blob: 998c220e8ab14e35d62d577b39d473fdc9326e09 [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;
19class GrBatchTarget;
20class GrGpu;
21
22/**
joshualitt4765bdc2015-07-23 10:58:48 -070023 * The GrBatchTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
24 * is indexed by a PackedID and GrFontScaler. The GrFontScaler is what actually creates the mask.
joshualitt7c3a2f82015-03-31 13:32:05 -070025 */
joshualittae32c102015-04-21 09:37:57 -070026class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> {
joshualitt7c3a2f82015-03-31 13:32:05 -070027public:
28 GrBatchTextStrike(GrBatchFontCache*, const GrFontDescKey* fontScalerKey);
29 ~GrBatchTextStrike();
30
31 const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; }
32 GrBatchFontCache* getBatchFontCache() const { return fBatchFontCache; }
33
joshualitt6c2c2b02015-07-24 10:37:00 -070034 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
35 GrFontScaler* scaler) {
joshualitt7c3a2f82015-03-31 13:32:05 -070036 GrGlyph* glyph = fCache.find(packed);
37 if (NULL == glyph) {
joshualitt6c2c2b02015-07-24 10:37:00 -070038 glyph = this->generateGlyph(skGlyph, packed, scaler);
joshualitt7c3a2f82015-03-31 13:32:05 -070039 }
40 return glyph;
41 }
42
joshualitt76cc6572015-07-31 05:51:45 -070043 // This variant of the above function is called by TextBatch. At this point, it is possible
44 // that the maskformat of the glyph differs from what we expect. In these cases we will just
45 // draw a clear square.
46 // skbug:4143 crbug:510931
47 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
48 GrMaskFormat expectedMaskFormat, GrFontScaler* scaler) {
49 GrGlyph* glyph = fCache.find(packed);
50 if (NULL == glyph) {
51 glyph = this->generateGlyph(skGlyph, packed, scaler);
52 glyph->fMaskFormat = expectedMaskFormat;
53 }
54 return glyph;
55 }
56
joshualitt4f19ca32015-07-30 07:59:20 -070057 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
58 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
59 // happen.
60 // TODO we can handle some of these cases if we really want to, but the long term solution is to
61 // get the actual glyph image itself when we get the glyph metrics.
62 bool addGlyphToAtlas(GrBatchTarget*, GrGlyph*, GrFontScaler*, const SkGlyph&,
63 GrMaskFormat expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -070064
65 // testing
66 int countGlyphs() const { return fCache.count(); }
67
68 // remove any references to this plot
69 void removeID(GrBatchAtlas::AtlasID);
70
joshualittae32c102015-04-21 09:37:57 -070071 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
72 bool isAbandoned() const { return fIsAbandoned; }
73
joshualitt7c3a2f82015-03-31 13:32:05 -070074 static const GrFontDescKey& GetKey(const GrBatchTextStrike& ts) {
75 return *(ts.fFontScalerKey);
76 }
77 static uint32_t Hash(const GrFontDescKey& key) {
78 return key.getHash();
79 }
80
81private:
82 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
83 SkAutoTUnref<const GrFontDescKey> fFontScalerKey;
84 SkVarAlloc fPool;
85
86 GrBatchFontCache* fBatchFontCache;
87 int fAtlasedGlyphs;
joshualittae32c102015-04-21 09:37:57 -070088 bool fIsAbandoned;
joshualitt7c3a2f82015-03-31 13:32:05 -070089
joshualitt6c2c2b02015-07-24 10:37:00 -070090 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, GrFontScaler*);
joshualitt7c3a2f82015-03-31 13:32:05 -070091
92 friend class GrBatchFontCache;
93};
94
95/*
96 * GrBatchFontCache manages strikes which are indexed by a GrFontScaler. These strikes can then be
97 * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtlases, though this is
joshualitt62db8ba2015-04-09 08:22:37 -070098 * more or less transparent to the client(aside from atlasGeneration, described below).
99 * Note - we used to initialize the backing atlas for the GrBatchFontCache at initialization time.
100 * However, this caused a regression, even when the GrBatchFontCache was unused. We now initialize
101 * the backing atlases lazily. Its not immediately clear why this improves the situation.
joshualitt7c3a2f82015-03-31 13:32:05 -0700102 */
103class GrBatchFontCache {
104public:
joshualitt62db8ba2015-04-09 08:22:37 -0700105 GrBatchFontCache(GrContext*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700106 ~GrBatchFontCache();
joshualittae32c102015-04-21 09:37:57 -0700107 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
108 // another client of the cache may cause the strike to be purged while it is still reffed.
109 // Therefore, the caller must check GrBatchTextStrike::isAbandoned() if there are other
110 // interactions with the cache since the strike was received.
joshualitt7c3a2f82015-03-31 13:32:05 -0700111 inline GrBatchTextStrike* getStrike(GrFontScaler* scaler) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700112 GrBatchTextStrike* strike = fCache.find(*(scaler->getKey()));
113 if (NULL == strike) {
114 strike = this->generateStrike(scaler);
115 }
116 return strike;
117 }
118
joshualitt62db8ba2015-04-09 08:22:37 -0700119 void freeAll();
120
121 // if getTexture returns NULL, the client must not try to use other functions on the
122 // GrBatchFontCache which use the atlas. This function *must* be called first, before other
123 // functions which use the atlas.
124 GrTexture* getTexture(GrMaskFormat format) {
125 if (this->initAtlas(format)) {
126 return this->getAtlas(format)->getTexture();
127 }
128 return NULL;
129 }
130
131 bool hasGlyph(GrGlyph* glyph) {
132 SkASSERT(glyph);
133 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
134 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700135
136 // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store,
joshualittb4c507e2015-04-08 08:07:59 -0700137 // the client must pass in the currentToken from the GrBatchTarget along with the GrGlyph.
138 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
139 // For convenience, this function will also set the use token for the current glyph if required
140 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
joshualitt62db8ba2015-04-09 08:22:37 -0700141 void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater,
142 GrGlyph* glyph, GrBatchAtlas::BatchToken token) {
143 SkASSERT(glyph);
144 updater->add(glyph->fID);
145 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
146 }
joshualittb4c507e2015-04-08 08:07:59 -0700147
joshualitt62db8ba2015-04-09 08:22:37 -0700148 void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater,
149 GrBatchAtlas::BatchToken token,
150 GrMaskFormat format) {
151 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
152 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700153
154 // add to texture atlas that matches this format
joshualitt62db8ba2015-04-09 08:22:37 -0700155 bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id,
156 GrBatchTarget* batchTarget,
157 GrMaskFormat format, int width, int height, const void* image,
158 SkIPoint16* loc) {
159 fPreserveStrike = strike;
160 return this->getAtlas(format)->addToAtlas(id, batchTarget, width, height, image, loc);
161 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700162
163 // Some clients may wish to verify the integrity of the texture backing store of the
164 // GrBatchAtlas. The atlasGeneration returned below is a monitonically increasing number which
165 // changes everytime something is removed from the texture backing store.
joshualitt62db8ba2015-04-09 08:22:37 -0700166 uint64_t atlasGeneration(GrMaskFormat format) const {
167 return this->getAtlas(format)->atlasGeneration();
168 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700169
joshualitt7c3a2f82015-03-31 13:32:05 -0700170 void dump() const;
171
172private:
bsalomon265697d2015-07-22 10:17:26 -0700173 static GrPixelConfig MaskFormatToPixelConfig(GrMaskFormat format) {
174 static const GrPixelConfig kPixelConfigs[] = {
175 kAlpha_8_GrPixelConfig,
176 kRGB_565_GrPixelConfig,
177 kSkia8888_GrPixelConfig
178 };
179 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_size_mismatch);
180
181 return kPixelConfigs[format];
182 }
183
joshualitt7c3a2f82015-03-31 13:32:05 -0700184 // There is a 1:1 mapping between GrMaskFormats and atlas indices
joshualitt62db8ba2015-04-09 08:22:37 -0700185 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
186 static const int sAtlasIndices[] = {
187 kA8_GrMaskFormat,
188 kA565_GrMaskFormat,
189 kARGB_GrMaskFormat,
190 };
191 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch);
joshualitt7c3a2f82015-03-31 13:32:05 -0700192
joshualitt62db8ba2015-04-09 08:22:37 -0700193 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
194 return sAtlasIndices[format];
195 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700196
joshualitt62db8ba2015-04-09 08:22:37 -0700197 bool initAtlas(GrMaskFormat);
198
199 GrBatchTextStrike* generateStrike(GrFontScaler* scaler) {
200 GrBatchTextStrike* strike = SkNEW_ARGS(GrBatchTextStrike, (this, scaler->getKey()));
201 fCache.add(strike);
202 return strike;
203 }
204
205 GrBatchAtlas* getAtlas(GrMaskFormat format) const {
206 int atlasIndex = MaskFormatToAtlasIndex(format);
207 SkASSERT(fAtlases[atlasIndex]);
208 return fAtlases[atlasIndex];
209 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700210
211 static void HandleEviction(GrBatchAtlas::AtlasID, void*);
212
joshualitt62db8ba2015-04-09 08:22:37 -0700213 GrContext* fContext;
joshualitt7c3a2f82015-03-31 13:32:05 -0700214 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey> fCache;
joshualitt7c3a2f82015-03-31 13:32:05 -0700215 GrBatchAtlas* fAtlases[kMaskFormatCount];
216 GrBatchTextStrike* fPreserveStrike;
217};
218
219#endif