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