blob: a1b07de6d6cdb04331e044ec6444939a8016e10a [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
egdaniel21aed572014-08-26 12:24:06 -070011#include "GrRODrawState.h"
12
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000013#include "GrBlend.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000014#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000015
egdaniel21aed572014-08-26 12:24:06 -070016/**
17 * Modifiable subclass derived from GrRODrawState. The majority of the data that represents a draw
18 * state is stored in the parent class. GrDrawState contains methods for setting, adding to, etc.
19 * various data members of the draw state. This class is used to configure the state used when
20 * issuing draws via GrDrawTarget.
21 */
22class GrDrawState : public GrRODrawState {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000023public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000024 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000025
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000026 GrDrawState() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000027 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000028 this->reset();
29 }
tomhudson@google.com93813632011-10-27 20:21:16 +000030
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000031 GrDrawState(const SkMatrix& initialViewMatrix) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000032 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000033 this->reset(initialViewMatrix);
34 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000035
36 /**
37 * Copies another draw state.
38 **/
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000039 GrDrawState(const GrDrawState& state) : INHERITED() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000040 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000041 *this = state;
42 }
43
bsalomon@google.com137f1342013-05-29 21:27:53 +000044 /**
45 * Copies another draw state with a preconcat to the view matrix.
46 **/
bsalomon8f727332014-08-05 07:50:06 -070047 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000048
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000049 virtual ~GrDrawState() { SkASSERT(0 == fBlockEffectRemovalCnt); }
bsalomon@google.com137f1342013-05-29 21:27:53 +000050
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000051 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +000052 * Resets to the default state. GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000053 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000054 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000055
bsalomon@google.com137f1342013-05-29 21:27:53 +000056 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000057
58 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000059 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
60 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070061 * equivalents are set to default values with the exception of vertex attribute state which
62 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000063 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000064 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000065
66 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000067 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000068 ////
69
jvanverth@google.com9b855c72013-03-01 18:21:22 +000070 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000071 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
72 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
73 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000074 *
jvanverth@google.com054ae992013-04-01 20:06:51 +000075 * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when
76 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000077 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000078
jvanverth@google.com9b855c72013-03-01 18:21:22 +000079 /**
robertphillips@google.com42903302013-04-20 12:26:07 +000080 * Sets vertex attributes for next draw. The object driving the templatization
81 * should be a global GrVertexAttrib array that is never changed.
egdaniel7b3d5ee2014-08-28 05:41:14 -070082 *
83 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
84 * @param stride the number of bytes between successive vertex data.
jvanverth@google.com9b855c72013-03-01 18:21:22 +000085 */
egdaniel7b3d5ee2014-08-28 05:41:14 -070086 template <const GrVertexAttrib A[]> void setVertexAttribs(int count, size_t stride) {
87 this->internalSetVertexAttribs(A, count, stride);
robertphillips@google.com42903302013-04-20 12:26:07 +000088 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000089
jvanverth@google.com9b855c72013-03-01 18:21:22 +000090 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000091 * Sets default vertex attributes for next draw. The default is a single attribute:
92 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +000093 */
94 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000095
jvanverth@google.com054ae992013-04-01 20:06:51 +000096 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +000097 * Helper to save/restore vertex attribs
98 */
99 class AutoVertexAttribRestore {
100 public:
bsalomon8f727332014-08-05 07:50:06 -0700101 AutoVertexAttribRestore(GrDrawState* drawState);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000102
egdaniel7b3d5ee2014-08-28 05:41:14 -0700103 ~AutoVertexAttribRestore() { fDrawState->internalSetVertexAttribs(fVAPtr, fVACount, fVAStride); }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000104
105 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000106 GrDrawState* fDrawState;
107 const GrVertexAttrib* fVAPtr;
108 int fVACount;
egdaniel7b3d5ee2014-08-28 05:41:14 -0700109 size_t fVAStride;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000110 };
111
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000112 /// @}
113
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000114 /**
bsalomon62c447d2014-08-08 08:08:50 -0700115 * Depending on features available in the underlying 3D API and the color blend mode requested
116 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
117 * the fragment shader.
118 *
119 * This function considers the current draw state and the draw target's capabilities to
120 * determine whether coverage can be handled correctly. This function assumes that the caller
121 * intends to specify fractional pixel coverage (via setCoverage(), through a coverage vertex
122 * attribute, or a coverage effect) but may not have specified it yet.
123 */
124 bool couldApplyCoverage(const GrDrawTargetCaps& caps) const;
125
jvanverth@google.comcc782382013-01-28 20:39:48 +0000126 /// @}
127
128 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000129 /// @name Color
130 ////
131
132 /**
133 * Sets color for next draw to a premultiplied-alpha color.
134 *
135 * @param color the color to set.
136 */
egdaniel9514d242014-07-18 06:15:43 -0700137 void setColor(GrColor color) {
138 fColor = color;
139 this->invalidateBlendOptFlags();
140 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000141
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000142 /**
143 * Sets the color to be used for the next draw to be
144 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
145 *
146 * @param alpha The alpha value to set as the color.
147 */
bsalomon62c447d2014-08-08 08:08:50 -0700148 void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) | a); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000149
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000150 /// @}
151
152 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000153 /// @name Coverage
154 ////
155
156 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000158 * initial value (after construction or reset()) is 0xff. The constant
159 * coverage is ignored when per-vertex coverage is provided.
160 */
161 void setCoverage(uint8_t coverage) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700162 fCoverage = coverage;
egdaniel9514d242014-07-18 06:15:43 -0700163 this->invalidateBlendOptFlags();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000164 }
165
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000166 /// @}
167
joshualittbd769d02014-09-04 08:56:46 -0700168 /**
169 * The geometry processor is the sole element of the skia pipeline which can use the vertex,
170 * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader
171 * but is never put in the color processing pipeline.
172 */
173
174 const GrEffect* setGeometryProcessor(const GrEffect* effect, int attr0 = -1, int attr1 = -1) {
175 SkASSERT(NULL != effect);
176 SkASSERT(!this->hasGeometryProcessor());
177 fGeometryProcessor.reset(new GrEffectStage(effect, attr0, attr1));
178 this->invalidateBlendOptFlags();
179 return effect;
180 }
181
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000182 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000183 /// @name Effect Stages
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000184 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
185 /// shader. Its inputs are the output from the previous stage as well as some variables
186 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
187 /// the fragment position, local coordinates).
188 ///
189 /// The stages are divided into two sets, color-computing and coverage-computing. The final
190 /// color stage produces the final pixel color. The coverage-computing stages function exactly
191 /// as the color-computing but the output of the final coverage stage is treated as a fractional
192 /// pixel coverage rather than as input to the src/dst color blend step.
193 ///
194 /// The input color to the first color-stage is either the constant color or interpolated
195 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
196 /// (usually full-coverage) or interpolated per-vertex coverage.
197 ///
198 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
199 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000200 ////
201
bsalomon97b9ab72014-07-08 06:52:35 -0700202 const GrEffect* addColorEffect(const GrEffect* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000203 SkASSERT(NULL != effect);
egdaniel8cbf3d52014-08-21 06:27:22 -0700204 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1));
egdaniel9514d242014-07-18 06:15:43 -0700205 this->invalidateBlendOptFlags();
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000206 return effect;
207 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000208
bsalomon97b9ab72014-07-08 06:52:35 -0700209 const GrEffect* addCoverageEffect(const GrEffect* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000210 SkASSERT(NULL != effect);
egdaniel8cbf3d52014-08-21 06:27:22 -0700211 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1));
egdaniel9514d242014-07-18 06:15:43 -0700212 this->invalidateBlendOptFlags();
bsalomon@google.comadc65362013-01-28 14:26:09 +0000213 return effect;
214 }
215
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000216 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000217 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000218 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000219 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon97b9ab72014-07-08 06:52:35 -0700220 this->addColorEffect(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000221 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000222
223 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon97b9ab72014-07-08 06:52:35 -0700224 this->addCoverageEffect(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000225 }
226
227 void addColorTextureEffect(GrTexture* texture,
228 const SkMatrix& matrix,
229 const GrTextureParams& params) {
bsalomon97b9ab72014-07-08 06:52:35 -0700230 this->addColorEffect(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000231 }
232
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000233 void addCoverageTextureEffect(GrTexture* texture,
234 const SkMatrix& matrix,
235 const GrTextureParams& params) {
bsalomon97b9ab72014-07-08 06:52:35 -0700236 this->addCoverageEffect(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000237 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000238
robertphillips@google.com972265d2012-06-13 18:49:30 +0000239 /**
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000240 * When this object is destroyed it will remove any effects from the draw state that were added
241 * after its constructor.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000242 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000243 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000244 public:
bsalomon@google.com2fad5a82013-06-13 19:47:23 +0000245 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000246
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000247 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {
248 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000249 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000250
251 ~AutoRestoreEffects() { this->set(NULL); }
252
bsalomon8f727332014-08-05 07:50:06 -0700253 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000254
bsalomon8af05232014-06-03 06:34:58 -0700255 bool isSet() const { return NULL != fDrawState; }
256
robertphillips@google.com972265d2012-06-13 18:49:30 +0000257 private:
258 GrDrawState* fDrawState;
joshualittbd769d02014-09-04 08:56:46 -0700259 SkAutoTDelete<GrEffectStage> fGeometryProcessor;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000260 int fColorEffectCnt;
261 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000262 };
263
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000264 /// @}
265
266 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000267 /// @name Blending
268 ////
269
270 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000271 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000272 *
273 * The blend function will be:
274 * D' = sat(S*srcCoef + D*dstCoef)
275 *
276 * where D is the existing destination color, S is the incoming source
277 * color, and D' is the new destination color that will be written. sat()
278 * is the saturation function.
279 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000280 * @param srcCoef coefficient applied to the src color.
281 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000282 */
283 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700284 fSrcBlend = srcCoeff;
285 fDstBlend = dstCoeff;
egdaniel9514d242014-07-18 06:15:43 -0700286 this->invalidateBlendOptFlags();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000287 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000288 if (GrBlendCoeffRefsDst(dstCoeff)) {
289 GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000290 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000291 if (GrBlendCoeffRefsSrc(srcCoeff)) {
292 GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000293 }
294 #endif
295 }
296
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000297 /**
298 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000299 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000300 * kConstC_GrBlendCoeff
301 * kIConstC_GrBlendCoeff
302 * kConstA_GrBlendCoeff
303 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000304 *
305 * @param constant the constant to set
306 */
egdaniel9514d242014-07-18 06:15:43 -0700307 void setBlendConstant(GrColor constant) {
308 fBlendConstant = constant;
309 this->invalidateBlendOptFlags();
310 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000311
312 /**
bsalomon@google.com2b446732013-02-12 16:47:41 +0000313 * Determines what optimizations can be applied based on the blend. The coefficients may have
314 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
315 * params that receive the tweaked coefficients. Normally the function looks at the current
316 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
317 * determine the blend optimizations that would be used if there was partial pixel coverage.
318 *
319 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
320 * playback) must call this function and respect the flags that replace the output color.
egdaniel9514d242014-07-18 06:15:43 -0700321 *
322 * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will
323 * simply returned the cached flags and coefficients. Otherwise it will calculate the values.
bsalomon@google.com2b446732013-02-12 16:47:41 +0000324 */
325 BlendOptFlags getBlendOpts(bool forceCoverage = false,
326 GrBlendCoeff* srcCoeff = NULL,
327 GrBlendCoeff* dstCoeff = NULL) const;
328
egdaniel21aed572014-08-26 12:24:06 -0700329 /**
330 * We don't use suplied vertex color attributes if our blend mode is EmitCoverage or
331 * EmitTransBlack
332 */
333 bool canIgnoreColorAttribute() const;
334
335
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000336 /// @}
337
338 ///////////////////////////////////////////////////////////////////////////
339 /// @name View Matrix
340 ////
341
342 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000343 * Sets the view matrix to identity and updates any installed effects to compensate for the
344 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000345 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000346 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000347
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000348 ////////////////////////////////////////////////////////////////////////////
349
350 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000351 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000352 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000353 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000354 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000355 public:
356 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000357
bsalomon@google.comc7818882013-03-20 19:19:53 +0000358 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000359 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000360 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000361 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000362
363 ~AutoViewMatrixRestore() { this->restore(); }
364
bsalomon@google.coma8347462012-10-08 18:59:39 +0000365 /**
366 * Can be called prior to destructor to restore the original matrix.
367 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000368 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000369
bsalomon@google.comc7818882013-03-20 19:19:53 +0000370 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000371
bsalomon@google.com137f1342013-05-29 21:27:53 +0000372 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
373 is not invertible. */
374 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000375
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000376 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000377 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
378
379 GrDrawState* fDrawState;
380 SkMatrix fViewMatrix;
381 int fNumColorStages;
joshualittbd769d02014-09-04 08:56:46 -0700382 bool fHasGeometryProcessor;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000383 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000384 };
385
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000386 /// @}
387
388 ///////////////////////////////////////////////////////////////////////////
389 /// @name Render Target
390 ////
391
392 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000393 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000394 *
395 * @param target The render target to set.
396 */
bsalomon62c447d2014-08-08 08:08:50 -0700397 void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef(target)); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000398
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000399 /// @}
400
401 ///////////////////////////////////////////////////////////////////////////
402 /// @name Stencil
403 ////
404
405 /**
406 * Sets the stencil settings to use for the next draw.
407 * Changing the clip has the side-effect of possibly zeroing
408 * out the client settable stencil bits. So multipass algorithms
409 * using stencil should not change the clip between passes.
410 * @param settings the stencil settings to use.
411 */
412 void setStencil(const GrStencilSettings& settings) {
bsalomon2ed5ef82014-07-07 08:44:05 -0700413 fStencilSettings = settings;
egdaniel9514d242014-07-18 06:15:43 -0700414 this->invalidateBlendOptFlags();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000415 }
416
417 /**
418 * Shortcut to disable stencil testing and ops.
419 */
420 void disableStencil() {
bsalomon2ed5ef82014-07-07 08:44:05 -0700421 fStencilSettings.setDisabled();
egdaniel9514d242014-07-18 06:15:43 -0700422 this->invalidateBlendOptFlags();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000423 }
424
bsalomon2ed5ef82014-07-07 08:44:05 -0700425 GrStencilSettings* stencil() { return &fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000426
427 /// @}
428
429 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000430 /// @name State Flags
431 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000432
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000433 void resetStateFlags() {
bsalomon2ed5ef82014-07-07 08:44:05 -0700434 fFlagBits = 0;
egdaniel9514d242014-07-18 06:15:43 -0700435 this->invalidateBlendOptFlags();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000436 }
437
438 /**
439 * Enable render state settings.
440 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000441 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000442 */
443 void enableState(uint32_t stateBits) {
bsalomon2ed5ef82014-07-07 08:44:05 -0700444 fFlagBits |= stateBits;
egdaniel9514d242014-07-18 06:15:43 -0700445 this->invalidateBlendOptFlags();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000446 }
447
448 /**
449 * Disable render state settings.
450 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000451 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000452 */
453 void disableState(uint32_t stateBits) {
bsalomon2ed5ef82014-07-07 08:44:05 -0700454 fFlagBits &= ~(stateBits);
egdaniel9514d242014-07-18 06:15:43 -0700455 this->invalidateBlendOptFlags();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000456 }
457
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000458 /**
459 * Enable or disable stateBits based on a boolean.
460 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000461 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000462 * @param enable if true enable stateBits, otherwise disable
463 */
464 void setState(uint32_t stateBits, bool enable) {
465 if (enable) {
466 this->enableState(stateBits);
467 } else {
468 this->disableState(stateBits);
469 }
470 }
471
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000472 /// @}
473
474 ///////////////////////////////////////////////////////////////////////////
475 /// @name Face Culling
476 ////
477
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000478 /**
479 * Controls whether clockwise, counterclockwise, or both faces are drawn.
480 * @param face the face(s) to draw.
481 */
482 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000483 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700484 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 }
486
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000487 /// @}
488
489 ///////////////////////////////////////////////////////////////////////////
bsalomon62c447d2014-08-08 08:08:50 -0700490 /// @name Hints
491 /// Hints that when provided can enable optimizations.
492 ////
493
494 enum Hints { kVertexColorsAreOpaque_Hint = 0x1, };
495
496 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); }
497
498 /// @}
499
500 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000501
bsalomon838f62d2014-08-05 07:15:57 -0700502 /** Return type for CombineIfPossible. */
503 enum CombinedState {
504 /** The GrDrawStates cannot be combined. */
505 kIncompatible_CombinedState,
506 /** Either draw state can be used in place of the other. */
507 kAOrB_CombinedState,
508 /** Use the first draw state. */
509 kA_CombinedState,
510 /** Use the second draw state. */
511 kB_CombinedState,
512 };
513
514 /** This function determines whether the GrDrawStates used for two draws can be combined into
515 a single GrDrawState. This is used to avoid storing redundant GrDrawStates and to determine
516 if draws can be batched. The return value indicates whether combining is possible and, if
517 so, which of the two inputs should be used. */
bsalomon62c447d2014-08-08 08:08:50 -0700518 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawState& b,
519 const GrDrawTargetCaps& caps);
bsalomon72336ed2014-08-05 07:35:56 -0700520
bsalomon8f727332014-08-05 07:50:06 -0700521 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000522
523private:
bsalomon8f727332014-08-05 07:50:06 -0700524 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000525
egdaniel21aed572014-08-26 12:24:06 -0700526 /**
527 * Determines whether src alpha is guaranteed to be one for all src pixels
528 */
529 bool srcAlphaWillBeOne() const;
530
531 /**
532 * Helper function for getBlendOpts.
533 */
egdaniel9514d242014-07-18 06:15:43 -0700534 BlendOptFlags calcBlendOpts(bool forceCoverage = false,
bsalomon62c447d2014-08-08 08:08:50 -0700535 GrBlendCoeff* srcCoeff = NULL,
536 GrBlendCoeff* dstCoeff = NULL) const;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000537
egdaniel21aed572014-08-26 12:24:06 -0700538 void invalidateBlendOptFlags() {
539 fBlendOptFlags = kInvalid_BlendOptFlag;
540 }
bsalomon62c447d2014-08-08 08:08:50 -0700541
542 uint32_t fHints;
bsalomon2ed5ef82014-07-07 08:44:05 -0700543
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000544 // Some of the auto restore objects assume that no effects are removed during their lifetime.
545 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000546 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547
egdaniel7b3d5ee2014-08-28 05:41:14 -0700548 void internalSetVertexAttribs(const GrVertexAttrib attribs[], int count, size_t stride);
robertphillips@google.com42903302013-04-20 12:26:07 +0000549
egdaniel21aed572014-08-26 12:24:06 -0700550 typedef GrRODrawState INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000551};
552
553#endif