Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 12 #include "GrDrawTarget.h" | |
| 13 #include "GrFontScaler.h" | |
| 14 #include "GrGlyph.h" | |
| 15 #include "SkTDynamicHash.h" | |
| 16 #include "SkVarAlloc.h" | |
| 17 | |
| 18 class GrBatchFontCache; | |
| 19 class GrBatchTarget; | |
| 20 class GrGpu; | |
| 21 | |
| 22 /** | |
| 23 * The GrBatchTextStrike manages a pool of CPU backing memory for Glyph Masks. This backing memory | |
| 24 * is abstracted by GrGlyph, and indexed by a PackedID and GrFontScaler. The G rFontScaler is what | |
| 25 * actually creates the mask. | |
| 26 */ | |
| 27 class GrBatchTextStrike { | |
| 28 public: | |
| 29 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrBatchTextStrike); | |
| 30 GrBatchTextStrike(GrBatchFontCache*, const GrFontDescKey* fontScalerKey); | |
| 31 ~GrBatchTextStrike(); | |
| 32 | |
| 33 const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; } | |
| 34 GrBatchFontCache* getBatchFontCache() const { return fBatchFontCache; } | |
| 35 | |
| 36 inline GrGlyph* getGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler) { | |
| 37 GrGlyph* glyph = fCache.find(packed); | |
| 38 if (NULL == glyph) { | |
| 39 glyph = this->generateGlyph(packed, scaler); | |
| 40 } | |
| 41 return glyph; | |
| 42 } | |
| 43 | |
| 44 // returns true if glyph (or glyph+padding for distance field) | |
| 45 // is too large to ever fit in texture atlas subregions (GrPlots) | |
| 46 bool glyphTooLargeForAtlas(GrGlyph*); | |
| 47 // returns true if glyph successfully added to texture atlas, false otherwis e | |
| 48 bool addGlyphToAtlas(GrBatchTarget*, GrGlyph*, GrFontScaler*); | |
| 49 | |
| 50 // testing | |
| 51 int countGlyphs() const { return fCache.count(); } | |
| 52 | |
| 53 // remove any references to this plot | |
| 54 void removeID(GrBatchAtlas::AtlasID); | |
| 55 | |
| 56 static const GrFontDescKey& GetKey(const GrBatchTextStrike& ts) { | |
| 57 return *(ts.fFontScalerKey); | |
| 58 } | |
| 59 static uint32_t Hash(const GrFontDescKey& key) { | |
| 60 return key.getHash(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; | |
| 65 SkAutoTUnref<const GrFontDescKey> fFontScalerKey; | |
| 66 SkVarAlloc fPool; | |
| 67 | |
| 68 GrBatchFontCache* fBatchFontCache; | |
| 69 int fAtlasedGlyphs; | |
| 70 | |
| 71 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler); | |
| 72 | |
| 73 friend class GrBatchFontCache; | |
| 74 }; | |
| 75 | |
| 76 /* | |
| 77 * GrBatchFontCache manages strikes which are indexed by a GrFontScaler. These strikes can then be | |
| 78 * used to individual Glyph Masks. The GrBatchFontCache also manages GrBatchAtl ases, though this is | |
| 79 * more or less transparent to the client(aside from atlasGeneration, described below) | |
| 80 */ | |
| 81 class GrBatchFontCache { | |
| 82 public: | |
| 83 GrBatchFontCache(GrContext*); | |
| 84 ~GrBatchFontCache(); | |
| 85 | |
| 86 inline GrBatchTextStrike* getStrike(GrFontScaler* scaler) { | |
| 87 SkDEBUGCODE(this->validate();) | |
| 88 | |
| 89 GrBatchTextStrike* strike = fCache.find(*(scaler->getKey())); | |
| 90 if (NULL == strike) { | |
| 91 strike = this->generateStrike(scaler); | |
| 92 } else { | |
| 93 this->makeMRU(strike); | |
| 94 } | |
| 95 SkDEBUGCODE(this->validate();) | |
| 96 return strike; | |
| 97 } | |
| 98 | |
| 99 bool hasGlyph(GrGlyph* glyph); | |
| 100 | |
| 101 // To ensure the GrBatchAtlas does not evict the Glyph Mask from its texture backing store, | |
| 102 // the client must pass in the currentToken from the GrBatchTarget along wit h the GrGlyph | |
| 103 void setGlyphRefToken(GrGlyph*, GrBatchAtlas::BatchToken); | |
| 104 | |
| 105 // add to texture atlas that matches this format | |
| 106 bool addToAtlas(GrBatchTextStrike*, GrBatchAtlas::AtlasID*, GrBatchTarget*, | |
| 107 GrMaskFormat, int width, int height, const void* image, | |
| 108 SkIPoint16* loc); | |
| 109 | |
| 110 // Some clients may wish to verify the integrity of the texture backing stor e of the | |
| 111 // GrBatchAtlas. The atlasGeneration returned below is a monitonically incr easing number which | |
| 112 // changes everytime something is removed from the texture backing store. | |
| 113 uint32_t atlasGeneration(GrMaskFormat) const; | |
| 114 | |
| 115 void freeAll(); | |
| 116 | |
| 117 inline void makeMRU(GrBatchTextStrike* strike) { | |
| 118 if (fStrikeList.head() == strike) { | |
|
Jvsquare
2015/03/27 15:07:29
I'm not sure if there's any reason to maintain MRU
joshualitt
2015/03/30 14:56:43
Acknowledged.
| |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 fStrikeList.remove(strike); | |
| 123 fStrikeList.addToHead(strike); | |
| 124 } | |
| 125 | |
| 126 GrTexture* getTexture(GrMaskFormat); | |
| 127 GrPixelConfig getPixelConfig(GrMaskFormat) const; | |
| 128 | |
| 129 SkDEBUGCODE(void validate() const;) | |
| 130 | |
| 131 void dump() const; | |
| 132 | |
| 133 private: | |
| 134 // There is a 1:1 mapping between GrMaskFormats and atlas indices | |
| 135 static int MaskFormatToAtlasIndex(GrMaskFormat); | |
| 136 static GrMaskFormat AtlasIndexToMaskFormat(int atlasIndex); | |
| 137 | |
| 138 GrBatchTextStrike* generateStrike(GrFontScaler*); | |
| 139 | |
| 140 void purgeStrike(GrBatchTextStrike* strike); | |
| 141 inline GrBatchAtlas* getAtlas(GrMaskFormat) const; | |
| 142 | |
| 143 static void HandleEviction(GrBatchAtlas::AtlasID, void*); | |
| 144 | |
| 145 typedef SkTInternalLList<GrBatchTextStrike> GrBatchStrikeList; | |
| 146 | |
| 147 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey> fCache; | |
| 148 GrBatchStrikeList fStrikeList; | |
| 149 | |
| 150 GrBatchAtlas* fAtlases[kMaskFormatCount]; | |
| 151 GrBatchTextStrike* fPreserveStrike; | |
| 152 }; | |
| 153 | |
| 154 #endif | |
| OLD | NEW |