1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 5 */ 6 7 #ifndef SkStrike_DEFINED 8 #define SkStrike_DEFINED 9 10 #include "include/core/SkFontMetrics.h" 11 #include "include/core/SkFontTypes.h" 12 #include "include/core/SkPaint.h" 13 #include "include/private/SkTHash.h" 14 #include "include/private/SkTemplates.h" 15 #include "src/core/SkArenaAlloc.h" 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" 21 #include <memory> 22 23 /** \class SkGlyphCache 24 25 This class represents a strike: a specific combination of typeface, size, matrix, etc., and 26 holds the glyphs for that strike. Calling any of the getGlyphID... methods will 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. 29 30 The strikes are held in a global list, available to all threads. To interact with one, call 31 either Find{OrCreate}Exclusive(). 32 33 The Find*Exclusive() method returns SkExclusiveStrikePtr, which releases exclusive ownership 34 when they go out of scope. 35 */ 36 class SkStrike final : public SkStrikeInterface { 37 public: 38 SkStrike(const SkDescriptor& desc, 39 std::unique_ptr<SkScalerContext> scaler, 40 const SkFontMetrics&); 41 42 // Return a glyph. Create it if it doesn't exist, and initialize the glyph with metrics and 43 // advances using a scaler. 44 SkGlyph* glyph(SkPackedGlyphID packedID); 45 SkGlyph* glyph(SkGlyphID glyphID); 46 SkGlyph* glyph(SkGlyphID, SkPoint); 47 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 51 // Return a glyph or nullptr if it does not exits in the strike. 52 SkGlyph* glyphOrNull(SkPackedGlyphID id) const; 53 54 const void* prepareImage(SkGlyph* glyph); 55 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); 61 62 // If the path has never been set, then use the scaler context to add the glyph. 63 const SkPath* preparePath(SkGlyph*); 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 68 /** Returns the number of glyphs for this strike. 69 */ 70 unsigned getGlyphCount() const; 71 72 /** Return the number of glyphs currently cached. */ 73 int countCachedGlyphs() const; 74 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, 79 SkGlyph* , SkScalar* array, int* count); 80 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; 89 90 /** Return the vertical metrics for this strike. 91 */ getFontMetrics()92 const SkFontMetrics& getFontMetrics() const { 93 return fFontMetrics; 94 } 95 getMaskFormat()96 SkMask::Format getMaskFormat() const { 97 return fScalerContext->getMaskFormat(); 98 } 99 isSubpixel()100 bool isSubpixel() const { 101 return fIsSubpixel; 102 } 103 104 SkVector rounding() const override; 105 subpixelMask()106 SkIPoint subpixelMask() const override { 107 return SkIPoint::Make((!fIsSubpixel || fAxisAlignment == kY_SkAxisAlignment) ? 0 : ~0, 108 (!fIsSubpixel || fAxisAlignment == kX_SkAxisAlignment) ? 0 : ~0); 109 } 110 111 const SkDescriptor& getDescriptor() const override; 112 113 SkSpan<const SkGlyph*> metrics(SkSpan<const SkGlyphID> glyphIDs, 114 const SkGlyph* results[]); 115 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 122 SkSpan<const SkGlyphPos> prepareForDrawingRemoveEmpty(const SkPackedGlyphID packedGlyphIDs[], 123 const SkPoint positions[], 124 size_t n, 125 int maxDimension, 126 PreparationDetail detail, 127 SkGlyphPos results[]) override; 128 129 void onAboutToExitScope() override; 130 131 /** Return the approx RAM usage for this cache. */ getMemoryUsed()132 size_t getMemoryUsed() const { return fMemoryUsed; } 133 134 void dump() const; 135 getScalerContext()136 SkScalerContext* getScalerContext() const { return fScalerContext.get(); } 137 138 #ifdef SK_DEBUG 139 void forceValidate() const; 140 void validate() const; 141 #else validate()142 void validate() const {} 143 #endif 144 145 class AutoValidate : SkNoncopyable { 146 public: AutoValidate(const SkStrike * cache)147 AutoValidate(const SkStrike* cache) : fCache(cache) { 148 if (fCache) { 149 fCache->validate(); 150 } 151 } ~AutoValidate()152 ~AutoValidate() { 153 if (fCache) { 154 fCache->validate(); 155 } 156 } forget()157 void forget() { 158 fCache = nullptr; 159 } 160 private: 161 const SkStrike* fCache; 162 }; 163 164 private: 165 class GlyphMapHashTraits { 166 public: GetKey(const SkGlyph * glyph)167 static SkPackedGlyphID GetKey(const SkGlyph* glyph) { 168 return glyph->getPackedID(); 169 } Hash(SkPackedGlyphID glyphId)170 static uint32_t Hash(SkPackedGlyphID glyphId) { 171 return glyphId.hash(); 172 } 173 }; 174 175 SkGlyph* makeGlyph(SkPackedGlyphID); 176 177 enum PathDetail { 178 kMetricsOnly, 179 kMetricsAndPath 180 }; 181 182 // internalPrepare will only be called with a mutex already held. 183 SkSpan<const SkGlyph*> internalPrepare( 184 SkSpan<const SkGlyphID> glyphIDs, 185 PathDetail pathDetail, 186 const SkGlyph** results); 187 188 const SkAutoDescriptor fDesc; 189 const std::unique_ptr<SkScalerContext> fScalerContext; 190 SkFontMetrics fFontMetrics; 191 192 // Map from a combined GlyphID and sub-pixel position to a SkGlyph*. 193 // The actual glyph is stored in the fAlloc. This structure provides an 194 // unchanging pointer as long as the strike is alive. 195 SkTHashTable<SkGlyph*, SkPackedGlyphID, GlyphMapHashTraits> fGlyphMap; 196 197 // so we don't grow our arrays a lot 198 static constexpr size_t kMinGlyphCount = 8; 199 static constexpr size_t kMinGlyphImageSize = 16 /* height */ * 8 /* width */; 200 static constexpr size_t kMinAllocAmount = kMinGlyphImageSize * kMinGlyphCount; 201 202 SkArenaAlloc fAlloc {kMinAllocAmount}; 203 204 // Tracks (approx) how much ram is tied-up in this strike. 205 size_t fMemoryUsed; 206 207 const bool fIsSubpixel; 208 const SkAxisAlignment fAxisAlignment; 209 }; 210 211 #endif // SkStrike_DEFINED 212