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