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 | |
| 14 | #include "GrAllocPool.h" |
| 15 | #include "GrFontScaler.h" |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 16 | #include "SkTDynamicHash.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #include "GrGlyph.h" |
commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 18 | #include "GrDrawTarget.h" |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 19 | #include "GrAtlas.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 | /** |
| 26 | * The textcache maps a hostfontscaler instance to a dictionary of |
| 27 | * glyphid->strike |
| 28 | */ |
| 29 | class GrTextStrike { |
| 30 | public: |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 31 | GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey, GrMaskFormat, GrAtlas*); |
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; } |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 36 | GrMaskFormat getMaskFormat() const { return fMaskFormat; } |
jvanverth | f17bc6c | 2014-07-25 16:46:53 -0700 | [diff] [blame] | 37 | GrTexture* getTexture() const { return fAtlas->getTexture(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | |
| 39 | inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*); |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 40 | bool addGlyphToAtlas(GrGlyph*, GrFontScaler*); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | |
| 42 | // testing |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 43 | int countGlyphs() const { return fCache.count(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 45 | // remove any references to this plot |
| 46 | void removePlot(const GrPlot* plot); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 47 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 48 | static const GrFontDescKey& GetKey(const GrTextStrike& ts) { |
| 49 | return *(ts.fFontScalerKey); |
| 50 | } |
| 51 | static uint32_t Hash(const GrFontDescKey& key) { |
| 52 | return key.getHash(); |
| 53 | } |
| 54 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | public: |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 56 | // for easy removal from list |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | GrTextStrike* fPrev; |
| 58 | GrTextStrike* fNext; |
| 59 | |
| 60 | private: |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 61 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 62 | const GrFontDescKey* fFontScalerKey; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | GrTAllocPool<GrGlyph> fPool; |
| 64 | |
| 65 | GrFontCache* fFontCache; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 66 | GrAtlas* fAtlas; |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 67 | GrMaskFormat fMaskFormat; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 68 | bool fUseDistanceField; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 69 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 70 | GrAtlas::ClientPlotUsage fPlotUsage; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 71 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | |
| 74 | friend class GrFontCache; |
| 75 | }; |
| 76 | |
| 77 | class GrFontCache { |
| 78 | public: |
| 79 | GrFontCache(GrGpu*); |
| 80 | ~GrFontCache(); |
| 81 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 82 | inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | |
| 84 | void freeAll(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 86 | // make an unused plot available |
| 87 | bool freeUnusedPlot(GrTextStrike* preserveStrike); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 88 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | // testing |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 90 | int countStrikes() const { return fCache.count(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | GrTextStrike* getHeadStrike() const { return fHead; } |
| 92 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 93 | void updateTextures() { |
| 94 | for (int i = 0; i < kAtlasCount; ++i) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 95 | if (fAtlases[i]) { |
| 96 | fAtlases[i]->uploadPlotsToTexture(); |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 101 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | void validate() const; |
| 103 | #else |
| 104 | void validate() const {} |
| 105 | #endif |
| 106 | |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 107 | void dump() const; |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 109 | enum AtlasType { |
| 110 | kA8_AtlasType, //!< 1-byte per pixel |
| 111 | k565_AtlasType, //!< 2-bytes per pixel |
| 112 | k8888_AtlasType, //!< 4-bytes per pixel |
| 113 | |
| 114 | kLast_AtlasType = k8888_AtlasType |
| 115 | }; |
| 116 | static const int kAtlasCount = kLast_AtlasType + 1; |
| 117 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 118 | private: |
| 119 | friend class GrFontPurgeListener; |
| 120 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 121 | SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | // for LRU |
| 123 | GrTextStrike* fHead; |
| 124 | GrTextStrike* fTail; |
| 125 | |
| 126 | GrGpu* fGpu; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 127 | GrAtlas* fAtlases[kAtlasCount]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 129 | GrTextStrike* generateStrike(GrFontScaler*); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | inline void detachStrikeFromList(GrTextStrike*); |
commit-bot@chromium.org | b2e9fa5 | 2013-10-27 20:50:23 +0000 | [diff] [blame] | 131 | void purgeStrike(GrTextStrike* strike); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #endif |