tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 11 | #include "GrBackendEffectFactory.h" |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 12 | #include "GrBlend.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 13 | #include "GrColor.h" |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 14 | #include "GrEffectStage.h" |
bsalomon@google.com | 73818dc | 2013-03-28 13:23:29 +0000 | [diff] [blame] | 15 | #include "GrPaint.h" |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 16 | #include "GrPoint.h" |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 17 | #include "GrRenderTarget.h" |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 18 | #include "GrStencil.h" |
| 19 | #include "GrTemplates.h" |
| 20 | #include "GrTexture.h" |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 21 | #include "GrTypesPriv.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 22 | #include "effects/GrSimpleTextureEffect.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 23 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 24 | #include "SkMatrix.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 25 | #include "SkTypes.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 26 | #include "SkXfermode.h" |
| 27 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 28 | class GrDrawState : public SkRefCnt { |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 29 | public: |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 30 | SK_DECLARE_INST_COUNT(GrDrawState) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 32 | GrDrawState() { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 33 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 34 | this->reset(); |
| 35 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 37 | GrDrawState(const SkMatrix& initialViewMatrix) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 38 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 39 | this->reset(initialViewMatrix); |
| 40 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Copies another draw state. |
| 44 | **/ |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 45 | GrDrawState(const GrDrawState& state) : INHERITED() { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 46 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 47 | *this = state; |
| 48 | } |
| 49 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Copies another draw state with a preconcat to the view matrix. |
| 52 | **/ |
| 53 | GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 54 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 55 | *this = state; |
| 56 | if (!preConcatMatrix.isIdentity()) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 57 | for (int i = 0; i < fColorStages.count(); ++i) { |
| 58 | fColorStages[i].localCoordChange(preConcatMatrix); |
| 59 | } |
| 60 | for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 61 | fCoverageStages[i].localCoordChange(preConcatMatrix); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 64 | } |
| 65 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 66 | virtual ~GrDrawState() { SkASSERT(0 == fBlockEffectRemovalCnt); } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 67 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 68 | /** |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 69 | * Resets to the default state. GrEffects will be removed from all stages. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | */ |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 71 | void reset() { this->onReset(NULL); } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 73 | void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); } |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 76 | * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that |
| 77 | * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 78 | * equivalents are set to default values. Clipping will be enabled. |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 79 | */ |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 80 | void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////// |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 83 | /// @name Vertex Attributes |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 84 | //// |
| 85 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 86 | enum { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 87 | kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 90 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 91 | * The format of vertices is represented as an array of GrVertexAttribs, with each representing |
| 92 | * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in |
| 93 | * GrTypesPriv.h). |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 94 | * |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 95 | * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when |
| 96 | * setEffect is called. |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 97 | */ |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 98 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 99 | /** |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 100 | * Sets vertex attributes for next draw. The object driving the templatization |
| 101 | * should be a global GrVertexAttrib array that is never changed. |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 102 | */ |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 103 | template <const GrVertexAttrib A[]> void setVertexAttribs(int count) { |
| 104 | this->setVertexAttribs(A, count); |
| 105 | } |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 106 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 107 | const GrVertexAttrib* getVertexAttribs() const { return fCommon.fVAPtr; } |
| 108 | int getVertexAttribCount() const { return fCommon.fVACount; } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 109 | |
| 110 | size_t getVertexSize() const; |
| 111 | |
| 112 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 113 | * Sets default vertex attributes for next draw. The default is a single attribute: |
| 114 | * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType} |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 115 | */ |
| 116 | void setDefaultVertexAttribs(); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 117 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the |
| 120 | * binding does not appear in the current attribs. These bindings should appear only once in |
| 121 | * the attrib array. |
| 122 | */ |
| 123 | |
| 124 | int positionAttributeIndex() const { |
| 125 | return fCommon.fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding]; |
| 126 | } |
| 127 | int localCoordAttributeIndex() const { |
| 128 | return fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding]; |
| 129 | } |
| 130 | int colorVertexAttributeIndex() const { |
| 131 | return fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; |
| 132 | } |
| 133 | int coverageVertexAttributeIndex() const { |
| 134 | return fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding]; |
| 135 | } |
| 136 | |
| 137 | bool hasLocalCoordAttribute() const { |
| 138 | return -1 != fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding]; |
| 139 | } |
| 140 | bool hasColorVertexAttribute() const { |
| 141 | return -1 != fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; |
| 142 | } |
| 143 | bool hasCoverageVertexAttribute() const { |
| 144 | return -1 != fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding]; |
| 145 | } |
| 146 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 147 | bool validateVertexAttribs() const; |
| 148 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 149 | /** |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 150 | * Helper to save/restore vertex attribs |
| 151 | */ |
| 152 | class AutoVertexAttribRestore { |
| 153 | public: |
| 154 | AutoVertexAttribRestore(GrDrawState* drawState) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 155 | SkASSERT(NULL != drawState); |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 156 | fDrawState = drawState; |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 157 | fVAPtr = drawState->fCommon.fVAPtr; |
| 158 | fVACount = drawState->fCommon.fVACount; |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 159 | fDrawState->setDefaultVertexAttribs(); |
| 160 | } |
| 161 | |
| 162 | ~AutoVertexAttribRestore(){ |
commit-bot@chromium.org | fe070ba | 2013-10-16 14:43:12 +0000 | [diff] [blame^] | 163 | fDrawState->setVertexAttribs(fVAPtr, fVACount); |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | private: |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 167 | GrDrawState* fDrawState; |
| 168 | const GrVertexAttrib* fVAPtr; |
| 169 | int fVACount; |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 173 | * Accessing positions, local coords, or colors, of a vertex within an array is a hassle |
| 174 | * involving casts and simple math. These helpers exist to keep GrDrawTarget clients' code a bit |
| 175 | * nicer looking. |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 176 | */ |
| 177 | |
| 178 | /** |
| 179 | * Gets a pointer to a GrPoint of a vertex's position or texture |
| 180 | * coordinate. |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 181 | * @param vertices the vertex array |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 182 | * @param vertexIndex the index of the vertex in the array |
| 183 | * @param vertexSize the size of each vertex in the array |
| 184 | * @param offset the offset in bytes of the vertex component. |
| 185 | * Defaults to zero (corresponding to vertex position) |
| 186 | * @return pointer to the vertex component as a GrPoint |
| 187 | */ |
| 188 | static GrPoint* GetVertexPoint(void* vertices, |
| 189 | int vertexIndex, |
| 190 | int vertexSize, |
| 191 | int offset = 0) { |
| 192 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 193 | return GrTCast<GrPoint*>(start + offset + |
| 194 | vertexIndex * vertexSize); |
| 195 | } |
| 196 | static const GrPoint* GetVertexPoint(const void* vertices, |
| 197 | int vertexIndex, |
| 198 | int vertexSize, |
| 199 | int offset = 0) { |
| 200 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 201 | return GrTCast<const GrPoint*>(start + offset + |
| 202 | vertexIndex * vertexSize); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Gets a pointer to a GrColor inside a vertex within a vertex array. |
| 207 | * @param vertices the vetex array |
| 208 | * @param vertexIndex the index of the vertex in the array |
| 209 | * @param vertexSize the size of each vertex in the array |
| 210 | * @param offset the offset in bytes of the vertex color |
| 211 | * @return pointer to the vertex component as a GrColor |
| 212 | */ |
| 213 | static GrColor* GetVertexColor(void* vertices, |
| 214 | int vertexIndex, |
| 215 | int vertexSize, |
| 216 | int offset) { |
| 217 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 218 | return GrTCast<GrColor*>(start + offset + |
| 219 | vertexIndex * vertexSize); |
| 220 | } |
| 221 | static const GrColor* GetVertexColor(const void* vertices, |
| 222 | int vertexIndex, |
| 223 | int vertexSize, |
| 224 | int offset) { |
| 225 | const intptr_t start = GrTCast<intptr_t>(vertices); |
| 226 | return GrTCast<const GrColor*>(start + offset + |
| 227 | vertexIndex * vertexSize); |
| 228 | } |
| 229 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 230 | /// @} |
| 231 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 232 | /** |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 233 | * Determines whether src alpha is guaranteed to be one for all src pixels |
| 234 | */ |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 235 | bool srcAlphaWillBeOne() const; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw. |
| 239 | */ |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 240 | bool hasSolidCoverage() const; |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 241 | |
| 242 | /// @} |
| 243 | |
| 244 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 245 | /// @name Color |
| 246 | //// |
| 247 | |
| 248 | /** |
| 249 | * Sets color for next draw to a premultiplied-alpha color. |
| 250 | * |
| 251 | * @param color the color to set. |
| 252 | */ |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 253 | void setColor(GrColor color) { fCommon.fColor = color; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 254 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 255 | GrColor getColor() const { return fCommon.fColor; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Sets the color to be used for the next draw to be |
| 259 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 260 | * |
| 261 | * @param alpha The alpha value to set as the color. |
| 262 | */ |
| 263 | void setAlpha(uint8_t a) { |
| 264 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Add a color filter that can be represented by a color and a mode. Applied |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 269 | * after color-computing effect stages. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 270 | */ |
| 271 | void setColorFilter(GrColor c, SkXfermode::Mode mode) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 272 | fCommon.fColorFilterColor = c; |
| 273 | fCommon.fColorFilterMode = mode; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 274 | } |
| 275 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 276 | GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; } |
| 277 | SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 278 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 279 | /** |
| 280 | * Constructor sets the color to be 'color' which is undone by the destructor. |
| 281 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 282 | class AutoColorRestore : public ::SkNoncopyable { |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 283 | public: |
sugoi@google.com | 66a58ac | 2013-03-05 20:40:52 +0000 | [diff] [blame] | 284 | AutoColorRestore() : fDrawState(NULL), fOldColor(0) {} |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 285 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 286 | AutoColorRestore(GrDrawState* drawState, GrColor color) { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 287 | fDrawState = NULL; |
| 288 | this->set(drawState, color); |
| 289 | } |
| 290 | |
| 291 | void reset() { |
| 292 | if (NULL != fDrawState) { |
| 293 | fDrawState->setColor(fOldColor); |
| 294 | fDrawState = NULL; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void set(GrDrawState* drawState, GrColor color) { |
| 299 | this->reset(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 300 | fDrawState = drawState; |
| 301 | fOldColor = fDrawState->getColor(); |
| 302 | fDrawState->setColor(color); |
| 303 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 304 | |
| 305 | ~AutoColorRestore() { this->reset(); } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 306 | private: |
| 307 | GrDrawState* fDrawState; |
| 308 | GrColor fOldColor; |
| 309 | }; |
| 310 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 311 | /// @} |
| 312 | |
| 313 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 314 | /// @name Coverage |
| 315 | //// |
| 316 | |
| 317 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 318 | * Sets a constant fractional coverage to be applied to the draw. The |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 319 | * initial value (after construction or reset()) is 0xff. The constant |
| 320 | * coverage is ignored when per-vertex coverage is provided. |
| 321 | */ |
| 322 | void setCoverage(uint8_t coverage) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 323 | fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Version of above that specifies 4 channel per-vertex color. The value |
| 328 | * should be premultiplied. |
| 329 | */ |
| 330 | void setCoverage4(GrColor coverage) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 331 | fCommon.fCoverage = coverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | GrColor getCoverage() const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 335 | return fCommon.fCoverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /// @} |
| 339 | |
| 340 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 341 | /// @name Effect Stages |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 342 | /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment |
| 343 | /// shader. Its inputs are the output from the previous stage as well as some variables |
| 344 | /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color, |
| 345 | /// the fragment position, local coordinates). |
| 346 | /// |
| 347 | /// The stages are divided into two sets, color-computing and coverage-computing. The final |
| 348 | /// color stage produces the final pixel color. The coverage-computing stages function exactly |
| 349 | /// as the color-computing but the output of the final coverage stage is treated as a fractional |
| 350 | /// pixel coverage rather than as input to the src/dst color blend step. |
| 351 | /// |
| 352 | /// The input color to the first color-stage is either the constant color or interpolated |
| 353 | /// per-vertex colors. The input to the first coverage stage is either a constant coverage |
| 354 | /// (usually full-coverage) or interpolated per-vertex coverage. |
| 355 | /// |
| 356 | /// See the documentation of kCoverageDrawing_StateBit for information about disabling the |
| 357 | /// the color / coverage distinction. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 358 | //// |
| 359 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 360 | const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 361 | SkASSERT(NULL != effect); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 362 | SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1)); |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 363 | return effect; |
| 364 | } |
skia.committer@gmail.com | 01c34ee | 2013-03-20 07:01:02 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 366 | const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 367 | SkASSERT(NULL != effect); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 368 | SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1)); |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 369 | return effect; |
| 370 | } |
| 371 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 372 | /** |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 373 | * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates. |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 374 | */ |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 375 | void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 376 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 377 | this->addColorEffect(effect)->unref(); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 378 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 379 | |
| 380 | void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) { |
| 381 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); |
| 382 | this->addCoverageEffect(effect)->unref(); |
| 383 | } |
| 384 | |
| 385 | void addColorTextureEffect(GrTexture* texture, |
| 386 | const SkMatrix& matrix, |
| 387 | const GrTextureParams& params) { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 388 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 389 | this->addColorEffect(effect)->unref(); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 390 | } |
| 391 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 392 | void addCoverageTextureEffect(GrTexture* texture, |
| 393 | const SkMatrix& matrix, |
| 394 | const GrTextureParams& params) { |
| 395 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params); |
| 396 | this->addCoverageEffect(effect)->unref(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 397 | } |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 398 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 399 | /** |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 400 | * When this object is destroyed it will remove any effects from the draw state that were added |
| 401 | * after its constructor. |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 402 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 403 | class AutoRestoreEffects : public ::SkNoncopyable { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 404 | public: |
bsalomon@google.com | 2fad5a8 | 2013-06-13 19:47:23 +0000 | [diff] [blame] | 405 | AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {} |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 406 | |
skia.committer@gmail.com | 5c493d5 | 2013-06-14 07:00:49 +0000 | [diff] [blame] | 407 | AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) { |
| 408 | this->set(ds); |
robertphillips@google.com | f09b87d | 2013-06-13 20:06:44 +0000 | [diff] [blame] | 409 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 410 | |
| 411 | ~AutoRestoreEffects() { this->set(NULL); } |
| 412 | |
| 413 | void set(GrDrawState* ds) { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 414 | if (NULL != fDrawState) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 415 | int n = fDrawState->fColorStages.count() - fColorEffectCnt; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 416 | SkASSERT(n >= 0); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 417 | fDrawState->fColorStages.pop_back_n(n); |
| 418 | n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 419 | SkASSERT(n >= 0); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 420 | fDrawState->fCoverageStages.pop_back_n(n); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 421 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 422 | } |
| 423 | fDrawState = ds; |
| 424 | if (NULL != ds) { |
| 425 | fColorEffectCnt = ds->fColorStages.count(); |
| 426 | fCoverageEffectCnt = ds->fCoverageStages.count(); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 427 | SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 430 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 431 | private: |
| 432 | GrDrawState* fDrawState; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 433 | int fColorEffectCnt; |
| 434 | int fCoverageEffectCnt; |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 435 | }; |
| 436 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 437 | int numColorStages() const { return fColorStages.count(); } |
| 438 | int numCoverageStages() const { return fCoverageStages.count(); } |
| 439 | int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 440 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 441 | const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; } |
| 442 | const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 443 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 444 | /** |
| 445 | * Checks whether any of the effects will read the dst pixel color. |
| 446 | */ |
bsalomon@google.com | d09ab84 | 2013-05-15 17:30:26 +0000 | [diff] [blame] | 447 | bool willEffectReadDstColor() const; |
reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame] | 448 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 449 | /// @} |
| 450 | |
| 451 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 452 | /// @name Blending |
| 453 | //// |
| 454 | |
| 455 | /** |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 456 | * Sets the blending function coefficients. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 457 | * |
| 458 | * The blend function will be: |
| 459 | * D' = sat(S*srcCoef + D*dstCoef) |
| 460 | * |
| 461 | * where D is the existing destination color, S is the incoming source |
| 462 | * color, and D' is the new destination color that will be written. sat() |
| 463 | * is the saturation function. |
| 464 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 465 | * @param srcCoef coefficient applied to the src color. |
| 466 | * @param dstCoef coefficient applied to the dst color. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 467 | */ |
| 468 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 469 | fCommon.fSrcBlend = srcCoeff; |
| 470 | fCommon.fDstBlend = dstCoeff; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 471 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 472 | if (GrBlendCoeffRefsDst(dstCoeff)) { |
| 473 | GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n"); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 474 | } |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 475 | if (GrBlendCoeffRefsSrc(srcCoeff)) { |
| 476 | GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n"); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 477 | } |
| 478 | #endif |
| 479 | } |
| 480 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 481 | GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; } |
| 482 | GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 483 | |
| 484 | void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff, |
| 485 | GrBlendCoeff* dstBlendCoeff) const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 486 | *srcBlendCoeff = fCommon.fSrcBlend; |
| 487 | *dstBlendCoeff = fCommon.fDstBlend; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Sets the blending function constant referenced by the following blending |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 492 | * coefficients: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 493 | * kConstC_GrBlendCoeff |
| 494 | * kIConstC_GrBlendCoeff |
| 495 | * kConstA_GrBlendCoeff |
| 496 | * kIConstA_GrBlendCoeff |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 497 | * |
| 498 | * @param constant the constant to set |
| 499 | */ |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 500 | void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * Retrieves the last value set by setBlendConstant() |
| 504 | * @return the blending constant value |
| 505 | */ |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 506 | GrColor getBlendConstant() const { return fCommon.fBlendConstant; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 507 | |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 508 | /** |
| 509 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 510 | * coverage before the blend will give the correct final destination color. In general it |
| 511 | * will not as coverage is applied after blending. |
| 512 | */ |
| 513 | bool canTweakAlphaForCoverage() const; |
| 514 | |
| 515 | /** |
| 516 | * Optimizations for blending / coverage to that can be applied based on the current state. |
| 517 | */ |
| 518 | enum BlendOptFlags { |
| 519 | /** |
| 520 | * No optimization |
| 521 | */ |
| 522 | kNone_BlendOpt = 0, |
| 523 | /** |
| 524 | * Don't draw at all |
| 525 | */ |
| 526 | kSkipDraw_BlendOptFlag = 0x1, |
| 527 | /** |
| 528 | * Emit the src color, disable HW blending (replace dst with src) |
| 529 | */ |
| 530 | kDisableBlend_BlendOptFlag = 0x2, |
| 531 | /** |
| 532 | * The coverage value does not have to be computed separately from alpha, the the output |
| 533 | * color can be the modulation of the two. |
| 534 | */ |
| 535 | kCoverageAsAlpha_BlendOptFlag = 0x4, |
| 536 | /** |
| 537 | * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are |
| 538 | * "don't cares". |
| 539 | */ |
| 540 | kEmitCoverage_BlendOptFlag = 0x8, |
| 541 | /** |
| 542 | * Emit transparent black instead of the src color, no need to compute coverage. |
| 543 | */ |
| 544 | kEmitTransBlack_BlendOptFlag = 0x10, |
| 545 | }; |
| 546 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 547 | |
| 548 | /** |
| 549 | * Determines what optimizations can be applied based on the blend. The coefficients may have |
| 550 | * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional |
| 551 | * params that receive the tweaked coefficients. Normally the function looks at the current |
| 552 | * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively |
| 553 | * determine the blend optimizations that would be used if there was partial pixel coverage. |
| 554 | * |
| 555 | * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for |
| 556 | * playback) must call this function and respect the flags that replace the output color. |
| 557 | */ |
| 558 | BlendOptFlags getBlendOpts(bool forceCoverage = false, |
| 559 | GrBlendCoeff* srcCoeff = NULL, |
| 560 | GrBlendCoeff* dstCoeff = NULL) const; |
| 561 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 562 | /// @} |
| 563 | |
| 564 | /////////////////////////////////////////////////////////////////////////// |
| 565 | /// @name View Matrix |
| 566 | //// |
| 567 | |
| 568 | /** |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 569 | * Sets the view matrix to identity and updates any installed effects to compensate for the |
| 570 | * coord system change. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 571 | */ |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 572 | bool setIdentityViewMatrix(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 573 | |
| 574 | /** |
| 575 | * Retrieves the current view matrix |
| 576 | * @return the current view matrix. |
| 577 | */ |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 578 | const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 579 | |
| 580 | /** |
| 581 | * Retrieves the inverse of the current view matrix. |
| 582 | * |
| 583 | * If the current view matrix is invertible, return true, and if matrix |
| 584 | * is non-null, copy the inverse into it. If the current view matrix is |
| 585 | * non-invertible, return false and ignore the matrix parameter. |
| 586 | * |
| 587 | * @param matrix if not null, will receive a copy of the current inverse. |
| 588 | */ |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 589 | bool getViewInverse(SkMatrix* matrix) const { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 590 | // TODO: determine whether we really need to leave matrix unmodified |
| 591 | // at call sites when inversion fails. |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 592 | SkMatrix inverse; |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 593 | if (fCommon.fViewMatrix.invert(&inverse)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 594 | if (matrix) { |
| 595 | *matrix = inverse; |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | return false; |
| 600 | } |
| 601 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 602 | //////////////////////////////////////////////////////////////////////////// |
| 603 | |
| 604 | /** |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 605 | * Preconcats the current view matrix and restores the previous view matrix in the destructor. |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 606 | * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor. |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 607 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 608 | class AutoViewMatrixRestore : public ::SkNoncopyable { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 609 | public: |
| 610 | AutoViewMatrixRestore() : fDrawState(NULL) {} |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 611 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 612 | AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 613 | fDrawState = NULL; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 614 | this->set(ds, preconcatMatrix); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 615 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 616 | |
| 617 | ~AutoViewMatrixRestore() { this->restore(); } |
| 618 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 619 | /** |
| 620 | * Can be called prior to destructor to restore the original matrix. |
| 621 | */ |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 622 | void restore(); |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 623 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 624 | void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 625 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 626 | /** Sets the draw state's matrix to identity. This can fail because the current view matrix |
| 627 | is not invertible. */ |
| 628 | bool setIdentity(GrDrawState* drawState); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 629 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 630 | private: |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 631 | void doEffectCoordChanges(const SkMatrix& coordChangeMatrix); |
| 632 | |
| 633 | GrDrawState* fDrawState; |
| 634 | SkMatrix fViewMatrix; |
| 635 | int fNumColorStages; |
| 636 | SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 637 | }; |
| 638 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 639 | /// @} |
| 640 | |
| 641 | /////////////////////////////////////////////////////////////////////////// |
| 642 | /// @name Render Target |
| 643 | //// |
| 644 | |
| 645 | /** |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 646 | * Sets the render-target used at the next drawing call |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 647 | * |
| 648 | * @param target The render target to set. |
| 649 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 650 | void setRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 651 | fRenderTarget.reset(SkSafeRef(target)); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 652 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 653 | |
| 654 | /** |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 655 | * Retrieves the currently set render-target. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 656 | * |
| 657 | * @return The currently set render target. |
| 658 | */ |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 659 | const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
| 660 | GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 661 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 662 | class AutoRenderTargetRestore : public ::SkNoncopyable { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 663 | public: |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 664 | AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {} |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 665 | AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 666 | fDrawState = NULL; |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 667 | fSavedTarget = NULL; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 668 | this->set(ds, newTarget); |
| 669 | } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 670 | ~AutoRenderTargetRestore() { this->restore(); } |
| 671 | |
| 672 | void restore() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 673 | if (NULL != fDrawState) { |
| 674 | fDrawState->setRenderTarget(fSavedTarget); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 675 | fDrawState = NULL; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 676 | } |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 677 | SkSafeSetNull(fSavedTarget); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void set(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 681 | this->restore(); |
| 682 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 683 | if (NULL != ds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 684 | SkASSERT(NULL == fSavedTarget); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 685 | fSavedTarget = ds->getRenderTarget(); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 686 | SkSafeRef(fSavedTarget); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 687 | ds->setRenderTarget(newTarget); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 688 | fDrawState = ds; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 689 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 690 | } |
| 691 | private: |
| 692 | GrDrawState* fDrawState; |
| 693 | GrRenderTarget* fSavedTarget; |
| 694 | }; |
| 695 | |
| 696 | /// @} |
| 697 | |
| 698 | /////////////////////////////////////////////////////////////////////////// |
| 699 | /// @name Stencil |
| 700 | //// |
| 701 | |
| 702 | /** |
| 703 | * Sets the stencil settings to use for the next draw. |
| 704 | * Changing the clip has the side-effect of possibly zeroing |
| 705 | * out the client settable stencil bits. So multipass algorithms |
| 706 | * using stencil should not change the clip between passes. |
| 707 | * @param settings the stencil settings to use. |
| 708 | */ |
| 709 | void setStencil(const GrStencilSettings& settings) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 710 | fCommon.fStencilSettings = settings; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Shortcut to disable stencil testing and ops. |
| 715 | */ |
| 716 | void disableStencil() { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 717 | fCommon.fStencilSettings.setDisabled(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 718 | } |
| 719 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 720 | const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 721 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 722 | GrStencilSettings* stencil() { return &fCommon.fStencilSettings; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 723 | |
| 724 | /// @} |
| 725 | |
| 726 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 727 | /// @name State Flags |
| 728 | //// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 729 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 730 | /** |
| 731 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 732 | * default to disabled. |
| 733 | */ |
| 734 | enum StateBits { |
| 735 | /** |
| 736 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 737 | */ |
| 738 | kDither_StateBit = 0x01, |
| 739 | /** |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 740 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 741 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 742 | * the 3D API. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 743 | */ |
| 744 | kHWAntialias_StateBit = 0x02, |
| 745 | /** |
| 746 | * Draws will respect the clip, otherwise the clip is ignored. |
| 747 | */ |
| 748 | kClip_StateBit = 0x04, |
| 749 | /** |
| 750 | * Disables writing to the color buffer. Useful when performing stencil |
| 751 | * operations. |
| 752 | */ |
| 753 | kNoColorWrites_StateBit = 0x08, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 754 | |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 755 | /** |
| 756 | * Usually coverage is applied after color blending. The color is blended using the coeffs |
| 757 | * specified by setBlendFunc(). The blended color is then combined with dst using coeffs |
| 758 | * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In |
| 759 | * this case there is no distinction between coverage and color and the caller needs direct |
| 760 | * control over the blend coeffs. When set, there will be a single blend step controlled by |
| 761 | * setBlendFunc() which will use coverage*color as the src color. |
| 762 | */ |
| 763 | kCoverageDrawing_StateBit = 0x10, |
| 764 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 765 | // Users of the class may add additional bits to the vector |
| 766 | kDummyStateBit, |
| 767 | kLastPublicStateBit = kDummyStateBit-1, |
| 768 | }; |
| 769 | |
| 770 | void resetStateFlags() { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 771 | fCommon.fFlagBits = 0; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Enable render state settings. |
| 776 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 777 | * @param stateBits bitfield of StateBits specifying the states to enable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 778 | */ |
| 779 | void enableState(uint32_t stateBits) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 780 | fCommon.fFlagBits |= stateBits; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Disable render state settings. |
| 785 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 786 | * @param stateBits bitfield of StateBits specifying the states to disable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 787 | */ |
| 788 | void disableState(uint32_t stateBits) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 789 | fCommon.fFlagBits &= ~(stateBits); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 790 | } |
| 791 | |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 792 | /** |
| 793 | * Enable or disable stateBits based on a boolean. |
| 794 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 795 | * @param stateBits bitfield of StateBits to enable or disable |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 796 | * @param enable if true enable stateBits, otherwise disable |
| 797 | */ |
| 798 | void setState(uint32_t stateBits, bool enable) { |
| 799 | if (enable) { |
| 800 | this->enableState(stateBits); |
| 801 | } else { |
| 802 | this->disableState(stateBits); |
| 803 | } |
| 804 | } |
| 805 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 806 | bool isDitherState() const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 807 | return 0 != (fCommon.fFlagBits & kDither_StateBit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | bool isHWAntialiasState() const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 811 | return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | bool isClipState() const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 815 | return 0 != (fCommon.fFlagBits & kClip_StateBit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | bool isColorWriteDisabled() const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 819 | return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 820 | } |
| 821 | |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 822 | bool isCoverageDrawing() const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 823 | return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit); |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 824 | } |
| 825 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 826 | bool isStateFlagEnabled(uint32_t stateBit) const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 827 | return 0 != (stateBit & fCommon.fFlagBits); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 828 | } |
| 829 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 830 | /// @} |
| 831 | |
| 832 | /////////////////////////////////////////////////////////////////////////// |
| 833 | /// @name Face Culling |
| 834 | //// |
| 835 | |
| 836 | enum DrawFace { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 837 | kInvalid_DrawFace = -1, |
| 838 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 839 | kBoth_DrawFace, |
| 840 | kCCW_DrawFace, |
| 841 | kCW_DrawFace, |
| 842 | }; |
| 843 | |
| 844 | /** |
| 845 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 846 | * @param face the face(s) to draw. |
| 847 | */ |
| 848 | void setDrawFace(DrawFace face) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 849 | SkASSERT(kInvalid_DrawFace != face); |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 850 | fCommon.fDrawFace = face; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 855 | * or both faces. |
| 856 | * @return the current draw face(s). |
| 857 | */ |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 858 | DrawFace getDrawFace() const { return fCommon.fDrawFace; } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 859 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 860 | /// @} |
| 861 | |
| 862 | /////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 863 | |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 864 | bool operator ==(const GrDrawState& s) const { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 865 | if (fRenderTarget.get() != s.fRenderTarget.get() || |
| 866 | fColorStages.count() != s.fColorStages.count() || |
| 867 | fCoverageStages.count() != s.fCoverageStages.count() || |
| 868 | fCommon != s.fCommon) { |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 869 | return false; |
| 870 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 871 | for (int i = 0; i < fColorStages.count(); i++) { |
| 872 | if (fColorStages[i] != s.fColorStages[i]) { |
bsalomon@google.com | f2f8fc3 | 2012-07-18 18:25:07 +0000 | [diff] [blame] | 873 | return false; |
| 874 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 875 | } |
| 876 | for (int i = 0; i < fCoverageStages.count(); i++) { |
| 877 | if (fCoverageStages[i] != s.fCoverageStages[i]) { |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 878 | return false; |
| 879 | } |
| 880 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 881 | return true; |
| 882 | } |
| 883 | bool operator !=(const GrDrawState& s) const { return !(*this == s); } |
| 884 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 885 | GrDrawState& operator= (const GrDrawState& s) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 886 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 887 | this->setRenderTarget(s.fRenderTarget.get()); |
| 888 | fCommon = s.fCommon; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 889 | fColorStages = s.fColorStages; |
| 890 | fCoverageStages = s.fCoverageStages; |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 891 | return *this; |
| 892 | } |
| 893 | |
| 894 | private: |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 895 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 896 | void onReset(const SkMatrix* initialViewMatrix) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 897 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 898 | fColorStages.reset(); |
| 899 | fCoverageStages.reset(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 900 | |
| 901 | fRenderTarget.reset(NULL); |
| 902 | |
| 903 | this->setDefaultVertexAttribs(); |
| 904 | |
| 905 | fCommon.fColor = 0xffffffff; |
| 906 | if (NULL == initialViewMatrix) { |
| 907 | fCommon.fViewMatrix.reset(); |
| 908 | } else { |
| 909 | fCommon.fViewMatrix = *initialViewMatrix; |
| 910 | } |
| 911 | fCommon.fSrcBlend = kOne_GrBlendCoeff; |
| 912 | fCommon.fDstBlend = kZero_GrBlendCoeff; |
| 913 | fCommon.fBlendConstant = 0x0; |
| 914 | fCommon.fFlagBits = 0x0; |
| 915 | fCommon.fStencilSettings.setDisabled(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 916 | fCommon.fCoverage = 0xffffffff; |
| 917 | fCommon.fColorFilterMode = SkXfermode::kDst_Mode; |
| 918 | fCommon.fColorFilterColor = 0x0; |
| 919 | fCommon.fDrawFace = kBoth_DrawFace; |
| 920 | } |
| 921 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 922 | /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */ |
| 923 | struct CommonState { |
| 924 | // These fields are roughly sorted by decreasing likelihood of being different in op== |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 925 | GrColor fColor; |
| 926 | SkMatrix fViewMatrix; |
| 927 | GrBlendCoeff fSrcBlend; |
| 928 | GrBlendCoeff fDstBlend; |
| 929 | GrColor fBlendConstant; |
| 930 | uint32_t fFlagBits; |
| 931 | const GrVertexAttrib* fVAPtr; |
| 932 | int fVACount; |
| 933 | GrStencilSettings fStencilSettings; |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 934 | GrColor fCoverage; |
| 935 | SkXfermode::Mode fColorFilterMode; |
| 936 | GrColor fColorFilterColor; |
| 937 | DrawFace fDrawFace; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 938 | |
| 939 | // This is simply a different representation of info in fVertexAttribs and thus does |
| 940 | // not need to be compared in op==. |
| 941 | int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt]; |
| 942 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 943 | bool operator== (const CommonState& other) const { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 944 | bool result = fColor == other.fColor && |
| 945 | fViewMatrix.cheapEqualTo(other.fViewMatrix) && |
| 946 | fSrcBlend == other.fSrcBlend && |
| 947 | fDstBlend == other.fDstBlend && |
| 948 | fBlendConstant == other.fBlendConstant && |
| 949 | fFlagBits == other.fFlagBits && |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 950 | fVACount == other.fVACount && |
| 951 | !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) && |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 952 | fStencilSettings == other.fStencilSettings && |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 953 | fCoverage == other.fCoverage && |
| 954 | fColorFilterMode == other.fColorFilterMode && |
| 955 | fColorFilterColor == other.fColorFilterColor && |
| 956 | fDrawFace == other.fDrawFace; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 957 | SkASSERT(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 958 | other.fFixedFunctionVertexAttribIndices, |
| 959 | sizeof(fFixedFunctionVertexAttribIndices))); |
| 960 | return result; |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 961 | } |
| 962 | bool operator!= (const CommonState& other) const { return !(*this == other); } |
| 963 | }; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 964 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 965 | /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef. |
| 966 | DeferredState must directly reference GrEffects, however. */ |
| 967 | struct SavedEffectStage { |
| 968 | SavedEffectStage() : fEffect(NULL) {} |
| 969 | const GrEffect* fEffect; |
| 970 | GrEffectStage::SavedCoordChange fCoordChange; |
| 971 | }; |
| 972 | |
| 973 | public: |
| 974 | /** |
| 975 | * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource |
| 976 | * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal |
| 977 | * dispose mechanism returns them to the cache. This allows recycling resources through the |
| 978 | * the cache while they are in a deferred draw queue. |
| 979 | */ |
| 980 | class DeferredState { |
| 981 | public: |
| 982 | DeferredState() : fRenderTarget(NULL) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 983 | SkDEBUGCODE(fInitialized = false;) |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 984 | } |
| 985 | // TODO: Remove this when DeferredState no longer holds a ref to the RT |
| 986 | ~DeferredState() { SkSafeUnref(fRenderTarget); } |
| 987 | |
| 988 | void saveFrom(const GrDrawState& drawState) { |
| 989 | fCommon = drawState.fCommon; |
| 990 | // TODO: Here we will copy the GrRenderTarget pointer without taking a ref. |
| 991 | fRenderTarget = drawState.fRenderTarget.get(); |
| 992 | SkSafeRef(fRenderTarget); |
| 993 | // Here we ref the effects directly rather than the effect-refs. TODO: When the effect- |
| 994 | // ref gets fully unref'ed it will cause the underlying effect to unref its resources |
| 995 | // and recycle them to the cache (if no one else is holding a ref to the resources). |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 996 | fStages.reset(drawState.fColorStages.count() + drawState.fCoverageStages.count()); |
| 997 | fColorStageCnt = drawState.fColorStages.count(); |
| 998 | for (int i = 0; i < fColorStageCnt; ++i) { |
| 999 | fStages[i].saveFrom(drawState.fColorStages[i]); |
| 1000 | } |
| 1001 | for (int i = 0; i < drawState.fCoverageStages.count(); ++i) { |
| 1002 | fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i]); |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1003 | } |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 1004 | SkDEBUGCODE(fInitialized = true;) |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | void restoreTo(GrDrawState* drawState) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1008 | SkASSERT(fInitialized); |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1009 | drawState->fCommon = fCommon; |
| 1010 | drawState->setRenderTarget(fRenderTarget); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1011 | // reinflate color/cov stage arrays. |
commit-bot@chromium.org | 2d3b492 | 2013-07-15 13:54:06 +0000 | [diff] [blame] | 1012 | drawState->fColorStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1013 | for (int i = 0; i < fColorStageCnt; ++i) { |
commit-bot@chromium.org | 2d3b492 | 2013-07-15 13:54:06 +0000 | [diff] [blame] | 1014 | SkNEW_APPEND_TO_TARRAY(&drawState->fColorStages, GrEffectStage, (fStages[i])); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1015 | } |
| 1016 | int coverageStageCnt = fStages.count() - fColorStageCnt; |
commit-bot@chromium.org | 2d3b492 | 2013-07-15 13:54:06 +0000 | [diff] [blame] | 1017 | drawState->fCoverageStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1018 | for (int i = 0; i < coverageStageCnt; ++i) { |
commit-bot@chromium.org | 2d3b492 | 2013-07-15 13:54:06 +0000 | [diff] [blame] | 1019 | SkNEW_APPEND_TO_TARRAY(&drawState->fCoverageStages, |
| 1020 | GrEffectStage, (fStages[i + fColorStageCnt])); |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | bool isEqual(const GrDrawState& state) const { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1025 | int numCoverageStages = fStages.count() - fColorStageCnt; |
| 1026 | if (fRenderTarget != state.fRenderTarget.get() || |
| 1027 | fColorStageCnt != state.fColorStages.count() || |
| 1028 | numCoverageStages != state.fCoverageStages.count() || |
| 1029 | fCommon != state.fCommon) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1030 | return false; |
| 1031 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1032 | bool explicitLocalCoords = state.hasLocalCoordAttribute(); |
| 1033 | for (int i = 0; i < fColorStageCnt; ++i) { |
| 1034 | if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoords)) { |
| 1035 | return false; |
| 1036 | } |
| 1037 | } |
| 1038 | for (int i = 0; i < numCoverageStages; ++i) { |
| 1039 | int s = fColorStageCnt + i; |
| 1040 | if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalCoords)) { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1041 | return false; |
| 1042 | } |
| 1043 | } |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
| 1047 | private: |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1048 | typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArray; |
| 1049 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1050 | GrRenderTarget* fRenderTarget; |
| 1051 | CommonState fCommon; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1052 | int fColorStageCnt; |
| 1053 | DeferredStageArray fStages; |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1054 | |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 1055 | SkDEBUGCODE(bool fInitialized;) |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | private: |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1059 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1060 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 1061 | CommonState fCommon; |
| 1062 | |
| 1063 | typedef SkSTArray<4, GrEffectStage> EffectStageArray; |
| 1064 | EffectStageArray fColorStages; |
| 1065 | EffectStageArray fCoverageStages; |
| 1066 | |
| 1067 | // Some of the auto restore objects assume that no effects are removed during their lifetime. |
| 1068 | // This is used to assert that this condition holds. |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 1069 | SkDEBUGCODE(int fBlockEffectRemovalCnt;) |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1070 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 1071 | /** |
| 1072 | * Sets vertex attributes for next draw. |
| 1073 | * |
| 1074 | * @param attribs the array of vertex attributes to set. |
| 1075 | * @param count the number of attributes being set, limited to kMaxVertexAttribCnt. |
| 1076 | */ |
| 1077 | void setVertexAttribs(const GrVertexAttrib attribs[], int count); |
| 1078 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 1079 | typedef SkRefCnt INHERITED; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1080 | }; |
| 1081 | |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 1082 | GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
| 1083 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1084 | #endif |