blob: 24f987d0ce535147e166526d95d355d9af681545 [file] [log] [blame]
tomhudson@google.com93813632011-10-27 20:21:16 +00001/*
2 * Copyright 2011 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 GrDrawState_DEFINED
9#define GrDrawState_DEFINED
10
egdaniel89af44a2014-09-26 06:15:04 -070011
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000012#include "GrBlend.h"
egdanielc0648242014-09-22 13:17:02 -070013#include "GrDrawTargetCaps.h"
bsalomon6251d172014-10-15 10:50:36 -070014#include "GrGeometryProcessor.h"
bsalomonf96ba022014-09-17 08:05:40 -070015#include "GrGpuResourceRef.h"
bsalomonae59b772014-11-19 08:23:49 -080016#include "GrFragmentStage.h"
egdanielb6cbc382014-11-13 11:00:34 -080017#include "GrProcOptInfo.h"
egdaniel89af44a2014-09-26 06:15:04 -070018#include "GrRenderTarget.h"
19#include "GrStencil.h"
egdaniel95131432014-12-09 11:15:43 -080020#include "GrXferProcessor.h"
egdaniel89af44a2014-09-26 06:15:04 -070021#include "SkMatrix.h"
egdaniel87509242014-12-17 13:37:13 -080022#include "effects/GrCoverageSetOpXP.h"
egdaniel080e6732014-12-22 07:35:52 -080023#include "effects/GrDisableColorXP.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrPorterDuffXferProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000025#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000026
egdaniel89af44a2014-09-26 06:15:04 -070027class GrDrawTargetCaps;
egdaniel89af44a2014-09-26 06:15:04 -070028class GrPaint;
29class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070030
joshualitt9853cce2014-11-17 14:22:48 -080031class GrDrawState {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000032public:
egdaniel69bb90c2014-11-11 07:32:45 -080033 GrDrawState() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000034 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000035 this->reset();
36 }
tomhudson@google.com93813632011-10-27 20:21:16 +000037
egdaniel69bb90c2014-11-11 07:32:45 -080038 GrDrawState(const SkMatrix& initialViewMatrix) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000039 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000040 this->reset(initialViewMatrix);
41 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000042
43 /**
44 * Copies another draw state.
45 **/
joshualitt9853cce2014-11-17 14:22:48 -080046 GrDrawState(const GrDrawState& state) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000047 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000048 *this = state;
49 }
50
bsalomon@google.com137f1342013-05-29 21:27:53 +000051 /**
52 * Copies another draw state with a preconcat to the view matrix.
53 **/
bsalomon8f727332014-08-05 07:50:06 -070054 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000055
egdaniel170f90b2014-09-16 12:54:40 -070056 virtual ~GrDrawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +000057
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000058 /**
joshualittb0a8a372014-09-23 09:50:21 -070059 * Resets to the default state. GrProcessors will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000060 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000061 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000062
bsalomon@google.com137f1342013-05-29 21:27:53 +000063 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000064
65 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000066 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
67 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070068 * equivalents are set to default values with the exception of vertex attribute state which
69 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000070 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000071 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000072
jvanverth@google.com9b855c72013-03-01 18:21:22 +000073 /// @}
74
jvanverth@google.com9b855c72013-03-01 18:21:22 +000075 /**
bsalomon62c447d2014-08-08 08:08:50 -070076 * Depending on features available in the underlying 3D API and the color blend mode requested
77 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
78 * the fragment shader.
79 *
80 * This function considers the current draw state and the draw target's capabilities to
81 * determine whether coverage can be handled correctly. This function assumes that the caller
joshualitt2e3b3e32014-12-09 13:31:14 -080082 * intends to specify fractional pixel coverage via a primitive processor but may not have
83 * specified it yet.
bsalomon62c447d2014-08-08 08:08:50 -070084 */
joshualitt2e3b3e32014-12-09 13:31:14 -080085 bool canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const;
bsalomon62c447d2014-08-08 08:08:50 -070086
egdaniel89af44a2014-09-26 06:15:04 -070087 /**
egdanielcd8b6302014-11-11 14:46:05 -080088 * This function returns true if the render target destination pixel values will be read for
89 * blending during draw.
90 */
joshualitt56995b52014-12-11 15:44:02 -080091 bool willBlendWithDst(const GrPrimitiveProcessor*) const;
joshualittbd769d02014-09-04 08:56:46 -070092
bsalomon@google.com2401ae82012-01-17 21:03:05 +000093 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +000094 /// @name Effect Stages
joshualittb0a8a372014-09-23 09:50:21 -070095 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
96 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000097 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
98 /// the fragment position, local coordinates).
99 ///
100 /// The stages are divided into two sets, color-computing and coverage-computing. The final
101 /// color stage produces the final pixel color. The coverage-computing stages function exactly
102 /// as the color-computing but the output of the final coverage stage is treated as a fractional
103 /// pixel coverage rather than as input to the src/dst color blend step.
104 ///
105 /// The input color to the first color-stage is either the constant color or interpolated
106 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
107 /// (usually full-coverage) or interpolated per-vertex coverage.
108 ///
109 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
110 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000111 ////
112
egdaniel89af44a2014-09-26 06:15:04 -0700113 int numColorStages() const { return fColorStages.count(); }
114 int numCoverageStages() const { return fCoverageStages.count(); }
joshualitt4dd99882014-11-11 08:51:30 -0800115 int numFragmentStages() const { return this->numColorStages() + this->numCoverageStages(); }
egdaniel378092f2014-12-03 10:40:13 -0800116
117 const GrXPFactory* getXPFactory() const { return fXPFactory.get(); }
118
egdaniel89af44a2014-09-26 06:15:04 -0700119 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; }
120 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; }
121
122 /**
123 * Checks whether any of the effects will read the dst pixel color.
joshualitt2e3b3e32014-12-09 13:31:14 -0800124 * TODO remove when we have an XP
egdaniel89af44a2014-09-26 06:15:04 -0700125 */
joshualitt56995b52014-12-11 15:44:02 -0800126 bool willEffectReadDstColor(const GrPrimitiveProcessor*) const;
egdaniel89af44a2014-09-26 06:15:04 -0700127
egdaniel95131432014-12-09 11:15:43 -0800128 /**
129 * The xfer processor factory.
130 */
131 const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) {
132 fXPFactory.reset(SkRef(xpFactory));
133 return xpFactory;
134 }
135
136 void setPorterDuffXPFactory(SkXfermode::Mode mode) {
137 fXPFactory.reset(GrPorterDuffXPFactory::Create(mode));
138 }
139
140 void setPorterDuffXPFactory(GrBlendCoeff src, GrBlendCoeff dst) {
141 fXPFactory.reset(GrPorterDuffXPFactory::Create(src, dst));
142 }
143
egdaniel87509242014-12-17 13:37:13 -0800144 void setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage = false) {
145 fXPFactory.reset(GrCoverageSetOpXPFactory::Create(regionOp, invertCoverage));
146 }
147
egdaniel080e6732014-12-22 07:35:52 -0800148 void setDisableColorXPFactory() {
149 fXPFactory.reset(GrDisableColorXPFactory::Create());
150 }
151
joshualittb0a8a372014-09-23 09:50:21 -0700152 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700153 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700154 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect));
egdanielb6cbc382014-11-13 11:00:34 -0800155 fColorProcInfoValid = false;
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000156 return effect;
157 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000158
joshualittb0a8a372014-09-23 09:50:21 -0700159 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700160 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700161 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect));
egdanielb6cbc382014-11-13 11:00:34 -0800162 fCoverageProcInfoValid = false;
bsalomon@google.comadc65362013-01-28 14:26:09 +0000163 return effect;
164 }
165
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000166 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000167 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000168 */
joshualittb0a8a372014-09-23 09:50:21 -0700169 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
170 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000171 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000172
joshualittb0a8a372014-09-23 09:50:21 -0700173 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
174 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000175 }
176
joshualittb0a8a372014-09-23 09:50:21 -0700177 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000178 const SkMatrix& matrix,
179 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -0700180 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
181 }
182
183 void addCoverageTextureProcessor(GrTexture* texture,
184 const SkMatrix& matrix,
185 const GrTextureParams& params) {
186 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000187 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000188
robertphillips@google.com972265d2012-06-13 18:49:30 +0000189 /**
bsalomon9b536522014-09-05 09:18:51 -0700190 * When this object is destroyed it will remove any color/coverage effects from the draw state
191 * that were added after its constructor.
192 *
193 * This class has strange behavior around geometry processor. If there is a GP on the draw state
194 * it will assert that the GP is not modified until after the destructor of the ARE. If the
195 * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the
196 * destructor.
197 *
198 * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add
199 * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState
200 * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred
201 * execution state and GrOptDrawState always will be (and will be immutable and therefore
202 * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore
203 * the GP.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000204 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000205 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000206 public:
bsalomon9b536522014-09-05 09:18:51 -0700207 AutoRestoreEffects()
208 : fDrawState(NULL)
bsalomon9b536522014-09-05 09:18:51 -0700209 , fColorEffectCnt(0)
210 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000211
bsalomon9b536522014-09-05 09:18:51 -0700212 AutoRestoreEffects(GrDrawState* ds)
213 : fDrawState(NULL)
bsalomon9b536522014-09-05 09:18:51 -0700214 , fColorEffectCnt(0)
215 , fCoverageEffectCnt(0) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000216 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000217 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000218
219 ~AutoRestoreEffects() { this->set(NULL); }
220
bsalomon8f727332014-08-05 07:50:06 -0700221 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000222
bsalomon49f085d2014-09-05 13:34:00 -0700223 bool isSet() const { return SkToBool(fDrawState); }
bsalomon8af05232014-06-03 06:34:58 -0700224
robertphillips@google.com972265d2012-06-13 18:49:30 +0000225 private:
bsalomon9b536522014-09-05 09:18:51 -0700226 GrDrawState* fDrawState;
bsalomon9b536522014-09-05 09:18:51 -0700227 int fColorEffectCnt;
228 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000229 };
230
joshualitta58fe352014-10-27 08:39:00 -0700231 /**
232 * AutoRestoreStencil
233 *
234 * This simple struct saves and restores the stencil settings
235 */
236 class AutoRestoreStencil : public ::SkNoncopyable {
237 public:
238 AutoRestoreStencil() : fDrawState(NULL) {}
239
240 AutoRestoreStencil(GrDrawState* ds) : fDrawState(NULL) { this->set(ds); }
241
242 ~AutoRestoreStencil() { this->set(NULL); }
243
244 void set(GrDrawState* ds) {
245 if (fDrawState) {
246 fDrawState->setStencil(fStencilSettings);
247 }
248 fDrawState = ds;
249 if (ds) {
250 fStencilSettings = ds->getStencil();
251 }
252 }
253
254 bool isSet() const { return SkToBool(fDrawState); }
255
256 private:
257 GrDrawState* fDrawState;
258 GrStencilSettings fStencilSettings;
259 };
260
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000261 /// @}
262
263 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000264 /// @name Blending
265 ////
266
egdaniel89af44a2014-09-26 06:15:04 -0700267 /**
268 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
269 * coverage before the blend will give the correct final destination color. In general it
270 * will not as coverage is applied after blending.
271 */
272 bool canTweakAlphaForCoverage() const;
273
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000274 /// @}
275
276 ///////////////////////////////////////////////////////////////////////////
277 /// @name View Matrix
278 ////
279
280 /**
egdaniel89af44a2014-09-26 06:15:04 -0700281 * Retrieves the current view matrix
282 * @return the current view matrix.
283 */
284 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
285
286 /**
287 * Retrieves the inverse of the current view matrix.
288 *
289 * If the current view matrix is invertible, return true, and if matrix
290 * is non-null, copy the inverse into it. If the current view matrix is
291 * non-invertible, return false and ignore the matrix parameter.
292 *
293 * @param matrix if not null, will receive a copy of the current inverse.
294 */
295 bool getViewInverse(SkMatrix* matrix) const {
296 SkMatrix inverse;
297 if (fViewMatrix.invert(&inverse)) {
298 if (matrix) {
299 *matrix = inverse;
300 }
301 return true;
302 }
303 return false;
304 }
305
306 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000307 * Sets the view matrix to identity and updates any installed effects to compensate for the
308 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000309 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000310 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000311
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000312 ////////////////////////////////////////////////////////////////////////////
313
314 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000315 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000316 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000317 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000318 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000319 public:
320 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000321
bsalomon@google.comc7818882013-03-20 19:19:53 +0000322 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000323 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000324 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000325 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000326
327 ~AutoViewMatrixRestore() { this->restore(); }
328
bsalomon@google.coma8347462012-10-08 18:59:39 +0000329 /**
330 * Can be called prior to destructor to restore the original matrix.
331 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000332 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000333
bsalomon@google.comc7818882013-03-20 19:19:53 +0000334 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000335
bsalomon@google.com137f1342013-05-29 21:27:53 +0000336 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
337 is not invertible. */
338 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000339
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000340 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000341 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
342
joshualittb0a8a372014-09-23 09:50:21 -0700343 GrDrawState* fDrawState;
344 SkMatrix fViewMatrix;
345 int fNumColorStages;
joshualitta5305a12014-10-10 17:47:00 -0700346 SkAutoSTArray<8, GrFragmentStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000347 };
348
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000349 /// @}
350
351 ///////////////////////////////////////////////////////////////////////////
352 /// @name Render Target
353 ////
354
355 /**
egdaniel89af44a2014-09-26 06:15:04 -0700356 * Retrieves the currently set render-target.
357 *
358 * @return The currently set render target.
359 */
bsalomon37dd3312014-11-03 08:47:23 -0800360 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700361
362 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000363 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000364 *
365 * @param target The render target to set.
366 */
bsalomonae59b772014-11-19 08:23:49 -0800367 void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef(target)); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000368
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000369 /// @}
370
371 ///////////////////////////////////////////////////////////////////////////
372 /// @name Stencil
373 ////
374
egdaniel89af44a2014-09-26 06:15:04 -0700375 const GrStencilSettings& getStencil() const { return fStencilSettings; }
376
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000377 /**
378 * Sets the stencil settings to use for the next draw.
379 * Changing the clip has the side-effect of possibly zeroing
380 * out the client settable stencil bits. So multipass algorithms
381 * using stencil should not change the clip between passes.
382 * @param settings the stencil settings to use.
383 */
bsalomon04ddf892014-11-19 12:36:22 -0800384 void setStencil(const GrStencilSettings& settings) { fStencilSettings = settings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000385
386 /**
387 * Shortcut to disable stencil testing and ops.
388 */
bsalomon04ddf892014-11-19 12:36:22 -0800389 void disableStencil() { fStencilSettings.setDisabled(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000390
bsalomon2ed5ef82014-07-07 08:44:05 -0700391 GrStencilSettings* stencil() { return &fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000392
393 /// @}
394
395 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000396 /// @name State Flags
397 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000398
egdaniel89af44a2014-09-26 06:15:04 -0700399 /**
400 * Flags that affect rendering. Controlled using enable/disableState(). All
401 * default to disabled.
402 */
403 enum StateBits {
404 /**
405 * Perform dithering. TODO: Re-evaluate whether we need this bit
406 */
407 kDither_StateBit = 0x01,
408 /**
409 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
410 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
411 * the 3D API.
412 */
413 kHWAntialias_StateBit = 0x02,
414 /**
415 * Draws will respect the clip, otherwise the clip is ignored.
416 */
417 kClip_StateBit = 0x04,
egdaniel89af44a2014-09-26 06:15:04 -0700418
egdaniel080e6732014-12-22 07:35:52 -0800419 kLast_StateBit = kClip_StateBit,
egdaniel89af44a2014-09-26 06:15:04 -0700420 };
421
egdaniel89af44a2014-09-26 06:15:04 -0700422 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
bsalomon04ddf892014-11-19 12:36:22 -0800423 bool isDither() const { return 0 != (fFlagBits & kDither_StateBit); }
424 bool isHWAntialias() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000425
426 /**
427 * Enable render state settings.
428 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000429 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000430 */
bsalomon04ddf892014-11-19 12:36:22 -0800431 void enableState(uint32_t stateBits) { fFlagBits |= stateBits; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000432
433 /**
434 * Disable render state settings.
435 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000436 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437 */
bsalomon04ddf892014-11-19 12:36:22 -0800438 void disableState(uint32_t stateBits) { fFlagBits &= ~(stateBits); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000439
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000440 /**
441 * Enable or disable stateBits based on a boolean.
442 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000443 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000444 * @param enable if true enable stateBits, otherwise disable
445 */
446 void setState(uint32_t stateBits, bool enable) {
447 if (enable) {
448 this->enableState(stateBits);
449 } else {
450 this->disableState(stateBits);
451 }
452 }
453
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000454 /// @}
455
456 ///////////////////////////////////////////////////////////////////////////
457 /// @name Face Culling
458 ////
459
egdaniel89af44a2014-09-26 06:15:04 -0700460 enum DrawFace {
461 kInvalid_DrawFace = -1,
462
463 kBoth_DrawFace,
464 kCCW_DrawFace,
465 kCW_DrawFace,
466 };
467
468 /**
469 * Gets whether the target is drawing clockwise, counterclockwise,
470 * or both faces.
471 * @return the current draw face(s).
472 */
473 DrawFace getDrawFace() const { return fDrawFace; }
474
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000475 /**
476 * Controls whether clockwise, counterclockwise, or both faces are drawn.
477 * @param face the face(s) to draw.
478 */
479 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000480 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700481 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000482 }
483
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000484 /// @}
485
486 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000487
bsalomon8f727332014-08-05 07:50:06 -0700488 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000489
490private:
joshualitt56995b52014-12-11 15:44:02 -0800491 bool isEqual(const GrDrawState& that, bool explicitLocalCoords) const;
egdaniel89af44a2014-09-26 06:15:04 -0700492
joshualitt56995b52014-12-11 15:44:02 -0800493 const GrProcOptInfo& colorProcInfo(const GrPrimitiveProcessor* pp) const {
494 this->calcColorInvariantOutput(pp);
egdaniel912b3d22014-11-17 07:45:53 -0800495 return fColorProcInfo;
496 }
497
joshualitt56995b52014-12-11 15:44:02 -0800498 const GrProcOptInfo& coverageProcInfo(const GrPrimitiveProcessor* pp) const {
499 this->calcCoverageInvariantOutput(pp);
egdaniel912b3d22014-11-17 07:45:53 -0800500 return fCoverageProcInfo;
501 }
502
egdaniel89af44a2014-09-26 06:15:04 -0700503 /**
joshualitt56995b52014-12-11 15:44:02 -0800504 * If fColorProcInfoValid is false, function calculates the invariant output for the color
505 * stages and results are stored in fColorProcInfo.
egdaniel89af44a2014-09-26 06:15:04 -0700506 */
joshualitt56995b52014-12-11 15:44:02 -0800507 void calcColorInvariantOutput(const GrPrimitiveProcessor*) const;
508
509 /**
510 * If fCoverageProcInfoValid is false, function calculates the invariant output for the coverage
511 * stages and results are stored in fCoverageProcInfo.
512 */
513 void calcCoverageInvariantOutput(const GrPrimitiveProcessor*) const;
egdaniel89af44a2014-09-26 06:15:04 -0700514
egdanielb6cbc382014-11-13 11:00:34 -0800515 /**
516 * If fColorProcInfoValid is false, function calculates the invariant output for the color
517 * stages and results are stored in fColorProcInfo.
518 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800519 void calcColorInvariantOutput(GrColor) const;
egdanielb6cbc382014-11-13 11:00:34 -0800520
521 /**
522 * If fCoverageProcInfoValid is false, function calculates the invariant output for the coverage
523 * stages and results are stored in fCoverageProcInfo.
524 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800525 void calcCoverageInvariantOutput(GrColor) const;
egdanielb6cbc382014-11-13 11:00:34 -0800526
bsalomon8f727332014-08-05 07:50:06 -0700527 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000528
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000529 // Some of the auto restore objects assume that no effects are removed during their lifetime.
530 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000531 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000532
joshualitta5305a12014-10-10 17:47:00 -0700533 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray;
egdaniel89af44a2014-09-26 06:15:04 -0700534
bsalomonae59b772014-11-19 08:23:49 -0800535 SkAutoTUnref<GrRenderTarget> fRenderTarget;
bsalomonae59b772014-11-19 08:23:49 -0800536 SkMatrix fViewMatrix;
bsalomonae59b772014-11-19 08:23:49 -0800537 uint32_t fFlagBits;
bsalomonae59b772014-11-19 08:23:49 -0800538 GrStencilSettings fStencilSettings;
bsalomonae59b772014-11-19 08:23:49 -0800539 DrawFace fDrawFace;
egdaniel378092f2014-12-03 10:40:13 -0800540 SkAutoTUnref<const GrXPFactory> fXPFactory;
bsalomonae59b772014-11-19 08:23:49 -0800541 FragmentStageArray fColorStages;
542 FragmentStageArray fCoverageStages;
egdaniel89af44a2014-09-26 06:15:04 -0700543
egdanielb6cbc382014-11-13 11:00:34 -0800544 mutable GrProcOptInfo fColorProcInfo;
545 mutable GrProcOptInfo fCoverageProcInfo;
546 mutable bool fColorProcInfoValid;
547 mutable bool fCoverageProcInfoValid;
joshualitt2e3b3e32014-12-09 13:31:14 -0800548 mutable GrColor fColorCache;
549 mutable GrColor fCoverageCache;
joshualitt56995b52014-12-11 15:44:02 -0800550 mutable const GrPrimitiveProcessor* fColorPrimProc;
551 mutable const GrPrimitiveProcessor* fCoveragePrimProc;
egdanielb6cbc382014-11-13 11:00:34 -0800552
egdanielb109ac22014-10-07 06:45:44 -0700553 friend class GrOptDrawState;
tomhudson@google.com93813632011-10-27 20:21:16 +0000554};
555
556#endif