OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 | |
10 #ifndef SkGlyphCache_DEFINED | 8 #ifndef SkGlyphCache_DEFINED |
11 #define SkGlyphCache_DEFINED | 9 #define SkGlyphCache_DEFINED |
12 | 10 |
13 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
| 12 #include "SkChecksum.h" |
14 #include "SkChunkAlloc.h" | 13 #include "SkChunkAlloc.h" |
15 #include "SkDescriptor.h" | 14 #include "SkDescriptor.h" |
16 #include "SkGlyph.h" | 15 #include "SkGlyph.h" |
17 #include "SkScalerContext.h" | 16 #include "SkScalerContext.h" |
18 #include "SkTemplates.h" | 17 #include "SkTemplates.h" |
19 #include "SkTDArray.h" | 18 #include "SkTDArray.h" |
20 | 19 |
21 struct SkDeviceProperties; | 20 struct SkDeviceProperties; |
22 class SkPaint; | 21 class SkPaint; |
23 | 22 |
24 class SkGlyphCache_Globals; | 23 class SkGlyphCache_Globals; |
25 | 24 |
| 25 // Enable this locally to add stats for hash-table hit rates. It also extends th
e dump() |
| 26 // output to show those stats. |
| 27 //#define SK_GLYPHCACHE_TRACK_HASH_STATS |
| 28 |
26 /** \class SkGlyphCache | 29 /** \class SkGlyphCache |
27 | 30 |
28 This class represents a strike: a specific combination of typeface, size, | 31 This class represents a strike: a specific combination of typeface, size, |
29 matrix, etc., and holds the glyphs for that strike. Calling any of the | 32 matrix, etc., and holds the glyphs for that strike. Calling any of the |
30 getUnichar.../getGlyphID... methods will return the requested glyph, | 33 getUnichar.../getGlyphID... methods will return the requested glyph, |
31 either instantly if it is already cached, or by first generating it and then | 34 either instantly if it is already cached, or by first generating it and then |
32 adding it to the strike. | 35 adding it to the strike. |
33 | 36 |
34 The strikes are held in a global list, available to all threads. To interact | 37 The strikes are held in a global list, available to all threads. To interact |
35 with one, call either VisitCache() or DetachCache(). | 38 with one, call either VisitCache() or DetachCache(). |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 const SkDescriptor& getDescriptor() const { return *fDesc; } | 97 const SkDescriptor& getDescriptor() const { return *fDesc; } |
95 | 98 |
96 SkMask::Format getMaskFormat() const { | 99 SkMask::Format getMaskFormat() const { |
97 return fScalerContext->getMaskFormat(); | 100 return fScalerContext->getMaskFormat(); |
98 } | 101 } |
99 | 102 |
100 bool isSubpixel() const { | 103 bool isSubpixel() const { |
101 return fScalerContext->isSubpixel(); | 104 return fScalerContext->isSubpixel(); |
102 } | 105 } |
103 | 106 |
| 107 void dump() const; |
| 108 |
104 /* AuxProc/Data allow a client to associate data with this cache entry. | 109 /* AuxProc/Data allow a client to associate data with this cache entry. |
105 Multiple clients can use this, as their data is keyed with a function | 110 Multiple clients can use this, as their data is keyed with a function |
106 pointer. In addition to serving as a key, the function pointer is called | 111 pointer. In addition to serving as a key, the function pointer is called |
107 with the data when the glyphcache object is deleted, so the client can | 112 with the data when the glyphcache object is deleted, so the client can |
108 cleanup their data as well. NOTE: the auxProc must not try to access | 113 cleanup their data as well. NOTE: the auxProc must not try to access |
109 this glyphcache in any way, since it may be in the process of being | 114 this glyphcache in any way, since it may be in the process of being |
110 deleted. | 115 deleted. |
111 */ | 116 */ |
112 | 117 |
113 //! If the proc is found, return true and set *dataPtr to its data | 118 //! If the proc is found, return true and set *dataPtr to its data |
(...skipping 24 matching lines...) Expand all Loading... |
138 a different strike will be generated. This is fine. It does mean we | 143 a different strike will be generated. This is fine. It does mean we |
139 can have more than 1 strike for the same descriptor, but that will | 144 can have more than 1 strike for the same descriptor, but that will |
140 eventually get purged, and the win is that different thread will never | 145 eventually get purged, and the win is that different thread will never |
141 block each other while a strike is being used. | 146 block each other while a strike is being used. |
142 */ | 147 */ |
143 static SkGlyphCache* DetachCache(SkTypeface* typeface, | 148 static SkGlyphCache* DetachCache(SkTypeface* typeface, |
144 const SkDescriptor* desc) { | 149 const SkDescriptor* desc) { |
145 return VisitCache(typeface, desc, DetachProc, NULL); | 150 return VisitCache(typeface, desc, DetachProc, NULL); |
146 } | 151 } |
147 | 152 |
| 153 static void Dump(); |
| 154 |
148 #ifdef SK_DEBUG | 155 #ifdef SK_DEBUG |
149 void validate() const; | 156 void validate() const; |
150 #else | 157 #else |
151 void validate() const {} | 158 void validate() const {} |
152 #endif | 159 #endif |
153 | 160 |
154 class AutoValidate : SkNoncopyable { | 161 class AutoValidate : SkNoncopyable { |
155 public: | 162 public: |
156 AutoValidate(const SkGlyphCache* cache) : fCache(cache) { | 163 AutoValidate(const SkGlyphCache* cache) : fCache(cache) { |
157 if (fCache) { | 164 if (fCache) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 SkTDArray<SkGlyph*> fGlyphArray; | 204 SkTDArray<SkGlyph*> fGlyphArray; |
198 SkChunkAlloc fGlyphAlloc; | 205 SkChunkAlloc fGlyphAlloc; |
199 | 206 |
200 struct CharGlyphRec { | 207 struct CharGlyphRec { |
201 uint32_t fID; // unichar + subpixel | 208 uint32_t fID; // unichar + subpixel |
202 SkGlyph* fGlyph; | 209 SkGlyph* fGlyph; |
203 }; | 210 }; |
204 // no reason to use the same kHashCount as fGlyphHash, but we do for now | 211 // no reason to use the same kHashCount as fGlyphHash, but we do for now |
205 CharGlyphRec fCharToGlyphHash[kHashCount]; | 212 CharGlyphRec fCharToGlyphHash[kHashCount]; |
206 | 213 |
207 static inline unsigned ID2HashIndex(uint32_t id) { | 214 static inline unsigned ID2HashIndex(uint32_t h) { |
208 id ^= id >> 16; | 215 return SkChecksum::CheapMix(h) & kHashMask; |
209 id ^= id >> 8; | |
210 return id & kHashMask; | |
211 } | 216 } |
212 | 217 |
213 // used to track (approx) how much ram is tied-up in this cache | 218 // used to track (approx) how much ram is tied-up in this cache |
214 size_t fMemoryUsed; | 219 size_t fMemoryUsed; |
215 | 220 |
| 221 |
| 222 #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 223 int fHashHitCount; |
| 224 int fHashMissCount; |
| 225 #endif |
| 226 |
216 struct AuxProcRec { | 227 struct AuxProcRec { |
217 AuxProcRec* fNext; | 228 AuxProcRec* fNext; |
218 void (*fProc)(void*); | 229 void (*fProc)(void*); |
219 void* fData; | 230 void* fData; |
220 }; | 231 }; |
221 AuxProcRec* fAuxProcList; | 232 AuxProcRec* fAuxProcList; |
222 void invokeAndRemoveAuxProcs(); | 233 void invokeAndRemoveAuxProcs(); |
223 | 234 |
224 inline static SkGlyphCache* FindTail(SkGlyphCache* head); | 235 inline static SkGlyphCache* FindTail(SkGlyphCache* head); |
225 | 236 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 const SkMatrix* matrix) { | 301 const SkMatrix* matrix) { |
291 fCache = paint.detachCache(deviceProperties, matrix, true); | 302 fCache = paint.detachCache(deviceProperties, matrix, true); |
292 } | 303 } |
293 | 304 |
294 private: | 305 private: |
295 SkAutoGlyphCacheNoGamma() : SkAutoGlyphCacheBase() {} | 306 SkAutoGlyphCacheNoGamma() : SkAutoGlyphCacheBase() {} |
296 }; | 307 }; |
297 #define SkAutoGlyphCacheNoGamma(...) SK_REQUIRE_LOCAL_VAR(SkAutoGlyphCacheNoGamm
a) | 308 #define SkAutoGlyphCacheNoGamma(...) SK_REQUIRE_LOCAL_VAR(SkAutoGlyphCacheNoGamm
a) |
298 | 309 |
299 #endif | 310 #endif |
OLD | NEW |