blob: 5e4ed9b4c0f91495bd9e24307bd19afbc87b4012 [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
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000011#include "GrBlend.h"
egdanielc0648242014-09-22 13:17:02 -070012#include "GrDrawTargetCaps.h"
bsalomonf96ba022014-09-17 08:05:40 -070013#include "GrGpuResourceRef.h"
bsalomon2a9ca782014-09-05 14:27:43 -070014#include "GrRODrawState.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000015#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000016
egdaniel170f90b2014-09-16 12:54:40 -070017class GrOptDrawState;
18
egdaniel21aed572014-08-26 12:24:06 -070019/**
20 * Modifiable subclass derived from GrRODrawState. The majority of the data that represents a draw
21 * state is stored in the parent class. GrDrawState contains methods for setting, adding to, etc.
22 * various data members of the draw state. This class is used to configure the state used when
23 * issuing draws via GrDrawTarget.
24 */
25class GrDrawState : public GrRODrawState {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000026public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000027 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
egdaniel3658f382014-09-15 07:01:59 -070029 GrDrawState() : fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000030 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000031 this->reset();
32 }
tomhudson@google.com93813632011-10-27 20:21:16 +000033
egdaniel3658f382014-09-15 07:01:59 -070034 GrDrawState(const SkMatrix& initialViewMatrix) : fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000035 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000036 this->reset(initialViewMatrix);
37 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000038
39 /**
40 * Copies another draw state.
41 **/
egdaniel3658f382014-09-15 07:01:59 -070042 GrDrawState(const GrDrawState& state) : INHERITED(), fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000043 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000044 *this = state;
45 }
46
bsalomon@google.com137f1342013-05-29 21:27:53 +000047 /**
48 * Copies another draw state with a preconcat to the view matrix.
49 **/
bsalomon8f727332014-08-05 07:50:06 -070050 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000051
egdaniel170f90b2014-09-16 12:54:40 -070052 virtual ~GrDrawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +000053
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000054 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +000055 * Resets to the default state. GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000056 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000057 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000058
bsalomon@google.com137f1342013-05-29 21:27:53 +000059 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000060
61 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000062 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
63 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070064 * equivalents are set to default values with the exception of vertex attribute state which
65 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000066 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000067 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000068
69 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000070 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000071 ////
72
jvanverth@google.com9b855c72013-03-01 18:21:22 +000073 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000074 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
75 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
76 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000077 *
jvanverth@google.com054ae992013-04-01 20:06:51 +000078 * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when
79 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000080 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000081
jvanverth@google.com9b855c72013-03-01 18:21:22 +000082 /**
robertphillips@google.com42903302013-04-20 12:26:07 +000083 * Sets vertex attributes for next draw. The object driving the templatization
84 * should be a global GrVertexAttrib array that is never changed.
egdaniel7b3d5ee2014-08-28 05:41:14 -070085 *
86 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
87 * @param stride the number of bytes between successive vertex data.
jvanverth@google.com9b855c72013-03-01 18:21:22 +000088 */
egdaniel7b3d5ee2014-08-28 05:41:14 -070089 template <const GrVertexAttrib A[]> void setVertexAttribs(int count, size_t stride) {
90 this->internalSetVertexAttribs(A, count, stride);
robertphillips@google.com42903302013-04-20 12:26:07 +000091 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000092
jvanverth@google.com9b855c72013-03-01 18:21:22 +000093 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000094 * Sets default vertex attributes for next draw. The default is a single attribute:
95 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +000096 */
97 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000098
jvanverth@google.com054ae992013-04-01 20:06:51 +000099 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000100 * Helper to save/restore vertex attribs
101 */
102 class AutoVertexAttribRestore {
103 public:
bsalomon8f727332014-08-05 07:50:06 -0700104 AutoVertexAttribRestore(GrDrawState* drawState);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000105
egdaniel3658f382014-09-15 07:01:59 -0700106 ~AutoVertexAttribRestore() { fDrawState->internalSetVertexAttribs(fVAPtr, fVACount,
107 fVAStride); }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000108
109 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000110 GrDrawState* fDrawState;
111 const GrVertexAttrib* fVAPtr;
112 int fVACount;
egdaniel7b3d5ee2014-08-28 05:41:14 -0700113 size_t fVAStride;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000114 };
115
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000116 /// @}
117
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000118 /**
bsalomon62c447d2014-08-08 08:08:50 -0700119 * Depending on features available in the underlying 3D API and the color blend mode requested
120 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
121 * the fragment shader.
122 *
123 * This function considers the current draw state and the draw target's capabilities to
124 * determine whether coverage can be handled correctly. This function assumes that the caller
125 * intends to specify fractional pixel coverage (via setCoverage(), through a coverage vertex
126 * attribute, or a coverage effect) but may not have specified it yet.
127 */
128 bool couldApplyCoverage(const GrDrawTargetCaps& caps) const;
129
jvanverth@google.comcc782382013-01-28 20:39:48 +0000130 /// @}
131
132 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000133 /// @name Color
134 ////
135
136 /**
137 * Sets color for next draw to a premultiplied-alpha color.
138 *
139 * @param color the color to set.
140 */
egdaniel9514d242014-07-18 06:15:43 -0700141 void setColor(GrColor color) {
egdaniel3658f382014-09-15 07:01:59 -0700142 if (color != fColor) {
143 fColor = color;
144 this->invalidateOptState();
145 }
egdaniel9514d242014-07-18 06:15:43 -0700146 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000147
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000148 /**
149 * Sets the color to be used for the next draw to be
150 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
151 *
152 * @param alpha The alpha value to set as the color.
153 */
bsalomon62c447d2014-08-08 08:08:50 -0700154 void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) | a); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000155
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000156 /// @}
157
158 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000159 /// @name Coverage
160 ////
161
162 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000164 * initial value (after construction or reset()) is 0xff. The constant
165 * coverage is ignored when per-vertex coverage is provided.
166 */
167 void setCoverage(uint8_t coverage) {
egdaniel3658f382014-09-15 07:01:59 -0700168 if (coverage != fCoverage) {
169 fCoverage = coverage;
170 this->invalidateOptState();
171 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000172 }
173
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000174 /// @}
175
joshualittbd769d02014-09-04 08:56:46 -0700176 /**
177 * The geometry processor is the sole element of the skia pipeline which can use the vertex,
178 * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader
179 * but is never put in the color processing pipeline.
180 */
181
joshualitt249af152014-09-15 11:41:13 -0700182 const GrEffect* setGeometryProcessor(const GrEffect* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700183 SkASSERT(effect);
joshualitt249af152014-09-15 11:41:13 -0700184 SkASSERT(effect->requiresVertexShader());
joshualittbd769d02014-09-04 08:56:46 -0700185 SkASSERT(!this->hasGeometryProcessor());
joshualitt249af152014-09-15 11:41:13 -0700186 fGeometryProcessor.reset(new GrEffectStage(effect));
egdaniel3658f382014-09-15 07:01:59 -0700187 this->invalidateOptState();
joshualittbd769d02014-09-04 08:56:46 -0700188 return effect;
189 }
190
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000191 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000192 /// @name Effect Stages
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000193 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
194 /// shader. Its inputs are the output from the previous stage as well as some variables
195 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
196 /// the fragment position, local coordinates).
197 ///
198 /// The stages are divided into two sets, color-computing and coverage-computing. The final
199 /// color stage produces the final pixel color. The coverage-computing stages function exactly
200 /// as the color-computing but the output of the final coverage stage is treated as a fractional
201 /// pixel coverage rather than as input to the src/dst color blend step.
202 ///
203 /// The input color to the first color-stage is either the constant color or interpolated
204 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
205 /// (usually full-coverage) or interpolated per-vertex coverage.
206 ///
207 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
208 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000209 ////
210
joshualitt249af152014-09-15 11:41:13 -0700211 const GrEffect* addColorEffect(const GrEffect* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700212 SkASSERT(effect);
joshualitt249af152014-09-15 11:41:13 -0700213 SkASSERT(!effect->requiresVertexShader());
214 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect));
egdaniel3658f382014-09-15 07:01:59 -0700215 this->invalidateOptState();
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000216 return effect;
217 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000218
joshualitt249af152014-09-15 11:41:13 -0700219 const GrEffect* addCoverageEffect(const GrEffect* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700220 SkASSERT(effect);
joshualitt249af152014-09-15 11:41:13 -0700221 SkASSERT(!effect->requiresVertexShader());
222 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect));
egdaniel3658f382014-09-15 07:01:59 -0700223 this->invalidateOptState();
bsalomon@google.comadc65362013-01-28 14:26:09 +0000224 return effect;
225 }
226
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000227 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000228 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000229 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000230 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon97b9ab72014-07-08 06:52:35 -0700231 this->addColorEffect(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000232 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000233
234 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon97b9ab72014-07-08 06:52:35 -0700235 this->addCoverageEffect(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000236 }
237
238 void addColorTextureEffect(GrTexture* texture,
239 const SkMatrix& matrix,
240 const GrTextureParams& params) {
bsalomon97b9ab72014-07-08 06:52:35 -0700241 this->addColorEffect(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000242 }
243
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000244 void addCoverageTextureEffect(GrTexture* texture,
245 const SkMatrix& matrix,
246 const GrTextureParams& params) {
bsalomon97b9ab72014-07-08 06:52:35 -0700247 this->addCoverageEffect(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000248 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000249
robertphillips@google.com972265d2012-06-13 18:49:30 +0000250 /**
bsalomon9b536522014-09-05 09:18:51 -0700251 * When this object is destroyed it will remove any color/coverage effects from the draw state
252 * that were added after its constructor.
253 *
254 * This class has strange behavior around geometry processor. If there is a GP on the draw state
255 * it will assert that the GP is not modified until after the destructor of the ARE. If the
256 * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the
257 * destructor.
258 *
259 * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add
260 * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState
261 * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred
262 * execution state and GrOptDrawState always will be (and will be immutable and therefore
263 * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore
264 * the GP.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000265 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000266 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000267 public:
bsalomon9b536522014-09-05 09:18:51 -0700268 AutoRestoreEffects()
269 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700270 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700271 , fColorEffectCnt(0)
272 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000273
bsalomon9b536522014-09-05 09:18:51 -0700274 AutoRestoreEffects(GrDrawState* ds)
275 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700276 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700277 , fColorEffectCnt(0)
278 , fCoverageEffectCnt(0) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000279 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000280 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000281
282 ~AutoRestoreEffects() { this->set(NULL); }
283
bsalomon8f727332014-08-05 07:50:06 -0700284 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000285
bsalomon49f085d2014-09-05 13:34:00 -0700286 bool isSet() const { return SkToBool(fDrawState); }
bsalomon8af05232014-06-03 06:34:58 -0700287
robertphillips@google.com972265d2012-06-13 18:49:30 +0000288 private:
bsalomon9b536522014-09-05 09:18:51 -0700289 GrDrawState* fDrawState;
bsalomon52e9d632014-09-05 12:23:12 -0700290 uint32_t fOriginalGPID;
bsalomon9b536522014-09-05 09:18:51 -0700291 int fColorEffectCnt;
292 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000293 };
294
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000295 /// @}
296
297 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000298 /// @name Blending
299 ////
300
301 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000302 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000303 *
304 * The blend function will be:
305 * D' = sat(S*srcCoef + D*dstCoef)
306 *
307 * where D is the existing destination color, S is the incoming source
308 * color, and D' is the new destination color that will be written. sat()
309 * is the saturation function.
310 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000311 * @param srcCoef coefficient applied to the src color.
312 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000313 */
314 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
egdaniel3658f382014-09-15 07:01:59 -0700315 if (srcCoeff != fSrcBlend || dstCoeff != fDstBlend) {
316 fSrcBlend = srcCoeff;
317 fDstBlend = dstCoeff;
318 this->invalidateOptState();
319 }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000320 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000321 if (GrBlendCoeffRefsDst(dstCoeff)) {
322 GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000323 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000324 if (GrBlendCoeffRefsSrc(srcCoeff)) {
325 GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000326 }
327 #endif
328 }
329
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000330 /**
331 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000332 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000333 * kConstC_GrBlendCoeff
334 * kIConstC_GrBlendCoeff
335 * kConstA_GrBlendCoeff
336 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000337 *
338 * @param constant the constant to set
339 */
egdaniel9514d242014-07-18 06:15:43 -0700340 void setBlendConstant(GrColor constant) {
egdaniel3658f382014-09-15 07:01:59 -0700341 if (constant != fBlendConstant) {
342 fBlendConstant = constant;
343 this->invalidateOptState();
344 }
egdaniel9514d242014-07-18 06:15:43 -0700345 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000346
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000347 /// @}
348
349 ///////////////////////////////////////////////////////////////////////////
350 /// @name View Matrix
351 ////
352
353 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000354 * Sets the view matrix to identity and updates any installed effects to compensate for the
355 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000356 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000357 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000358
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000359 ////////////////////////////////////////////////////////////////////////////
360
361 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000362 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000363 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000364 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000365 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000366 public:
367 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000368
bsalomon@google.comc7818882013-03-20 19:19:53 +0000369 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000370 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000371 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000372 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000373
374 ~AutoViewMatrixRestore() { this->restore(); }
375
bsalomon@google.coma8347462012-10-08 18:59:39 +0000376 /**
377 * Can be called prior to destructor to restore the original matrix.
378 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000379 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000380
bsalomon@google.comc7818882013-03-20 19:19:53 +0000381 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000382
bsalomon@google.com137f1342013-05-29 21:27:53 +0000383 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
384 is not invertible. */
385 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000386
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000387 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000388 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
389
390 GrDrawState* fDrawState;
391 SkMatrix fViewMatrix;
392 int fNumColorStages;
joshualittbd769d02014-09-04 08:56:46 -0700393 bool fHasGeometryProcessor;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000394 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000395 };
396
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000397 /// @}
398
399 ///////////////////////////////////////////////////////////////////////////
400 /// @name Render Target
401 ////
402
403 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000404 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000405 *
406 * @param target The render target to set.
407 */
bsalomon2a9ca782014-09-05 14:27:43 -0700408 void setRenderTarget(GrRenderTarget* target) {
bsalomon45725db2014-09-19 11:48:02 -0700409 fRenderTarget.set(SkSafeRef(target), GrIORef::kWrite_IOType);
egdaniel3658f382014-09-15 07:01:59 -0700410 this->invalidateOptState();
bsalomon2a9ca782014-09-05 14:27:43 -0700411 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000412
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000413 /// @}
414
415 ///////////////////////////////////////////////////////////////////////////
416 /// @name Stencil
417 ////
418
419 /**
420 * Sets the stencil settings to use for the next draw.
421 * Changing the clip has the side-effect of possibly zeroing
422 * out the client settable stencil bits. So multipass algorithms
423 * using stencil should not change the clip between passes.
424 * @param settings the stencil settings to use.
425 */
426 void setStencil(const GrStencilSettings& settings) {
egdaniel3658f382014-09-15 07:01:59 -0700427 if (settings != fStencilSettings) {
428 fStencilSettings = settings;
429 this->invalidateOptState();
430 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000431 }
432
433 /**
434 * Shortcut to disable stencil testing and ops.
435 */
436 void disableStencil() {
egdaniel3658f382014-09-15 07:01:59 -0700437 if (!fStencilSettings.isDisabled()) {
438 fStencilSettings.setDisabled();
439 this->invalidateOptState();
440 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000441 }
442
bsalomon2ed5ef82014-07-07 08:44:05 -0700443 GrStencilSettings* stencil() { return &fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000444
445 /// @}
446
447 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000448 /// @name State Flags
449 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000450
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000451 void resetStateFlags() {
egdaniel3658f382014-09-15 07:01:59 -0700452 if (0 != fFlagBits) {
453 fFlagBits = 0;
454 this->invalidateOptState();
455 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000456 }
457
458 /**
459 * Enable render state settings.
460 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000461 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000462 */
463 void enableState(uint32_t stateBits) {
egdaniel3658f382014-09-15 07:01:59 -0700464 if (stateBits & ~fFlagBits) {
465 fFlagBits |= stateBits;
466 this->invalidateOptState();
467 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000468 }
469
470 /**
471 * Disable render state settings.
472 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000473 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000474 */
475 void disableState(uint32_t stateBits) {
egdaniel3658f382014-09-15 07:01:59 -0700476 if (stateBits & fFlagBits) {
477 fFlagBits &= ~(stateBits);
478 this->invalidateOptState();
479 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000480 }
481
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000482 /**
483 * Enable or disable stateBits based on a boolean.
484 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000485 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000486 * @param enable if true enable stateBits, otherwise disable
487 */
488 void setState(uint32_t stateBits, bool enable) {
489 if (enable) {
490 this->enableState(stateBits);
491 } else {
492 this->disableState(stateBits);
493 }
494 }
495
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000496 /// @}
497
498 ///////////////////////////////////////////////////////////////////////////
499 /// @name Face Culling
500 ////
501
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 /**
503 * Controls whether clockwise, counterclockwise, or both faces are drawn.
504 * @param face the face(s) to draw.
505 */
506 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000507 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700508 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000509 }
510
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511 /// @}
512
513 ///////////////////////////////////////////////////////////////////////////
bsalomon62c447d2014-08-08 08:08:50 -0700514 /// @name Hints
515 /// Hints that when provided can enable optimizations.
516 ////
517
bsalomon62c447d2014-08-08 08:08:50 -0700518 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); }
519
520 /// @}
521
522 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000523
bsalomon838f62d2014-08-05 07:15:57 -0700524 /** Return type for CombineIfPossible. */
525 enum CombinedState {
526 /** The GrDrawStates cannot be combined. */
527 kIncompatible_CombinedState,
528 /** Either draw state can be used in place of the other. */
529 kAOrB_CombinedState,
530 /** Use the first draw state. */
531 kA_CombinedState,
532 /** Use the second draw state. */
533 kB_CombinedState,
534 };
535
536 /** This function determines whether the GrDrawStates used for two draws can be combined into
537 a single GrDrawState. This is used to avoid storing redundant GrDrawStates and to determine
538 if draws can be batched. The return value indicates whether combining is possible and, if
539 so, which of the two inputs should be used. */
bsalomon62c447d2014-08-08 08:08:50 -0700540 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawState& b,
541 const GrDrawTargetCaps& caps);
bsalomon72336ed2014-08-05 07:35:56 -0700542
bsalomon8f727332014-08-05 07:50:06 -0700543 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000544
egdaniel3658f382014-09-15 07:01:59 -0700545 /**
546 * Returns a snapshot of the current optimized state. If the current drawState has a valid
547 * cached optimiezed state it will simply return a pointer to it otherwise it will create a new
548 * GrOptDrawState. In all cases the GrOptDrawState is reffed and ownership is given to the
549 * caller.
550 */
egdanielc0648242014-09-22 13:17:02 -0700551 GrOptDrawState* createOptState(const GrDrawTargetCaps&) const;
egdaniel3658f382014-09-15 07:01:59 -0700552
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000553private:
egdaniel170f90b2014-09-16 12:54:40 -0700554 void invalidateOptState() const;
egdaniel3658f382014-09-15 07:01:59 -0700555
bsalomon8f727332014-08-05 07:50:06 -0700556 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000557
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000558 // Some of the auto restore objects assume that no effects are removed during their lifetime.
559 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000560 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000561
egdaniel7b3d5ee2014-08-28 05:41:14 -0700562 void internalSetVertexAttribs(const GrVertexAttrib attribs[], int count, size_t stride);
robertphillips@google.com42903302013-04-20 12:26:07 +0000563
egdaniel3658f382014-09-15 07:01:59 -0700564 mutable GrOptDrawState* fCachedOptState;
egdanielc0648242014-09-22 13:17:02 -0700565 mutable uint32_t fCachedCapsID;
egdaniel3658f382014-09-15 07:01:59 -0700566
egdaniel21aed572014-08-26 12:24:06 -0700567 typedef GrRODrawState INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000568};
569
570#endif