| 1 |
/* |
| 2 |
* Copyright 2015 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef sktext_gpu_StrikeCache_DEFINED |
| 9 |
#define sktext_gpu_StrikeCache_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRefCnt.h" |
| 12 |
#include "src/base/SkArenaAlloc.h" |
| 13 |
#include "src/core/SkDescriptor.h" |
| 14 |
#include "src/core/SkStrikeSpec.h" |
| 15 |
#include "src/core/SkTHash.h" |
| 16 |
#include "src/gpu/AtlasTypes.h" |
| 17 |
|
| 18 |
#include <cstddef> |
| 19 |
#include <cstdint> |
| 20 |
|
| 21 |
struct SkPackedGlyphID; |
| 22 |
|
| 23 |
// SK_DEFAULT_GPU_FONT_CACHE_COUNT_LIMIT and SK_DEFAULT_GPU_FONT_CACHE_LIMIT can be set using -D |
| 24 |
// on your ompiler commandline, or by using the defines in SkUserConfig.h |
| 25 |
#ifndef SK_DEFAULT_GPU_FONT_CACHE_COUNT_LIMIT |
| 26 |
#define SK_DEFAULT_GPU_FONT_CACHE_COUNT_LIMIT 2048 |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifndef SK_DEFAULT_GPU_FONT_CACHE_LIMIT |
| 30 |
#define SK_DEFAULT_GPU_FONT_CACHE_LIMIT (2 * 1024 * 1024) |
| 31 |
#endif |
| 32 |
|
| 33 |
namespace sktext::gpu { |
| 34 |
|
| 35 |
class Glyph; |
| 36 |
struct GlyphEntryKey; |
| 37 |
class StrikeCache; |
| 38 |
|
| 39 |
// The TextStrike manages an SkArenaAlloc for Glyphs. The SkStrike is what actually creates |
| 40 |
// the mask. The TextStrike may outlive the generating SkStrike. However, it retains a copy |
| 41 |
// of it's SkDescriptor as a key to access (or regenerate) the SkStrike. TextStrikes are |
| 42 |
// created by and owned by a StrikeCache. |
| 43 |
class TextStrike : public SkNVRefCnt<TextStrike> { |
| 44 |
public: |
| 45 |
TextStrike(StrikeCache* strikeCache, |
| 46 |
const SkStrikeSpec& strikeSpec); |
| 47 |
|
| 48 |
Glyph* getGlyph(SkPackedGlyphID, skgpu::MaskFormat format); |
| 49 |
const SkStrikeSpec& strikeSpec() const { return fStrikeSpec; } |
| 50 |
const SkDescriptor& getDescriptor() const { return fStrikeSpec.descriptor(); } |
| 51 |
|
| 52 |
private: |
| 53 |
StrikeCache* const fStrikeCache; |
| 54 |
|
| 55 |
// Key for retrieving the SkStrike for creating new atlas data. |
| 56 |
const SkStrikeSpec fStrikeSpec; |
| 57 |
|
| 58 |
struct HashTraits { |
| 59 |
static const GlyphEntryKey& GetKey(const Glyph* glyph); |
| 60 |
static uint32_t Hash(GlyphEntryKey key); |
| 61 |
}; |
| 62 |
// Map SkPackedGlyphID -> Glyph*. |
| 63 |
skia_private::THashTable<Glyph*, GlyphEntryKey, HashTraits> fCache; |
| 64 |
|
| 65 |
// Store for the glyph information. |
| 66 |
SkArenaAlloc fAlloc{512}; |
| 67 |
|
| 68 |
TextStrike* fNext{nullptr}; |
| 69 |
TextStrike* fPrev{nullptr}; |
| 70 |
size_t fMemoryUsed{sizeof(TextStrike)}; |
| 71 |
bool fRemoved{false}; |
| 72 |
|
| 73 |
friend class StrikeCache; |
| 74 |
}; |
| 75 |
|
| 76 |
// StrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be |
| 77 |
// used to generate individual Glyph Masks. |
| 78 |
class StrikeCache { |
| 79 |
public: |
| 80 |
~StrikeCache(); |
| 81 |
|
| 82 |
// The user of the cache may hold a long-lived ref to the returned strike. |
| 83 |
sk_sp<TextStrike> findOrCreateStrike(const SkStrikeSpec& strikeSpec); |
| 84 |
|
| 85 |
void freeAll(); |
| 86 |
|
| 87 |
private: |
| 88 |
friend class TextStrike; // for TextStrike::getGlyph |
| 89 |
sk_sp<TextStrike> internalFindStrikeOrNull(const SkDescriptor& desc); |
| 90 |
sk_sp<TextStrike> generateStrike(const SkStrikeSpec& strikeSpec); |
| 91 |
|
| 92 |
void internalRemoveStrike(TextStrike* strike); |
| 93 |
void internalAttachToHead(sk_sp<TextStrike> strike); |
| 94 |
|
| 95 |
// Checkout budgets, modulated by the specified min-bytes-needed-to-purge, |
| 96 |
// and attempt to purge caches to match. |
| 97 |
// Returns number of bytes freed. |
| 98 |
size_t internalPurge(size_t minBytesNeeded = 0); |
| 99 |
|
| 100 |
// A simple accounting of what each glyph cache reports and the strike cache total. |
| 101 |
void validate() const; |
| 102 |
|
| 103 |
TextStrike* fHead{nullptr}; |
| 104 |
TextStrike* fTail{nullptr}; |
| 105 |
|
| 106 |
struct HashTraits { |
| 107 |
static const SkDescriptor& GetKey(const sk_sp<TextStrike>& strike); |
| 108 |
static uint32_t Hash(const SkDescriptor& strikeSpec); |
| 109 |
}; |
| 110 |
using StrikeHash = skia_private::THashTable<sk_sp<TextStrike>, const SkDescriptor&, HashTraits>; |
| 111 |
|
| 112 |
StrikeHash fCache; |
| 113 |
|
| 114 |
size_t fCacheSizeLimit{SK_DEFAULT_GPU_FONT_CACHE_LIMIT}; |
| 115 |
size_t fTotalMemoryUsed{0}; |
| 116 |
int32_t fCacheCountLimit{SK_DEFAULT_GPU_FONT_CACHE_COUNT_LIMIT}; |
| 117 |
int32_t fCacheCount{0}; |
| 118 |
}; |
| 119 |
|
| 120 |
} // namespace sktext::gpu |
| 121 |
|
| 122 |
#endif // sktext_gpu_StrikeCache_DEFINED |