joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 1 | /* |
| 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" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 12 | #include "GrFontScaler.h" |
| 13 | #include "GrGlyph.h" |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 14 | #include "SkGlyph.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 15 | #include "SkTDynamicHash.h" |
| 16 | #include "SkVarAlloc.h" |
| 17 | |
| 18 | class GrBatchFontCache; |
| 19 | class GrBatchTarget; |
| 20 | class GrGpu; |
| 21 | |
| 22 | /** |
joshualitt | 4765bdc | 2015-07-23 10:58:48 -0700 | [diff] [blame] | 23 | * 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 25 | */ |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 26 | class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 27 | public: |
| 28 | GrBatchTextStrike(GrBatchFontCache*, const GrFontDescKey* fontScalerKey); |
| 29 | ~GrBatchTextStrike(); |
| 30 | |
| 31 | const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; } |
| 32 | GrBatchFontCache* getBatchFontCache() const { return fBatchFontCache; } |
| 33 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 34 | inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed, |
| 35 | GrFontScaler* scaler) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 36 | GrGlyph* glyph = fCache.find(packed); |
| 37 | if (NULL == glyph) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 38 | glyph = this->generateGlyph(skGlyph, packed, scaler); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 39 | } |
| 40 | return glyph; |
| 41 | } |
| 42 | |
joshualitt | 76cc657 | 2015-07-31 05:51:45 -0700 | [diff] [blame] | 43 | // 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 | |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 57 | // 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); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 64 | |
| 65 | // testing |
| 66 | int countGlyphs() const { return fCache.count(); } |
| 67 | |
| 68 | // remove any references to this plot |
| 69 | void removeID(GrBatchAtlas::AtlasID); |
| 70 | |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 71 | // If a TextStrike is abandoned by the cache, then the caller must get a new strike |
| 72 | bool isAbandoned() const { return fIsAbandoned; } |
| 73 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 74 | 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 | |
| 81 | private: |
| 82 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; |
| 83 | SkAutoTUnref<const GrFontDescKey> fFontScalerKey; |
| 84 | SkVarAlloc fPool; |
| 85 | |
| 86 | GrBatchFontCache* fBatchFontCache; |
| 87 | int fAtlasedGlyphs; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 88 | bool fIsAbandoned; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 89 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 90 | GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, GrFontScaler*); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 91 | |
| 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 |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 98 | * 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 102 | */ |
| 103 | class GrBatchFontCache { |
| 104 | public: |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 105 | GrBatchFontCache(GrContext*); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 106 | ~GrBatchFontCache(); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 107 | // 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 111 | inline GrBatchTextStrike* getStrike(GrFontScaler* scaler) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 112 | GrBatchTextStrike* strike = fCache.find(*(scaler->getKey())); |
| 113 | if (NULL == strike) { |
| 114 | strike = this->generateStrike(scaler); |
| 115 | } |
| 116 | return strike; |
| 117 | } |
| 118 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 119 | 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 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 135 | |
| 136 | // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store, |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 137 | // 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 |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 141 | 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 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 147 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 148 | void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater, |
| 149 | GrBatchAtlas::BatchToken token, |
| 150 | GrMaskFormat format) { |
| 151 | this->getAtlas(format)->setLastUseTokenBulk(updater, token); |
| 152 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 153 | |
| 154 | // add to texture atlas that matches this format |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 155 | 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 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 162 | |
| 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. |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 166 | uint64_t atlasGeneration(GrMaskFormat format) const { |
| 167 | return this->getAtlas(format)->atlasGeneration(); |
| 168 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 169 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 170 | void dump() const; |
| 171 | |
| 172 | private: |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 173 | 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 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 184 | // There is a 1:1 mapping between GrMaskFormats and atlas indices |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 185 | 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); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 192 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 193 | SkASSERT(sAtlasIndices[format] < kMaskFormatCount); |
| 194 | return sAtlasIndices[format]; |
| 195 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 196 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 197 | 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 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 210 | |
| 211 | static void HandleEviction(GrBatchAtlas::AtlasID, void*); |
| 212 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 213 | GrContext* fContext; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 214 | SkTDynamicHash<GrBatchTextStrike, GrFontDescKey> fCache; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 215 | GrBatchAtlas* fAtlases[kMaskFormatCount]; |
| 216 | GrBatchTextStrike* fPreserveStrike; |
| 217 | }; |
| 218 | |
| 219 | #endif |