blob: a3877a0a7f2b787423ab789764c947a7ce08199c [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 "include/private/SkArenaAlloc.h"
12#include "src/codec/SkMasks.h"
13#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 Derbyf5e5f192019-05-07 15:39:01 -040052 const SkGlyph& skGlyph = skStrike->getGlyphIDMetrics(packed);
53 grGlyph = fAlloc.make<GrGlyph>(skGlyph);
54 fCache.add(grGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050055 }
Herb Derbyf5e5f192019-05-07 15:39:01 -040056 return grGlyph;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050057 }
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 Phillipsd2e9f762018-03-07 11:54:37 -050064 GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
Herb Derby081e6f32019-01-16 13:46:02 -050065 GrStrikeCache*, GrAtlasManager*, GrGlyph*,
Herb Derby5fd955e2019-01-16 11:23:29 -050066 SkStrike*, GrMaskFormat expectedMaskFormat,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050067 bool isScaledGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050068
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 Phillipscaf1ebb2018-03-01 14:28:44 -050078 static const SkDescriptor& GetKey(const GrTextStrike& strike) {
79 return *strike.fFontScalerKey.getDesc();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050080 }
81
82 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
83
84private:
Herb Derby5a3fdee2018-12-20 14:47:03 -050085 SkTDynamicHash<GrGlyph, SkPackedGlyphID> fCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050086 SkAutoDescriptor fFontScalerKey;
Herb Derbyb03e0242018-12-20 13:19:44 -050087 SkArenaAlloc fAlloc{512};
Robert Phillipsc4039ea2018-03-01 11:36:45 -050088
Herbert Derby83ea5222018-11-28 14:39:29 -050089 int fAtlasedGlyphs{0};
90 bool fIsAbandoned{false};
Robert Phillipsc4039ea2018-03-01 11:36:45 -050091
Herb Derby081e6f32019-01-16 13:46:02 -050092 friend class GrStrikeCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050093};
94
95/**
Herb Derby081e6f32019-01-16 13:46:02 -050096 * GrStrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be
Robert Phillipsc4039ea2018-03-01 11:36:45 -050097 * used to generate individual Glyph Masks.
98 */
Herb Derby081e6f32019-01-16 13:46:02 -050099class GrStrikeCache {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500100public:
Herb Derby081e6f32019-01-16 13:46:02 -0500101 GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes);
102 ~GrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500103
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500104 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500105
106 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
107 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500108 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500109 // interactions with the cache since the strike was received.
Herb Derby30595ea2019-02-11 11:25:55 -0500110 sk_sp<GrTextStrike> getStrike(const SkDescriptor& desc) {
111 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(desc));
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500112 if (!strike) {
Herb Derby30595ea2019-02-11 11:25:55 -0500113 strike = this->generateStrike(desc);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500114 }
115 return strike;
116 }
117
Timothy Liang91e260f2018-06-15 13:28:35 -0400118 const SkMasks& getMasks() const { return *f565Masks; }
119
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500120 void freeAll();
121
122 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
123
124private:
Herb Derby30595ea2019-02-11 11:25:55 -0500125 sk_sp<GrTextStrike> generateStrike(const SkDescriptor& desc) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500126 // 'fCache' get the construction ref
Herb Derby30595ea2019-02-11 11:25:55 -0500127 sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(desc));
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500128 fCache.add(strike.get());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500129 return strike;
130 }
131
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500132 using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500133
134 StrikeHash fCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500135 GrTextStrike* fPreserveStrike;
Timothy Liang91e260f2018-06-15 13:28:35 -0400136 std::unique_ptr<const SkMasks> f565Masks;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500137};
138
Herb Derby081e6f32019-01-16 13:46:02 -0500139#endif // GrStrikeCache_DEFINED