blob: 827981424181cdb64462c98e4e0944ea04eb2c72 [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
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"
15#include "SkTDynamicHash.h"
16
17class GrGlyphCache;
18class GrAtlasManager;
19class GrGpu;
20
21/**
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050022 * The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
Robert Phillipsc4039ea2018-03-01 11:36:45 -050023 * is indexed by a PackedID and SkGlyphCache. The SkGlyphCache is what actually creates the mask.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050024 * The GrTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
25 * of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrTextStrikes are
Robert Phillipsc4039ea2018-03-01 11:36:45 -050026 * created by and owned by a GrGlyphCache.
27 */
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050028class GrTextStrike : public SkNVRefCnt<GrTextStrike> {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050029public:
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050030 GrTextStrike(const SkDescriptor& fontScalerKey);
31 ~GrTextStrike();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050032
33 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
34 SkGlyphCache* cache) {
35 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050036 if (!glyph) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050037 glyph = this->generateGlyph(skGlyph, packed, cache);
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
46 inline GrGlyph* getGlyph(GrGlyph::PackedID packed,
47 GrMaskFormat expectedMaskFormat,
48 SkGlyphCache* cache) {
49 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050050 if (!glyph) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050051 // We could return this to the caller, but in practice it adds code complexity for
52 // potentially little benefit(ie, if the glyph is not in our font cache, then its not
53 // in the atlas and we're going to be doing a texture upload anyways).
54 const SkGlyph& skGlyph = GrToSkGlyph(cache, packed);
55 glyph = this->generateGlyph(skGlyph, packed, cache);
56 glyph->fMaskFormat = expectedMaskFormat;
57 }
58 return glyph;
59 }
60
61 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
62 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
63 // happen.
64 // TODO we can handle some of these cases if we really want to, but the long term solution is to
65 // get the actual glyph image itself when we get the glyph metrics.
66 bool addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*, GrGlyphCache*,
67 GrAtlasManager*, GrGlyph*,
68 SkGlyphCache*, GrMaskFormat expectedMaskFormat);
69
70 // testing
71 int countGlyphs() const { return fCache.count(); }
72
73 // remove any references to this plot
74 void removeID(GrDrawOpAtlas::AtlasID);
75
76 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
77 bool isAbandoned() const { return fIsAbandoned; }
78
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050079 static const SkDescriptor& GetKey(const GrTextStrike& strike) {
80 return *strike.fFontScalerKey.getDesc();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050081 }
82
83 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
84
85private:
86 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
87 SkAutoDescriptor fFontScalerKey;
88 SkArenaAlloc fPool{512};
89
90 int fAtlasedGlyphs;
91 bool fIsAbandoned;
92
93 static const SkGlyph& GrToSkGlyph(SkGlyphCache* cache, GrGlyph::PackedID id) {
94 return cache->getGlyphIDMetrics(GrGlyph::UnpackID(id),
95 GrGlyph::UnpackFixedX(id),
96 GrGlyph::UnpackFixedY(id));
97 }
98
99 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, SkGlyphCache*);
100
101 friend class GrGlyphCache;
102};
103
104/**
105 * GrGlyphCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be
106 * used to generate individual Glyph Masks.
107 */
108class GrGlyphCache {
109public:
110 GrGlyphCache();
111 ~GrGlyphCache();
112
113 void setGlyphSizeLimit(SkScalar sizeLimit) { fGlyphSizeLimit = sizeLimit; }
114 SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
115
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500116 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500117
118 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
119 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500120 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500121 // interactions with the cache since the strike was received.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500122 inline sk_sp<GrTextStrike> getStrike(const SkGlyphCache* cache) {
123 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor()));
124 if (!strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500125 strike = this->generateStrike(cache);
126 }
127 return strike;
128 }
129
130 void freeAll();
131
132 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
133
134private:
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500135 sk_sp<GrTextStrike> generateStrike(const SkGlyphCache* cache) {
136 // 'fCache' get the construction ref
137 sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(cache->getDescriptor()));
138 fCache.add(strike.get());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500139 return strike;
140 }
141
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500142 using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500143
144 StrikeHash fCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500145 GrTextStrike* fPreserveStrike;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500146 SkScalar fGlyphSizeLimit;
147};
148
149#endif