SkGlyphCache.h revision 66e6cdb5ebbab9f3a695adc963f16b9c8b165afe
1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#ifndef SkGlyphCache_DEFINED 11#define SkGlyphCache_DEFINED 12 13#include "SkBitmap.h" 14#include "SkChunkAlloc.h" 15#include "SkDescriptor.h" 16#include "SkScalerContext.h" 17#include "SkTemplates.h" 18 19class SkPaint; 20 21class SkGlyphCache_Globals; 22 23/** \class SkGlyphCache 24 25 This class represents a strike: a specific combination of typeface, size, 26 matrix, etc., and holds the glyphs for that strike. Calling any of the 27 getUnichar.../getGlyphID... methods will return the requested glyph, 28 either instantly if it is already cahced, or by first generating it and then 29 adding it to the strike. 30 31 The strikes are held in a global list, available to all threads. To interact 32 with one, call either VisitCache() or DetachCache(). 33*/ 34class SkGlyphCache { 35public: 36 /** Returns a glyph with valid fAdvance and fDevKern fields. 37 The remaining fields may be valid, but that is not guaranteed. If you 38 require those, call getUnicharMetrics or getGlyphIDMetrics instead. 39 */ 40 const SkGlyph& getUnicharAdvance(SkUnichar); 41 const SkGlyph& getGlyphIDAdvance(uint16_t); 42 43 /** Returns a glyph with all fields valid except fImage and fPath, which 44 may be null. If they are null, call findImage or findPath for those. 45 If they are not null, then they are valid. 46 47 This call is potentially slower than the matching ...Advance call. If 48 you only need the fAdvance/fDevKern fields, call those instead. 49 */ 50 const SkGlyph& getUnicharMetrics(SkUnichar); 51 const SkGlyph& getGlyphIDMetrics(uint16_t); 52 53 /** These are variants that take the device position of the glyph. Call 54 these only if you are drawing in subpixel mode. Passing 0, 0 is 55 effectively the same as calling the variants w/o the extra params, tho 56 a tiny bit slower. 57 */ 58 const SkGlyph& getUnicharMetrics(SkUnichar, SkFixed x, SkFixed y); 59 const SkGlyph& getGlyphIDMetrics(uint16_t, SkFixed x, SkFixed y); 60 61 /** Return the glyphID for the specified Unichar. If the char has already 62 been seen, use the existing cache entry. If not, ask the scalercontext 63 to compute it for us. 64 */ 65 uint16_t unicharToGlyph(SkUnichar); 66 67 /** Map the glyph to its Unicode equivalent. Unmappable glyphs map to 68 a character code of zero. 69 */ 70 SkUnichar glyphToUnichar(uint16_t); 71 72 /** Returns the number of glyphs for this strike. 73 */ 74 unsigned getGlyphCount(); 75 76 /** Return the image associated with the glyph. If it has not been generated 77 this will trigger that. 78 */ 79 const void* findImage(const SkGlyph&); 80 /** Return the Path associated with the glyph. If it has not been generated 81 this will trigger that. 82 */ 83 const SkPath* findPath(const SkGlyph&); 84 85 /** Return the vertical metrics for this strike. 86 */ 87 const SkPaint::FontMetrics& getFontMetricsY() const { 88 return fFontMetricsY; 89 } 90 91 const SkDescriptor& getDescriptor() const { return *fDesc; } 92 93 SkMask::Format getMaskFormat() const { 94 return fScalerContext->getMaskFormat(); 95 } 96 97 /* AuxProc/Data allow a client to associate data with this cache entry. 98 Multiple clients can use this, as their data is keyed with a function 99 pointer. In addition to serving as a key, the function pointer is called 100 with the data when the glyphcache object is deleted, so the client can 101 cleanup their data as well. NOTE: the auxProc must not try to access 102 this glyphcache in any way, since it may be in the process of being 103 deleted. 104 */ 105 106 //! If the proc is found, return true and set *dataPtr to its data 107 bool getAuxProcData(void (*auxProc)(void*), void** dataPtr) const; 108 //! Add a proc/data pair to the glyphcache. proc should be non-null 109 void setAuxProc(void (*auxProc)(void*), void* auxData); 110 //! If found, remove the proc/data pair from the glyphcache (does not 111 // call the proc) 112 void removeAuxProc(void (*auxProc)(void*)); 113 114 /** Call proc on all cache entries, stopping early if proc returns true. 115 The proc should not create or delete caches, since it could produce 116 deadlock. 117 */ 118 static void VisitAllCaches(bool (*proc)(SkGlyphCache*, void*), void* ctx); 119 120 /** Find a matching cache entry, and call proc() with it. If none is found 121 create a new one. If the proc() returns true, detach the cache and 122 return it, otherwise leave it and return NULL. 123 */ 124 static SkGlyphCache* VisitCache(const SkDescriptor* desc, 125 bool (*proc)(const SkGlyphCache*, void*), 126 void* context); 127 128 /** Given a strike that was returned by either VisitCache() or DetachCache() 129 add it back into the global cache list (after which the caller should 130 not reference it anymore. 131 */ 132 static void AttachCache(SkGlyphCache*); 133 134 /** Detach a strike from the global cache matching the specified descriptor. 135 Once detached, it can be queried/modified by the current thread, and 136 when finished, be reattached to the global cache with AttachCache(). 137 While detached, if another request is made with the same descriptor, 138 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 140 eventually get purged, and the win is that different thread will never 141 block each other while a strike is being used. 142 */ 143 static SkGlyphCache* DetachCache(const SkDescriptor* desc) { 144 return VisitCache(desc, DetachProc, NULL); 145 } 146 147 /** Return the approximate number of bytes used by the font cache 148 */ 149 static size_t GetCacheUsed(); 150 151 /** This can be called to purge old font data, in an attempt to free 152 enough bytes such that the font cache is not using more than the 153 specified number of bytes. It is thread-safe, and may be called at 154 any time. 155 Return true if some amount of the cache was purged. 156 */ 157 static bool SetCacheUsed(size_t bytesUsed); 158 159#ifdef SK_DEBUG 160 void validate() const; 161#else 162 void validate() const {} 163#endif 164 165 class AutoValidate : SkNoncopyable { 166 public: 167 AutoValidate(const SkGlyphCache* cache) : fCache(cache) { 168 if (fCache) { 169 fCache->validate(); 170 } 171 } 172 ~AutoValidate() { 173 if (fCache) { 174 fCache->validate(); 175 } 176 } 177 void forget() { 178 fCache = NULL; 179 } 180 private: 181 const SkGlyphCache* fCache; 182 }; 183 184private: 185 SkGlyphCache(const SkDescriptor*); 186 ~SkGlyphCache(); 187 188 enum MetricsType { 189 kJustAdvance_MetricsType, 190 kFull_MetricsType 191 }; 192 193 SkGlyph* lookupMetrics(uint32_t id, MetricsType); 194 static bool DetachProc(const SkGlyphCache*, void*) { return true; } 195 196 void detach(SkGlyphCache** head) { 197 if (fPrev) { 198 fPrev->fNext = fNext; 199 } else { 200 *head = fNext; 201 } 202 if (fNext) { 203 fNext->fPrev = fPrev; 204 } 205 fPrev = fNext = NULL; 206 } 207 208 void attachToHead(SkGlyphCache** head) { 209 SkASSERT(NULL == fPrev && NULL == fNext); 210 if (*head) { 211 (*head)->fPrev = this; 212 fNext = *head; 213 } 214 *head = this; 215 } 216 217 SkGlyphCache* fNext, *fPrev; 218 SkDescriptor* fDesc; 219 SkScalerContext* fScalerContext; 220 SkPaint::FontMetrics fFontMetricsY; 221 222 enum { 223 kHashBits = 12, 224 kHashCount = 1 << kHashBits, 225 kHashMask = kHashCount - 1 226 }; 227 SkGlyph* fGlyphHash[kHashCount]; 228 SkTDArray<SkGlyph*> fGlyphArray; 229 SkChunkAlloc fGlyphAlloc; 230 SkChunkAlloc fImageAlloc; 231 232 int fMetricsCount, fAdvanceCount; 233 234 struct CharGlyphRec { 235 uint32_t fID; // unichar + subpixel 236 SkGlyph* fGlyph; 237 }; 238 // no reason to use the same kHashCount as fGlyphHash, but we do for now 239 CharGlyphRec fCharToGlyphHash[kHashCount]; 240 241 enum { 242 // shift so that the top bits fall into kHashBits region 243 kShiftForHashIndex = SkGlyph::kSubShift + 244 SkGlyph::kSubBits*2 - 245 kHashBits 246 }; 247 248 static inline unsigned ID2HashIndex(uint32_t id) { 249 return (id ^ (id >> kShiftForHashIndex)) & kHashMask; 250 } 251 252 // used to track (approx) how much ram is tied-up in this cache 253 size_t fMemoryUsed; 254 255 struct AuxProcRec { 256 AuxProcRec* fNext; 257 void (*fProc)(void*); 258 void* fData; 259 }; 260 AuxProcRec* fAuxProcList; 261 void invokeAndRemoveAuxProcs(); 262 263 // This relies on the caller to have already acquired the mutex to access the global cache 264 static size_t InternalFreeCache(SkGlyphCache_Globals*, size_t bytesNeeded); 265 266 inline static SkGlyphCache* FindTail(SkGlyphCache* head); 267 static size_t ComputeMemoryUsed(const SkGlyphCache* head); 268 269 friend class SkGlyphCache_Globals; 270}; 271 272class SkAutoGlyphCache { 273public: 274 SkAutoGlyphCache(SkGlyphCache* cache) : fCache(cache) {} 275 SkAutoGlyphCache(const SkDescriptor* desc) { 276 fCache = SkGlyphCache::DetachCache(desc); 277 } 278 SkAutoGlyphCache(const SkPaint& paint, const SkMatrix* matrix) { 279 fCache = paint.detachCache(matrix); 280 } 281 ~SkAutoGlyphCache() { 282 if (fCache) { 283 SkGlyphCache::AttachCache(fCache); 284 } 285 } 286 287 SkGlyphCache* getCache() const { return fCache; } 288 289 void release() { 290 if (fCache) { 291 SkGlyphCache::AttachCache(fCache); 292 fCache = NULL; 293 } 294 } 295 296private: 297 SkGlyphCache* fCache; 298 299 static bool DetachProc(const SkGlyphCache*, void*); 300}; 301 302#endif 303 304