GrPipeline.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 GrPipeline_DEFINED
  8. #define GrPipeline_DEFINED
  9. #include "include/core/SkMatrix.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "src/gpu/GrColor.h"
  12. #include "src/gpu/GrFragmentProcessor.h"
  13. #include "src/gpu/GrNonAtomicRef.h"
  14. #include "src/gpu/GrPendingIOResource.h"
  15. #include "src/gpu/GrProcessorSet.h"
  16. #include "src/gpu/GrProgramDesc.h"
  17. #include "src/gpu/GrScissorState.h"
  18. #include "src/gpu/GrUserStencilSettings.h"
  19. #include "src/gpu/GrWindowRectsState.h"
  20. #include "src/gpu/effects/GrCoverageSetOpXP.h"
  21. #include "src/gpu/effects/GrDisableColorXP.h"
  22. #include "src/gpu/effects/GrPorterDuffXferProcessor.h"
  23. #include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
  24. #include "src/gpu/geometry/GrRect.h"
  25. class GrAppliedClip;
  26. class GrOp;
  27. class GrRenderTargetContext;
  28. /**
  29. * This immutable object contains information needed to set build a shader program and set API
  30. * state for a draw. It is used along with a GrPrimitiveProcessor and a source of geometric
  31. * data (GrMesh or GrPath) to draw.
  32. */
  33. class GrPipeline {
  34. public:
  35. ///////////////////////////////////////////////////////////////////////////
  36. /// @name Creation
  37. // Pipeline options that the caller may enable.
  38. // NOTE: This enum is extended later by GrPipeline::Flags.
  39. enum class InputFlags : uint8_t {
  40. kNone = 0,
  41. /**
  42. * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
  43. * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
  44. * the 3D API.
  45. */
  46. kHWAntialias = (1 << 0),
  47. /**
  48. * Modifies the vertex shader so that vertices will be positioned at pixel centers.
  49. */
  50. kSnapVerticesToPixelCenters = (1 << 1), // This value must be last. (See kLastInputFlag.)
  51. };
  52. struct InitArgs {
  53. InputFlags fInputFlags = InputFlags::kNone;
  54. const GrUserStencilSettings* fUserStencil = &GrUserStencilSettings::kUnused;
  55. const GrCaps* fCaps = nullptr;
  56. GrXferProcessor::DstProxy fDstProxy;
  57. GrSwizzle fOutputSwizzle;
  58. };
  59. /**
  60. * Some state can be changed between GrMeshes without changing GrPipelines. This is generally
  61. * less expensive then using multiple pipelines. Such state is called "dynamic state". It can
  62. * be specified in two ways:
  63. * 1) FixedDynamicState - use this to specify state that does not vary between GrMeshes.
  64. * 2) DynamicStateArrays - use this to specify per mesh values for dynamic state.
  65. **/
  66. struct FixedDynamicState {
  67. explicit FixedDynamicState(const SkIRect& scissorRect) : fScissorRect(scissorRect) {}
  68. FixedDynamicState() = default;
  69. SkIRect fScissorRect = SkIRect::EmptyIRect();
  70. // Must have GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers
  71. // or textures are passed using DynamicStateArrays.
  72. GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
  73. };
  74. /**
  75. * Any non-null array overrides the FixedDynamicState on a mesh-by-mesh basis. Arrays must
  76. * have one entry for each GrMesh.
  77. */
  78. struct DynamicStateArrays {
  79. const SkIRect* fScissorRects = nullptr;
  80. // Must have GrPrimitiveProcessor::numTextureSamplers() * num_meshes entries.
  81. // Can be null if no samplers or to use the same textures for all meshes via'
  82. // FixedDynamicState.
  83. GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
  84. };
  85. /**
  86. * Creates a simple pipeline with default settings and no processors. The provided blend mode
  87. * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
  88. * specify a scissor rectangle through the DynamicState struct.
  89. **/
  90. GrPipeline(GrScissorTest scissor, SkBlendMode blend, const GrSwizzle& outputSwizzle,
  91. InputFlags flags = InputFlags::kNone,
  92. const GrUserStencilSettings* stencil = &GrUserStencilSettings::kUnused)
  93. : GrPipeline(scissor, GrPorterDuffXPFactory::MakeNoCoverageXP(blend), outputSwizzle,
  94. flags, stencil) {
  95. }
  96. GrPipeline(GrScissorTest, sk_sp<const GrXferProcessor>, const GrSwizzle& outputSwizzle,
  97. InputFlags = InputFlags::kNone,
  98. const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
  99. GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
  100. GrPipeline(const GrPipeline&) = delete;
  101. GrPipeline& operator=(const GrPipeline&) = delete;
  102. /// @}
  103. ///////////////////////////////////////////////////////////////////////////
  104. /// @name GrFragmentProcessors
  105. // Make the renderTargetContext's GrOpList be dependent on any GrOpLists in this pipeline
  106. void addDependenciesTo(GrOpList* recipient, const GrCaps&) const;
  107. int numColorFragmentProcessors() const { return fNumColorProcessors; }
  108. int numCoverageFragmentProcessors() const {
  109. return fFragmentProcessors.count() - fNumColorProcessors;
  110. }
  111. int numFragmentProcessors() const { return fFragmentProcessors.count(); }
  112. const GrXferProcessor& getXferProcessor() const {
  113. if (fXferProcessor) {
  114. return *fXferProcessor.get();
  115. } else {
  116. // A null xp member means the common src-over case. GrXferProcessor's ref'ing
  117. // mechanism is not thread safe so we do not hold a ref on this global.
  118. return GrPorterDuffXPFactory::SimpleSrcOverXP();
  119. }
  120. }
  121. /**
  122. * If the GrXferProcessor uses a texture to access the dst color, then this returns that
  123. * texture and the offset to the dst contents within that texture.
  124. */
  125. GrTextureProxy* dstTextureProxy(SkIPoint* offset = nullptr) const {
  126. if (offset) {
  127. *offset = fDstTextureOffset;
  128. }
  129. return fDstTextureProxy ? fDstTextureProxy->asTextureProxy() : nullptr;
  130. }
  131. GrTexture* peekDstTexture(SkIPoint* offset = nullptr) const {
  132. if (GrTextureProxy* dstProxy = this->dstTextureProxy(offset)) {
  133. return dstProxy->peekTexture();
  134. }
  135. return nullptr;
  136. }
  137. const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
  138. SkASSERT(idx < this->numColorFragmentProcessors());
  139. return *fFragmentProcessors[idx].get();
  140. }
  141. const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
  142. SkASSERT(idx < this->numCoverageFragmentProcessors());
  143. return *fFragmentProcessors[fNumColorProcessors + idx].get();
  144. }
  145. const GrFragmentProcessor& getFragmentProcessor(int idx) const {
  146. return *fFragmentProcessors[idx].get();
  147. }
  148. /// @}
  149. const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
  150. bool isScissorEnabled() const {
  151. return SkToBool(fFlags & Flags::kScissorEnabled);
  152. }
  153. const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
  154. bool isHWAntialiasState() const { return SkToBool(fFlags & InputFlags::kHWAntialias); }
  155. bool snapVerticesToPixelCenters() const {
  156. return SkToBool(fFlags & InputFlags::kSnapVerticesToPixelCenters);
  157. }
  158. bool hasStencilClip() const {
  159. return SkToBool(fFlags & Flags::kHasStencilClip);
  160. }
  161. bool isStencilEnabled() const {
  162. return SkToBool(fFlags & Flags::kStencilEnabled);
  163. }
  164. SkDEBUGCODE(bool isBad() const { return SkToBool(fFlags & Flags::kIsBad); })
  165. GrXferBarrierType xferBarrierType(GrTexture*, const GrCaps&) const;
  166. // Used by Vulkan and Metal to cache their respective pipeline objects
  167. uint32_t getBlendInfoKey() const;
  168. const GrSwizzle& outputSwizzle() const { return fOutputSwizzle; }
  169. private:
  170. SkDEBUGCODE(void markAsBad() { fFlags |= Flags::kIsBad; })
  171. static constexpr uint8_t kLastInputFlag = (uint8_t)InputFlags::kSnapVerticesToPixelCenters;
  172. /** This is a continuation of the public "InputFlags" enum. */
  173. enum class Flags : uint8_t {
  174. kHasStencilClip = (kLastInputFlag << 1),
  175. kStencilEnabled = (kLastInputFlag << 2),
  176. kScissorEnabled = (kLastInputFlag << 3),
  177. #ifdef SK_DEBUG
  178. kIsBad = (kLastInputFlag << 4),
  179. #endif
  180. };
  181. GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
  182. friend bool operator&(Flags, InputFlags);
  183. using FragmentProcessorArray = SkAutoSTArray<8, std::unique_ptr<const GrFragmentProcessor>>;
  184. GrProxyPendingIO fDstTextureProxy;
  185. SkIPoint fDstTextureOffset;
  186. GrWindowRectsState fWindowRectsState;
  187. const GrUserStencilSettings* fUserStencilSettings;
  188. Flags fFlags;
  189. sk_sp<const GrXferProcessor> fXferProcessor;
  190. FragmentProcessorArray fFragmentProcessors;
  191. // This value is also the index in fFragmentProcessors where coverage processors begin.
  192. int fNumColorProcessors;
  193. GrSwizzle fOutputSwizzle;
  194. };
  195. GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::InputFlags);
  196. GR_MAKE_BITFIELD_CLASS_OPS(GrPipeline::Flags);
  197. inline bool operator&(GrPipeline::Flags flags, GrPipeline::InputFlags inputFlag) {
  198. return (flags & (GrPipeline::Flags)inputFlag);
  199. }
  200. #endif