blob: b855c8725bf1381d953b6db693082d2049ed4215 [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"
bsalomonf96ba022014-09-17 08:05:40 -070014#include "GrGpuResourceRef.h"
egdaniel89af44a2014-09-26 06:15:04 -070015#include "GrProcessorStage.h"
16#include "GrRenderTarget.h"
17#include "GrStencil.h"
18#include "SkMatrix.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000019#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000020
egdaniel89af44a2014-09-26 06:15:04 -070021class GrDrawTargetCaps;
egdaniel170f90b2014-09-16 12:54:40 -070022class GrOptDrawState;
egdaniel89af44a2014-09-26 06:15:04 -070023class GrPaint;
24class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070025
egdaniel89af44a2014-09-26 06:15:04 -070026class GrDrawState : public SkRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000027public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000028 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000029
egdaniel3658f382014-09-15 07:01:59 -070030 GrDrawState() : fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000031 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000032 this->reset();
33 }
tomhudson@google.com93813632011-10-27 20:21:16 +000034
egdaniel3658f382014-09-15 07:01:59 -070035 GrDrawState(const SkMatrix& initialViewMatrix) : fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000036 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000037 this->reset(initialViewMatrix);
38 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000039
40 /**
41 * Copies another draw state.
42 **/
egdaniel3658f382014-09-15 07:01:59 -070043 GrDrawState(const GrDrawState& state) : INHERITED(), fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000044 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000045 *this = state;
46 }
47
bsalomon@google.com137f1342013-05-29 21:27:53 +000048 /**
49 * Copies another draw state with a preconcat to the view matrix.
50 **/
bsalomon8f727332014-08-05 07:50:06 -070051 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000052
egdaniel170f90b2014-09-16 12:54:40 -070053 virtual ~GrDrawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +000054
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000055 /**
joshualittb0a8a372014-09-23 09:50:21 -070056 * Resets to the default state. GrProcessors will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000057 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000058 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000059
bsalomon@google.com137f1342013-05-29 21:27:53 +000060 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000061
62 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000063 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
64 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070065 * equivalents are set to default values with the exception of vertex attribute state which
66 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000067 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000068 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000069
70 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000071 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000072 ////
73
egdaniel89af44a2014-09-26 06:15:04 -070074 enum {
75 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
76 };
77
78 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; }
79 int getVertexAttribCount() const { return fVACount; }
80
81 size_t getVertexStride() const { return fVAStride; }
82
83 bool hasLocalCoordAttribute() const {
84 return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
85 }
86 bool hasColorVertexAttribute() const {
87 return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
88 }
89 bool hasCoverageVertexAttribute() const {
90 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
91 }
92
93 const int* getFixedFunctionVertexAttribIndices() const {
94 return fFixedFunctionVertexAttribIndices;
95 }
96
97 bool validateVertexAttribs() const;
98
jvanverth@google.com9b855c72013-03-01 18:21:22 +000099 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000100 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
101 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
102 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000103 *
joshualittb0a8a372014-09-23 09:50:21 -0700104 * The mapping of attributes with kEffect bindings to GrProcessor inputs is specified when
jvanverth@google.com054ae992013-04-01 20:06:51 +0000105 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000106 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000107
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000108 /**
robertphillips@google.com42903302013-04-20 12:26:07 +0000109 * Sets vertex attributes for next draw. The object driving the templatization
110 * should be a global GrVertexAttrib array that is never changed.
egdaniel7b3d5ee2014-08-28 05:41:14 -0700111 *
112 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
113 * @param stride the number of bytes between successive vertex data.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000114 */
egdaniel7b3d5ee2014-08-28 05:41:14 -0700115 template <const GrVertexAttrib A[]> void setVertexAttribs(int count, size_t stride) {
116 this->internalSetVertexAttribs(A, count, stride);
robertphillips@google.com42903302013-04-20 12:26:07 +0000117 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000118
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000119 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000120 * Sets default vertex attributes for next draw. The default is a single attribute:
121 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000122 */
123 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000124
jvanverth@google.com054ae992013-04-01 20:06:51 +0000125 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000126 * Helper to save/restore vertex attribs
127 */
128 class AutoVertexAttribRestore {
129 public:
bsalomon8f727332014-08-05 07:50:06 -0700130 AutoVertexAttribRestore(GrDrawState* drawState);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000131
egdaniel3658f382014-09-15 07:01:59 -0700132 ~AutoVertexAttribRestore() { fDrawState->internalSetVertexAttribs(fVAPtr, fVACount,
133 fVAStride); }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000134
135 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000136 GrDrawState* fDrawState;
137 const GrVertexAttrib* fVAPtr;
138 int fVACount;
egdaniel7b3d5ee2014-08-28 05:41:14 -0700139 size_t fVAStride;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000140 };
141
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000142 /// @}
143
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000144 /**
bsalomon62c447d2014-08-08 08:08:50 -0700145 * Depending on features available in the underlying 3D API and the color blend mode requested
146 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
147 * the fragment shader.
148 *
149 * This function considers the current draw state and the draw target's capabilities to
150 * determine whether coverage can be handled correctly. This function assumes that the caller
151 * intends to specify fractional pixel coverage (via setCoverage(), through a coverage vertex
152 * attribute, or a coverage effect) but may not have specified it yet.
153 */
154 bool couldApplyCoverage(const GrDrawTargetCaps& caps) const;
155
egdaniel89af44a2014-09-26 06:15:04 -0700156 /**
157 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
158 */
159 bool hasSolidCoverage() const;
160
jvanverth@google.comcc782382013-01-28 20:39:48 +0000161 /// @}
162
163 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000164 /// @name Color
165 ////
166
egdaniel89af44a2014-09-26 06:15:04 -0700167 GrColor getColor() const { return fColor; }
168
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000169 /**
170 * Sets color for next draw to a premultiplied-alpha color.
171 *
172 * @param color the color to set.
173 */
egdaniel9514d242014-07-18 06:15:43 -0700174 void setColor(GrColor color) {
egdaniel3658f382014-09-15 07:01:59 -0700175 if (color != fColor) {
176 fColor = color;
177 this->invalidateOptState();
178 }
egdaniel9514d242014-07-18 06:15:43 -0700179 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000180
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000181 /**
182 * Sets the color to be used for the next draw to be
183 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
184 *
185 * @param alpha The alpha value to set as the color.
186 */
bsalomon62c447d2014-08-08 08:08:50 -0700187 void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) | a); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000188
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189 /// @}
190
191 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000192 /// @name Coverage
193 ////
194
egdaniel89af44a2014-09-26 06:15:04 -0700195 uint8_t getCoverage() const { return fCoverage; }
196
197 GrColor getCoverageColor() const {
198 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
199 }
200
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000201 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000202 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000203 * initial value (after construction or reset()) is 0xff. The constant
204 * coverage is ignored when per-vertex coverage is provided.
205 */
206 void setCoverage(uint8_t coverage) {
egdaniel3658f382014-09-15 07:01:59 -0700207 if (coverage != fCoverage) {
208 fCoverage = coverage;
209 this->invalidateOptState();
210 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000211 }
212
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000213 /// @}
214
joshualittbd769d02014-09-04 08:56:46 -0700215 /**
216 * The geometry processor is the sole element of the skia pipeline which can use the vertex,
217 * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader
218 * but is never put in the color processing pipeline.
219 */
220
joshualittb0a8a372014-09-23 09:50:21 -0700221 const GrGeometryProcessor* setGeometryProcessor(const GrGeometryProcessor* geometryProcessor) {
222 SkASSERT(geometryProcessor);
joshualittbd769d02014-09-04 08:56:46 -0700223 SkASSERT(!this->hasGeometryProcessor());
joshualittb0a8a372014-09-23 09:50:21 -0700224 fGeometryProcessor.reset(new GrGeometryStage(geometryProcessor));
egdaniel3658f382014-09-15 07:01:59 -0700225 this->invalidateOptState();
joshualittb0a8a372014-09-23 09:50:21 -0700226 return geometryProcessor;
joshualittbd769d02014-09-04 08:56:46 -0700227 }
228
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000229 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000230 /// @name Effect Stages
joshualittb0a8a372014-09-23 09:50:21 -0700231 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
232 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000233 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
234 /// the fragment position, local coordinates).
235 ///
236 /// The stages are divided into two sets, color-computing and coverage-computing. The final
237 /// color stage produces the final pixel color. The coverage-computing stages function exactly
238 /// as the color-computing but the output of the final coverage stage is treated as a fractional
239 /// pixel coverage rather than as input to the src/dst color blend step.
240 ///
241 /// The input color to the first color-stage is either the constant color or interpolated
242 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
243 /// (usually full-coverage) or interpolated per-vertex coverage.
244 ///
245 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
246 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000247 ////
248
egdaniel89af44a2014-09-26 06:15:04 -0700249 int numColorStages() const { return fColorStages.count(); }
250 int numCoverageStages() const { return fCoverageStages.count(); }
251 int numTotalStages() const {
252 return this->numColorStages() + this->numCoverageStages() +
253 (this->hasGeometryProcessor() ? 1 : 0);
254 }
255
256 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
257 const GrGeometryStage* getGeometryProcessor() const { return fGeometryProcessor.get(); }
258 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; }
259 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; }
260
261 /**
262 * Checks whether any of the effects will read the dst pixel color.
263 */
264 bool willEffectReadDstColor() const;
265
joshualittb0a8a372014-09-23 09:50:21 -0700266 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700267 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700268 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect));
egdaniel3658f382014-09-15 07:01:59 -0700269 this->invalidateOptState();
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000270 return effect;
271 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000272
joshualittb0a8a372014-09-23 09:50:21 -0700273 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700274 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700275 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect));
egdaniel3658f382014-09-15 07:01:59 -0700276 this->invalidateOptState();
bsalomon@google.comadc65362013-01-28 14:26:09 +0000277 return effect;
278 }
279
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000280 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000281 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000282 */
joshualittb0a8a372014-09-23 09:50:21 -0700283 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
284 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000285 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000286
joshualittb0a8a372014-09-23 09:50:21 -0700287 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
288 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000289 }
290
joshualittb0a8a372014-09-23 09:50:21 -0700291 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000292 const SkMatrix& matrix,
293 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -0700294 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
295 }
296
297 void addCoverageTextureProcessor(GrTexture* texture,
298 const SkMatrix& matrix,
299 const GrTextureParams& params) {
300 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000301 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000302
robertphillips@google.com972265d2012-06-13 18:49:30 +0000303 /**
bsalomon9b536522014-09-05 09:18:51 -0700304 * When this object is destroyed it will remove any color/coverage effects from the draw state
305 * that were added after its constructor.
306 *
307 * This class has strange behavior around geometry processor. If there is a GP on the draw state
308 * it will assert that the GP is not modified until after the destructor of the ARE. If the
309 * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the
310 * destructor.
311 *
312 * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add
313 * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState
314 * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred
315 * execution state and GrOptDrawState always will be (and will be immutable and therefore
316 * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore
317 * the GP.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000318 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000319 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000320 public:
bsalomon9b536522014-09-05 09:18:51 -0700321 AutoRestoreEffects()
322 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700323 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700324 , fColorEffectCnt(0)
325 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000326
bsalomon9b536522014-09-05 09:18:51 -0700327 AutoRestoreEffects(GrDrawState* ds)
328 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700329 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700330 , fColorEffectCnt(0)
331 , fCoverageEffectCnt(0) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000332 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000333 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000334
335 ~AutoRestoreEffects() { this->set(NULL); }
336
bsalomon8f727332014-08-05 07:50:06 -0700337 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000338
bsalomon49f085d2014-09-05 13:34:00 -0700339 bool isSet() const { return SkToBool(fDrawState); }
bsalomon8af05232014-06-03 06:34:58 -0700340
robertphillips@google.com972265d2012-06-13 18:49:30 +0000341 private:
bsalomon9b536522014-09-05 09:18:51 -0700342 GrDrawState* fDrawState;
bsalomon52e9d632014-09-05 12:23:12 -0700343 uint32_t fOriginalGPID;
bsalomon9b536522014-09-05 09:18:51 -0700344 int fColorEffectCnt;
345 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000346 };
347
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000348 /// @}
349
350 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000351 /// @name Blending
352 ////
353
egdaniel89af44a2014-09-26 06:15:04 -0700354 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
355 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
356
357 /**
358 * Retrieves the last value set by setBlendConstant()
359 * @return the blending constant value
360 */
361 GrColor getBlendConstant() const { return fBlendConstant; }
362
363 /**
364 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
365 * coverage before the blend will give the correct final destination color. In general it
366 * will not as coverage is applied after blending.
367 */
368 bool canTweakAlphaForCoverage() const;
369
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000370 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000371 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000372 *
373 * The blend function will be:
374 * D' = sat(S*srcCoef + D*dstCoef)
375 *
376 * where D is the existing destination color, S is the incoming source
377 * color, and D' is the new destination color that will be written. sat()
378 * is the saturation function.
379 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000380 * @param srcCoef coefficient applied to the src color.
381 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000382 */
383 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
egdaniel3658f382014-09-15 07:01:59 -0700384 if (srcCoeff != fSrcBlend || dstCoeff != fDstBlend) {
385 fSrcBlend = srcCoeff;
386 fDstBlend = dstCoeff;
387 this->invalidateOptState();
388 }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000389 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000390 if (GrBlendCoeffRefsDst(dstCoeff)) {
391 GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000392 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000393 if (GrBlendCoeffRefsSrc(srcCoeff)) {
394 GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000395 }
396 #endif
397 }
398
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000399 /**
400 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000401 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000402 * kConstC_GrBlendCoeff
403 * kIConstC_GrBlendCoeff
404 * kConstA_GrBlendCoeff
405 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000406 *
407 * @param constant the constant to set
408 */
egdaniel9514d242014-07-18 06:15:43 -0700409 void setBlendConstant(GrColor constant) {
egdaniel3658f382014-09-15 07:01:59 -0700410 if (constant != fBlendConstant) {
411 fBlendConstant = constant;
412 this->invalidateOptState();
413 }
egdaniel9514d242014-07-18 06:15:43 -0700414 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000415
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000416 /// @}
417
418 ///////////////////////////////////////////////////////////////////////////
419 /// @name View Matrix
420 ////
421
422 /**
egdaniel89af44a2014-09-26 06:15:04 -0700423 * Retrieves the current view matrix
424 * @return the current view matrix.
425 */
426 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
427
428 /**
429 * Retrieves the inverse of the current view matrix.
430 *
431 * If the current view matrix is invertible, return true, and if matrix
432 * is non-null, copy the inverse into it. If the current view matrix is
433 * non-invertible, return false and ignore the matrix parameter.
434 *
435 * @param matrix if not null, will receive a copy of the current inverse.
436 */
437 bool getViewInverse(SkMatrix* matrix) const {
438 SkMatrix inverse;
439 if (fViewMatrix.invert(&inverse)) {
440 if (matrix) {
441 *matrix = inverse;
442 }
443 return true;
444 }
445 return false;
446 }
447
448 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000449 * Sets the view matrix to identity and updates any installed effects to compensate for the
450 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000451 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000452 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000453
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000454 ////////////////////////////////////////////////////////////////////////////
455
456 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000457 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000458 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000459 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000460 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000461 public:
462 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000463
bsalomon@google.comc7818882013-03-20 19:19:53 +0000464 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000465 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000466 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000467 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000468
469 ~AutoViewMatrixRestore() { this->restore(); }
470
bsalomon@google.coma8347462012-10-08 18:59:39 +0000471 /**
472 * Can be called prior to destructor to restore the original matrix.
473 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000474 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000475
bsalomon@google.comc7818882013-03-20 19:19:53 +0000476 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000477
bsalomon@google.com137f1342013-05-29 21:27:53 +0000478 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
479 is not invertible. */
480 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000481
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000482 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000483 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
484
joshualittb0a8a372014-09-23 09:50:21 -0700485 GrDrawState* fDrawState;
486 SkMatrix fViewMatrix;
487 int fNumColorStages;
488 bool fHasGeometryProcessor;
489 SkAutoSTArray<8, GrProcessorStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000490 };
491
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000492 /// @}
493
494 ///////////////////////////////////////////////////////////////////////////
495 /// @name Render Target
496 ////
497
498 /**
egdaniel89af44a2014-09-26 06:15:04 -0700499 * Retrieves the currently set render-target.
500 *
501 * @return The currently set render target.
502 */
503 GrRenderTarget* getRenderTarget() const {
504 return static_cast<GrRenderTarget*>(fRenderTarget.getResource());
505 }
506
507 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000508 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000509 *
510 * @param target The render target to set.
511 */
bsalomon2a9ca782014-09-05 14:27:43 -0700512 void setRenderTarget(GrRenderTarget* target) {
bsalomon45725db2014-09-19 11:48:02 -0700513 fRenderTarget.set(SkSafeRef(target), GrIORef::kWrite_IOType);
egdaniel3658f382014-09-15 07:01:59 -0700514 this->invalidateOptState();
bsalomon2a9ca782014-09-05 14:27:43 -0700515 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000516
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000517 /// @}
518
519 ///////////////////////////////////////////////////////////////////////////
520 /// @name Stencil
521 ////
522
egdaniel89af44a2014-09-26 06:15:04 -0700523 const GrStencilSettings& getStencil() const { return fStencilSettings; }
524
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000525 /**
526 * Sets the stencil settings to use for the next draw.
527 * Changing the clip has the side-effect of possibly zeroing
528 * out the client settable stencil bits. So multipass algorithms
529 * using stencil should not change the clip between passes.
530 * @param settings the stencil settings to use.
531 */
532 void setStencil(const GrStencilSettings& settings) {
egdaniel3658f382014-09-15 07:01:59 -0700533 if (settings != fStencilSettings) {
534 fStencilSettings = settings;
535 this->invalidateOptState();
536 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000537 }
538
539 /**
540 * Shortcut to disable stencil testing and ops.
541 */
542 void disableStencil() {
egdaniel3658f382014-09-15 07:01:59 -0700543 if (!fStencilSettings.isDisabled()) {
544 fStencilSettings.setDisabled();
545 this->invalidateOptState();
546 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547 }
548
bsalomon2ed5ef82014-07-07 08:44:05 -0700549 GrStencilSettings* stencil() { return &fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000550
551 /// @}
552
553 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000554 /// @name State Flags
555 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000556
egdaniel89af44a2014-09-26 06:15:04 -0700557 /**
558 * Flags that affect rendering. Controlled using enable/disableState(). All
559 * default to disabled.
560 */
561 enum StateBits {
562 /**
563 * Perform dithering. TODO: Re-evaluate whether we need this bit
564 */
565 kDither_StateBit = 0x01,
566 /**
567 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
568 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
569 * the 3D API.
570 */
571 kHWAntialias_StateBit = 0x02,
572 /**
573 * Draws will respect the clip, otherwise the clip is ignored.
574 */
575 kClip_StateBit = 0x04,
576 /**
577 * Disables writing to the color buffer. Useful when performing stencil
578 * operations.
579 */
580 kNoColorWrites_StateBit = 0x08,
581
582 /**
583 * Usually coverage is applied after color blending. The color is blended using the coeffs
584 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
585 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
586 * this case there is no distinction between coverage and color and the caller needs direct
587 * control over the blend coeffs. When set, there will be a single blend step controlled by
588 * setBlendFunc() which will use coverage*color as the src color.
589 */
590 kCoverageDrawing_StateBit = 0x10,
591
592 // Users of the class may add additional bits to the vector
593 kDummyStateBit,
594 kLastPublicStateBit = kDummyStateBit-1,
595 };
596
597 uint32_t getFlagBits() const { return fFlagBits; }
598
599 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); }
600
601 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
602 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
603 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
604
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 void resetStateFlags() {
egdaniel3658f382014-09-15 07:01:59 -0700606 if (0 != fFlagBits) {
607 fFlagBits = 0;
608 this->invalidateOptState();
609 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 }
611
612 /**
613 * Enable render state settings.
614 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000615 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616 */
617 void enableState(uint32_t stateBits) {
egdaniel3658f382014-09-15 07:01:59 -0700618 if (stateBits & ~fFlagBits) {
619 fFlagBits |= stateBits;
620 this->invalidateOptState();
621 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000622 }
623
624 /**
625 * Disable render state settings.
626 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000627 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 */
629 void disableState(uint32_t stateBits) {
egdaniel3658f382014-09-15 07:01:59 -0700630 if (stateBits & fFlagBits) {
631 fFlagBits &= ~(stateBits);
632 this->invalidateOptState();
633 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000634 }
635
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000636 /**
637 * Enable or disable stateBits based on a boolean.
638 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000639 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000640 * @param enable if true enable stateBits, otherwise disable
641 */
642 void setState(uint32_t stateBits, bool enable) {
643 if (enable) {
644 this->enableState(stateBits);
645 } else {
646 this->disableState(stateBits);
647 }
648 }
649
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 /// @}
651
652 ///////////////////////////////////////////////////////////////////////////
653 /// @name Face Culling
654 ////
655
egdaniel89af44a2014-09-26 06:15:04 -0700656 enum DrawFace {
657 kInvalid_DrawFace = -1,
658
659 kBoth_DrawFace,
660 kCCW_DrawFace,
661 kCW_DrawFace,
662 };
663
664 /**
665 * Gets whether the target is drawing clockwise, counterclockwise,
666 * or both faces.
667 * @return the current draw face(s).
668 */
669 DrawFace getDrawFace() const { return fDrawFace; }
670
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 /**
672 * Controls whether clockwise, counterclockwise, or both faces are drawn.
673 * @param face the face(s) to draw.
674 */
675 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000676 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700677 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 }
679
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000680 /// @}
681
682 ///////////////////////////////////////////////////////////////////////////
bsalomon62c447d2014-08-08 08:08:50 -0700683 /// @name Hints
684 /// Hints that when provided can enable optimizations.
685 ////
686
egdaniel89af44a2014-09-26 06:15:04 -0700687 enum Hints { kVertexColorsAreOpaque_Hint = 0x1, };
688
bsalomon62c447d2014-08-08 08:08:50 -0700689 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); }
690
egdaniel89af44a2014-09-26 06:15:04 -0700691 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
692
bsalomon62c447d2014-08-08 08:08:50 -0700693 /// @}
694
695 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000696
bsalomon838f62d2014-08-05 07:15:57 -0700697 /** Return type for CombineIfPossible. */
698 enum CombinedState {
699 /** The GrDrawStates cannot be combined. */
700 kIncompatible_CombinedState,
701 /** Either draw state can be used in place of the other. */
702 kAOrB_CombinedState,
703 /** Use the first draw state. */
704 kA_CombinedState,
705 /** Use the second draw state. */
706 kB_CombinedState,
707 };
708
709 /** This function determines whether the GrDrawStates used for two draws can be combined into
710 a single GrDrawState. This is used to avoid storing redundant GrDrawStates and to determine
711 if draws can be batched. The return value indicates whether combining is possible and, if
712 so, which of the two inputs should be used. */
bsalomon62c447d2014-08-08 08:08:50 -0700713 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawState& b,
714 const GrDrawTargetCaps& caps);
bsalomon72336ed2014-08-05 07:35:56 -0700715
bsalomon8f727332014-08-05 07:50:06 -0700716 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000717
egdaniel3658f382014-09-15 07:01:59 -0700718 /**
719 * Returns a snapshot of the current optimized state. If the current drawState has a valid
720 * cached optimiezed state it will simply return a pointer to it otherwise it will create a new
721 * GrOptDrawState. In all cases the GrOptDrawState is reffed and ownership is given to the
722 * caller.
723 */
egdanielc0648242014-09-22 13:17:02 -0700724 GrOptDrawState* createOptState(const GrDrawTargetCaps&) const;
egdaniel3658f382014-09-15 07:01:59 -0700725
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000726private:
egdaniel89af44a2014-09-26 06:15:04 -0700727 /**
728 * Converts refs on GrGpuResources owned directly or indirectly by this GrDrawState into
729 * pending reads and writes. This should be called when a GrDrawState is recorded into
730 * a GrDrawTarget for later execution. Subclasses of GrDrawState may add setters. However,
731 * once this call has been made the GrDrawState is immutable. It is also no longer copyable.
732 * In the future this conversion will automatically happen when converting a GrDrawState into
733 * an optimized draw state.
734 */
735 void convertToPendingExec();
736
737 friend class GrDrawTarget;
738
739 bool isEqual(const GrDrawState& that) const;
740
741 /**
742 * Optimizations for blending / coverage to that can be applied based on the current state.
743 */
744 enum BlendOptFlags {
745 /**
746 * No optimization
747 */
748 kNone_BlendOpt = 0,
749 /**
750 * Don't draw at all
751 */
752 kSkipDraw_BlendOptFlag = 0x1,
753 /**
754 * The coverage value does not have to be computed separately from alpha, the the output
755 * color can be the modulation of the two.
756 */
757 kCoverageAsAlpha_BlendOptFlag = 0x2,
758 /**
759 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
760 * "don't cares".
761 */
762 kEmitCoverage_BlendOptFlag = 0x4,
763 /**
764 * Emit transparent black instead of the src color, no need to compute coverage.
765 */
766 kEmitTransBlack_BlendOptFlag = 0x8,
767 };
768 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
769
770 /**
771 * Determines what optimizations can be applied based on the blend. The coefficients may have
772 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
773 * params that receive the tweaked coefficients. Normally the function looks at the current
774 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
775 * determine the blend optimizations that would be used if there was partial pixel coverage.
776 *
777 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
778 * playback) must call this function and respect the flags that replace the output color.
779 *
780 * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will
781 * simply returned the cached flags and coefficients. Otherwise it will calculate the values.
782 */
783 BlendOptFlags getBlendOpts(bool forceCoverage = false,
784 GrBlendCoeff* srcCoeff = NULL,
785 GrBlendCoeff* dstCoeff = NULL) const;
786
787 /**
788 * Determines whether src alpha is guaranteed to be one for all src pixels
789 */
790 bool srcAlphaWillBeOne() const;
791
egdaniel170f90b2014-09-16 12:54:40 -0700792 void invalidateOptState() const;
egdaniel3658f382014-09-15 07:01:59 -0700793
bsalomon8f727332014-08-05 07:50:06 -0700794 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000795
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000796 // Some of the auto restore objects assume that no effects are removed during their lifetime.
797 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000798 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799
egdaniel7b3d5ee2014-08-28 05:41:14 -0700800 void internalSetVertexAttribs(const GrVertexAttrib attribs[], int count, size_t stride);
robertphillips@google.com42903302013-04-20 12:26:07 +0000801
egdaniel89af44a2014-09-26 06:15:04 -0700802 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget;
803 // These fields are roughly sorted by decreasing likelihood of being different in op==
804 ProgramRenderTarget fRenderTarget;
805 GrColor fColor;
806 SkMatrix fViewMatrix;
807 GrColor fBlendConstant;
808 uint32_t fFlagBits;
809 const GrVertexAttrib* fVAPtr;
810 int fVACount;
811 size_t fVAStride;
812 GrStencilSettings fStencilSettings;
813 uint8_t fCoverage;
814 DrawFace fDrawFace;
815 GrBlendCoeff fSrcBlend;
816 GrBlendCoeff fDstBlend;
817
818 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray;
819 SkAutoTDelete<GrGeometryStage> fGeometryProcessor;
820 FragmentStageArray fColorStages;
821 FragmentStageArray fCoverageStages;
822
823 uint32_t fHints;
824
825 // This is simply a different representation of info in fVertexAttribs and thus does
826 // not need to be compared in op==.
827 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
828
egdaniel3658f382014-09-15 07:01:59 -0700829 mutable GrOptDrawState* fCachedOptState;
egdanielc0648242014-09-22 13:17:02 -0700830 mutable uint32_t fCachedCapsID;
egdaniel3658f382014-09-15 07:01:59 -0700831
egdaniel89af44a2014-09-26 06:15:04 -0700832 typedef SkRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000833};
834
egdaniel89af44a2014-09-26 06:15:04 -0700835//GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
836
tomhudson@google.com93813632011-10-27 20:21:16 +0000837#endif