blob: 17bd0f43df8fe0e6ab80a57871420b001d9debfa [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"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrPorterDuffXferProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000023#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000024
egdaniel89af44a2014-09-26 06:15:04 -070025class GrDrawTargetCaps;
egdaniel89af44a2014-09-26 06:15:04 -070026class GrPaint;
27class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070028
joshualitt9853cce2014-11-17 14:22:48 -080029class GrDrawState {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000030public:
egdaniel69bb90c2014-11-11 07:32:45 -080031 GrDrawState() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000032 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000033 this->reset();
34 }
tomhudson@google.com93813632011-10-27 20:21:16 +000035
egdaniel69bb90c2014-11-11 07:32:45 -080036 GrDrawState(const SkMatrix& initialViewMatrix) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000037 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000038 this->reset(initialViewMatrix);
39 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000040
41 /**
42 * Copies another draw state.
43 **/
joshualitt9853cce2014-11-17 14:22:48 -080044 GrDrawState(const GrDrawState& state) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000045 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000046 *this = state;
47 }
48
bsalomon@google.com137f1342013-05-29 21:27:53 +000049 /**
50 * Copies another draw state with a preconcat to the view matrix.
51 **/
bsalomon8f727332014-08-05 07:50:06 -070052 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000053
egdaniel170f90b2014-09-16 12:54:40 -070054 virtual ~GrDrawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +000055
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000056 /**
joshualittb0a8a372014-09-23 09:50:21 -070057 * Resets to the default state. GrProcessors will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000058 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000059 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000060
bsalomon@google.com137f1342013-05-29 21:27:53 +000061 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000062
63 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000064 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
65 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070066 * equivalents are set to default values with the exception of vertex attribute state which
67 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000068 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000069 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000070
71 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000072 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000073 ////
74
joshualitt2dd1ae02014-12-03 06:24:10 -080075 // TODO when we move this info off of GrGeometryProcessor, delete these
egdaniel89af44a2014-09-26 06:15:04 -070076 bool hasLocalCoordAttribute() const {
joshualitt2dd1ae02014-12-03 06:24:10 -080077 return this->hasGeometryProcessor() && this->getGeometryProcessor()->hasLocalCoords();
egdaniel89af44a2014-09-26 06:15:04 -070078 }
79 bool hasColorVertexAttribute() const {
joshualitt2dd1ae02014-12-03 06:24:10 -080080 return this->hasGeometryProcessor() && this->getGeometryProcessor()->hasVertexColor();
egdaniel89af44a2014-09-26 06:15:04 -070081 }
82 bool hasCoverageVertexAttribute() const {
joshualitt2dd1ae02014-12-03 06:24:10 -080083 return this->hasGeometryProcessor() && this->getGeometryProcessor()->hasVertexCoverage();
egdaniel89af44a2014-09-26 06:15:04 -070084 }
85
jvanverth@google.com9b855c72013-03-01 18:21:22 +000086 /// @}
87
jvanverth@google.com9b855c72013-03-01 18:21:22 +000088 /**
bsalomon62c447d2014-08-08 08:08:50 -070089 * Depending on features available in the underlying 3D API and the color blend mode requested
90 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
91 * the fragment shader.
92 *
93 * This function considers the current draw state and the draw target's capabilities to
94 * determine whether coverage can be handled correctly. This function assumes that the caller
joshualitt2e3b3e32014-12-09 13:31:14 -080095 * intends to specify fractional pixel coverage via a primitive processor but may not have
96 * specified it yet.
bsalomon62c447d2014-08-08 08:08:50 -070097 */
joshualitt2e3b3e32014-12-09 13:31:14 -080098 bool canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const;
bsalomon62c447d2014-08-08 08:08:50 -070099
egdaniel89af44a2014-09-26 06:15:04 -0700100 /**
101 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
102 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800103 bool hasSolidCoverage(GrColor coverage) const;
egdaniel89af44a2014-09-26 06:15:04 -0700104
egdanielcd8b6302014-11-11 14:46:05 -0800105 /**
106 * This function returns true if the render target destination pixel values will be read for
107 * blending during draw.
108 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800109 bool willBlendWithDst(GrColor color, GrColor coverage) const;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000110
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000111 /// @}
112
joshualittbd769d02014-09-04 08:56:46 -0700113 /**
114 * The geometry processor is the sole element of the skia pipeline which can use the vertex,
115 * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader
116 * but is never put in the color processing pipeline.
117 */
118
joshualittb0a8a372014-09-23 09:50:21 -0700119 const GrGeometryProcessor* setGeometryProcessor(const GrGeometryProcessor* geometryProcessor) {
120 SkASSERT(geometryProcessor);
joshualittbd769d02014-09-04 08:56:46 -0700121 SkASSERT(!this->hasGeometryProcessor());
joshualitta5305a12014-10-10 17:47:00 -0700122 fGeometryProcessor.reset(SkRef(geometryProcessor));
egdanielb6cbc382014-11-13 11:00:34 -0800123 fCoverageProcInfoValid = false;
joshualittb0a8a372014-09-23 09:50:21 -0700124 return geometryProcessor;
joshualittbd769d02014-09-04 08:56:46 -0700125 }
126
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000127 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000128 /// @name Effect Stages
joshualittb0a8a372014-09-23 09:50:21 -0700129 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
130 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000131 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
132 /// the fragment position, local coordinates).
133 ///
134 /// The stages are divided into two sets, color-computing and coverage-computing. The final
135 /// color stage produces the final pixel color. The coverage-computing stages function exactly
136 /// as the color-computing but the output of the final coverage stage is treated as a fractional
137 /// pixel coverage rather than as input to the src/dst color blend step.
138 ///
139 /// The input color to the first color-stage is either the constant color or interpolated
140 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
141 /// (usually full-coverage) or interpolated per-vertex coverage.
142 ///
143 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
144 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000145 ////
146
egdaniel89af44a2014-09-26 06:15:04 -0700147 int numColorStages() const { return fColorStages.count(); }
148 int numCoverageStages() const { return fCoverageStages.count(); }
joshualitt4dd99882014-11-11 08:51:30 -0800149 int numFragmentStages() const { return this->numColorStages() + this->numCoverageStages(); }
egdaniel89af44a2014-09-26 06:15:04 -0700150 int numTotalStages() const {
joshualitt4dd99882014-11-11 08:51:30 -0800151 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 : 0);
egdaniel89af44a2014-09-26 06:15:04 -0700152 }
153
154 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualitta5305a12014-10-10 17:47:00 -0700155 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
egdaniel378092f2014-12-03 10:40:13 -0800156
157 const GrXPFactory* getXPFactory() const { return fXPFactory.get(); }
158
egdaniel89af44a2014-09-26 06:15:04 -0700159 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; }
160 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; }
161
162 /**
163 * Checks whether any of the effects will read the dst pixel color.
joshualitt2e3b3e32014-12-09 13:31:14 -0800164 * TODO remove when we have an XP
egdaniel89af44a2014-09-26 06:15:04 -0700165 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800166 bool willEffectReadDstColor(GrColor color, GrColor coverage) const;
egdaniel89af44a2014-09-26 06:15:04 -0700167
egdaniel95131432014-12-09 11:15:43 -0800168 /**
169 * The xfer processor factory.
170 */
171 const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) {
172 fXPFactory.reset(SkRef(xpFactory));
173 return xpFactory;
174 }
175
176 void setPorterDuffXPFactory(SkXfermode::Mode mode) {
177 fXPFactory.reset(GrPorterDuffXPFactory::Create(mode));
178 }
179
180 void setPorterDuffXPFactory(GrBlendCoeff src, GrBlendCoeff dst) {
181 fXPFactory.reset(GrPorterDuffXPFactory::Create(src, dst));
182 }
183
joshualittb0a8a372014-09-23 09:50:21 -0700184 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700185 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700186 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect));
egdanielb6cbc382014-11-13 11:00:34 -0800187 fColorProcInfoValid = false;
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000188 return effect;
189 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000190
joshualittb0a8a372014-09-23 09:50:21 -0700191 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700192 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700193 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect));
egdanielb6cbc382014-11-13 11:00:34 -0800194 fCoverageProcInfoValid = false;
bsalomon@google.comadc65362013-01-28 14:26:09 +0000195 return effect;
196 }
197
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000198 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000199 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000200 */
joshualittb0a8a372014-09-23 09:50:21 -0700201 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
202 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000203 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000204
joshualittb0a8a372014-09-23 09:50:21 -0700205 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
206 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000207 }
208
joshualittb0a8a372014-09-23 09:50:21 -0700209 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000210 const SkMatrix& matrix,
211 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -0700212 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
213 }
214
215 void addCoverageTextureProcessor(GrTexture* texture,
216 const SkMatrix& matrix,
217 const GrTextureParams& params) {
218 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000219 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000220
robertphillips@google.com972265d2012-06-13 18:49:30 +0000221 /**
bsalomon9b536522014-09-05 09:18:51 -0700222 * When this object is destroyed it will remove any color/coverage effects from the draw state
223 * that were added after its constructor.
224 *
225 * This class has strange behavior around geometry processor. If there is a GP on the draw state
226 * it will assert that the GP is not modified until after the destructor of the ARE. If the
227 * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the
228 * destructor.
229 *
230 * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add
231 * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState
232 * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred
233 * execution state and GrOptDrawState always will be (and will be immutable and therefore
234 * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore
235 * the GP.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000236 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000237 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000238 public:
bsalomon9b536522014-09-05 09:18:51 -0700239 AutoRestoreEffects()
240 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700241 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700242 , fColorEffectCnt(0)
243 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000244
bsalomon9b536522014-09-05 09:18:51 -0700245 AutoRestoreEffects(GrDrawState* ds)
246 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700247 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700248 , fColorEffectCnt(0)
249 , fCoverageEffectCnt(0) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000250 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000251 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000252
253 ~AutoRestoreEffects() { this->set(NULL); }
254
bsalomon8f727332014-08-05 07:50:06 -0700255 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000256
bsalomon49f085d2014-09-05 13:34:00 -0700257 bool isSet() const { return SkToBool(fDrawState); }
bsalomon8af05232014-06-03 06:34:58 -0700258
robertphillips@google.com972265d2012-06-13 18:49:30 +0000259 private:
bsalomon9b536522014-09-05 09:18:51 -0700260 GrDrawState* fDrawState;
bsalomon52e9d632014-09-05 12:23:12 -0700261 uint32_t fOriginalGPID;
bsalomon9b536522014-09-05 09:18:51 -0700262 int fColorEffectCnt;
263 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000264 };
265
joshualitta58fe352014-10-27 08:39:00 -0700266 /**
267 * AutoRestoreStencil
268 *
269 * This simple struct saves and restores the stencil settings
270 */
271 class AutoRestoreStencil : public ::SkNoncopyable {
272 public:
273 AutoRestoreStencil() : fDrawState(NULL) {}
274
275 AutoRestoreStencil(GrDrawState* ds) : fDrawState(NULL) { this->set(ds); }
276
277 ~AutoRestoreStencil() { this->set(NULL); }
278
279 void set(GrDrawState* ds) {
280 if (fDrawState) {
281 fDrawState->setStencil(fStencilSettings);
282 }
283 fDrawState = ds;
284 if (ds) {
285 fStencilSettings = ds->getStencil();
286 }
287 }
288
289 bool isSet() const { return SkToBool(fDrawState); }
290
291 private:
292 GrDrawState* fDrawState;
293 GrStencilSettings fStencilSettings;
294 };
295
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000296 /// @}
297
298 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000299 /// @name Blending
300 ////
301
egdaniel89af44a2014-09-26 06:15:04 -0700302 /**
303 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
304 * coverage before the blend will give the correct final destination color. In general it
305 * will not as coverage is applied after blending.
306 */
307 bool canTweakAlphaForCoverage() const;
308
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000309 /// @}
310
311 ///////////////////////////////////////////////////////////////////////////
312 /// @name View Matrix
313 ////
314
315 /**
egdaniel89af44a2014-09-26 06:15:04 -0700316 * Retrieves the current view matrix
317 * @return the current view matrix.
318 */
319 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
320
321 /**
322 * Retrieves the inverse of the current view matrix.
323 *
324 * If the current view matrix is invertible, return true, and if matrix
325 * is non-null, copy the inverse into it. If the current view matrix is
326 * non-invertible, return false and ignore the matrix parameter.
327 *
328 * @param matrix if not null, will receive a copy of the current inverse.
329 */
330 bool getViewInverse(SkMatrix* matrix) const {
331 SkMatrix inverse;
332 if (fViewMatrix.invert(&inverse)) {
333 if (matrix) {
334 *matrix = inverse;
335 }
336 return true;
337 }
338 return false;
339 }
340
341 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000342 * Sets the view matrix to identity and updates any installed effects to compensate for the
343 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000344 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000345 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000346
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000347 ////////////////////////////////////////////////////////////////////////////
348
349 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000350 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000351 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000352 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000353 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000354 public:
355 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000356
bsalomon@google.comc7818882013-03-20 19:19:53 +0000357 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000358 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000359 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000360 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000361
362 ~AutoViewMatrixRestore() { this->restore(); }
363
bsalomon@google.coma8347462012-10-08 18:59:39 +0000364 /**
365 * Can be called prior to destructor to restore the original matrix.
366 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000367 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000368
bsalomon@google.comc7818882013-03-20 19:19:53 +0000369 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000370
bsalomon@google.com137f1342013-05-29 21:27:53 +0000371 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
372 is not invertible. */
373 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000374
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000375 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000376 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
377
joshualittb0a8a372014-09-23 09:50:21 -0700378 GrDrawState* fDrawState;
379 SkMatrix fViewMatrix;
380 int fNumColorStages;
joshualitta5305a12014-10-10 17:47:00 -0700381 SkAutoSTArray<8, GrFragmentStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000382 };
383
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000384 /// @}
385
386 ///////////////////////////////////////////////////////////////////////////
387 /// @name Render Target
388 ////
389
390 /**
egdaniel89af44a2014-09-26 06:15:04 -0700391 * Retrieves the currently set render-target.
392 *
393 * @return The currently set render target.
394 */
bsalomon37dd3312014-11-03 08:47:23 -0800395 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700396
397 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000398 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000399 *
400 * @param target The render target to set.
401 */
bsalomonae59b772014-11-19 08:23:49 -0800402 void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef(target)); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000403
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000404 /// @}
405
406 ///////////////////////////////////////////////////////////////////////////
407 /// @name Stencil
408 ////
409
egdaniel89af44a2014-09-26 06:15:04 -0700410 const GrStencilSettings& getStencil() const { return fStencilSettings; }
411
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000412 /**
413 * Sets the stencil settings to use for the next draw.
414 * Changing the clip has the side-effect of possibly zeroing
415 * out the client settable stencil bits. So multipass algorithms
416 * using stencil should not change the clip between passes.
417 * @param settings the stencil settings to use.
418 */
bsalomon04ddf892014-11-19 12:36:22 -0800419 void setStencil(const GrStencilSettings& settings) { fStencilSettings = settings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000420
421 /**
422 * Shortcut to disable stencil testing and ops.
423 */
bsalomon04ddf892014-11-19 12:36:22 -0800424 void disableStencil() { fStencilSettings.setDisabled(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000425
bsalomon2ed5ef82014-07-07 08:44:05 -0700426 GrStencilSettings* stencil() { return &fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000427
428 /// @}
429
430 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000431 /// @name State Flags
432 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000433
egdaniel89af44a2014-09-26 06:15:04 -0700434 /**
435 * Flags that affect rendering. Controlled using enable/disableState(). All
436 * default to disabled.
437 */
438 enum StateBits {
439 /**
440 * Perform dithering. TODO: Re-evaluate whether we need this bit
441 */
442 kDither_StateBit = 0x01,
443 /**
444 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
445 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
446 * the 3D API.
447 */
448 kHWAntialias_StateBit = 0x02,
449 /**
450 * Draws will respect the clip, otherwise the clip is ignored.
451 */
452 kClip_StateBit = 0x04,
453 /**
454 * Disables writing to the color buffer. Useful when performing stencil
455 * operations.
456 */
457 kNoColorWrites_StateBit = 0x08,
458
459 /**
460 * Usually coverage is applied after color blending. The color is blended using the coeffs
461 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
462 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
463 * this case there is no distinction between coverage and color and the caller needs direct
464 * control over the blend coeffs. When set, there will be a single blend step controlled by
465 * setBlendFunc() which will use coverage*color as the src color.
466 */
467 kCoverageDrawing_StateBit = 0x10,
joshualitt7a6184f2014-10-29 18:29:27 -0700468 kLast_StateBit = kCoverageDrawing_StateBit,
egdaniel89af44a2014-09-26 06:15:04 -0700469 };
470
egdaniel89af44a2014-09-26 06:15:04 -0700471 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
472 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
473 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
bsalomon04ddf892014-11-19 12:36:22 -0800474 bool isDither() const { return 0 != (fFlagBits & kDither_StateBit); }
475 bool isHWAntialias() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000476
477 /**
478 * Enable render state settings.
479 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000480 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 */
bsalomon04ddf892014-11-19 12:36:22 -0800482 void enableState(uint32_t stateBits) { fFlagBits |= stateBits; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000483
484 /**
485 * Disable render state settings.
486 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000487 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000488 */
bsalomon04ddf892014-11-19 12:36:22 -0800489 void disableState(uint32_t stateBits) { fFlagBits &= ~(stateBits); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000491 /**
492 * Enable or disable stateBits based on a boolean.
493 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000494 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000495 * @param enable if true enable stateBits, otherwise disable
496 */
497 void setState(uint32_t stateBits, bool enable) {
498 if (enable) {
499 this->enableState(stateBits);
500 } else {
501 this->disableState(stateBits);
502 }
503 }
504
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 /// @}
506
507 ///////////////////////////////////////////////////////////////////////////
508 /// @name Face Culling
509 ////
510
egdaniel89af44a2014-09-26 06:15:04 -0700511 enum DrawFace {
512 kInvalid_DrawFace = -1,
513
514 kBoth_DrawFace,
515 kCCW_DrawFace,
516 kCW_DrawFace,
517 };
518
519 /**
520 * Gets whether the target is drawing clockwise, counterclockwise,
521 * or both faces.
522 * @return the current draw face(s).
523 */
524 DrawFace getDrawFace() const { return fDrawFace; }
525
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000526 /**
527 * Controls whether clockwise, counterclockwise, or both faces are drawn.
528 * @param face the face(s) to draw.
529 */
530 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000531 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700532 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533 }
534
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000535 /// @}
536
537 ///////////////////////////////////////////////////////////////////////////
bsalomon62c447d2014-08-08 08:08:50 -0700538 /// @name Hints
539 /// Hints that when provided can enable optimizations.
540 ////
541
joshualitt65171342014-10-09 07:25:36 -0700542 enum Hints {
543 kVertexColorsAreOpaque_Hint = 0x1,
544 kLast_Hint = kVertexColorsAreOpaque_Hint
545 };
egdaniel89af44a2014-09-26 06:15:04 -0700546
bsalomon62c447d2014-08-08 08:08:50 -0700547 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); }
548
egdaniel89af44a2014-09-26 06:15:04 -0700549 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
550
bsalomon62c447d2014-08-08 08:08:50 -0700551 /// @}
552
553 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000554
bsalomon8f727332014-08-05 07:50:06 -0700555 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000556
557private:
egdaniel89af44a2014-09-26 06:15:04 -0700558 bool isEqual(const GrDrawState& that) const;
559
joshualitt2e3b3e32014-12-09 13:31:14 -0800560 const GrProcOptInfo& colorProcInfo(GrColor color) const {
561 this->calcColorInvariantOutput(color);
egdaniel912b3d22014-11-17 07:45:53 -0800562 return fColorProcInfo;
563 }
564
joshualitt2e3b3e32014-12-09 13:31:14 -0800565 const GrProcOptInfo& coverageProcInfo(GrColor coverage) const {
566 this->calcCoverageInvariantOutput(coverage);
egdaniel912b3d22014-11-17 07:45:53 -0800567 return fCoverageProcInfo;
568 }
569
egdaniel89af44a2014-09-26 06:15:04 -0700570 /**
571 * Determines whether src alpha is guaranteed to be one for all src pixels
572 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800573 bool srcAlphaWillBeOne(GrColor color, GrColor coverage) const;
egdaniel89af44a2014-09-26 06:15:04 -0700574
egdanielb6cbc382014-11-13 11:00:34 -0800575 /**
576 * If fColorProcInfoValid is false, function calculates the invariant output for the color
577 * stages and results are stored in fColorProcInfo.
578 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800579 void calcColorInvariantOutput(GrColor) const;
egdanielb6cbc382014-11-13 11:00:34 -0800580
581 /**
582 * If fCoverageProcInfoValid is false, function calculates the invariant output for the coverage
583 * stages and results are stored in fCoverageProcInfo.
584 */
joshualitt2e3b3e32014-12-09 13:31:14 -0800585 void calcCoverageInvariantOutput(GrColor) const;
egdanielb6cbc382014-11-13 11:00:34 -0800586
bsalomon8f727332014-08-05 07:50:06 -0700587 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000588
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000589 // Some of the auto restore objects assume that no effects are removed during their lifetime.
590 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000591 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592
joshualitta5305a12014-10-10 17:47:00 -0700593 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray;
egdaniel89af44a2014-09-26 06:15:04 -0700594
bsalomonae59b772014-11-19 08:23:49 -0800595 SkAutoTUnref<GrRenderTarget> fRenderTarget;
bsalomonae59b772014-11-19 08:23:49 -0800596 SkMatrix fViewMatrix;
bsalomonae59b772014-11-19 08:23:49 -0800597 uint32_t fFlagBits;
bsalomonae59b772014-11-19 08:23:49 -0800598 GrStencilSettings fStencilSettings;
bsalomonae59b772014-11-19 08:23:49 -0800599 DrawFace fDrawFace;
bsalomonae59b772014-11-19 08:23:49 -0800600 SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800601 SkAutoTUnref<const GrXPFactory> fXPFactory;
bsalomonae59b772014-11-19 08:23:49 -0800602 FragmentStageArray fColorStages;
603 FragmentStageArray fCoverageStages;
604 uint32_t fHints;
egdaniel89af44a2014-09-26 06:15:04 -0700605
egdanielb6cbc382014-11-13 11:00:34 -0800606 mutable GrProcOptInfo fColorProcInfo;
607 mutable GrProcOptInfo fCoverageProcInfo;
608 mutable bool fColorProcInfoValid;
609 mutable bool fCoverageProcInfoValid;
joshualitt2e3b3e32014-12-09 13:31:14 -0800610 mutable GrColor fColorCache;
611 mutable GrColor fCoverageCache;
egdanielb6cbc382014-11-13 11:00:34 -0800612
egdanielb109ac22014-10-07 06:45:44 -0700613 friend class GrOptDrawState;
tomhudson@google.com93813632011-10-27 20:21:16 +0000614};
615
616#endif