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