blob: ed8e7079cbd6b7ab508c24bada6e3bd9871cc2f0 [file] [log] [blame]
egdaniel378092f2014-12-03 10:40:13 -08001/*
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
8#ifndef GrXferProcessor_DEFINED
9#define GrXferProcessor_DEFINED
10
cdaltonee0175f2015-06-12 08:21:26 -070011#include "GrBlend.h"
egdaniel378092f2014-12-03 10:40:13 -080012#include "GrColor.h"
Brian Salomond61c9d92017-04-10 10:54:25 -040013#include "GrNonAtomicRef.h"
egdanielc2304142014-12-11 13:15:13 -080014#include "GrProcessor.h"
Hal Canaryce78bad2017-05-04 14:15:40 -040015#include "GrProcessorAnalysis.h"
bsalomon50785a32015-02-06 07:02:37 -080016#include "GrTexture.h"
egdaniel378092f2014-12-03 10:40:13 -080017#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080018
egdanielfa4cc8b2015-11-13 08:34:52 -080019class GrGLSLXferProcessor;
Hal Canaryce78bad2017-05-04 14:15:40 -040020class GrProcessorSet;
21class GrShaderCaps;
egdaniel95131432014-12-09 11:15:43 -080022
egdaniel378092f2014-12-03 10:40:13 -080023/**
cdalton9954bc32015-04-29 14:17:00 -070024 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
25 * required after a pixel has been written, before it can be safely read again.
26 */
27enum GrXferBarrierType {
bsalomoncb02b382015-08-12 11:14:50 -070028 kNone_GrXferBarrierType = 0, //<! No barrier is required
29 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
30 kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
cdalton9954bc32015-04-29 14:17:00 -070031};
bsalomoncb02b382015-08-12 11:14:50 -070032/** Should be able to treat kNone as false in boolean expressions */
33GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false);
cdalton9954bc32015-04-29 14:17:00 -070034
35/**
egdaniel378092f2014-12-03 10:40:13 -080036 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
cdaltonedbb31f2015-06-08 12:14:44 -070037 * color, and for applying any coverage. It does this by emitting fragment shader code and
38 * controlling the fixed-function blend state. When dual-source blending is available, it may also
39 * write a seconday fragment shader output color. GrXferProcessor has two modes of operation:
40 *
41 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
42 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
43 * provides shader code that blends the src and dst colors, and the base class applies coverage.
44 *
45 * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
46 * function blend state and/or secondary output, and is responsible to apply coverage on its own.
egdaniel378092f2014-12-03 10:40:13 -080047 *
48 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
49 * GrXPFactory once we have finalized the state of our draw.
50 */
Brian Salomond61c9d92017-04-10 10:54:25 -040051class GrXferProcessor : public GrProcessor, public GrNonAtomicRef<GrXferProcessor> {
egdaniel95131432014-12-09 11:15:43 -080052public:
53 /**
bsalomon6a44c6a2015-05-26 09:49:05 -070054 * A texture that contains the dst pixel values and an integer coord offset from device space
55 * to the space of the texture. Depending on GPU capabilities a DstTexture may be used by a
56 * GrXferProcessor for blending in the fragment shader.
57 */
Robert Phillipsc3757042017-05-17 13:00:14 +000058 class DstTexture {
bsalomon6a44c6a2015-05-26 09:49:05 -070059 public:
Robert Phillipsc3757042017-05-17 13:00:14 +000060 DstTexture() { fOffset.set(0, 0); }
bsalomon6a44c6a2015-05-26 09:49:05 -070061
Robert Phillipsc3757042017-05-17 13:00:14 +000062 DstTexture(const DstTexture& other) {
bsalomon6a44c6a2015-05-26 09:49:05 -070063 *this = other;
64 }
65
Robert Phillipsc3757042017-05-17 13:00:14 +000066 DstTexture(GrTexture* texture, const SkIPoint& offset)
67 : fTexture(SkSafeRef(texture)), fOffset(texture ? offset : SkIPoint{0, 0}) {}
bsalomon6a44c6a2015-05-26 09:49:05 -070068
Robert Phillipsc3757042017-05-17 13:00:14 +000069 DstTexture& operator=(const DstTexture& other) {
70 fTexture = other.fTexture;
bsalomon6a44c6a2015-05-26 09:49:05 -070071 fOffset = other.fOffset;
72 return *this;
73 }
74
Robert Phillipsc3757042017-05-17 13:00:14 +000075 bool operator==(const DstTexture& that) const {
76 return fTexture == that.fTexture && fOffset == that.fOffset;
Brian Salomon54d212e2017-03-21 14:22:38 -040077 }
Robert Phillipsc3757042017-05-17 13:00:14 +000078 bool operator!=(const DstTexture& that) const { return !(*this == that); }
Brian Salomon54d212e2017-03-21 14:22:38 -040079
bsalomon6a44c6a2015-05-26 09:49:05 -070080 const SkIPoint& offset() const { return fOffset; }
81
82 void setOffset(const SkIPoint& offset) { fOffset = offset; }
83 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
84
Robert Phillipsc3757042017-05-17 13:00:14 +000085 GrTexture* texture() const { return fTexture.get(); }
bsalomon6a44c6a2015-05-26 09:49:05 -070086
Robert Phillipsc3757042017-05-17 13:00:14 +000087 void setTexture(sk_sp<GrTexture> texture) {
88 fTexture = std::move(texture);
89 if (!fTexture) {
Brian Salomon54d212e2017-03-21 14:22:38 -040090 fOffset = {0, 0};
91 }
bsalomon6a44c6a2015-05-26 09:49:05 -070092 }
93
94 private:
Robert Phillipsc3757042017-05-17 13:00:14 +000095 sk_sp<GrTexture> fTexture;
96 SkIPoint fOffset;
bsalomon6a44c6a2015-05-26 09:49:05 -070097 };
98
99 /**
egdaniel57d3b032015-11-13 11:57:27 -0800100 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
bsalomon50785a32015-02-06 07:02:37 -0800101 * specific subclass's key.
Brian Salomon18dfa982017-04-03 16:57:43 -0400102 */
103 void getGLSLProcessorKey(const GrShaderCaps&,
104 GrProcessorKeyBuilder*,
105 const GrSurfaceOrigin* originIfDstTexture) const;
egdanielc2304142014-12-11 13:15:13 -0800106
107 /** Returns a new instance of the appropriate *GL* implementation class
108 for the given GrXferProcessor; caller is responsible for deleting
109 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800110 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
egdanielc2304142014-12-11 13:15:13 -0800111
112 /**
Brian Salomon18dfa982017-04-03 16:57:43 -0400113 * Returns the barrier type, if any, that this XP will require. Note that the possibility
114 * that a kTexture type barrier is required is handled by the GrPipeline and need not be
115 * considered by subclass overrides of this function.
cdalton9954bc32015-04-29 14:17:00 -0700116 */
Brian Salomon18dfa982017-04-03 16:57:43 -0400117 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
118 return kNone_GrXferBarrierType;
119 }
cdalton9954bc32015-04-29 14:17:00 -0700120
egdaniel95131432014-12-09 11:15:43 -0800121 struct BlendInfo {
cdaltonf4f2b442015-04-23 09:40:23 -0700122 void reset() {
cdalton8917d622015-05-06 13:40:21 -0700123 fEquation = kAdd_GrBlendEquation;
cdaltonf4f2b442015-04-23 09:40:23 -0700124 fSrcBlend = kOne_GrBlendCoeff;
125 fDstBlend = kZero_GrBlendCoeff;
126 fBlendConstant = 0;
127 fWriteColor = true;
128 }
egdaniel080e6732014-12-22 07:35:52 -0800129
bsalomonf7cc8772015-05-11 11:21:14 -0700130 SkDEBUGCODE(SkString dump() const;)
131
cdalton8917d622015-05-06 13:40:21 -0700132 GrBlendEquation fEquation;
133 GrBlendCoeff fSrcBlend;
134 GrBlendCoeff fDstBlend;
135 GrColor fBlendConstant;
136 bool fWriteColor;
egdaniel95131432014-12-09 11:15:43 -0800137 };
138
cdaltonedbb31f2015-06-08 12:14:44 -0700139 void getBlendInfo(BlendInfo* blendInfo) const;
egdaniel95131432014-12-09 11:15:43 -0800140
egdaniel95131432014-12-09 11:15:43 -0800141 bool willReadDstColor() const { return fWillReadDstColor; }
142
bsalomon50785a32015-02-06 07:02:37 -0800143 /**
cdalton86ae0a92015-06-08 15:11:04 -0700144 * If we are performing a dst read, returns whether the base class will use mixed samples to
145 * antialias the shader's final output. If not doing a dst read, the subclass is responsible
146 * for antialiasing and this returns false.
147 */
148 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
149
150 /**
egdanielc2304142014-12-11 13:15:13 -0800151 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
152 * source blending.
153 */
cdaltonedbb31f2015-06-08 12:14:44 -0700154 bool hasSecondaryOutput() const;
egdanielc2304142014-12-11 13:15:13 -0800155
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400156 bool isLCD() const { return fIsLCD; }
157
egdanielc2304142014-12-11 13:15:13 -0800158 /** Returns true if this and other processor conservatively draw identically. It can only return
159 true when the two processor are of the same subclass (i.e. they return the same object from
160 from getFactory()).
161
162 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800163 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
164 */
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400165
egdanielc2304142014-12-11 13:15:13 -0800166 bool isEqual(const GrXferProcessor& that) const {
167 if (this->classID() != that.classID()) {
168 return false;
169 }
bsalomon50785a32015-02-06 07:02:37 -0800170 if (this->fWillReadDstColor != that.fWillReadDstColor) {
171 return false;
172 }
cdalton86ae0a92015-06-08 15:11:04 -0700173 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
174 return false;
175 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400176 if (fIsLCD != that.fIsLCD) {
177 return false;
178 }
egdanielc2304142014-12-11 13:15:13 -0800179 return this->onIsEqual(that);
180 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500181
egdaniel95131432014-12-09 11:15:43 -0800182protected:
bsalomon50785a32015-02-06 07:02:37 -0800183 GrXferProcessor();
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400184 GrXferProcessor(bool willReadDstColor, bool hasMixedSamples, GrProcessorAnalysisCoverage);
egdaniel95131432014-12-09 11:15:43 -0800185
egdaniel378092f2014-12-03 10:40:13 -0800186private:
bsalomon50785a32015-02-06 07:02:37 -0800187 /**
188 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
189 * processor's GL backend implementation.
190 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500191 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
bsalomon50785a32015-02-06 07:02:37 -0800192
cdaltonf4f2b442015-04-23 09:40:23 -0700193 /**
cdaltonedbb31f2015-06-08 12:14:44 -0700194 * If we are not performing a dst read, returns whether the subclass will set a secondary
cdalton86ae0a92015-06-08 15:11:04 -0700195 * output. When using dst reads, the base class controls the secondary output and this method
cdaltonedbb31f2015-06-08 12:14:44 -0700196 * will not be called.
197 */
198 virtual bool onHasSecondaryOutput() const { return false; }
199
200 /**
201 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
cdalton86ae0a92015-06-08 15:11:04 -0700202 * subclass. When using dst reads, the base class controls the fixed-function blend state and
203 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
cdaltonf4f2b442015-04-23 09:40:23 -0700204 */
205 virtual void onGetBlendInfo(BlendInfo*) const {}
206
egdanielc2304142014-12-11 13:15:13 -0800207 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800208
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400209 bool fWillReadDstColor;
210 bool fDstReadUsesMixedSamples;
211 bool fIsLCD;
egdaniel95131432014-12-09 11:15:43 -0800212
egdaniel378092f2014-12-03 10:40:13 -0800213 typedef GrFragmentProcessor INHERITED;
214};
215
216/**
217 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
218 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
219 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
220 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
221 * the draw.
222 *
223 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
224 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
225 * blend with the destination color.
Brian Salomona1633922017-01-09 11:46:10 -0500226 *
227 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
228 * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
229 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
egdaniel378092f2014-12-03 10:40:13 -0800230 */
Brian Salomona1633922017-01-09 11:46:10 -0500231
232// In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
233// GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
234// since these objects have no need for destructors. However, GCC and clang throw a warning when a
235// class has virtual functions and a non-virtual destructor. We suppress that warning here and
236// for the subclasses.
237#if defined(__GNUC__) || defined(__clang)
238#pragma GCC diagnostic push
239#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
240#endif
241class GrXPFactory {
egdaniel378092f2014-12-03 10:40:13 -0800242public:
Robert Phillipsc3757042017-05-17 13:00:14 +0000243 typedef GrXferProcessor::DstTexture DstTexture;
Brian Salomon5be6c952017-01-20 19:06:29 +0000244
Brian Salomon31853842017-03-28 16:32:05 -0400245 enum class AnalysisProperties : unsigned {
246 kNone = 0x0,
Brian Salomon4fc77402017-03-30 16:48:26 -0400247 /**
248 * The fragment shader will require the destination color.
249 */
Brian Salomon31853842017-03-28 16:32:05 -0400250 kReadsDstInShader = 0x1,
Brian Salomon4fc77402017-03-30 16:48:26 -0400251 /**
252 * The op may apply coverage as alpha and still blend correctly.
253 */
254 kCompatibleWithAlphaAsCoverage = 0x2,
255 /**
256 * The color input to the GrXferProcessor will be ignored.
257 */
258 kIgnoresInputColor = 0x4,
259 /**
260 * If set overlapping stencil and cover operations can be replaced by a combined stencil
261 * followed by a combined cover.
262 */
263 kCanCombineOverlappedStencilAndCover = 0x8,
264 /**
265 * The destination color will be provided to the fragment processor using a texture. This is
266 * additional information about the implementation of kReadsDstInShader.
267 */
268 kRequiresDstTexture = 0x10,
269 /**
270 * If set overlapping draws may not be combined because a barrier must be inserted between
271 * them.
272 */
273 kRequiresBarrierBetweenOverlappingDraws = 0x20,
Brian Salomon31853842017-03-28 16:32:05 -0400274 };
275 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties);
egdaniel080e6732014-12-22 07:35:52 -0800276
Brian Salomond61c9d92017-04-10 10:54:25 -0400277 static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*,
278 const GrProcessorAnalysisColor&,
279 GrProcessorAnalysisCoverage,
280 bool hasMixedSamples,
281 const GrCaps& caps);
Brian Salomona076d872017-04-04 15:17:03 -0400282
Brian Salomon31853842017-03-28 16:32:05 -0400283 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*,
Brian Salomona811b122017-03-30 08:21:32 -0400284 const GrProcessorAnalysisColor&,
285 const GrProcessorAnalysisCoverage&,
Brian Salomon31853842017-03-28 16:32:05 -0400286 const GrCaps&);
Brian Salomon54d212e2017-03-21 14:22:38 -0400287
egdaniel915187b2014-12-05 12:58:28 -0800288protected:
Brian Salomona1633922017-01-09 11:46:10 -0500289 constexpr GrXPFactory() {}
egdaniel915187b2014-12-05 12:58:28 -0800290
egdaniel378092f2014-12-03 10:40:13 -0800291private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400292 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
293 GrProcessorAnalysisCoverage,
294 bool hasMixedSamples,
295 const GrCaps&) const = 0;
ethannicholas22793252016-01-30 09:59:10 -0800296
bsalomon50785a32015-02-06 07:02:37 -0800297 /**
Brian Salomon31853842017-03-28 16:32:05 -0400298 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
299 * inferred by the base class based on kReadsDstInShader and the caps.
bsalomon50785a32015-02-06 07:02:37 -0800300 */
Brian Salomona811b122017-03-30 08:21:32 -0400301 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
302 const GrProcessorAnalysisCoverage&,
Brian Salomon31853842017-03-28 16:32:05 -0400303 const GrCaps&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800304};
Brian Salomona1633922017-01-09 11:46:10 -0500305#if defined(__GNUC__) || defined(__clang)
306#pragma GCC diagnostic pop
307#endif
egdaniel378092f2014-12-03 10:40:13 -0800308
Brian Salomon31853842017-03-28 16:32:05 -0400309GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties);
egdaniel378092f2014-12-03 10:40:13 -0800310
Brian Salomon31853842017-03-28 16:32:05 -0400311#endif