GrCCPathProcessor.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2017 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 GrCCPathProcessor_DEFINED
  8. #define GrCCPathProcessor_DEFINED
  9. #include <array>
  10. #include "include/core/SkPath.h"
  11. #include "src/gpu/GrCaps.h"
  12. #include "src/gpu/GrGeometryProcessor.h"
  13. #include "src/gpu/GrPipeline.h"
  14. #include "src/gpu/ccpr/GrCCAtlas.h"
  15. #include "src/gpu/ccpr/GrOctoBounds.h"
  16. class GrCCPathCacheEntry;
  17. class GrCCPerFlushResources;
  18. class GrOnFlushResourceProvider;
  19. class GrOpFlushState;
  20. /**
  21. * This class draws AA paths using the coverage count masks produced by GrCCCoverageProcessor.
  22. *
  23. * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and
  24. * fill rule.
  25. *
  26. * To draw paths, the caller must set up an instance buffer as detailed below, then call drawPaths()
  27. * providing its own instance buffer alongside the buffers found by calling FindIndexBuffer/
  28. * FindVertexBuffer.
  29. */
  30. class GrCCPathProcessor : public GrGeometryProcessor {
  31. public:
  32. struct Instance {
  33. SkRect fDevBounds; // "right < left" indicates even-odd fill type.
  34. SkRect fDevBounds45; // Bounding box in "| 1 -1 | * devCoords" space. See GrOctoBounds.
  35. // | 1 1 |
  36. SkIVector fDevToAtlasOffset; // Translation from device space to location in atlas.
  37. uint64_t fColor; // Color always stored as 4 x fp16
  38. void set(const GrOctoBounds&, const SkIVector& devToAtlasOffset, uint64_t, GrFillRule);
  39. void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t, GrFillRule);
  40. };
  41. GR_STATIC_ASSERT(4 * 12 == sizeof(Instance));
  42. static sk_sp<const GrGpuBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
  43. static sk_sp<const GrGpuBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
  44. enum class CoverageMode : bool {
  45. kCoverageCount,
  46. kLiteral
  47. };
  48. static CoverageMode GetCoverageMode(GrCCAtlas::CoverageType coverageType) {
  49. return (GrCCAtlas::CoverageType::kFP16_CoverageCount == coverageType)
  50. ? CoverageMode::kCoverageCount
  51. : CoverageMode::kLiteral;
  52. }
  53. GrCCPathProcessor(
  54. CoverageMode, const GrTexture* atlasTexture, const GrSwizzle&,
  55. GrSurfaceOrigin atlasOrigin,
  56. const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I());
  57. const char* name() const override { return "GrCCPathProcessor"; }
  58. void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
  59. b->add32((uint32_t)fCoverageMode);
  60. }
  61. GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
  62. void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
  63. const GrCCPerFlushResources&, int baseInstance, int endInstance,
  64. const SkRect& bounds) const;
  65. private:
  66. const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
  67. const CoverageMode fCoverageMode;
  68. const TextureSampler fAtlasAccess;
  69. SkISize fAtlasSize;
  70. GrSurfaceOrigin fAtlasOrigin;
  71. SkMatrix fLocalMatrix;
  72. static constexpr Attribute kInstanceAttribs[] = {
  73. {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
  74. {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
  75. {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
  76. {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
  77. };
  78. static constexpr int kColorAttribIdx = 3;
  79. static constexpr Attribute kCornersAttrib =
  80. {"corners", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
  81. class Impl;
  82. typedef GrGeometryProcessor INHERITED;
  83. };
  84. inline void GrCCPathProcessor::Instance::set(
  85. const GrOctoBounds& octoBounds, const SkIVector& devToAtlasOffset, uint64_t color,
  86. GrFillRule fillRule) {
  87. if (GrFillRule::kNonzero == fillRule) {
  88. // We cover "nonzero" paths with clockwise triangles, which is the default result from
  89. // normal octo bounds.
  90. fDevBounds = octoBounds.bounds();
  91. fDevBounds45 = octoBounds.bounds45();
  92. } else {
  93. // We cover "even/odd" paths with counterclockwise triangles. Here we reorder the bounding
  94. // box vertices so the output is flipped horizontally.
  95. fDevBounds.setLTRB(
  96. octoBounds.right(), octoBounds.top(), octoBounds.left(), octoBounds.bottom());
  97. fDevBounds45.setLTRB(
  98. octoBounds.bottom45(), octoBounds.right45(), octoBounds.top45(),
  99. octoBounds.left45());
  100. }
  101. fDevToAtlasOffset = devToAtlasOffset;
  102. fColor = color;
  103. }
  104. #endif