GrSimpleMeshDrawOpHelper.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "src/gpu/GrAppliedClip.h"
  8. #include "src/gpu/GrProcessorSet.h"
  9. #include "src/gpu/GrUserStencilSettings.h"
  10. #include "src/gpu/SkGr.h"
  11. #include "src/gpu/geometry/GrRect.h"
  12. #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
  13. GrSimpleMeshDrawOpHelper::GrSimpleMeshDrawOpHelper(const MakeArgs& args, GrAAType aaType,
  14. InputFlags inputFlags)
  15. : fProcessors(args.fProcessorSet)
  16. , fPipelineFlags((GrPipeline::InputFlags)inputFlags)
  17. , fAAType((int)aaType)
  18. , fUsesLocalCoords(false)
  19. , fCompatibleWithCoverageAsAlpha(false) {
  20. SkDEBUGCODE(fDidAnalysis = false);
  21. SkDEBUGCODE(fMadePipeline = false);
  22. if (GrAATypeIsHW(aaType)) {
  23. fPipelineFlags |= GrPipeline::InputFlags::kHWAntialias;
  24. }
  25. }
  26. GrSimpleMeshDrawOpHelper::~GrSimpleMeshDrawOpHelper() {
  27. if (fProcessors) {
  28. fProcessors->~GrProcessorSet();
  29. }
  30. }
  31. GrDrawOp::FixedFunctionFlags GrSimpleMeshDrawOpHelper::fixedFunctionFlags() const {
  32. return GrAATypeIsHW((this->aaType())) ? GrDrawOp::FixedFunctionFlags::kUsesHWAA
  33. : GrDrawOp::FixedFunctionFlags::kNone;
  34. }
  35. static bool none_as_coverage_aa_compatible(GrAAType aa1, GrAAType aa2) {
  36. return (aa1 == GrAAType::kNone && aa2 == GrAAType::kCoverage) ||
  37. (aa1 == GrAAType::kCoverage && aa2 == GrAAType::kNone);
  38. }
  39. bool GrSimpleMeshDrawOpHelper::isCompatible(const GrSimpleMeshDrawOpHelper& that,
  40. const GrCaps& caps, const SkRect& thisBounds,
  41. const SkRect& thatBounds, bool noneAsCoverageAA) const {
  42. if (SkToBool(fProcessors) != SkToBool(that.fProcessors)) {
  43. return false;
  44. }
  45. if (fProcessors) {
  46. if (*fProcessors != *that.fProcessors) {
  47. return false;
  48. }
  49. }
  50. bool result = fPipelineFlags == that.fPipelineFlags && (fAAType == that.fAAType ||
  51. (noneAsCoverageAA && none_as_coverage_aa_compatible(this->aaType(), that.aaType())));
  52. SkASSERT(!result || fCompatibleWithCoverageAsAlpha == that.fCompatibleWithCoverageAsAlpha);
  53. SkASSERT(!result || fUsesLocalCoords == that.fUsesLocalCoords);
  54. return result;
  55. }
  56. GrProcessorSet::Analysis GrSimpleMeshDrawOpHelper::finalizeProcessors(
  57. const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
  58. GrClampType clampType, GrProcessorAnalysisCoverage geometryCoverage,
  59. SkPMColor4f* geometryColor, bool* wideColor) {
  60. GrProcessorAnalysisColor color = *geometryColor;
  61. auto result = this->finalizeProcessors(
  62. caps, clip, hasMixedSampledCoverage, clampType, geometryCoverage, &color);
  63. color.isConstant(geometryColor);
  64. if (wideColor) {
  65. *wideColor = SkPMColor4fNeedsWideColor(*geometryColor, clampType, caps);
  66. }
  67. return result;
  68. }
  69. GrProcessorSet::Analysis GrSimpleMeshDrawOpHelper::finalizeProcessors(
  70. const GrCaps& caps, const GrAppliedClip* clip, const GrUserStencilSettings* userStencil,
  71. bool hasMixedSampledCoverage, GrClampType clampType,
  72. GrProcessorAnalysisCoverage geometryCoverage, GrProcessorAnalysisColor* geometryColor) {
  73. SkDEBUGCODE(fDidAnalysis = true);
  74. GrProcessorSet::Analysis analysis;
  75. if (fProcessors) {
  76. GrProcessorAnalysisCoverage coverage = geometryCoverage;
  77. if (GrProcessorAnalysisCoverage::kNone == coverage) {
  78. coverage = clip->numClipCoverageFragmentProcessors()
  79. ? GrProcessorAnalysisCoverage::kSingleChannel
  80. : GrProcessorAnalysisCoverage::kNone;
  81. }
  82. SkPMColor4f overrideColor;
  83. analysis = fProcessors->finalize(*geometryColor, coverage, clip, userStencil,
  84. hasMixedSampledCoverage, caps, clampType, &overrideColor);
  85. if (analysis.inputColorIsOverridden()) {
  86. *geometryColor = overrideColor;
  87. }
  88. } else {
  89. analysis = GrProcessorSet::EmptySetAnalysis();
  90. }
  91. fUsesLocalCoords = analysis.usesLocalCoords();
  92. fCompatibleWithCoverageAsAlpha = analysis.isCompatibleWithCoverageAsAlpha();
  93. return analysis;
  94. }
  95. void GrSimpleMeshDrawOpHelper::executeDrawsAndUploads(
  96. const GrOp* op, GrOpFlushState* flushState, const SkRect& chainBounds) {
  97. if (fProcessors) {
  98. flushState->executeDrawsAndUploadsForMeshDrawOp(
  99. op, chainBounds, std::move(*fProcessors), fPipelineFlags);
  100. } else {
  101. flushState->executeDrawsAndUploadsForMeshDrawOp(
  102. op, chainBounds, GrProcessorSet::MakeEmptySet(), fPipelineFlags);
  103. }
  104. }
  105. #ifdef SK_DEBUG
  106. static void dump_pipeline_flags(GrPipeline::InputFlags flags, SkString* result) {
  107. if (GrPipeline::InputFlags::kNone != flags) {
  108. if (flags & GrPipeline::InputFlags::kSnapVerticesToPixelCenters) {
  109. result->append("Snap vertices to pixel center.\n");
  110. }
  111. if (flags & GrPipeline::InputFlags::kHWAntialias) {
  112. result->append("HW Antialiasing enabled.\n");
  113. }
  114. return;
  115. }
  116. result->append("No pipeline flags\n");
  117. }
  118. SkString GrSimpleMeshDrawOpHelper::dumpInfo() const {
  119. const GrProcessorSet& processors = fProcessors ? *fProcessors : GrProcessorSet::EmptySet();
  120. SkString result = processors.dumpProcessors();
  121. result.append("AA Type: ");
  122. switch (this->aaType()) {
  123. case GrAAType::kNone:
  124. result.append(" none\n");
  125. break;
  126. case GrAAType::kCoverage:
  127. result.append(" coverage\n");
  128. break;
  129. case GrAAType::kMSAA:
  130. result.append(" msaa\n");
  131. break;
  132. }
  133. dump_pipeline_flags(fPipelineFlags, &result);
  134. return result;
  135. }
  136. #endif
  137. GrSimpleMeshDrawOpHelperWithStencil::GrSimpleMeshDrawOpHelperWithStencil(
  138. const MakeArgs& args, GrAAType aaType, const GrUserStencilSettings* stencilSettings,
  139. InputFlags inputFlags)
  140. : INHERITED(args, aaType, inputFlags)
  141. , fStencilSettings(stencilSettings ? stencilSettings : &GrUserStencilSettings::kUnused) {}
  142. GrDrawOp::FixedFunctionFlags GrSimpleMeshDrawOpHelperWithStencil::fixedFunctionFlags() const {
  143. GrDrawOp::FixedFunctionFlags flags = INHERITED::fixedFunctionFlags();
  144. if (fStencilSettings != &GrUserStencilSettings::kUnused) {
  145. flags |= GrDrawOp::FixedFunctionFlags::kUsesStencil;
  146. }
  147. return flags;
  148. }
  149. GrProcessorSet::Analysis GrSimpleMeshDrawOpHelperWithStencil::finalizeProcessors(
  150. const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
  151. GrClampType clampType, GrProcessorAnalysisCoverage geometryCoverage,
  152. SkPMColor4f* geometryColor, bool* wideColor) {
  153. GrProcessorAnalysisColor color = *geometryColor;
  154. auto result = this->finalizeProcessors(
  155. caps, clip, hasMixedSampledCoverage, clampType, geometryCoverage, &color);
  156. color.isConstant(geometryColor);
  157. if (wideColor) {
  158. *wideColor = SkPMColor4fNeedsWideColor(*geometryColor, clampType, caps);
  159. }
  160. return result;
  161. }
  162. bool GrSimpleMeshDrawOpHelperWithStencil::isCompatible(
  163. const GrSimpleMeshDrawOpHelperWithStencil& that, const GrCaps& caps,
  164. const SkRect& thisBounds, const SkRect& thatBounds, bool noneAsCoverageAA) const {
  165. return INHERITED::isCompatible(that, caps, thisBounds, thatBounds, noneAsCoverageAA) &&
  166. fStencilSettings == that.fStencilSettings;
  167. }
  168. void GrSimpleMeshDrawOpHelperWithStencil::executeDrawsAndUploads(
  169. const GrOp* op, GrOpFlushState* flushState, const SkRect& chainBounds) {
  170. if (fProcessors) {
  171. flushState->executeDrawsAndUploadsForMeshDrawOp(
  172. op, chainBounds, std::move(*fProcessors), fPipelineFlags, fStencilSettings);
  173. } else {
  174. flushState->executeDrawsAndUploadsForMeshDrawOp(
  175. op, chainBounds, GrProcessorSet::MakeEmptySet(), fPipelineFlags, fStencilSettings);
  176. }
  177. }
  178. #ifdef SK_DEBUG
  179. SkString GrSimpleMeshDrawOpHelperWithStencil::dumpInfo() const {
  180. SkString result = INHERITED::dumpInfo();
  181. result.appendf("Stencil settings: %s\n", (fStencilSettings ? "yes" : "no"));
  182. return result;
  183. }
  184. #endif