OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 | 10 |
11 #include "SkTypefaceCache.h" | 11 #include "SkTypefaceCache.h" |
12 #include "SkThread.h" | 12 #include "SkThread.h" |
13 | 13 |
14 #define TYPEFACE_CACHE_LIMIT 1024 | 14 #define TYPEFACE_CACHE_LIMIT 1024 |
15 | 15 |
16 SkTypefaceCache::SkTypefaceCache() {} | 16 SkTypefaceCache::SkTypefaceCache() {} |
17 | 17 |
18 SkTypefaceCache::~SkTypefaceCache() { | 18 SkTypefaceCache::~SkTypefaceCache() { |
19 const Rec* curr = fArray.begin(); | 19 const Rec* curr = fArray.begin(); |
20 const Rec* stop = fArray.end(); | 20 const Rec* stop = fArray.end(); |
21 while (curr < stop) { | 21 while (curr < stop) { |
22 if (curr->fStrong) { | 22 curr->fFace->unref(); |
23 curr->fFace->unref(); | |
24 } else { | |
25 curr->fFace->weak_unref(); | |
26 } | |
27 curr += 1; | 23 curr += 1; |
28 } | 24 } |
29 } | 25 } |
30 | 26 |
31 void SkTypefaceCache::add(SkTypeface* face, | 27 void SkTypefaceCache::add(SkTypeface* face, const SkFontStyle& requestedStyle) { |
32 const SkFontStyle& requestedStyle, | |
33 bool strong) { | |
34 if (fArray.count() >= TYPEFACE_CACHE_LIMIT) { | 28 if (fArray.count() >= TYPEFACE_CACHE_LIMIT) { |
35 this->purge(TYPEFACE_CACHE_LIMIT >> 2); | 29 this->purge(TYPEFACE_CACHE_LIMIT >> 2); |
36 } | 30 } |
37 | 31 |
38 Rec* rec = fArray.append(); | 32 Rec* rec = fArray.append(); |
39 rec->fFace = face; | 33 rec->fFace = SkRef(face); |
40 rec->fRequestedStyle = requestedStyle; | 34 rec->fRequestedStyle = requestedStyle; |
41 rec->fStrong = strong; | |
42 if (strong) { | |
43 face->ref(); | |
44 } else { | |
45 face->weak_ref(); | |
46 } | |
47 } | 35 } |
48 | 36 |
49 SkTypeface* SkTypefaceCache::findByID(SkFontID fontID) const { | 37 SkTypeface* SkTypefaceCache::findByID(SkFontID fontID) const { |
50 const Rec* curr = fArray.begin(); | 38 const Rec* curr = fArray.begin(); |
51 const Rec* stop = fArray.end(); | 39 const Rec* stop = fArray.end(); |
52 while (curr < stop) { | 40 while (curr < stop) { |
53 if (curr->fFace->uniqueID() == fontID) { | 41 if (curr->fFace->uniqueID() == fontID) { |
54 return curr->fFace; | 42 return curr->fFace; |
55 } | 43 } |
56 curr += 1; | 44 curr += 1; |
57 } | 45 } |
58 return NULL; | 46 return NULL; |
59 } | 47 } |
60 | 48 |
61 SkTypeface* SkTypefaceCache::findByProcAndRef(FindProc proc, void* ctx) const { | 49 SkTypeface* SkTypefaceCache::findByProcAndRef(FindProc proc, void* ctx) const { |
62 const Rec* curr = fArray.begin(); | 50 const Rec* curr = fArray.begin(); |
63 const Rec* stop = fArray.end(); | 51 const Rec* stop = fArray.end(); |
64 while (curr < stop) { | 52 while (curr < stop) { |
65 SkTypeface* currFace = curr->fFace; | 53 SkTypeface* currFace = curr->fFace; |
66 if (proc(currFace, curr->fRequestedStyle, ctx)) { | 54 if (proc(currFace, curr->fRequestedStyle, ctx)) { |
67 if (curr->fStrong) { | 55 return SkRef(currFace); |
68 currFace->ref(); | |
69 return currFace; | |
70 } else if (currFace->try_ref()) { | |
71 return currFace; | |
72 } else { | |
73 //remove currFace from fArray? | |
74 } | |
75 } | 56 } |
76 curr += 1; | 57 curr += 1; |
77 } | 58 } |
78 return NULL; | 59 return NULL; |
79 } | 60 } |
80 | 61 |
81 void SkTypefaceCache::purge(int numToPurge) { | 62 void SkTypefaceCache::purge(int numToPurge) { |
82 int count = fArray.count(); | 63 int count = fArray.count(); |
83 int i = 0; | 64 int i = 0; |
84 while (i < count) { | 65 while (i < count) { |
85 SkTypeface* face = fArray[i].fFace; | 66 SkTypeface* face = fArray[i].fFace; |
86 bool strong = fArray[i].fStrong; | 67 if (face->unique()) { |
87 if ((strong && face->unique()) || (!strong && face->weak_expired())) { | 68 face->unref(); |
88 if (strong) { | |
89 face->unref(); | |
90 } else { | |
91 face->weak_unref(); | |
92 } | |
93 fArray.remove(i); | 69 fArray.remove(i); |
94 --count; | 70 --count; |
95 if (--numToPurge == 0) { | 71 if (--numToPurge == 0) { |
96 return; | 72 return; |
97 } | 73 } |
98 } else { | 74 } else { |
99 ++i; | 75 ++i; |
100 } | 76 } |
101 } | 77 } |
102 } | 78 } |
103 | 79 |
104 void SkTypefaceCache::purgeAll() { | 80 void SkTypefaceCache::purgeAll() { |
105 this->purge(fArray.count()); | 81 this->purge(fArray.count()); |
106 } | 82 } |
107 | 83 |
108 /////////////////////////////////////////////////////////////////////////////// | 84 /////////////////////////////////////////////////////////////////////////////// |
109 | 85 |
110 SkTypefaceCache& SkTypefaceCache::Get() { | 86 SkTypefaceCache& SkTypefaceCache::Get() { |
111 static SkTypefaceCache gCache; | 87 static SkTypefaceCache gCache; |
112 return gCache; | 88 return gCache; |
113 } | 89 } |
114 | 90 |
115 SkFontID SkTypefaceCache::NewFontID() { | 91 SkFontID SkTypefaceCache::NewFontID() { |
116 static int32_t gFontID; | 92 static int32_t gFontID; |
117 return sk_atomic_inc(&gFontID) + 1; | 93 return sk_atomic_inc(&gFontID) + 1; |
118 } | 94 } |
119 | 95 |
120 SK_DECLARE_STATIC_MUTEX(gMutex); | 96 SK_DECLARE_STATIC_MUTEX(gMutex); |
121 | 97 |
122 void SkTypefaceCache::Add(SkTypeface* face, | 98 void SkTypefaceCache::Add(SkTypeface* face, const SkFontStyle& requestedStyle) { |
123 const SkFontStyle& requestedStyle, | |
124 bool strong) { | |
125 SkAutoMutexAcquire ama(gMutex); | 99 SkAutoMutexAcquire ama(gMutex); |
126 Get().add(face, requestedStyle, strong); | 100 Get().add(face, requestedStyle); |
127 } | 101 } |
128 | 102 |
129 SkTypeface* SkTypefaceCache::FindByID(SkFontID fontID) { | 103 SkTypeface* SkTypefaceCache::FindByID(SkFontID fontID) { |
130 SkAutoMutexAcquire ama(gMutex); | 104 SkAutoMutexAcquire ama(gMutex); |
131 return Get().findByID(fontID); | 105 return Get().findByID(fontID); |
132 } | 106 } |
133 | 107 |
134 SkTypeface* SkTypefaceCache::FindByProcAndRef(FindProc proc, void* ctx) { | 108 SkTypeface* SkTypefaceCache::FindByProcAndRef(FindProc proc, void* ctx) { |
135 SkAutoMutexAcquire ama(gMutex); | 109 SkAutoMutexAcquire ama(gMutex); |
136 SkTypeface* typeface = Get().findByProcAndRef(proc, ctx); | 110 SkTypeface* typeface = Get().findByProcAndRef(proc, ctx); |
(...skipping 14 matching lines...) Expand all Loading... |
151 return false; | 125 return false; |
152 } | 126 } |
153 #endif | 127 #endif |
154 | 128 |
155 void SkTypefaceCache::Dump() { | 129 void SkTypefaceCache::Dump() { |
156 #ifdef SK_DEBUG | 130 #ifdef SK_DEBUG |
157 SkAutoMutexAcquire ama(gMutex); | 131 SkAutoMutexAcquire ama(gMutex); |
158 (void)Get().findByProcAndRef(DumpProc, NULL); | 132 (void)Get().findByProcAndRef(DumpProc, NULL); |
159 #endif | 133 #endif |
160 } | 134 } |
OLD | NEW |