blob: 0691eaa6432b9c4307489430666c6fbe01d3ec74 [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:
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000016 explicit Key(const GrKey* fontScalarKey) {
17 fFontScalerKey = fontScalarKey;
reed@google.comac10a2d2010-12-22 21:39:39 +000018 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
robertphillips@google.com8b169312013-10-15 17:47:36 +000020 intptr_t getHash() const { return fFontScalerKey->getHash(); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
rmistry@google.comd6bab022013-12-02 13:50:38 +000022 static bool LessThan(const GrTextStrike& strike, const Key& key) {
reed@google.comac10a2d2010-12-22 21:39:39 +000023 return *strike.getFontScalerKey() < *key.fFontScalerKey;
24 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000025 static bool Equals(const GrTextStrike& strike, const Key& key) {
reed@google.comac10a2d2010-12-22 21:39:39 +000026 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
jvanverth@google.comd830d132013-11-11 20:54:09 +000051#if SK_DISTANCEFIELD_FONTS
52GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler, bool useDistanceField) {
53#else
reed@google.comac10a2d2010-12-22 21:39:39 +000054GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler) {
jvanverth@google.comd830d132013-11-11 20:54:09 +000055#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000056 this->validate();
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000058 const Key key(scaler->getKey());
reed@google.comac10a2d2010-12-22 21:39:39 +000059 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.comd830d132013-11-11 20:54:09 +000072#if SK_DISTANCEFIELD_FONTS
73 strike->fUseDistanceField = useDistanceField;
74#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000075 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 */
85class GrTextStrike::Key {
86public:
87 Key(GrGlyph::PackedID id) : fPackedID(id) {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@google.comac10a2d2010-12-22 21:39:39 +000089 uint32_t getHash() const { return fPackedID; }
rmistry@google.comd6176b02012-08-23 18:14:13 +000090
rmistry@google.comd6bab022013-12-02 13:50:38 +000091 static bool LessThan(const GrGlyph& glyph, const Key& key) {
reed@google.comac10a2d2010-12-22 21:39:39 +000092 return glyph.fPackedID < key.fPackedID;
93 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000094 static bool Equals(const GrGlyph& glyph, const Key& key) {
reed@google.comac10a2d2010-12-22 21:39:39 +000095 return glyph.fPackedID == key.fPackedID;
96 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
reed@google.comac10a2d2010-12-22 21:39:39 +000098private:
99 GrGlyph::PackedID fPackedID;
100};
101
102GrGlyph* 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