blob: 466e448de7ceecd8c16adb4f97f33fabe0b71f6f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTextStrike_impl_DEFINED
12#define GrTextStrike_impl_DEFINED
13
14class GrFontCache::Key {
15public:
16 Key(GrFontScaler* scaler) {
17 fFontScalerKey = scaler->getKey();
18 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
reed@google.comac10a2d2010-12-22 21:39:39 +000020 uint32_t getHash() const { return fFontScalerKey->getHash(); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022 static bool LT(const GrTextStrike& strike, const Key& key) {
23 return *strike.getFontScalerKey() < *key.fFontScalerKey;
24 }
25 static bool EQ(const GrTextStrike& strike, const Key& key) {
26 return *strike.getFontScalerKey() == *key.fFontScalerKey;
27 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029private:
30 const GrKey* fFontScalerKey;
31};
32
33void GrFontCache::detachStrikeFromList(GrTextStrike* strike) {
34 if (strike->fPrev) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000035 SkASSERT(fHead != strike);
reed@google.comac10a2d2010-12-22 21:39:39 +000036 strike->fPrev->fNext = strike->fNext;
37 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000038 SkASSERT(fHead == strike);
reed@google.comac10a2d2010-12-22 21:39:39 +000039 fHead = strike->fNext;
40 }
41
42 if (strike->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000043 SkASSERT(fTail != strike);
reed@google.comac10a2d2010-12-22 21:39:39 +000044 strike->fNext->fPrev = strike->fPrev;
45 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000046 SkASSERT(fTail == strike);
reed@google.comac10a2d2010-12-22 21:39:39 +000047 fTail = strike->fPrev;
48 }
49}
50
51GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler) {
52 this->validate();
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
reed@google.comac10a2d2010-12-22 21:39:39 +000054 Key key(scaler);
55 GrTextStrike* strike = fCache.find(key);
56 if (NULL == strike) {
57 strike = this->generateStrike(scaler, key);
58 } else if (strike->fPrev) {
59 // Need to put the strike at the head of its dllist, since that is how
60 // we age the strikes for purging (we purge from the back of the list
61 this->detachStrikeFromList(strike);
62 // attach at the head
63 fHead->fPrev = strike;
64 strike->fNext = fHead;
65 strike->fPrev = NULL;
66 fHead = strike;
67 }
68
69 this->validate();
70 return strike;
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
75/**
76 * This Key just wraps a glyphID, and matches the protocol need for
77 * GrTHashTable
78 */
79class GrTextStrike::Key {
80public:
81 Key(GrGlyph::PackedID id) : fPackedID(id) {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000082
reed@google.comac10a2d2010-12-22 21:39:39 +000083 uint32_t getHash() const { return fPackedID; }
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
reed@google.comac10a2d2010-12-22 21:39:39 +000085 static bool LT(const GrGlyph& glyph, const Key& key) {
86 return glyph.fPackedID < key.fPackedID;
87 }
88 static bool EQ(const GrGlyph& glyph, const Key& key) {
89 return glyph.fPackedID == key.fPackedID;
90 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
reed@google.comac10a2d2010-12-22 21:39:39 +000092private:
93 GrGlyph::PackedID fPackedID;
94};
95
96GrGlyph* GrTextStrike::getGlyph(GrGlyph::PackedID packed,
97 GrFontScaler* scaler) {
98 GrGlyph* glyph = fCache.find(packed);
99 if (NULL == glyph) {
100 glyph = this->generateGlyph(packed, scaler);
101 }
102 return glyph;
103}
104
105#endif