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