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