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