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