SkStrike.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef SkStrike_DEFINED
  7. #define SkStrike_DEFINED
  8. #include "include/core/SkFontMetrics.h"
  9. #include "include/core/SkFontTypes.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/private/SkTHash.h"
  12. #include "include/private/SkTemplates.h"
  13. #include "src/core/SkArenaAlloc.h"
  14. #include "src/core/SkDescriptor.h"
  15. #include "src/core/SkGlyph.h"
  16. #include "src/core/SkGlyphRunPainter.h"
  17. #include "src/core/SkScalerContext.h"
  18. #include "src/core/SkStrikeInterface.h"
  19. #include <memory>
  20. /** \class SkGlyphCache
  21. This class represents a strike: a specific combination of typeface, size, matrix, etc., and
  22. holds the glyphs for that strike. Calling any of the getGlyphID... methods will
  23. return the requested glyph, either instantly if it is already cached, or by first generating
  24. it and then adding it to the strike.
  25. The strikes are held in a global list, available to all threads. To interact with one, call
  26. either Find{OrCreate}Exclusive().
  27. The Find*Exclusive() method returns SkExclusiveStrikePtr, which releases exclusive ownership
  28. when they go out of scope.
  29. */
  30. class SkStrike final : public SkStrikeInterface {
  31. public:
  32. SkStrike(const SkDescriptor& desc,
  33. std::unique_ptr<SkScalerContext> scaler,
  34. const SkFontMetrics&);
  35. // Return a glyph. Create it if it doesn't exist, and initialize the glyph with metrics and
  36. // advances using a scaler.
  37. SkGlyph* glyph(SkPackedGlyphID packedID);
  38. SkGlyph* glyph(SkGlyphID glyphID);
  39. SkGlyph* glyph(SkGlyphID, SkPoint);
  40. // Return a glyph. Create it if it doesn't exist, and initialize with the prototype.
  41. SkGlyph* glyphFromPrototype(const SkGlyphPrototype& p, void* image = nullptr);
  42. // Return a glyph or nullptr if it does not exits in the strike.
  43. SkGlyph* glyphOrNull(SkPackedGlyphID id) const;
  44. const void* prepareImage(SkGlyph* glyph);
  45. // Lookup (or create if needed) the toGlyph using toID. If that glyph is not initialized with
  46. // an image, then use the information in from to initialize the width, height top, left,
  47. // format and image of the toGlyph. This is mainly used preserving the glyph if it was
  48. // created by a search of desperation.
  49. SkGlyph* mergeGlyphAndImage(SkPackedGlyphID toID, const SkGlyph& from);
  50. // If the path has never been set, then use the scaler context to add the glyph.
  51. const SkPath* preparePath(SkGlyph*);
  52. // If the path has never been set, then add a path to glyph.
  53. const SkPath* preparePath(SkGlyph* glyph, const SkPath* path);
  54. /** Returns the number of glyphs for this strike.
  55. */
  56. unsigned getGlyphCount() const;
  57. /** Return the number of glyphs currently cached. */
  58. int countCachedGlyphs() const;
  59. /** If the advance axis intersects the glyph's path, append the positions scaled and offset
  60. to the array (if non-null), and set the count to the updated array length.
  61. */
  62. void findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos,
  63. SkGlyph* , SkScalar* array, int* count);
  64. /** Fallback glyphs used during font remoting if the original glyph can't be found.
  65. */
  66. bool belongsToCache(const SkGlyph* glyph) const;
  67. /** Find any glyph in this cache with the given ID, regardless of subpixel positioning.
  68. * If set and present, skip over the glyph with vetoID.
  69. */
  70. const SkGlyph* getCachedGlyphAnySubPix(SkGlyphID,
  71. SkPackedGlyphID vetoID = SkPackedGlyphID()) const;
  72. /** Return the vertical metrics for this strike.
  73. */
  74. const SkFontMetrics& getFontMetrics() const {
  75. return fFontMetrics;
  76. }
  77. SkMask::Format getMaskFormat() const {
  78. return fScalerContext->getMaskFormat();
  79. }
  80. bool isSubpixel() const {
  81. return fIsSubpixel;
  82. }
  83. SkVector rounding() const override;
  84. SkIPoint subpixelMask() const override {
  85. return SkIPoint::Make((!fIsSubpixel || fAxisAlignment == kY_SkAxisAlignment) ? 0 : ~0,
  86. (!fIsSubpixel || fAxisAlignment == kX_SkAxisAlignment) ? 0 : ~0);
  87. }
  88. const SkDescriptor& getDescriptor() const override;
  89. SkSpan<const SkGlyph*> metrics(SkSpan<const SkGlyphID> glyphIDs,
  90. const SkGlyph* results[]);
  91. SkSpan<const SkGlyph*> preparePaths(SkSpan<const SkGlyphID> glyphIDs,
  92. const SkGlyph* results[]);
  93. SkSpan<const SkGlyph*> prepareImages(SkSpan<const SkPackedGlyphID> glyphIDs,
  94. const SkGlyph* results[]);
  95. SkSpan<const SkGlyphPos> prepareForDrawing(const SkPackedGlyphID packedGlyphIDs[],
  96. const SkPoint positions[],
  97. size_t n,
  98. int maxDimension,
  99. PreparationDetail detail,
  100. SkGlyphPos results[]) override;
  101. void onAboutToExitScope() override;
  102. /** Return the approx RAM usage for this cache. */
  103. size_t getMemoryUsed() const { return fMemoryUsed; }
  104. void dump() const;
  105. SkScalerContext* getScalerContext() const { return fScalerContext.get(); }
  106. #ifdef SK_DEBUG
  107. void forceValidate() const;
  108. void validate() const;
  109. #else
  110. void validate() const {}
  111. #endif
  112. class AutoValidate : SkNoncopyable {
  113. public:
  114. AutoValidate(const SkStrike* cache) : fCache(cache) {
  115. if (fCache) {
  116. fCache->validate();
  117. }
  118. }
  119. ~AutoValidate() {
  120. if (fCache) {
  121. fCache->validate();
  122. }
  123. }
  124. void forget() {
  125. fCache = nullptr;
  126. }
  127. private:
  128. const SkStrike* fCache;
  129. };
  130. private:
  131. class GlyphMapHashTraits {
  132. public:
  133. static SkPackedGlyphID GetKey(const SkGlyph* glyph) {
  134. return glyph->getPackedID();
  135. }
  136. static uint32_t Hash(SkPackedGlyphID glyphId) {
  137. return glyphId.hash();
  138. }
  139. };
  140. SkGlyph* makeGlyph(SkPackedGlyphID);
  141. enum PathDetail {
  142. kMetricsOnly,
  143. kMetricsAndPath
  144. };
  145. // internalPrepare will only be called with a mutex already held.
  146. SkSpan<const SkGlyph*> internalPrepare(
  147. SkSpan<const SkGlyphID> glyphIDs,
  148. PathDetail pathDetail,
  149. const SkGlyph** results);
  150. const SkAutoDescriptor fDesc;
  151. const std::unique_ptr<SkScalerContext> fScalerContext;
  152. SkFontMetrics fFontMetrics;
  153. // Map from a combined GlyphID and sub-pixel position to a SkGlyph*.
  154. // The actual glyph is stored in the fAlloc. This structure provides an
  155. // unchanging pointer as long as the strike is alive.
  156. SkTHashTable<SkGlyph*, SkPackedGlyphID, GlyphMapHashTraits> fGlyphMap;
  157. // so we don't grow our arrays a lot
  158. static constexpr size_t kMinGlyphCount = 8;
  159. static constexpr size_t kMinGlyphImageSize = 16 /* height */ * 8 /* width */;
  160. static constexpr size_t kMinAllocAmount = kMinGlyphImageSize * kMinGlyphCount;
  161. SkArenaAlloc fAlloc {kMinAllocAmount};
  162. // Tracks (approx) how much ram is tied-up in this strike.
  163. size_t fMemoryUsed;
  164. const bool fIsSubpixel;
  165. const SkAxisAlignment fAxisAlignment;
  166. };
  167. #endif // SkStrike_DEFINED