epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 7 | #ifndef SkStrike_DEFINED |
| 8 | #define SkStrike_DEFINED |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
Herb Derby | 7c5a806 | 2017-01-20 11:59:03 -0500 | [diff] [blame] | 10 | #include "SkArenaAlloc.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkDescriptor.h" |
Mike Reed | 77f94ea | 2019-01-22 16:30:40 -0500 | [diff] [blame] | 12 | #include "SkFontMetrics.h" |
| 13 | #include "SkFontTypes.h" |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 14 | #include "SkGlyph.h" |
Herbert Derby | a0f5935 | 2018-11-05 11:48:00 -0500 | [diff] [blame] | 15 | #include "SkGlyphRunPainter.h" |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 16 | #include "SkPaint.h" |
herb | c71239b | 2015-07-21 15:36:25 -0700 | [diff] [blame] | 17 | #include "SkTHash.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkScalerContext.h" |
Herb Derby | 0f27b5e | 2019-03-04 11:20:28 -0500 | [diff] [blame] | 19 | #include "SkStrikeInterface.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkTemplates.h" |
bungeman | 520ced6 | 2016-10-18 08:03:42 -0400 | [diff] [blame] | 21 | #include <memory> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | /** \class SkGlyphCache |
| 24 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 25 | This class represents a strike: a specific combination of typeface, size, matrix, etc., and |
Mike Reed | fdb876d | 2019-02-01 09:55:20 -0500 | [diff] [blame] | 26 | holds the glyphs for that strike. Calling any of the getGlyphID... methods will |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 27 | return the requested glyph, either instantly if it is already cached, or by first generating |
| 28 | it and then adding it to the strike. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 30 | The strikes are held in a global list, available to all threads. To interact with one, call |
Herb Derby | 82a36ac | 2018-03-09 16:40:48 -0500 | [diff] [blame] | 31 | either Find{OrCreate}Exclusive(). |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 32 | |
| 33 | The Find*Exclusive() method returns SkExclusiveStrikePtr, which releases exclusive ownership |
| 34 | when they go out of scope. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | */ |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 36 | class SkStrike final : public SkStrikeInterface { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | public: |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 38 | SkStrike(const SkDescriptor& desc, |
| 39 | std::unique_ptr<SkScalerContext> scaler, |
| 40 | const SkFontMetrics&); |
Herb Derby | dce19a7 | 2018-04-18 16:02:17 -0400 | [diff] [blame] | 41 | |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 42 | /** Return true if glyph is cached. */ |
| 43 | bool isGlyphCached(SkGlyphID glyphID, SkFixed x, SkFixed y) const; |
| 44 | |
| 45 | /** Return a glyph that has no information if it is not already filled out. */ |
| 46 | SkGlyph* getRawGlyphByID(SkPackedGlyphID); |
| 47 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 48 | /** Returns a glyph with valid fAdvance and fDevKern fields. The remaining fields may be |
Mike Reed | fdb876d | 2019-02-01 09:55:20 -0500 | [diff] [blame] | 49 | valid, but that is not guaranteed. If you require those, call getGlyphIDMetrics instead. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | */ |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 51 | const SkGlyph& getGlyphIDAdvance(SkGlyphID); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 52 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 53 | /** Returns a glyph with all fields valid except fImage and fPath, which may be null. If they |
| 54 | are null, call findImage or findPath for those. If they are not null, then they are valid. |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 55 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 56 | This call is potentially slower than the matching ...Advance call. If you only need the |
| 57 | fAdvance/fDevKern fields, call those instead. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | */ |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 59 | const SkGlyph& getGlyphIDMetrics(SkGlyphID); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 60 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 61 | /** These are variants that take the device position of the glyph. Call these only if you are |
| 62 | drawing in subpixel mode. Passing 0, 0 is effectively the same as calling the variants |
| 63 | w/o the extra params, though a tiny bit slower. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | const SkGlyph& getGlyphIDMetrics(uint16_t, SkFixed x, SkFixed y); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 66 | |
Herb Derby | ed55419 | 2018-06-22 17:05:04 -0400 | [diff] [blame] | 67 | void getAdvances(SkSpan<const SkGlyphID>, SkPoint[]); |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 68 | |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 69 | /** Returns the number of glyphs for this strike. |
| 70 | */ |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 71 | unsigned getGlyphCount() const; |
| 72 | |
| 73 | /** Return the number of glyphs currently cached. */ |
| 74 | int countCachedGlyphs() const; |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 75 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 76 | /** Return the image associated with the glyph. If it has not been generated this will |
| 77 | trigger that. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | */ |
| 79 | const void* findImage(const SkGlyph&); |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 80 | |
Khushal | 4010c79 | 2018-06-13 09:44:23 -0700 | [diff] [blame] | 81 | /** Initializes the image associated with the glyph with |data|. |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 82 | */ |
Khushal | 4010c79 | 2018-06-13 09:44:23 -0700 | [diff] [blame] | 83 | void initializeImage(const volatile void* data, size_t size, SkGlyph*); |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 84 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 85 | /** If the advance axis intersects the glyph's path, append the positions scaled and offset |
| 86 | to the array (if non-null), and set the count to the updated array length. |
| 87 | */ |
| 88 | void findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos, |
| 89 | bool yAxis, SkGlyph* , SkScalar* array, int* count); |
| 90 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 91 | /** Return the Path associated with the glyph. If it has not been generated this will trigger |
| 92 | that. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | */ |
| 94 | const SkPath* findPath(const SkGlyph&); |
| 95 | |
Khushal | 4010c79 | 2018-06-13 09:44:23 -0700 | [diff] [blame] | 96 | /** Initializes the path associated with the glyph with |data|. Returns false if |
| 97 | * data is invalid. |
Khushal | 51371a4 | 2018-05-17 10:41:40 -0700 | [diff] [blame] | 98 | */ |
| 99 | bool initializePath(SkGlyph*, const volatile void* data, size_t size); |
| 100 | |
Khushal | 8523b6b | 2018-06-12 11:26:17 -0700 | [diff] [blame] | 101 | /** Fallback glyphs used during font remoting if the original glyph can't be found. |
| 102 | */ |
| 103 | bool belongsToCache(const SkGlyph* glyph) const; |
| 104 | /** Find any glyph in this cache with the given ID, regardless of subpixel positioning. |
| 105 | * If set and present, skip over the glyph with vetoID. |
| 106 | */ |
| 107 | const SkGlyph* getCachedGlyphAnySubPix(SkGlyphID, |
| 108 | SkPackedGlyphID vetoID = SkPackedGlyphID()) const; |
| 109 | void initializeGlyphFromFallback(SkGlyph* glyph, const SkGlyph&); |
| 110 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | /** Return the vertical metrics for this strike. |
| 112 | */ |
Mike Reed | cb6f53e | 2018-11-06 12:44:54 -0500 | [diff] [blame] | 113 | const SkFontMetrics& getFontMetrics() const { |
reed@google.com | 0a01f5a | 2013-05-08 14:19:08 +0000 | [diff] [blame] | 114 | return fFontMetrics; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 116 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 117 | SkMask::Format getMaskFormat() const { |
| 118 | return fScalerContext->getMaskFormat(); |
| 119 | } |
| 120 | |
reed@google.com | abf00aa | 2012-01-03 19:43:20 +0000 | [diff] [blame] | 121 | bool isSubpixel() const { |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 122 | return fIsSubpixel; |
reed@google.com | abf00aa | 2012-01-03 19:43:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 125 | SkVector rounding() const override; |
| 126 | |
| 127 | const SkGlyph& getGlyphMetrics(SkGlyphID glyphID, SkPoint position) override; |
| 128 | |
Herb Derby | 5dd7f84 | 2019-04-16 11:20:38 -0400 | [diff] [blame] | 129 | void generatePath(const SkGlyph& glyph) override; |
Herb Derby | 2d4f7cb | 2018-11-27 12:37:50 -0500 | [diff] [blame] | 130 | |
Herb Derby | 9685057 | 2019-02-11 13:02:18 -0500 | [diff] [blame] | 131 | const SkDescriptor& getDescriptor() const override; |
| 132 | |
Herb Derby | a4c6487 | 2019-02-13 11:46:19 -0500 | [diff] [blame] | 133 | SkStrikeSpec strikeSpec() const override { |
| 134 | return SkStrikeSpec{this->getDescriptor(), |
| 135 | *this->getScalerContext()->getTypeface(), |
| 136 | this->getScalerContext()->getEffects()}; |
| 137 | } |
| 138 | |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 139 | SkSpan<const SkGlyphPos> prepareForDrawing(const SkGlyphID glyphIDs[], |
| 140 | const SkPoint positions[], |
| 141 | size_t n, |
| 142 | int maxDimension, |
Herb Derby | 1c550f7 | 2019-04-23 11:04:29 -0400 | [diff] [blame^] | 143 | PreparationDetail detail, |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 144 | SkGlyphPos results[]) override; |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 145 | |
Herb Derby | 4925364 | 2019-02-11 14:20:31 -0500 | [diff] [blame] | 146 | void onAboutToExitScope() override; |
Herb Derby | 9685057 | 2019-02-11 13:02:18 -0500 | [diff] [blame] | 147 | |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 148 | /** Return the approx RAM usage for this cache. */ |
| 149 | size_t getMemoryUsed() const { return fMemoryUsed; } |
| 150 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 151 | void dump() const; |
| 152 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 153 | SkScalerContext* getScalerContext() const { return fScalerContext.get(); } |
djsollen@google.com | 15eeca0 | 2012-06-01 12:52:26 +0000 | [diff] [blame] | 154 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 155 | #ifdef SK_DEBUG |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 156 | void forceValidate() const; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 157 | void validate() const; |
| 158 | #else |
| 159 | void validate() const {} |
| 160 | #endif |
| 161 | |
| 162 | class AutoValidate : SkNoncopyable { |
| 163 | public: |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 164 | AutoValidate(const SkStrike* cache) : fCache(cache) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 165 | if (fCache) { |
| 166 | fCache->validate(); |
| 167 | } |
| 168 | } |
| 169 | ~AutoValidate() { |
| 170 | if (fCache) { |
| 171 | fCache->validate(); |
| 172 | } |
| 173 | } |
| 174 | void forget() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 175 | fCache = nullptr; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 176 | } |
| 177 | private: |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 178 | const SkStrike* fCache; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 179 | }; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 180 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | private: |
mtklein | 8d57faf | 2015-10-06 07:00:45 -0700 | [diff] [blame] | 182 | enum MetricsType { |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 183 | kNothing_MetricsType, |
mtklein | 8d57faf | 2015-10-06 07:00:45 -0700 | [diff] [blame] | 184 | kJustAdvance_MetricsType, |
| 185 | kFull_MetricsType |
| 186 | }; |
| 187 | |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 188 | enum { |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 189 | kHashBits = 8, |
| 190 | kHashCount = 1 << kHashBits, |
| 191 | kHashMask = kHashCount - 1 |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 194 | // Return the SkGlyph* associated with MakeID. The id parameter is the |
| 195 | // combined glyph/x/y id generated by MakeID. If it is just a glyph id |
Herb Derby | 883a991 | 2019-02-12 16:43:43 -0500 | [diff] [blame] | 196 | // then x and y are assumed to be zero. Limit the amount of work using type. |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 197 | SkGlyph* lookupByPackedGlyphID(SkPackedGlyphID packedGlyphID, MetricsType type); |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 198 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 199 | static void OffsetResults(const SkGlyph::Intercept* intercept, SkScalar scale, |
| 200 | SkScalar xPos, SkScalar* array, int* count); |
| 201 | static void AddInterval(SkScalar val, SkGlyph::Intercept* intercept); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 202 | static void AddPoints(const SkPoint* pts, int ptCount, const SkScalar bounds[2], |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 203 | bool yAxis, SkGlyph::Intercept* intercept); |
| 204 | static void AddLine(const SkPoint pts[2], SkScalar axis, bool yAxis, |
| 205 | SkGlyph::Intercept* intercept); |
| 206 | static void AddQuad(const SkPoint pts[2], SkScalar axis, bool yAxis, |
| 207 | SkGlyph::Intercept* intercept); |
| 208 | static void AddCubic(const SkPoint pts[3], SkScalar axis, bool yAxis, |
| 209 | SkGlyph::Intercept* intercept); |
| 210 | static const SkGlyph::Intercept* MatchBounds(const SkGlyph* glyph, |
| 211 | const SkScalar bounds[2]); |
| 212 | |
Herb Derby | dce19a7 | 2018-04-18 16:02:17 -0400 | [diff] [blame] | 213 | const SkAutoDescriptor fDesc; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 214 | const std::unique_ptr<SkScalerContext> fScalerContext; |
Mike Reed | cb6f53e | 2018-11-06 12:44:54 -0500 | [diff] [blame] | 215 | SkFontMetrics fFontMetrics; |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 216 | |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 217 | class GlyphMapHashTraits { |
| 218 | public: |
| 219 | static SkPackedGlyphID GetKey(const SkGlyph* glyph) { |
| 220 | return glyph->getPackedID(); |
| 221 | } |
| 222 | static uint32_t Hash(SkPackedGlyphID glyphId) { |
| 223 | return glyphId.hash(); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | // Map from a combined GlyphID and sub-pixel position to a SkGlyph*. |
| 228 | // The actual glyph is stored in the fAlloc. This structure provides an |
| 229 | // unchanging pointer as long as the cache is alive. |
| 230 | SkTHashTable<SkGlyph*, SkPackedGlyphID, GlyphMapHashTraits> fGlyphMap; |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 231 | |
Herb Derby | 7c5a806 | 2017-01-20 11:59:03 -0500 | [diff] [blame] | 232 | // so we don't grow our arrays a lot |
Herb Derby | 80d22ea | 2017-01-24 11:01:59 -0500 | [diff] [blame] | 233 | static constexpr size_t kMinGlyphCount = 8; |
| 234 | static constexpr size_t kMinGlyphImageSize = 16 /* height */ * 8 /* width */; |
| 235 | static constexpr size_t kMinAllocAmount = kMinGlyphImageSize * kMinGlyphCount; |
Herb Derby | 7c5a806 | 2017-01-20 11:59:03 -0500 | [diff] [blame] | 236 | |
Herb Derby | 80d22ea | 2017-01-24 11:01:59 -0500 | [diff] [blame] | 237 | SkArenaAlloc fAlloc {kMinAllocAmount}; |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | // used to track (approx) how much ram is tied-up in this cache |
Herb Derby | 80d22ea | 2017-01-24 11:01:59 -0500 | [diff] [blame] | 240 | size_t fMemoryUsed; |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 241 | |
| 242 | const bool fIsSubpixel; |
| 243 | const SkAxisAlignment fAxisAlignment; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 246 | #endif // SkStrike_DEFINED |