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