blob: e4113384317e98063b649246a1bd7a2188190a04 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrDrawTarget_DEFINED
12#define GrDrawTarget_DEFINED
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrClip.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrColor.h"
16#include "GrMatrix.h"
17#include "GrRefCnt.h"
18#include "GrRenderTarget.h"
19#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000020#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
Scroggo97c88c22011-05-11 14:05:25 +000023#include "SkXfermode.h"
24
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrClipIterator;
27class GrVertexBuffer;
28class GrIndexBuffer;
29
30class GrDrawTarget : public GrRefCnt {
31public:
32 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033 * Represents the draw target capabilities.
34 */
35 struct Caps {
36 Caps() { memset(this, 0, sizeof(Caps)); }
37 Caps(const Caps& c) { *this = c; }
38 Caps& operator= (const Caps& c) {
39 memcpy(this, &c, sizeof(Caps));
40 return *this;
41 }
42 void print() const;
43 bool f8BitPaletteSupport : 1;
44 bool fNPOTTextureSupport : 1;
45 bool fNPOTTextureTileSupport : 1;
46 bool fNPOTRenderTargetSupport : 1;
47 bool fTwoSidedStencilSupport : 1;
48 bool fStencilWrapOpsSupport : 1;
49 bool fHWAALineSupport : 1;
50 bool fShaderSupport : 1;
51 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000052 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000053 bool fFSAASupport : 1;
54 bool fDualSourceBlendingSupport : 1;
55 bool fBufferLockSupport : 1;
bsalomon@google.coma3108262011-10-10 14:08:47 +000056 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000057 int fMinRenderTargetWidth;
58 int fMinRenderTargetHeight;
59 int fMaxRenderTargetSize;
60 int fMaxTextureSize;
61 };
62
63 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000064 * Number of texture stages. Each stage takes as input a color and
65 * 2D texture coordinates. The color input to the first enabled stage is the
66 * per-vertex color or the constant color (setColor/setAlpha) if there are
67 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000068 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000069 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000070 * lookups are specified by a texture a sampler (setSamplerState). Texture
71 * coordinates for each stage come from the vertices based on a
72 * GrVertexLayout bitfield. The output fragment color is the output color of
73 * the last enabled stage. The presence or absence of texture coordinates
74 * for each stage in the vertex layout indicates whether a stage is enabled
75 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000076 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000077 enum {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000078 kNumStages = 3,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000079 kMaxTexCoords = kNumStages
80 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000082 /**
83 * The absolute maximum number of edges that may be specified for
84 * a single draw call when performing edge antialiasing. This is used for
85 * the size of several static buffers, so implementations of getMaxEdges()
86 * (below) should clamp to this value.
87 */
88 enum {
89 kMaxEdges = 32
90 };
91
bsalomon@google.comaeb21602011-08-30 18:13:44 +000092 /**
93 * When specifying edges as vertex data this enum specifies what type of
94 * edges are in use. The edges are always 4 GrScalars in memory, even when
95 * the edge type requires fewer than 4.
96 */
97 enum VertexEdgeType {
98 /* 1-pixel wide line
99 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
100 kHairLine_EdgeType,
101 /* 1-pixel wide quadratic
102 u^2-v canonical coords (only 2 components used) */
103 kHairQuad_EdgeType
104 };
105
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000106 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000107 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000109 typedef int StageBitfield;
110 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 /**
113 * Flags that affect rendering. Controlled using enable/disableState(). All
114 * default to disabled.
115 */
116 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000117 kDither_StateBit = 0x01, //<! Perform color dithering
118 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 // target must support some form of AA
120 // (msaa, coverage sampling, etc). For
121 // GrGpu-created rendertarget/textures
122 // this is controlled by parameters
123 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000124 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 // against the region specified by
126 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000127 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
128 // Useful while performing stencil
129 // ops.
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000130 kEdgeAAConcave_StateBit = 0x10,//<! If set, edge AA will test edge
131 // pairs for convexity while
132 // rasterizing. Set this if the
133 // source polygon is non-convex.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134
135 // subclass may use additional bits internally
136 kDummyStateBit,
137 kLastPublicStateBit = kDummyStateBit-1
138 };
139
140 enum DrawFace {
141 kBoth_DrawFace,
142 kCCW_DrawFace,
143 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 };
145
146 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000147 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000148 * Changing the clip has the side-effect of possibly zeroing
149 * out the client settable stencil bits. So multipass algorithms
150 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000151 * @param settings the stencil settings to use.
152 */
153 void setStencil(const GrStencilSettings& settings) {
154 fCurrDrawState.fStencilSettings = settings;
155 }
156
157 /**
158 * Shortcut to disable stencil testing and ops.
159 */
160 void disableStencil() {
161 fCurrDrawState.fStencilSettings.setDisabled();
162 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000163
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000164 class Edge {
165 public:
166 Edge() {}
167 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
168 GrPoint intersect(const Edge& other) {
169 return GrPoint::Make(
170 (fY * other.fZ - other.fY * fZ) /
171 (fX * other.fY - other.fX * fY),
172 (fX * other.fZ - other.fX * fZ) /
173 (other.fX * fY - fX * other.fY));
174 }
175 float fX, fY, fZ;
176 };
177
reed@google.comac10a2d2010-12-22 21:39:39 +0000178protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
reed@google.com8195f672011-01-12 18:14:28 +0000180 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000181 DrState() {
182 // make sure any pad is zero for memcmp
183 // all DrState members should default to something
184 // valid by the memset
185 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000186
187 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000188 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000189 fFirstCoverageStage = kNumStages;
190
191 // pedantic assertion that our ptrs will
192 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000193 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000194
195 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000196 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000197 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000198 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000200 GrBlendCoeff fSrcBlend;
201 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000202 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000203 GrTexture* fTextures[kNumStages];
204 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000205 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000206 GrRenderTarget* fRenderTarget;
207 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000208 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000209 GrColor fColorFilterColor;
210 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000211
212 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213 GrMatrix fViewMatrix;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214 VertexEdgeType fVertexEdgeType;
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000215 Edge fEdgeAAEdges[kMaxEdges];
216 int fEdgeAANumEdges;
reed@google.com8195f672011-01-12 18:14:28 +0000217 bool operator ==(const DrState& s) const {
218 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 }
reed@google.com8195f672011-01-12 18:14:28 +0000220 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 };
222
223public:
224 ///////////////////////////////////////////////////////////////////////////
225
226 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000227 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
229 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 * Gets the capabilities of the draw target.
231 */
232 const Caps& getCaps() const { return fCaps; }
233
234 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 * Sets the current clip to the region specified by clip. All draws will be
236 * clipped against this clip if kClip_StateBit is enabled.
237 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000238 * Setting the clip may (or may not) zero out the client's stencil bits.
239 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 * @param description of the clipping region
241 */
242 void setClip(const GrClip& clip);
243
244 /**
245 * Gets the current clip.
246 *
247 * @return the clip.
248 */
249 const GrClip& getClip() const;
250
251 /**
252 * Sets the texture used at the next drawing call
253 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000254 * @param stage The texture stage for which the texture will be set
255 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 * @param texture The texture to set. Can be NULL though there is no advantage
257 * to settings a NULL texture if doing non-textured drawing
258 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000259 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
261 /**
262 * Retrieves the currently set texture.
263 *
264 * @return The currently set texture. The return value will be NULL if no
265 * texture has been set, NULL was most recently passed to
266 * setTexture, or the last setTexture was destroyed.
267 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 const GrTexture* getTexture(int stage) const;
269 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 /**
272 * Sets the rendertarget used at the next drawing call
273 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 */
276 void setRenderTarget(GrRenderTarget* target);
277
278 /**
279 * Retrieves the currently set rendertarget.
280 *
281 * @return The currently set render target.
282 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000283 const GrRenderTarget* getRenderTarget() const;
284 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000285
286 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000287 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000289 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000290 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000292 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 * @param samplerState Specifies the sampler state.
294 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000296
297 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000298 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000300 * @param stage the stage of the sampler to set
301 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000303 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
304 GrAssert(stage >= 0 && stage < kNumStages);
305 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000306 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
308 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000310 * matrix
311 */
312 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
313 for (int i = 0; i < kNumStages; ++i) {
314 if ((1 << i) & stageMask) {
315 this->preConcatSamplerMatrix(i, matrix);
316 }
317 }
318 }
319
320 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000321 * Shortcut for preConcatSamplerMatrix on all enabled stages in mask with
322 * same matrix
323 *
324 * @param stage the stage of the sampler to set
325 * @param matrix the matrix to concat
326 */
327 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
328 StageBitfield stageMask = this->enabledStages();
329 this->preConcatSamplerMatrices(stageMask, matrix);
330 }
331
332 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000333 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000334 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000335 * @param stage the stage to of sampler to get
336 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000337 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000338 const GrMatrix& getSamplerMatrix(int stage) const {
339 return fCurrDrawState.fSamplerStates[stage].getMatrix();
340 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000341
342 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000343 * Sets the matrix of a stage's sampler
344 *
345 * @param stage the stage of sampler set
346 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000347 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000348 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000349 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
350 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000351
352 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 * Sets the matrix applied to veretx positions.
354 *
355 * In the post-view-matrix space the rectangle [0,w]x[0,h]
356 * fully covers the render target. (w and h are the width and height of the
357 * the rendertarget.)
358 *
359 * @param m the matrix used to transform the vertex positions.
360 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000361 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
363 /**
364 * Multiplies the current view matrix by a matrix
365 *
366 * After this call V' = V*m where V is the old view matrix,
367 * m is the parameter to this function, and V' is the new view matrix.
368 * (We consider positions to be column vectors so position vector p is
369 * transformed by matrix X as p' = X*p.)
370 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000371 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000373 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000374
375 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000376 * Multiplies the current view matrix by a matrix
377 *
378 * After this call V' = m*V where V is the old view matrix,
379 * m is the parameter to this function, and V' is the new view matrix.
380 * (We consider positions to be column vectors so position vector p is
381 * transformed by matrix X as p' = X*p.)
382 *
383 * @param m the matrix used to modify the view matrix.
384 */
385 void postConcatViewMatrix(const GrMatrix& m);
386
387 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000388 * Retrieves the current view matrix
389 * @return the current view matrix.
390 */
391 const GrMatrix& getViewMatrix() const;
392
393 /**
394 * Retrieves the inverse of the current view matrix.
395 *
396 * If the current view matrix is invertible, return true, and if matrix
397 * is non-null, copy the inverse into it. If the current view matrix is
398 * non-invertible, return false and ignore the matrix parameter.
399 *
400 * @param matrix if not null, will receive a copy of the current inverse.
401 */
402 bool getViewInverse(GrMatrix* matrix) const;
403
404 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 * Sets color for next draw to a premultiplied-alpha color.
406 *
407 * @param the color to set.
408 */
409 void setColor(GrColor);
410
411 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000412 * Gets the currently set color.
413 * @return the current color.
414 */
415 GrColor getColor() const { return fCurrDrawState.fColor; }
416
417 /**
Scroggo97c88c22011-05-11 14:05:25 +0000418 * Add a color filter that can be represented by a color and a mode.
419 */
420 void setColorFilter(GrColor, SkXfermode::Mode);
421
422 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 * Sets the color to be used for the next draw to be
424 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
425 *
426 * @param alpha The alpha value to set as the color.
427 */
428 void setAlpha(uint8_t alpha);
429
430 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000431 * Controls whether clockwise, counterclockwise, or both faces are drawn.
432 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000434 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
436 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000437 * A common pattern is to compute a color with the initial stages and then
438 * modulate that color by a coverage value in later stage(s) (AA, mask-
439 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
440 * computed based on the pre-coverage-modulated color. The division of
441 * stages between color-computing and coverage-computing is specified by
442 * this method. Initially this is kNumStages (all stages are color-
443 * computing).
444 */
445 void setFirstCoverageStage(int firstCoverageStage) {
446 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
447 }
448
449 /**
450 * Gets the index of the first coverage-computing stage.
451 */
452 int getFirstCoverageStage() const {
453 return fCurrDrawState.fFirstCoverageStage;
454 }
455
456 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000457 * Gets whether the target is drawing clockwise, counterclockwise,
458 * or both faces.
459 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000461 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
463 /**
464 * Enable render state settings.
465 *
466 * @param flags bitfield of StateBits specifing the states to enable
467 */
468 void enableState(uint32_t stateBits);
469
470 /**
471 * Disable render state settings.
472 *
473 * @param flags bitfield of StateBits specifing the states to disable
474 */
475 void disableState(uint32_t stateBits);
476
477 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000478 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
479 }
480
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000481 bool isAntialiasState() const {
482 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
483 }
484
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000485 bool isClipState() const {
486 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 }
488
bsalomon@google.comd302f142011-03-03 13:54:13 +0000489 bool isColorWriteDisabled() const {
490 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
491 }
492
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 * Sets the blending function coeffecients.
495 *
496 * The blend function will be:
497 * D' = sat(S*srcCoef + D*dstCoef)
498 *
499 * where D is the existing destination color, S is the incoming source
500 * color, and D' is the new destination color that will be written. sat()
501 * is the saturation function.
502 *
503 * @param srcCoef coeffecient applied to the src color.
504 * @param dstCoef coeffecient applied to the dst color.
505 */
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000506 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
reed@google.comac10a2d2010-12-22 21:39:39 +0000507
508 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000509 * Sets the blending function constant referenced by the following blending
510 * coeffecients:
511 * kConstC_BlendCoeff
512 * kIConstC_BlendCoeff
513 * kConstA_BlendCoeff
514 * kIConstA_BlendCoeff
515 *
516 * @param constant the constant to set
517 */
518 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
519
520 /**
521 * Retrieves the last value set by setBlendConstant()
522 * @return the blending constant value
523 */
524 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
525
526 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000527 * Determines if blend is effectively disabled.
528 *
529 * @return true if blend can be disabled without changing the rendering
530 * result given the current state including the vertex layout specified
531 * with the vertex source.
532 */
533 bool canDisableBlend() const;
534
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000535 /**
536 * Color alpha and coverage are two inputs to the drawing pipeline. For some
537 * blend modes it is safe to fold the coverage into constant or per-vertex
538 * color alpha value. For other blend modes they must be handled separately.
539 * Depending on features available in the underlying 3D API this may or may
540 * not be possible.
541 *
542 * This function looks at the current blend on the draw target and the draw
543 * target's capabilities to determine whether coverage can be handled
544 * correctly.
545 */
546 bool canApplyCoverage() const;
547
548 /**
549 * Determines whether incorporating partial pixel coverage into the constant
550 * color specified by setColor or per-vertex colors will give the right
551 * blending result.
552 */
553 bool canTweakAlphaForCoverage() const {
554 return CanTweakAlphaForCoverage(fCurrDrawState.fDstBlend);
555 }
556
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000557 /**
558 * Determines the interpretation per-vertex edge data when the
559 * kEdge_VertexLayoutBit is set (see below). When per-vertex edges are not
560 * specified the value of this setting has no effect.
561 */
562 void setVertexEdgeType(VertexEdgeType type) {
563 fCurrDrawState.fVertexEdgeType = type;
564 }
565
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000566 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000567 * Given the current draw state, vertex layout, and hw support, will HW AA
568 * lines be used (if line primitive type is drawn)? (Note that lines are
569 * always 1 pixel wide)
570 */
571 virtual bool willUseHWAALines() const = 0;
572
573 /**
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000574 * Sets the edge data required for edge antialiasing.
575 *
576 * @param edges 3 * 6 float values, representing the edge
577 * equations in Ax + By + C form
578 */
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000579 void setEdgeAAData(const Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000580
bsalomon@google.coma3108262011-10-10 14:08:47 +0000581 /**
582 * Used to save and restore the GrGpu's drawing state
583 */
584 struct SavedDrawState {
585 private:
586 DrState fState;
587 friend class GrDrawTarget;
588 };
589
590 /**
591 * Saves the current draw state. The state can be restored at a later time
592 * with restoreDrawState.
593 *
594 * See also AutoStateRestore class.
595 *
596 * @param state will hold the state after the function returns.
597 */
598 void saveCurrentDrawState(SavedDrawState* state) const;
599
600 /**
601 * Restores previously saved draw state. The client guarantees that state
602 * was previously passed to saveCurrentDrawState and that the rendertarget
603 * and texture set at save are still valid.
604 *
605 * See also AutoStateRestore class.
606 *
607 * @param state the previously saved state to restore.
608 */
609 void restoreDrawState(const SavedDrawState& state);
610
611 /**
612 * Copies the draw state from another target to this target.
613 *
614 * @param srcTarget draw target used as src of the draw state.
615 */
616 void copyDrawState(const GrDrawTarget& srcTarget);
617
618 /**
619 * The format of vertices is represented as a bitfield of flags.
620 * Flags that indicate the layout of vertex data. Vertices always contain
621 * positions and may also contain up to kMaxTexCoords sets of 2D texture
622 * coordinates, per-vertex colors, and per-vertex coverage. Each stage can
623 * use any of the texture coordinates as its input texture coordinates or it
624 * may use the positions as texture coordinates.
625 *
626 * If no texture coordinates are specified for a stage then the stage is
627 * disabled.
628 *
629 * Only one type of texture coord can be specified per stage. For
630 * example StageTexCoordVertexLayoutBit(0, 2) and
631 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
632 *
633 * The order in memory is always (position, texture coord 0, ..., color,
634 * coverage) with any unused fields omitted. Note that this means that if
635 * only texture coordinates 1 is referenced then there is no texture
636 * coordinates 0 and the order would be (position, texture coordinate 1
637 * [, color][, coverage]).
638 */
639
640 /**
641 * Generates a bit indicating that a texture stage uses texture coordinates
642 *
643 * @param stage the stage that will use texture coordinates.
644 * @param texCoordIdx the index of the texture coordinates to use
645 *
646 * @return the bit to add to a GrVertexLayout bitfield.
647 */
648 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
649 GrAssert(stage < kNumStages);
650 GrAssert(texCoordIdx < kMaxTexCoords);
651 return 1 << (stage + (texCoordIdx * kNumStages));
652 }
653
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000654private:
655 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
656public:
657 /**
658 * Generates a bit indicating that a texture stage uses the position
659 * as its texture coordinate.
660 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000661 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000662 * coordinates.
663 *
664 * @return the bit to add to a GrVertexLayout bitfield.
665 */
666 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
667 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000668 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000669 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000670
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000671private:
672 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000673
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000674public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000675
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000676 /**
677 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000678 */
679 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000680 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000681 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000682 /* vertices have coverage (GrColor where all channels should have the
683 * same value)
684 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000686 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000687 * text [GrGpuTextVertex vs GrPoint].)
688 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000689 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000690
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000691 /* Each vertex specificies an edge. Distance to the edge is used to
692 * compute a coverage. See setVertexEdgeType().
693 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000694 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000695 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000696 kDummyVertexLayoutBit,
697 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000698 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000699 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000700 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000701
702 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000703 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000704 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000705 * can use a different method. Once geometry is specified it can be used for
706 * multiple drawIndexed and drawNonIndexed calls.
707 *
708 * Sometimes it is necessary to perform a draw while upstack code has
709 * already specified geometry that it isn't finished with. There are push
710 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000711 *
712 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
713 * caller's client has already provided vertex data in a format
714 * the time compatible with a GrVertexLayout. The array must contain the
715 * data at set*SourceToArray is called. The source stays in effect for
716 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
717 * again or one of the other two paths is chosen.
718 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000719 * 2. Reserve. This is most useful when the caller has data it must
720 * transform before drawing and is not long-lived. The caller requests
721 * that the draw target make room for some amount of vertex and/or index
722 * data. The target provides ptrs to hold the vertex and/or index data.
723 *
724 * The data is writable up until the next drawIndexed, drawNonIndexed,
725 * or pushGeometrySource At this point the data is frozen and the ptrs
726 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000727 *
728 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
730 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
731 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000732 */
733
734 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000735 * Reserves space for vertices. Draw target will use reserved vertices at
736 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000737 *
738 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000739 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000740 * of vertices to be filled by caller. The next draw will read
741 * these vertices.
742 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000743 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000744 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000745 * GrDrawTarget to decide whether to put data in buffers, to group vertex
746 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000747 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748 * After the next draw or pushGeometrySource the vertices ptr is no longer
749 * valid and the geometry data cannot be further modified. The contents
750 * that were put in the reserved space can be drawn by multiple draws,
751 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000752 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000755 * @param vertices will point to reserved vertex space if vertexCount is
756 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000757 *
758 * @return true if succeeded in allocating space for the vertices and false
759 * if not.
760 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000761 bool reserveVertexSpace(GrVertexLayout vertexLayout,
762 int vertexCount,
763 void** vertices);
764 /**
765 * Reserves space for indices. Draw target will use the reserved indices at
766 * the next indexed draw.
767 *
768 * If succeeds:
769 * if indexCount > 0, *indices will be the array
770 * of indices to be filled by caller. The next draw will read
771 * these indices.
772 *
773 * If a client does not already have a index buffer then this is the
774 * preferred way to allocate index data. It allows the subclass of
775 * GrDrawTarget to decide whether to put data in buffers, to group index
776 * data that uses the same state (e.g. for deferred rendering), etc.
777 *
778 * After the next indexed draw or pushGeometrySource the indices ptr is no
779 * longer valid and the geometry data cannot be further modified. The
780 * contents that were put in the reserved space can be drawn by multiple
781 * draws, however.
782 *
783 * @param indexCount the number of indices to reserve space for. Can be 0.
784 * @param indices will point to reserved index space if indexCount is
785 * non-zero. Illegal to pass NULL if indexCount > 0.
786 */
787
788 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000789 /**
790 * Provides hints to caller about the number of vertices and indices
791 * that can be allocated cheaply. This can be useful if caller is reserving
792 * space but doesn't know exactly how much geometry is needed.
793 *
794 * Also may hint whether the draw target should be flushed first. This is
795 * useful for deferred targets.
796 *
797 * @param vertexLayout layout of vertices caller would like to reserve
798 * @param vertexCount in: hint about how many vertices the caller would
799 * like to allocate.
800 * out: a hint about the number of vertices that can be
801 * allocated cheaply. Negative means no hint.
802 * Ignored if NULL.
803 * @param indexCount in: hint about how many indices the caller would
804 * like to allocate.
805 * out: a hint about the number of indices that can be
806 * allocated cheaply. Negative means no hint.
807 * Ignored if NULL.
808 *
809 * @return true if target should be flushed based on the input values.
810 */
811 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000812 int* vertexCount,
813 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000814
815 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000816 * Sets source of vertex data for the next draw. Array must contain
817 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000818 *
819 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000820 * @param size size of the vertex data.
821 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000822 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000823 void setVertexSourceToArray(GrVertexLayout vertexLayout,
824 const void* vertexArray,
825 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000826
827 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000828 * Sets source of index data for the next indexed draw. Array must contain
829 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000830 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000831 * @param array cpu array containing index data.
832 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000833 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000834 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000835
836 /**
837 * Sets source of vertex data for the next draw. Data does not have to be
838 * in the buffer until drawIndexed or drawNonIndexed.
839 *
840 * @param buffer vertex buffer containing vertex data. Must be
841 * unlocked before draw call.
842 * @param vertexLayout layout of the vertex data in the buffer.
843 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000844 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
845 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000846
847 /**
848 * Sets source of index data for the next indexed draw. Data does not have
849 * to be in the buffer until drawIndexed or drawNonIndexed.
850 *
851 * @param buffer index buffer containing indices. Must be unlocked
852 * before indexed draw call.
853 */
854 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000855
856 /**
857 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
858 * source to reserved, array, or buffer before next draw. May be able to free
859 * up temporary storage allocated by setVertexSourceToArray or
860 * reserveVertexSpace.
861 */
862 void resetVertexSource();
863
864 /**
865 * Resets index source. Indexed Drawing from reset indices is illegal. Set
866 * index source to reserved, array, or buffer before next indexed draw. May
867 * be able to free up temporary storage allocated by setIndexSourceToArray
868 * or reserveIndexSpace.
869 */
870 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000871
872 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000873 * Pushes and resets the vertex/index sources. Any reserved vertex / index
874 * data is finalized (i.e. cannot be updated after the matching pop but can
875 * be drawn from). Must be balanced by a pop.
876 */
877 void pushGeometrySource();
878
879 /**
880 * Pops the vertex / index sources from the matching push.
881 */
882 void popGeometrySource();
883
884 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000885 * Draws indexed geometry using the current state and current vertex / index
886 * sources.
887 *
888 * @param type The type of primitives to draw.
889 * @param startVertex the vertex in the vertex array/buffer corresponding
890 * to index 0
891 * @param startIndex first index to read from index src.
892 * @param vertexCount one greater than the max index.
893 * @param indexCount the number of index elements to read. The index count
894 * is effectively trimmed to the last completely
895 * specified primitive.
896 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000897 void drawIndexed(GrPrimitiveType type,
898 int startVertex,
899 int startIndex,
900 int vertexCount,
901 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000902
903 /**
904 * Draws non-indexed geometry using the current state and current vertex
905 * sources.
906 *
907 * @param type The type of primitives to draw.
908 * @param startVertex the vertex in the vertex array/buffer corresponding
909 * to index 0
910 * @param vertexCount one greater than the max index.
911 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000912 void drawNonIndexed(GrPrimitiveType type,
913 int startVertex,
914 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000915
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000916 /**
917 * Helper function for drawing rects. This does not use the current index
918 * and vertex sources. After returning, the vertex and index sources may
919 * have changed. They should be reestablished before the next drawIndexed
920 * or drawNonIndexed. This cannot be called between reserving and releasing
921 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000922 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000923 * drawNonIndexed.
924 * @param rect the rect to draw
925 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000926 * @param stageEnableBitfield bitmask indicating which stages are enabled.
927 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000928 * @param srcRects specifies rects for stages enabled by stageEnableMask.
929 * if stageEnableMask bit i is 1, srcRects is not NULL,
930 * and srcRects[i] is not NULL, then srcRects[i] will be
931 * used as coordinates for stage i. Otherwise, if stage i
932 * is enabled then rect is used as the coordinates.
933 * @param srcMatrices optional matrices applied to srcRects. If
934 * srcRect[i] is non-NULL and srcMatrices[i] is
935 * non-NULL then srcRect[i] will be transformed by
936 * srcMatrix[i]. srcMatrices can be NULL when no
937 * srcMatrices are desired.
938 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000939 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000940 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000941 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000942 const GrRect* srcRects[],
943 const GrMatrix* srcMatrices[]);
944
945 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000946 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000947 * matrices.
948 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000949 void drawSimpleRect(const GrRect& rect,
950 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000951 StageBitfield stageEnableBitfield) {
952 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000953 }
954
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000955 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000956 * Clear the render target. Ignores the clip and all other draw state
957 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
958 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000959 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000960 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000961
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000962 /**
963 * Returns the maximum number of edges that may be specified in a single
964 * draw call when performing edge antialiasing. This is usually limited
965 * by the number of fragment uniforms which may be uploaded. Must be a
966 * minimum of six, since a triangle's vertices each belong to two boundary
967 * edges which may be distinct.
968 */
969 virtual int getMaxEdges() const { return 6; }
970
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000971 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000972
973 class AutoStateRestore : ::GrNoncopyable {
974 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000975 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000976 AutoStateRestore(GrDrawTarget* target);
977 ~AutoStateRestore();
978
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000979 /**
980 * if this object is already saving state for param target then
981 * this does nothing. Otherise, it restores previously saved state on
982 * previous target (if any) and saves current state on param target.
983 */
984 void set(GrDrawTarget* target);
985
reed@google.comac10a2d2010-12-22 21:39:39 +0000986 private:
987 GrDrawTarget* fDrawTarget;
988 SavedDrawState fDrawState;
989 };
990
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000991 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000992
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000993 class AutoViewMatrixRestore : ::GrNoncopyable {
994 public:
995 AutoViewMatrixRestore() {
996 fDrawTarget = NULL;
997 }
998
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000999 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +00001000 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
1001 GrAssert(NULL != target);
1002 }
1003
1004 void set(GrDrawTarget* target) {
1005 GrAssert(NULL != target);
1006 if (NULL != fDrawTarget) {
1007 fDrawTarget->setViewMatrix(fMatrix);
1008 }
1009 fDrawTarget = target;
1010 fMatrix = target->getViewMatrix();
1011 }
1012
1013 ~AutoViewMatrixRestore() {
1014 if (NULL != fDrawTarget) {
1015 fDrawTarget->setViewMatrix(fMatrix);
1016 }
1017 }
1018
1019 private:
1020 GrDrawTarget* fDrawTarget;
1021 GrMatrix fMatrix;
1022 };
1023
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001024 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001025
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001026 /**
1027 * Sets the view matrix to I and preconcats all stage matrices enabled in
1028 * mask by the view inverse. Destructor undoes these changes.
1029 */
1030 class AutoDeviceCoordDraw : ::GrNoncopyable {
1031 public:
1032 AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask);
1033 ~AutoDeviceCoordDraw();
1034 private:
1035 GrDrawTarget* fDrawTarget;
1036 GrMatrix fViewMatrix;
1037 GrMatrix fSamplerMatrices[kNumStages];
1038 int fStageMask;
1039 };
1040
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001041 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001042
reed@google.comac10a2d2010-12-22 21:39:39 +00001043 class AutoReleaseGeometry : ::GrNoncopyable {
1044 public:
1045 AutoReleaseGeometry(GrDrawTarget* target,
1046 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001047 int vertexCount,
1048 int indexCount);
1049 AutoReleaseGeometry();
1050 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001051 bool set(GrDrawTarget* target,
1052 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001053 int vertexCount,
1054 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001055 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001056 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
1057 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001059 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 }
1061
1062 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001063 void reset();
1064
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 void* fVertices;
1067 void* fIndices;
1068 };
1069
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001070 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001071
1072 class AutoClipRestore : ::GrNoncopyable {
1073 public:
1074 AutoClipRestore(GrDrawTarget* target) {
1075 fTarget = target;
1076 fClip = fTarget->getClip();
1077 }
1078
1079 ~AutoClipRestore() {
1080 fTarget->setClip(fClip);
1081 }
1082 private:
1083 GrDrawTarget* fTarget;
1084 GrClip fClip;
1085 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001086
1087 ////////////////////////////////////////////////////////////////////////////
1088
1089 class AutoGeometryPush : ::GrNoncopyable {
1090 public:
1091 AutoGeometryPush(GrDrawTarget* target) {
1092 GrAssert(NULL != target);
1093 fTarget = target;
1094 target->pushGeometrySource();
1095 }
1096 ~AutoGeometryPush() {
1097 fTarget->popGeometrySource();
1098 }
1099 private:
1100 GrDrawTarget* fTarget;
1101 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
1103 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001104 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001105
reed@google.comac10a2d2010-12-22 21:39:39 +00001106 /**
1107 * Helper function to compute the size of a vertex from a vertex layout
1108 * @return size of a single vertex.
1109 */
1110 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001111
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001112 /**
1113 * Helper function for determining the index of texture coordinates that
1114 * is input for a texture stage. Note that a stage may instead use positions
1115 * as texture coordinates, in which case the result of the function is
1116 * indistinguishable from the case when the stage is disabled.
1117 *
1118 * @param stage the stage to query
1119 * @param vertexLayout layout to query
1120 *
1121 * @return the texture coordinate index or -1 if the stage doesn't use
1122 * separate (non-position) texture coordinates.
1123 */
1124 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001125
1126 /**
1127 * Helper function to compute the offset of texture coordinates in a vertex
1128 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001129 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001130 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001132 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001133
1134 /**
1135 * Helper function to compute the offset of the color in a vertex
1136 * @return offset of color in vertex layout or -1 if the
1137 * layout has no color.
1138 */
1139 static int VertexColorOffset(GrVertexLayout vertexLayout);
1140
bsalomon@google.coma3108262011-10-10 14:08:47 +00001141 /**
1142 * Helper function to compute the offset of the coverage in a vertex
1143 * @return offset of coverage in vertex layout or -1 if the
1144 * layout has no coverage.
1145 */
1146 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
1147
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001148 /**
1149 * Helper function to compute the offset of the edge pts in a vertex
1150 * @return offset of edge in vertex layout or -1 if the
1151 * layout has no edge.
1152 */
1153 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
1154
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001156 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001157 * coordinates of some index.
1158 *
1159 * @param coordIndex the tex coord index to query
1160 * @param vertexLayout layout to query
1161 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001162 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001163 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001164 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001165 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001166 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001167
reed@google.comac10a2d2010-12-22 21:39:39 +00001168 /**
1169 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001170 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001172 * @param stage the stage to query
1173 * @param vertexLayout layout to query
1174 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001175 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001176 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001177 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001178 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001179
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001180 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001181 * Helper function to compute the size of each vertex and the offsets of
1182 * texture coordinates and color. Determines tex coord offsets by tex coord
1183 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001184 * by StageTexCoordVertexLayoutBit.)
1185 *
1186 * @param vertexLayout the layout to query
1187 * @param texCoordOffsetsByIdx after return it is the offset of each
1188 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001189 * index isn't used. (optional)
1190 * @param colorOffset after return it is the offset of the
1191 * color field in each vertex, or -1 if
1192 * there aren't per-vertex colors. (optional)
1193 * @param coverageOffset after return it is the offset of the
1194 * coverage field in each vertex, or -1 if
1195 * there aren't per-vertex coeverages.
1196 * (optional)
1197 * @param edgeOffset after return it is the offset of the
1198 * edge eq field in each vertex, or -1 if
1199 * there aren't per-vertex edge equations.
1200 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001201 * @return size of a single vertex
1202 */
1203 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1204 int texCoordOffsetsByIdx[kMaxTexCoords],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001205 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001206 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001207 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001208
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001209 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001210 * Helper function to compute the size of each vertex and the offsets of
1211 * texture coordinates and color. Determines tex coord offsets by stage
1212 * rather than by index. (Each stage can be mapped to any t.c. index
1213 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001214 * tex coords then that stage's offset will be 0 (positions are always at 0).
1215 *
1216 * @param vertexLayout the layout to query
1217 * @param texCoordOffsetsByStage after return it is the offset of each
1218 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001219 * index isn't used. (optional)
1220 * @param colorOffset after return it is the offset of the
1221 * color field in each vertex, or -1 if
1222 * there aren't per-vertex colors.
1223 * (optional)
1224 * @param coverageOffset after return it is the offset of the
1225 * coverage field in each vertex, or -1 if
1226 * there aren't per-vertex coeverages.
1227 * (optional)
1228 * @param edgeOffset after return it is the offset of the
1229 * edge eq field in each vertex, or -1 if
1230 * there aren't per-vertex edge equations.
1231 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001232 * @return size of a single vertex
1233 */
1234 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1235 int texCoordOffsetsByStage[kNumStages],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001236 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001237 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001238 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001239
1240 /**
1241 * Accessing positions, texture coords, or colors, of a vertex within an
1242 * array is a hassle involving casts and simple math. These helpers exist
1243 * to keep GrDrawTarget clients' code a bit nicer looking.
1244 */
1245
1246 /**
1247 * Gets a pointer to a GrPoint of a vertex's position or texture
1248 * coordinate.
1249 * @param vertices the vetex array
1250 * @param vertexIndex the index of the vertex in the array
1251 * @param vertexSize the size of each vertex in the array
1252 * @param offset the offset in bytes of the vertex component.
1253 * Defaults to zero (corresponding to vertex position)
1254 * @return pointer to the vertex component as a GrPoint
1255 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001256 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001257 int vertexIndex,
1258 int vertexSize,
1259 int offset = 0) {
1260 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001261 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001262 vertexIndex * vertexSize);
1263 }
1264 static const GrPoint* GetVertexPoint(const void* vertices,
1265 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001266 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001267 int offset = 0) {
1268 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001269 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001270 vertexIndex * vertexSize);
1271 }
1272
1273 /**
1274 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1275 * @param vertices the vetex array
1276 * @param vertexIndex the index of the vertex in the array
1277 * @param vertexSize the size of each vertex in the array
1278 * @param offset the offset in bytes of the vertex color
1279 * @return pointer to the vertex component as a GrColor
1280 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001281 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001282 int vertexIndex,
1283 int vertexSize,
1284 int offset) {
1285 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001286 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001287 vertexIndex * vertexSize);
1288 }
1289 static const GrColor* GetVertexColor(const void* vertices,
1290 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001291 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001292 int offset) {
1293 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001294 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001295 vertexIndex * vertexSize);
1296 }
1297
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001298 static void VertexLayoutUnitTest();
1299
reed@google.comac10a2d2010-12-22 21:39:39 +00001300protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +00001301
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001302 // Determines whether it is correct to apply partial pixel coverage
1303 // by multiplying the src color by the fractional coverage.
1304 static bool CanTweakAlphaForCoverage(GrBlendCoeff dstCoeff);
1305
bsalomon@google.com471d4712011-08-23 15:45:25 +00001306 // determines whether HW blending can be disabled or not
1307 static bool CanDisableBlend(GrVertexLayout layout, const DrState& state);
1308
1309 // determines whether HW AA lines can be used or not
1310 static bool CanUseHWAALines(GrVertexLayout layout, const DrState& state);
1311
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001312 enum GeometrySrcType {
1313 kNone_GeometrySrcType, //<! src has not been specified
1314 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1315 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1316 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1317 };
1318
1319 struct GeometrySrcState {
1320 GeometrySrcType fVertexSrc;
1321 union {
1322 // valid if src type is buffer
1323 const GrVertexBuffer* fVertexBuffer;
1324 // valid if src type is reserved or array
1325 int fVertexCount;
1326 };
1327
1328 GeometrySrcType fIndexSrc;
1329 union {
1330 // valid if src type is buffer
1331 const GrIndexBuffer* fIndexBuffer;
1332 // valid if src type is reserved or array
1333 int fIndexCount;
1334 };
1335
1336 GrVertexLayout fVertexLayout;
1337 };
1338
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001339 // given a vertex layout and a draw state, will a stage be used?
1340 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001341 const DrState& state) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001342 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1343 }
1344
1345 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001346 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1347 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001348 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001349
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001350 StageBitfield enabledStages() const {
1351 StageBitfield mask = 0;
1352 for (int s = 0; s < kNumStages; ++s) {
1353 mask |= this->isStageEnabled(s) ? 1 : 0;
1354 }
1355 return mask;
1356 }
1357
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 // Helpers for GrDrawTarget subclasses that won't have private access to
1359 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001360 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001362 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 { return sds.fState; }
1364
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001365 // implemented by subclass to allocate space for reserved geom
1366 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1367 int vertexCount,
1368 void** vertices) = 0;
1369 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1370 // implemented by subclass to handle release of reserved geom space
1371 virtual void releaseReservedVertexSpace() = 0;
1372 virtual void releaseReservedIndexSpace() = 0;
1373 // subclass must consume array contents when set
1374 virtual void onSetVertexSourceToArray(const void* vertexArray,
1375 int vertexCount) = 0;
1376 virtual void onSetIndexSourceToArray(const void* indexArray,
1377 int indexCount) = 0;
1378 // subclass is notified that geom source will be set away from an array
1379 virtual void releaseVertexArray() = 0;
1380 virtual void releaseIndexArray() = 0;
1381 // subclass overrides to be notified just before geo src state
1382 // is pushed/popped.
1383 virtual void geometrySourceWillPush() = 0;
1384 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1385 // subclass called to perform drawing
1386 virtual void onDrawIndexed(GrPrimitiveType type,
1387 int startVertex,
1388 int startIndex,
1389 int vertexCount,
1390 int indexCount) = 0;
1391 virtual void onDrawNonIndexed(GrPrimitiveType type,
1392 int startVertex,
1393 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001394 // subclass overrides to be notified when clip is set. Must call
1395 // INHERITED::clipwillBeSet
1396 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001397
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001398 // Helpers for drawRect, protected so subclasses that override drawRect
1399 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001400 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001401 const GrRect* srcRects[]);
1402
1403 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001404 const GrMatrix* matrix,
1405 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001406 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001407 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001408 void* vertices);
1409
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001410 // accessor for derived classes
1411 const GeometrySrcState& getGeomSrc() const {
1412 return fGeoSrcStateStack.back();
1413 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001414
1415 GrClip fClip;
1416
reed@google.com8195f672011-01-12 18:14:28 +00001417 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001418
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001419 Caps fCaps;
1420
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001421private:
1422 // called when setting a new vert/idx source to unref prev vb/ib
1423 void releasePreviousVertexSource();
1424 void releasePreviousIndexSource();
1425
1426 enum {
1427 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001429 SkSTArray<kPreallocGeoSrcStateStackCnt,
1430 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001431
reed@google.comac10a2d2010-12-22 21:39:39 +00001432};
1433
1434#endif