blob: ec4ca2b008c19401146ee4f89814bc8f59af8213 [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"
Timothy Liang91e260f2018-06-15 13:28:35 -040015#include "SkMasks.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050016#include "SkTDynamicHash.h"
17
18class GrGlyphCache;
19class GrAtlasManager;
20class GrGpu;
21
22/**
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050023 * The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
Robert Phillipsc4039ea2018-03-01 11:36:45 -050024 * is indexed by a PackedID and SkGlyphCache. The SkGlyphCache is what actually creates the mask.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050025 * The GrTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
26 * of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrTextStrikes are
Robert Phillipsc4039ea2018-03-01 11:36:45 -050027 * created by and owned by a GrGlyphCache.
28 */
Mike Klein0fb1ee92018-10-30 07:53:43 -040029class GrTextStrike : public SkRefCnt {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050030public:
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050031 GrTextStrike(const SkDescriptor& fontScalerKey);
32 ~GrTextStrike();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050033
34 inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
35 SkGlyphCache* cache) {
36 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050037 if (!glyph) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050038 glyph = this->generateGlyph(skGlyph, packed, cache);
39 }
40 return glyph;
41 }
42
43 // This variant of the above function is called by GrAtlasTextOp. At this point, it is possible
44 // that the maskformat of the glyph differs from what we expect. In these cases we will just
45 // draw a clear square.
46 // skbug:4143 crbug:510931
47 inline GrGlyph* getGlyph(GrGlyph::PackedID packed,
48 GrMaskFormat expectedMaskFormat,
49 SkGlyphCache* cache) {
50 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050051 if (!glyph) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050052 // We could return this to the caller, but in practice it adds code complexity for
53 // potentially little benefit(ie, if the glyph is not in our font cache, then its not
54 // in the atlas and we're going to be doing a texture upload anyways).
55 const SkGlyph& skGlyph = GrToSkGlyph(cache, packed);
56 glyph = this->generateGlyph(skGlyph, packed, cache);
57 glyph->fMaskFormat = expectedMaskFormat;
58 }
59 return glyph;
60 }
61
62 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
63 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
64 // happen.
65 // TODO we can handle some of these cases if we really want to, but the long term solution is to
66 // get the actual glyph image itself when we get the glyph metrics.
Robert Phillipsd2e9f762018-03-07 11:54:37 -050067 GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
68 GrGlyphCache*, GrAtlasManager*, GrGlyph*,
69 SkGlyphCache*, GrMaskFormat expectedMaskFormat,
70 bool isScaledGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050071
72 // testing
73 int countGlyphs() const { return fCache.count(); }
74
75 // remove any references to this plot
76 void removeID(GrDrawOpAtlas::AtlasID);
77
78 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
79 bool isAbandoned() const { return fIsAbandoned; }
80
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050081 static const SkDescriptor& GetKey(const GrTextStrike& strike) {
82 return *strike.fFontScalerKey.getDesc();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050083 }
84
85 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
86
87private:
88 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
89 SkAutoDescriptor fFontScalerKey;
90 SkArenaAlloc fPool{512};
91
92 int fAtlasedGlyphs;
93 bool fIsAbandoned;
94
95 static const SkGlyph& GrToSkGlyph(SkGlyphCache* cache, GrGlyph::PackedID id) {
96 return cache->getGlyphIDMetrics(GrGlyph::UnpackID(id),
97 GrGlyph::UnpackFixedX(id),
98 GrGlyph::UnpackFixedY(id));
99 }
100
101 GrGlyph* generateGlyph(const SkGlyph&, GrGlyph::PackedID, SkGlyphCache*);
102
103 friend class GrGlyphCache;
104};
105
106/**
107 * GrGlyphCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be
108 * used to generate individual Glyph Masks.
109 */
110class GrGlyphCache {
111public:
Khushalfa8ff092018-06-06 17:46:38 -0700112 GrGlyphCache(const GrCaps* caps, size_t maxTextureBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500113 ~GrGlyphCache();
114
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500115 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500116
117 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
118 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500119 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500120 // interactions with the cache since the strike was received.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500121 inline sk_sp<GrTextStrike> getStrike(const SkGlyphCache* cache) {
122 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor()));
123 if (!strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500124 strike = this->generateStrike(cache);
125 }
126 return strike;
127 }
128
Timothy Liang91e260f2018-06-15 13:28:35 -0400129 const SkMasks& getMasks() const { return *f565Masks; }
130
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500131 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;
Timothy Liang91e260f2018-06-15 13:28:35 -0400147 std::unique_ptr<const SkMasks> f565Masks;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500148};
149
150#endif