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 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 11 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 12 | #include "GrBlend.h" |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 13 | #include "GrDrawTargetCaps.h" |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 14 | #include "GrGpuResourceRef.h" |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 15 | #include "GrProcessorStage.h" |
| 16 | #include "GrRenderTarget.h" |
| 17 | #include "GrStencil.h" |
| 18 | #include "SkMatrix.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 19 | #include "effects/GrSimpleTextureEffect.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 20 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 21 | class GrDrawTargetCaps; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 22 | class GrOptDrawState; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 23 | class GrPaint; |
| 24 | class GrTexture; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 25 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 26 | class GrDrawState : public SkRefCnt { |
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 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 30 | GrDrawState() : fCachedOptState(NULL) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 31 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 32 | this->reset(); |
| 33 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 34 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 35 | GrDrawState(const SkMatrix& initialViewMatrix) : fCachedOptState(NULL) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 36 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 37 | this->reset(initialViewMatrix); |
| 38 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Copies another draw state. |
| 42 | **/ |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 43 | GrDrawState(const GrDrawState& state) : INHERITED(), fCachedOptState(NULL) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 44 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 45 | *this = state; |
| 46 | } |
| 47 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 48 | /** |
| 49 | * Copies another draw state with a preconcat to the view matrix. |
| 50 | **/ |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 51 | GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 52 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 53 | virtual ~GrDrawState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 55 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 56 | * Resets to the default state. GrProcessors will be removed from all stages. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | */ |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 58 | void reset() { this->onReset(NULL); } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 59 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 60 | void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); } |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 63 | * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that |
| 64 | * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 65 | * equivalents are set to default values with the exception of vertex attribute state which |
| 66 | * is unmodified by this function and clipping which will be enabled. |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 67 | */ |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 68 | void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////// |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 71 | /// @name Vertex Attributes |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 72 | //// |
| 73 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 74 | enum { |
| 75 | kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
| 76 | }; |
| 77 | |
| 78 | const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } |
| 79 | int getVertexAttribCount() const { return fVACount; } |
| 80 | |
| 81 | size_t getVertexStride() const { return fVAStride; } |
| 82 | |
| 83 | bool hasLocalCoordAttribute() const { |
| 84 | return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding]; |
| 85 | } |
| 86 | bool hasColorVertexAttribute() const { |
| 87 | return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; |
| 88 | } |
| 89 | bool hasCoverageVertexAttribute() const { |
| 90 | return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding]; |
| 91 | } |
| 92 | |
| 93 | const int* getFixedFunctionVertexAttribIndices() const { |
| 94 | return fFixedFunctionVertexAttribIndices; |
| 95 | } |
| 96 | |
| 97 | bool validateVertexAttribs() const; |
| 98 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 99 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 100 | * The format of vertices is represented as an array of GrVertexAttribs, with each representing |
| 101 | * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in |
| 102 | * GrTypesPriv.h). |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 103 | * |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 104 | * The mapping of attributes with kEffect bindings to GrProcessor inputs is specified when |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 105 | * setEffect is called. |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 106 | */ |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 107 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 108 | /** |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 109 | * Sets vertex attributes for next draw. The object driving the templatization |
| 110 | * should be a global GrVertexAttrib array that is never changed. |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 111 | * |
| 112 | * @param count the number of attributes being set, limited to kMaxVertexAttribCnt. |
| 113 | * @param stride the number of bytes between successive vertex data. |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 114 | */ |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 115 | template <const GrVertexAttrib A[]> void setVertexAttribs(int count, size_t stride) { |
| 116 | this->internalSetVertexAttribs(A, count, stride); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 117 | } |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 118 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 119 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 120 | * Sets default vertex attributes for next draw. The default is a single attribute: |
| 121 | * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType} |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 122 | */ |
| 123 | void setDefaultVertexAttribs(); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 124 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 125 | /** |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 126 | * Helper to save/restore vertex attribs |
| 127 | */ |
| 128 | class AutoVertexAttribRestore { |
| 129 | public: |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 130 | AutoVertexAttribRestore(GrDrawState* drawState); |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 131 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 132 | ~AutoVertexAttribRestore() { fDrawState->internalSetVertexAttribs(fVAPtr, fVACount, |
| 133 | fVAStride); } |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 134 | |
| 135 | private: |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 136 | GrDrawState* fDrawState; |
| 137 | const GrVertexAttrib* fVAPtr; |
| 138 | int fVACount; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 139 | size_t fVAStride; |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 142 | /// @} |
| 143 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 144 | /** |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 145 | * Depending on features available in the underlying 3D API and the color blend mode requested |
| 146 | * it may or may not be possible to correctly blend with fractional pixel coverage generated by |
| 147 | * the fragment shader. |
| 148 | * |
| 149 | * This function considers the current draw state and the draw target's capabilities to |
| 150 | * determine whether coverage can be handled correctly. This function assumes that the caller |
| 151 | * intends to specify fractional pixel coverage (via setCoverage(), through a coverage vertex |
| 152 | * attribute, or a coverage effect) but may not have specified it yet. |
| 153 | */ |
| 154 | bool couldApplyCoverage(const GrDrawTargetCaps& caps) const; |
| 155 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw. |
| 158 | */ |
| 159 | bool hasSolidCoverage() const; |
| 160 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 161 | /// @} |
| 162 | |
| 163 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 164 | /// @name Color |
| 165 | //// |
| 166 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 167 | GrColor getColor() const { return fColor; } |
| 168 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Sets color for next draw to a premultiplied-alpha color. |
| 171 | * |
| 172 | * @param color the color to set. |
| 173 | */ |
egdaniel | 9514d24 | 2014-07-18 06:15:43 -0700 | [diff] [blame] | 174 | void setColor(GrColor color) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 175 | if (color != fColor) { |
| 176 | fColor = color; |
| 177 | this->invalidateOptState(); |
| 178 | } |
egdaniel | 9514d24 | 2014-07-18 06:15:43 -0700 | [diff] [blame] | 179 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Sets the color to be used for the next draw to be |
| 183 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 184 | * |
| 185 | * @param alpha The alpha value to set as the color. |
| 186 | */ |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 187 | void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) | a); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 188 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 189 | /// @} |
| 190 | |
| 191 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 192 | /// @name Coverage |
| 193 | //// |
| 194 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 195 | uint8_t getCoverage() const { return fCoverage; } |
| 196 | |
| 197 | GrColor getCoverageColor() const { |
| 198 | return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage); |
| 199 | } |
| 200 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 201 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 202 | * 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] | 203 | * initial value (after construction or reset()) is 0xff. The constant |
| 204 | * coverage is ignored when per-vertex coverage is provided. |
| 205 | */ |
| 206 | void setCoverage(uint8_t coverage) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 207 | if (coverage != fCoverage) { |
| 208 | fCoverage = coverage; |
| 209 | this->invalidateOptState(); |
| 210 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 211 | } |
| 212 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 213 | /// @} |
| 214 | |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 215 | /** |
| 216 | * The geometry processor is the sole element of the skia pipeline which can use the vertex, |
| 217 | * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader |
| 218 | * but is never put in the color processing pipeline. |
| 219 | */ |
| 220 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 221 | const GrGeometryProcessor* setGeometryProcessor(const GrGeometryProcessor* geometryProcessor) { |
| 222 | SkASSERT(geometryProcessor); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 223 | SkASSERT(!this->hasGeometryProcessor()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 224 | fGeometryProcessor.reset(SkRef(geometryProcessor)); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 225 | this->invalidateOptState(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 226 | return geometryProcessor; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 227 | } |
| 228 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 229 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 230 | /// @name Effect Stages |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 231 | /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the |
| 232 | /// fragment shader. Its inputs are the output from the previous stage as well as some variables |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 233 | /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color, |
| 234 | /// the fragment position, local coordinates). |
| 235 | /// |
| 236 | /// The stages are divided into two sets, color-computing and coverage-computing. The final |
| 237 | /// color stage produces the final pixel color. The coverage-computing stages function exactly |
| 238 | /// as the color-computing but the output of the final coverage stage is treated as a fractional |
| 239 | /// pixel coverage rather than as input to the src/dst color blend step. |
| 240 | /// |
| 241 | /// The input color to the first color-stage is either the constant color or interpolated |
| 242 | /// per-vertex colors. The input to the first coverage stage is either a constant coverage |
| 243 | /// (usually full-coverage) or interpolated per-vertex coverage. |
| 244 | /// |
| 245 | /// See the documentation of kCoverageDrawing_StateBit for information about disabling the |
| 246 | /// the color / coverage distinction. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 247 | //// |
| 248 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 249 | int numColorStages() const { return fColorStages.count(); } |
| 250 | int numCoverageStages() const { return fCoverageStages.count(); } |
| 251 | int numTotalStages() const { |
| 252 | return this->numColorStages() + this->numCoverageStages() + |
| 253 | (this->hasGeometryProcessor() ? 1 : 0); |
| 254 | } |
| 255 | |
| 256 | bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 257 | const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 258 | const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; } |
| 259 | const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; } |
| 260 | |
| 261 | /** |
| 262 | * Checks whether any of the effects will read the dst pixel color. |
| 263 | */ |
| 264 | bool willEffectReadDstColor() const; |
| 265 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 266 | const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 267 | SkASSERT(effect); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 268 | SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect)); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 269 | this->invalidateOptState(); |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 270 | return effect; |
| 271 | } |
skia.committer@gmail.com | 01c34ee | 2013-03-20 07:01:02 +0000 | [diff] [blame] | 272 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 273 | const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 274 | SkASSERT(effect); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 275 | SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect)); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 276 | this->invalidateOptState(); |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 277 | return effect; |
| 278 | } |
| 279 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 280 | /** |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 281 | * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates. |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 282 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { |
| 284 | this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref(); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 285 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 286 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 287 | void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { |
| 288 | this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 289 | } |
| 290 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 291 | void addColorTextureProcessor(GrTexture* texture, |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 292 | const SkMatrix& matrix, |
| 293 | const GrTextureParams& params) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 294 | this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref(); |
| 295 | } |
| 296 | |
| 297 | void addCoverageTextureProcessor(GrTexture* texture, |
| 298 | const SkMatrix& matrix, |
| 299 | const GrTextureParams& params) { |
| 300 | this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 301 | } |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 302 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 303 | /** |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 304 | * When this object is destroyed it will remove any color/coverage effects from the draw state |
| 305 | * that were added after its constructor. |
| 306 | * |
| 307 | * This class has strange behavior around geometry processor. If there is a GP on the draw state |
| 308 | * it will assert that the GP is not modified until after the destructor of the ARE. If the |
| 309 | * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the |
| 310 | * destructor. |
| 311 | * |
| 312 | * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add |
| 313 | * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState |
| 314 | * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred |
| 315 | * execution state and GrOptDrawState always will be (and will be immutable and therefore |
| 316 | * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore |
| 317 | * the GP. |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 318 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 319 | class AutoRestoreEffects : public ::SkNoncopyable { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 320 | public: |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 321 | AutoRestoreEffects() |
| 322 | : fDrawState(NULL) |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 323 | , fOriginalGPID(SK_InvalidUniqueID) |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 324 | , fColorEffectCnt(0) |
| 325 | , fCoverageEffectCnt(0) {} |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 326 | |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 327 | AutoRestoreEffects(GrDrawState* ds) |
| 328 | : fDrawState(NULL) |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 329 | , fOriginalGPID(SK_InvalidUniqueID) |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 330 | , fColorEffectCnt(0) |
| 331 | , fCoverageEffectCnt(0) { |
skia.committer@gmail.com | 5c493d5 | 2013-06-14 07:00:49 +0000 | [diff] [blame] | 332 | this->set(ds); |
robertphillips@google.com | f09b87d | 2013-06-13 20:06:44 +0000 | [diff] [blame] | 333 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 334 | |
| 335 | ~AutoRestoreEffects() { this->set(NULL); } |
| 336 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 337 | void set(GrDrawState* ds); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 338 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 339 | bool isSet() const { return SkToBool(fDrawState); } |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 340 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 341 | private: |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 342 | GrDrawState* fDrawState; |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 343 | uint32_t fOriginalGPID; |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 344 | int fColorEffectCnt; |
| 345 | int fCoverageEffectCnt; |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 348 | /// @} |
| 349 | |
| 350 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 351 | /// @name Blending |
| 352 | //// |
| 353 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 354 | GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 355 | GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 356 | |
| 357 | /** |
| 358 | * Retrieves the last value set by setBlendConstant() |
| 359 | * @return the blending constant value |
| 360 | */ |
| 361 | GrColor getBlendConstant() const { return fBlendConstant; } |
| 362 | |
| 363 | /** |
| 364 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 365 | * coverage before the blend will give the correct final destination color. In general it |
| 366 | * will not as coverage is applied after blending. |
| 367 | */ |
| 368 | bool canTweakAlphaForCoverage() const; |
| 369 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 370 | /** |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 371 | * Sets the blending function coefficients. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 372 | * |
| 373 | * The blend function will be: |
| 374 | * D' = sat(S*srcCoef + D*dstCoef) |
| 375 | * |
| 376 | * where D is the existing destination color, S is the incoming source |
| 377 | * color, and D' is the new destination color that will be written. sat() |
| 378 | * is the saturation function. |
| 379 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 380 | * @param srcCoef coefficient applied to the src color. |
| 381 | * @param dstCoef coefficient applied to the dst color. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 382 | */ |
| 383 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 384 | if (srcCoeff != fSrcBlend || dstCoeff != fDstBlend) { |
| 385 | fSrcBlend = srcCoeff; |
| 386 | fDstBlend = dstCoeff; |
| 387 | this->invalidateOptState(); |
| 388 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 389 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 390 | if (GrBlendCoeffRefsDst(dstCoeff)) { |
| 391 | 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] | 392 | } |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 393 | if (GrBlendCoeffRefsSrc(srcCoeff)) { |
| 394 | 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] | 395 | } |
| 396 | #endif |
| 397 | } |
| 398 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 399 | /** |
| 400 | * Sets the blending function constant referenced by the following blending |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 401 | * coefficients: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 402 | * kConstC_GrBlendCoeff |
| 403 | * kIConstC_GrBlendCoeff |
| 404 | * kConstA_GrBlendCoeff |
| 405 | * kIConstA_GrBlendCoeff |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 406 | * |
| 407 | * @param constant the constant to set |
| 408 | */ |
egdaniel | 9514d24 | 2014-07-18 06:15:43 -0700 | [diff] [blame] | 409 | void setBlendConstant(GrColor constant) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 410 | if (constant != fBlendConstant) { |
| 411 | fBlendConstant = constant; |
| 412 | this->invalidateOptState(); |
| 413 | } |
egdaniel | 9514d24 | 2014-07-18 06:15:43 -0700 | [diff] [blame] | 414 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 415 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 416 | /// @} |
| 417 | |
| 418 | /////////////////////////////////////////////////////////////////////////// |
| 419 | /// @name View Matrix |
| 420 | //// |
| 421 | |
| 422 | /** |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 423 | * Retrieves the current view matrix |
| 424 | * @return the current view matrix. |
| 425 | */ |
| 426 | const SkMatrix& getViewMatrix() const { return fViewMatrix; } |
| 427 | |
| 428 | /** |
| 429 | * Retrieves the inverse of the current view matrix. |
| 430 | * |
| 431 | * If the current view matrix is invertible, return true, and if matrix |
| 432 | * is non-null, copy the inverse into it. If the current view matrix is |
| 433 | * non-invertible, return false and ignore the matrix parameter. |
| 434 | * |
| 435 | * @param matrix if not null, will receive a copy of the current inverse. |
| 436 | */ |
| 437 | bool getViewInverse(SkMatrix* matrix) const { |
| 438 | SkMatrix inverse; |
| 439 | if (fViewMatrix.invert(&inverse)) { |
| 440 | if (matrix) { |
| 441 | *matrix = inverse; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | /** |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 449 | * Sets the view matrix to identity and updates any installed effects to compensate for the |
| 450 | * coord system change. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 451 | */ |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 452 | bool setIdentityViewMatrix(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 453 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 454 | //////////////////////////////////////////////////////////////////////////// |
| 455 | |
| 456 | /** |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 457 | * 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] | 458 | * 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] | 459 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 460 | class AutoViewMatrixRestore : public ::SkNoncopyable { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 461 | public: |
| 462 | AutoViewMatrixRestore() : fDrawState(NULL) {} |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 463 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 464 | AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 465 | fDrawState = NULL; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 466 | this->set(ds, preconcatMatrix); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 467 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 468 | |
| 469 | ~AutoViewMatrixRestore() { this->restore(); } |
| 470 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 471 | /** |
| 472 | * Can be called prior to destructor to restore the original matrix. |
| 473 | */ |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 474 | void restore(); |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 475 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 476 | void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 477 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 478 | /** Sets the draw state's matrix to identity. This can fail because the current view matrix |
| 479 | is not invertible. */ |
| 480 | bool setIdentity(GrDrawState* drawState); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 481 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 482 | private: |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 483 | void doEffectCoordChanges(const SkMatrix& coordChangeMatrix); |
| 484 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 485 | GrDrawState* fDrawState; |
| 486 | SkMatrix fViewMatrix; |
| 487 | int fNumColorStages; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 488 | SkAutoSTArray<8, GrFragmentStage::SavedCoordChange> fSavedCoordChanges; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 491 | /// @} |
| 492 | |
| 493 | /////////////////////////////////////////////////////////////////////////// |
| 494 | /// @name Render Target |
| 495 | //// |
| 496 | |
| 497 | /** |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 498 | * Retrieves the currently set render-target. |
| 499 | * |
| 500 | * @return The currently set render target. |
| 501 | */ |
| 502 | GrRenderTarget* getRenderTarget() const { |
| 503 | return static_cast<GrRenderTarget*>(fRenderTarget.getResource()); |
| 504 | } |
| 505 | |
| 506 | /** |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 507 | * Sets the render-target used at the next drawing call |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 508 | * |
| 509 | * @param target The render target to set. |
| 510 | */ |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 511 | void setRenderTarget(GrRenderTarget* target) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 512 | fRenderTarget.set(SkSafeRef(target), kWrite_GrIOType); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 513 | this->invalidateOptState(); |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 514 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 515 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 516 | /// @} |
| 517 | |
| 518 | /////////////////////////////////////////////////////////////////////////// |
| 519 | /// @name Stencil |
| 520 | //// |
| 521 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 522 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 523 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 524 | /** |
| 525 | * Sets the stencil settings to use for the next draw. |
| 526 | * Changing the clip has the side-effect of possibly zeroing |
| 527 | * out the client settable stencil bits. So multipass algorithms |
| 528 | * using stencil should not change the clip between passes. |
| 529 | * @param settings the stencil settings to use. |
| 530 | */ |
| 531 | void setStencil(const GrStencilSettings& settings) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 532 | if (settings != fStencilSettings) { |
| 533 | fStencilSettings = settings; |
| 534 | this->invalidateOptState(); |
| 535 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Shortcut to disable stencil testing and ops. |
| 540 | */ |
| 541 | void disableStencil() { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 542 | if (!fStencilSettings.isDisabled()) { |
| 543 | fStencilSettings.setDisabled(); |
| 544 | this->invalidateOptState(); |
| 545 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 546 | } |
| 547 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 548 | GrStencilSettings* stencil() { return &fStencilSettings; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 549 | |
| 550 | /// @} |
| 551 | |
| 552 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 553 | /// @name State Flags |
| 554 | //// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 555 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 556 | /** |
| 557 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 558 | * default to disabled. |
| 559 | */ |
| 560 | enum StateBits { |
| 561 | /** |
| 562 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 563 | */ |
| 564 | kDither_StateBit = 0x01, |
| 565 | /** |
| 566 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 567 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 568 | * the 3D API. |
| 569 | */ |
| 570 | kHWAntialias_StateBit = 0x02, |
| 571 | /** |
| 572 | * Draws will respect the clip, otherwise the clip is ignored. |
| 573 | */ |
| 574 | kClip_StateBit = 0x04, |
| 575 | /** |
| 576 | * Disables writing to the color buffer. Useful when performing stencil |
| 577 | * operations. |
| 578 | */ |
| 579 | kNoColorWrites_StateBit = 0x08, |
| 580 | |
| 581 | /** |
| 582 | * Usually coverage is applied after color blending. The color is blended using the coeffs |
| 583 | * specified by setBlendFunc(). The blended color is then combined with dst using coeffs |
| 584 | * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In |
| 585 | * this case there is no distinction between coverage and color and the caller needs direct |
| 586 | * control over the blend coeffs. When set, there will be a single blend step controlled by |
| 587 | * setBlendFunc() which will use coverage*color as the src color. |
| 588 | */ |
| 589 | kCoverageDrawing_StateBit = 0x10, |
| 590 | |
| 591 | // Users of the class may add additional bits to the vector |
| 592 | kDummyStateBit, |
| 593 | kLastPublicStateBit = kDummyStateBit-1, |
| 594 | }; |
| 595 | |
| 596 | uint32_t getFlagBits() const { return fFlagBits; } |
| 597 | |
| 598 | bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); } |
| 599 | |
| 600 | bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } |
| 601 | bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); } |
| 602 | bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); } |
| 603 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 604 | void resetStateFlags() { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 605 | if (0 != fFlagBits) { |
| 606 | fFlagBits = 0; |
| 607 | this->invalidateOptState(); |
| 608 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Enable render state settings. |
| 613 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 614 | * @param stateBits bitfield of StateBits specifying the states to enable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 615 | */ |
| 616 | void enableState(uint32_t stateBits) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 617 | if (stateBits & ~fFlagBits) { |
| 618 | fFlagBits |= stateBits; |
| 619 | this->invalidateOptState(); |
| 620 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Disable render state settings. |
| 625 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 626 | * @param stateBits bitfield of StateBits specifying the states to disable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 627 | */ |
| 628 | void disableState(uint32_t stateBits) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 629 | if (stateBits & fFlagBits) { |
| 630 | fFlagBits &= ~(stateBits); |
| 631 | this->invalidateOptState(); |
| 632 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 633 | } |
| 634 | |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 635 | /** |
| 636 | * Enable or disable stateBits based on a boolean. |
| 637 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 638 | * @param stateBits bitfield of StateBits to enable or disable |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 639 | * @param enable if true enable stateBits, otherwise disable |
| 640 | */ |
| 641 | void setState(uint32_t stateBits, bool enable) { |
| 642 | if (enable) { |
| 643 | this->enableState(stateBits); |
| 644 | } else { |
| 645 | this->disableState(stateBits); |
| 646 | } |
| 647 | } |
| 648 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 649 | /// @} |
| 650 | |
| 651 | /////////////////////////////////////////////////////////////////////////// |
| 652 | /// @name Face Culling |
| 653 | //// |
| 654 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 655 | enum DrawFace { |
| 656 | kInvalid_DrawFace = -1, |
| 657 | |
| 658 | kBoth_DrawFace, |
| 659 | kCCW_DrawFace, |
| 660 | kCW_DrawFace, |
| 661 | }; |
| 662 | |
| 663 | /** |
| 664 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 665 | * or both faces. |
| 666 | * @return the current draw face(s). |
| 667 | */ |
| 668 | DrawFace getDrawFace() const { return fDrawFace; } |
| 669 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 670 | /** |
| 671 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 672 | * @param face the face(s) to draw. |
| 673 | */ |
| 674 | void setDrawFace(DrawFace face) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 675 | SkASSERT(kInvalid_DrawFace != face); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 676 | fDrawFace = face; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 677 | } |
| 678 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 679 | /// @} |
| 680 | |
| 681 | /////////////////////////////////////////////////////////////////////////// |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 682 | /// @name Hints |
| 683 | /// Hints that when provided can enable optimizations. |
| 684 | //// |
| 685 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 686 | enum Hints { |
| 687 | kVertexColorsAreOpaque_Hint = 0x1, |
| 688 | kLast_Hint = kVertexColorsAreOpaque_Hint |
| 689 | }; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 690 | |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 691 | void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); } |
| 692 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 693 | bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; } |
| 694 | |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 695 | /// @} |
| 696 | |
| 697 | /////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 698 | |
bsalomon | 838f62d | 2014-08-05 07:15:57 -0700 | [diff] [blame] | 699 | /** Return type for CombineIfPossible. */ |
| 700 | enum CombinedState { |
| 701 | /** The GrDrawStates cannot be combined. */ |
| 702 | kIncompatible_CombinedState, |
| 703 | /** Either draw state can be used in place of the other. */ |
| 704 | kAOrB_CombinedState, |
| 705 | /** Use the first draw state. */ |
| 706 | kA_CombinedState, |
| 707 | /** Use the second draw state. */ |
| 708 | kB_CombinedState, |
| 709 | }; |
| 710 | |
| 711 | /** This function determines whether the GrDrawStates used for two draws can be combined into |
| 712 | a single GrDrawState. This is used to avoid storing redundant GrDrawStates and to determine |
| 713 | if draws can be batched. The return value indicates whether combining is possible and, if |
| 714 | so, which of the two inputs should be used. */ |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 715 | static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawState& b, |
| 716 | const GrDrawTargetCaps& caps); |
bsalomon | 72336ed | 2014-08-05 07:35:56 -0700 | [diff] [blame] | 717 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 718 | GrDrawState& operator= (const GrDrawState& that); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 719 | |
| 720 | private: |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 721 | /** |
| 722 | * Converts refs on GrGpuResources owned directly or indirectly by this GrDrawState into |
| 723 | * pending reads and writes. This should be called when a GrDrawState is recorded into |
| 724 | * a GrDrawTarget for later execution. Subclasses of GrDrawState may add setters. However, |
| 725 | * once this call has been made the GrDrawState is immutable. It is also no longer copyable. |
| 726 | * In the future this conversion will automatically happen when converting a GrDrawState into |
| 727 | * an optimized draw state. |
| 728 | */ |
| 729 | void convertToPendingExec(); |
| 730 | |
| 731 | friend class GrDrawTarget; |
| 732 | |
| 733 | bool isEqual(const GrDrawState& that) const; |
| 734 | |
| 735 | /** |
| 736 | * Optimizations for blending / coverage to that can be applied based on the current state. |
| 737 | */ |
| 738 | enum BlendOptFlags { |
| 739 | /** |
| 740 | * No optimization |
| 741 | */ |
| 742 | kNone_BlendOpt = 0, |
| 743 | /** |
| 744 | * Don't draw at all |
| 745 | */ |
| 746 | kSkipDraw_BlendOptFlag = 0x1, |
| 747 | /** |
| 748 | * The coverage value does not have to be computed separately from alpha, the the output |
| 749 | * color can be the modulation of the two. |
| 750 | */ |
| 751 | kCoverageAsAlpha_BlendOptFlag = 0x2, |
| 752 | /** |
| 753 | * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are |
| 754 | * "don't cares". |
| 755 | */ |
| 756 | kEmitCoverage_BlendOptFlag = 0x4, |
| 757 | /** |
| 758 | * Emit transparent black instead of the src color, no need to compute coverage. |
| 759 | */ |
| 760 | kEmitTransBlack_BlendOptFlag = 0x8, |
| 761 | }; |
| 762 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 763 | |
| 764 | /** |
| 765 | * Determines what optimizations can be applied based on the blend. The coefficients may have |
| 766 | * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional |
| 767 | * params that receive the tweaked coefficients. Normally the function looks at the current |
| 768 | * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively |
| 769 | * determine the blend optimizations that would be used if there was partial pixel coverage. |
| 770 | * |
| 771 | * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for |
| 772 | * playback) must call this function and respect the flags that replace the output color. |
| 773 | * |
| 774 | * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will |
| 775 | * simply returned the cached flags and coefficients. Otherwise it will calculate the values. |
| 776 | */ |
| 777 | BlendOptFlags getBlendOpts(bool forceCoverage = false, |
| 778 | GrBlendCoeff* srcCoeff = NULL, |
| 779 | GrBlendCoeff* dstCoeff = NULL) const; |
| 780 | |
| 781 | /** |
| 782 | * Determines whether src alpha is guaranteed to be one for all src pixels |
| 783 | */ |
| 784 | bool srcAlphaWillBeOne() const; |
| 785 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 786 | void invalidateOptState() const; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 787 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 788 | void onReset(const SkMatrix* initialViewMatrix); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 789 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 790 | // Some of the auto restore objects assume that no effects are removed during their lifetime. |
| 791 | // This is used to assert that this condition holds. |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 792 | SkDEBUGCODE(int fBlockEffectRemovalCnt;) |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 793 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 794 | void internalSetVertexAttribs(const GrVertexAttrib attribs[], int count, size_t stride); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 795 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 796 | typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget; |
| 797 | // These fields are roughly sorted by decreasing likelihood of being different in op== |
| 798 | ProgramRenderTarget fRenderTarget; |
| 799 | GrColor fColor; |
| 800 | SkMatrix fViewMatrix; |
| 801 | GrColor fBlendConstant; |
| 802 | uint32_t fFlagBits; |
| 803 | const GrVertexAttrib* fVAPtr; |
| 804 | int fVACount; |
| 805 | size_t fVAStride; |
| 806 | GrStencilSettings fStencilSettings; |
| 807 | uint8_t fCoverage; |
| 808 | DrawFace fDrawFace; |
| 809 | GrBlendCoeff fSrcBlend; |
| 810 | GrBlendCoeff fDstBlend; |
| 811 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 812 | typedef SkSTArray<4, GrFragmentStage> FragmentStageArray; |
| 813 | typedef GrProgramElementRef<const GrGeometryProcessor> ProgramGeometryProcessor; |
| 814 | ProgramGeometryProcessor fGeometryProcessor; |
| 815 | FragmentStageArray fColorStages; |
| 816 | FragmentStageArray fCoverageStages; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 817 | |
| 818 | uint32_t fHints; |
| 819 | |
| 820 | // This is simply a different representation of info in fVertexAttribs and thus does |
| 821 | // not need to be compared in op==. |
| 822 | int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt]; |
| 823 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 824 | mutable GrOptDrawState* fCachedOptState; |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 825 | mutable uint32_t fCachedCapsID; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 826 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 827 | friend class GrOptDrawState; |
| 828 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 829 | typedef SkRefCnt INHERITED; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 832 | GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 833 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 834 | #endif |