blob: 98585c6a5374b311ef532b352567c158a26977eb [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
11#include "GrColor.h"
egdanielc2304142014-12-11 13:15:13 -080012#include "GrProcessor.h"
egdaniel378092f2014-12-03 10:40:13 -080013#include "GrTypes.h"
14#include "SkXfermode.h"
15
egdanielc2304142014-12-11 13:15:13 -080016class GrDrawTargetCaps;
17class GrGLCaps;
18class GrGLXferProcessor;
egdaniel95131432014-12-09 11:15:43 -080019class GrProcOptInfo;
20
egdaniel378092f2014-12-03 10:40:13 -080021/**
22 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
23 * color. It does this by emitting fragment shader code and controlling the fixed-function blend
24 * state. The inputs to its shader code are the final computed src color and fractional pixel
25 * coverage. The GrXferProcessor's shader code writes the fragment shader output color that goes
26 * into the fixed-function blend. When dual-source blending is available, it may also write a
27 * seconday fragment shader output color. When allowed by the backend API, the GrXferProcessor may
28 * read the destination color. The GrXferProcessor is responsible for setting the blend coefficients
29 * and blend constant color.
30 *
31 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
32 * GrXPFactory once we have finalized the state of our draw.
33 */
egdanielc2304142014-12-11 13:15:13 -080034class GrXferProcessor : public GrProcessor {
egdaniel95131432014-12-09 11:15:43 -080035public:
36 /**
egdanielc2304142014-12-11 13:15:13 -080037 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
38 * processor's GL backend implementation.
39 */
40 virtual void getGLProcessorKey(const GrGLCaps& caps,
41 GrProcessorKeyBuilder* b) const = 0;
42
43 /** Returns a new instance of the appropriate *GL* implementation class
44 for the given GrXferProcessor; caller is responsible for deleting
45 the object. */
46 virtual GrGLXferProcessor* createGLInstance() const = 0;
47
48 /**
egdaniel95131432014-12-09 11:15:43 -080049 * Optimizations for blending / coverage that an OptDrawState should apply to itself.
50 */
51 enum OptFlags {
52 /**
53 * No optimizations needed
54 */
55 kNone_Opt = 0,
56 /**
57 * The draw can be skipped completely.
58 */
59 kSkipDraw_OptFlag = 0x1,
60 /**
joshualitt9b989322014-12-15 14:16:27 -080061 * GrXferProcessor will ignore color, thus no need to provide
egdaniel95131432014-12-09 11:15:43 -080062 */
joshualitt9b989322014-12-15 14:16:27 -080063 kIgnoreColor_OptFlag = 0x2,
egdaniel95131432014-12-09 11:15:43 -080064 /**
joshualitt9b989322014-12-15 14:16:27 -080065 * GrXferProcessor will ignore coverage, thus no need to provide
egdaniel95131432014-12-09 11:15:43 -080066 */
joshualitt9b989322014-12-15 14:16:27 -080067 kIgnoreCoverage_OptFlag = 0x4,
egdaniel95131432014-12-09 11:15:43 -080068 /**
egdaniel54160f32014-12-15 12:38:53 -080069 * Clear color stages and override input color to that returned by getOptimizations
70 */
71 kOverrideColor_OptFlag = 0x8,
72 /**
egdaniel95131432014-12-09 11:15:43 -080073 * Set CoverageDrawing_StateBit
74 */
egdaniel54160f32014-12-15 12:38:53 -080075 kSetCoverageDrawing_OptFlag = 0x10,
egdaniel95131432014-12-09 11:15:43 -080076 };
77
78 GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags);
79
80 /**
81 * Determines which optimizations (as described by the ptFlags above) can be performed by
82 * the draw with this xfer processor. If this function is called, the xfer processor may change
egdaniel54160f32014-12-15 12:38:53 -080083 * its state to reflected the given blend optimizations. If the XP needs to see a specific input
84 * color to blend correctly, it will set the OverrideColor flag and the output parameter
85 * overrideColor will be the required value that should be passed into the XP.
egdaniel95131432014-12-09 11:15:43 -080086 * A caller who calls this function on a XP is required to honor the returned OptFlags
egdaniel54160f32014-12-15 12:38:53 -080087 * and color values for its draw.
egdaniel95131432014-12-09 11:15:43 -080088 */
egdaniel95131432014-12-09 11:15:43 -080089 // TODO: remove need for colorWriteDisabled once colorWriteDisabled is its own XP.
90 virtual OptFlags getOptimizations(const GrProcOptInfo& colorPOI,
91 const GrProcOptInfo& coveragePOI,
egdaniel95131432014-12-09 11:15:43 -080092 bool colorWriteDisabled,
93 bool doesStencilWrite,
egdaniel54160f32014-12-15 12:38:53 -080094 GrColor* overrideColor,
egdanielc2304142014-12-11 13:15:13 -080095 const GrDrawTargetCaps& caps) = 0;
egdaniel95131432014-12-09 11:15:43 -080096
97 struct BlendInfo {
98 GrBlendCoeff fSrcBlend;
99 GrBlendCoeff fDstBlend;
100 GrColor fBlendConstant;
101 };
102
103 virtual void getBlendInfo(BlendInfo* blendInfo) const = 0;
104
105 /** Will this prceossor read the destination pixel value? */
106 bool willReadDstColor() const { return fWillReadDstColor; }
107
egdanielc2304142014-12-11 13:15:13 -0800108 /**
109 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
110 * source blending.
111 */
112 virtual bool hasSecondaryOutput() const { return false; }
113
114 /** Returns true if this and other processor conservatively draw identically. It can only return
115 true when the two processor are of the same subclass (i.e. they return the same object from
116 from getFactory()).
117
118 A return value of true from isEqual() should not be used to test whether the processor would
119 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
120
121 bool isEqual(const GrXferProcessor& that) const {
122 if (this->classID() != that.classID()) {
123 return false;
124 }
125 return this->onIsEqual(that);
126 }
127
egdaniel95131432014-12-09 11:15:43 -0800128protected:
129 GrXferProcessor() : fWillReadDstColor(false) {}
130
131 /**
132 * If the prceossor subclass will read the destination pixel value then it must call this
133 * function from its constructor. Otherwise, when its generated backend-specific prceossor class
134 * attempts to generate code that reads the destination pixel it will fail.
135 */
136 void setWillReadDstColor() { fWillReadDstColor = true; }
137
egdaniel378092f2014-12-03 10:40:13 -0800138private:
egdanielc2304142014-12-11 13:15:13 -0800139 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800140
egdaniel95131432014-12-09 11:15:43 -0800141 bool fWillReadDstColor;
142
egdaniel378092f2014-12-03 10:40:13 -0800143 typedef GrFragmentProcessor INHERITED;
144};
145
egdaniel95131432014-12-09 11:15:43 -0800146GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags);
147
egdaniel378092f2014-12-03 10:40:13 -0800148/**
149 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
150 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
151 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
152 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
153 * the draw.
154 *
155 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
156 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
157 * blend with the destination color.
158 */
egdanielc016fb82014-12-03 11:41:54 -0800159class GrXPFactory : public SkRefCnt {
egdaniel378092f2014-12-03 10:40:13 -0800160public:
egdaniel95131432014-12-09 11:15:43 -0800161 virtual GrXferProcessor* createXferProcessor(const GrProcOptInfo& colorPOI,
162 const GrProcOptInfo& coveragePOI) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800163
164 /**
165 * This function returns true if the GrXferProcessor generated from this factory will be able to
166 * correctly blend when using RGB coverage. The knownColor and knownColorFlags represent the
167 * final computed color from the color stages.
168 */
169 virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const = 0;
170
egdaniel95131432014-12-09 11:15:43 -0800171 /**
172 * Depending on color blend mode requested it may or may not be possible to correctly blend with
173 * fractional pixel coverage generated by the fragment shader.
174 *
175 * This function considers the known color and coverage input into the xfer processor and
egdaniel87509242014-12-17 13:37:13 -0800176 * certain state information (colorWriteDisabled) to determine whether
egdaniel95131432014-12-09 11:15:43 -0800177 * coverage can be handled correctly.
178 */
egdaniel95131432014-12-09 11:15:43 -0800179 // TODO: remove need for colorWriteDisabled once colorWriteDisabled is its own XP.
180 virtual bool canApplyCoverage(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI,
egdaniel87509242014-12-17 13:37:13 -0800181 bool colorWriteDisabled) const = 0;
egdaniel95131432014-12-09 11:15:43 -0800182
egdaniel9e4ecdc2014-12-18 12:44:55 -0800183
184 struct InvariantOutput {
185 bool fWillBlendWithDst;
186 GrColor fBlendedColor;
187 uint32_t fBlendedColorFlags;
188 };
189
190 /**
191 * This function returns known information about the output of the xfer processor produced by
192 * this xp factory. The invariant color information returned by this function refers to the
193 * final color produced after all blending.
egdaniel95131432014-12-09 11:15:43 -0800194 */
egdaniel95131432014-12-09 11:15:43 -0800195 // TODO: remove need for colorWriteDisabled once only XP can read dst.
egdaniel9e4ecdc2014-12-18 12:44:55 -0800196 virtual void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI,
197 bool colorWriteDisabled, InvariantOutput*) const = 0;
egdaniel95131432014-12-09 11:15:43 -0800198
199 /**
200 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
201 * coverage before the blend will give the correct final destination color. In general it
202 * will not as coverage is applied after blending.
203 */
egdaniel87509242014-12-17 13:37:13 -0800204 virtual bool canTweakAlphaForCoverage() const = 0;
egdaniel95131432014-12-09 11:15:43 -0800205
egdaniel915187b2014-12-05 12:58:28 -0800206 bool isEqual(const GrXPFactory& that) const {
207 if (this->classID() != that.classID()) {
208 return false;
209 }
210 return this->onIsEqual(that);
211 }
212
213 /**
214 * Helper for down-casting to a GrXPFactory subclass
215 */
216 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
217
218 uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; }
219
220protected:
221 GrXPFactory() : fClassID(kIllegalXPFClassID) {}
222
223 template <typename XPF_SUBCLASS> void initClassID() {
224 static uint32_t kClassID = GenClassID();
225 fClassID = kClassID;
226 }
227
228 uint32_t fClassID;
229
egdaniel378092f2014-12-03 10:40:13 -0800230private:
egdaniel915187b2014-12-05 12:58:28 -0800231 virtual bool onIsEqual(const GrXPFactory&) const = 0;
232
233 static uint32_t GenClassID() {
234 // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The
235 // atomic inc returns the old value not the incremented value. So we add
236 // 1 to the returned value.
237 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1;
238 if (!id) {
239 SkFAIL("This should never wrap as it should only be called once for each GrXPFactory "
240 "subclass.");
241 }
242 return id;
243 }
244
245 enum {
246 kIllegalXPFClassID = 0,
247 };
248 static int32_t gCurrXPFClassID;
249
egdaniel378092f2014-12-03 10:40:13 -0800250 typedef GrProgramElement INHERITED;
251};
252
253#endif
254