GrXferProcessor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2014 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 GrXferProcessor_DEFINED
  8. #define GrXferProcessor_DEFINED
  9. #include "include/gpu/GrBlend.h"
  10. #include "include/gpu/GrTypes.h"
  11. #include "src/gpu/GrNonAtomicRef.h"
  12. #include "src/gpu/GrProcessor.h"
  13. #include "src/gpu/GrProcessorAnalysis.h"
  14. class GrGLSLXferProcessor;
  15. class GrProcessorSet;
  16. class GrShaderCaps;
  17. /**
  18. * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
  19. * required after a pixel has been written, before it can be safely read again.
  20. */
  21. enum GrXferBarrierType {
  22. kNone_GrXferBarrierType = 0, //<! No barrier is required
  23. kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
  24. kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
  25. };
  26. /** Should be able to treat kNone as false in boolean expressions */
  27. GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false);
  28. /**
  29. * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
  30. * color, and for applying any coverage. It does this by emitting fragment shader code and
  31. * controlling the fixed-function blend state. When dual-source blending is available, it may also
  32. * write a seconday fragment shader output color. GrXferProcessor has two modes of operation:
  33. *
  34. * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
  35. * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
  36. * provides shader code that blends the src and dst colors, and the base class applies coverage.
  37. *
  38. * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
  39. * function blend state and/or secondary output, and is responsible to apply coverage on its own.
  40. *
  41. * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
  42. * GrXPFactory once we have finalized the state of our draw.
  43. */
  44. class GrXferProcessor : public GrProcessor, public GrNonAtomicRef<GrXferProcessor> {
  45. public:
  46. /**
  47. * A texture that contains the dst pixel values and an integer coord offset from device space
  48. * to the space of the texture. Depending on GPU capabilities a DstTexture may be used by a
  49. * GrXferProcessor for blending in the fragment shader.
  50. */
  51. class DstProxy {
  52. public:
  53. DstProxy() { fOffset.set(0, 0); }
  54. DstProxy(const DstProxy& other) {
  55. *this = other;
  56. }
  57. DstProxy(sk_sp<GrTextureProxy> proxy, const SkIPoint& offset)
  58. : fProxy(std::move(proxy)) {
  59. if (fProxy) {
  60. fOffset = offset;
  61. } else {
  62. fOffset.set(0, 0);
  63. }
  64. }
  65. DstProxy& operator=(const DstProxy& other) {
  66. fProxy = other.fProxy;
  67. fOffset = other.fOffset;
  68. return *this;
  69. }
  70. bool operator==(const DstProxy& that) const {
  71. return fProxy == that.fProxy && fOffset == that.fOffset;
  72. }
  73. bool operator!=(const DstProxy& that) const { return !(*this == that); }
  74. const SkIPoint& offset() const { return fOffset; }
  75. void setOffset(const SkIPoint& offset) { fOffset = offset; }
  76. void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
  77. GrTextureProxy* proxy() const { return fProxy.get(); }
  78. void setProxy(sk_sp<GrTextureProxy> proxy) {
  79. fProxy = std::move(proxy);
  80. if (!fProxy) {
  81. fOffset = {0, 0};
  82. }
  83. }
  84. private:
  85. sk_sp<GrTextureProxy> fProxy;
  86. SkIPoint fOffset;
  87. };
  88. /**
  89. * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
  90. * specific subclass's key.
  91. */
  92. void getGLSLProcessorKey(const GrShaderCaps&,
  93. GrProcessorKeyBuilder*,
  94. const GrSurfaceOrigin* originIfDstTexture) const;
  95. /** Returns a new instance of the appropriate *GL* implementation class
  96. for the given GrXferProcessor; caller is responsible for deleting
  97. the object. */
  98. virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
  99. /**
  100. * Returns the barrier type, if any, that this XP will require. Note that the possibility
  101. * that a kTexture type barrier is required is handled by the GrPipeline and need not be
  102. * considered by subclass overrides of this function.
  103. */
  104. virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
  105. return kNone_GrXferBarrierType;
  106. }
  107. struct BlendInfo {
  108. SkDEBUGCODE(SkString dump() const;)
  109. GrBlendEquation fEquation = kAdd_GrBlendEquation;
  110. GrBlendCoeff fSrcBlend = kOne_GrBlendCoeff;
  111. GrBlendCoeff fDstBlend = kZero_GrBlendCoeff;
  112. SkPMColor4f fBlendConstant = SK_PMColor4fTRANSPARENT;
  113. bool fWriteColor = true;
  114. };
  115. inline BlendInfo getBlendInfo() const {
  116. BlendInfo blendInfo;
  117. if (!this->willReadDstColor()) {
  118. this->onGetBlendInfo(&blendInfo);
  119. } else if (this->dstReadUsesMixedSamples()) {
  120. blendInfo.fDstBlend = kIS2A_GrBlendCoeff;
  121. }
  122. return blendInfo;
  123. }
  124. bool willReadDstColor() const { return fWillReadDstColor; }
  125. /**
  126. * If we are performing a dst read, returns whether the base class will use mixed samples to
  127. * antialias the shader's final output. If not doing a dst read, the subclass is responsible
  128. * for antialiasing and this returns false.
  129. */
  130. bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
  131. /**
  132. * Returns whether or not this xferProcossor will set a secondary output to be used with dual
  133. * source blending.
  134. */
  135. bool hasSecondaryOutput() const;
  136. bool isLCD() const { return fIsLCD; }
  137. /** Returns true if this and other processor conservatively draw identically. It can only return
  138. true when the two processor are of the same subclass (i.e. they return the same object from
  139. from getFactory()).
  140. A return value of true from isEqual() should not be used to test whether the processor would
  141. generate the same shader code. To test for identical code generation use getGLSLProcessorKey
  142. */
  143. bool isEqual(const GrXferProcessor& that) const {
  144. if (this->classID() != that.classID()) {
  145. return false;
  146. }
  147. if (this->fWillReadDstColor != that.fWillReadDstColor) {
  148. return false;
  149. }
  150. if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
  151. return false;
  152. }
  153. if (fIsLCD != that.fIsLCD) {
  154. return false;
  155. }
  156. return this->onIsEqual(that);
  157. }
  158. protected:
  159. GrXferProcessor(ClassID classID);
  160. GrXferProcessor(ClassID classID, bool willReadDstColor, bool hasMixedSamples,
  161. GrProcessorAnalysisCoverage);
  162. private:
  163. /**
  164. * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
  165. * processor's GL backend implementation.
  166. */
  167. virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
  168. /**
  169. * If we are not performing a dst read, returns whether the subclass will set a secondary
  170. * output. When using dst reads, the base class controls the secondary output and this method
  171. * will not be called.
  172. */
  173. virtual bool onHasSecondaryOutput() const { return false; }
  174. /**
  175. * If we are not performing a dst read, retrieves the fixed-function blend state required by the
  176. * subclass. When using dst reads, the base class controls the fixed-function blend state and
  177. * this method will not be called. The BlendInfo struct comes initialized to "no blending".
  178. */
  179. virtual void onGetBlendInfo(BlendInfo*) const {}
  180. virtual bool onIsEqual(const GrXferProcessor&) const = 0;
  181. bool fWillReadDstColor;
  182. bool fDstReadUsesMixedSamples;
  183. bool fIsLCD;
  184. typedef GrProcessor INHERITED;
  185. };
  186. /**
  187. * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
  188. * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
  189. * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
  190. * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
  191. * the draw.
  192. *
  193. * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
  194. * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
  195. * blend with the destination color.
  196. *
  197. * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
  198. * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
  199. * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
  200. */
  201. // In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
  202. // GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
  203. // since these objects have no need for destructors. However, GCC and clang throw a warning when a
  204. // class has virtual functions and a non-virtual destructor. We suppress that warning here and
  205. // for the subclasses.
  206. #if defined(__GNUC__)
  207. #pragma GCC diagnostic push
  208. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  209. #endif
  210. #if defined(__clang__)
  211. #pragma clang diagnostic push
  212. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  213. #endif
  214. class GrXPFactory {
  215. public:
  216. typedef GrXferProcessor::DstProxy DstProxy;
  217. enum class AnalysisProperties : unsigned {
  218. kNone = 0x0,
  219. /**
  220. * The fragment shader will require the destination color.
  221. */
  222. kReadsDstInShader = 0x1,
  223. /**
  224. * The op may apply coverage as alpha and still blend correctly.
  225. */
  226. kCompatibleWithCoverageAsAlpha = 0x2,
  227. /**
  228. * The color input to the GrXferProcessor will be ignored.
  229. */
  230. kIgnoresInputColor = 0x4,
  231. /**
  232. * The destination color will be provided to the fragment processor using a texture. This is
  233. * additional information about the implementation of kReadsDstInShader.
  234. */
  235. kRequiresDstTexture = 0x10,
  236. /**
  237. * If set, each pixel can only be touched once during a draw (e.g., because we have a dst
  238. * texture or because we need an xfer barrier).
  239. */
  240. kRequiresNonOverlappingDraws = 0x20,
  241. };
  242. GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties);
  243. static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*,
  244. const GrProcessorAnalysisColor&,
  245. GrProcessorAnalysisCoverage,
  246. bool hasMixedSamples,
  247. const GrCaps& caps,
  248. GrClampType);
  249. static AnalysisProperties GetAnalysisProperties(const GrXPFactory*,
  250. const GrProcessorAnalysisColor&,
  251. const GrProcessorAnalysisCoverage&,
  252. const GrCaps&,
  253. GrClampType);
  254. protected:
  255. constexpr GrXPFactory() {}
  256. private:
  257. virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
  258. GrProcessorAnalysisCoverage,
  259. bool hasMixedSamples,
  260. const GrCaps&,
  261. GrClampType) const = 0;
  262. /**
  263. * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
  264. * inferred by the base class based on kReadsDstInShader and the caps.
  265. */
  266. virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
  267. const GrProcessorAnalysisCoverage&,
  268. const GrCaps&,
  269. GrClampType) const = 0;
  270. };
  271. #if defined(__GNUC__)
  272. #pragma GCC diagnostic pop
  273. #endif
  274. #if defined(__clang__)
  275. #pragma clang diagnostic pop
  276. #endif
  277. GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties);
  278. #endif