GrStencilAndCoverPathRenderer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2012 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. #include "include/private/GrRecordingContext.h"
  8. #include "src/gpu/GrCaps.h"
  9. #include "src/gpu/GrFixedClip.h"
  10. #include "src/gpu/GrGpu.h"
  11. #include "src/gpu/GrPath.h"
  12. #include "src/gpu/GrRenderTargetContextPriv.h"
  13. #include "src/gpu/GrResourceProvider.h"
  14. #include "src/gpu/GrStencilClip.h"
  15. #include "src/gpu/GrStyle.h"
  16. #include "src/gpu/geometry/GrShape.h"
  17. #include "src/gpu/ops/GrDrawPathOp.h"
  18. #include "src/gpu/ops/GrStencilAndCoverPathRenderer.h"
  19. #include "src/gpu/ops/GrStencilPathOp.h"
  20. GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider,
  21. const GrCaps& caps) {
  22. if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) {
  23. return new GrStencilAndCoverPathRenderer(resourceProvider);
  24. } else {
  25. return nullptr;
  26. }
  27. }
  28. GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider)
  29. : fResourceProvider(resourceProvider) {
  30. }
  31. GrPathRenderer::CanDrawPath
  32. GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
  33. SkASSERT(!args.fTargetIsWrappedVkSecondaryCB);
  34. // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
  35. // path.
  36. if (args.fShape->style().strokeRec().isHairlineStyle() ||
  37. args.fShape->style().hasNonDashPathEffect() ||
  38. args.fHasUserStencilSettings) {
  39. return CanDrawPath::kNo;
  40. }
  41. if (GrAAType::kCoverage == args.fAAType && !args.fProxy->canUseMixedSamples(*args.fCaps)) {
  42. // We rely on a mixed sampled stencil buffer to implement coverage AA.
  43. return CanDrawPath::kNo;
  44. }
  45. return CanDrawPath::kYes;
  46. }
  47. static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
  48. GrUniqueKey key;
  49. bool isVolatile;
  50. GrPath::ComputeKey(shape, &key, &isVolatile);
  51. sk_sp<GrPath> path;
  52. if (!isVolatile) {
  53. path = resourceProvider->findByUniqueKey<GrPath>(key);
  54. }
  55. if (!path) {
  56. SkPath skPath;
  57. shape.asPath(&skPath);
  58. path = resourceProvider->createPath(skPath, shape.style());
  59. if (!isVolatile) {
  60. resourceProvider->assignUniqueKeyToResource(key, path.get());
  61. }
  62. } else {
  63. #ifdef SK_DEBUG
  64. SkPath skPath;
  65. shape.asPath(&skPath);
  66. SkASSERT(path->isEqualTo(skPath, shape.style()));
  67. #endif
  68. }
  69. return path;
  70. }
  71. void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
  72. GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
  73. "GrStencilAndCoverPathRenderer::onStencilPath");
  74. sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
  75. args.fRenderTargetContext->priv().stencilPath(
  76. *args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, p.get());
  77. }
  78. bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
  79. GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
  80. "GrStencilAndCoverPathRenderer::onDrawPath");
  81. SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
  82. const SkMatrix& viewMatrix = *args.fViewMatrix;
  83. bool doStencilMSAA = GrAAType::kNone != args.fAAType;
  84. sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
  85. if (args.fShape->inverseFilled()) {
  86. SkMatrix vmi;
  87. if (!viewMatrix.invert(&vmi)) {
  88. return true;
  89. }
  90. SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
  91. args.fRenderTargetContext->height()); // Inverse fill.
  92. // fake inverse with a stencil and cover
  93. GrAppliedClip appliedClip;
  94. if (!args.fClip->apply(
  95. args.fContext, args.fRenderTargetContext, doStencilMSAA, true, &appliedClip,
  96. &devBounds)) {
  97. return true;
  98. }
  99. GrStencilClip stencilClip(appliedClip.stencilStackID());
  100. if (appliedClip.scissorState().enabled()) {
  101. stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
  102. }
  103. if (appliedClip.windowRectsState().enabled()) {
  104. stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
  105. appliedClip.windowRectsState().mode());
  106. }
  107. // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
  108. // final draw and it is meaningless to multiply by coverage when drawing to stencil.
  109. args.fRenderTargetContext->priv().stencilPath(
  110. stencilClip, GrAA(doStencilMSAA), viewMatrix, path.get());
  111. {
  112. static constexpr GrUserStencilSettings kInvertedCoverPass(
  113. GrUserStencilSettings::StaticInit<
  114. 0x0000,
  115. // We know our rect will hit pixels outside the clip and the user bits will
  116. // be 0 outside the clip. So we can't just fill where the user bits are 0. We
  117. // also need to check that the clip bit is set.
  118. GrUserStencilTest::kEqualIfInClip,
  119. 0xffff,
  120. GrUserStencilOp::kKeep,
  121. GrUserStencilOp::kZero,
  122. 0xffff>()
  123. );
  124. SkRect coverBounds;
  125. // mapRect through persp matrix may not be correct
  126. if (!viewMatrix.hasPerspective()) {
  127. vmi.mapRect(&coverBounds, devBounds);
  128. // theoretically could set bloat = 0, instead leave it because of matrix inversion
  129. // precision.
  130. SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
  131. coverBounds.outset(bloat, bloat);
  132. } else {
  133. coverBounds = devBounds;
  134. }
  135. const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
  136. const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
  137. // We have to suppress enabling MSAA for mixed samples or we will get seams due to
  138. // coverage modulation along the edge where two triangles making up the rect meet.
  139. GrAA doStencilMSAA = GrAA::kNo;
  140. if (GrAAType::kMSAA == args.fAAType) {
  141. doStencilMSAA = GrAA::kYes;
  142. }
  143. args.fRenderTargetContext->priv().stencilRect(
  144. *args.fClip, &kInvertedCoverPass, std::move(args.fPaint), doStencilMSAA,
  145. coverMatrix, coverBounds, &localMatrix);
  146. }
  147. } else {
  148. std::unique_ptr<GrDrawOp> op = GrDrawPathOp::Make(
  149. args.fContext, viewMatrix, std::move(args.fPaint), GrAA(doStencilMSAA), path.get());
  150. args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
  151. }
  152. return true;
  153. }