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