blob: 0874ffc50b9495d2a89a68bbb1809ee414066b3e [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
Herb Derby081e6f32019-01-16 13:46:02 -05008#ifndef GrStrikeCache_DEFINED
9#define GrStrikeCache_DEFINED
Robert Phillipsc4039ea2018-03-01 11:36:45 -050010
11#include "GrDrawOpAtlas.h"
12#include "GrGlyph.h"
13#include "SkArenaAlloc.h"
Timothy Liang91e260f2018-06-15 13:28:35 -040014#include "SkMasks.h"
Herb Derby5fd955e2019-01-16 11:23:29 -050015#include "SkStrike.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050016#include "SkTDynamicHash.h"
17
Robert Phillipsc4039ea2018-03-01 11:36:45 -050018class GrAtlasManager;
19class GrGpu;
Herb Derby081e6f32019-01-16 13:46:02 -050020class GrStrikeCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050021
22/**
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050023 * The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
Herb Derby081e6f32019-01-16 13:46:02 -050024 * is indexed by a PackedID and SkStrike. The SkStrike is what actually creates the mask.
25 * The GrTextStrike may outlive the generating SkStrike. However, it retains a copy
26 * of it's SkDescriptor as a key to access (or regenerate) the SkStrike. GrTextStrikes are
27 * created by and owned by a GrStrikeCache.
Robert Phillipsc4039ea2018-03-01 11:36:45 -050028 */
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) {
Herb Derby5a3fdee2018-12-20 14:47:03 -050034 GrGlyph* glyph = fCache.find(skGlyph.getPackedID());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050035 if (!glyph) {
Herb Derby4c35be02018-12-20 14:03:47 -050036 glyph = this->generateGlyph(skGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050037 }
38 return glyph;
39 }
40
41 // This variant of the above function is called by GrAtlasTextOp. At this point, it is possible
42 // that the maskformat of the glyph differs from what we expect. In these cases we will just
43 // draw a clear square.
44 // skbug:4143 crbug:510931
Herb Derby5a3fdee2018-12-20 14:47:03 -050045 GrGlyph* getGlyph(SkPackedGlyphID packed,
Herb Derby5fd955e2019-01-16 11:23:29 -050046 SkStrike* cache) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050047 GrGlyph* glyph = fCache.find(packed);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050048 if (!glyph) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050049 // We could return this to the caller, but in practice it adds code complexity for
50 // potentially little benefit(ie, if the glyph is not in our font cache, then its not
51 // in the atlas and we're going to be doing a texture upload anyways).
52 const SkGlyph& skGlyph = GrToSkGlyph(cache, packed);
Herb Derby4c35be02018-12-20 14:03:47 -050053 glyph = this->generateGlyph(skGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050054 }
55 return glyph;
56 }
57
58 // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
59 // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
60 // happen.
61 // TODO we can handle some of these cases if we really want to, but the long term solution is to
62 // get the actual glyph image itself when we get the glyph metrics.
Robert Phillipsd2e9f762018-03-07 11:54:37 -050063 GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
Herb Derby081e6f32019-01-16 13:46:02 -050064 GrStrikeCache*, GrAtlasManager*, GrGlyph*,
Herb Derby5fd955e2019-01-16 11:23:29 -050065 SkStrike*, GrMaskFormat expectedMaskFormat,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050066 bool isScaledGlyph);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050067
68 // testing
69 int countGlyphs() const { return fCache.count(); }
70
71 // remove any references to this plot
72 void removeID(GrDrawOpAtlas::AtlasID);
73
74 // If a TextStrike is abandoned by the cache, then the caller must get a new strike
75 bool isAbandoned() const { return fIsAbandoned; }
76
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050077 static const SkDescriptor& GetKey(const GrTextStrike& strike) {
78 return *strike.fFontScalerKey.getDesc();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050079 }
80
81 static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
82
83private:
Herb Derby5a3fdee2018-12-20 14:47:03 -050084 SkTDynamicHash<GrGlyph, SkPackedGlyphID> fCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050085 SkAutoDescriptor fFontScalerKey;
Herb Derbyb03e0242018-12-20 13:19:44 -050086 SkArenaAlloc fAlloc{512};
Robert Phillipsc4039ea2018-03-01 11:36:45 -050087
Herbert Derby83ea5222018-11-28 14:39:29 -050088 int fAtlasedGlyphs{0};
89 bool fIsAbandoned{false};
Robert Phillipsc4039ea2018-03-01 11:36:45 -050090
Herb Derby5fd955e2019-01-16 11:23:29 -050091 static const SkGlyph& GrToSkGlyph(SkStrike* cache, SkPackedGlyphID id) {
Herb Derby5a3fdee2018-12-20 14:47:03 -050092 return cache->getGlyphIDMetrics(id.code(), id.getSubXFixed(), id.getSubYFixed());
Robert Phillipsc4039ea2018-03-01 11:36:45 -050093 }
94
Herb Derby4c35be02018-12-20 14:03:47 -050095 GrGlyph* generateGlyph(const SkGlyph&);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050096
Herb Derby081e6f32019-01-16 13:46:02 -050097 friend class GrStrikeCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050098};
99
100/**
Herb Derby081e6f32019-01-16 13:46:02 -0500101 * GrStrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500102 * used to generate individual Glyph Masks.
103 */
Herb Derby081e6f32019-01-16 13:46:02 -0500104class GrStrikeCache {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500105public:
Herb Derby081e6f32019-01-16 13:46:02 -0500106 GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes);
107 ~GrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500108
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500109 void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500110
111 // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
112 // another client of the cache may cause the strike to be purged while it is still reffed.
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500113 // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500114 // interactions with the cache since the strike was received.
Herb Derby5fd955e2019-01-16 11:23:29 -0500115 sk_sp<GrTextStrike> getStrike(const SkStrike* cache) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500116 sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor()));
117 if (!strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500118 strike = this->generateStrike(cache);
119 }
120 return strike;
121 }
122
Timothy Liang91e260f2018-06-15 13:28:35 -0400123 const SkMasks& getMasks() const { return *f565Masks; }
124
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500125 void freeAll();
126
127 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
128
129private:
Herb Derby5fd955e2019-01-16 11:23:29 -0500130 sk_sp<GrTextStrike> generateStrike(const SkStrike* cache) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500131 // 'fCache' get the construction ref
132 sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(cache->getDescriptor()));
133 fCache.add(strike.get());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500134 return strike;
135 }
136
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500137 using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500138
139 StrikeHash fCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500140 GrTextStrike* fPreserveStrike;
Timothy Liang91e260f2018-06-15 13:28:35 -0400141 std::unique_ptr<const SkMasks> f565Masks;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500142};
143
Herb Derby081e6f32019-01-16 13:46:02 -0500144#endif // GrStrikeCache_DEFINED