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" |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 16 | #include "GrFragmentStage.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" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 20 | #include "GrXferProcessor.h" |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 21 | #include "SkMatrix.h" |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 22 | #include "effects/GrCoverageSetOpXP.h" |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 23 | #include "effects/GrDisableColorXP.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 24 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 25 | #include "effects/GrSimpleTextureEffect.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 26 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 27 | class GrDrawTargetCaps; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 28 | class GrPaint; |
| 29 | class GrTexture; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 30 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 31 | class GrDrawState { |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 32 | public: |
egdaniel | 69bb90c | 2014-11-11 07:32:45 -0800 | [diff] [blame] | 33 | GrDrawState() { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 34 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 35 | this->reset(); |
| 36 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Copies another draw state. |
| 40 | **/ |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 41 | GrDrawState(const GrDrawState& state) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 42 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 43 | *this = state; |
| 44 | } |
| 45 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 46 | virtual ~GrDrawState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 47 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 48 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 49 | * 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] | 50 | */ |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 51 | void reset() { this->onReset(); } |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 54 | * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that |
| 55 | * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 56 | * equivalents are set to default values with the exception of vertex attribute state which |
| 57 | * is unmodified by this function and clipping which will be enabled. |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 58 | */ |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 59 | void setFromPaint(const GrPaint&, GrRenderTarget*); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 60 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 61 | /// @} |
| 62 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 63 | /** |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 64 | * Depending on features available in the underlying 3D API and the color blend mode requested |
| 65 | * it may or may not be possible to correctly blend with fractional pixel coverage generated by |
| 66 | * the fragment shader. |
| 67 | * |
| 68 | * This function considers the current draw state and the draw target's capabilities to |
| 69 | * determine whether coverage can be handled correctly. This function assumes that the caller |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 70 | * intends to specify fractional pixel coverage via a primitive processor but may not have |
| 71 | * specified it yet. |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 72 | */ |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 73 | bool canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const; |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 74 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 75 | /** |
egdaniel | cd8b630 | 2014-11-11 14:46:05 -0800 | [diff] [blame] | 76 | * This function returns true if the render target destination pixel values will be read for |
| 77 | * blending during draw. |
| 78 | */ |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 79 | bool willBlendWithDst(const GrPrimitiveProcessor*) const; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 80 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 81 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 82 | /// @name Effect Stages |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the |
| 84 | /// 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] | 85 | /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color, |
| 86 | /// the fragment position, local coordinates). |
| 87 | /// |
| 88 | /// The stages are divided into two sets, color-computing and coverage-computing. The final |
| 89 | /// color stage produces the final pixel color. The coverage-computing stages function exactly |
| 90 | /// as the color-computing but the output of the final coverage stage is treated as a fractional |
| 91 | /// pixel coverage rather than as input to the src/dst color blend step. |
| 92 | /// |
| 93 | /// The input color to the first color-stage is either the constant color or interpolated |
| 94 | /// per-vertex colors. The input to the first coverage stage is either a constant coverage |
| 95 | /// (usually full-coverage) or interpolated per-vertex coverage. |
| 96 | /// |
| 97 | /// See the documentation of kCoverageDrawing_StateBit for information about disabling the |
| 98 | /// the color / coverage distinction. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 99 | //// |
| 100 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 101 | int numColorStages() const { return fColorStages.count(); } |
| 102 | int numCoverageStages() const { return fCoverageStages.count(); } |
joshualitt | 4dd9988 | 2014-11-11 08:51:30 -0800 | [diff] [blame] | 103 | int numFragmentStages() const { return this->numColorStages() + this->numCoverageStages(); } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 104 | |
| 105 | const GrXPFactory* getXPFactory() const { return fXPFactory.get(); } |
| 106 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 107 | const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; } |
| 108 | const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; } |
| 109 | |
| 110 | /** |
| 111 | * Checks whether any of the effects will read the dst pixel color. |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 112 | * TODO remove when we have an XP |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 113 | */ |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 114 | bool willEffectReadDstColor(const GrPrimitiveProcessor*) const; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 115 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 116 | /** |
| 117 | * The xfer processor factory. |
| 118 | */ |
| 119 | const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) { |
| 120 | fXPFactory.reset(SkRef(xpFactory)); |
| 121 | return xpFactory; |
| 122 | } |
| 123 | |
| 124 | void setPorterDuffXPFactory(SkXfermode::Mode mode) { |
| 125 | fXPFactory.reset(GrPorterDuffXPFactory::Create(mode)); |
| 126 | } |
| 127 | |
| 128 | void setPorterDuffXPFactory(GrBlendCoeff src, GrBlendCoeff dst) { |
| 129 | fXPFactory.reset(GrPorterDuffXPFactory::Create(src, dst)); |
| 130 | } |
| 131 | |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 132 | void setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage = false) { |
| 133 | fXPFactory.reset(GrCoverageSetOpXPFactory::Create(regionOp, invertCoverage)); |
| 134 | } |
| 135 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 136 | void setDisableColorXPFactory() { |
| 137 | fXPFactory.reset(GrDisableColorXPFactory::Create()); |
| 138 | } |
| 139 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 140 | const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 141 | SkASSERT(effect); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 142 | SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect)); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 143 | fColorProcInfoValid = false; |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 144 | return effect; |
| 145 | } |
skia.committer@gmail.com | 01c34ee | 2013-03-20 07:01:02 +0000 | [diff] [blame] | 146 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 147 | const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 148 | SkASSERT(effect); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 149 | SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect)); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 150 | fCoverageProcInfoValid = false; |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 151 | return effect; |
| 152 | } |
| 153 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 154 | /** |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 155 | * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates. |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 156 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 157 | void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { |
| 158 | this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref(); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 159 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 160 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 161 | void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { |
| 162 | this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 163 | } |
| 164 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 165 | void addColorTextureProcessor(GrTexture* texture, |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 166 | const SkMatrix& matrix, |
| 167 | const GrTextureParams& params) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 168 | this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref(); |
| 169 | } |
| 170 | |
| 171 | void addCoverageTextureProcessor(GrTexture* texture, |
| 172 | const SkMatrix& matrix, |
| 173 | const GrTextureParams& params) { |
| 174 | this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 175 | } |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 176 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 177 | /** |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 178 | * When this object is destroyed it will remove any color/coverage effects from the draw state |
| 179 | * that were added after its constructor. |
| 180 | * |
| 181 | * This class has strange behavior around geometry processor. If there is a GP on the draw state |
| 182 | * it will assert that the GP is not modified until after the destructor of the ARE. If the |
| 183 | * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the |
| 184 | * destructor. |
| 185 | * |
| 186 | * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add |
| 187 | * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState |
| 188 | * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred |
| 189 | * execution state and GrOptDrawState always will be (and will be immutable and therefore |
| 190 | * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore |
| 191 | * the GP. |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 192 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 193 | class AutoRestoreEffects : public ::SkNoncopyable { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 194 | public: |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 195 | AutoRestoreEffects() |
| 196 | : fDrawState(NULL) |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 197 | , fColorEffectCnt(0) |
| 198 | , fCoverageEffectCnt(0) {} |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 199 | |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 200 | AutoRestoreEffects(GrDrawState* ds) |
| 201 | : fDrawState(NULL) |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 202 | , fColorEffectCnt(0) |
| 203 | , fCoverageEffectCnt(0) { |
skia.committer@gmail.com | 5c493d5 | 2013-06-14 07:00:49 +0000 | [diff] [blame] | 204 | this->set(ds); |
robertphillips@google.com | f09b87d | 2013-06-13 20:06:44 +0000 | [diff] [blame] | 205 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 206 | |
| 207 | ~AutoRestoreEffects() { this->set(NULL); } |
| 208 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 209 | void set(GrDrawState* ds); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 210 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 211 | bool isSet() const { return SkToBool(fDrawState); } |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 212 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 213 | private: |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 214 | GrDrawState* fDrawState; |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 215 | int fColorEffectCnt; |
| 216 | int fCoverageEffectCnt; |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 219 | /** |
| 220 | * AutoRestoreStencil |
| 221 | * |
| 222 | * This simple struct saves and restores the stencil settings |
| 223 | */ |
| 224 | class AutoRestoreStencil : public ::SkNoncopyable { |
| 225 | public: |
| 226 | AutoRestoreStencil() : fDrawState(NULL) {} |
| 227 | |
| 228 | AutoRestoreStencil(GrDrawState* ds) : fDrawState(NULL) { this->set(ds); } |
| 229 | |
| 230 | ~AutoRestoreStencil() { this->set(NULL); } |
| 231 | |
| 232 | void set(GrDrawState* ds) { |
| 233 | if (fDrawState) { |
| 234 | fDrawState->setStencil(fStencilSettings); |
| 235 | } |
| 236 | fDrawState = ds; |
| 237 | if (ds) { |
| 238 | fStencilSettings = ds->getStencil(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool isSet() const { return SkToBool(fDrawState); } |
| 243 | |
| 244 | private: |
| 245 | GrDrawState* fDrawState; |
| 246 | GrStencilSettings fStencilSettings; |
| 247 | }; |
| 248 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 249 | /// @} |
| 250 | |
| 251 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 252 | /// @name Blending |
| 253 | //// |
| 254 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 257 | * coverage before the blend will give the correct final destination color. In general it |
| 258 | * will not as coverage is applied after blending. |
| 259 | */ |
| 260 | bool canTweakAlphaForCoverage() const; |
| 261 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 262 | /// @} |
| 263 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 264 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 265 | /// @} |
| 266 | |
| 267 | /////////////////////////////////////////////////////////////////////////// |
| 268 | /// @name Render Target |
| 269 | //// |
| 270 | |
| 271 | /** |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 272 | * Retrieves the currently set render-target. |
| 273 | * |
| 274 | * @return The currently set render target. |
| 275 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 276 | GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 277 | |
| 278 | /** |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 279 | * Sets the render-target used at the next drawing call |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 280 | * |
| 281 | * @param target The render target to set. |
| 282 | */ |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 283 | void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef(target)); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 284 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 285 | /// @} |
| 286 | |
| 287 | /////////////////////////////////////////////////////////////////////////// |
| 288 | /// @name Stencil |
| 289 | //// |
| 290 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 291 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 292 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 293 | /** |
| 294 | * Sets the stencil settings to use for the next draw. |
| 295 | * Changing the clip has the side-effect of possibly zeroing |
| 296 | * out the client settable stencil bits. So multipass algorithms |
| 297 | * using stencil should not change the clip between passes. |
| 298 | * @param settings the stencil settings to use. |
| 299 | */ |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 300 | void setStencil(const GrStencilSettings& settings) { fStencilSettings = settings; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Shortcut to disable stencil testing and ops. |
| 304 | */ |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 305 | void disableStencil() { fStencilSettings.setDisabled(); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 306 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 307 | GrStencilSettings* stencil() { return &fStencilSettings; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 308 | |
| 309 | /// @} |
| 310 | |
| 311 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 312 | /// @name State Flags |
| 313 | //// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 314 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 315 | /** |
| 316 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 317 | * default to disabled. |
| 318 | */ |
| 319 | enum StateBits { |
| 320 | /** |
| 321 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 322 | */ |
| 323 | kDither_StateBit = 0x01, |
| 324 | /** |
| 325 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 326 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 327 | * the 3D API. |
| 328 | */ |
| 329 | kHWAntialias_StateBit = 0x02, |
| 330 | /** |
| 331 | * Draws will respect the clip, otherwise the clip is ignored. |
| 332 | */ |
| 333 | kClip_StateBit = 0x04, |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 334 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 335 | kLast_StateBit = kClip_StateBit, |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 338 | bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 339 | bool isDither() const { return 0 != (fFlagBits & kDither_StateBit); } |
| 340 | bool isHWAntialias() const { return 0 != (fFlagBits & kHWAntialias_StateBit); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Enable render state settings. |
| 344 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 345 | * @param stateBits bitfield of StateBits specifying the states to enable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 346 | */ |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 347 | void enableState(uint32_t stateBits) { fFlagBits |= stateBits; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 348 | |
| 349 | /** |
| 350 | * Disable render state settings. |
| 351 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 352 | * @param stateBits bitfield of StateBits specifying the states to disable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 353 | */ |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 354 | void disableState(uint32_t stateBits) { fFlagBits &= ~(stateBits); } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 355 | |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 356 | /** |
| 357 | * Enable or disable stateBits based on a boolean. |
| 358 | * |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 359 | * @param stateBits bitfield of StateBits to enable or disable |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 360 | * @param enable if true enable stateBits, otherwise disable |
| 361 | */ |
| 362 | void setState(uint32_t stateBits, bool enable) { |
| 363 | if (enable) { |
| 364 | this->enableState(stateBits); |
| 365 | } else { |
| 366 | this->disableState(stateBits); |
| 367 | } |
| 368 | } |
| 369 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 370 | /// @} |
| 371 | |
| 372 | /////////////////////////////////////////////////////////////////////////// |
| 373 | /// @name Face Culling |
| 374 | //// |
| 375 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 376 | enum DrawFace { |
| 377 | kInvalid_DrawFace = -1, |
| 378 | |
| 379 | kBoth_DrawFace, |
| 380 | kCCW_DrawFace, |
| 381 | kCW_DrawFace, |
| 382 | }; |
| 383 | |
| 384 | /** |
| 385 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 386 | * or both faces. |
| 387 | * @return the current draw face(s). |
| 388 | */ |
| 389 | DrawFace getDrawFace() const { return fDrawFace; } |
| 390 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 391 | /** |
| 392 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 393 | * @param face the face(s) to draw. |
| 394 | */ |
| 395 | void setDrawFace(DrawFace face) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 396 | SkASSERT(kInvalid_DrawFace != face); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 397 | fDrawFace = face; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 398 | } |
| 399 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 400 | /// @} |
| 401 | |
| 402 | /////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 403 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 404 | GrDrawState& operator= (const GrDrawState& that); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 405 | |
| 406 | private: |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 407 | bool isEqual(const GrDrawState& that, bool explicitLocalCoords) const; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 408 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 409 | const GrProcOptInfo& colorProcInfo(const GrPrimitiveProcessor* pp) const { |
| 410 | this->calcColorInvariantOutput(pp); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 411 | return fColorProcInfo; |
| 412 | } |
| 413 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 414 | const GrProcOptInfo& coverageProcInfo(const GrPrimitiveProcessor* pp) const { |
| 415 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 416 | return fCoverageProcInfo; |
| 417 | } |
| 418 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 419 | /** |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 420 | * If fColorProcInfoValid is false, function calculates the invariant output for the color |
| 421 | * stages and results are stored in fColorProcInfo. |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 422 | */ |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 423 | void calcColorInvariantOutput(const GrPrimitiveProcessor*) const; |
| 424 | |
| 425 | /** |
| 426 | * If fCoverageProcInfoValid is false, function calculates the invariant output for the coverage |
| 427 | * stages and results are stored in fCoverageProcInfo. |
| 428 | */ |
| 429 | void calcCoverageInvariantOutput(const GrPrimitiveProcessor*) const; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 430 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 431 | /** |
| 432 | * If fColorProcInfoValid is false, function calculates the invariant output for the color |
| 433 | * stages and results are stored in fColorProcInfo. |
| 434 | */ |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 435 | void calcColorInvariantOutput(GrColor) const; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * If fCoverageProcInfoValid is false, function calculates the invariant output for the coverage |
| 439 | * stages and results are stored in fCoverageProcInfo. |
| 440 | */ |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 441 | void calcCoverageInvariantOutput(GrColor) const; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 442 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 443 | void onReset(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 444 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 445 | // Some of the auto restore objects assume that no effects are removed during their lifetime. |
| 446 | // This is used to assert that this condition holds. |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 447 | SkDEBUGCODE(int fBlockEffectRemovalCnt;) |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 448 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 449 | typedef SkSTArray<4, GrFragmentStage> FragmentStageArray; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 450 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 451 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 452 | uint32_t fFlagBits; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 453 | GrStencilSettings fStencilSettings; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 454 | DrawFace fDrawFace; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 455 | SkAutoTUnref<const GrXPFactory> fXPFactory; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 456 | FragmentStageArray fColorStages; |
| 457 | FragmentStageArray fCoverageStages; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 458 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 459 | mutable GrProcOptInfo fColorProcInfo; |
| 460 | mutable GrProcOptInfo fCoverageProcInfo; |
| 461 | mutable bool fColorProcInfoValid; |
| 462 | mutable bool fCoverageProcInfoValid; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 463 | mutable GrColor fColorCache; |
| 464 | mutable GrColor fCoverageCache; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 465 | mutable const GrPrimitiveProcessor* fColorPrimProc; |
| 466 | mutable const GrPrimitiveProcessor* fCoveragePrimProc; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 467 | |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 468 | friend class GrOptDrawState; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | #endif |