blob: d748e7d83aac72e6ee5afe9dff65e34eb8ebca19 [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 /**
egdaniel3658f382014-09-15 07:01:59 -0700170 * We don't use supplied vertex color attributes if our blend mode is EmitCoverage or
171 * EmitTransBlack
172 */
173 bool canIgnoreColorAttribute() const;
174
175 /**
egdaniel21aed572014-08-26 12:24:06 -0700176 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
177 * coverage before the blend will give the correct final destination color. In general it
178 * will not as coverage is applied after blending.
179 */
180 bool canTweakAlphaForCoverage() const;
181
182 /**
183 * Optimizations for blending / coverage to that can be applied based on the current state.
184 */
185 enum BlendOptFlags {
186 /**
187 * No optimization
188 */
189 kNone_BlendOpt = 0,
190 /**
191 * Don't draw at all
192 */
193 kSkipDraw_BlendOptFlag = 0x1,
194 /**
195 * The coverage value does not have to be computed separately from alpha, the output
196 * color can be the modulation of the two.
197 */
198 kCoverageAsAlpha_BlendOptFlag = 0x2,
199 /**
200 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
201 * "don't cares".
202 */
203 kEmitCoverage_BlendOptFlag = 0x4,
204 /**
205 * Emit transparent black instead of the src color, no need to compute coverage.
206 */
207 kEmitTransBlack_BlendOptFlag = 0x8,
208 /**
209 * Flag used to invalidate the cached BlendOptFlags, OptSrcCoeff, and OptDstCoeff cached by
210 * the get BlendOpts function.
211 */
212 kInvalid_BlendOptFlag = 1 << 31,
213 };
214 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
215
egdaniel3658f382014-09-15 07:01:59 -0700216 /**
217 * Determines what optimizations can be applied based on the blend. The coefficients may have
218 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
219 * params that receive the tweaked coefficients. Normally the function looks at the current
220 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
221 * determine the blend optimizations that would be used if there was partial pixel coverage.
222 *
223 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
224 * playback) must call this function and respect the flags that replace the output color.
225 *
226 * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will
227 * simply returned the cached flags and coefficients. Otherwise it will calculate the values.
228 */
229 BlendOptFlags getBlendOpts(bool forceCoverage = false,
230 GrBlendCoeff* srcCoeff = NULL,
231 GrBlendCoeff* dstCoeff = NULL) const;
egdaniel21aed572014-08-26 12:24:06 -0700232 /// @}
233
234 ///////////////////////////////////////////////////////////////////////////
235 /// @name View Matrix
236 ////
237
238 /**
239 * Retrieves the current view matrix
240 * @return the current view matrix.
241 */
242 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
243
244 /**
245 * Retrieves the inverse of the current view matrix.
246 *
247 * If the current view matrix is invertible, return true, and if matrix
248 * is non-null, copy the inverse into it. If the current view matrix is
249 * non-invertible, return false and ignore the matrix parameter.
250 *
251 * @param matrix if not null, will receive a copy of the current inverse.
252 */
253 bool getViewInverse(SkMatrix* matrix) const {
254 // TODO: determine whether we really need to leave matrix unmodified
255 // at call sites when inversion fails.
256 SkMatrix inverse;
257 if (fViewMatrix.invert(&inverse)) {
258 if (matrix) {
259 *matrix = inverse;
260 }
261 return true;
262 }
263 return false;
264 }
265
266 /// @}
267
268 ///////////////////////////////////////////////////////////////////////////
269 /// @name Render Target
270 ////
271
272 /**
273 * Retrieves the currently set render-target.
274 *
275 * @return The currently set render target.
276 */
bsalomon2a9ca782014-09-05 14:27:43 -0700277 GrRenderTarget* getRenderTarget() const {
278 return static_cast<GrRenderTarget*>(fRenderTarget.getResource());
279 }
egdaniel21aed572014-08-26 12:24:06 -0700280
281 /// @}
282
283 ///////////////////////////////////////////////////////////////////////////
284 /// @name Stencil
285 ////
286
287 const GrStencilSettings& getStencil() const { return fStencilSettings; }
288
289 /// @}
290
291 ///////////////////////////////////////////////////////////////////////////
292 /// @name State Flags
293 ////
294
295 /**
296 * Flags that affect rendering. Controlled using enable/disableState(). All
297 * default to disabled.
298 */
299 enum StateBits {
300 /**
301 * Perform dithering. TODO: Re-evaluate whether we need this bit
302 */
303 kDither_StateBit = 0x01,
304 /**
305 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
306 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
307 * the 3D API.
308 */
309 kHWAntialias_StateBit = 0x02,
310 /**
311 * Draws will respect the clip, otherwise the clip is ignored.
312 */
313 kClip_StateBit = 0x04,
314 /**
315 * Disables writing to the color buffer. Useful when performing stencil
316 * operations.
317 */
318 kNoColorWrites_StateBit = 0x08,
319
320 /**
321 * Usually coverage is applied after color blending. The color is blended using the coeffs
322 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
323 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
324 * this case there is no distinction between coverage and color and the caller needs direct
325 * control over the blend coeffs. When set, there will be a single blend step controlled by
326 * setBlendFunc() which will use coverage*color as the src color.
327 */
328 kCoverageDrawing_StateBit = 0x10,
329
330 // Users of the class may add additional bits to the vector
331 kDummyStateBit,
332 kLastPublicStateBit = kDummyStateBit-1,
333 };
334
egdaniel3658f382014-09-15 07:01:59 -0700335 uint32_t getFlagBits() const { return fFlagBits; }
336
egdaniel21aed572014-08-26 12:24:06 -0700337 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); }
338
339 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); }
340 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
341 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
342 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
343 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
344
345 /// @}
346
347 ///////////////////////////////////////////////////////////////////////////
348 /// @name Face Culling
349 ////
350
351 enum DrawFace {
352 kInvalid_DrawFace = -1,
353
354 kBoth_DrawFace,
355 kCCW_DrawFace,
356 kCW_DrawFace,
357 };
358
359 /**
360 * Gets whether the target is drawing clockwise, counterclockwise,
361 * or both faces.
362 * @return the current draw face(s).
363 */
364 DrawFace getDrawFace() const { return fDrawFace; }
365
366 /// @}
367
368 ///////////////////////////////////////////////////////////////////////////
egdaniel3658f382014-09-15 07:01:59 -0700369 /// @name Hints
370 /// Hints that when provided can enable optimizations.
371 ////
372
373 enum Hints { kVertexColorsAreOpaque_Hint = 0x1, };
374
375 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
376
377 /// @}
378
379 ///////////////////////////////////////////////////////////////////////////
egdaniel21aed572014-08-26 12:24:06 -0700380
381 /** Return type for CombineIfPossible. */
382 enum CombinedState {
383 /** The GrDrawStates cannot be combined. */
384 kIncompatible_CombinedState,
385 /** Either draw state can be used in place of the other. */
386 kAOrB_CombinedState,
387 /** Use the first draw state. */
388 kA_CombinedState,
389 /** Use the second draw state. */
390 kB_CombinedState,
391 };
392
egdaniel21aed572014-08-26 12:24:06 -0700393protected:
bsalomon2a9ca782014-09-05 14:27:43 -0700394 /**
395 * Converts refs on GrGpuResources owned directly or indirectly by this GrRODrawState into
396 * pending reads and writes. This should be called when a GrDrawState is recorded into
397 * a GrDrawTarget for later execution. Subclasses of GrRODrawState may add setters. However,
398 * once this call has been made the GrRODrawState is immutable. It is also no longer copyable.
399 * In the future this conversion will automatically happen when converting a GrDrawState into
400 * an optimized draw state.
401 */
402 void convertToPendingExec();
403
404 friend class GrDrawTarget;
405
egdaniel3658f382014-09-15 07:01:59 -0700406 explicit GrRODrawState(const GrRODrawState& drawState);
407
egdaniel21aed572014-08-26 12:24:06 -0700408 bool isEqual(const GrRODrawState& that) const;
409
410 // These fields are roughly sorted by decreasing likelihood of being different in op==
bsalomon2a9ca782014-09-05 14:27:43 -0700411 GrProgramResource fRenderTarget;
egdaniel21aed572014-08-26 12:24:06 -0700412 GrColor fColor;
413 SkMatrix fViewMatrix;
414 GrColor fBlendConstant;
415 uint32_t fFlagBits;
416 const GrVertexAttrib* fVAPtr;
417 int fVACount;
egdaniel7b3d5ee2014-08-28 05:41:14 -0700418 size_t fVAStride;
egdaniel21aed572014-08-26 12:24:06 -0700419 GrStencilSettings fStencilSettings;
420 uint8_t fCoverage;
421 DrawFace fDrawFace;
422 GrBlendCoeff fSrcBlend;
423 GrBlendCoeff fDstBlend;
424
425 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
joshualittbd769d02014-09-04 08:56:46 -0700426 SkAutoTDelete<GrEffectStage> fGeometryProcessor;
egdaniel21aed572014-08-26 12:24:06 -0700427 EffectStageArray fColorStages;
428 EffectStageArray fCoverageStages;
429
egdaniel3658f382014-09-15 07:01:59 -0700430 uint32_t fHints;
431
egdaniel21aed572014-08-26 12:24:06 -0700432 mutable GrBlendCoeff fOptSrcBlend;
433 mutable GrBlendCoeff fOptDstBlend;
434 mutable BlendOptFlags fBlendOptFlags;
435
436 // This is simply a different representation of info in fVertexAttribs and thus does
437 // not need to be compared in op==.
438 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
439
440private:
egdaniel3658f382014-09-15 07:01:59 -0700441 /**
442 * Determines whether src alpha is guaranteed to be one for all src pixels
443 */
444 bool srcAlphaWillBeOne() const;
445
446 /**
447 * Helper function for getBlendOpts.
448 */
449 BlendOptFlags calcBlendOpts(bool forceCoverage = false,
450 GrBlendCoeff* srcCoeff = NULL,
451 GrBlendCoeff* dstCoeff = NULL) const;
452
egdaniel21aed572014-08-26 12:24:06 -0700453 typedef SkRefCnt INHERITED;
454};
455
456GR_MAKE_BITFIELD_OPS(GrRODrawState::BlendOptFlags);
457
458#endif