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