Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 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 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 8 | #ifndef GrStrikeCache_DEFINED |
| 9 | #define GrStrikeCache_DEFINED |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 10 | |
| 11 | #include "GrDrawOpAtlas.h" |
| 12 | #include "GrGlyph.h" |
| 13 | #include "SkArenaAlloc.h" |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 14 | #include "SkMasks.h" |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 15 | #include "SkStrike.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 16 | #include "SkTDynamicHash.h" |
| 17 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 18 | class GrAtlasManager; |
| 19 | class GrGpu; |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 20 | class GrStrikeCache; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 21 | |
| 22 | /** |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 23 | * The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 24 | * is indexed by a PackedID and SkStrike. The SkStrike is what actually creates the mask. |
| 25 | * The GrTextStrike may outlive the generating SkStrike. However, it retains a copy |
| 26 | * of it's SkDescriptor as a key to access (or regenerate) the SkStrike. GrTextStrikes are |
| 27 | * created by and owned by a GrStrikeCache. |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 28 | */ |
Mike Klein | 408ef21 | 2018-10-30 15:23:00 +0000 | [diff] [blame] | 29 | class GrTextStrike : public SkNVRefCnt<GrTextStrike> { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 30 | public: |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 31 | GrTextStrike(const SkDescriptor& fontScalerKey); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 32 | |
Herb Derby | 4c35be0 | 2018-12-20 14:03:47 -0500 | [diff] [blame] | 33 | GrGlyph* getGlyph(const SkGlyph& skGlyph) { |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 34 | GrGlyph* glyph = fCache.find(skGlyph.getPackedID()); |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 35 | if (!glyph) { |
Herb Derby | 4c35be0 | 2018-12-20 14:03:47 -0500 | [diff] [blame] | 36 | glyph = this->generateGlyph(skGlyph); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 37 | } |
| 38 | return glyph; |
| 39 | } |
| 40 | |
| 41 | // This variant of the above function is called by GrAtlasTextOp. At this point, it is possible |
| 42 | // that the maskformat of the glyph differs from what we expect. In these cases we will just |
| 43 | // draw a clear square. |
| 44 | // skbug:4143 crbug:510931 |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 45 | GrGlyph* getGlyph(SkPackedGlyphID packed, |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 46 | SkStrike* cache) { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 47 | GrGlyph* glyph = fCache.find(packed); |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 48 | if (!glyph) { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 49 | // We could return this to the caller, but in practice it adds code complexity for |
| 50 | // potentially little benefit(ie, if the glyph is not in our font cache, then its not |
| 51 | // in the atlas and we're going to be doing a texture upload anyways). |
| 52 | const SkGlyph& skGlyph = GrToSkGlyph(cache, packed); |
Herb Derby | 4c35be0 | 2018-12-20 14:03:47 -0500 | [diff] [blame] | 53 | glyph = this->generateGlyph(skGlyph); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 54 | } |
| 55 | return glyph; |
| 56 | } |
| 57 | |
| 58 | // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's |
| 59 | // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never |
| 60 | // happen. |
| 61 | // TODO we can handle some of these cases if we really want to, but the long term solution is to |
| 62 | // get the actual glyph image itself when we get the glyph metrics. |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 63 | GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*, |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 64 | GrStrikeCache*, GrAtlasManager*, GrGlyph*, |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 65 | SkStrike*, GrMaskFormat expectedMaskFormat, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 66 | bool isScaledGlyph); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 67 | |
| 68 | // testing |
| 69 | int countGlyphs() const { return fCache.count(); } |
| 70 | |
| 71 | // remove any references to this plot |
| 72 | void removeID(GrDrawOpAtlas::AtlasID); |
| 73 | |
| 74 | // If a TextStrike is abandoned by the cache, then the caller must get a new strike |
| 75 | bool isAbandoned() const { return fIsAbandoned; } |
| 76 | |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 77 | static const SkDescriptor& GetKey(const GrTextStrike& strike) { |
| 78 | return *strike.fFontScalerKey.getDesc(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); } |
| 82 | |
| 83 | private: |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 84 | SkTDynamicHash<GrGlyph, SkPackedGlyphID> fCache; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 85 | SkAutoDescriptor fFontScalerKey; |
Herb Derby | b03e024 | 2018-12-20 13:19:44 -0500 | [diff] [blame] | 86 | SkArenaAlloc fAlloc{512}; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 87 | |
Herbert Derby | 83ea522 | 2018-11-28 14:39:29 -0500 | [diff] [blame] | 88 | int fAtlasedGlyphs{0}; |
| 89 | bool fIsAbandoned{false}; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 90 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 91 | static const SkGlyph& GrToSkGlyph(SkStrike* cache, SkPackedGlyphID id) { |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 92 | return cache->getGlyphIDMetrics(id.code(), id.getSubXFixed(), id.getSubYFixed()); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Herb Derby | 4c35be0 | 2018-12-20 14:03:47 -0500 | [diff] [blame] | 95 | GrGlyph* generateGlyph(const SkGlyph&); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 96 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 97 | friend class GrStrikeCache; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /** |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 101 | * GrStrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 102 | * used to generate individual Glyph Masks. |
| 103 | */ |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 104 | class GrStrikeCache { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 105 | public: |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 106 | GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes); |
| 107 | ~GrStrikeCache(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 108 | |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 109 | void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; } |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 110 | |
| 111 | // The user of the cache may hold a long-lived ref to the returned strike. However, actions by |
| 112 | // another client of the cache may cause the strike to be purged while it is still reffed. |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 113 | // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 114 | // interactions with the cache since the strike was received. |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 115 | sk_sp<GrTextStrike> getStrike(const SkStrike* cache) { |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 116 | sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor())); |
| 117 | if (!strike) { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 118 | strike = this->generateStrike(cache); |
| 119 | } |
| 120 | return strike; |
| 121 | } |
| 122 | |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 123 | const SkMasks& getMasks() const { return *f565Masks; } |
| 124 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 125 | void freeAll(); |
| 126 | |
| 127 | static void HandleEviction(GrDrawOpAtlas::AtlasID, void*); |
| 128 | |
| 129 | private: |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 130 | sk_sp<GrTextStrike> generateStrike(const SkStrike* cache) { |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 131 | // 'fCache' get the construction ref |
| 132 | sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(cache->getDescriptor())); |
| 133 | fCache.add(strike.get()); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 134 | return strike; |
| 135 | } |
| 136 | |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 137 | using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 138 | |
| 139 | StrikeHash fCache; |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 140 | GrTextStrike* fPreserveStrike; |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 141 | std::unique_ptr<const SkMasks> f565Masks; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 142 | }; |
| 143 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame^] | 144 | #endif // GrStrikeCache_DEFINED |