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