OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be found
in the LICENSE file. | 4 * Use of this source code is governed by a BSD-style license that can be found
in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #ifndef SkGlyphCache_DEFINED | 7 #ifndef SkGlyphCache_DEFINED |
8 #define SkGlyphCache_DEFINED | 8 #define SkGlyphCache_DEFINED |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkChunkAlloc.h" | 11 #include "SkChunkAlloc.h" |
12 #include "SkDescriptor.h" | 12 #include "SkDescriptor.h" |
13 #include "SkGlyph.h" | 13 #include "SkGlyph.h" |
| 14 #include "SkPaint.h" |
14 #include "SkTHash.h" | 15 #include "SkTHash.h" |
15 #include "SkScalerContext.h" | 16 #include "SkScalerContext.h" |
16 #include "SkTemplates.h" | 17 #include "SkTemplates.h" |
17 #include "SkTDArray.h" | 18 #include "SkTDArray.h" |
18 | 19 |
19 class SkPaint; | |
20 class SkTraceMemoryDump; | 20 class SkTraceMemoryDump; |
21 | 21 |
22 class SkGlyphCache_Globals; | 22 class SkGlyphCache_Globals; |
23 | 23 |
24 /** \class SkGlyphCache | 24 /** \class SkGlyphCache |
25 | 25 |
26 This class represents a strike: a specific combination of typeface, size, ma
trix, etc., and | 26 This class represents a strike: a specific combination of typeface, size, ma
trix, etc., and |
27 holds the glyphs for that strike. Calling any of the getUnichar.../getGlyphI
D... methods will | 27 holds the glyphs for that strike. Calling any of the getUnichar.../getGlyphI
D... methods will |
28 return the requested glyph, either instantly if it is already cached, or by
first generating | 28 return the requested glyph, either instantly if it is already cached, or by
first generating |
29 it and then adding it to the strike. | 29 it and then adding it to the strike. |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 nullptr. | 130 nullptr. |
131 */ | 131 */ |
132 static SkGlyphCache* VisitCache(SkTypeface*, const SkDescriptor* desc, | 132 static SkGlyphCache* VisitCache(SkTypeface*, const SkDescriptor* desc, |
133 bool (*proc)(const SkGlyphCache*, void*), | 133 bool (*proc)(const SkGlyphCache*, void*), |
134 void* context); | 134 void* context); |
135 | 135 |
136 /** Given a strike that was returned by either VisitCache() or DetachCache()
add it back into | 136 /** Given a strike that was returned by either VisitCache() or DetachCache()
add it back into |
137 the global cache list (after which the caller should not reference it an
ymore. | 137 the global cache list (after which the caller should not reference it an
ymore. |
138 */ | 138 */ |
139 static void AttachCache(SkGlyphCache*); | 139 static void AttachCache(SkGlyphCache*); |
| 140 using AttachCacheFunctor = SkFunctionWrapper<void, SkGlyphCache, AttachCache
>; |
140 | 141 |
141 /** Detach a strike from the global cache matching the specified descriptor.
Once detached, | 142 /** Detach a strike from the global cache matching the specified descriptor.
Once detached, |
142 it can be queried/modified by the current thread, and when finished, be
reattached to the | 143 it can be queried/modified by the current thread, and when finished, be
reattached to the |
143 global cache with AttachCache(). While detached, if another request is m
ade with the same | 144 global cache with AttachCache(). While detached, if another request is m
ade with the same |
144 descriptor, a different strike will be generated. This is fine. It does
mean we can have | 145 descriptor, a different strike will be generated. This is fine. It does
mean we can have |
145 more than 1 strike for the same descriptor, but that will eventually get
purged, and the | 146 more than 1 strike for the same descriptor, but that will eventually get
purged, and the |
146 win is that different thread will never block each other while a strike
is being used. | 147 win is that different thread will never block each other while a strike
is being used. |
147 */ | 148 */ |
148 static SkGlyphCache* DetachCache(SkTypeface* typeface, const SkDescriptor* d
esc) { | 149 static SkGlyphCache* DetachCache(SkTypeface* typeface, const SkDescriptor* d
esc) { |
149 return VisitCache(typeface, desc, DetachProc, nullptr); | 150 return VisitCache(typeface, desc, DetachProc, nullptr); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 SkChunkAlloc fGlyphAlloc; | 265 SkChunkAlloc fGlyphAlloc; |
265 | 266 |
266 SkAutoTArray<CharGlyphRec> fPackedUnicharIDToPackedGlyphID; | 267 SkAutoTArray<CharGlyphRec> fPackedUnicharIDToPackedGlyphID; |
267 | 268 |
268 // used to track (approx) how much ram is tied-up in this cache | 269 // used to track (approx) how much ram is tied-up in this cache |
269 size_t fMemoryUsed; | 270 size_t fMemoryUsed; |
270 | 271 |
271 AuxProcRec* fAuxProcList; | 272 AuxProcRec* fAuxProcList; |
272 }; | 273 }; |
273 | 274 |
274 class SkAutoGlyphCacheBase { | 275 class SkAutoGlyphCache : public skstd::unique_ptr<SkGlyphCache, SkGlyphCache::At
tachCacheFunctor> { |
275 public: | 276 public: |
276 SkGlyphCache* getCache() const { return fCache; } | 277 /** deprecated: use get() */ |
| 278 SkGlyphCache* getCache() const { return this->get(); } |
277 | 279 |
278 void release() { | 280 SkAutoGlyphCache(SkGlyphCache* cache) : INHERITED(cache) {} |
279 if (fCache) { | 281 SkAutoGlyphCache(SkTypeface* typeface, const SkDescriptor* desc) |
280 SkGlyphCache::AttachCache(fCache); | 282 : INHERITED(SkGlyphCache::DetachCache(typeface, desc)) |
281 fCache = nullptr; | 283 {} |
282 } | 284 /** deprecated: always enables fake gamma */ |
283 } | 285 SkAutoGlyphCache(const SkPaint& paint, |
284 | 286 const SkSurfaceProps* surfaceProps, |
285 protected: | 287 const SkMatrix* matrix) |
286 // Hide the constructors so we can't create one of these directly. Create Sk
AutoGlyphCache or | 288 : INHERITED(paint.detachCache(surfaceProps, SkPaint::FakeGamma::On, matr
ix)) |
287 // SkAutoGlyphCacheNoCache instead. | 289 {} |
288 SkAutoGlyphCacheBase(SkGlyphCache* cache) : fCache(cache) {} | 290 SkAutoGlyphCache(const SkPaint& paint, |
289 SkAutoGlyphCacheBase(SkTypeface* typeface, const SkDescriptor* desc) { | 291 const SkSurfaceProps* surfaceProps, |
290 fCache = SkGlyphCache::DetachCache(typeface, desc); | 292 SkPaint::FakeGamma fakeGamma, |
291 } | 293 const SkMatrix* matrix) |
292 SkAutoGlyphCacheBase(const SkPaint& /*paint*/, | 294 : INHERITED(paint.detachCache(surfaceProps, fakeGamma, matrix)) |
293 const SkSurfaceProps* /*surfaceProps*/, | 295 {} |
294 const SkMatrix* /*matrix*/) { | |
295 fCache = nullptr; | |
296 } | |
297 SkAutoGlyphCacheBase() { | |
298 fCache = nullptr; | |
299 } | |
300 ~SkAutoGlyphCacheBase() { | |
301 if (fCache) { | |
302 SkGlyphCache::AttachCache(fCache); | |
303 } | |
304 } | |
305 | |
306 SkGlyphCache* fCache; | |
307 | |
308 private: | 296 private: |
309 static bool DetachProc(const SkGlyphCache*, void*); | 297 using INHERITED = skstd::unique_ptr<SkGlyphCache, SkGlyphCache::AttachCacheF
unctor>; |
310 }; | 298 }; |
311 | 299 |
312 class SkAutoGlyphCache : public SkAutoGlyphCacheBase { | 300 class SkAutoGlyphCacheNoGamma : public SkAutoGlyphCache { |
313 public: | 301 public: |
314 SkAutoGlyphCache(SkGlyphCache* cache) : SkAutoGlyphCacheBase(cache) {} | 302 SkAutoGlyphCacheNoGamma(const SkPaint& paint, |
315 SkAutoGlyphCache(SkTypeface* typeface, const SkDescriptor* desc) : | 303 const SkSurfaceProps* surfaceProps, |
316 SkAutoGlyphCacheBase(typeface, desc) {} | 304 const SkMatrix* matrix) |
317 SkAutoGlyphCache(const SkPaint& paint, | 305 : SkAutoGlyphCache(paint, surfaceProps, SkPaint::FakeGamma::Off, matrix) |
318 const SkSurfaceProps* surfaceProps, | 306 {} |
319 const SkMatrix* matrix) { | |
320 fCache = paint.detachCache(surfaceProps, matrix, false); | |
321 } | |
322 | |
323 private: | |
324 SkAutoGlyphCache() : SkAutoGlyphCacheBase() {} | |
325 }; | 307 }; |
326 #define SkAutoGlyphCache(...) SK_REQUIRE_LOCAL_VAR(SkAutoGlyphCache) | 308 #define SkAutoGlyphCache(...) SK_REQUIRE_LOCAL_VAR(SkAutoGlyphCache) |
327 | |
328 class SkAutoGlyphCacheNoGamma : public SkAutoGlyphCacheBase { | |
329 public: | |
330 SkAutoGlyphCacheNoGamma(SkGlyphCache* cache) : SkAutoGlyphCacheBase(cache) {
} | |
331 SkAutoGlyphCacheNoGamma(SkTypeface* typeface, const SkDescriptor* desc) : | |
332 SkAutoGlyphCacheBase(typeface, desc) {} | |
333 SkAutoGlyphCacheNoGamma(const SkPaint& paint, | |
334 const SkSurfaceProps* surfaceProps, | |
335 const SkMatrix* matrix) { | |
336 fCache = paint.detachCache(surfaceProps, matrix, true); | |
337 } | |
338 | |
339 private: | |
340 SkAutoGlyphCacheNoGamma() : SkAutoGlyphCacheBase() {} | |
341 }; | |
342 #define SkAutoGlyphCacheNoGamma(...) SK_REQUIRE_LOCAL_VAR(SkAutoGlyphCacheNoGamm
a) | 309 #define SkAutoGlyphCacheNoGamma(...) SK_REQUIRE_LOCAL_VAR(SkAutoGlyphCacheNoGamm
a) |
343 | 310 |
344 #endif | 311 #endif |
OLD | NEW |