blob: 2aa67ddebee22bbfe821a66b4b803f500fd75a01 [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 "GrGlyph.h"
bsalomonc2878e22016-05-17 13:18:03 -070013#include "SkGlyphCache.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070014#include "SkTDynamicHash.h"
15#include "SkVarAlloc.h"
16
17class GrBatchFontCache;
joshualitt7c3a2f82015-03-31 13:32:05 -070018class GrGpu;
19
20/**
joshualitt4765bdc2015-07-23 10:58:48 -070021 * The GrBatchTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
bsalomonc2878e22016-05-17 13:18:03 -070022 * is indexed by a PackedID and SkGlyphCache. The SkGlyphCache is what actually creates the mask.
23 * The GrBatchTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
24 * of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrBatchTextStrikes are
bsalomonc5fd5c42016-05-17 11:58:24 -070025 * created by and owned by a GrBatchFontCache.
joshualitt7c3a2f82015-03-31 13:32:05 -070026 */
joshualittae32c102015-04-21 09:37:57 -070027class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> {
joshualitt7c3a2f82015-03-31 13:32:05 -070028public:
bsalomonc5fd5c42016-05-17 11:58:24 -070029 /** Owner is the cache that owns this strike. */
30 GrBatchTextStrike(GrBatchFontCache* owner, const SkDescriptor& fontScalerKey);
joshualitt7c3a2f82015-03-31 13:32:05 -070031 ~GrBatchTextStrike();
32
joshualitt6c2c2b02015-07-24 10:37:00 -070033 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
bsalomonc2878e22016-05-17 13:18:03 -070034 SkGlyphCache* cache) {
joshualitt7c3a2f82015-03-31 13:32:05 -070035 GrGlyph* glyph = fCache.find(packed);
halcanary96fcdcc2015-08-27 07:41:13 -070036 if (nullptr == glyph) {
bsalomonc2878e22016-05-17 13:18:03 -070037 glyph = this->generateGlyph(skGlyph, packed, cache);
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,
bsalomonc2878e22016-05-17 13:18:03 -070048 SkGlyphCache* cache) {
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).
bsalomonc2878e22016-05-17 13:18:03 -070054 const SkGlyph& skGlyph = GrToSkGlyph(cache, packed);
55 glyph = this->generateGlyph(skGlyph, packed, cache);
joshualitt76cc6572015-07-31 05:51:45 -070056 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.
bsalomonc2878e22016-05-17 13:18:03 -070066 bool addGlyphToAtlas(GrDrawBatch::Target*, GrGlyph*, SkGlyphCache*,
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
bsalomonc5fd5c42016-05-17 11:58:24 -070078 static const SkDescriptor& GetKey(const GrBatchTextStrike& ts) {
79 return *ts.fFontScalerKey.getDesc();
joshualitt7c3a2f82015-03-31 13:32:05 -070080 }
bsalomonc5fd5c42016-05-17 11:58:24 -070081
82 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
joshualitt7c3a2f82015-03-31 13:32:05 -070083
84private:
85 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
bsalomonc5fd5c42016-05-17 11:58:24 -070086 SkAutoDescriptor fFontScalerKey;
joshualitt7c3a2f82015-03-31 13:32:05 -070087 SkVarAlloc fPool;
88
89 GrBatchFontCache* fBatchFontCache;
90 int fAtlasedGlyphs;
joshualittae32c102015-04-21 09:37:57 -070091 bool fIsAbandoned;
joshualitt7c3a2f82015-03-31 13:32:05 -070092
bsalomonc2878e22016-05-17 13:18:03 -070093 static const SkGlyph& GrToSkGlyph(SkGlyphCache* cache, GrGlyph::PackedID id) {
94 return cache->getGlyphIDMetrics(GrGlyph::UnpackID(id),
95 GrGlyph::UnpackFixedX(id),
96 GrGlyph::UnpackFixedY(id));
97 }
98
99 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, SkGlyphCache*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700100
101 friend class GrBatchFontCache;
102};
103
104/*
bsalomonc2878e22016-05-17 13:18:03 -0700105 * GrBatchFontCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be
joshualitt7c3a2f82015-03-31 13:32:05 -0700106 * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtlases, though this is
joshualitt62db8ba2015-04-09 08:22:37 -0700107 * more or less transparent to the client(aside from atlasGeneration, described below).
108 * Note - we used to initialize the backing atlas for the GrBatchFontCache at initialization time.
109 * However, this caused a regression, even when the GrBatchFontCache was unused. We now initialize
110 * the backing atlases lazily. Its not immediately clear why this improves the situation.
joshualitt7c3a2f82015-03-31 13:32:05 -0700111 */
112class GrBatchFontCache {
113public:
joshualitt62db8ba2015-04-09 08:22:37 -0700114 GrBatchFontCache(GrContext*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700115 ~GrBatchFontCache();
joshualittae32c102015-04-21 09:37:57 -0700116 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
117 // another client of the cache may cause the strike to be purged while it is still reffed.
118 // Therefore, the caller must check GrBatchTextStrike::isAbandoned() if there are other
119 // interactions with the cache since the strike was received.
bsalomonc2878e22016-05-17 13:18:03 -0700120 inline GrBatchTextStrike* getStrike(const SkGlyphCache* cache) {
121 GrBatchTextStrike* strike = fCache.find(cache->getDescriptor());
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (nullptr == strike) {
bsalomonc2878e22016-05-17 13:18:03 -0700123 strike = this->generateStrike(cache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700124 }
125 return strike;
126 }
127
joshualitt62db8ba2015-04-09 08:22:37 -0700128 void freeAll();
129
halcanary96fcdcc2015-08-27 07:41:13 -0700130 // if getTexture returns nullptr, the client must not try to use other functions on the
joshualitt62db8ba2015-04-09 08:22:37 -0700131 // GrBatchFontCache which use the atlas. This function *must* be called first, before other
132 // functions which use the atlas.
133 GrTexture* getTexture(GrMaskFormat format) {
134 if (this->initAtlas(format)) {
135 return this->getAtlas(format)->getTexture();
136 }
halcanary96fcdcc2015-08-27 07:41:13 -0700137 return nullptr;
joshualitt62db8ba2015-04-09 08:22:37 -0700138 }
139
140 bool hasGlyph(GrGlyph* glyph) {
141 SkASSERT(glyph);
142 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
143 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700144
145 // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store,
bsalomon75398562015-08-17 12:55:38 -0700146 // the client must pass in the current batch token along with the GrGlyph.
joshualittb4c507e2015-04-08 08:07:59 -0700147 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
148 // For convenience, this function will also set the use token for the current glyph if required
149 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
joshualitt62db8ba2015-04-09 08:22:37 -0700150 void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater,
bsalomon342bfc22016-04-01 06:06:20 -0700151 GrGlyph* glyph, GrBatchDrawToken token) {
joshualitt62db8ba2015-04-09 08:22:37 -0700152 SkASSERT(glyph);
153 updater->add(glyph->fID);
154 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
155 }
joshualittb4c507e2015-04-08 08:07:59 -0700156
joshualitt62db8ba2015-04-09 08:22:37 -0700157 void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater,
bsalomon342bfc22016-04-01 06:06:20 -0700158 GrBatchDrawToken token,
joshualitt62db8ba2015-04-09 08:22:37 -0700159 GrMaskFormat format) {
160 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
161 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700162
163 // add to texture atlas that matches this format
joshualitt62db8ba2015-04-09 08:22:37 -0700164 bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id,
bsalomon75398562015-08-17 12:55:38 -0700165 GrDrawBatch::Target* target,
joshualitt62db8ba2015-04-09 08:22:37 -0700166 GrMaskFormat format, int width, int height, const void* image,
167 SkIPoint16* loc) {
168 fPreserveStrike = strike;
bsalomon75398562015-08-17 12:55:38 -0700169 return this->getAtlas(format)->addToAtlas(id, target, width, height, image, loc);
joshualitt62db8ba2015-04-09 08:22:37 -0700170 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700171
172 // Some clients may wish to verify the integrity of the texture backing store of the
173 // GrBatchAtlas. The atlasGeneration returned below is a monitonically increasing number which
174 // changes everytime something is removed from the texture backing store.
joshualitt62db8ba2015-04-09 08:22:37 -0700175 uint64_t atlasGeneration(GrMaskFormat format) const {
176 return this->getAtlas(format)->atlasGeneration();
177 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700178
jvanverth7023a002016-02-22 11:25:32 -0800179 int log2Width(GrMaskFormat format) { return fAtlasConfigs[format].fLog2Width; }
180 int log2Height(GrMaskFormat format) { return fAtlasConfigs[format].fLog2Height; }
181
joshualittda04e0e2015-08-19 08:16:43 -0700182 ///////////////////////////////////////////////////////////////////////////
183 // Functions intended debug only
joshualitt7c3a2f82015-03-31 13:32:05 -0700184 void dump() const;
185
joshualittda04e0e2015-08-19 08:16:43 -0700186 void setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]);
187
joshualitt7c3a2f82015-03-31 13:32:05 -0700188private:
brianosman86dc2262016-07-08 06:15:45 -0700189 static GrPixelConfig MaskFormatToPixelConfig(GrMaskFormat format, const GrCaps& caps) {
190 switch (format) {
191 case kA8_GrMaskFormat:
192 return kAlpha_8_GrPixelConfig;
193 case kA565_GrMaskFormat:
194 return kRGB_565_GrPixelConfig;
195 case kARGB_GrMaskFormat:
196 return caps.srgbSupport() ? kSkiaGamma8888_GrPixelConfig : kSkia8888_GrPixelConfig;
197 default:
198 SkDEBUGFAIL("unsupported GrMaskFormat");
199 return kAlpha_8_GrPixelConfig;
200 }
bsalomon265697d2015-07-22 10:17:26 -0700201 }
202
joshualitt7c3a2f82015-03-31 13:32:05 -0700203 // There is a 1:1 mapping between GrMaskFormats and atlas indices
joshualitt62db8ba2015-04-09 08:22:37 -0700204 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
205 static const int sAtlasIndices[] = {
206 kA8_GrMaskFormat,
207 kA565_GrMaskFormat,
208 kARGB_GrMaskFormat,
209 };
bungeman99fe8222015-08-20 07:57:51 -0700210 static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch");
joshualitt7c3a2f82015-03-31 13:32:05 -0700211
joshualitt62db8ba2015-04-09 08:22:37 -0700212 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
213 return sAtlasIndices[format];
214 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700215
joshualitt62db8ba2015-04-09 08:22:37 -0700216 bool initAtlas(GrMaskFormat);
217
bsalomonc2878e22016-05-17 13:18:03 -0700218 GrBatchTextStrike* generateStrike(const SkGlyphCache* cache) {
219 GrBatchTextStrike* strike = new GrBatchTextStrike(this, cache->getDescriptor());
joshualitt62db8ba2015-04-09 08:22:37 -0700220 fCache.add(strike);
221 return strike;
222 }
223
224 GrBatchAtlas* getAtlas(GrMaskFormat format) const {
225 int atlasIndex = MaskFormatToAtlasIndex(format);
226 SkASSERT(fAtlases[atlasIndex]);
227 return fAtlases[atlasIndex];
228 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700229
230 static void HandleEviction(GrBatchAtlas::AtlasID, void*);
231
bsalomonc5fd5c42016-05-17 11:58:24 -0700232 using StrikeHash = SkTDynamicHash<GrBatchTextStrike, SkDescriptor>;
joshualitt62db8ba2015-04-09 08:22:37 -0700233 GrContext* fContext;
bsalomonc5fd5c42016-05-17 11:58:24 -0700234 StrikeHash fCache;
joshualitt7c3a2f82015-03-31 13:32:05 -0700235 GrBatchAtlas* fAtlases[kMaskFormatCount];
236 GrBatchTextStrike* fPreserveStrike;
joshualittda04e0e2015-08-19 08:16:43 -0700237 GrBatchAtlasConfig fAtlasConfigs[kMaskFormatCount];
joshualitt7c3a2f82015-03-31 13:32:05 -0700238};
239
240#endif