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