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