• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright 2010 Google Inc.
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8          http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16 
17 
18 #ifndef GrTextStrike_impl_DEFINED
19 #define GrTextStrike_impl_DEFINED
20 
21 class GrFontCache::Key {
22 public:
Key(GrFontScaler * scaler)23     Key(GrFontScaler* scaler) {
24         fFontScalerKey = scaler->getKey();
25     }
26 
getHash()27     uint32_t getHash() const { return fFontScalerKey->getHash(); }
28 
LT(const GrTextStrike & strike,const Key & key)29     static bool LT(const GrTextStrike& strike, const Key& key) {
30         return *strike.getFontScalerKey() < *key.fFontScalerKey;
31     }
EQ(const GrTextStrike & strike,const Key & key)32     static bool EQ(const GrTextStrike& strike, const Key& key) {
33         return *strike.getFontScalerKey() == *key.fFontScalerKey;
34     }
35 
36 private:
37     const GrKey* fFontScalerKey;
38 };
39 
detachStrikeFromList(GrTextStrike * strike)40 void GrFontCache::detachStrikeFromList(GrTextStrike* strike) {
41     if (strike->fPrev) {
42         GrAssert(fHead != strike);
43         strike->fPrev->fNext = strike->fNext;
44     } else {
45         GrAssert(fHead == strike);
46         fHead = strike->fNext;
47     }
48 
49     if (strike->fNext) {
50         GrAssert(fTail != strike);
51         strike->fNext->fPrev = strike->fPrev;
52     } else {
53         GrAssert(fTail == strike);
54         fTail = strike->fPrev;
55     }
56 }
57 
getStrike(GrFontScaler * scaler)58 GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler) {
59     this->validate();
60 
61     Key key(scaler);
62     GrTextStrike* strike = fCache.find(key);
63     if (NULL == strike) {
64         strike = this->generateStrike(scaler, key);
65     } else if (strike->fPrev) {
66         // Need to put the strike at the head of its dllist, since that is how
67         // we age the strikes for purging (we purge from the back of the list
68         this->detachStrikeFromList(strike);
69         // attach at the head
70         fHead->fPrev = strike;
71         strike->fNext = fHead;
72         strike->fPrev = NULL;
73         fHead = strike;
74     }
75 
76     this->validate();
77     return strike;
78 }
79 
80 ///////////////////////////////////////////////////////////////////////////////
81 
82 /**
83  *  This Key just wraps a glyphID, and matches the protocol need for
84  *  GrTHashTable
85  */
86 class GrTextStrike::Key {
87 public:
Key(GrGlyph::PackedID id)88     Key(GrGlyph::PackedID id) : fPackedID(id) {}
89 
getHash()90     uint32_t getHash() const { return fPackedID; }
91 
LT(const GrGlyph & glyph,const Key & key)92     static bool LT(const GrGlyph& glyph, const Key& key) {
93         return glyph.fPackedID < key.fPackedID;
94     }
EQ(const GrGlyph & glyph,const Key & key)95     static bool EQ(const GrGlyph& glyph, const Key& key) {
96         return glyph.fPackedID == key.fPackedID;
97     }
98 
99 private:
100     GrGlyph::PackedID fPackedID;
101 };
102 
getGlyph(GrGlyph::PackedID packed,GrFontScaler * scaler)103 GrGlyph* GrTextStrike::getGlyph(GrGlyph::PackedID packed,
104                                 GrFontScaler* scaler) {
105     GrGlyph* glyph = fCache.find(packed);
106     if (NULL == glyph) {
107         glyph = this->generateGlyph(packed, scaler);
108     }
109     return glyph;
110 }
111 
112 #endif
113 
114