blob: 3043fd76d52afb6169cb08381ee8f9cb88f84fc1 [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"
egdaniel89af44a2014-09-26 06:15:04 -070016#include "GrProcessorStage.h"
17#include "GrRenderTarget.h"
18#include "GrStencil.h"
19#include "SkMatrix.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000020#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000021
egdaniel89af44a2014-09-26 06:15:04 -070022class GrDrawTargetCaps;
egdaniel170f90b2014-09-16 12:54:40 -070023class GrOptDrawState;
egdaniel89af44a2014-09-26 06:15:04 -070024class GrPaint;
25class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070026
egdaniel89af44a2014-09-26 06:15:04 -070027class GrDrawState : public SkRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000028public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000029 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
egdaniel3658f382014-09-15 07:01:59 -070031 GrDrawState() : fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000032 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000033 this->reset();
34 }
tomhudson@google.com93813632011-10-27 20:21:16 +000035
egdaniel3658f382014-09-15 07:01:59 -070036 GrDrawState(const SkMatrix& initialViewMatrix) : fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000037 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000038 this->reset(initialViewMatrix);
39 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000040
41 /**
42 * Copies another draw state.
43 **/
egdaniel3658f382014-09-15 07:01:59 -070044 GrDrawState(const GrDrawState& state) : INHERITED(), fCachedOptState(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000045 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000046 *this = state;
47 }
48
bsalomon@google.com137f1342013-05-29 21:27:53 +000049 /**
50 * Copies another draw state with a preconcat to the view matrix.
51 **/
bsalomon8f727332014-08-05 07:50:06 -070052 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix);
robertphillips@google.com9ec07532012-06-22 12:01:30 +000053
egdaniel170f90b2014-09-16 12:54:40 -070054 virtual ~GrDrawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +000055
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000056 /**
joshualittb0a8a372014-09-23 09:50:21 -070057 * Resets to the default state. GrProcessors will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000058 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000059 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000060
bsalomon@google.com137f1342013-05-29 21:27:53 +000061 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000062
63 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000064 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
65 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon9c0822a2014-08-11 11:07:48 -070066 * equivalents are set to default values with the exception of vertex attribute state which
67 * is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000068 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000069 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000070
71 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000072 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000073 ////
74
egdaniel89af44a2014-09-26 06:15:04 -070075 enum {
76 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
77 };
78
79 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; }
80 int getVertexAttribCount() const { return fVACount; }
81
82 size_t getVertexStride() const { return fVAStride; }
83
84 bool hasLocalCoordAttribute() const {
85 return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
86 }
87 bool hasColorVertexAttribute() const {
88 return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
89 }
90 bool hasCoverageVertexAttribute() const {
91 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
92 }
93
94 const int* getFixedFunctionVertexAttribIndices() const {
95 return fFixedFunctionVertexAttribIndices;
96 }
97
98 bool validateVertexAttribs() const;
99
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000100 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000101 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
102 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
103 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000104 *
joshualittb0a8a372014-09-23 09:50:21 -0700105 * The mapping of attributes with kEffect bindings to GrProcessor inputs is specified when
jvanverth@google.com054ae992013-04-01 20:06:51 +0000106 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000107 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000108
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000109 /**
robertphillips@google.com42903302013-04-20 12:26:07 +0000110 * Sets vertex attributes for next draw. The object driving the templatization
111 * should be a global GrVertexAttrib array that is never changed.
egdaniel7b3d5ee2014-08-28 05:41:14 -0700112 *
113 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
114 * @param stride the number of bytes between successive vertex data.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000115 */
egdaniel7b3d5ee2014-08-28 05:41:14 -0700116 template <const GrVertexAttrib A[]> void setVertexAttribs(int count, size_t stride) {
117 this->internalSetVertexAttribs(A, count, stride);
robertphillips@google.com42903302013-04-20 12:26:07 +0000118 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000119
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000120 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000121 * Sets default vertex attributes for next draw. The default is a single attribute:
122 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000123 */
124 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000125
jvanverth@google.com054ae992013-04-01 20:06:51 +0000126 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000127 * Helper to save/restore vertex attribs
128 */
129 class AutoVertexAttribRestore {
130 public:
bsalomon8f727332014-08-05 07:50:06 -0700131 AutoVertexAttribRestore(GrDrawState* drawState);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000132
egdaniel3658f382014-09-15 07:01:59 -0700133 ~AutoVertexAttribRestore() { fDrawState->internalSetVertexAttribs(fVAPtr, fVACount,
134 fVAStride); }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000135
136 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000137 GrDrawState* fDrawState;
138 const GrVertexAttrib* fVAPtr;
139 int fVACount;
egdaniel7b3d5ee2014-08-28 05:41:14 -0700140 size_t fVAStride;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000141 };
142
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000143 /// @}
144
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000145 /**
bsalomon62c447d2014-08-08 08:08:50 -0700146 * Depending on features available in the underlying 3D API and the color blend mode requested
147 * it may or may not be possible to correctly blend with fractional pixel coverage generated by
148 * the fragment shader.
149 *
150 * This function considers the current draw state and the draw target's capabilities to
151 * determine whether coverage can be handled correctly. This function assumes that the caller
152 * intends to specify fractional pixel coverage (via setCoverage(), through a coverage vertex
153 * attribute, or a coverage effect) but may not have specified it yet.
154 */
155 bool couldApplyCoverage(const GrDrawTargetCaps& caps) const;
156
egdaniel89af44a2014-09-26 06:15:04 -0700157 /**
158 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
159 */
160 bool hasSolidCoverage() const;
161
jvanverth@google.comcc782382013-01-28 20:39:48 +0000162 /// @}
163
164 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000165 /// @name Color
166 ////
167
egdaniel89af44a2014-09-26 06:15:04 -0700168 GrColor getColor() const { return fColor; }
169
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000170 /**
171 * Sets color for next draw to a premultiplied-alpha color.
172 *
173 * @param color the color to set.
174 */
egdaniel9514d242014-07-18 06:15:43 -0700175 void setColor(GrColor color) {
egdaniel3658f382014-09-15 07:01:59 -0700176 if (color != fColor) {
177 fColor = color;
178 this->invalidateOptState();
179 }
egdaniel9514d242014-07-18 06:15:43 -0700180 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000181
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000182 /**
183 * Sets the color to be used for the next draw to be
184 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
185 *
186 * @param alpha The alpha value to set as the color.
187 */
bsalomon62c447d2014-08-08 08:08:50 -0700188 void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) | a); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000190 /// @}
191
192 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000193 /// @name Coverage
194 ////
195
egdaniel89af44a2014-09-26 06:15:04 -0700196 uint8_t getCoverage() const { return fCoverage; }
197
198 GrColor getCoverageColor() const {
199 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
200 }
201
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000202 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000204 * initial value (after construction or reset()) is 0xff. The constant
205 * coverage is ignored when per-vertex coverage is provided.
206 */
207 void setCoverage(uint8_t coverage) {
egdaniel3658f382014-09-15 07:01:59 -0700208 if (coverage != fCoverage) {
209 fCoverage = coverage;
210 this->invalidateOptState();
211 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000212 }
213
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000214 /// @}
215
joshualittbd769d02014-09-04 08:56:46 -0700216 /**
217 * The geometry processor is the sole element of the skia pipeline which can use the vertex,
218 * geometry, and tesselation shaders. The GP may also compute a coverage in its fragment shader
219 * but is never put in the color processing pipeline.
220 */
221
joshualittb0a8a372014-09-23 09:50:21 -0700222 const GrGeometryProcessor* setGeometryProcessor(const GrGeometryProcessor* geometryProcessor) {
223 SkASSERT(geometryProcessor);
joshualittbd769d02014-09-04 08:56:46 -0700224 SkASSERT(!this->hasGeometryProcessor());
joshualitta5305a12014-10-10 17:47:00 -0700225 fGeometryProcessor.reset(SkRef(geometryProcessor));
egdaniel3658f382014-09-15 07:01:59 -0700226 this->invalidateOptState();
joshualittb0a8a372014-09-23 09:50:21 -0700227 return geometryProcessor;
joshualittbd769d02014-09-04 08:56:46 -0700228 }
229
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000230 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000231 /// @name Effect Stages
joshualittb0a8a372014-09-23 09:50:21 -0700232 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
233 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000234 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
235 /// the fragment position, local coordinates).
236 ///
237 /// The stages are divided into two sets, color-computing and coverage-computing. The final
238 /// color stage produces the final pixel color. The coverage-computing stages function exactly
239 /// as the color-computing but the output of the final coverage stage is treated as a fractional
240 /// pixel coverage rather than as input to the src/dst color blend step.
241 ///
242 /// The input color to the first color-stage is either the constant color or interpolated
243 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
244 /// (usually full-coverage) or interpolated per-vertex coverage.
245 ///
246 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
247 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000248 ////
249
egdaniel89af44a2014-09-26 06:15:04 -0700250 int numColorStages() const { return fColorStages.count(); }
251 int numCoverageStages() const { return fCoverageStages.count(); }
252 int numTotalStages() const {
253 return this->numColorStages() + this->numCoverageStages() +
254 (this->hasGeometryProcessor() ? 1 : 0);
255 }
256
257 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualitta5305a12014-10-10 17:47:00 -0700258 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700259 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[idx]; }
260 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageStages[idx]; }
261
262 /**
263 * Checks whether any of the effects will read the dst pixel color.
264 */
265 bool willEffectReadDstColor() const;
266
joshualittb0a8a372014-09-23 09:50:21 -0700267 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700268 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700269 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect));
egdaniel3658f382014-09-15 07:01:59 -0700270 this->invalidateOptState();
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000271 return effect;
272 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000273
joshualittb0a8a372014-09-23 09:50:21 -0700274 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* effect) {
bsalomon49f085d2014-09-05 13:34:00 -0700275 SkASSERT(effect);
joshualittb0a8a372014-09-23 09:50:21 -0700276 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect));
egdaniel3658f382014-09-15 07:01:59 -0700277 this->invalidateOptState();
bsalomon@google.comadc65362013-01-28 14:26:09 +0000278 return effect;
279 }
280
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000281 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000282 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000283 */
joshualittb0a8a372014-09-23 09:50:21 -0700284 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
285 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000286 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000287
joshualittb0a8a372014-09-23 09:50:21 -0700288 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
289 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000290 }
291
joshualittb0a8a372014-09-23 09:50:21 -0700292 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000293 const SkMatrix& matrix,
294 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -0700295 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
296 }
297
298 void addCoverageTextureProcessor(GrTexture* texture,
299 const SkMatrix& matrix,
300 const GrTextureParams& params) {
301 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000302 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000303
robertphillips@google.com972265d2012-06-13 18:49:30 +0000304 /**
bsalomon9b536522014-09-05 09:18:51 -0700305 * When this object is destroyed it will remove any color/coverage effects from the draw state
306 * that were added after its constructor.
307 *
308 * This class has strange behavior around geometry processor. If there is a GP on the draw state
309 * it will assert that the GP is not modified until after the destructor of the ARE. If the
310 * draw state has a NULL GP when the ARE is constructed then it will reset it to null in the
311 * destructor.
312 *
313 * TODO: We'd prefer for the ARE to just save and restore the GP. However, this would add
314 * significant complexity to the multi-ref architecture for deferred drawing. Once GrDrawState
315 * and GrOptDrawState are fully separated then GrDrawState will never be in the deferred
316 * execution state and GrOptDrawState always will be (and will be immutable and therefore
317 * unable to have an ARE). At this point we can restore sanity and have the ARE save and restore
318 * the GP.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000319 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000320 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000321 public:
bsalomon9b536522014-09-05 09:18:51 -0700322 AutoRestoreEffects()
323 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700324 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700325 , fColorEffectCnt(0)
326 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000327
bsalomon9b536522014-09-05 09:18:51 -0700328 AutoRestoreEffects(GrDrawState* ds)
329 : fDrawState(NULL)
bsalomon52e9d632014-09-05 12:23:12 -0700330 , fOriginalGPID(SK_InvalidUniqueID)
bsalomon9b536522014-09-05 09:18:51 -0700331 , fColorEffectCnt(0)
332 , fCoverageEffectCnt(0) {
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000333 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000334 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000335
336 ~AutoRestoreEffects() { this->set(NULL); }
337
bsalomon8f727332014-08-05 07:50:06 -0700338 void set(GrDrawState* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000339
bsalomon49f085d2014-09-05 13:34:00 -0700340 bool isSet() const { return SkToBool(fDrawState); }
bsalomon8af05232014-06-03 06:34:58 -0700341
robertphillips@google.com972265d2012-06-13 18:49:30 +0000342 private:
bsalomon9b536522014-09-05 09:18:51 -0700343 GrDrawState* fDrawState;
bsalomon52e9d632014-09-05 12:23:12 -0700344 uint32_t fOriginalGPID;
bsalomon9b536522014-09-05 09:18:51 -0700345 int fColorEffectCnt;
346 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000347 };
348
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000349 /// @}
350
351 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000352 /// @name Blending
353 ////
354
egdaniel89af44a2014-09-26 06:15:04 -0700355 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
356 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
357
358 /**
359 * Retrieves the last value set by setBlendConstant()
360 * @return the blending constant value
361 */
362 GrColor getBlendConstant() const { return fBlendConstant; }
363
364 /**
365 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
366 * coverage before the blend will give the correct final destination color. In general it
367 * will not as coverage is applied after blending.
368 */
369 bool canTweakAlphaForCoverage() const;
370
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000371 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000372 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000373 *
374 * The blend function will be:
375 * D' = sat(S*srcCoef + D*dstCoef)
376 *
377 * where D is the existing destination color, S is the incoming source
378 * color, and D' is the new destination color that will be written. sat()
379 * is the saturation function.
380 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000381 * @param srcCoef coefficient applied to the src color.
382 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000383 */
384 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
egdaniel3658f382014-09-15 07:01:59 -0700385 if (srcCoeff != fSrcBlend || dstCoeff != fDstBlend) {
386 fSrcBlend = srcCoeff;
387 fDstBlend = dstCoeff;
388 this->invalidateOptState();
389 }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000390 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000391 if (GrBlendCoeffRefsDst(dstCoeff)) {
392 GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000393 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000394 if (GrBlendCoeffRefsSrc(srcCoeff)) {
395 GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000396 }
397 #endif
398 }
399
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400 /**
401 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000402 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000403 * kConstC_GrBlendCoeff
404 * kIConstC_GrBlendCoeff
405 * kConstA_GrBlendCoeff
406 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000407 *
408 * @param constant the constant to set
409 */
egdaniel9514d242014-07-18 06:15:43 -0700410 void setBlendConstant(GrColor constant) {
egdaniel3658f382014-09-15 07:01:59 -0700411 if (constant != fBlendConstant) {
412 fBlendConstant = constant;
413 this->invalidateOptState();
414 }
egdaniel9514d242014-07-18 06:15:43 -0700415 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000416
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000417 /// @}
418
419 ///////////////////////////////////////////////////////////////////////////
420 /// @name View Matrix
421 ////
422
423 /**
egdaniel89af44a2014-09-26 06:15:04 -0700424 * Retrieves the current view matrix
425 * @return the current view matrix.
426 */
427 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
428
429 /**
430 * Retrieves the inverse of the current view matrix.
431 *
432 * If the current view matrix is invertible, return true, and if matrix
433 * is non-null, copy the inverse into it. If the current view matrix is
434 * non-invertible, return false and ignore the matrix parameter.
435 *
436 * @param matrix if not null, will receive a copy of the current inverse.
437 */
438 bool getViewInverse(SkMatrix* matrix) const {
439 SkMatrix inverse;
440 if (fViewMatrix.invert(&inverse)) {
441 if (matrix) {
442 *matrix = inverse;
443 }
444 return true;
445 }
446 return false;
447 }
448
449 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000450 * Sets the view matrix to identity and updates any installed effects to compensate for the
451 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000452 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000453 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000454
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000455 ////////////////////////////////////////////////////////////////////////////
456
457 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000458 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000459 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000460 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000461 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000462 public:
463 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000464
bsalomon@google.comc7818882013-03-20 19:19:53 +0000465 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000467 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000468 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000469
470 ~AutoViewMatrixRestore() { this->restore(); }
471
bsalomon@google.coma8347462012-10-08 18:59:39 +0000472 /**
473 * Can be called prior to destructor to restore the original matrix.
474 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000475 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000476
bsalomon@google.comc7818882013-03-20 19:19:53 +0000477 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000478
bsalomon@google.com137f1342013-05-29 21:27:53 +0000479 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
480 is not invertible. */
481 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000482
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000483 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000484 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
485
joshualittb0a8a372014-09-23 09:50:21 -0700486 GrDrawState* fDrawState;
487 SkMatrix fViewMatrix;
488 int fNumColorStages;
joshualitta5305a12014-10-10 17:47:00 -0700489 SkAutoSTArray<8, GrFragmentStage::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) {
bsalomonbcf0a522014-10-08 08:40:09 -0700513 fRenderTarget.set(SkSafeRef(target), kWrite_GrIOType);
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
joshualitt65171342014-10-09 07:25:36 -0700687 enum Hints {
688 kVertexColorsAreOpaque_Hint = 0x1,
689 kLast_Hint = kVertexColorsAreOpaque_Hint
690 };
egdaniel89af44a2014-09-26 06:15:04 -0700691
bsalomon62c447d2014-08-08 08:08:50 -0700692 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (fHints & ~hint); }
693
egdaniel89af44a2014-09-26 06:15:04 -0700694 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
695
bsalomon62c447d2014-08-08 08:08:50 -0700696 /// @}
697
698 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000699
bsalomon838f62d2014-08-05 07:15:57 -0700700 /** Return type for CombineIfPossible. */
701 enum CombinedState {
702 /** The GrDrawStates cannot be combined. */
703 kIncompatible_CombinedState,
704 /** Either draw state can be used in place of the other. */
705 kAOrB_CombinedState,
706 /** Use the first draw state. */
707 kA_CombinedState,
708 /** Use the second draw state. */
709 kB_CombinedState,
710 };
711
712 /** This function determines whether the GrDrawStates used for two draws can be combined into
713 a single GrDrawState. This is used to avoid storing redundant GrDrawStates and to determine
714 if draws can be batched. The return value indicates whether combining is possible and, if
715 so, which of the two inputs should be used. */
bsalomon62c447d2014-08-08 08:08:50 -0700716 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawState& b,
717 const GrDrawTargetCaps& caps);
bsalomon72336ed2014-08-05 07:35:56 -0700718
bsalomon8f727332014-08-05 07:50:06 -0700719 GrDrawState& operator= (const GrDrawState& that);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000720
721private:
egdaniel89af44a2014-09-26 06:15:04 -0700722 /**
723 * Converts refs on GrGpuResources owned directly or indirectly by this GrDrawState into
724 * pending reads and writes. This should be called when a GrDrawState is recorded into
725 * a GrDrawTarget for later execution. Subclasses of GrDrawState may add setters. However,
726 * once this call has been made the GrDrawState is immutable. It is also no longer copyable.
727 * In the future this conversion will automatically happen when converting a GrDrawState into
728 * an optimized draw state.
729 */
730 void convertToPendingExec();
731
732 friend class GrDrawTarget;
733
734 bool isEqual(const GrDrawState& that) const;
735
736 /**
737 * Optimizations for blending / coverage to that can be applied based on the current state.
738 */
739 enum BlendOptFlags {
740 /**
741 * No optimization
742 */
743 kNone_BlendOpt = 0,
744 /**
745 * Don't draw at all
746 */
747 kSkipDraw_BlendOptFlag = 0x1,
748 /**
749 * The coverage value does not have to be computed separately from alpha, the the output
750 * color can be the modulation of the two.
751 */
752 kCoverageAsAlpha_BlendOptFlag = 0x2,
753 /**
754 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
755 * "don't cares".
756 */
757 kEmitCoverage_BlendOptFlag = 0x4,
758 /**
759 * Emit transparent black instead of the src color, no need to compute coverage.
760 */
761 kEmitTransBlack_BlendOptFlag = 0x8,
762 };
763 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
764
765 /**
766 * Determines what optimizations can be applied based on the blend. The coefficients may have
767 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
768 * params that receive the tweaked coefficients. Normally the function looks at the current
769 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
770 * determine the blend optimizations that would be used if there was partial pixel coverage.
771 *
772 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
773 * playback) must call this function and respect the flags that replace the output color.
774 *
775 * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will
776 * simply returned the cached flags and coefficients. Otherwise it will calculate the values.
777 */
778 BlendOptFlags getBlendOpts(bool forceCoverage = false,
779 GrBlendCoeff* srcCoeff = NULL,
780 GrBlendCoeff* dstCoeff = NULL) const;
781
782 /**
783 * Determines whether src alpha is guaranteed to be one for all src pixels
784 */
785 bool srcAlphaWillBeOne() const;
786
egdaniel170f90b2014-09-16 12:54:40 -0700787 void invalidateOptState() const;
egdaniel3658f382014-09-15 07:01:59 -0700788
bsalomon8f727332014-08-05 07:50:06 -0700789 void onReset(const SkMatrix* initialViewMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000790
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000791 // Some of the auto restore objects assume that no effects are removed during their lifetime.
792 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000793 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000794
egdaniel7b3d5ee2014-08-28 05:41:14 -0700795 void internalSetVertexAttribs(const GrVertexAttrib attribs[], int count, size_t stride);
robertphillips@google.com42903302013-04-20 12:26:07 +0000796
egdaniel89af44a2014-09-26 06:15:04 -0700797 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget;
798 // These fields are roughly sorted by decreasing likelihood of being different in op==
799 ProgramRenderTarget fRenderTarget;
800 GrColor fColor;
801 SkMatrix fViewMatrix;
802 GrColor fBlendConstant;
803 uint32_t fFlagBits;
804 const GrVertexAttrib* fVAPtr;
805 int fVACount;
806 size_t fVAStride;
807 GrStencilSettings fStencilSettings;
808 uint8_t fCoverage;
809 DrawFace fDrawFace;
810 GrBlendCoeff fSrcBlend;
811 GrBlendCoeff fDstBlend;
812
joshualitta5305a12014-10-10 17:47:00 -0700813 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray;
814 typedef GrProgramElementRef<const GrGeometryProcessor> ProgramGeometryProcessor;
815 ProgramGeometryProcessor fGeometryProcessor;
816 FragmentStageArray fColorStages;
817 FragmentStageArray fCoverageStages;
egdaniel89af44a2014-09-26 06:15:04 -0700818
819 uint32_t fHints;
820
821 // This is simply a different representation of info in fVertexAttribs and thus does
822 // not need to be compared in op==.
823 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
824
egdaniel3658f382014-09-15 07:01:59 -0700825 mutable GrOptDrawState* fCachedOptState;
egdanielc0648242014-09-22 13:17:02 -0700826 mutable uint32_t fCachedCapsID;
egdaniel3658f382014-09-15 07:01:59 -0700827
egdanielb109ac22014-10-07 06:45:44 -0700828 friend class GrOptDrawState;
829
egdaniel89af44a2014-09-26 06:15:04 -0700830 typedef SkRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000831};
832
egdanielb109ac22014-10-07 06:45:44 -0700833GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
egdaniel89af44a2014-09-26 06:15:04 -0700834
tomhudson@google.com93813632011-10-27 20:21:16 +0000835#endif