blob: 169e4b30a5566b436b3690675dbdff98f718aa24 [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/**
22 * The GrAtlasTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
23 * is indexed by a PackedID and SkGlyphCache. The SkGlyphCache is what actually creates the mask.
24 * The GrAtlasTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
25 * of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrAtlasTextStrike are
26 * created by and owned by a GrGlyphCache.
27 */
28class GrAtlasTextStrike : public SkNVRefCnt<GrAtlasTextStrike> {
29public:
30 GrAtlasTextStrike(const SkDescriptor& fontScalerKey);
31 ~GrAtlasTextStrike();
32
33 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
34 SkGlyphCache* cache) {
35 GrGlyph* glyph = fCache.find(packed);
36 if (nullptr == glyph) {
37 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);
50 if (nullptr == glyph) {
51 // 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
79 static const SkDescriptor& GetKey(const GrAtlasTextStrike& ts) {
80 return *ts.fFontScalerKey.getDesc();
81 }
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
116 void setStrikeToPreserve(GrAtlasTextStrike* strike) { fPreserveStrike = strike; }
117
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.
120 // Therefore, the caller must check GrAtlasTextStrike::isAbandoned() if there are other
121 // interactions with the cache since the strike was received.
122 inline GrAtlasTextStrike* getStrike(const SkGlyphCache* cache) {
123 GrAtlasTextStrike* strike = fCache.find(cache->getDescriptor());
124 if (nullptr == strike) {
125 strike = this->generateStrike(cache);
126 }
127 return strike;
128 }
129
130 void freeAll();
131
132 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
133
134private:
135 GrAtlasTextStrike* generateStrike(const SkGlyphCache* cache) {
136 GrAtlasTextStrike* strike = new GrAtlasTextStrike(cache->getDescriptor());
137 fCache.add(strike);
138 return strike;
139 }
140
141 using StrikeHash = SkTDynamicHash<GrAtlasTextStrike, SkDescriptor>;
142
143 StrikeHash fCache;
144 GrAtlasTextStrike* fPreserveStrike;
145 SkScalar fGlyphSizeLimit;
146};
147
148#endif