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" |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 13 | #include "GrGlyph.h" |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 14 | #include "SkGlyphCache.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 |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 23 | * 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 |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 26 | * created by and owned by a GrBatchFontCache. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 27 | */ |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 28 | class GrBatchTextStrike : public SkNVRefCnt<GrBatchTextStrike> { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 29 | public: |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 30 | /** Owner is the cache that owns this strike. */ |
| 31 | GrBatchTextStrike(GrBatchFontCache* owner, const SkDescriptor& fontScalerKey); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 32 | ~GrBatchTextStrike(); |
| 33 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 34 | inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed, |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 35 | SkGlyphCache* cache) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 36 | GrGlyph* glyph = fCache.find(packed); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | if (nullptr == glyph) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 38 | glyph = this->generateGlyph(skGlyph, packed, cache); |
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 |
joshualitt | 50aa15b | 2015-11-23 14:09:55 -0800 | [diff] [blame] | 47 | inline GrGlyph* getGlyph(GrGlyph::PackedID packed, |
| 48 | GrMaskFormat expectedMaskFormat, |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 49 | SkGlyphCache* cache) { |
joshualitt | 76cc657 | 2015-07-31 05:51:45 -0700 | [diff] [blame] | 50 | GrGlyph* glyph = fCache.find(packed); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | if (nullptr == glyph) { |
joshualitt | 50aa15b | 2015-11-23 14:09:55 -0800 | [diff] [blame] | 52 | // 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). |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 55 | const SkGlyph& skGlyph = GrToSkGlyph(cache, packed); |
| 56 | glyph = this->generateGlyph(skGlyph, packed, cache); |
joshualitt | 76cc657 | 2015-07-31 05:51:45 -0700 | [diff] [blame] | 57 | glyph->fMaskFormat = expectedMaskFormat; |
| 58 | } |
| 59 | return glyph; |
| 60 | } |
| 61 | |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 62 | // 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 Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame^] | 67 | bool addGlyphToAtlas(GrDrawOp::Target*, GrGlyph*, SkGlyphCache*, |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 68 | GrMaskFormat expectedMaskFormat); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 69 | |
| 70 | // testing |
| 71 | int countGlyphs() const { return fCache.count(); } |
| 72 | |
| 73 | // remove any references to this plot |
| 74 | void removeID(GrBatchAtlas::AtlasID); |
| 75 | |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 76 | // If a TextStrike is abandoned by the cache, then the caller must get a new strike |
| 77 | bool isAbandoned() const { return fIsAbandoned; } |
| 78 | |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 79 | static const SkDescriptor& GetKey(const GrBatchTextStrike& ts) { |
| 80 | return *ts.fFontScalerKey.getDesc(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 81 | } |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 82 | |
| 83 | static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 87 | SkAutoDescriptor fFontScalerKey; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 88 | SkVarAlloc fPool; |
| 89 | |
| 90 | GrBatchFontCache* fBatchFontCache; |
| 91 | int fAtlasedGlyphs; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 92 | bool fIsAbandoned; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 93 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 94 | 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*); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 101 | |
| 102 | friend class GrBatchFontCache; |
| 103 | }; |
| 104 | |
| 105 | /* |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 106 | * GrBatchFontCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 107 | * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtlases, though this is |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 108 | * 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. |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 112 | */ |
| 113 | class GrBatchFontCache { |
| 114 | public: |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 115 | GrBatchFontCache(GrContext*); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 116 | ~GrBatchFontCache(); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 117 | // 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. |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 121 | inline GrBatchTextStrike* getStrike(const SkGlyphCache* cache) { |
| 122 | GrBatchTextStrike* strike = fCache.find(cache->getDescriptor()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | if (nullptr == strike) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 124 | strike = this->generateStrike(cache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 125 | } |
| 126 | return strike; |
| 127 | } |
| 128 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 129 | void freeAll(); |
| 130 | |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 131 | // if texture returns nullptr, the client must not try to use other functions on the |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 132 | // 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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | return nullptr; |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool hasGlyph(GrGlyph* glyph) { |
| 142 | SkASSERT(glyph); |
| 143 | return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID); |
| 144 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 145 | |
| 146 | // 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] | 147 | // the client must pass in the current batch token along with the GrGlyph. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 148 | // 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 |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 151 | void addGlyphToBulkAndSetUseToken(GrBatchAtlas::BulkUseTokenUpdater* updater, |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame^] | 152 | GrGlyph* glyph, GrDrawOpUploadToken token) { |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 153 | SkASSERT(glyph); |
| 154 | updater->add(glyph->fID); |
| 155 | this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token); |
| 156 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 157 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 158 | void setUseTokenBulk(const GrBatchAtlas::BulkUseTokenUpdater& updater, |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame^] | 159 | GrDrawOpUploadToken token, |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 160 | GrMaskFormat format) { |
| 161 | this->getAtlas(format)->setLastUseTokenBulk(updater, token); |
| 162 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 163 | |
| 164 | // add to texture atlas that matches this format |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 165 | bool addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id, |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame^] | 166 | GrDrawOp::Target* target, |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 167 | GrMaskFormat format, int width, int height, const void* image, |
| 168 | SkIPoint16* loc) { |
| 169 | fPreserveStrike = strike; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 170 | return this->getAtlas(format)->addToAtlas(id, target, width, height, image, loc); |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 171 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 172 | |
| 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. |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 176 | uint64_t atlasGeneration(GrMaskFormat format) const { |
| 177 | return this->getAtlas(format)->atlasGeneration(); |
| 178 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 179 | |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 180 | int log2Width(GrMaskFormat format) { return fAtlasConfigs[format].fLog2Width; } |
| 181 | int log2Height(GrMaskFormat format) { return fAtlasConfigs[format].fLog2Height; } |
| 182 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 183 | /////////////////////////////////////////////////////////////////////////// |
| 184 | // Functions intended debug only |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 185 | void dump() const; |
| 186 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 187 | void setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]); |
| 188 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 189 | private: |
brianosman | 86dc226 | 2016-07-08 06:15:45 -0700 | [diff] [blame] | 190 | 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 Osman | cce3e58 | 2016-10-14 11:42:20 -0400 | [diff] [blame] | 197 | return caps.srgbSupport() ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig; |
brianosman | 86dc226 | 2016-07-08 06:15:45 -0700 | [diff] [blame] | 198 | default: |
| 199 | SkDEBUGFAIL("unsupported GrMaskFormat"); |
| 200 | return kAlpha_8_GrPixelConfig; |
| 201 | } |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 202 | } |
| 203 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 204 | // There is a 1:1 mapping between GrMaskFormats and atlas indices |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 205 | static int MaskFormatToAtlasIndex(GrMaskFormat format) { |
| 206 | static const int sAtlasIndices[] = { |
| 207 | kA8_GrMaskFormat, |
| 208 | kA565_GrMaskFormat, |
| 209 | kARGB_GrMaskFormat, |
| 210 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 211 | static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch"); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 212 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 213 | SkASSERT(sAtlasIndices[format] < kMaskFormatCount); |
| 214 | return sAtlasIndices[format]; |
| 215 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 216 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 217 | bool initAtlas(GrMaskFormat); |
| 218 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 219 | GrBatchTextStrike* generateStrike(const SkGlyphCache* cache) { |
| 220 | GrBatchTextStrike* strike = new GrBatchTextStrike(this, cache->getDescriptor()); |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 221 | fCache.add(strike); |
| 222 | return strike; |
| 223 | } |
| 224 | |
| 225 | GrBatchAtlas* getAtlas(GrMaskFormat format) const { |
| 226 | int atlasIndex = MaskFormatToAtlasIndex(format); |
| 227 | SkASSERT(fAtlases[atlasIndex]); |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 228 | return fAtlases[atlasIndex].get(); |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 229 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 230 | |
| 231 | static void HandleEviction(GrBatchAtlas::AtlasID, void*); |
| 232 | |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 233 | using StrikeHash = SkTDynamicHash<GrBatchTextStrike, SkDescriptor>; |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 234 | GrContext* fContext; |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 235 | StrikeHash fCache; |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 236 | std::unique_ptr<GrBatchAtlas> fAtlases[kMaskFormatCount]; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 237 | GrBatchTextStrike* fPreserveStrike; |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 238 | GrBatchAtlasConfig fAtlasConfigs[kMaskFormatCount]; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | #endif |