GrAtlasTextOp.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2015 Google Inc.
  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 GrAtlasTextOp_DEFINED
  8. #define GrAtlasTextOp_DEFINED
  9. #include "src/gpu/ops/GrMeshDrawOp.h"
  10. #include "src/gpu/text/GrDistanceFieldAdjustTable.h"
  11. #include "src/gpu/text/GrTextBlob.h"
  12. class GrRecordingContext;
  13. class SkAtlasTextTarget;
  14. class GrAtlasTextOp final : public GrMeshDrawOp {
  15. public:
  16. DEFINE_OP_CLASS_ID
  17. ~GrAtlasTextOp() override {
  18. for (int i = 0; i < fGeoCount; i++) {
  19. fGeoData[i].fBlob->unref();
  20. }
  21. }
  22. static const int kVerticesPerGlyph = GrTextBlob::kVerticesPerGlyph;
  23. static const int kIndicesPerGlyph = 6;
  24. typedef GrTextBlob Blob;
  25. struct Geometry {
  26. SkMatrix fViewMatrix;
  27. SkIRect fClipRect;
  28. Blob* fBlob;
  29. SkScalar fX;
  30. SkScalar fY;
  31. uint16_t fRun;
  32. uint16_t fSubRun;
  33. SkPMColor4f fColor;
  34. };
  35. static std::unique_ptr<GrAtlasTextOp> MakeBitmap(GrRecordingContext*,
  36. GrPaint&&,
  37. GrMaskFormat,
  38. int glyphCount,
  39. bool needsTransform);
  40. static std::unique_ptr<GrAtlasTextOp> MakeDistanceField(
  41. GrRecordingContext*,
  42. GrPaint&&,
  43. int glyphCount,
  44. const GrDistanceFieldAdjustTable*,
  45. bool useGammaCorrectDistanceTable,
  46. SkColor luminanceColor,
  47. const SkSurfaceProps&,
  48. bool isAntiAliased,
  49. bool useLCD);
  50. // To avoid even the initial copy of the struct, we have a getter for the first item which
  51. // is used to seed the op with its initial geometry. After seeding, the client should call
  52. // init() so the op can initialize itself
  53. Geometry& geometry() { return fGeoData[0]; }
  54. /** Called after this->geometry() has been configured. */
  55. void init();
  56. const char* name() const override { return "AtlasTextOp"; }
  57. void visitProxies(const VisitProxyFunc& func) const override;
  58. #ifdef SK_DEBUG
  59. SkString dumpInfo() const override;
  60. #endif
  61. FixedFunctionFlags fixedFunctionFlags() const override;
  62. GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
  63. bool hasMixedSampledCoverage, GrClampType) override;
  64. enum MaskType {
  65. kGrayscaleCoverageMask_MaskType,
  66. kLCDCoverageMask_MaskType,
  67. kColorBitmapMask_MaskType,
  68. kAliasedDistanceField_MaskType,
  69. kGrayscaleDistanceField_MaskType,
  70. kLCDDistanceField_MaskType,
  71. kLCDBGRDistanceField_MaskType,
  72. };
  73. MaskType maskType() const { return fMaskType; }
  74. void finalizeForTextTarget(uint32_t color, const GrCaps&);
  75. void executeForTextTarget(SkAtlasTextTarget*);
  76. private:
  77. friend class GrOpMemoryPool; // for ctor
  78. // The minimum number of Geometry we will try to allocate.
  79. static constexpr auto kMinGeometryAllocated = 12;
  80. GrAtlasTextOp(GrPaint&& paint)
  81. : INHERITED(ClassID())
  82. , fGeoDataAllocSize(kMinGeometryAllocated)
  83. , fProcessors(std::move(paint)) {}
  84. struct FlushInfo {
  85. sk_sp<const GrBuffer> fVertexBuffer;
  86. sk_sp<const GrBuffer> fIndexBuffer;
  87. sk_sp<GrGeometryProcessor> fGeometryProcessor;
  88. GrPipeline::FixedDynamicState* fFixedDynamicState;
  89. int fGlyphsToFlush;
  90. int fVertexOffset;
  91. };
  92. void onPrepareDraws(Target*) override;
  93. void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
  94. GrMaskFormat maskFormat() const {
  95. switch (fMaskType) {
  96. case kLCDCoverageMask_MaskType:
  97. return kA565_GrMaskFormat;
  98. case kColorBitmapMask_MaskType:
  99. return kARGB_GrMaskFormat;
  100. case kGrayscaleCoverageMask_MaskType:
  101. case kAliasedDistanceField_MaskType:
  102. case kGrayscaleDistanceField_MaskType:
  103. case kLCDDistanceField_MaskType:
  104. case kLCDBGRDistanceField_MaskType:
  105. return kA8_GrMaskFormat;
  106. }
  107. return kA8_GrMaskFormat; // suppress warning
  108. }
  109. bool usesDistanceFields() const {
  110. return kAliasedDistanceField_MaskType == fMaskType ||
  111. kGrayscaleDistanceField_MaskType == fMaskType ||
  112. kLCDDistanceField_MaskType == fMaskType ||
  113. kLCDBGRDistanceField_MaskType == fMaskType;
  114. }
  115. bool isLCD() const {
  116. return kLCDCoverageMask_MaskType == fMaskType ||
  117. kLCDDistanceField_MaskType == fMaskType ||
  118. kLCDBGRDistanceField_MaskType == fMaskType;
  119. }
  120. inline void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const;
  121. const SkPMColor4f& color() const { SkASSERT(fGeoCount > 0); return fGeoData[0].fColor; }
  122. bool usesLocalCoords() const { return fUsesLocalCoords; }
  123. int numGlyphs() const { return fNumGlyphs; }
  124. CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
  125. sk_sp<GrGeometryProcessor> setupDfProcessor(const GrShaderCaps& caps,
  126. const sk_sp<GrTextureProxy>* proxies,
  127. unsigned int numActiveProxies) const;
  128. SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
  129. int fGeoDataAllocSize;
  130. GrProcessorSet fProcessors;
  131. struct {
  132. uint32_t fUsesLocalCoords : 1;
  133. uint32_t fUseGammaCorrectDistanceTable : 1;
  134. uint32_t fNeedsGlyphTransform : 1;
  135. };
  136. int fGeoCount;
  137. int fNumGlyphs;
  138. MaskType fMaskType;
  139. // Distance field properties
  140. sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
  141. SkColor fLuminanceColor;
  142. uint32_t fDFGPFlags = 0;
  143. typedef GrMeshDrawOp INHERITED;
  144. };
  145. #endif