blob: eea6f9b98a32468a803ade71ca536b22d4f2f661 [file] [log] [blame]
egdaniel21aed572014-08-26 12:24:06 -07001/*
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 GrRODrawState_DEFINED
9#define GrRODrawState_DEFINED
10
egdaniel21aed572014-08-26 12:24:06 -070011#include "GrEffectStage.h"
bsalomon2a9ca782014-09-05 14:27:43 -070012#include "GrRenderTarget.h"
13#include "GrStencil.h"
egdaniel21aed572014-08-26 12:24:06 -070014#include "SkMatrix.h"
15
egdaniel3658f382014-09-15 07:01:59 -070016class GrDrawState;
egdaniel21aed572014-08-26 12:24:06 -070017class GrDrawTargetCaps;
18class GrPaint;
egdaniel21aed572014-08-26 12:24:06 -070019class GrTexture;
20
21/**
22 * Read-only base class for GrDrawState. This class contains all the necessary data to represent a
23 * canonical DrawState. All methods in the class are const, thus once created the data in the class
24 * cannot be changed.
25 */
26class GrRODrawState : public SkRefCnt {
27public:
28 SK_DECLARE_INST_COUNT(GrRODrawState)
29
egdaniel3658f382014-09-15 07:01:59 -070030 GrRODrawState() {}
31
32 GrRODrawState& operator= (const GrRODrawState& that);
33
egdaniel21aed572014-08-26 12:24:06 -070034 ///////////////////////////////////////////////////////////////////////////
35 /// @name Vertex Attributes
36 ////
37
38 enum {
39 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
40 };
41
42 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; }
43 int getVertexAttribCount() const { return fVACount; }
44
egdaniel7b3d5ee2014-08-28 05:41:14 -070045 size_t getVertexStride() const { return fVAStride; }
egdaniel21aed572014-08-26 12:24:06 -070046
47 /**
48 * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the
49 * binding does not appear in the current attribs. These bindings should appear only once in
50 * the attrib array.
51 */
52
53 int positionAttributeIndex() const {
54 return fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
55 }
56 int localCoordAttributeIndex() const {
57 return fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
58 }
59 int colorVertexAttributeIndex() const {
60 return fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
61 }
62 int coverageVertexAttributeIndex() const {
63 return fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
64 }
65
66 bool hasLocalCoordAttribute() const {
67 return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
68 }
69 bool hasColorVertexAttribute() const {
70 return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
71 }
72 bool hasCoverageVertexAttribute() const {
73 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
74 }
75
egdaniel3658f382014-09-15 07:01:59 -070076 const int* getFixedFunctionVertexAttribIndices() const {
77 return fFixedFunctionVertexAttribIndices;
78 }
79
egdaniel21aed572014-08-26 12:24:06 -070080 bool validateVertexAttribs() const;
81
82 /// @}
83
84 /**
85 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
86 */
87 bool hasSolidCoverage() const;
88
89 /// @}
90
91 ///////////////////////////////////////////////////////////////////////////
92 /// @name Color
93 ////
94
95 GrColor getColor() const { return fColor; }
96
97 /// @}
98
99 ///////////////////////////////////////////////////////////////////////////
100 /// @name Coverage
101 ////
102
103 uint8_t getCoverage() const { return fCoverage; }
104
105 GrColor getCoverageColor() const {
106 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
107 }
108
109 /// @}
110
111 ///////////////////////////////////////////////////////////////////////////
112 /// @name Effect Stages
113 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
114 /// shader. Its inputs are the output from the previous stage as well as some variables
115 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
116 /// the fragment position, local coordinates).
117 ///
118 /// The stages are divided into two sets, color-computing and coverage-computing. The final
119 /// color stage produces the final pixel color. The coverage-computing stages function exactly
120 /// as the color-computing but the output of the final coverage stage is treated as a fractional
121 /// pixel coverage rather than as input to the src/dst color blend step.
122 ///
123 /// The input color to the first color-stage is either the constant color or interpolated
124 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
125 /// (usually full-coverage) or interpolated per-vertex coverage.
126 ///
127 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
128 /// the color / coverage distinction.
129 ////
130
131 int numColorStages() const { return fColorStages.count(); }
132 int numCoverageStages() const { return fCoverageStages.count(); }
joshualittbd769d02014-09-04 08:56:46 -0700133 int numTotalStages() const {
134 return this->numColorStages() + this->numCoverageStages() +
135 (this->hasGeometryProcessor() ? 1 : 0);
136 }
egdaniel21aed572014-08-26 12:24:06 -0700137
bsalomon49f085d2014-09-05 13:34:00 -0700138 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualittbd769d02014-09-04 08:56:46 -0700139 const GrEffectStage* getGeometryProcessor() const { return fGeometryProcessor.get(); }
egdaniel21aed572014-08-26 12:24:06 -0700140 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
141 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
142
143 /**
144 * Checks whether any of the effects will read the dst pixel color.
145 */
146 bool willEffectReadDstColor() const;
147
148 /// @}
149
150 ///////////////////////////////////////////////////////////////////////////
151 /// @name Blending
152 ////
153
154 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
155 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
156
157 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
158 GrBlendCoeff* dstBlendCoeff) const {
159 *srcBlendCoeff = fSrcBlend;
160 *dstBlendCoeff = fDstBlend;
161 }
162
163 /**
164 * Retrieves the last value set by setBlendConstant()
165 * @return the blending constant value
166 */
167 GrColor getBlendConstant() const { return fBlendConstant; }
168
169 /**
170 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
171 * coverage before the blend will give the correct final destination color. In general it
172 * will not as coverage is applied after blending.
173 */
174 bool canTweakAlphaForCoverage() const;
175
egdaniel21aed572014-08-26 12:24:06 -0700176 /// @}
177
178 ///////////////////////////////////////////////////////////////////////////
179 /// @name View Matrix
180 ////
181
182 /**
183 * Retrieves the current view matrix
184 * @return the current view matrix.
185 */
186 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
187
188 /**
189 * Retrieves the inverse of the current view matrix.
190 *
191 * If the current view matrix is invertible, return true, and if matrix
192 * is non-null, copy the inverse into it. If the current view matrix is
193 * non-invertible, return false and ignore the matrix parameter.
194 *
195 * @param matrix if not null, will receive a copy of the current inverse.
196 */
197 bool getViewInverse(SkMatrix* matrix) const {
198 // TODO: determine whether we really need to leave matrix unmodified
199 // at call sites when inversion fails.
200 SkMatrix inverse;
201 if (fViewMatrix.invert(&inverse)) {
202 if (matrix) {
203 *matrix = inverse;
204 }
205 return true;
206 }
207 return false;
208 }
209
210 /// @}
211
212 ///////////////////////////////////////////////////////////////////////////
213 /// @name Render Target
214 ////
215
216 /**
217 * Retrieves the currently set render-target.
218 *
219 * @return The currently set render target.
220 */
bsalomon2a9ca782014-09-05 14:27:43 -0700221 GrRenderTarget* getRenderTarget() const {
222 return static_cast<GrRenderTarget*>(fRenderTarget.getResource());
223 }
egdaniel21aed572014-08-26 12:24:06 -0700224
225 /// @}
226
227 ///////////////////////////////////////////////////////////////////////////
228 /// @name Stencil
229 ////
230
231 const GrStencilSettings& getStencil() const { return fStencilSettings; }
232
233 /// @}
234
235 ///////////////////////////////////////////////////////////////////////////
236 /// @name State Flags
237 ////
238
239 /**
240 * Flags that affect rendering. Controlled using enable/disableState(). All
241 * default to disabled.
242 */
243 enum StateBits {
244 /**
245 * Perform dithering. TODO: Re-evaluate whether we need this bit
246 */
247 kDither_StateBit = 0x01,
248 /**
249 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
250 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
251 * the 3D API.
252 */
253 kHWAntialias_StateBit = 0x02,
254 /**
255 * Draws will respect the clip, otherwise the clip is ignored.
256 */
257 kClip_StateBit = 0x04,
258 /**
259 * Disables writing to the color buffer. Useful when performing stencil
260 * operations.
261 */
262 kNoColorWrites_StateBit = 0x08,
263
264 /**
265 * Usually coverage is applied after color blending. The color is blended using the coeffs
266 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
267 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
268 * this case there is no distinction between coverage and color and the caller needs direct
269 * control over the blend coeffs. When set, there will be a single blend step controlled by
270 * setBlendFunc() which will use coverage*color as the src color.
271 */
272 kCoverageDrawing_StateBit = 0x10,
273
274 // Users of the class may add additional bits to the vector
275 kDummyStateBit,
276 kLastPublicStateBit = kDummyStateBit-1,
277 };
278
egdaniel3658f382014-09-15 07:01:59 -0700279 uint32_t getFlagBits() const { return fFlagBits; }
280
egdaniel21aed572014-08-26 12:24:06 -0700281 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); }
282
283 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); }
284 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
285 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
286 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
287 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
288
289 /// @}
290
291 ///////////////////////////////////////////////////////////////////////////
292 /// @name Face Culling
293 ////
294
295 enum DrawFace {
296 kInvalid_DrawFace = -1,
297
298 kBoth_DrawFace,
299 kCCW_DrawFace,
300 kCW_DrawFace,
301 };
302
303 /**
304 * Gets whether the target is drawing clockwise, counterclockwise,
305 * or both faces.
306 * @return the current draw face(s).
307 */
308 DrawFace getDrawFace() const { return fDrawFace; }
309
310 /// @}
311
312 ///////////////////////////////////////////////////////////////////////////
egdaniel3658f382014-09-15 07:01:59 -0700313 /// @name Hints
314 /// Hints that when provided can enable optimizations.
315 ////
316
317 enum Hints { kVertexColorsAreOpaque_Hint = 0x1, };
318
319 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
320
321 /// @}
322
323 ///////////////////////////////////////////////////////////////////////////
egdaniel21aed572014-08-26 12:24:06 -0700324
325 /** Return type for CombineIfPossible. */
326 enum CombinedState {
327 /** The GrDrawStates cannot be combined. */
328 kIncompatible_CombinedState,
329 /** Either draw state can be used in place of the other. */
330 kAOrB_CombinedState,
331 /** Use the first draw state. */
332 kA_CombinedState,
333 /** Use the second draw state. */
334 kB_CombinedState,
335 };
336
egdaniel21aed572014-08-26 12:24:06 -0700337protected:
bsalomon2a9ca782014-09-05 14:27:43 -0700338 /**
339 * Converts refs on GrGpuResources owned directly or indirectly by this GrRODrawState into
340 * pending reads and writes. This should be called when a GrDrawState is recorded into
341 * a GrDrawTarget for later execution. Subclasses of GrRODrawState may add setters. However,
342 * once this call has been made the GrRODrawState is immutable. It is also no longer copyable.
343 * In the future this conversion will automatically happen when converting a GrDrawState into
344 * an optimized draw state.
345 */
346 void convertToPendingExec();
347
348 friend class GrDrawTarget;
349
egdaniel3658f382014-09-15 07:01:59 -0700350 explicit GrRODrawState(const GrRODrawState& drawState);
351
egdaniel21aed572014-08-26 12:24:06 -0700352 bool isEqual(const GrRODrawState& that) const;
353
egdaniel170f90b2014-09-16 12:54:40 -0700354 /**
355 * Optimizations for blending / coverage to that can be applied based on the current state.
356 */
357 enum BlendOptFlags {
358 /**
359 * No optimization
360 */
361 kNone_BlendOpt = 0,
362 /**
363 * Don't draw at all
364 */
365 kSkipDraw_BlendOptFlag = 0x1,
366 /**
367 * The coverage value does not have to be computed separately from alpha, the the output
368 * color can be the modulation of the two.
369 */
370 kCoverageAsAlpha_BlendOptFlag = 0x2,
371 /**
372 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
373 * "don't cares".
374 */
375 kEmitCoverage_BlendOptFlag = 0x4,
376 /**
377 * Emit transparent black instead of the src color, no need to compute coverage.
378 */
379 kEmitTransBlack_BlendOptFlag = 0x8,
380 };
381 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
382
383 /**
384 * Determines what optimizations can be applied based on the blend. The coefficients may have
385 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
386 * params that receive the tweaked coefficients. Normally the function looks at the current
387 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
388 * determine the blend optimizations that would be used if there was partial pixel coverage.
389 *
390 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
391 * playback) must call this function and respect the flags that replace the output color.
392 *
393 * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will
394 * simply returned the cached flags and coefficients. Otherwise it will calculate the values.
395 */
396 BlendOptFlags getBlendOpts(bool forceCoverage = false,
397 GrBlendCoeff* srcCoeff = NULL,
398 GrBlendCoeff* dstCoeff = NULL) const;
399
egdaniel21aed572014-08-26 12:24:06 -0700400 // These fields are roughly sorted by decreasing likelihood of being different in op==
bsalomon2a9ca782014-09-05 14:27:43 -0700401 GrProgramResource fRenderTarget;
egdaniel21aed572014-08-26 12:24:06 -0700402 GrColor fColor;
403 SkMatrix fViewMatrix;
404 GrColor fBlendConstant;
405 uint32_t fFlagBits;
406 const GrVertexAttrib* fVAPtr;
407 int fVACount;
egdaniel7b3d5ee2014-08-28 05:41:14 -0700408 size_t fVAStride;
egdaniel21aed572014-08-26 12:24:06 -0700409 GrStencilSettings fStencilSettings;
410 uint8_t fCoverage;
411 DrawFace fDrawFace;
412 GrBlendCoeff fSrcBlend;
413 GrBlendCoeff fDstBlend;
414
415 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
joshualittbd769d02014-09-04 08:56:46 -0700416 SkAutoTDelete<GrEffectStage> fGeometryProcessor;
egdaniel21aed572014-08-26 12:24:06 -0700417 EffectStageArray fColorStages;
418 EffectStageArray fCoverageStages;
419
egdaniel3658f382014-09-15 07:01:59 -0700420 uint32_t fHints;
421
egdaniel21aed572014-08-26 12:24:06 -0700422 // This is simply a different representation of info in fVertexAttribs and thus does
423 // not need to be compared in op==.
424 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
425
426private:
egdaniel3658f382014-09-15 07:01:59 -0700427 /**
428 * Determines whether src alpha is guaranteed to be one for all src pixels
429 */
430 bool srcAlphaWillBeOne() const;
431
egdaniel21aed572014-08-26 12:24:06 -0700432 typedef SkRefCnt INHERITED;
433};
434
435GR_MAKE_BITFIELD_OPS(GrRODrawState::BlendOptFlags);
436
437#endif