blob: a236d1a55986b3aa717f24a272c7d069f28660c7 [file] [log] [blame]
tomhudson@google.com93813632011-10-27 20:21:16 +00001/*
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
egdaniel89af44a2014-09-26 06:15:04 -070011
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000012#include "GrBlend.h"
egdanielc0648242014-09-22 13:17:02 -070013#include "GrDrawTargetCaps.h"
bsalomon6251d172014-10-15 10:50:36 -070014#include "GrGeometryProcessor.h"
bsalomonf96ba022014-09-17 08:05:40 -070015#include "GrGpuResourceRef.h"
bsalomonae59b772014-11-19 08:23:49 -080016#include "GrFragmentStage.h"
egdanielb6cbc382014-11-13 11:00:34 -080017#include "GrProcOptInfo.h"
egdaniel89af44a2014-09-26 06:15:04 -070018#include "GrRenderTarget.h"
19#include "GrStencil.h"
20#include "SkMatrix.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000021#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000022
egdaniel89af44a2014-09-26 06:15:04 -070023class GrDrawTargetCaps;
egdaniel89af44a2014-09-26 06:15:04 -070024class GrPaint;
25class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070026
joshualitt9853cce2014-11-17 14:22:48 -080027class GrDrawState {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000028public:
egdaniel69bb90c2014-11-11 07:32:45 -080029 GrDrawState() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000030 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000031 this->reset();
32 }
tomhudson@google.com93813632011-10-27 20:21:16 +000033
egdaniel69bb90c2014-11-11 07:32:45 -080034 GrDrawState(const SkMatrix& initialViewMatrix) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000035 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000036 this->reset(initialViewMatrix);
37 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000038
39 /**
40 * Copies another draw state.
41 **/
joshualitt9853cce2014-11-17 14:22:48 -080042 GrDrawState(const GrDrawState& state) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000043 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000044 *this = state;
45 }
46
bsalomon@google.com137f1342013-05-29 21:27:53 +000047 /**
48 * Copies another draw state with a preconcat to the view matrix.
49 **/
bsalomon8f727332014-08-05 07:50:06 -070050 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000051
egdaniel170f90b2014-09-16 12:54:40 -070052 virtual ~GrDrawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +000053
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000054 /**
joshualittb0a8a372014-09-23 09:50:21 -070055 * Resets to the default state. GrProcessors will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000056 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000057 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000058
bsalomon@google.com137f1342013-05-29 21:27:53 +000059 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000060
61 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000062 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
63 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070064 * equivalents are set to default values with the exception of vertex attribute state which
65 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000066 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000067 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000068
69 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000070 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000071 ////
72
joshualitt2dd1ae02014-12-03 06:24:10 -080073 // TODO when we move this info off of GrGeometryProcessor, delete these
egdaniel89af44a2014-09-26 06:15:04 -070074 bool hasLocalCoordAttribute() const {
joshualitt2dd1ae02014-12-03 06:24:10 -080075 return this->hasGeometryProcessor() && this->getGeometryProcessor()->hasLocalCoords();
egdaniel89af44a2014-09-26 06:15:04 -070076 }
77 bool hasColorVertexAttribute() const {
joshualitt2dd1ae02014-12-03 06:24:10 -080078 return this->hasGeometryProcessor() && this->getGeometryProcessor()->hasVertexColor();
egdaniel89af44a2014-09-26 06:15:04 -070079 }
80 bool hasCoverageVertexAttribute() const {
joshualitt2dd1ae02014-12-03 06:24:10 -080081 return this->hasGeometryProcessor() && this->getGeometryProcessor()->hasVertexCoverage();
egdaniel89af44a2014-09-26 06:15:04 -070082 }
83
jvanverth@google.com9b855c72013-03-01 18:21:22 +000084 /// @}
85
jvanverth@google.com9b855c72013-03-01 18:21:22 +000086 /**
bsalomon62c447d2014-08-08 08:08:50 -070087 * Depending on features available in the underlying 3D API and the color blend mode requested
88 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
89 * the fragment shader.
90 *
91 * This function considers the current draw state and the draw target's capabilities to
92 * determine whether coverage can be handled correctly. This function assumes that the caller
93 * intends to specify fractional pixel coverage (via setCoverage(), through a coverage vertex
94 * attribute, or a coverage effect) but may not have specified it yet.
95 */
96 bool couldApplyCoverage(const GrDrawTargetCaps& caps) const;
97
egdaniel89af44a2014-09-26 06:15:04 -070098 /**
99 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
100 */
101 bool hasSolidCoverage() const;
102
egdanielcd8b6302014-11-11 14:46:05 -0800103 /**
104 * This function returns true if the render target destination pixel values will be read for
105 * blending during draw.
106 */
107 bool willBlendWithDst() const;
108
jvanverth@google.comcc782382013-01-28 20:39:48 +0000109 /// @}
110
111 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000112 /// @name Color
113 ////
114
egdaniel89af44a2014-09-26 06:15:04 -0700115 GrColor getColor() const { return fColor; }
116
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000117 /**
118 * Sets color for next draw to a premultiplied-alpha color.
119 *
120 * @param color the color to set.
121 */
egdaniel9514d242014-07-18 06:15:43 -0700122 void setColor(GrColor color) {
egdaniel3658f382014-09-15 07:01:59 -0700123 if (color != fColor) {
124 fColor = color;
egdanielb6cbc382014-11-13 11:00:34 -0800125 fColorProcInfoValid = false;
egdaniel3658f382014-09-15 07:01:59 -0700126 }
egdaniel9514d242014-07-18 06:15:43 -0700127 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000128
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000129 /**
130 * Sets the color to be used for the next draw to be
131 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
132 *
133 * @param alpha The alpha value to set as the color.
134 */
bsalomon62c447d2014-08-08 08:08:50 -0700135 void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) | a); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000136
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000137 /// @}
138
139 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000140 /// @name Coverage
141 ////
142
egdaniel89af44a2014-09-26 06:15:04 -0700143 uint8_t getCoverage() const { return fCoverage; }
144
145 GrColor getCoverageColor() const {
146 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
147 }
148
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000149 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000151 * initial value (after construction or reset()) is 0xff. The constant
152 * coverage is ignored when per-vertex coverage is provided.
153 */
154 void setCoverage(uint8_t coverage) {
egdaniel3658f382014-09-15 07:01:59 -0700155 if (coverage != fCoverage) {
156 fCoverage = coverage;
egdanielb6cbc382014-11-13 11:00:34 -0800157 fCoverageProcInfoValid = false;
egdaniel3658f382014-09-15 07:01:59 -0700158 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000159 }
160
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000161 /// @}
162
joshualittbd769d02014-09-04 08:56:46 -0700163 /**
164 * The geometry processor is the sole element of the skia pipeline which can use the vertex,
165 * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader
166 * but is never put in the color processing pipeline.
167 */
168
joshualittb0a8a372014-09-23 09:50:21 -0700169 const GrGeometryProcessor* setGeometryProcessor(const GrGeometryProcessor* geometryProcessor) {
170 SkASSERT(geometryProcessor);
joshualittbd769d02014-09-04 08:56:46 -0700171 SkASSERT(!this->hasGeometryProcessor());
joshualitta5305a12014-10-10 17:47:00 -0700172 fGeometryProcessor.reset(SkRef(geometryProcessor));
egdanielb6cbc382014-11-13 11:00:34 -0800173 fCoverageProcInfoValid = false;
joshualittb0a8a372014-09-23 09:50:21 -0700174 return geometryProcessor;
joshualittbd769d02014-09-04 08:56:46 -0700175 }
176
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000177 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000178 /// @name Effect Stages
joshualittb0a8a372014-09-23 09:50:21 -0700179 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
180 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000181 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
182 /// the fragment position, local coordinates).
183 ///
184 /// The stages are divided into two sets, color-computing and coverage-computing. The final
185 /// color stage produces the final pixel color. The coverage-computing stages function exactly
186 /// as the color-computing but the output of the final coverage stage is treated as a fractional
187 /// pixel coverage rather than as input to the src/dst color blend step.
188 ///
189 /// The input color to the first color-stage is either the constant color or interpolated
190 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
191 /// (usually full-coverage) or interpolated per-vertex coverage.
192 ///
193 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
194 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000195 ////
196
egdaniel89af44a2014-09-26 06:15:04 -0700197 int numColorStages() const { return fColorStages.count(); }
198 int numCoverageStages() const { return fCoverageStages.count(); }
joshualitt4dd99882014-11-11 08:51:30 -0800199 int numFragmentStages() const { return this->numColorStages() + this->numCoverageStages(); }
egdaniel89af44a2014-09-26 06:15:04 -0700200 int numTotalStages() const {
joshualitt4dd99882014-11-11 08:51:30 -0800201 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 : 0);
egdaniel89af44a2014-09-26 06:15:04 -0700202 }
203
204 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualitta5305a12014-10-10 17:47:00 -0700205 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700206 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; }
207 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; }
208
209 /**
210 * Checks whether any of the effects will read the dst pixel color.
211 */
212 bool willEffectReadDstColor() const;
213
joshualittb0a8a372014-09-23 09:50:21 -0700214 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700215 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700216 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect));
egdanielb6cbc382014-11-13 11:00:34 -0800217 fColorProcInfoValid = false;
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000218 return effect;
219 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000220
joshualittb0a8a372014-09-23 09:50:21 -0700221 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700222 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700223 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect));
egdanielb6cbc382014-11-13 11:00:34 -0800224 fCoverageProcInfoValid = false;
bsalomon@google.comadc65362013-01-28 14:26:09 +0000225 return effect;
226 }
227
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000228 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000229 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000230 */
joshualittb0a8a372014-09-23 09:50:21 -0700231 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
232 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000233 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000234
joshualittb0a8a372014-09-23 09:50:21 -0700235 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
236 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000237 }
238
joshualittb0a8a372014-09-23 09:50:21 -0700239 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000240 const SkMatrix& matrix,
241 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -0700242 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
243 }
244
245 void addCoverageTextureProcessor(GrTexture* texture,
246 const SkMatrix& matrix,
247 const GrTextureParams& params) {
248 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000249 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000250
robertphillips@google.com972265d2012-06-13 18:49:30 +0000251 /**
bsalomon9b536522014-09-05 09:18:51 -0700252 * When this object is destroyed it will remove any color/coverage effects from the draw state
253 * that were added after its constructor.
254 *
255 * This class has strange behavior around geometry processor. If there is a GP on the draw state
256 * it will assert that the GP is not modified until after the destructor of the ARE. If the
257 * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the
258 * destructor.
259 *
260 * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add
261 * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState
262 * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred
263 * execution state and GrOptDrawState always will be (and will be immutable and therefore
264 * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore
265 * the GP.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000266 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000267 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000268 public:
bsalomon9b536522014-09-05 09:18:51 -0700269 AutoRestoreEffects()
270 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700271 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700272 , fColorEffectCnt(0)
273 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000274
bsalomon9b536522014-09-05 09:18:51 -0700275 AutoRestoreEffects(GrDrawState* ds)
276 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700277 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700278 , fColorEffectCnt(0)
279 , fCoverageEffectCnt(0) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000280 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000281 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000282
283 ~AutoRestoreEffects() { this->set(NULL); }
284
bsalomon8f727332014-08-05 07:50:06 -0700285 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000286
bsalomon49f085d2014-09-05 13:34:00 -0700287 bool isSet() const { return SkToBool(fDrawState); }
bsalomon8af05232014-06-03 06:34:58 -0700288
robertphillips@google.com972265d2012-06-13 18:49:30 +0000289 private:
bsalomon9b536522014-09-05 09:18:51 -0700290 GrDrawState* fDrawState;
bsalomon52e9d632014-09-05 12:23:12 -0700291 uint32_t fOriginalGPID;
bsalomon9b536522014-09-05 09:18:51 -0700292 int fColorEffectCnt;
293 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000294 };
295
joshualitta58fe352014-10-27 08:39:00 -0700296 /**
297 * AutoRestoreStencil
298 *
299 * This simple struct saves and restores the stencil settings
300 */
301 class AutoRestoreStencil : public ::SkNoncopyable {
302 public:
303 AutoRestoreStencil() : fDrawState(NULL) {}
304
305 AutoRestoreStencil(GrDrawState* ds) : fDrawState(NULL) { this->set(ds); }
306
307 ~AutoRestoreStencil() { this->set(NULL); }
308
309 void set(GrDrawState* ds) {
310 if (fDrawState) {
311 fDrawState->setStencil(fStencilSettings);
312 }
313 fDrawState = ds;
314 if (ds) {
315 fStencilSettings = ds->getStencil();
316 }
317 }
318
319 bool isSet() const { return SkToBool(fDrawState); }
320
321 private:
322 GrDrawState* fDrawState;
323 GrStencilSettings fStencilSettings;
324 };
325
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000326 /// @}
327
328 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000329 /// @name Blending
330 ////
331
egdaniel89af44a2014-09-26 06:15:04 -0700332 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
333 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
334
335 /**
336 * Retrieves the last value set by setBlendConstant()
337 * @return the blending constant value
338 */
339 GrColor getBlendConstant() const { return fBlendConstant; }
340
341 /**
342 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
343 * coverage before the blend will give the correct final destination color. In general it
344 * will not as coverage is applied after blending.
345 */
346 bool canTweakAlphaForCoverage() const;
347
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000348 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000349 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000350 *
351 * The blend function will be:
352 * D' = sat(S*srcCoef + D*dstCoef)
353 *
354 * where D is the existing destination color, S is the incoming source
355 * color, and D' is the new destination color that will be written. sat()
356 * is the saturation function.
357 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000358 * @param srcCoef coefficient applied to the src color.
359 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000360 */
361 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon04ddf892014-11-19 12:36:22 -0800362 fSrcBlend = srcCoeff;
363 fDstBlend = dstCoeff;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000364 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000365 if (GrBlendCoeffRefsDst(dstCoeff)) {
tfarina38406c82014-10-31 07:11:12 -0700366 SkDebugf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000367 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000368 if (GrBlendCoeffRefsSrc(srcCoeff)) {
tfarina38406c82014-10-31 07:11:12 -0700369 SkDebugf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000370 }
371 #endif
372 }
373
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000374 /**
375 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000376 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000377 * kConstC_GrBlendCoeff
378 * kIConstC_GrBlendCoeff
379 * kConstA_GrBlendCoeff
380 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000381 *
382 * @param constant the constant to set
383 */
bsalomon04ddf892014-11-19 12:36:22 -0800384 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000385
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000386 /// @}
387
388 ///////////////////////////////////////////////////////////////////////////
389 /// @name View Matrix
390 ////
391
392 /**
egdaniel89af44a2014-09-26 06:15:04 -0700393 * Retrieves the current view matrix
394 * @return the current view matrix.
395 */
396 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
397
398 /**
399 * Retrieves the inverse of the current view matrix.
400 *
401 * If the current view matrix is invertible, return true, and if matrix
402 * is non-null, copy the inverse into it. If the current view matrix is
403 * non-invertible, return false and ignore the matrix parameter.
404 *
405 * @param matrix if not null, will receive a copy of the current inverse.
406 */
407 bool getViewInverse(SkMatrix* matrix) const {
408 SkMatrix inverse;
409 if (fViewMatrix.invert(&inverse)) {
410 if (matrix) {
411 *matrix = inverse;
412 }
413 return true;
414 }
415 return false;
416 }
417
418 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000419 * Sets the view matrix to identity and updates any installed effects to compensate for the
420 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000421 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000422 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000423
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000424 ////////////////////////////////////////////////////////////////////////////
425
426 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000427 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000428 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000429 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000430 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000431 public:
432 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000433
bsalomon@google.comc7818882013-03-20 19:19:53 +0000434 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000435 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000436 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000438
439 ~AutoViewMatrixRestore() { this->restore(); }
440
bsalomon@google.coma8347462012-10-08 18:59:39 +0000441 /**
442 * Can be called prior to destructor to restore the original matrix.
443 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000444 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000445
bsalomon@google.comc7818882013-03-20 19:19:53 +0000446 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000447
bsalomon@google.com137f1342013-05-29 21:27:53 +0000448 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
449 is not invertible. */
450 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000451
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000452 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000453 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
454
joshualittb0a8a372014-09-23 09:50:21 -0700455 GrDrawState* fDrawState;
456 SkMatrix fViewMatrix;
457 int fNumColorStages;
joshualitta5305a12014-10-10 17:47:00 -0700458 SkAutoSTArray<8, GrFragmentStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000459 };
460
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000461 /// @}
462
463 ///////////////////////////////////////////////////////////////////////////
464 /// @name Render Target
465 ////
466
467 /**
egdaniel89af44a2014-09-26 06:15:04 -0700468 * Retrieves the currently set render-target.
469 *
470 * @return The currently set render target.
471 */
bsalomon37dd3312014-11-03 08:47:23 -0800472 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700473
474 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000475 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000476 *
477 * @param target The render target to set.
478 */
bsalomonae59b772014-11-19 08:23:49 -0800479 void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef(target)); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000480
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 /// @}
482
483 ///////////////////////////////////////////////////////////////////////////
484 /// @name Stencil
485 ////
486
egdaniel89af44a2014-09-26 06:15:04 -0700487 const GrStencilSettings& getStencil() const { return fStencilSettings; }
488
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000489 /**
490 * Sets the stencil settings to use for the next draw.
491 * Changing the clip has the side-effect of possibly zeroing
492 * out the client settable stencil bits. So multipass algorithms
493 * using stencil should not change the clip between passes.
494 * @param settings the stencil settings to use.
495 */
bsalomon04ddf892014-11-19 12:36:22 -0800496 void setStencil(const GrStencilSettings& settings) { fStencilSettings = settings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000497
498 /**
499 * Shortcut to disable stencil testing and ops.
500 */
bsalomon04ddf892014-11-19 12:36:22 -0800501 void disableStencil() { fStencilSettings.setDisabled(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502
bsalomon2ed5ef82014-07-07 08:44:05 -0700503 GrStencilSettings* stencil() { return &fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000504
505 /// @}
506
507 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000508 /// @name State Flags
509 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000510
egdaniel89af44a2014-09-26 06:15:04 -0700511 /**
512 * Flags that affect rendering. Controlled using enable/disableState(). All
513 * default to disabled.
514 */
515 enum StateBits {
516 /**
517 * Perform dithering. TODO: Re-evaluate whether we need this bit
518 */
519 kDither_StateBit = 0x01,
520 /**
521 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
522 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
523 * the 3D API.
524 */
525 kHWAntialias_StateBit = 0x02,
526 /**
527 * Draws will respect the clip, otherwise the clip is ignored.
528 */
529 kClip_StateBit = 0x04,
530 /**
531 * Disables writing to the color buffer. Useful when performing stencil
532 * operations.
533 */
534 kNoColorWrites_StateBit = 0x08,
535
536 /**
537 * Usually coverage is applied after color blending. The color is blended using the coeffs
538 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
539 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
540 * this case there is no distinction between coverage and color and the caller needs direct
541 * control over the blend coeffs. When set, there will be a single blend step controlled by
542 * setBlendFunc() which will use coverage*color as the src color.
543 */
544 kCoverageDrawing_StateBit = 0x10,
joshualitt7a6184f2014-10-29 18:29:27 -0700545 kLast_StateBit = kCoverageDrawing_StateBit,
egdaniel89af44a2014-09-26 06:15:04 -0700546 };
547
egdaniel89af44a2014-09-26 06:15:04 -0700548 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
549 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
550 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
bsalomon04ddf892014-11-19 12:36:22 -0800551 bool isDither() const { return 0 != (fFlagBits & kDither_StateBit); }
552 bool isHWAntialias() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000553
554 /**
555 * Enable render state settings.
556 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000557 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000558 */
bsalomon04ddf892014-11-19 12:36:22 -0800559 void enableState(uint32_t stateBits) { fFlagBits |= stateBits; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000560
561 /**
562 * Disable render state settings.
563 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000564 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000565 */
bsalomon04ddf892014-11-19 12:36:22 -0800566 void disableState(uint32_t stateBits) { fFlagBits &= ~(stateBits); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000567
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000568 /**
569 * Enable or disable stateBits based on a boolean.
570 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000571 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000572 * @param enable if true enable stateBits, otherwise disable
573 */
574 void setState(uint32_t stateBits, bool enable) {
575 if (enable) {
576 this->enableState(stateBits);
577 } else {
578 this->disableState(stateBits);
579 }
580 }
581
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000582 /// @}
583
584 ///////////////////////////////////////////////////////////////////////////
585 /// @name Face Culling
586 ////
587
egdaniel89af44a2014-09-26 06:15:04 -0700588 enum DrawFace {
589 kInvalid_DrawFace = -1,
590
591 kBoth_DrawFace,
592 kCCW_DrawFace,
593 kCW_DrawFace,
594 };
595
596 /**
597 * Gets whether the target is drawing clockwise, counterclockwise,
598 * or both faces.
599 * @return the current draw face(s).
600 */
601 DrawFace getDrawFace() const { return fDrawFace; }
602
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 /**
604 * Controls whether clockwise, counterclockwise, or both faces are drawn.
605 * @param face the face(s) to draw.
606 */
607 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000608 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700609 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 }
611
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000612 /// @}
613
614 ///////////////////////////////////////////////////////////////////////////
bsalomon62c447d2014-08-08 08:08:50 -0700615 /// @name Hints
616 /// Hints that when provided can enable optimizations.
617 ////
618
joshualitt65171342014-10-09 07:25:36 -0700619 enum Hints {
620 kVertexColorsAreOpaque_Hint = 0x1,
621 kLast_Hint = kVertexColorsAreOpaque_Hint
622 };
egdaniel89af44a2014-09-26 06:15:04 -0700623
bsalomon62c447d2014-08-08 08:08:50 -0700624 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); }
625
egdaniel89af44a2014-09-26 06:15:04 -0700626 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
627
bsalomon62c447d2014-08-08 08:08:50 -0700628 /// @}
629
630 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000631
bsalomon8f727332014-08-05 07:50:06 -0700632 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000633
634private:
egdaniel89af44a2014-09-26 06:15:04 -0700635 bool isEqual(const GrDrawState& that) const;
636
637 /**
638 * Optimizations for blending / coverage to that can be applied based on the current state.
639 */
bsalomon04ddf892014-11-19 12:36:22 -0800640 enum BlendOpt {
egdaniel89af44a2014-09-26 06:15:04 -0700641 /**
642 * No optimization
643 */
bsalomon04ddf892014-11-19 12:36:22 -0800644 kNone_BlendOpt,
egdaniel89af44a2014-09-26 06:15:04 -0700645 /**
646 * Don't draw at all
647 */
bsalomon04ddf892014-11-19 12:36:22 -0800648 kSkipDraw_BlendOpt,
egdaniel89af44a2014-09-26 06:15:04 -0700649 /**
650 * The coverage value does not have to be computed separately from alpha, the the output
651 * color can be the modulation of the two.
652 */
bsalomon04ddf892014-11-19 12:36:22 -0800653 kCoverageAsAlpha_BlendOpt,
egdaniel89af44a2014-09-26 06:15:04 -0700654 /**
655 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
656 * "don't cares".
657 */
bsalomon04ddf892014-11-19 12:36:22 -0800658 kEmitCoverage_BlendOpt,
egdaniel89af44a2014-09-26 06:15:04 -0700659 /**
660 * Emit transparent black instead of the src color, no need to compute coverage.
661 */
bsalomon04ddf892014-11-19 12:36:22 -0800662 kEmitTransBlack_BlendOpt
egdaniel89af44a2014-09-26 06:15:04 -0700663 };
egdaniel89af44a2014-09-26 06:15:04 -0700664
665 /**
666 * Determines what optimizations can be applied based on the blend. The coefficients may have
667 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
668 * params that receive the tweaked coefficients. Normally the function looks at the current
669 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
670 * determine the blend optimizations that would be used if there was partial pixel coverage.
671 *
bsalomon04ddf892014-11-19 12:36:22 -0800672 * This is used internally and when constructing a GrOptDrawState.
egdaniel89af44a2014-09-26 06:15:04 -0700673 */
bsalomon04ddf892014-11-19 12:36:22 -0800674 BlendOpt getBlendOpt(bool forceCoverage = false,
675 GrBlendCoeff* srcCoeff = NULL,
676 GrBlendCoeff* dstCoeff = NULL) const;
egdaniel89af44a2014-09-26 06:15:04 -0700677
egdaniel912b3d22014-11-17 07:45:53 -0800678 const GrProcOptInfo& colorProcInfo() const {
679 this->calcColorInvariantOutput();
680 return fColorProcInfo;
681 }
682
683 const GrProcOptInfo& coverageProcInfo() const {
684 this->calcCoverageInvariantOutput();
685 return fCoverageProcInfo;
686 }
687
egdaniel89af44a2014-09-26 06:15:04 -0700688 /**
689 * Determines whether src alpha is guaranteed to be one for all src pixels
690 */
691 bool srcAlphaWillBeOne() const;
692
egdanielb6cbc382014-11-13 11:00:34 -0800693 /**
694 * If fColorProcInfoValid is false, function calculates the invariant output for the color
695 * stages and results are stored in fColorProcInfo.
696 */
697 void calcColorInvariantOutput() const;
698
699 /**
700 * If fCoverageProcInfoValid is false, function calculates the invariant output for the coverage
701 * stages and results are stored in fCoverageProcInfo.
702 */
703 void calcCoverageInvariantOutput() const;
704
bsalomon8f727332014-08-05 07:50:06 -0700705 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000706
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000707 // Some of the auto restore objects assume that no effects are removed during their lifetime.
708 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000709 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000710
joshualitta5305a12014-10-10 17:47:00 -0700711 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray;
egdaniel89af44a2014-09-26 06:15:04 -0700712
bsalomonae59b772014-11-19 08:23:49 -0800713 SkAutoTUnref<GrRenderTarget> fRenderTarget;
714 GrColor fColor;
715 SkMatrix fViewMatrix;
716 GrColor fBlendConstant;
717 uint32_t fFlagBits;
bsalomonae59b772014-11-19 08:23:49 -0800718 GrStencilSettings fStencilSettings;
719 uint8_t fCoverage;
720 DrawFace fDrawFace;
721 GrBlendCoeff fSrcBlend;
722 GrBlendCoeff fDstBlend;
723 SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
724 FragmentStageArray fColorStages;
725 FragmentStageArray fCoverageStages;
726 uint32_t fHints;
egdaniel89af44a2014-09-26 06:15:04 -0700727
egdanielb6cbc382014-11-13 11:00:34 -0800728 mutable GrProcOptInfo fColorProcInfo;
729 mutable GrProcOptInfo fCoverageProcInfo;
730 mutable bool fColorProcInfoValid;
731 mutable bool fCoverageProcInfoValid;
732
egdanielb109ac22014-10-07 06:45:44 -0700733 friend class GrOptDrawState;
tomhudson@google.com93813632011-10-27 20:21:16 +0000734};
735
736#endif