blob: b5cfaf2cff2c66979218e61146f6db6475503a4e [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"
robertphillips5fa7f302016-07-21 09:21:04 -070012#include "GrCaps.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070013#include "GrGlyph.h"
bsalomonc2878e22016-05-17 13:18:03 -070014#include "SkGlyphCache.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
bsalomonc2878e22016-05-17 13:18:03 -070023 * is indexed by a PackedID and SkGlyphCache. The SkGlyphCache is what actually creates the mask.
24 * The GrBatchTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
25 * of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrBatchTextStrikes are
bsalomonc5fd5c42016-05-17 11:58:24 -070026 * created by and owned by a GrBatchFontCache.
joshualitt7c3a2f82015-03-31 13:32:05 -070027 */
joshualittae32c102015-04-21 09:37:57 -070028class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> {
joshualitt7c3a2f82015-03-31 13:32:05 -070029public:
bsalomonc5fd5c42016-05-17 11:58:24 -070030 /** Owner is the cache that owns this strike. */
31 GrBatchTextStrike(GrBatchFontCache* owner, const SkDescriptor& fontScalerKey);
joshualitt7c3a2f82015-03-31 13:32:05 -070032 ~GrBatchTextStrike();
33
joshualitt6c2c2b02015-07-24 10:37:00 -070034 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
bsalomonc2878e22016-05-17 13:18:03 -070035 SkGlyphCache* cache) {
joshualitt7c3a2f82015-03-31 13:32:05 -070036 GrGlyph* glyph = fCache.find(packed);
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (nullptr == glyph) {
bsalomonc2878e22016-05-17 13:18:03 -070038 glyph = this->generateGlyph(skGlyph, packed, cache);
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
joshualitt50aa15b2015-11-23 14:09:55 -080047 inline GrGlyph* getGlyph(GrGlyph::PackedID packed,
48 GrMaskFormat expectedMaskFormat,
bsalomonc2878e22016-05-17 13:18:03 -070049 SkGlyphCache* cache) {
joshualitt76cc6572015-07-31 05:51:45 -070050 GrGlyph* glyph = fCache.find(packed);
halcanary96fcdcc2015-08-27 07:41:13 -070051 if (nullptr == glyph) {
joshualitt50aa15b2015-11-23 14:09:55 -080052 // We could return this to the caller, but in practice it adds code complexity for
53 // potentially little benefit(ie, if the glyph is not in our font cache, then its not
54 // in the atlas and we're going to be doing a texture upload anyways).
bsalomonc2878e22016-05-17 13:18:03 -070055 const SkGlyph& skGlyph = GrToSkGlyph(cache, packed);
56 glyph = this->generateGlyph(skGlyph, packed, cache);
joshualitt76cc6572015-07-31 05:51:45 -070057 glyph->fMaskFormat = expectedMaskFormat;
58 }
59 return glyph;
60 }
61
joshualitt4f19ca32015-07-30 07:59:20 -070062 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
63 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
64 // happen.
65 // TODO we can handle some of these cases if we really want to, but the long term solution is to
66 // get the actual glyph image itself when we get the glyph metrics.
Brian Salomon9afd3712016-12-01 10:59:09 -050067 bool addGlyphToAtlas(GrDrawOp::Target*, GrGlyph*, SkGlyphCache*,
joshualitt4f19ca32015-07-30 07:59:20 -070068 GrMaskFormat expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -070069
70 // testing
71 int countGlyphs() const { return fCache.count(); }
72
73 // remove any references to this plot
74 void removeID(GrBatchAtlas::AtlasID);
75
joshualittae32c102015-04-21 09:37:57 -070076 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
77 bool isAbandoned() const { return fIsAbandoned; }
78
bsalomonc5fd5c42016-05-17 11:58:24 -070079 static const SkDescriptor& GetKey(const GrBatchTextStrike& ts) {
80 return *ts.fFontScalerKey.getDesc();
joshualitt7c3a2f82015-03-31 13:32:05 -070081 }
bsalomonc5fd5c42016-05-17 11:58:24 -070082
83 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
joshualitt7c3a2f82015-03-31 13:32:05 -070084
85private:
86 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
bsalomonc5fd5c42016-05-17 11:58:24 -070087 SkAutoDescriptor fFontScalerKey;
joshualitt7c3a2f82015-03-31 13:32:05 -070088 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
bsalomonc2878e22016-05-17 13:18:03 -070094 static const SkGlyph& GrToSkGlyph(SkGlyphCache* cache, GrGlyph::PackedID id) {
95 return cache->getGlyphIDMetrics(GrGlyph::UnpackID(id),
96 GrGlyph::UnpackFixedX(id),
97 GrGlyph::UnpackFixedY(id));
98 }
99
100 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, SkGlyphCache*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700101
102 friend class GrBatchFontCache;
103};
104
105/*
bsalomonc2878e22016-05-17 13:18:03 -0700106 * GrBatchFontCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be
joshualitt7c3a2f82015-03-31 13:32:05 -0700107 * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtlases, though this is
joshualitt62db8ba2015-04-09 08:22:37 -0700108 * more or less transparent to the client(aside from atlasGeneration, described below).
109 * Note - we used to initialize the backing atlas for the GrBatchFontCache at initialization time.
110 * However, this caused a regression, even when the GrBatchFontCache was unused. We now initialize
111 * the backing atlases lazily. Its not immediately clear why this improves the situation.
joshualitt7c3a2f82015-03-31 13:32:05 -0700112 */
113class GrBatchFontCache {
114public:
joshualitt62db8ba2015-04-09 08:22:37 -0700115 GrBatchFontCache(GrContext*);
joshualitt7c3a2f82015-03-31 13:32:05 -0700116 ~GrBatchFontCache();
joshualittae32c102015-04-21 09:37:57 -0700117 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
118 // another client of the cache may cause the strike to be purged while it is still reffed.
119 // Therefore, the caller must check GrBatchTextStrike::isAbandoned() if there are other
120 // interactions with the cache since the strike was received.
bsalomonc2878e22016-05-17 13:18:03 -0700121 inline GrBatchTextStrike* getStrike(const SkGlyphCache* cache) {
122 GrBatchTextStrike* strike = fCache.find(cache->getDescriptor());
halcanary96fcdcc2015-08-27 07:41:13 -0700123 if (nullptr == strike) {
bsalomonc2878e22016-05-17 13:18:03 -0700124 strike = this->generateStrike(cache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700125 }
126 return strike;
127 }
128
joshualitt62db8ba2015-04-09 08:22:37 -0700129 void freeAll();
130
Brian Salomondb4183d2016-11-17 12:48:40 -0500131 // if texture returns nullptr, the client must not try to use other functions on the
joshualitt62db8ba2015-04-09 08:22:37 -0700132 // GrBatchFontCache which use the atlas. This function *must* be called first, before other
133 // functions which use the atlas.
134 GrTexture* getTexture(GrMaskFormat format) {
135 if (this->initAtlas(format)) {
136 return this->getAtlas(format)->getTexture();
137 }
halcanary96fcdcc2015-08-27 07:41:13 -0700138 return nullptr;
joshualitt62db8ba2015-04-09 08:22:37 -0700139 }
140
141 bool hasGlyph(GrGlyph* glyph) {
142 SkASSERT(glyph);
143 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
144 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700145
146 // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store,
bsalomon75398562015-08-17 12:55:38 -0700147 // the client must pass in the current batch token along with the GrGlyph.
joshualittb4c507e2015-04-08 08:07:59 -0700148 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
149 // For convenience, this function will also set the use token for the current glyph if required
150 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
joshualitt62db8ba2015-04-09 08:22:37 -0700151 void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater,
Brian Salomon9afd3712016-12-01 10:59:09 -0500152 GrGlyph* glyph, GrDrawOpUploadToken token) {
joshualitt62db8ba2015-04-09 08:22:37 -0700153 SkASSERT(glyph);
154 updater->add(glyph->fID);
155 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
156 }
joshualittb4c507e2015-04-08 08:07:59 -0700157
joshualitt62db8ba2015-04-09 08:22:37 -0700158 void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater,
Brian Salomon9afd3712016-12-01 10:59:09 -0500159 GrDrawOpUploadToken token,
joshualitt62db8ba2015-04-09 08:22:37 -0700160 GrMaskFormat format) {
161 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
162 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700163
164 // add to texture atlas that matches this format
joshualitt62db8ba2015-04-09 08:22:37 -0700165 bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id,
Brian Salomon9afd3712016-12-01 10:59:09 -0500166 GrDrawOp::Target* target,
joshualitt62db8ba2015-04-09 08:22:37 -0700167 GrMaskFormat format, int width, int height, const void* image,
168 SkIPoint16* loc) {
169 fPreserveStrike = strike;
bsalomon75398562015-08-17 12:55:38 -0700170 return this->getAtlas(format)->addToAtlas(id, target, width, height, image, loc);
joshualitt62db8ba2015-04-09 08:22:37 -0700171 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700172
173 // Some clients may wish to verify the integrity of the texture backing store of the
174 // GrBatchAtlas. The atlasGeneration returned below is a monitonically increasing number which
175 // changes everytime something is removed from the texture backing store.
joshualitt62db8ba2015-04-09 08:22:37 -0700176 uint64_t atlasGeneration(GrMaskFormat format) const {
177 return this->getAtlas(format)->atlasGeneration();
178 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700179
jvanverth7023a002016-02-22 11:25:32 -0800180 int log2Width(GrMaskFormat format) { return fAtlasConfigs[format].fLog2Width; }
181 int log2Height(GrMaskFormat format) { return fAtlasConfigs[format].fLog2Height; }
182
joshualittda04e0e2015-08-19 08:16:43 -0700183 ///////////////////////////////////////////////////////////////////////////
184 // Functions intended debug only
joshualitt7c3a2f82015-03-31 13:32:05 -0700185 void dump() const;
186
joshualittda04e0e2015-08-19 08:16:43 -0700187 void setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]);
188
joshualitt7c3a2f82015-03-31 13:32:05 -0700189private:
brianosman86dc2262016-07-08 06:15:45 -0700190 static GrPixelConfig MaskFormatToPixelConfig(GrMaskFormat format, const GrCaps& caps) {
191 switch (format) {
192 case kA8_GrMaskFormat:
193 return kAlpha_8_GrPixelConfig;
194 case kA565_GrMaskFormat:
195 return kRGB_565_GrPixelConfig;
196 case kARGB_GrMaskFormat:
Brian Osmancce3e582016-10-14 11:42:20 -0400197 return caps.srgbSupport() ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
brianosman86dc2262016-07-08 06:15:45 -0700198 default:
199 SkDEBUGFAIL("unsupported GrMaskFormat");
200 return kAlpha_8_GrPixelConfig;
201 }
bsalomon265697d2015-07-22 10:17:26 -0700202 }
203
joshualitt7c3a2f82015-03-31 13:32:05 -0700204 // There is a 1:1 mapping between GrMaskFormats and atlas indices
joshualitt62db8ba2015-04-09 08:22:37 -0700205 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
206 static const int sAtlasIndices[] = {
207 kA8_GrMaskFormat,
208 kA565_GrMaskFormat,
209 kARGB_GrMaskFormat,
210 };
bungeman99fe8222015-08-20 07:57:51 -0700211 static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch");
joshualitt7c3a2f82015-03-31 13:32:05 -0700212
joshualitt62db8ba2015-04-09 08:22:37 -0700213 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
214 return sAtlasIndices[format];
215 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700216
joshualitt62db8ba2015-04-09 08:22:37 -0700217 bool initAtlas(GrMaskFormat);
218
bsalomonc2878e22016-05-17 13:18:03 -0700219 GrBatchTextStrike* generateStrike(const SkGlyphCache* cache) {
220 GrBatchTextStrike* strike = new GrBatchTextStrike(this, cache->getDescriptor());
joshualitt62db8ba2015-04-09 08:22:37 -0700221 fCache.add(strike);
222 return strike;
223 }
224
225 GrBatchAtlas* getAtlas(GrMaskFormat format) const {
226 int atlasIndex = MaskFormatToAtlasIndex(format);
227 SkASSERT(fAtlases[atlasIndex]);
Ben Wagner594f9ed2016-11-08 14:13:39 -0500228 return fAtlases[atlasIndex].get();
joshualitt62db8ba2015-04-09 08:22:37 -0700229 }
joshualitt7c3a2f82015-03-31 13:32:05 -0700230
231 static void HandleEviction(GrBatchAtlas::AtlasID, void*);
232
bsalomonc5fd5c42016-05-17 11:58:24 -0700233 using StrikeHash = SkTDynamicHash<GrBatchTextStrike, SkDescriptor>;
joshualitt62db8ba2015-04-09 08:22:37 -0700234 GrContext* fContext;
bsalomonc5fd5c42016-05-17 11:58:24 -0700235 StrikeHash fCache;
Ben Wagner594f9ed2016-11-08 14:13:39 -0500236 std::unique_ptr<GrBatchAtlas> fAtlases[kMaskFormatCount];
joshualitt7c3a2f82015-03-31 13:32:05 -0700237 GrBatchTextStrike* fPreserveStrike;
joshualittda04e0e2015-08-19 08:16:43 -0700238 GrBatchAtlasConfig fAtlasConfigs[kMaskFormatCount];
joshualitt7c3a2f82015-03-31 13:32:05 -0700239};
240
241#endif