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_impl_DEFINED |
| 12 | #define GrTextStrike_impl_DEFINED |
| 13 | |
| 14 | class GrFontCache::Key { |
| 15 | public: |
commit-bot@chromium.org | b2e9fa5 | 2013-10-27 20:50:23 +0000 | [diff] [blame] | 16 | explicit Key(const GrKey* fontScalarKey) { |
| 17 | fFontScalerKey = fontScalarKey; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 20 | intptr_t getHash() const { return fFontScalerKey->getHash(); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 22 | static bool LessThan(const GrTextStrike& strike, const Key& key) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | return *strike.getFontScalerKey() < *key.fFontScalerKey; |
| 24 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 25 | static bool Equals(const GrTextStrike& strike, const Key& key) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | return *strike.getFontScalerKey() == *key.fFontScalerKey; |
| 27 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | private: |
| 30 | const GrKey* fFontScalerKey; |
| 31 | }; |
| 32 | |
| 33 | void GrFontCache::detachStrikeFromList(GrTextStrike* strike) { |
| 34 | if (strike->fPrev) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 35 | SkASSERT(fHead != strike); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | strike->fPrev->fNext = strike->fNext; |
| 37 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 38 | SkASSERT(fHead == strike); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | fHead = strike->fNext; |
| 40 | } |
| 41 | |
| 42 | if (strike->fNext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 43 | SkASSERT(fTail != strike); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | strike->fNext->fPrev = strike->fPrev; |
| 45 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 46 | SkASSERT(fTail == strike); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | fTail = strike->fPrev; |
| 48 | } |
| 49 | } |
| 50 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 51 | #if SK_DISTANCEFIELD_FONTS |
| 52 | GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler, bool useDistanceField) { |
| 53 | #else |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 55 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | this->validate(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | |
commit-bot@chromium.org | b2e9fa5 | 2013-10-27 20:50:23 +0000 | [diff] [blame] | 58 | const Key key(scaler->getKey()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | GrTextStrike* strike = fCache.find(key); |
| 60 | if (NULL == strike) { |
| 61 | strike = this->generateStrike(scaler, key); |
| 62 | } else if (strike->fPrev) { |
| 63 | // Need to put the strike at the head of its dllist, since that is how |
| 64 | // we age the strikes for purging (we purge from the back of the list |
| 65 | this->detachStrikeFromList(strike); |
| 66 | // attach at the head |
| 67 | fHead->fPrev = strike; |
| 68 | strike->fNext = fHead; |
| 69 | strike->fPrev = NULL; |
| 70 | fHead = strike; |
| 71 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 72 | #if SK_DISTANCEFIELD_FONTS |
| 73 | strike->fUseDistanceField = useDistanceField; |
| 74 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | this->validate(); |
| 76 | return strike; |
| 77 | } |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
| 81 | /** |
| 82 | * This Key just wraps a glyphID, and matches the protocol need for |
| 83 | * GrTHashTable |
| 84 | */ |
| 85 | class GrTextStrike::Key { |
| 86 | public: |
| 87 | Key(GrGlyph::PackedID id) : fPackedID(id) {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 88 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | uint32_t getHash() const { return fPackedID; } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 90 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 91 | static bool LessThan(const GrGlyph& glyph, const Key& key) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | return glyph.fPackedID < key.fPackedID; |
| 93 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 94 | static bool Equals(const GrGlyph& glyph, const Key& key) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | return glyph.fPackedID == key.fPackedID; |
| 96 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 97 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | private: |
| 99 | GrGlyph::PackedID fPackedID; |
| 100 | }; |
| 101 | |
| 102 | GrGlyph* GrTextStrike::getGlyph(GrGlyph::PackedID packed, |
| 103 | GrFontScaler* scaler) { |
| 104 | GrGlyph* glyph = fCache.find(packed); |
| 105 | if (NULL == glyph) { |
| 106 | glyph = this->generateGlyph(packed, scaler); |
| 107 | } |
| 108 | return glyph; |
| 109 | } |
| 110 | |
| 111 | #endif |