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