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