SkGlyphRunPainter.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2018 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkGlyphRunPainter_DEFINED
  8. #define SkGlyphRunPainter_DEFINED
  9. #include "include/core/SkSurfaceProps.h"
  10. #include "src/core/SkDistanceFieldGen.h"
  11. #include "src/core/SkGlyphRun.h"
  12. #include "src/core/SkScalerContext.h"
  13. #include "src/core/SkTextBlobPriv.h"
  14. #if SK_SUPPORT_GPU
  15. #include "src/gpu/text/GrTextContext.h"
  16. class GrColorSpaceInfo;
  17. class GrRenderTargetContext;
  18. #endif
  19. class SkGlyphRunPainterInterface;
  20. class SkStrikeSpec;
  21. class SkStrikeCommon {
  22. public:
  23. static SkVector PixelRounding(bool isSubpixel, SkAxisAlignment axisAlignment);
  24. // An atlas consists of plots, and plots hold glyphs. The minimum a plot can be is 256x256.
  25. // This means that the maximum size a glyph can be is 256x256.
  26. static constexpr uint16_t kSkSideTooBigForAtlas = 256;
  27. };
  28. class SkGlyphRunListPainter {
  29. public:
  30. // Constructor for SkBitmpapDevice.
  31. SkGlyphRunListPainter(const SkSurfaceProps& props,
  32. SkColorType colorType,
  33. SkColorSpace* cs,
  34. SkStrikeCacheInterface* strikeCache);
  35. #if SK_SUPPORT_GPU
  36. // The following two ctors are used exclusively by the GPU, and will always use the global
  37. // strike cache.
  38. SkGlyphRunListPainter(const SkSurfaceProps&, const GrColorSpaceInfo&);
  39. explicit SkGlyphRunListPainter(const GrRenderTargetContext& renderTargetContext);
  40. #endif // SK_SUPPORT_GPU
  41. class BitmapDevicePainter {
  42. public:
  43. virtual ~BitmapDevicePainter() = default;
  44. virtual void paintPaths(SkSpan<const SkPathPos> pathsAndPositions,
  45. SkScalar scale,
  46. const SkPaint& paint) const = 0;
  47. virtual void paintMasks(SkSpan<const SkMask> masks, const SkPaint& paint) const = 0;
  48. };
  49. void drawForBitmapDevice(
  50. const SkGlyphRunList& glyphRunList, const SkMatrix& deviceMatrix,
  51. const BitmapDevicePainter* bitmapDevice);
  52. #if SK_SUPPORT_GPU
  53. // A nullptr for process means that the calls to the cache will be performed, but none of the
  54. // callbacks will be called.
  55. void processGlyphRunList(const SkGlyphRunList& glyphRunList,
  56. const SkMatrix& viewMatrix,
  57. const SkSurfaceProps& props,
  58. bool contextSupportsDistanceFieldText,
  59. const GrTextContext::Options& options,
  60. SkGlyphRunPainterInterface* process);
  61. #endif // SK_SUPPORT_GPU
  62. private:
  63. SkGlyphRunListPainter(const SkSurfaceProps& props, SkColorType colorType,
  64. SkScalerContextFlags flags, SkStrikeCacheInterface* strikeCache);
  65. struct ScopedBuffers {
  66. ScopedBuffers(SkGlyphRunListPainter* painter, int size);
  67. ~ScopedBuffers();
  68. SkGlyphRunListPainter* fPainter;
  69. };
  70. ScopedBuffers SK_WARN_UNUSED_RESULT ensureBuffers(const SkGlyphRunList& glyphRunList);
  71. // TODO: Remove once I can hoist ensureBuffers above the list for loop in all cases.
  72. ScopedBuffers SK_WARN_UNUSED_RESULT ensureBuffers(const SkGlyphRun& glyphRun);
  73. /**
  74. * @param fARGBPositions in source space
  75. * @param fARGBGlyphsIDs the glyphs to process
  76. * @param fGlyphPos used as scratch space
  77. * @param maxSourceGlyphDimension the longest dimension of any glyph as if all fARGBGlyphsIDs
  78. * were drawn in source space (as if viewMatrix were identity)
  79. */
  80. void processARGBFallback(SkScalar maxSourceGlyphDimension,
  81. const SkPaint& runPaint,
  82. const SkFont& runFont,
  83. const SkMatrix& viewMatrix,
  84. SkGlyphRunPainterInterface* process);
  85. static SkSpan<const SkPackedGlyphID> DeviceSpacePackedGlyphIDs(
  86. SkStrikeInterface* strike,
  87. const SkMatrix& viewMatrix,
  88. const SkPoint& origin,
  89. int n,
  90. const SkGlyphID* glyphIDs,
  91. const SkPoint* positions,
  92. SkPoint* mappedPositions,
  93. SkPackedGlyphID* results);
  94. static SkSpan<const SkPackedGlyphID> SourceSpacePackedGlyphIDs(
  95. const SkPoint& origin,
  96. int n,
  97. const SkGlyphID* glyphIDs,
  98. const SkPoint* positions,
  99. SkPoint* mappedPositions,
  100. SkPackedGlyphID* results);
  101. // The props as on the actual device.
  102. const SkSurfaceProps fDeviceProps;
  103. // The props for when the bitmap device can't draw LCD text.
  104. const SkSurfaceProps fBitmapFallbackProps;
  105. const SkColorType fColorType;
  106. const SkScalerContextFlags fScalerContextFlags;
  107. SkStrikeCacheInterface* const fStrikeCache;
  108. int fMaxRunSize{0};
  109. SkAutoTMalloc<SkPoint> fPositions;
  110. SkAutoTMalloc<SkPackedGlyphID> fPackedGlyphIDs;
  111. SkAutoTMalloc<SkGlyphPos> fGlyphPos;
  112. std::vector<SkGlyphPos> fPaths;
  113. // Vectors for tracking ARGB fallback information.
  114. std::vector<SkGlyphID> fARGBGlyphsIDs;
  115. std::vector<SkPoint> fARGBPositions;
  116. };
  117. // SkGlyphRunPainterInterface are all the ways that Ganesh generates glyphs. The first
  118. // distinction is between Device and Source.
  119. // * Device - the data in the cache is scaled to the device. There is no transformation from the
  120. // cache to the screen.
  121. // * Source - the data in the cache needs to be scaled from the cache to source space using the
  122. // factor cacheToSourceScale. When drawn the system must combine cacheToSourceScale and the
  123. // deviceView matrix to transform the cache data onto the screen. This allows zooming and
  124. // simple animation to reuse the same glyph data by just changing the transform.
  125. //
  126. // In addition to transformation type above, Masks, Paths, SDFT, and Fallback (or really the
  127. // rendering method of last resort) are the different
  128. // formats of data used from the cache.
  129. class SkGlyphRunPainterInterface {
  130. public:
  131. virtual ~SkGlyphRunPainterInterface() = default;
  132. virtual void startRun(const SkGlyphRun& glyphRun, bool useSDFT) = 0;
  133. virtual void processDeviceMasks(SkSpan<const SkGlyphPos> masks,
  134. const SkStrikeSpec& strikeSpec) = 0;
  135. virtual void processSourcePaths(SkSpan<const SkGlyphPos> paths,
  136. const SkStrikeSpec& strikeSpec) = 0;
  137. virtual void processDevicePaths(SkSpan<const SkGlyphPos> paths) = 0;
  138. virtual void processSourceSDFT(SkSpan<const SkGlyphPos> masks,
  139. const SkStrikeSpec& strikeSpec,
  140. const SkFont& runFont,
  141. SkScalar minScale,
  142. SkScalar maxScale,
  143. bool hasWCoord) = 0;
  144. virtual void processSourceFallback(SkSpan<const SkGlyphPos> masks,
  145. const SkStrikeSpec& strikeSpec,
  146. bool hasW) = 0;
  147. virtual void processDeviceFallback(SkSpan<const SkGlyphPos> masks,
  148. const SkStrikeSpec& strikeSpec) = 0;
  149. };
  150. #endif // SkGlyphRunPainter_DEFINED