blob: 365e0a78686c7ab551d97f83919d27673606b8f3 [file] [log] [blame]
Robert Phillipsc4039ea2018-03-01 11:36:45 -05001/*
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 Derby081e6f32019-01-16 13:46:02 -05008#ifndef GrStrikeCache_DEFINED
9#define GrStrikeCache_DEFINED
Robert Phillipsc4039ea2018-03-01 11:36:45 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/codec/SkMasks.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040012#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkStrike.h"
14#include "src/core/SkTDynamicHash.h"
15#include "src/gpu/GrDrawOpAtlas.h"
16#include "src/gpu/GrGlyph.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050017
Robert Phillipsc4039ea2018-03-01 11:36:45 -050018class GrAtlasManager;
19class GrGpu;
Herb Derby081e6f32019-01-16 13:46:02 -050020class GrStrikeCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050021
22/**
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050023 * The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
Herb Derby081e6f32019-01-16 13:46:02 -050024 * 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 Phillipsc4039ea2018-03-01 11:36:45 -050028 */
Mike Klein408ef212018-10-30 15:23:00 +000029class GrTextStrike : public SkNVRefCnt<GrTextStrike> {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050030public:
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050031 GrTextStrike(const SkDescriptor& fontScalerKey);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050032
Herb Derby4c35be02018-12-20 14:03:47 -050033 GrGlyph* getGlyph(const SkGlyph& skGlyph) {
Herb Derbyf5e5f192019-05-07 15:39:01 -040034 GrGlyph* grGlyph = fCache.find(skGlyph.getPackedID());
35 if (grGlyph == nullptr) {
36 grGlyph = fAlloc.make<GrGlyph>(skGlyph);
37 fCache.add(grGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050038 }
Herb Derbyf5e5f192019-05-07 15:39:01 -040039 return grGlyph;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050040 }
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
Herb Derbyf5e5f192019-05-07 15:39:01 -040046 GrGlyph* getGlyph(SkPackedGlyphID packed, SkStrike* skStrike) {
47 GrGlyph* grGlyph = fCache.find(packed);
48 if (grGlyph == nullptr) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050049 // 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).
Herb Derby3e8e34e2019-06-20 15:16:13 -040052 grGlyph = fAlloc.make<GrGlyph>(*skStrike->glyph(packed));
Herb Derbyf5e5f192019-05-07 15:39:01 -040053 fCache.add(grGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050054 }
Herb Derbyf5e5f192019-05-07 15:39:01 -040055 return grGlyph;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050056 }
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 Phillipsd2e9f762018-03-07 11:54:37 -050063 GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
Herb Derby081e6f32019-01-16 13:46:02 -050064 GrStrikeCache*, GrAtlasManager*, GrGlyph*,
Herb Derby5fd955e2019-01-16 11:23:29 -050065 SkStrike*, GrMaskFormat expectedMaskFormat,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050066 bool isScaledGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050067
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 Phillipscaf1ebb2018-03-01 14:28:44 -050077 static const SkDescriptor& GetKey(const GrTextStrike& strike) {
78 return *strike.fFontScalerKey.getDesc();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050079 }
80
81 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
82
83private:
Herb Derby5a3fdee2018-12-20 14:47:03 -050084 SkTDynamicHash<GrGlyph, SkPackedGlyphID> fCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050085 SkAutoDescriptor fFontScalerKey;
Herb Derbyb03e0242018-12-20 13:19:44 -050086 SkArenaAlloc fAlloc{512};
Robert Phillipsc4039ea2018-03-01 11:36:45 -050087
Herbert Derby83ea5222018-11-28 14:39:29 -050088 int fAtlasedGlyphs{0};
89 bool fIsAbandoned{false};
Robert Phillipsc4039ea2018-03-01 11:36:45 -050090
Herb Derby081e6f32019-01-16 13:46:02 -050091 friend class GrStrikeCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050092};
93
94/**
Herb Derby081e6f32019-01-16 13:46:02 -050095 * GrStrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be
Robert Phillipsc4039ea2018-03-01 11:36:45 -050096 * used to generate individual Glyph Masks.
97 */
Herb Derby081e6f32019-01-16 13:46:02 -050098class GrStrikeCache {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050099public:
Herb Derby081e6f32019-01-16 13:46:02 -0500100 GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes);
101 ~GrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500102
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500103 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500104
105 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
106 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500107 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500108 // interactions with the cache since the strike was received.
Herb Derby30595ea2019-02-11 11:25:55 -0500109 sk_sp<GrTextStrike> getStrike(const SkDescriptor& desc) {
110 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(desc));
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500111 if (!strike) {
Herb Derby30595ea2019-02-11 11:25:55 -0500112 strike = this->generateStrike(desc);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500113 }
114 return strike;
115 }
116
Timothy Liang91e260f2018-06-15 13:28:35 -0400117 const SkMasks& getMasks() const { return *f565Masks; }
118
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500119 void freeAll();
120
121 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
122
123private:
Herb Derby30595ea2019-02-11 11:25:55 -0500124 sk_sp<GrTextStrike> generateStrike(const SkDescriptor& desc) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500125 // 'fCache' get the construction ref
Herb Derby30595ea2019-02-11 11:25:55 -0500126 sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(desc));
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500127 fCache.add(strike.get());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500128 return strike;
129 }
130
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500131 using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500132
133 StrikeHash fCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500134 GrTextStrike* fPreserveStrike;
Timothy Liang91e260f2018-06-15 13:28:35 -0400135 std::unique_ptr<const SkMasks> f565Masks;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500136};
137
Herb Derby081e6f32019-01-16 13:46:02 -0500138#endif // GrStrikeCache_DEFINED