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