blob: de23df76507390ebf595006c16a50d2dcf67667a [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
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000011#include "GrBackendEffectFactory.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrColor.h"
bsalomon@google.com08283af2012-10-26 13:01:20 +000013#include "GrEffectStage.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000014#include "GrRefCnt.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000015#include "GrRenderTarget.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000016#include "GrStencil.h"
17#include "GrTemplates.h"
18#include "GrTexture.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000019#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000020
jvanverth@google.comcc782382013-01-28 20:39:48 +000021#include "SkMatrix.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000022#include "SkXfermode.h"
23
bsalomon@google.comaf84e742012-10-05 13:23:24 +000024class GrPaint;
tomhudson@google.com93813632011-10-27 20:21:16 +000025
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000026class GrDrawState : public GrRefCnt {
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
tomhudson@google.com93813632011-10-27 20:21:16 +000030 /**
bsalomon@google.com13221342012-10-26 13:41:59 +000031 * Total number of effect stages. Each stage can host a GrEffect. A stage is enabled if it has a
32 * GrEffect. The effect produces an output color in the fragment shader. It's inputs are the
33 * output from the previous enabled stage and a position. The position is either derived from
34 * the interpolated vertex positions or explicit per-vertex coords, depending upon the
35 * GrVertexLayout used to draw.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000036 *
bsalomon@google.com13221342012-10-26 13:41:59 +000037 * The stages are divided into two sets, color-computing and coverage-computing. The final color
38 * stage produces the final pixel color. The coverage-computing stages function exactly as the
39 * color-computing but the output of the final coverage stage is treated as a fractional pixel
40 * coverage rather than as input to the src/dst color blend step.
41 *
42 * The input color to the first enabled color-stage is either the constant color or interpolated
43 * per-vertex colors, depending upon GrVertexLayout. The input to the first coverage stage is
44 * either a constant coverage (usually full-coverage), interpolated per-vertex coverage, or
45 * edge-AA computed coverage. (This latter is going away as soon as it can be rewritten as a
46 * GrEffect).
47 *
bsalomon@google.comcf939ae2012-12-13 19:59:23 +000048 * See the documentation of kCoverageDrawing_StateBit for information about disabling the
49 * the color / coverage distinction.
50 *
bsalomon@google.com13221342012-10-26 13:41:59 +000051 * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied from the client's
52 * GrPaint. Stages GrPaint::kTotalStages through kNumStages-2 are earmarked for use by
53 * GrTextContext and GrPathRenderer-derived classes. kNumStages-1 is earmarked for clipping
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000054 * by GrClipMaskManager.
tomhudson@google.com93813632011-10-27 20:21:16 +000055 */
56 enum {
twiz@google.com58071162012-07-18 21:41:50 +000057 kNumStages = 5,
tomhudson@google.com93813632011-10-27 20:21:16 +000058 kMaxTexCoords = kNumStages
59 };
60
bsalomon@google.comca432082013-01-23 19:53:46 +000061 GrDrawState() {
reed@google.com75847192013-01-28 20:53:22 +000062#if GR_DEBUG
63 VertexLayoutUnitTest();
64#endif
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000065 this->reset();
66 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000067
bsalomon@google.comca432082013-01-23 19:53:46 +000068 GrDrawState(const GrDrawState& state) {
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000069 *this = state;
70 }
71
robertphillips@google.com9ec07532012-06-22 12:01:30 +000072 virtual ~GrDrawState() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000073 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000074 }
75
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000076 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000077 * Resets to the default state.
bsalomon@google.com08283af2012-10-26 13:01:20 +000078 * GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000079 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000080 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000081
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000082 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000083
bsalomon@google.comca432082013-01-23 19:53:46 +000084 fRenderTarget.reset(NULL);
85
86 fCommon.fColor = 0xffffffff;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000087 fCommon.fVertexLayout = kDefault_VertexLayout;
bsalomon@google.comca432082013-01-23 19:53:46 +000088 fCommon.fViewMatrix.reset();
89 fCommon.fSrcBlend = kOne_GrBlendCoeff;
90 fCommon.fDstBlend = kZero_GrBlendCoeff;
91 fCommon.fBlendConstant = 0x0;
92 fCommon.fFlagBits = 0x0;
93 fCommon.fVertexEdgeType = kHairLine_EdgeType;
94 fCommon.fStencilSettings.setDisabled();
95 fCommon.fFirstCoverageStage = kNumStages;
96 fCommon.fCoverage = 0xffffffff;
97 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
98 fCommon.fColorFilterColor = 0x0;
99 fCommon.fDrawFace = kBoth_DrawFace;
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000100 }
101
102 /**
103 * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000104 * encompasses more than GrPaint. Aspects of GrDrawState that have no
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000105 * GrPaint equivalents are not modified. GrPaint has fewer stages than
106 * GrDrawState. The extra GrDrawState stages are disabled.
107 */
108 void setFromPaint(const GrPaint& paint);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000109
110 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000111 /// @name Vertex Layout
jvanverth@google.comcc782382013-01-28 20:39:48 +0000112 ////
113
114 /**
115 * The format of vertices is represented as a bitfield of flags.
116 * Flags that indicate the layout of vertex data. Vertices always contain
117 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
118 * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
119 * Each stage can
120 * use any of the texture coordinates as its input texture coordinates or it
121 * may use the positions as texture coordinates.
122 *
123 * If no texture coordinates are specified for a stage then the stage is
124 * disabled.
125 *
126 * Only one type of texture coord can be specified per stage. For
127 * example StageTexCoordVertexLayoutBit(0, 2) and
128 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
129 *
130 * The order in memory is always (position, texture coord 0, ..., color,
131 * coverage) with any unused fields omitted. Note that this means that if
132 * only texture coordinates 1 is referenced then there is no texture
133 * coordinates 0 and the order would be (position, texture coordinate 1
134 * [, color][, coverage]).
135 */
136
137 /**
138 * Generates a bit indicating that a texture stage uses texture coordinates
139 *
140 * @param stageIdx the stage that will use texture coordinates.
141 * @param texCoordIdx the index of the texture coordinates to use
142 *
143 * @return the bit to add to a GrVertexLayout bitfield.
144 */
145 static int StageTexCoordVertexLayoutBit(int stageIdx, int texCoordIdx) {
146 GrAssert(stageIdx < kNumStages);
147 GrAssert(texCoordIdx < kMaxTexCoords);
148 return 1 << (stageIdx + (texCoordIdx * kNumStages));
149 }
150
151 static bool StageUsesTexCoords(GrVertexLayout layout, int stageIdx);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000152
jvanverth@google.comcc782382013-01-28 20:39:48 +0000153private:
154 // non-stage bits start at this index.
155 static const int STAGE_BIT_CNT = kNumStages * kMaxTexCoords;
156public:
157
158 /**
159 * Additional Bits that can be specified in GrVertexLayout.
160 */
161 enum VertexLayoutBits {
162 /* vertices have colors (GrColor) */
163 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
164 /* vertices have coverage (GrColor)
165 */
166 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
167 /* Use text vertices. (Pos and tex coords may be a different type for
168 * text [GrGpuTextVertex vs GrPoint].)
169 */
170 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
171
172 /* Each vertex specificies an edge. Distance to the edge is used to
173 * compute a coverage. See GrDrawState::setVertexEdgeType().
174 */
175 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
176 // for below assert
177 kDummyVertexLayoutBit,
178 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
179 };
180 // make sure we haven't exceeded the number of bits in GrVertexLayout.
181 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
182
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000183 enum VertexLayout {
184 kDefault_VertexLayout = 0
185 };
186
187 /**
188 * Sets vertex layout for next draw.
189 *
190 * @param layout the vertex layout to set.
191 */
192 void setVertexLayout(GrVertexLayout layout) { fCommon.fVertexLayout = layout; }
193
194 GrVertexLayout getVertexLayout() const { return fCommon.fVertexLayout; }
195 size_t getVertexSize() const { return VertexSize(fCommon.fVertexLayout); }
196
197
jvanverth@google.comcc782382013-01-28 20:39:48 +0000198 ////////////////////////////////////////////////////////////////////////////
199 // Helpers for picking apart vertex layouts
200
201 /**
202 * Helper function to compute the size of a vertex from a vertex layout
203 * @return size of a single vertex.
204 */
205 static size_t VertexSize(GrVertexLayout vertexLayout);
206
207 /**
208 * Helper function for determining the index of texture coordinates that
209 * is input for a texture stage. Note that a stage may instead use positions
210 * as texture coordinates, in which case the result of the function is
211 * indistinguishable from the case when the stage is disabled.
212 *
213 * @param stageIdx the stage to query
214 * @param vertexLayout layout to query
215 *
216 * @return the texture coordinate index or -1 if the stage doesn't use
217 * separate (non-position) texture coordinates.
218 */
219 static int VertexTexCoordsForStage(int stageIdx, GrVertexLayout vertexLayout);
220
221 /**
222 * Helper function to compute the offset of texture coordinates in a vertex
223 * @return offset of texture coordinates in vertex layout or -1 if the
224 * layout has no texture coordinates. Will be 0 if positions are
225 * used as texture coordinates for the stage.
226 */
227 static int VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout);
228
229 /**
230 * Helper function to compute the offset of the color in a vertex
231 * @return offset of color in vertex layout or -1 if the
232 * layout has no color.
233 */
234 static int VertexColorOffset(GrVertexLayout vertexLayout);
235
236 /**
237 * Helper function to compute the offset of the coverage in a vertex
238 * @return offset of coverage in vertex layout or -1 if the
239 * layout has no coverage.
240 */
241 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
242
243 /**
244 * Helper function to compute the offset of the edge pts in a vertex
245 * @return offset of edge in vertex layout or -1 if the
246 * layout has no edge.
247 */
248 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
249
250 /**
251 * Helper function to determine if vertex layout contains explicit texture
252 * coordinates of some index.
253 *
254 * @param coordIndex the tex coord index to query
255 * @param vertexLayout layout to query
256 *
257 * @return true if vertex specifies texture coordinates for the index,
258 * false otherwise.
259 */
260 static bool VertexUsesTexCoordIdx(int coordIndex,
261 GrVertexLayout vertexLayout);
262
263 /**
264 * Helper function to compute the size of each vertex and the offsets of
265 * texture coordinates and color. Determines tex coord offsets by tex coord
266 * index rather than by stage. (Each stage can be mapped to any t.c. index
267 * by StageTexCoordVertexLayoutBit.)
268 *
269 * @param vertexLayout the layout to query
270 * @param texCoordOffsetsByIdx after return it is the offset of each
271 * tex coord index in the vertex or -1 if
272 * index isn't used. (optional)
273 * @param colorOffset after return it is the offset of the
274 * color field in each vertex, or -1 if
275 * there aren't per-vertex colors. (optional)
276 * @param coverageOffset after return it is the offset of the
277 * coverage field in each vertex, or -1 if
278 * there aren't per-vertex coeverages.
279 * (optional)
280 * @param edgeOffset after return it is the offset of the
281 * edge eq field in each vertex, or -1 if
282 * there aren't per-vertex edge equations.
283 * (optional)
284 * @return size of a single vertex
285 */
286 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
287 int texCoordOffsetsByIdx[kMaxTexCoords],
288 int *colorOffset,
289 int *coverageOffset,
290 int* edgeOffset);
291
292 /**
293 * Helper function to compute the size of each vertex and the offsets of
294 * texture coordinates and color. Determines tex coord offsets by stage
295 * rather than by index. (Each stage can be mapped to any t.c. index
296 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
297 * tex coords then that stage's offset will be 0 (positions are always at 0).
298 *
299 * @param vertexLayout the layout to query
300 * @param texCoordOffsetsByStage after return it is the offset of each
301 * tex coord index in the vertex or -1 if
302 * index isn't used. (optional)
303 * @param colorOffset after return it is the offset of the
304 * color field in each vertex, or -1 if
305 * there aren't per-vertex colors.
306 * (optional)
307 * @param coverageOffset after return it is the offset of the
308 * coverage field in each vertex, or -1 if
309 * there aren't per-vertex coeverages.
310 * (optional)
311 * @param edgeOffset after return it is the offset of the
312 * edge eq field in each vertex, or -1 if
313 * there aren't per-vertex edge equations.
314 * (optional)
315 * @return size of a single vertex
316 */
317 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
318 int texCoordOffsetsByStage[kNumStages],
319 int* colorOffset,
320 int* coverageOffset,
321 int* edgeOffset);
322
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000323 /**
324 * Determines whether src alpha is guaranteed to be one for all src pixels
325 */
326 bool srcAlphaWillBeOne(GrVertexLayout) const;
327
328 /**
329 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
330 */
331 bool hasSolidCoverage(GrVertexLayout) const;
jvanverth@google.comcc782382013-01-28 20:39:48 +0000332
333 /**
334 * Accessing positions, texture coords, or colors, of a vertex within an
335 * array is a hassle involving casts and simple math. These helpers exist
336 * to keep GrDrawTarget clients' code a bit nicer looking.
337 */
338
339 /**
340 * Gets a pointer to a GrPoint of a vertex's position or texture
341 * coordinate.
342 * @param vertices the vetex array
343 * @param vertexIndex the index of the vertex in the array
344 * @param vertexSize the size of each vertex in the array
345 * @param offset the offset in bytes of the vertex component.
346 * Defaults to zero (corresponding to vertex position)
347 * @return pointer to the vertex component as a GrPoint
348 */
349 static GrPoint* GetVertexPoint(void* vertices,
350 int vertexIndex,
351 int vertexSize,
352 int offset = 0) {
353 intptr_t start = GrTCast<intptr_t>(vertices);
354 return GrTCast<GrPoint*>(start + offset +
355 vertexIndex * vertexSize);
356 }
357 static const GrPoint* GetVertexPoint(const void* vertices,
358 int vertexIndex,
359 int vertexSize,
360 int offset = 0) {
361 intptr_t start = GrTCast<intptr_t>(vertices);
362 return GrTCast<const GrPoint*>(start + offset +
363 vertexIndex * vertexSize);
364 }
365
366 /**
367 * Gets a pointer to a GrColor inside a vertex within a vertex array.
368 * @param vertices the vetex array
369 * @param vertexIndex the index of the vertex in the array
370 * @param vertexSize the size of each vertex in the array
371 * @param offset the offset in bytes of the vertex color
372 * @return pointer to the vertex component as a GrColor
373 */
374 static GrColor* GetVertexColor(void* vertices,
375 int vertexIndex,
376 int vertexSize,
377 int offset) {
378 intptr_t start = GrTCast<intptr_t>(vertices);
379 return GrTCast<GrColor*>(start + offset +
380 vertexIndex * vertexSize);
381 }
382 static const GrColor* GetVertexColor(const void* vertices,
383 int vertexIndex,
384 int vertexSize,
385 int offset) {
386 const intptr_t start = GrTCast<intptr_t>(vertices);
387 return GrTCast<const GrColor*>(start + offset +
388 vertexIndex * vertexSize);
389 }
390
391 static void VertexLayoutUnitTest();
392
393 /// @}
394
395 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000396 /// @name Color
397 ////
398
399 /**
400 * Sets color for next draw to a premultiplied-alpha color.
401 *
402 * @param color the color to set.
403 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000404 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000405
bsalomon@google.comca432082013-01-23 19:53:46 +0000406 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000407
408 /**
409 * Sets the color to be used for the next draw to be
410 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
411 *
412 * @param alpha The alpha value to set as the color.
413 */
414 void setAlpha(uint8_t a) {
415 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
416 }
417
418 /**
419 * Add a color filter that can be represented by a color and a mode. Applied
420 * after color-computing texture stages.
421 */
422 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000423 fCommon.fColorFilterColor = c;
424 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000425 }
426
bsalomon@google.comca432082013-01-23 19:53:46 +0000427 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
428 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000429
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000430 /**
431 * Constructor sets the color to be 'color' which is undone by the destructor.
432 */
433 class AutoColorRestore : public ::GrNoncopyable {
434 public:
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000435 AutoColorRestore() : fDrawState(NULL) {}
436
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000437 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000438 fDrawState = NULL;
439 this->set(drawState, color);
440 }
441
442 void reset() {
443 if (NULL != fDrawState) {
444 fDrawState->setColor(fOldColor);
445 fDrawState = NULL;
446 }
447 }
448
449 void set(GrDrawState* drawState, GrColor color) {
450 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000451 fDrawState = drawState;
452 fOldColor = fDrawState->getColor();
453 fDrawState->setColor(color);
454 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000455
456 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000457 private:
458 GrDrawState* fDrawState;
459 GrColor fOldColor;
460 };
461
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000462 /// @}
463
464 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000465 /// @name Coverage
466 ////
467
468 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000469 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000470 * initial value (after construction or reset()) is 0xff. The constant
471 * coverage is ignored when per-vertex coverage is provided.
472 */
473 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000474 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000475 }
476
477 /**
478 * Version of above that specifies 4 channel per-vertex color. The value
479 * should be premultiplied.
480 */
481 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000482 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000483 }
484
485 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000486 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000487 }
488
489 /// @}
490
491 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000492 /// @name Effect Stages
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000493 ////
494
bsalomon@google.comadc65362013-01-28 14:26:09 +0000495 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
496 fStages[stageIdx].setEffect(effect);
497 return effect;
498 }
499
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 /**
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000501 * Creates a GrSimpleTextureEffect.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000502 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000503 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000504 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000505 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000506 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000507 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000508 void createTextureEffect(int stageIdx,
509 GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000510 const SkMatrix& matrix,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000511 const GrTextureParams& params) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000512 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000513 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000514 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000515 }
516
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000517 bool stagesDisabled() {
518 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000519 if (NULL != fStages[i].getEffect()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000520 return false;
521 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000522 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000523 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000524 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000525
bsalomon@google.comadc65362013-01-28 14:26:09 +0000526 void disableStage(int stageIdx) { this->setEffect(stageIdx, NULL); }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000527
robertphillips@google.com972265d2012-06-13 18:49:30 +0000528 /**
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000529 * Release all the GrEffects referred to by this draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000530 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000531 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000532 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000533 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000534 }
535 }
536
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000537 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000538 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000539 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
540 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000541 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000542 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000543 }
544 }
545 private:
546 GrDrawState* fDrawState;
547 };
548
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000549 /**
bsalomon@google.com08283af2012-10-26 13:01:20 +0000550 * Returns the current stage by index.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000552 const GrEffectStage& getStage(int stageIdx) const {
553 GrAssert((unsigned)stageIdx < kNumStages);
554 return fStages[stageIdx];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000555 }
556
557 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000558 * Called when the source coord system is changing. preConcat gives the transformation from the
559 * old coord system to the new coord system.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000560 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000561 void preConcatStageMatrices(const SkMatrix& preConcat) {
bsalomon@google.comcabe00e2013-01-28 16:46:55 +0000562 this->preConcatStageMatrices(~0U, preConcat);
563 }
564 /**
565 * Version of above that applies the update matrix selectively to stages via a mask.
566 */
567 void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000568 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comcabe00e2013-01-28 16:46:55 +0000569 if (((1 << i) & stageMask) && this->isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000570 fStages[i].preConcatCoordChange(preConcat);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000571 }
572 }
573 }
574
bsalomon@google.come3d32162012-07-20 13:37:06 +0000575 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000576 * Called when the source coord system is changing. preConcatInverse is the inverse of the
577 * transformation from the old coord system to the new coord system. Returns false if the matrix
578 * cannot be inverted.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000579 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000580 bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
581 SkMatrix inv;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000582 bool computed = false;
583 for (int i = 0; i < kNumStages; ++i) {
584 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000585 if (!computed && !preConcatInverse.invert(&inv)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000586 return false;
587 } else {
588 computed = true;
589 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000590 fStages[i].preConcatCoordChange(preConcatInverse);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000591 }
592 }
593 return true;
594 }
595
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000596 /// @}
597
598 ///////////////////////////////////////////////////////////////////////////
599 /// @name Coverage / Color Stages
600 ////
601
602 /**
603 * A common pattern is to compute a color with the initial stages and then
604 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000605 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
606 * computed based on the pre-coverage-modulated color. The division of
607 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608 * this method. Initially this is kNumStages (all stages
609 * are color-computing).
610 */
611 void setFirstCoverageStage(int firstCoverageStage) {
612 GrAssert((unsigned)firstCoverageStage <= kNumStages);
bsalomon@google.comca432082013-01-23 19:53:46 +0000613 fCommon.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 }
615
616 /**
617 * Gets the index of the first coverage-computing stage.
618 */
619 int getFirstCoverageStage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000620 return fCommon.fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000621 }
622
623 ///@}
624
625 ///////////////////////////////////////////////////////////////////////////
626 /// @name Blending
627 ////
628
629 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000630 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000631 *
632 * The blend function will be:
633 * D' = sat(S*srcCoef + D*dstCoef)
634 *
635 * where D is the existing destination color, S is the incoming source
636 * color, and D' is the new destination color that will be written. sat()
637 * is the saturation function.
638 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000639 * @param srcCoef coefficient applied to the src color.
640 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000641 */
642 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000643 fCommon.fSrcBlend = srcCoeff;
644 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000645 #if GR_DEBUG
646 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000647 case kDC_GrBlendCoeff:
648 case kIDC_GrBlendCoeff:
649 case kDA_GrBlendCoeff:
650 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000651 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
652 "coverage stages.\n");
653 break;
654 default:
655 break;
656 }
657 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000658 case kSC_GrBlendCoeff:
659 case kISC_GrBlendCoeff:
660 case kSA_GrBlendCoeff:
661 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000662 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
663 "coverage stages.\n");
664 break;
665 default:
666 break;
667 }
668 #endif
669 }
670
bsalomon@google.comca432082013-01-23 19:53:46 +0000671 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
672 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000673
674 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
675 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000676 *srcBlendCoeff = fCommon.fSrcBlend;
677 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 }
679
680 /**
681 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000682 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000683 * kConstC_GrBlendCoeff
684 * kIConstC_GrBlendCoeff
685 * kConstA_GrBlendCoeff
686 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000687 *
688 * @param constant the constant to set
689 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000690 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691
692 /**
693 * Retrieves the last value set by setBlendConstant()
694 * @return the blending constant value
695 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000696 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000697
698 /// @}
699
700 ///////////////////////////////////////////////////////////////////////////
701 /// @name View Matrix
702 ////
703
704 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000705 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 *
707 * In the post-view-matrix space the rectangle [0,w]x[0,h]
708 * fully covers the render target. (w and h are the width and height of the
bsalomon@google.comca432082013-01-23 19:53:46 +0000709 * the render-target.)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000710 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000711 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000712
713 /**
714 * Gets a writable pointer to the view matrix.
715 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000716 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000717
718 /**
719 * Multiplies the current view matrix by a matrix
720 *
721 * After this call V' = V*m where V is the old view matrix,
722 * m is the parameter to this function, and V' is the new view matrix.
723 * (We consider positions to be column vectors so position vector p is
724 * transformed by matrix X as p' = X*p.)
725 *
726 * @param m the matrix used to modify the view matrix.
727 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000728 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000729
730 /**
731 * Multiplies the current view matrix by a matrix
732 *
733 * After this call V' = m*V where V is the old view matrix,
734 * m is the parameter to this function, and V' is the new view matrix.
735 * (We consider positions to be column vectors so position vector p is
736 * transformed by matrix X as p' = X*p.)
737 *
738 * @param m the matrix used to modify the view matrix.
739 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000740 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000741
742 /**
743 * Retrieves the current view matrix
744 * @return the current view matrix.
745 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000746 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000747
748 /**
749 * Retrieves the inverse of the current view matrix.
750 *
751 * If the current view matrix is invertible, return true, and if matrix
752 * is non-null, copy the inverse into it. If the current view matrix is
753 * non-invertible, return false and ignore the matrix parameter.
754 *
755 * @param matrix if not null, will receive a copy of the current inverse.
756 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000757 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000758 // TODO: determine whether we really need to leave matrix unmodified
759 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000760 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000761 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762 if (matrix) {
763 *matrix = inverse;
764 }
765 return true;
766 }
767 return false;
768 }
769
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000770 ////////////////////////////////////////////////////////////////////////////
771
772 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000773 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.comc196b522012-10-25 21:52:43 +0000774 * Effect matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000775 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000776 class AutoViewMatrixRestore : public ::GrNoncopyable {
777 public:
778 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000779
780 AutoViewMatrixRestore(GrDrawState* ds,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000781 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000782 uint32_t explicitCoordStageMask = 0) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000783 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000784 this->set(ds, preconcatMatrix, explicitCoordStageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000785 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000786
787 ~AutoViewMatrixRestore() { this->restore(); }
788
bsalomon@google.coma8347462012-10-08 18:59:39 +0000789 /**
790 * Can be called prior to destructor to restore the original matrix.
791 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000792 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000793
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000794 void set(GrDrawState* drawState,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000795 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000796 uint32_t explicitCoordStageMask = 0);
797
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000798 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000799
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000801 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000802 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000803 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000804 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000805 };
806
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000807 ////////////////////////////////////////////////////////////////////////////
808
809 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000810 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
811 * destructor undoes the changes, restoring the view matrix that was set before the
812 * constructor. It is similar to passing the inverse of the current view matrix to
813 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000814 */
815 class AutoDeviceCoordDraw : ::GrNoncopyable {
816 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000817 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000818 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000819 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
820 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
821 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000822 */
823 AutoDeviceCoordDraw(GrDrawState* drawState,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000824 uint32_t explicitCoordStageMask = 0) {
825 fDrawState = NULL;
826 this->set(drawState, explicitCoordStageMask);
827 }
828
bsalomon@google.coma8347462012-10-08 18:59:39 +0000829 ~AutoDeviceCoordDraw() { this->restore(); }
830
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000831 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
832
bsalomon@google.coma8347462012-10-08 18:59:39 +0000833 /**
834 * Returns true if this object was successfully initialized on to a GrDrawState. It may
835 * return false because a non-default constructor or set() were never called or because
836 * the view matrix was not invertible.
837 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000838 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000839
bsalomon@google.coma8347462012-10-08 18:59:39 +0000840 /**
841 * Returns the matrix that was set previously set on the drawState. This is only valid
842 * if succeeded returns true.
843 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000844 const SkMatrix& getOriginalMatrix() const {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000845 GrAssert(this->succeeded());
846 return fViewMatrix;
847 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000848
bsalomon@google.coma8347462012-10-08 18:59:39 +0000849 /**
850 * Can be called prior to destructor to restore the original matrix.
851 */
852 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000853
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000854 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000855 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000856 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000857 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000858 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000859 };
860
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 /// @}
862
863 ///////////////////////////////////////////////////////////////////////////
864 /// @name Render Target
865 ////
866
867 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000868 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000869 *
870 * @param target The render target to set.
871 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000872 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000873 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000874 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000875
876 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000877 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 *
879 * @return The currently set render target.
880 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000881 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
882 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000883
884 class AutoRenderTargetRestore : public ::GrNoncopyable {
885 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000886 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000887 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
888 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000889 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000890 this->set(ds, newTarget);
891 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000892 ~AutoRenderTargetRestore() { this->restore(); }
893
894 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000895 if (NULL != fDrawState) {
896 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000897 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000898 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000899 GrSafeSetNull(fSavedTarget);
900 }
901
902 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
903 this->restore();
904
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000905 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000906 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000907 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000908 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000909 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000910 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000911 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000912 }
913 private:
914 GrDrawState* fDrawState;
915 GrRenderTarget* fSavedTarget;
916 };
917
918 /// @}
919
920 ///////////////////////////////////////////////////////////////////////////
921 /// @name Stencil
922 ////
923
924 /**
925 * Sets the stencil settings to use for the next draw.
926 * Changing the clip has the side-effect of possibly zeroing
927 * out the client settable stencil bits. So multipass algorithms
928 * using stencil should not change the clip between passes.
929 * @param settings the stencil settings to use.
930 */
931 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000932 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000933 }
934
935 /**
936 * Shortcut to disable stencil testing and ops.
937 */
938 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000939 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000940 }
941
bsalomon@google.comca432082013-01-23 19:53:46 +0000942 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000943
bsalomon@google.comca432082013-01-23 19:53:46 +0000944 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000945
946 /// @}
947
948 ///////////////////////////////////////////////////////////////////////////
949 // @name Edge AA
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000950 // Edge equations can be specified to perform anti-aliasing. Because the
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000951 // edges are specified as per-vertex data, vertices that are shared by
952 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000953 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000954 ////
955
956 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000957 * When specifying edges as vertex data this enum specifies what type of
bsalomon@google.com81712882012-11-01 17:12:34 +0000958 * edges are in use. The edges are always 4 SkScalars in memory, even when
tomhudson@google.com93813632011-10-27 20:21:16 +0000959 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000960 *
961 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
962 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000963 */
964 enum VertexEdgeType {
965 /* 1-pixel wide line
966 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
967 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000968 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000969 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000970 being inside, positive outside. Edge specified in window space
971 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000972 kQuad_EdgeType,
973 /* Same as above but for hairline quadratics. Uses unsigned distance.
974 Coverage is min(0, 1-distance). */
975 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000976 /* Circle specified as center_x, center_y, outer_radius, inner_radius
977 all in window space (y-down). */
978 kCircle_EdgeType,
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000979 /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius
980 all in window space (y-down). */
981 kEllipse_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000982
983 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000984 };
985
986 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000987 * Determines the interpretation per-vertex edge data when the
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000988 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
989 * are not specified the value of this setting has no effect.
990 */
991 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000992 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.comca432082013-01-23 19:53:46 +0000993 fCommon.fVertexEdgeType = type;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000994 }
995
bsalomon@google.comca432082013-01-23 19:53:46 +0000996 VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000997
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000998 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000999
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001000 ///////////////////////////////////////////////////////////////////////////
1001 /// @name State Flags
1002 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +00001003
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001004 /**
1005 * Flags that affect rendering. Controlled using enable/disableState(). All
1006 * default to disabled.
1007 */
1008 enum StateBits {
1009 /**
1010 * Perform dithering. TODO: Re-evaluate whether we need this bit
1011 */
1012 kDither_StateBit = 0x01,
1013 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001014 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
1015 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
1016 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001017 */
1018 kHWAntialias_StateBit = 0x02,
1019 /**
1020 * Draws will respect the clip, otherwise the clip is ignored.
1021 */
1022 kClip_StateBit = 0x04,
1023 /**
1024 * Disables writing to the color buffer. Useful when performing stencil
1025 * operations.
1026 */
1027 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001028
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001029 /**
1030 * Usually coverage is applied after color blending. The color is blended using the coeffs
1031 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
1032 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
1033 * this case there is no distinction between coverage and color and the caller needs direct
1034 * control over the blend coeffs. When set, there will be a single blend step controlled by
1035 * setBlendFunc() which will use coverage*color as the src color.
1036 */
1037 kCoverageDrawing_StateBit = 0x10,
1038
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001039 // Users of the class may add additional bits to the vector
1040 kDummyStateBit,
1041 kLastPublicStateBit = kDummyStateBit-1,
1042 };
1043
1044 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +00001045 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001046 }
1047
1048 /**
1049 * Enable render state settings.
1050 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001051 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001052 */
1053 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001054 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001055 }
1056
1057 /**
1058 * Disable render state settings.
1059 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001060 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001061 */
1062 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001063 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001064 }
1065
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +00001066 /**
1067 * Enable or disable stateBits based on a boolean.
1068 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001069 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +00001070 * @param enable if true enable stateBits, otherwise disable
1071 */
1072 void setState(uint32_t stateBits, bool enable) {
1073 if (enable) {
1074 this->enableState(stateBits);
1075 } else {
1076 this->disableState(stateBits);
1077 }
1078 }
1079
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001080 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001081 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001082 }
1083
1084 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001085 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001086 }
1087
1088 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001089 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001090 }
1091
1092 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001093 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001094 }
1095
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001096 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001097 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001098 }
1099
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001100 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001101 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001102 }
1103
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001104 /// @}
1105
1106 ///////////////////////////////////////////////////////////////////////////
1107 /// @name Face Culling
1108 ////
1109
1110 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001111 kInvalid_DrawFace = -1,
1112
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001113 kBoth_DrawFace,
1114 kCCW_DrawFace,
1115 kCW_DrawFace,
1116 };
1117
1118 /**
1119 * Controls whether clockwise, counterclockwise, or both faces are drawn.
1120 * @param face the face(s) to draw.
1121 */
1122 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001123 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +00001124 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001125 }
1126
1127 /**
1128 * Gets whether the target is drawing clockwise, counterclockwise,
1129 * or both faces.
1130 * @return the current draw face(s).
1131 */
bsalomon@google.comca432082013-01-23 19:53:46 +00001132 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001133
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001134 /// @}
1135
1136 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +00001137
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001138 bool isStageEnabled(int s) const {
1139 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com08283af2012-10-26 13:01:20 +00001140 return (NULL != fStages[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001141 }
1142
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001143 // Most stages are usually not used, so conditionals here
1144 // reduce the expected number of bytes touched by 50%.
1145 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001146 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001147 return false;
1148 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001149
1150 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +00001151 bool enabled = this->isStageEnabled(i);
1152 if (enabled != s.isStageEnabled(i)) {
1153 return false;
1154 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001155 if (enabled && this->fStages[i] != s.fStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001156 return false;
1157 }
1158 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001159 return true;
1160 }
1161 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
1162
bsalomon@google.comca432082013-01-23 19:53:46 +00001163 GrDrawState& operator= (const GrDrawState& s) {
1164 this->setRenderTarget(s.fRenderTarget.get());
1165 fCommon = s.fCommon;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001166 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +00001167 if (s.isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001168 this->fStages[i] = s.fStages[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001169 }
1170 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001171 return *this;
1172 }
1173
1174private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +00001175
bsalomon@google.comca432082013-01-23 19:53:46 +00001176 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
1177 struct CommonState {
1178 // These fields are roughly sorted by decreasing likelihood of being different in op==
1179 GrColor fColor;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001180 GrVertexLayout fVertexLayout;
bsalomon@google.comca432082013-01-23 19:53:46 +00001181 SkMatrix fViewMatrix;
1182 GrBlendCoeff fSrcBlend;
1183 GrBlendCoeff fDstBlend;
1184 GrColor fBlendConstant;
1185 uint32_t fFlagBits;
1186 VertexEdgeType fVertexEdgeType;
1187 GrStencilSettings fStencilSettings;
1188 int fFirstCoverageStage;
1189 GrColor fCoverage;
1190 SkXfermode::Mode fColorFilterMode;
1191 GrColor fColorFilterColor;
1192 DrawFace fDrawFace;
1193 bool operator== (const CommonState& other) const {
1194 return fColor == other.fColor &&
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001195 fVertexLayout == other.fVertexLayout &&
bsalomon@google.comca432082013-01-23 19:53:46 +00001196 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
1197 fSrcBlend == other.fSrcBlend &&
1198 fDstBlend == other.fDstBlend &&
1199 fBlendConstant == other.fBlendConstant &&
1200 fFlagBits == other.fFlagBits &&
1201 fVertexEdgeType == other.fVertexEdgeType &&
1202 fStencilSettings == other.fStencilSettings &&
1203 fFirstCoverageStage == other.fFirstCoverageStage &&
1204 fCoverage == other.fCoverage &&
1205 fColorFilterMode == other.fColorFilterMode &&
1206 fColorFilterColor == other.fColorFilterColor &&
1207 fDrawFace == other.fDrawFace;
1208 }
1209 bool operator!= (const CommonState& other) const { return !(*this == other); }
1210 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001211
bsalomon@google.comca432082013-01-23 19:53:46 +00001212 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
1213 DeferredState must directly reference GrEffects, however. */
1214 struct SavedEffectStage {
1215 SavedEffectStage() : fEffect(NULL) {}
1216 const GrEffect* fEffect;
1217 GrEffectStage::SavedCoordChange fCoordChange;
1218 };
1219
1220public:
1221 /**
1222 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
1223 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
1224 * dispose mechanism returns them to the cache. This allows recycling resources through the
1225 * the cache while they are in a deferred draw queue.
1226 */
1227 class DeferredState {
1228 public:
1229 DeferredState() : fRenderTarget(NULL) {
1230 GR_DEBUGCODE(fInitialized = false;)
1231 }
1232 // TODO: Remove this when DeferredState no longer holds a ref to the RT
1233 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1234
1235 void saveFrom(const GrDrawState& drawState) {
1236 fCommon = drawState.fCommon;
1237 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1238 fRenderTarget = drawState.fRenderTarget.get();
1239 SkSafeRef(fRenderTarget);
1240 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1241 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1242 // and recycle them to the cache (if no one else is holding a ref to the resources).
1243 for (int i = 0; i < kNumStages; ++i) {
1244 fStages[i].saveFrom(drawState.fStages[i]);
1245 }
1246 GR_DEBUGCODE(fInitialized = true;)
1247 }
1248
1249 void restoreTo(GrDrawState* drawState) {
1250 GrAssert(fInitialized);
1251 drawState->fCommon = fCommon;
1252 drawState->setRenderTarget(fRenderTarget);
1253 for (int i = 0; i < kNumStages; ++i) {
1254 fStages[i].restoreTo(&drawState->fStages[i]);
1255 }
1256 }
1257
1258 bool isEqual(const GrDrawState& state) const {
1259 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
1260 return false;
1261 }
1262 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comdcd69bf2013-01-24 18:28:51 +00001263 if (!fStages[i].isEqual(state.fStages[i])) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001264 return false;
1265 }
1266 }
1267 return true;
1268 }
1269
1270 private:
1271 GrRenderTarget* fRenderTarget;
1272 CommonState fCommon;
1273 GrEffectStage::DeferredStage fStages[kNumStages];
1274
1275 GR_DEBUGCODE(bool fInitialized;)
1276 };
1277
1278private:
1279 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1280 CommonState fCommon;
1281 GrEffectStage fStages[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001282
reed@google.comfa35e3d2012-06-26 20:16:17 +00001283 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001284};
1285
1286#endif