blob: 835d4846be915b2475f1863bfd884df39a877791 [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 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) {
34 GrGlyph::PackedID packed = GrGlyph::FromSkGlyph(skGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050035 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050036 if (!glyph) {
Herb Derby4c35be02018-12-20 14:03:47 -050037 glyph = this->generateGlyph(skGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050038 }
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
Herbert Derbyc8f6ab382018-11-28 13:00:00 -050046 GrGlyph* getGlyph(GrGlyph::PackedID packed,
Herbert Derbyc8f6ab382018-11-28 13:00:00 -050047 SkGlyphCache* cache) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050048 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050049 if (!glyph) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050050 // We could return this to the caller, but in practice it adds code complexity for
51 // potentially little benefit(ie, if the glyph is not in our font cache, then its not
52 // in the atlas and we're going to be doing a texture upload anyways).
53 const SkGlyph& skGlyph = GrToSkGlyph(cache, packed);
Herb Derby4c35be02018-12-20 14:03:47 -050054 glyph = this->generateGlyph(skGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050055 }
56 return glyph;
57 }
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*,
65 GrGlyphCache*, GrAtlasManager*, GrGlyph*,
66 SkGlyphCache*, GrMaskFormat expectedMaskFormat,
67 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:
85 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
86 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
92 static const SkGlyph& GrToSkGlyph(SkGlyphCache* cache, GrGlyph::PackedID id) {
93 return cache->getGlyphIDMetrics(GrGlyph::UnpackID(id),
94 GrGlyph::UnpackFixedX(id),
95 GrGlyph::UnpackFixedY(id));
96 }
97
Herb Derby4c35be02018-12-20 14:03:47 -050098 GrGlyph* generateGlyph(const SkGlyph&);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050099
100 friend class GrGlyphCache;
101};
102
103/**
104 * GrGlyphCache manages strikes which are indexed by a SkGlyphCache. These strikes can then be
105 * used to generate individual Glyph Masks.
106 */
107class GrGlyphCache {
108public:
Khushalfa8ff092018-06-06 17:46:38 -0700109 GrGlyphCache(const GrCaps* caps, size_t maxTextureBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500110 ~GrGlyphCache();
111
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500112 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500113
114 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
115 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500116 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500117 // interactions with the cache since the strike was received.
Herbert Derbyc8f6ab382018-11-28 13:00:00 -0500118 sk_sp<GrTextStrike> getStrike(const SkGlyphCache* cache) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500119 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor()));
120 if (!strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500121 strike = this->generateStrike(cache);
122 }
123 return strike;
124 }
125
Timothy Liang91e260f2018-06-15 13:28:35 -0400126 const SkMasks& getMasks() const { return *f565Masks; }
127
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500128 void freeAll();
129
130 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
131
132private:
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500133 sk_sp<GrTextStrike> generateStrike(const SkGlyphCache* cache) {
134 // 'fCache' get the construction ref
135 sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(cache->getDescriptor()));
136 fCache.add(strike.get());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500137 return strike;
138 }
139
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500140 using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500141
142 StrikeHash fCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500143 GrTextStrike* fPreserveStrike;
Timothy Liang91e260f2018-06-15 13:28:35 -0400144 std::unique_ptr<const SkMasks> f565Masks;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500145};
146
147#endif