blob: bbc199fac1d167184661686bb9e1fcdddbfd635e [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.
Robert Phillipsd2e9f762018-03-07 11:54:37 -050066 GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
67 GrGlyphCache*, GrAtlasManager*, GrGlyph*,
68 SkGlyphCache*, GrMaskFormat expectedMaskFormat,
69 bool isScaledGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050070
71 // testing
72 int countGlyphs() const { return fCache.count(); }
73
74 // remove any references to this plot
75 void removeID(GrDrawOpAtlas::AtlasID);
76
77 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
78 bool isAbandoned() const { return fIsAbandoned; }
79
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050080 static const SkDescriptor& GetKey(const GrTextStrike& strike) {
81 return *strike.fFontScalerKey.getDesc();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050082 }
83
84 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
85
86private:
87 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
88 SkAutoDescriptor fFontScalerKey;
89 SkArenaAlloc fPool{512};
90
91 int fAtlasedGlyphs;
92 bool fIsAbandoned;
93
94 static const SkGlyph& GrToSkGlyph(SkGlyphCache* cache, GrGlyph::PackedID id) {
95 return cache->getGlyphIDMetrics(GrGlyph::UnpackID(id),
96 GrGlyph::UnpackFixedX(id),
97 GrGlyph::UnpackFixedY(id));
98 }
99
100 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, SkGlyphCache*);
101
102 friend class GrGlyphCache;
103};
104
105/**
106 * GrGlyphCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be
107 * used to generate individual Glyph Masks.
108 */
109class GrGlyphCache {
110public:
111 GrGlyphCache();
112 ~GrGlyphCache();
113
114 void setGlyphSizeLimit(SkScalar sizeLimit) { fGlyphSizeLimit = sizeLimit; }
115 SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
116
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500117 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500118
119 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
120 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500121 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500122 // interactions with the cache since the strike was received.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500123 inline sk_sp<GrTextStrike> getStrike(const SkGlyphCache* cache) {
124 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor()));
125 if (!strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500126 strike = this->generateStrike(cache);
127 }
128 return strike;
129 }
130
131 void freeAll();
132
133 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
134
135private:
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500136 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 Phillipsc4039ea2018-03-01 11:36:45 -0500140 return strike;
141 }
142
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500143 using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500144
145 StrikeHash fCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500146 GrTextStrike* fPreserveStrike;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500147 SkScalar fGlyphSizeLimit;
148};
149
150#endif