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; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 19 | class GrGpu; |
| 20 | |
| 21 | /** |
joshualitt | 4765bdc | 2015-07-23 10:58:48 -0700 | [diff] [blame] | 22 | * 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 24 | */ |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 25 | class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 26 | public: |
| 27 | GrBatchTextStrike(GrBatchFontCache*, const GrFontDescKey* fontScalerKey); |
| 28 | ~GrBatchTextStrike(); |
| 29 | |
| 30 | const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; } |
| 31 | GrBatchFontCache* getBatchFontCache() const { return fBatchFontCache; } |
| 32 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 33 | inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed, |
| 34 | GrFontScaler* scaler) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 35 | GrGlyph* glyph = fCache.find(packed); |
| 36 | if (NULL == glyph) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 37 | glyph = this->generateGlyph(skGlyph, packed, scaler); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 38 | } |
| 39 | return glyph; |
| 40 | } |
| 41 | |
joshualitt | 76cc657 | 2015-07-31 05:51:45 -0700 | [diff] [blame] | 42 | // 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 | |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 56 | // 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. |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 61 | bool addGlyphToAtlas(GrDrawBatch::Target*, GrGlyph*, GrFontScaler*, const SkGlyph&, |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 62 | GrMaskFormat expectedMaskFormat); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 63 | |
| 64 | // testing |
| 65 | int countGlyphs() const { return fCache.count(); } |
| 66 | |
| 67 | // remove any references to this plot |
| 68 | void removeID(GrBatchAtlas::AtlasID); |
| 69 | |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 70 | // If a TextStrike is abandoned by the cache, then the caller must get a new strike |
| 71 | bool isAbandoned() const { return fIsAbandoned; } |
| 72 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 73 | 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 | |
| 80 | private: |
| 81 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; |
| 82 | SkAutoTUnref<const GrFontDescKey> fFontScalerKey; |
| 83 | SkVarAlloc fPool; |
| 84 | |
| 85 | GrBatchFontCache* fBatchFontCache; |
| 86 | int fAtlasedGlyphs; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 87 | bool fIsAbandoned; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 88 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 89 | GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, GrFontScaler*); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 90 | |
| 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 |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 97 | * 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 101 | */ |
| 102 | class GrBatchFontCache { |
| 103 | public: |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 104 | GrBatchFontCache(GrContext*); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 105 | ~GrBatchFontCache(); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 106 | // 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 110 | inline GrBatchTextStrike* getStrike(GrFontScaler* scaler) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 111 | GrBatchTextStrike* strike = fCache.find(*(scaler->getKey())); |
| 112 | if (NULL == strike) { |
| 113 | strike = this->generateStrike(scaler); |
| 114 | } |
| 115 | return strike; |
| 116 | } |
| 117 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 118 | 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 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 134 | |
| 135 | // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 136 | // the client must pass in the current batch token along with the GrGlyph. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 137 | // 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 |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 140 | void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 141 | GrGlyph* glyph, GrBatchToken token) { |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 142 | SkASSERT(glyph); |
| 143 | updater->add(glyph->fID); |
| 144 | this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token); |
| 145 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 146 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 147 | void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 148 | GrBatchToken token, |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 149 | GrMaskFormat format) { |
| 150 | this->getAtlas(format)->setLastUseTokenBulk(updater, token); |
| 151 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 152 | |
| 153 | // add to texture atlas that matches this format |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 154 | bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 155 | GrDrawBatch::Target* target, |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 156 | GrMaskFormat format, int width, int height, const void* image, |
| 157 | SkIPoint16* loc) { |
| 158 | fPreserveStrike = strike; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 159 | return this->getAtlas(format)->addToAtlas(id, target, width, height, image, loc); |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 160 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 161 | |
| 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. |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 165 | uint64_t atlasGeneration(GrMaskFormat format) const { |
| 166 | return this->getAtlas(format)->atlasGeneration(); |
| 167 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 168 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame^] | 169 | /////////////////////////////////////////////////////////////////////////// |
| 170 | // Functions intended debug only |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 171 | void dump() const; |
| 172 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame^] | 173 | void setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]); |
| 174 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 175 | private: |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 176 | 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 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 187 | // There is a 1:1 mapping between GrMaskFormats and atlas indices |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 188 | 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); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 195 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 196 | SkASSERT(sAtlasIndices[format] < kMaskFormatCount); |
| 197 | return sAtlasIndices[format]; |
| 198 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 199 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 200 | 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 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 213 | |
| 214 | static void HandleEviction(GrBatchAtlas::AtlasID, void*); |
| 215 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 216 | GrContext* fContext; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 217 | SkTDynamicHash<GrBatchTextStrike, GrFontDescKey> fCache; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 218 | GrBatchAtlas* fAtlases[kMaskFormatCount]; |
| 219 | GrBatchTextStrike* fPreserveStrike; |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame^] | 220 | GrBatchAtlasConfig fAtlasConfigs[kMaskFormatCount]; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | #endif |