epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrTextStrike_DEFINED |
| 12 | #define GrTextStrike_DEFINED |
| 13 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 14 | #include "GrAtlas.h" |
joshualitt | 947556f | 2014-11-21 09:03:45 -0800 | [diff] [blame] | 15 | #include "GrDrawTarget.h" |
| 16 | #include "GrFontScaler.h" |
| 17 | #include "GrGlyph.h" |
| 18 | #include "SkTDynamicHash.h" |
| 19 | #include "SkVarAlloc.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | class GrFontCache; |
| 22 | class GrGpu; |
| 23 | class GrFontPurgeListener; |
| 24 | |
| 25 | /** |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 26 | * The textstrike maps a hostfontscaler instance to a dictionary of |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | * glyphid->strike |
| 28 | */ |
| 29 | class GrTextStrike { |
| 30 | public: |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 31 | GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | ~GrTextStrike(); |
| 33 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 34 | const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | GrFontCache* getFontCache() const { return fFontCache; } |
| 36 | |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame^] | 37 | inline GrGlyph* getGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler) { |
| 38 | GrGlyph* glyph = fCache.find(packed); |
| 39 | if (NULL == glyph) { |
| 40 | glyph = this->generateGlyph(packed, scaler); |
| 41 | } |
| 42 | return glyph; |
| 43 | } |
| 44 | |
jvanverth | 681e65b | 2014-09-19 13:07:38 -0700 | [diff] [blame] | 45 | // returns true if glyph (or glyph+padding for distance field) |
| 46 | // is too large to ever fit in texture atlas subregions (GrPlots) |
| 47 | bool glyphTooLargeForAtlas(GrGlyph*); |
| 48 | // returns true if glyph successfully added to texture atlas, false otherwise |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 49 | bool addGlyphToAtlas(GrGlyph*, GrFontScaler*); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | |
| 51 | // testing |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 52 | int countGlyphs() const { return fCache.count(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 54 | // remove any references to this plot |
| 55 | void removePlot(const GrPlot* plot); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 56 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 57 | static const GrFontDescKey& GetKey(const GrTextStrike& ts) { |
| 58 | return *(ts.fFontScalerKey); |
| 59 | } |
| 60 | static uint32_t Hash(const GrFontDescKey& key) { |
| 61 | return key.getHash(); |
| 62 | } |
| 63 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | public: |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 65 | // for easy removal from list |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | GrTextStrike* fPrev; |
| 67 | GrTextStrike* fNext; |
| 68 | |
| 69 | private: |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 70 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 71 | const GrFontDescKey* fFontScalerKey; |
joshualitt | 947556f | 2014-11-21 09:03:45 -0800 | [diff] [blame] | 72 | SkVarAlloc fPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | |
| 74 | GrFontCache* fFontCache; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 75 | bool fUseDistanceField; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 76 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 77 | GrAtlas::ClientPlotUsage fPlotUsage; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 78 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | |
| 81 | friend class GrFontCache; |
| 82 | }; |
| 83 | |
| 84 | class GrFontCache { |
| 85 | public: |
| 86 | GrFontCache(GrGpu*); |
| 87 | ~GrFontCache(); |
| 88 | |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame^] | 89 | inline GrTextStrike* getStrike(GrFontScaler* scaler, bool useDistanceField) { |
| 90 | this->validate(); |
| 91 | |
| 92 | GrTextStrike* strike = fCache.find(*(scaler->getKey())); |
| 93 | if (NULL == strike) { |
| 94 | strike = this->generateStrike(scaler); |
| 95 | } else if (strike->fPrev) { |
| 96 | // Need to put the strike at the head of its dllist, since that is how |
| 97 | // we age the strikes for purging (we purge from the back of the list) |
| 98 | this->detachStrikeFromList(strike); |
| 99 | // attach at the head |
| 100 | fHead->fPrev = strike; |
| 101 | strike->fNext = fHead; |
| 102 | strike->fPrev = NULL; |
| 103 | fHead = strike; |
| 104 | } |
| 105 | strike->fUseDistanceField = useDistanceField; |
| 106 | this->validate(); |
| 107 | return strike; |
| 108 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 110 | // add to texture atlas that matches this format |
| 111 | GrPlot* addToAtlas(GrMaskFormat format, GrAtlas::ClientPlotUsage* usage, |
| 112 | int width, int height, const void* image, |
| 113 | SkIPoint16* loc); |
| 114 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 115 | void freeAll(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 117 | // make an unused plot available for this glyph |
| 118 | bool freeUnusedPlot(GrTextStrike* preserveStrike, const GrGlyph* glyph); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 119 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | // testing |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 121 | int countStrikes() const { return fCache.count(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | GrTextStrike* getHeadStrike() const { return fHead; } |
| 123 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 124 | void updateTextures() { |
| 125 | for (int i = 0; i < kAtlasCount; ++i) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 126 | if (fAtlases[i]) { |
| 127 | fAtlases[i]->uploadPlotsToTexture(); |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 132 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | void validate() const; |
| 134 | #else |
| 135 | void validate() const {} |
| 136 | #endif |
| 137 | |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 138 | void dump() const; |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 139 | |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 140 | enum AtlasType { |
| 141 | kA8_AtlasType, //!< 1-byte per pixel |
| 142 | k565_AtlasType, //!< 2-bytes per pixel |
| 143 | k8888_AtlasType, //!< 4-bytes per pixel |
| 144 | |
| 145 | kLast_AtlasType = k8888_AtlasType |
| 146 | }; |
| 147 | static const int kAtlasCount = kLast_AtlasType + 1; |
| 148 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | private: |
| 150 | friend class GrFontPurgeListener; |
| 151 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 152 | SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | // for LRU |
| 154 | GrTextStrike* fHead; |
| 155 | GrTextStrike* fTail; |
| 156 | |
| 157 | GrGpu* fGpu; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 158 | GrAtlas* fAtlases[kAtlasCount]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 160 | GrTextStrike* generateStrike(GrFontScaler*); |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame^] | 161 | |
| 162 | inline void detachStrikeFromList(GrTextStrike* strike) { |
| 163 | if (strike->fPrev) { |
| 164 | SkASSERT(fHead != strike); |
| 165 | strike->fPrev->fNext = strike->fNext; |
| 166 | } else { |
| 167 | SkASSERT(fHead == strike); |
| 168 | fHead = strike->fNext; |
| 169 | } |
| 170 | |
| 171 | if (strike->fNext) { |
| 172 | SkASSERT(fTail != strike); |
| 173 | strike->fNext->fPrev = strike->fPrev; |
| 174 | } else { |
| 175 | SkASSERT(fTail == strike); |
| 176 | fTail = strike->fPrev; |
| 177 | } |
| 178 | } |
| 179 | |
commit-bot@chromium.org | b2e9fa5 | 2013-10-27 20:50:23 +0000 | [diff] [blame] | 180 | void purgeStrike(GrTextStrike* strike); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | #endif |