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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkFontMetrics.h" |
| 11 | #include "include/core/SkFontTypes.h" |
| 12 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/private/SkTHash.h" |
| 14 | #include "include/private/SkTemplates.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 15 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkDescriptor.h" |
| 17 | #include "src/core/SkGlyph.h" |
| 18 | #include "src/core/SkGlyphRunPainter.h" |
| 19 | #include "src/core/SkScalerContext.h" |
| 20 | #include "src/core/SkStrikeInterface.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 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 45 | // Return a glyph. Create it if it doesn't exist, and initialize the glyph with metrics and |
| 46 | // advances. |
Herb Derby | 3721688 | 2019-06-13 17:24:24 -0400 | [diff] [blame] | 47 | SkGlyph* glyph(SkPackedGlyphID packedID); |
| 48 | SkGlyph* glyph(SkGlyphID glyphID); |
Herb Derby | 511dcfc | 2019-06-24 12:58:41 -0400 | [diff] [blame^] | 49 | SkGlyph* glyphFromPrototype(const SkGlyphPrototype& p, void* image = nullptr); |
| 50 | SkGlyph* glyph(SkGlyphID, SkPoint); |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 51 | |
Herb Derby | f0e7581 | 2019-06-12 11:14:50 -0400 | [diff] [blame] | 52 | // Return a glyph or nullptr if it does not exits in the strike. |
| 53 | SkGlyph* glyphOrNull(SkPackedGlyphID id) const; |
| 54 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 55 | // Return a glyph. Create it if it doesn't exist, but zero the data. |
| 56 | SkGlyph* uninitializedGlyph(SkPackedGlyphID id); |
| 57 | |
Herb Derby | f0e7581 | 2019-06-12 11:14:50 -0400 | [diff] [blame] | 58 | // If the path has never been set, then use the scaler context to add the glyph. |
| 59 | const SkPath* preparePath(SkGlyph*) override; |
| 60 | |
| 61 | // If the path has never been set, then add a path to glyph. |
| 62 | const SkPath* preparePath(SkGlyph* glyph, const SkPath* path); |
| 63 | |
Herb Derby | d2a1887 | 2019-06-18 17:07:39 -0400 | [diff] [blame] | 64 | SkSpan<SkPoint> getAdvances(SkSpan<const SkGlyphID>, SkPoint[]); |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 65 | |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 66 | /** Returns the number of glyphs for this strike. |
| 67 | */ |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 68 | unsigned getGlyphCount() const; |
| 69 | |
| 70 | /** Return the number of glyphs currently cached. */ |
| 71 | int countCachedGlyphs() const; |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 72 | |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 73 | /** Return the image associated with the glyph. If it has not been generated this will |
| 74 | trigger that. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | */ |
| 76 | const void* findImage(const SkGlyph&); |
herb | 0c752f4 | 2015-06-24 13:06:21 -0700 | [diff] [blame] | 77 | |
Khushal | 4010c79 | 2018-06-13 09:44:23 -0700 | [diff] [blame] | 78 | /** Initializes the image associated with the glyph with |data|. |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 79 | */ |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 80 | void initializeImage(const void* data, size_t size, SkGlyph*); |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 81 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 82 | /** If the advance axis intersects the glyph's path, append the positions scaled and offset |
| 83 | to the array (if non-null), and set the count to the updated array length. |
| 84 | */ |
| 85 | void findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos, |
Herb Derby | 6a07591 | 2019-05-29 17:02:49 -0400 | [diff] [blame] | 86 | SkGlyph* , SkScalar* array, int* count); |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 87 | |
Khushal | 8523b6b | 2018-06-12 11:26:17 -0700 | [diff] [blame] | 88 | /** Fallback glyphs used during font remoting if the original glyph can't be found. |
| 89 | */ |
| 90 | bool belongsToCache(const SkGlyph* glyph) const; |
| 91 | /** Find any glyph in this cache with the given ID, regardless of subpixel positioning. |
| 92 | * If set and present, skip over the glyph with vetoID. |
| 93 | */ |
| 94 | const SkGlyph* getCachedGlyphAnySubPix(SkGlyphID, |
| 95 | SkPackedGlyphID vetoID = SkPackedGlyphID()) const; |
| 96 | void initializeGlyphFromFallback(SkGlyph* glyph, const SkGlyph&); |
| 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | /** Return the vertical metrics for this strike. |
| 99 | */ |
Mike Reed | cb6f53e | 2018-11-06 12:44:54 -0500 | [diff] [blame] | 100 | const SkFontMetrics& getFontMetrics() const { |
reed@google.com | 0a01f5a | 2013-05-08 14:19:08 +0000 | [diff] [blame] | 101 | return fFontMetrics; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 103 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 104 | SkMask::Format getMaskFormat() const { |
| 105 | return fScalerContext->getMaskFormat(); |
| 106 | } |
| 107 | |
reed@google.com | abf00aa | 2012-01-03 19:43:20 +0000 | [diff] [blame] | 108 | bool isSubpixel() const { |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 109 | return fIsSubpixel; |
reed@google.com | abf00aa | 2012-01-03 19:43:20 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 112 | SkVector rounding() const override; |
| 113 | |
| 114 | const SkGlyph& getGlyphMetrics(SkGlyphID glyphID, SkPoint position) override; |
| 115 | |
Herb Derby | 9685057 | 2019-02-11 13:02:18 -0500 | [diff] [blame] | 116 | const SkDescriptor& getDescriptor() const override; |
| 117 | |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 118 | SkSpan<const SkGlyphPos> prepareForDrawing(const SkGlyphID glyphIDs[], |
| 119 | const SkPoint positions[], |
| 120 | size_t n, |
| 121 | int maxDimension, |
Herb Derby | 1c550f7 | 2019-04-23 11:04:29 -0400 | [diff] [blame] | 122 | PreparationDetail detail, |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 123 | SkGlyphPos results[]) override; |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 124 | |
Herb Derby | 4925364 | 2019-02-11 14:20:31 -0500 | [diff] [blame] | 125 | void onAboutToExitScope() override; |
Herb Derby | 9685057 | 2019-02-11 13:02:18 -0500 | [diff] [blame] | 126 | |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 127 | /** Return the approx RAM usage for this cache. */ |
| 128 | size_t getMemoryUsed() const { return fMemoryUsed; } |
| 129 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 130 | void dump() const; |
| 131 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 132 | SkScalerContext* getScalerContext() const { return fScalerContext.get(); } |
djsollen@google.com | 15eeca0 | 2012-06-01 12:52:26 +0000 | [diff] [blame] | 133 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 134 | #ifdef SK_DEBUG |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 135 | void forceValidate() const; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 136 | void validate() const; |
| 137 | #else |
| 138 | void validate() const {} |
| 139 | #endif |
| 140 | |
| 141 | class AutoValidate : SkNoncopyable { |
| 142 | public: |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 143 | AutoValidate(const SkStrike* cache) : fCache(cache) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 144 | if (fCache) { |
| 145 | fCache->validate(); |
| 146 | } |
| 147 | } |
| 148 | ~AutoValidate() { |
| 149 | if (fCache) { |
| 150 | fCache->validate(); |
| 151 | } |
| 152 | } |
| 153 | void forget() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 154 | fCache = nullptr; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 155 | } |
| 156 | private: |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 157 | const SkStrike* fCache; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 158 | }; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | private: |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 161 | class GlyphMapHashTraits { |
| 162 | public: |
| 163 | static SkPackedGlyphID GetKey(const SkGlyph* glyph) { |
| 164 | return glyph->getPackedID(); |
| 165 | } |
| 166 | static uint32_t Hash(SkPackedGlyphID glyphId) { |
| 167 | return glyphId.hash(); |
| 168 | } |
| 169 | }; |
| 170 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 171 | SkGlyph* makeGlyph(SkPackedGlyphID); |
| 172 | |
Herb Derby | d2a1887 | 2019-06-18 17:07:39 -0400 | [diff] [blame] | 173 | // Metrics will hold a mutex while doing its work. This is one of the few places that will |
| 174 | // need a mutex. |
| 175 | SkSpan<const SkGlyph*> metrics(SkSpan<const SkGlyphID>glyphIDs, const SkGlyph* result[]); |
| 176 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 177 | const SkAutoDescriptor fDesc; |
| 178 | const std::unique_ptr<SkScalerContext> fScalerContext; |
| 179 | SkFontMetrics fFontMetrics; |
| 180 | |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 181 | // Map from a combined GlyphID and sub-pixel position to a SkGlyph*. |
| 182 | // The actual glyph is stored in the fAlloc. This structure provides an |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 183 | // unchanging pointer as long as the strike is alive. |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 184 | SkTHashTable<SkGlyph*, SkPackedGlyphID, GlyphMapHashTraits> fGlyphMap; |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 185 | |
Herb Derby | 7c5a806 | 2017-01-20 11:59:03 -0500 | [diff] [blame] | 186 | // so we don't grow our arrays a lot |
Herb Derby | 80d22ea | 2017-01-24 11:01:59 -0500 | [diff] [blame] | 187 | static constexpr size_t kMinGlyphCount = 8; |
| 188 | static constexpr size_t kMinGlyphImageSize = 16 /* height */ * 8 /* width */; |
| 189 | static constexpr size_t kMinAllocAmount = kMinGlyphImageSize * kMinGlyphCount; |
Herb Derby | 7c5a806 | 2017-01-20 11:59:03 -0500 | [diff] [blame] | 190 | |
Herb Derby | 80d22ea | 2017-01-24 11:01:59 -0500 | [diff] [blame] | 191 | SkArenaAlloc fAlloc {kMinAllocAmount}; |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 192 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 193 | // Tracks (approx) how much ram is tied-up in this strike. |
Herb Derby | 80d22ea | 2017-01-24 11:01:59 -0500 | [diff] [blame] | 194 | size_t fMemoryUsed; |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 195 | |
| 196 | const bool fIsSubpixel; |
| 197 | const SkAxisAlignment fAxisAlignment; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 200 | #endif // SkStrike_DEFINED |